-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmanager_test.go
319 lines (280 loc) · 8.21 KB
/
manager_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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package croner
import (
"fmt"
"testing"
"time"
)
var tmp = [5]string{}
var manager = NewCronManager(CronManagerConfig{true, false, 0, 0})
// reset tmp array items to null string
func resetTmp() {
tmp = [5]string{}
}
// remove all job in manager
func resetRunner() {
if manager.running {
manager.RemoveAll()
manager.Stop()
}
}
// panicJob will panic when running
type PanicJob struct{}
func (j PanicJob) Run() JobRunReturn {
fmt.Println("Start runnning panic Job: ", time.Now().Minute(), ":", time.Now().Second())
panic("hello, i am a panic job")
}
// returnSoonJob will return soon....
type ReturnSoonJob struct{}
func (j ReturnSoonJob) Run() JobRunReturn {
fmt.Println("Start runnning good Job: ", time.Now().Minute(), ":", time.Now().Second())
return JobRunReturn{"Hello , I am a good job", nil}
}
// time5SecJob will return after 5 seconds
type Time3SecJob struct{}
func (j Time3SecJob) Run() JobRunReturn {
fmt.Println("Start runnning 3 sec Job: ", time.Now().Minute(), ":", time.Now().Second())
time.Sleep(3 * time.Second)
fmt.Println("End runnning 3 sec Job: ", time.Now().Minute(), ":", time.Now().Second())
return JobRunReturn{"Hello , I am a timeout job", nil}
}
// hook function when job return. push value in tmp array
func hookAppendResultToTmp(runReturn *JobRunReturnWithEid) {
fmt.Println("Hook running: ", time.Now().Minute(), ":", time.Now().Second())
for i, v := range tmp {
if v == "" {
tmpStr := fmt.Sprintf("%v", runReturn.Value)
tmp[i] = tmpStr
break
}
}
}
// Simple test
func TestRunning(t *testing.T) {
resetRunner()
resetTmp()
if len(jobReturnHooks) == 0 {
OnJobReturn(hookAppendResultToTmp)
}
manager.Start()
fmt.Println("Manager start running: ", time.Now().Minute(), ":", time.Now().Second())
entryId, _ := manager.Add("@every 2s", ReturnSoonJob{}, nil)
// sleep 3 second, tmp length should be 1
time.Sleep(3 * time.Second)
if tmp[1] != "" || tmp[0] == "" {
t.FailNow()
}
// sleep 2 second again, tmp length should be 2
time.Sleep(2 * time.Second)
if tmp[2] != "" || tmp[1] == "" {
t.FailNow()
}
// status should be "IDLE"
if manager.JobMap[entryId].Status() != "IDLE" {
t.FailNow()
}
// successTime should be 2, totalTime should be 2
if manager.JobMap[entryId].SuccessCount != 2 ||
manager.JobMap[entryId].TotalCount != 2 {
t.FailNow()
}
}
// Test Ignore Panic = True, Manager re-execute panic job even it's panic
func TestIgnorePanic(t *testing.T) {
resetRunner()
resetTmp()
if len(jobReturnHooks) == 0 {
OnJobReturn(hookAppendResultToTmp)
}
manager.SetConfig(CronManagerConfig{true, false, 0, 0})
fmt.Println("Manager start running: ", time.Now().Minute(), ":", time.Now().Second())
manager.Start()
entryId, _ := manager.Add("@every 2s", PanicJob{}, nil)
// sleep 5 second, tmp length should be 2, Even this is a panic job
time.Sleep(5 * time.Second)
if tmp[2] != "" || tmp[1] == "" {
t.FailNow()
}
// status should be "Fail"
if manager.JobMap[entryId].Status() != "FAIL" {
t.FailNow()
}
// successTime should be 0, totalTime should be 2
if manager.JobMap[entryId].SuccessCount != 0 ||
manager.JobMap[entryId].TotalCount != 2 {
t.FailNow()
}
}
// Test Ingore Panic = False, Manager won't re-execute panic job
func TestNotIgnorePanic(t *testing.T) {
resetRunner()
resetTmp()
if len(jobReturnHooks) == 0 {
OnJobReturn(hookAppendResultToTmp)
}
manager.SetConfig(CronManagerConfig{false, false, 0, 0})
fmt.Println("Manager start running: ", time.Now().Minute(), ":", time.Now().Second())
manager.Start()
entryId, _ := manager.Add("@every 2s", PanicJob{}, nil)
// sleep 5 second, tmp length should be 1
time.Sleep(5 * time.Second)
if tmp[1] != "" || tmp[0] == "" {
print("Fail: Two job return")
t.FailNow()
}
// status should be "STOP"
if manager.JobMap[entryId].Status() != "STOP" {
print("Fail: Status should be Stop")
t.FailNow()
}
// successTime should be 0, totalTime should be 1
if manager.JobMap[entryId].SuccessCount != 0 ||
manager.JobMap[entryId].TotalCount != 1 {
t.FailNow()
}
}
// Test Pool size = 2
func TestPoolSize(t *testing.T) {
resetRunner()
resetTmp()
// add job only on time
if len(jobReturnHooks) == 0 {
OnJobReturn(hookAppendResultToTmp)
}
manager.SetConfig(CronManagerConfig{false, false, 2, 0})
fmt.Println("Manager start running: ", time.Now().Minute(), ":", time.Now().Second())
manager.Start()
entryId, _ := manager.Add("@every 1s", Time3SecJob{}, nil)
// one running even every 2s
time.Sleep(5 * time.Second)
// 0s-1s idle
// 1s-4s first execution
// 2s-5s second execution
// 3s-4s third block
// 4s-5s third execution
// 5s forth block
if tmp[2] != "" || tmp[1] == "" {
fmt.Print(tmp)
print("Fail: Two job return")
t.FailNow()
}
// status should be "STOP"
if manager.JobMap[entryId].Status() != "RUNNING" {
print("Fail: Status should be running")
t.FailNow()
}
// successTime should be 1, totalTime should be 1
if manager.JobMap[entryId].SuccessCount != 2 ||
manager.JobMap[entryId].TotalCount != 2 {
print("Fail: Status should be running")
t.FailNow()
}
}
//// Test Timeout =2
//func TestTimeOut(t *testing.T) {
// resetRunner()
// resetTmp()
// // add job only on time
// if len(jobReturnHooks) == 0 {
// OnJobReturn(hookAppendResultToTmp)
// }
// manager.SetConfig(CronManagerConfig{true, false, 0, 2})
// fmt.Println("Manager start running: ", time.Now().Minute(), ":", time.Now().Second())
// manager.Start()
//
// entryId, _ := manager.Add("@every 1s", Time3SecJob{}, nil)
// // one running even every 2s
// time.Sleep(5 * time.Second)
// // 0s-1s idle
// // 1s-4s first execution, but timeout in 3s
// // 2s-5s second execution, but timeout in 4s
//
// if tmp[1] != "" || tmp[0] != "" {
// fmt.Print(tmp)
// t.FailNow()
// }
//
// if manager.JobMap[entryId].SuccessCount != 0 ||
// manager.JobMap[entryId].TotalCount != 2 {
// print("Fail: Status should be running")
// t.FailNow()
// }
//}
// Test Only One = True, Each job only execute one time , no matter what schedule is
func TestOnlyOne(t *testing.T) {
resetRunner()
resetTmp()
// add job only on time
if len(jobReturnHooks) == 0 {
OnJobReturn(hookAppendResultToTmp)
}
manager.SetConfig(CronManagerConfig{false, true, 0, 0})
fmt.Println("Manager start running: ", time.Now().Minute(), ":", time.Now().Second())
manager.Start()
entryId, _ := manager.Add("@every 2s", Time3SecJob{}, nil)
// one running even every 2s
time.Sleep(6 * time.Second)
// 0s-2s idle
// 2s-5s first execution
// 4s-5s second execution block
// 5s-6s second execution, but not finish
// 6s third execution block
if tmp[1] != "" || tmp[0] == "" {
print("Fail: Two job return")
t.FailNow()
}
// status should be "STOP"
if manager.JobMap[entryId].Status() != "RUNNING" {
print("Fail: Status should be running")
t.FailNow()
}
// successTime should be 1, totalTime should be 1
if manager.JobMap[entryId].SuccessCount != 1 ||
manager.JobMap[entryId].TotalCount != 1 {
print("Fail: Status should be running")
t.FailNow()
}
// wait all execution done
ticker := time.NewTicker(5 * time.Second)
select {
case <-ticker.C:
return
case <-manager.jobReturnsWithEid:
return
}
}
// Test only one = False, parallel job running
func TestNotOnlyOne(t *testing.T) {
resetRunner()
resetTmp()
// add job only on time
if len(jobReturnHooks) == 0 {
OnJobReturn(hookAppendResultToTmp)
}
manager.SetConfig(CronManagerConfig{false, false, 0, 0})
fmt.Println("Manager start running: ", time.Now().Minute(), ":", time.Now().Second())
manager.Start()
entryId, _ := manager.Add("@every 2s", Time3SecJob{}, nil)
fmt.Println("Start All job: ", time.Now().Minute(), ":", time.Now().Second())
// one running even every 2s
time.Sleep(8 * time.Second)
// 2s-5s first execution
// 4s-7s second execution
// 6s-8s third execution but not finish
// 8s fourth execution start
if tmp[2] != "" || tmp[1] == "" {
fmt.Println(tmp)
fmt.Println("Fail: 2 job done, 1 job")
t.FailNow()
}
// status should be "STOP"
if manager.JobMap[entryId].Status() != "RUNNING" {
fmt.Println("Fail: Status should be running")
t.FailNow()
}
// successTime should be 1, totalTime should be 1
if manager.JobMap[entryId].SuccessCount != 2 ||
manager.JobMap[entryId].TotalCount != 2 {
fmt.Println("Fail: Status should be running")
t.FailNow()
}
}