/
job_kicker_test.go
193 lines (171 loc) · 6.16 KB
/
job_kicker_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
package jobkicker
import (
"fmt"
"testing"
"time"
)
func testingTask1() {
println("Hello from job 1")
}
func testingTaskWithParams(name string, age int) {
fmt.Printf("hello %s, your age is %d\n", name, age)
}
func TestNewScheduler(t *testing.T) {
jk := NewScheduler(nil, nil)
jk.jobQueue.RLock()
pendingJobsSize := len(jk.jobQueue.PendingJobs)
doneJobsSize := len(jk.jobQueue.DoneJobs)
if pendingJobsSize != 0 {
t.Errorf("newly intialized jobkicker's pendingjobs should be empty but it's size %d", pendingJobsSize)
}
if doneJobsSize != 0 {
t.Errorf("newly intialized jobkicker's donejobs should be empty but it's size %d", doneJobsSize)
}
t.Log("newly intialized jobkicker's jobqueue is empty passed")
jk.jobQueue.RUnlock()
}
func TestKickOnceAfter(t *testing.T) {
jk := NewScheduler(nil, nil)
delay := time.Date(0, 0, 0, 0, 0, 3, 0, time.UTC)
jk.KickOnceAfter(delay, testingTask1)
jk.jobQueue.RLock()
pendingJobsSize := len(jk.jobQueue.PendingJobs)
doneJobsSize := len(jk.jobQueue.DoneJobs)
if pendingJobsSize != 1 {
t.Errorf("jobkicker's pendingjobs should have size %d but found it's size %d", 1, pendingJobsSize)
}
if doneJobsSize != 0 {
t.Errorf("jobkicker's donejobs should have size %d but found it's size %d", 0, doneJobsSize)
}
jk.jobQueue.RUnlock()
t.Log("scheduling new job passed")
time.Sleep(4 * time.Second)
jk.jobQueue.RLock()
pendingJobsSize = len(jk.jobQueue.PendingJobs)
doneJobsSize = len(jk.jobQueue.DoneJobs)
if pendingJobsSize != 0 {
t.Errorf("jobkicker's pendingjobs should have size %d but found it's size %d", 0, pendingJobsSize)
}
if doneJobsSize != 1 {
t.Errorf("jobkicker's donejobs should have size %d but found it's size %d", 1, doneJobsSize)
}
jk.jobQueue.RUnlock()
t.Log("finishing scheduled job passed")
}
func TestKickOnceAt(t *testing.T) {
jk := NewScheduler(nil, nil)
runAt := time.Now().Add(3 * time.Second)
jk.KickOnceAt(runAt, testingTask1)
jk.jobQueue.RLock()
pendingJobsSize := len(jk.jobQueue.PendingJobs)
doneJobsSize := len(jk.jobQueue.DoneJobs)
if pendingJobsSize != 1 {
t.Errorf("jobkicker's pendingjobs should have size %d but found it's size %d", 1, pendingJobsSize)
}
if doneJobsSize != 0 {
t.Errorf("jobkicker's donejobs should have size %d but found it's size %d", 0, doneJobsSize)
}
jk.jobQueue.RUnlock()
t.Log("scheduling new job passed")
time.Sleep(4 * time.Second)
jk.jobQueue.RLock()
pendingJobsSize = len(jk.jobQueue.PendingJobs)
doneJobsSize = len(jk.jobQueue.DoneJobs)
if pendingJobsSize != 0 {
t.Errorf("jobkicker's pendingjobs should have size %d but found it's size %d", 0, pendingJobsSize)
}
if doneJobsSize != 1 {
t.Errorf("jobkicker's donejobs should have size %d but found it's size %d", 1, doneJobsSize)
}
jk.jobQueue.RUnlock()
t.Log("finishing scheduled job passed")
}
func TestKickPeriodicallyEvery(t *testing.T) {
jk := NewScheduler(nil, nil)
delay := time.Date(0, 0, 0, 0, 0, 3, 0, time.UTC)
jobID := jk.KickPeriodicallyEvery(delay, testingTask1)
jk.jobQueue.RLock()
pendingJobsSize := len(jk.jobQueue.PendingJobs)
doneJobsSize := len(jk.jobQueue.DoneJobs)
if pendingJobsSize != 1 {
t.Errorf("jobkicker's pendingjobs should have size %d but found it's size %d", 1, pendingJobsSize)
}
if doneJobsSize != 0 {
t.Errorf("jobkicker's donejobs should have size %d but found it's size %d", 0, doneJobsSize)
}
jk.jobQueue.RUnlock()
t.Log("scheduling new job passed")
time.Sleep(4 * time.Second)
jk.jobQueue.RLock()
pendingJobsSize = len(jk.jobQueue.PendingJobs)
doneJobsSize = len(jk.jobQueue.DoneJobs)
if pendingJobsSize != 1 {
t.Errorf("jobkicker's pendingjobs should have size %d but found it's size %d", 1, pendingJobsSize)
}
if doneJobsSize != 1 {
t.Errorf("jobkicker's donejobs should have size %d but found it's size %d", 1, doneJobsSize)
}
firstFinishTime := jk.jobQueue.DoneJobs[jobID]
jk.jobQueue.RUnlock()
time.Sleep(4 * time.Second)
jk.jobQueue.RLock()
pendingJobsSize = len(jk.jobQueue.PendingJobs)
doneJobsSize = len(jk.jobQueue.DoneJobs)
if pendingJobsSize != 1 {
t.Errorf("jobkicker's pendingjobs should have size %d but found it's size %d", 1, pendingJobsSize)
}
if doneJobsSize != 1 {
t.Errorf("jobkicker's donejobs should have size %d but found it's size %d", 1, doneJobsSize)
}
if firstFinishTime.Equal(jk.jobQueue.DoneJobs[jobID]) {
t.Errorf("Job's first running time and second running time are equal first:%v and second: %v", firstFinishTime, jk.jobQueue.DoneJobs[jobID])
}
jk.jobQueue.RUnlock()
err := jk.CancelJob(jobID)
if err != nil {
t.Errorf("can't cancel job due to error: %s", err.Error())
}
// a little delay before locking the jobQueue
// to allow locking it from cancel side to delete
// it from the pending jobs map
time.Sleep(time.Second)
jk.jobQueue.RLock()
pendingJobsSize = len(jk.jobQueue.PendingJobs)
doneJobsSize = len(jk.jobQueue.DoneJobs)
if pendingJobsSize != 0 {
t.Errorf("jobkicker's pendingjobs should have size %d but found it's size %d", 0, pendingJobsSize)
}
if doneJobsSize != 1 {
t.Errorf("jobkicker's donejobs should have size %d but found it's size %d", 1, doneJobsSize)
}
jk.jobQueue.RUnlock()
t.Log("finishing scheduled job passed")
}
func TestKickWithParams(t *testing.T) {
jk := NewScheduler(nil, nil)
delay := time.Date(0, 0, 0, 0, 0, 3, 0, time.UTC)
jk.KickOnceAfter(delay, testingTaskWithParams, "Mohab", 25)
jk.jobQueue.RLock()
pendingJobsSize := len(jk.jobQueue.PendingJobs)
doneJobsSize := len(jk.jobQueue.DoneJobs)
if pendingJobsSize != 1 {
t.Errorf("jobkicker's pendingjobs should have size %d but found it's size %d", 1, pendingJobsSize)
}
if doneJobsSize != 0 {
t.Errorf("jobkicker's donejobs should have size %d but found it's size %d", 0, doneJobsSize)
}
jk.jobQueue.RUnlock()
t.Log("scheduling new job with arguments passed")
time.Sleep(4 * time.Second)
jk.jobQueue.RLock()
pendingJobsSize = len(jk.jobQueue.PendingJobs)
doneJobsSize = len(jk.jobQueue.DoneJobs)
if pendingJobsSize != 0 {
t.Errorf("jobkicker's pendingjobs should have size %d but found it's size %d", 0, pendingJobsSize)
}
if doneJobsSize != 1 {
t.Errorf("jobkicker's donejobs should have size %d but found it's size %d", 1, doneJobsSize)
}
jk.jobQueue.RUnlock()
t.Log("finishing scheduled job passed")
}