2021-08-17 01:12:07 +00:00
|
|
|
package scheduler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-03-16 13:33:15 +00:00
|
|
|
"errors"
|
2021-08-17 01:12:07 +00:00
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2021-09-29 23:58:10 +00:00
|
|
|
var jobInterval = time.Second
|
|
|
|
|
|
|
|
func Test_ScheduledJobRuns(t *testing.T) {
|
2021-08-17 01:12:07 +00:00
|
|
|
s := NewScheduler(context.Background())
|
2021-09-29 23:58:10 +00:00
|
|
|
defer s.Shutdown()
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2*jobInterval)
|
|
|
|
|
|
|
|
var workDone bool
|
|
|
|
s.StartJobEvery(jobInterval, func() error {
|
|
|
|
workDone = true
|
2021-08-17 01:12:07 +00:00
|
|
|
|
2021-09-29 23:58:10 +00:00
|
|
|
cancel()
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
<-ctx.Done()
|
|
|
|
assert.True(t, workDone, "value should been set in the job")
|
2021-08-17 01:12:07 +00:00
|
|
|
}
|
|
|
|
|
2021-09-29 23:58:10 +00:00
|
|
|
func Test_JobCanBeStopped(t *testing.T) {
|
|
|
|
s := NewScheduler(context.Background())
|
|
|
|
defer s.Shutdown()
|
2021-08-17 01:12:07 +00:00
|
|
|
|
2021-09-29 23:58:10 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2*jobInterval)
|
|
|
|
|
|
|
|
var workDone bool
|
|
|
|
jobID := s.StartJobEvery(jobInterval, func() error {
|
|
|
|
workDone = true
|
|
|
|
|
|
|
|
cancel()
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
s.StopJob(jobID)
|
2021-08-17 01:12:07 +00:00
|
|
|
|
2021-09-29 23:58:10 +00:00
|
|
|
<-ctx.Done()
|
|
|
|
assert.False(t, workDone, "job shouldn't had a chance to run")
|
2021-08-17 01:12:07 +00:00
|
|
|
}
|
|
|
|
|
2021-09-29 23:58:10 +00:00
|
|
|
func Test_JobShouldStop_UponError(t *testing.T) {
|
2021-08-17 01:12:07 +00:00
|
|
|
s := NewScheduler(context.Background())
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
2021-09-29 23:58:10 +00:00
|
|
|
var acc int
|
2022-03-15 12:52:58 +00:00
|
|
|
ch := make(chan struct{})
|
2021-09-29 23:58:10 +00:00
|
|
|
s.StartJobEvery(jobInterval, func() error {
|
|
|
|
acc++
|
2022-03-15 12:52:58 +00:00
|
|
|
close(ch)
|
2021-09-29 23:58:10 +00:00
|
|
|
return fmt.Errorf("failed")
|
|
|
|
})
|
|
|
|
|
|
|
|
<-time.After(3 * jobInterval)
|
2022-03-15 12:52:58 +00:00
|
|
|
<-ch
|
2021-09-29 23:58:10 +00:00
|
|
|
assert.Equal(t, 1, acc, "job stop after the first run because it returns an error")
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_CanTerminateAllJobs_ByShuttingDownScheduler(t *testing.T) {
|
|
|
|
s := NewScheduler(context.Background())
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2*jobInterval)
|
2021-08-17 01:12:07 +00:00
|
|
|
|
|
|
|
var workDone bool
|
2021-09-29 23:58:10 +00:00
|
|
|
s.StartJobEvery(jobInterval, func() error {
|
2021-08-17 01:12:07 +00:00
|
|
|
workDone = true
|
|
|
|
|
|
|
|
cancel()
|
2021-09-29 23:58:10 +00:00
|
|
|
return nil
|
2021-08-17 01:12:07 +00:00
|
|
|
})
|
|
|
|
|
2021-09-29 23:58:10 +00:00
|
|
|
s.Shutdown()
|
|
|
|
|
2021-08-17 01:12:07 +00:00
|
|
|
<-ctx.Done()
|
2021-09-29 23:58:10 +00:00
|
|
|
assert.False(t, workDone, "job shouldn't had a chance to run")
|
|
|
|
}
|
2021-08-17 01:12:07 +00:00
|
|
|
|
2021-09-29 23:58:10 +00:00
|
|
|
func Test_CanTerminateAllJobs_ByCancellingParentContext(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2*jobInterval)
|
|
|
|
s := NewScheduler(ctx)
|
|
|
|
|
|
|
|
var workDone bool
|
|
|
|
s.StartJobEvery(jobInterval, func() error {
|
|
|
|
workDone = true
|
|
|
|
|
|
|
|
cancel()
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
cancel()
|
|
|
|
|
|
|
|
<-ctx.Done()
|
|
|
|
assert.False(t, workDone, "job shouldn't had a chance to run")
|
2021-08-17 01:12:07 +00:00
|
|
|
}
|
2022-03-16 13:33:15 +00:00
|
|
|
|
|
|
|
func Test_StartJobEvery_Concurrently(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2*jobInterval)
|
|
|
|
s := NewScheduler(ctx)
|
|
|
|
|
|
|
|
f := func() error {
|
|
|
|
return errors.New("error")
|
|
|
|
}
|
|
|
|
|
|
|
|
go s.StartJobEvery(jobInterval, f)
|
|
|
|
s.StartJobEvery(jobInterval, f)
|
|
|
|
|
|
|
|
cancel()
|
|
|
|
|
|
|
|
<-ctx.Done()
|
|
|
|
}
|