1
1
package cron
2
2
3
3
import (
4
+ "context"
4
5
"io/ioutil"
5
6
"log"
6
7
"reflect"
@@ -11,7 +12,7 @@ import (
11
12
12
13
func appendingJob (slice * []int , value int ) Job {
13
14
var m sync.Mutex
14
- return FuncJob (func () {
15
+ return FuncJob (func (ctx context. Context ) {
15
16
m .Lock ()
16
17
* slice = append (* slice , value )
17
18
m .Unlock ()
@@ -20,9 +21,9 @@ func appendingJob(slice *[]int, value int) Job {
20
21
21
22
func appendingWrapper (slice * []int , value int ) JobWrapper {
22
23
return func (j Job ) Job {
23
- return FuncJob (func () {
24
- appendingJob (slice , value ).Run ()
25
- j .Run ()
24
+ return FuncJob (func (ctx context. Context ) {
25
+ appendingJob (slice , value ).Run (ctx )
26
+ j .Run (ctx )
26
27
})
27
28
}
28
29
}
@@ -35,14 +36,14 @@ func TestChain(t *testing.T) {
35
36
append3 = appendingWrapper (& nums , 3 )
36
37
append4 = appendingJob (& nums , 4 )
37
38
)
38
- NewChain (append1 , append2 , append3 ).Then (append4 ).Run ()
39
+ NewChain (append1 , append2 , append3 ).Then (append4 ).Run (context . Background () )
39
40
if ! reflect .DeepEqual (nums , []int {1 , 2 , 3 , 4 }) {
40
41
t .Error ("unexpected order of calls:" , nums )
41
42
}
42
43
}
43
44
44
45
func TestChainRecover (t * testing.T ) {
45
- panickingJob := FuncJob (func () {
46
+ panickingJob := FuncJob (func (ctx context. Context ) {
46
47
panic ("panickingJob panics" )
47
48
})
48
49
@@ -53,19 +54,19 @@ func TestChainRecover(t *testing.T) {
53
54
}
54
55
}()
55
56
NewChain ().Then (panickingJob ).
56
- Run ()
57
+ Run (context . Background () )
57
58
})
58
59
59
60
t .Run ("Recovering JobWrapper recovers" , func (t * testing.T ) {
60
61
NewChain (Recover (PrintfLogger (log .New (ioutil .Discard , "" , 0 )))).
61
62
Then (panickingJob ).
62
- Run ()
63
+ Run (context . Background () )
63
64
})
64
65
65
66
t .Run ("composed with the *IfStillRunning wrappers" , func (t * testing.T ) {
66
67
NewChain (Recover (PrintfLogger (log .New (ioutil .Discard , "" , 0 )))).
67
68
Then (panickingJob ).
68
- Run ()
69
+ Run (context . Background () )
69
70
})
70
71
}
71
72
@@ -76,7 +77,7 @@ type countJob struct {
76
77
delay time.Duration
77
78
}
78
79
79
- func (j * countJob ) Run () {
80
+ func (j * countJob ) Run (context. Context ) {
80
81
j .m .Lock ()
81
82
j .started ++
82
83
j .m .Unlock ()
@@ -103,7 +104,7 @@ func TestChainDelayIfStillRunning(t *testing.T) {
103
104
t .Run ("runs immediately" , func (t * testing.T ) {
104
105
var j countJob
105
106
wrappedJob := NewChain (DelayIfStillRunning (DiscardLogger )).Then (& j )
106
- go wrappedJob .Run ()
107
+ go wrappedJob .Run (context . Background () )
107
108
time .Sleep (2 * time .Millisecond ) // Give the job 2ms to complete.
108
109
if c := j .Done (); c != 1 {
109
110
t .Errorf ("expected job run once, immediately, got %d" , c )
@@ -114,9 +115,9 @@ func TestChainDelayIfStillRunning(t *testing.T) {
114
115
var j countJob
115
116
wrappedJob := NewChain (DelayIfStillRunning (DiscardLogger )).Then (& j )
116
117
go func () {
117
- go wrappedJob .Run ()
118
+ go wrappedJob .Run (context . Background () )
118
119
time .Sleep (time .Millisecond )
119
- go wrappedJob .Run ()
120
+ go wrappedJob .Run (context . Background () )
120
121
}()
121
122
time .Sleep (3 * time .Millisecond ) // Give both jobs 3ms to complete.
122
123
if c := j .Done (); c != 2 {
@@ -129,9 +130,9 @@ func TestChainDelayIfStillRunning(t *testing.T) {
129
130
j .delay = 10 * time .Millisecond
130
131
wrappedJob := NewChain (DelayIfStillRunning (DiscardLogger )).Then (& j )
131
132
go func () {
132
- go wrappedJob .Run ()
133
+ go wrappedJob .Run (context . Background () )
133
134
time .Sleep (time .Millisecond )
134
- go wrappedJob .Run ()
135
+ go wrappedJob .Run (context . Background () )
135
136
}()
136
137
137
138
// After 5ms, the first job is still in progress, and the second job was
@@ -157,7 +158,7 @@ func TestChainSkipIfStillRunning(t *testing.T) {
157
158
t .Run ("runs immediately" , func (t * testing.T ) {
158
159
var j countJob
159
160
wrappedJob := NewChain (SkipIfStillRunning (DiscardLogger )).Then (& j )
160
- go wrappedJob .Run ()
161
+ go wrappedJob .Run (context . Background () )
161
162
time .Sleep (2 * time .Millisecond ) // Give the job 2ms to complete.
162
163
if c := j .Done (); c != 1 {
163
164
t .Errorf ("expected job run once, immediately, got %d" , c )
@@ -168,9 +169,9 @@ func TestChainSkipIfStillRunning(t *testing.T) {
168
169
var j countJob
169
170
wrappedJob := NewChain (SkipIfStillRunning (DiscardLogger )).Then (& j )
170
171
go func () {
171
- go wrappedJob .Run ()
172
+ go wrappedJob .Run (context . Background () )
172
173
time .Sleep (time .Millisecond )
173
- go wrappedJob .Run ()
174
+ go wrappedJob .Run (context . Background () )
174
175
}()
175
176
time .Sleep (3 * time .Millisecond ) // Give both jobs 3ms to complete.
176
177
if c := j .Done (); c != 2 {
@@ -183,9 +184,9 @@ func TestChainSkipIfStillRunning(t *testing.T) {
183
184
j .delay = 10 * time .Millisecond
184
185
wrappedJob := NewChain (SkipIfStillRunning (DiscardLogger )).Then (& j )
185
186
go func () {
186
- go wrappedJob .Run ()
187
+ go wrappedJob .Run (context . Background () )
187
188
time .Sleep (time .Millisecond )
188
- go wrappedJob .Run ()
189
+ go wrappedJob .Run (context . Background () )
189
190
}()
190
191
191
192
// After 5ms, the first job is still in progress, and the second job was
@@ -209,7 +210,7 @@ func TestChainSkipIfStillRunning(t *testing.T) {
209
210
j .delay = 10 * time .Millisecond
210
211
wrappedJob := NewChain (SkipIfStillRunning (DiscardLogger )).Then (& j )
211
212
for i := 0 ; i < 11 ; i ++ {
212
- go wrappedJob .Run ()
213
+ go wrappedJob .Run (context . Background () )
213
214
}
214
215
time .Sleep (200 * time .Millisecond )
215
216
done := j .Done ()
0 commit comments