-
Notifications
You must be signed in to change notification settings - Fork 0
/
cron_test.go
308 lines (301 loc) · 8.18 KB
/
cron_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
package hardloop
import (
"fmt"
"testing"
"time"
)
func Test_ParseSchedule(t *testing.T) {
logAMS, err := time.LoadLocation("Europe/Amsterdam")
if err != nil {
t.Fatalf("failed to load location: %v", err)
}
type testTime struct {
timeNow time.Time
next []time.Time
prev []time.Time
}
tests := []struct {
message string
schedule string
tests []testTime
}{
{
message: "every day at 7:00",
schedule: "0 7 * * *",
tests: []testTime{
{
timeNow: time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC),
next: []time.Time{
time.Date(2023, 1, 1, 7, 0, 0, 0, time.UTC),
time.Date(2023, 1, 2, 7, 0, 0, 0, time.UTC),
time.Date(2023, 1, 3, 7, 0, 0, 0, time.UTC),
},
prev: []time.Time{
time.Date(2022, 12, 31, 7, 0, 0, 0, time.UTC),
time.Date(2022, 12, 30, 7, 0, 0, 0, time.UTC),
time.Date(2022, 12, 29, 7, 0, 0, 0, time.UTC),
},
},
},
},
{
message: "every 5 minutes",
schedule: "*/5 * * * *",
tests: []testTime{
{
timeNow: time.Date(2023, 1, 1, 1, 0, 0, 0, time.UTC),
next: []time.Time{
time.Date(2023, 1, 1, 1, 5, 0, 0, time.UTC),
time.Date(2023, 1, 1, 1, 10, 0, 0, time.UTC),
time.Date(2023, 1, 1, 1, 15, 0, 0, time.UTC),
time.Date(2023, 1, 1, 1, 20, 0, 0, time.UTC),
},
prev: []time.Time{
time.Date(2023, 1, 1, 0, 55, 0, 0, time.UTC),
time.Date(2023, 1, 1, 0, 50, 0, 0, time.UTC),
time.Date(2023, 1, 1, 0, 45, 0, 0, time.UTC),
},
},
},
},
{
message: "every minutes",
schedule: "* * * * *",
tests: []testTime{
{
timeNow: time.Date(2023, 1, 1, 1, 0, 0, 0, time.UTC),
next: []time.Time{
time.Date(2023, 1, 1, 1, 1, 0, 0, time.UTC),
time.Date(2023, 1, 1, 1, 2, 0, 0, time.UTC),
time.Date(2023, 1, 1, 1, 3, 0, 0, time.UTC),
},
prev: []time.Time{
time.Date(2023, 1, 1, 0, 59, 0, 0, time.UTC),
time.Date(2023, 1, 1, 0, 58, 0, 0, time.UTC),
time.Date(2023, 1, 1, 0, 57, 0, 0, time.UTC),
},
},
},
},
{
message: "weekdays at 17:00",
schedule: "0 17 * * 1,2,3,4,5",
tests: []testTime{
{
timeNow: time.Date(2023, 1, 1, 5, 0, 0, 0, time.UTC),
next: []time.Time{
time.Date(2023, 1, 2, 17, 0, 0, 0, time.UTC),
time.Date(2023, 1, 3, 17, 0, 0, 0, time.UTC),
time.Date(2023, 1, 4, 17, 0, 0, 0, time.UTC),
time.Date(2023, 1, 5, 17, 0, 0, 0, time.UTC),
time.Date(2023, 1, 6, 17, 0, 0, 0, time.UTC),
time.Date(2023, 1, 9, 17, 0, 0, 0, time.UTC),
time.Date(2023, 1, 10, 17, 0, 0, 0, time.UTC),
},
prev: []time.Time{
time.Date(2022, 12, 30, 17, 0, 0, 0, time.UTC),
time.Date(2022, 12, 29, 17, 0, 0, 0, time.UTC),
time.Date(2022, 12, 28, 17, 0, 0, 0, time.UTC),
time.Date(2022, 12, 27, 17, 0, 0, 0, time.UTC),
time.Date(2022, 12, 26, 17, 0, 0, 0, time.UTC),
time.Date(2022, 12, 23, 17, 0, 0, 0, time.UTC),
},
},
},
},
{
message: "every 5 minutes in specific days",
schedule: "*/5 9-16 1-17,19-31 * 1-5",
tests: []testTime{
{
timeNow: time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC),
next: []time.Time{
time.Date(2023, 1, 2, 9, 0, 0, 0, time.UTC),
time.Date(2023, 1, 2, 9, 5, 0, 0, time.UTC),
},
},
{
// weekend will be monday
timeNow: time.Date(2023, time.August, 5, 0, 0, 0, 0, time.UTC),
next: []time.Time{
time.Date(2023, time.August, 7, 9, 0, 0, 0, time.UTC),
},
},
{
// 18th day of month is not in schedule
timeNow: time.Date(2023, time.August, 18, 0, 0, 0, 0, time.UTC),
next: []time.Time{
time.Date(2023, time.August, 21, 9, 0, 0, 0, time.UTC),
time.Date(2023, time.August, 21, 9, 5, 0, 0, time.UTC),
time.Date(2023, time.August, 21, 9, 10, 0, 0, time.UTC),
},
prev: []time.Time{
time.Date(2023, time.August, 17, 16, 55, 0, 0, time.UTC),
},
},
{
// 18th day of month and it is weekend is not in schedule
timeNow: time.Date(2023, time.November, 18, 0, 0, 0, 0, time.UTC),
next: []time.Time{
time.Date(2023, time.November, 20, 9, 0, 0, 0, time.UTC),
time.Date(2023, time.November, 20, 9, 5, 0, 0, time.UTC),
time.Date(2023, time.November, 20, 9, 10, 0, 0, time.UTC),
},
prev: []time.Time{
time.Date(2023, time.November, 17, 16, 55, 0, 0, time.UTC),
},
},
},
},
{
message: "to may",
schedule: "0 17 23 5 *",
tests: []testTime{
{
timeNow: time.Date(2020, time.January, 5, 10, 50, 0, 0, time.UTC),
next: []time.Time{
time.Date(2020, time.May, 23, 17, 0, 0, 0, time.UTC),
},
prev: []time.Time{
time.Date(2019, time.May, 23, 17, 0, 0, 0, time.UTC),
},
},
},
},
{
message: "year pass time",
schedule: "35 17 * * 1,2,3,4,5",
tests: []testTime{
{
timeNow: time.Date(2020, time.January, 5, 10, 20, 0, 0, time.UTC),
prev: []time.Time{
time.Date(2020, time.January, 3, 17, 35, 0, 0, time.UTC),
},
},
{
timeNow: time.Date(2020, time.January, 5, 10, 50, 0, 0, time.UTC),
prev: []time.Time{
time.Date(2020, time.January, 3, 17, 35, 0, 0, time.UTC),
},
},
},
},
{
message: "random 1",
schedule: "23 0-20/2 * * *",
tests: []testTime{
{
timeNow: time.Date(2023, time.August, 5, 2, 26, 0, 0, time.UTC),
next: []time.Time{
time.Date(2023, time.August, 5, 4, 23, 0, 0, time.UTC),
},
prev: []time.Time{
time.Date(2023, time.August, 5, 2, 23, 0, 0, time.UTC),
},
},
},
},
{
message: "random 2",
schedule: "0 0,12 1 */2 *",
tests: []testTime{
{
timeNow: time.Date(2023, time.August, 5, 2, 26, 0, 0, time.UTC),
next: []time.Time{
time.Date(2023, time.September, 1, 0, 0, 0, 0, time.UTC),
},
prev: []time.Time{
time.Date(2023, time.July, 1, 12, 0, 0, 0, time.UTC),
time.Date(2023, time.July, 1, 0, 0, 0, 0, time.UTC),
},
},
},
},
{
message: "random 3",
schedule: "0 0 1,15 * 3",
tests: []testTime{
{
timeNow: time.Date(2023, time.August, 5, 2, 26, 0, 0, time.UTC),
next: []time.Time{
time.Date(2023, time.November, 1, 0, 0, 0, 0, time.UTC),
},
prev: []time.Time{
time.Date(2023, time.March, 15, 0, 0, 0, 0, time.UTC),
},
},
},
},
{
message: "DTS test",
schedule: "30 2 26 3 *",
tests: []testTime{
{
timeNow: time.Date(2023, time.March, 1, 0, 0, 0, 0, time.UTC),
next: []time.Time{
time.Date(2023, time.March, 26, 2, 30, 0, 0, time.UTC),
},
prev: []time.Time{
time.Date(2022, time.March, 26, 2, 30, 0, 0, time.UTC),
},
},
},
},
{
message: "Amsterdam DTS test",
schedule: "CRON_TZ=Europe/Amsterdam 30 1 26 3 *",
tests: []testTime{
{
timeNow: time.Date(2023, time.March, 1, 0, 0, 0, 0, logAMS),
next: []time.Time{
time.Date(2023, time.March, 26, 1, 30, 0, 0, logAMS),
},
prev: []time.Time{
time.Date(2022, time.March, 26, 1, 30, 0, 0, logAMS),
},
},
},
},
{
message: "every 5 minutes",
schedule: "@every 5m",
tests: []testTime{
{
timeNow: time.Date(2023, time.March, 1, 0, 10, 0, 0, time.UTC),
next: []time.Time{
time.Date(2023, time.March, 1, 0, 15, 0, 0, time.UTC),
time.Date(2023, time.March, 1, 0, 20, 0, 0, time.UTC),
},
prev: []time.Time{
time.Date(2023, time.March, 1, 0, 5, 0, 0, time.UTC),
time.Date(2023, time.March, 1, 0, 0, 0, 0, time.UTC),
},
},
},
},
}
for i, tt := range tests {
t.Run(fmt.Sprintf("parse_%d", i), func(t *testing.T) {
schedule, err := ParseStandard(tt.schedule)
if err != nil {
t.Fatalf("failed to parse schedule: %v", err)
}
for _, testN := range tt.tests {
now := testN.timeNow
for _, next := range testN.next {
now = schedule.Next(now)
if !now.Equal(next) {
t.Fatalf("[next] %s expected %v, got %v, now %v", tt.schedule, next, now, testN.timeNow)
}
}
now = testN.timeNow
for _, prev := range testN.prev {
now = schedule.Prev(now)
if !now.Equal(prev) {
t.Fatalf("[prev] %s expected %v, got %v, now %v", tt.schedule, prev, now, testN.timeNow)
}
}
}
})
}
}