-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathschedule_test.go
275 lines (229 loc) · 6.61 KB
/
schedule_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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package worker_test
import (
"context"
"sync/atomic"
"testing"
"time"
"github.com/chapsuk/worker"
. "github.com/smartystreets/goconvey/convey"
)
func TestByCustomSchedule(t *testing.T) {
Convey("Given target int == 5, decrement job and custom schedule (until int > 0)", t, func() {
var (
i int32 = 5
res = make(chan struct{})
job = func(ctx context.Context) {
atomic.AddInt32(&i, -1)
}
)
schedule := func(ctx context.Context, j worker.Job) worker.Job {
return func(ctx context.Context) {
for atomic.LoadInt32(&i) > 0 {
j(ctx)
}
res <- struct{}{}
}
}
Convey("When run worker", func() {
go worker.New(job).
BySchedule(schedule).
Run(context.Background())
Convey("Job should be executed 5 times", func() {
So(readFromChannelWithTimeout(res), ShouldBeTrue)
So(atomic.LoadInt32(&i), ShouldEqual, 0)
})
})
})
}
func TestByTimer(t *testing.T) {
Convey("Given job who send to result channel execution time and sleep for 1s", t, func() {
res := make(chan time.Time)
job := createWriterJob(time.Second, res)
Convey("When create worker and run with 1s timer", func() {
wrk := worker.
New(job).
ByTimer(time.Second)
ctx, cancel := context.WithCancel(context.Background())
go wrk.Run(ctx)
expectedNextExecutionTime := time.Now().Add(time.Second)
Convey("job should be executed after 1s from previous run", func() {
timer := time.NewTimer(2 * time.Second)
defer timer.Stop()
for i := 0; i < 3; i++ {
select {
case r := <-res:
So(int64(expectedNextExecutionTime.Sub(r).Seconds()), ShouldEqual, 0)
expectedNextExecutionTime = r.Add(time.Second)
timer.Reset(2 * time.Second)
case <-timer.C:
So(false, ShouldBeTrue)
}
}
})
Convey("When cancel context", func() {
cancel()
Convey("job execution should be stopped", func() {
timer := time.NewTimer(2 * time.Second)
defer timer.Stop()
select {
case <-res:
So(false, ShouldBeTrue)
case <-timer.C:
So(true, ShouldBeTrue)
}
})
})
})
})
}
func TestByTicker(t *testing.T) {
Convey("Given job who send to result channel execution time and sleep for 1s", t, func() {
res := make(chan time.Time)
job := createWriterJob(time.Second, res)
Convey("When create worker and run with 1s ticker", func() {
wrk := worker.
New(job).
ByTicker(time.Second)
ctx, cancel := context.WithCancel(context.Background())
go wrk.Run(ctx)
expectedNextExecutionTime := time.Now().Add(time.Second)
Convey("job should be executed every 1s", func() {
timer := time.NewTimer(2 * time.Second)
defer timer.Stop()
for i := 0; i < 3; i++ {
select {
case r := <-res:
So(int64(expectedNextExecutionTime.Sub(r).Seconds()), ShouldEqual, 0)
expectedNextExecutionTime = r.Add(time.Second)
timer.Reset(2 * time.Second)
case <-timer.C:
So(false, ShouldBeTrue)
}
}
})
Convey("When cancel context", func() {
cancel()
Convey("job execution should be stopped", func() {
timer := time.NewTimer(time.Second)
defer timer.Stop()
select {
case <-res:
So(false, ShouldBeTrue)
case <-timer.C:
So(true, ShouldBeTrue)
}
})
})
})
})
Convey("Given job who send to channels start/stop signals, blocking with context", t, func() {
var (
start = make(chan struct{})
stop = make(chan struct{})
complete = make(chan struct{})
)
job := func(ctx context.Context) {
start <- struct{}{}
<-ctx.Done()
stop <- struct{}{}
}
Convey("When run with ticker", func() {
wrk := worker.New(job).ByTicker(time.Millisecond)
ctx, cancel := context.WithCancel(context.Background())
go func() {
wrk.Run(ctx)
complete <- struct{}{}
}()
So(readFromChannelWithTimeout(start), ShouldBeTrue)
Convey("Cancel context should stop job on next run (context check prioriity)", func() {
cancel()
So(readFromChannelWithTimeout(stop), ShouldBeTrue)
So(readFromChannelWithTimeout(complete), ShouldBeTrue)
})
})
})
Convey("Given job which send stop start events to channels", t, func() {
var (
start = make(chan struct{})
stop = make(chan struct{})
complete = make(chan struct{})
)
job := func(ctx context.Context) {
start <- struct{}{}
stop <- struct{}{}
}
Convey("When start job with ticker, set immediiatly for run on start", func() {
wrk := worker.
New(job).
ByTicker(time.Minute).
SetImmediately(true)
ctx, cancel := context.WithCancel(context.Background())
go func() {
wrk.Run(ctx)
complete <- struct{}{}
}()
Convey("Job should execute, cancel context should stop worker", func() {
So(readFromChannelWithTimeout(start), ShouldBeTrue)
So(readFromChannelWithTimeout(stop), ShouldBeTrue)
// skip context check priiority
<-time.Tick(100 * time.Millisecond)
cancel()
So(readFromChannelWithTimeout(complete), ShouldBeTrue)
})
})
})
}
func TestByCronSchedule(t *testing.T) {
Convey("Given job who send to result channel execution time and sleep for 1s", t, func() {
res := make(chan time.Time)
job := createWriterJob(time.Microsecond, res)
Convey("When create worker with incorrect cron spec should panic", func() {
So(func() { worker.New(job).ByCronSpec("завтра") }, ShouldPanic)
So(func() { worker.New(job).ByCronSpec("@today") }, ShouldPanic)
So(func() { worker.New(job).ByCronSpec("*") }, ShouldPanic)
})
Convey("When create worker and run with 1s cron schedule", func() {
wrk := worker.
New(job).
ByCronSpec("@every 1s")
ctx, cancel := context.WithCancel(context.Background())
go wrk.Run(ctx)
expectedNextExecutionTime := time.Now().Add(time.Second)
Convey("job should be executed every 1s", func() {
timer := time.NewTimer(2 * time.Second)
defer timer.Stop()
for i := 0; i < 3; i++ {
select {
case r := <-res:
So(int64(expectedNextExecutionTime.Sub(r).Seconds()), ShouldEqual, 0)
expectedNextExecutionTime = r.Add(time.Second)
timer.Reset(2 * time.Second)
case <-timer.C:
So(false, ShouldBeTrue)
}
}
})
Convey("When cancel context", func() {
cancel()
Convey("job execution should be stopped", func() {
timer := time.NewTimer(time.Second)
defer timer.Stop()
select {
case <-res:
So(false, ShouldBeTrue)
case <-timer.C:
So(true, ShouldBeTrue)
}
})
})
})
})
}
func createWriterJob(sleep time.Duration, ch chan time.Time) worker.Job {
return func(ctx context.Context) {
select {
case ch <- time.Now():
case <-ctx.Done():
}
}
}