-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler_test.go
196 lines (175 loc) · 4.85 KB
/
scheduler_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
package smallben
import (
"github.com/robfig/cron/v3"
"sync"
"testing"
"time"
)
type SchedulerTestSuite struct {
scheduler scheduler
jobs []JobWithSchedule
}
var lockMap = new(LockMap)
type SchedulerTestCronJob struct{}
func (s *SchedulerTestCronJob) Run(input CronJobInput) {
lockMap.lock.Lock()
defer lockMap.lock.Unlock()
lockMap.counter += input.OtherInputs["counter"].(int)
}
type LockMap struct {
counter int
lock sync.Mutex
}
func (s *SchedulerTestSuite) TestAdd(t *testing.T) {
lenOfJobsBeforeAnything := len(s.scheduler.cron.Entries())
// add the jobs
s.scheduler.AddJobs(s.jobs)
// make sure they have been added
lenOfJobsAfterFirstAdd := len(s.scheduler.cron.Entries())
if lenOfJobsAfterFirstAdd != lenOfJobsBeforeAnything+len(s.jobs) {
t.Errorf("Fail to add jobs. Got: %d Expected: %d\n", lenOfJobsAfterFirstAdd, lenOfJobsBeforeAnything+len(s.jobs))
}
// make sure they have been executed, so we
// compute the expected sum
// it is the lower bound
expectedSum := 0
for _, job := range s.jobs {
expectedSum += job.runInput.OtherInputs["counter"].(int)
}
// sleep fot the necessary time.
time.Sleep(2 * time.Second)
lockMap.lock.Lock()
if lockMap.counter < expectedSum {
t.Errorf("The counter has not been incremented properly. Got: %d Expected: %d\n",
lockMap.counter, expectedSum)
}
lockMap.lock.Unlock()
// ok, now remove the entries
s.scheduler.DeleteJobsWithSchedule(s.jobs)
// and make sure they have been removed
lenOfJobsAfterFirstRemove := len(s.scheduler.cron.Entries())
if lenOfJobsAfterFirstRemove != lenOfJobsBeforeAnything {
t.Errorf("Some jobs have not been removed properly. Got: %d Expected: %d\n",
lenOfJobsAfterFirstRemove, lenOfJobsBeforeAnything)
}
// now, re-add, in order to test the other delete method
s.scheduler.AddJobs(s.jobs)
// now we need to build the array for raw jobs
rawJobs := make([]RawJob, len(s.jobs))
for i := range s.jobs {
rawJobs[i] = s.jobs[i].rawJob
}
s.scheduler.DeleteJobs(rawJobs)
lenOfJobsAfterSecondRemove := len(s.scheduler.cron.Entries())
if lenOfJobsAfterSecondRemove != lenOfJobsBeforeAnything {
t.Errorf("Some jobs have not been removed properly. Got: %d Expected: %d\n",
lenOfJobsAfterSecondRemove, lenOfJobsBeforeAnything)
}
}
func (s *SchedulerTestSuite) setup() {
s.scheduler = newScheduler(&SchedulerConfig{WithSeconds: true})
s.scheduler.cron.Start()
s.jobs = []JobWithSchedule{
{
rawJob: RawJob{
ID: 1,
GroupID: 1,
SuperGroupID: 1,
CronID: 0,
CronExpression: "@every 1s",
Paused: false,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
SerializedJob: "",
SerializedJobInput: "",
},
run: &SchedulerTestCronJob{},
runInput: CronJobInput{
JobID: 1,
GroupID: 1,
SuperGroupID: 1,
OtherInputs: map[string]interface{}{
"counter": 10,
},
},
schedule: cron.ConstantDelaySchedule{Delay: 1 * time.Second},
},
{
rawJob: RawJob{
ID: 2,
GroupID: 1,
SuperGroupID: 1,
CronID: 0,
CronExpression: "@every 1s",
Paused: false,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
SerializedJob: "",
SerializedJobInput: "",
},
run: &SchedulerTestCronJob{},
runInput: CronJobInput{
JobID: 2,
GroupID: 1,
SuperGroupID: 1,
OtherInputs: map[string]interface{}{
"counter": 1,
},
},
schedule: cron.ConstantDelaySchedule{Delay: 1 * time.Second},
},
{
rawJob: RawJob{
ID: 3,
GroupID: 1,
SuperGroupID: 1,
CronID: 0,
CronExpression: "@every 1s",
Paused: false,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
SerializedJob: "",
SerializedJobInput: "",
},
run: &SchedulerTestCronJob{},
runInput: CronJobInput{
JobID: 3,
GroupID: 1,
SuperGroupID: 1,
OtherInputs: map[string]interface{}{
"counter": 3,
},
},
schedule: cron.ConstantDelaySchedule{Delay: 1 * time.Second},
},
}
}
func (s *SchedulerTestSuite) teardown() {
s.scheduler.DeleteJobsWithSchedule(s.jobs)
s.scheduler.cron.Stop()
}
// Some basic options mangling to increase coverage
func TestSchedulerWithOptions(t *testing.T) {
location, err := time.LoadLocation("Europe/Rome")
if err != nil {
t.Errorf("Fail to setup Location: %s\n", err.Error())
t.FailNow()
}
options := []SchedulerConfig{
{
DelayIfStillRunning: true,
SkipIfStillRunning: true,
WithLocation: location,
withLogger: DefaultLogger,
},
}
for _, option := range options {
newScheduler(&option)
}
}
func TestScheduler(t *testing.T) {
test := new(SchedulerTestSuite)
test.setup()
test.TestAdd(t)
test.teardown()
}