package scheduler
import (
"context"
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
var jobInterval = time.Second
func Test_ScheduledJobRuns(t *testing.T) {
s := NewScheduler(context.Background())
defer s.Shutdown()
ctx, cancel := context.WithTimeout(context.Background(), 2*jobInterval)
var workDone bool
s.StartJobEvery(jobInterval, func() error {
workDone = true
cancel()
return nil
})
<-ctx.Done()
assert.True(t, workDone, "value should been set in the job")
}
func Test_JobCanBeStopped(t *testing.T) {
jobID := s.StartJobEvery(jobInterval, func() error {
s.StopJob(jobID)
assert.False(t, workDone, "job shouldn't had a chance to run")
func Test_JobShouldStop_UponError(t *testing.T) {
var acc int
acc++
return fmt.Errorf("failed")
<-time.After(3 * jobInterval)
assert.Equal(t, 1, acc, "job stop after the first run because it returns an error")
func Test_CanTerminateAllJobs_ByShuttingDownScheduler(t *testing.T) {
s.Shutdown()
func Test_CanTerminateAllJobs_ByCancellingParentContext(t *testing.T) {
s := NewScheduler(ctx)