Skip to content

Commit

Permalink
tweaking tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnRoesler committed Nov 4, 2023
1 parent daf46e8 commit 3dbfb18
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 29 deletions.
12 changes: 7 additions & 5 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
38 changes: 14 additions & 24 deletions scheduler_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gocron

import (
"context"
"testing"
"time"

Expand All @@ -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
Expand Down Expand Up @@ -243,54 +244,42 @@ 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():
}
},
),
nil,
},
{
"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():
}
},
),
Expand All @@ -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),
)
Expand All @@ -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)
})
}
}
Expand Down

0 comments on commit 3dbfb18

Please sign in to comment.