-
Notifications
You must be signed in to change notification settings - Fork 0
/
cron_test.go
74 lines (64 loc) · 1.38 KB
/
cron_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package cron_test
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/troydai/cron"
)
func TestCron(t *testing.T) {
t.Parallel()
testcases := []struct {
name string
repeat int
interval time.Duration
timeout time.Duration
assertCount func(t *testing.T, count int)
}{
{
name: "run once",
interval: 50 * time.Millisecond,
repeat: 1,
timeout: time.Second,
assertCount: func(t *testing.T, count int) {
assert.Equal(t, 1, count)
},
},
{
name: "run twice",
interval: 50 * time.Millisecond,
repeat: 2,
timeout: time.Second,
assertCount: func(t *testing.T, count int) {
assert.Equal(t, 2, count)
},
},
{
name: "run till timeout",
interval: 50 * time.Millisecond,
repeat: 9999,
timeout: time.Second,
assertCount: func(t *testing.T, count int) {
assert.GreaterOrEqual(t, count, 19)
},
},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), tc.timeout)
defer cancel()
var counter int
job := func(ctx context.Context) bool {
if counter >= tc.repeat {
return false
}
counter++
return true
}
term, err := cron.Start(ctx, job, cron.WithInterval(tc.interval))
assert.NoError(t, err)
<-term
tc.assertCount(t, counter)
})
}
}