diff --git a/example_test.go b/example_test.go index c8b14b0a..099825cd 100644 --- a/example_test.go +++ b/example_test.go @@ -250,6 +250,8 @@ func ExampleScheduler_RemoveByTags() { ) fmt.Println(len(s.Jobs())) + time.Sleep(20 * time.Millisecond) + s.RemoveByTags("tag1", "tag2") fmt.Println(len(s.Jobs())) @@ -274,7 +276,7 @@ func ExampleScheduler_RemoveJob() { _ = s.RemoveJob(j.ID()) - time.Sleep(5 * time.Millisecond) + time.Sleep(20 * time.Millisecond) fmt.Println(len(s.Jobs())) // Output: // 1 @@ -581,12 +583,12 @@ func ExampleWithStartAt() { ), ) s.Start() - time.Sleep(10 * time.Millisecond) - defer func() { - _ = s.StopJobs() - }() + time.Sleep(20 * time.Millisecond) + next, _ := j.NextRun() fmt.Println(next) + + _ = s.StopJobs() // Output: // 9999-09-09 09:09:09.000000009 +0000 UTC } diff --git a/scheduler_test.go b/scheduler_test.go index ff6842c1..43f0cf9f 100644 --- a/scheduler_test.go +++ b/scheduler_test.go @@ -1,6 +1,7 @@ package gocron import ( + "context" "testing" "time" @@ -11,8 +12,8 @@ import ( func TestScheduler_OneSecond_NoOptions(t *testing.T) { defer goleak.VerifyNone(t) - cronNoOptionsCh := make(chan struct{}) - durationNoOptionsCh := make(chan struct{}) + cronNoOptionsCh := make(chan struct{}, 10) + durationNoOptionsCh := make(chan struct{}, 10) tests := []struct { name string @@ -243,33 +244,27 @@ func TestScheduler_Update(t *testing.T) { func TestScheduler_StopTimeout(t *testing.T) { defer goleak.VerifyNone(t) - durationCh := make(chan struct{}, 10) - durationSingletonCh := make(chan struct{}, 10) - testDone := make(chan struct{}) + var ( + testDoneCtx context.Context + cancel context.CancelFunc + ) tests := []struct { name string - ch chan struct{} jd JobDefinition tsk Task opts []JobOption }{ { "duration", - durationCh, DurationJob( - time.Millisecond * 500, + time.Millisecond * 100, ), NewTask( func() { select { case <-time.After(10 * time.Second): - case <-testDone: - } - - select { - case durationCh <- struct{}{}: - default: + case <-testDoneCtx.Done(): } }, ), @@ -277,20 +272,14 @@ func TestScheduler_StopTimeout(t *testing.T) { }, { "duration singleton", - durationSingletonCh, DurationJob( - time.Millisecond * 500, + time.Millisecond * 100, ), NewTask( func() { select { case <-time.After(10 * time.Second): - case <-testDone: - } - - select { - case durationSingletonCh <- struct{}{}: - default: + case <-testDoneCtx.Done(): } }, ), @@ -300,6 +289,7 @@ func TestScheduler_StopTimeout(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { + testDoneCtx, cancel = context.WithCancel(context.Background()) s, err := NewScheduler( WithStopTimeout(time.Second * 1), ) @@ -312,8 +302,8 @@ func TestScheduler_StopTimeout(t *testing.T) { time.Sleep(time.Second) err = s.Shutdown() assert.ErrorIs(t, err, ErrStopTimedOut) - testDone <- struct{}{} - time.Sleep(50 * time.Millisecond) + cancel() + time.Sleep(200 * time.Millisecond) }) } }