forked from gorhill/cronexpr
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cronexpr_hash_test.go
415 lines (410 loc) · 12.2 KB
/
cronexpr_hash_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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
package cronexpr
import (
"fmt"
"testing"
"time"
)
func ExampleHashString() {
for _, str := range []string{
"myid1",
"myid2",
"myid3",
} {
fmt.Printf("%d\n", HashString(str))
}
// Output:
// 316181436714908099
// 8964299977724969587
// 12738036773875955645
}
func Test_hash_GetValue(t *testing.T) {
tests := []struct {
name string
value uint64
min int
max int
want int
}{
{name: "zero value", value: 0, min: 0, max: 9, want: 0},
{name: "offset", value: 0, min: 5, max: 9, want: 5},
{name: "value", value: 3, min: 0, max: 9, want: 3},
{name: "modulo", value: 15, min: 0, max: 9, want: 5},
{name: "modulo boundary", value: 10, min: 0, max: 9, want: 0},
{name: "value with offset", value: 3, min: 5, max: 9, want: 8},
{name: "modulo with offset", value: 13, min: 5, max: 9, want: 8},
{name: "modulo boundary with offset", value: 10, min: 5, max: 9, want: 5},
// int(12738036773875955645) = -5708707299833595971, ensure that we don't get a negative value out of range
{name: "negative integer overflow mod 16", value: 12738036773875955645, min: 0, max: 15, want: 13},
{name: "negative integer overflow mod 15", value: 12738036773875955645, min: 0, max: 14, want: 4},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
h := hash{
value: tt.value,
hashed: true,
}
if got := h.GetValue(tt.min, tt.max); got != tt.want {
t.Errorf("GetValue() = %v, want %v", got, tt.want)
}
})
}
}
func TestHashExpressions(t *testing.T) {
tests := []struct {
name string
expr string
fmt CronFormat
opts []ParseOption
times map[string][]crontimes
}{
{
name: "parsing single H in minute",
expr: "0 H * ? * * *",
fmt: CronFormatStandard,
times: map[string][]crontimes{
"myid1": { // hash mod 60 = 59
{"2021-09-01 00:00:00", "2021-09-01 00:59:00"},
},
"myid2": { // hash mod 60 = 7
{"2021-09-01 00:00:00", "2021-09-01 00:07:00"},
},
},
},
{
name: "parsing single H in hour",
expr: "0 0 H ? * * *",
fmt: CronFormatStandard,
times: map[string][]crontimes{
"myid1": { // hash mod 24 = 11
{"2021-09-01 00:00:00", "2021-09-01 11:00:00"},
},
"myid2": { // hash mod 24 = 19
{"2021-09-01 00:00:00", "2021-09-01 19:00:00"},
},
},
},
{
name: "parsing single H in day of month",
expr: "0 0 0 H * * *",
fmt: CronFormatStandard,
times: map[string][]crontimes{
"myid1": { // hash mod 28 = 27
{"2021-09-01 00:00:00", "2021-09-28 00:00:00"},
},
"myid2": { // hash mod 28 = 3
{"2021-09-01 00:00:00", "2021-09-04 00:00:00"},
},
},
},
{
name: "parsing single H in month",
expr: "0 0 0 ? H * *",
fmt: CronFormatStandard,
times: map[string][]crontimes{
"myid1": { // hash mod 12 = 11
{"2021-01-01 00:00:00", "2021-12-01 00:00:00"},
},
"myid2": { // hash mod 12 = 7
{"2021-01-01 00:00:00", "2021-08-01 00:00:00"},
},
},
},
{
name: "parsing single H in day of week",
expr: "0 0 0 ? * H *",
fmt: CronFormatStandard,
times: map[string][]crontimes{
"myid1": { // hash mod 7 = 6
{"2021-09-01 00:00:00", "2021-09-04 00:00:00"},
},
"myid2": { // hash mod 7 = 3
{"2021-09-01 00:00:00", "2021-09-08 00:00:00"},
},
},
},
{
name: "parsing single H in day of week with quartz",
expr: "0 0 0 ? * H *",
fmt: CronFormatQuartz,
times: map[string][]crontimes{
"myid1": { // hash mod 7 = 6
{"2021-09-01 00:00:00", "2021-09-04 00:00:00"},
},
"myid2": { // hash mod 7 = 3
{"2021-09-01 00:00:00", "2021-09-08 00:00:00"},
},
},
},
{
name: "parsing single H in year",
expr: "0 0 0 ? * * H",
fmt: CronFormatStandard,
times: map[string][]crontimes{
"myid1": { // hash mod 130 = 49
{"2015-01-01 00:00:00", "2019-01-01 00:00:00"},
{"2021-01-01 00:00:00", "0001-01-01 00:00:00"}, // out of range, more than 2099
},
"myid2": { // hash mod 130 = 47
{"2015-01-01 00:00:00", "2017-01-01 00:00:00"},
{"2021-01-01 00:00:00", "0001-01-01 00:00:00"}, // out of range, more than 2099
},
},
},
{
name: "parsing H in minute and hour, schedule once a day",
expr: "0 H H ? * * *",
fmt: CronFormatStandard,
times: map[string][]crontimes{
"myid1": {
{"2021-09-01 00:00:00", "2021-09-01 11:59:00"},
{"2021-09-01 11:59:00", "2021-09-02 11:59:00"},
},
"myid2": {
{"2021-09-01 00:00:00", "2021-09-01 19:07:00"},
{"2021-09-01 19:07:00", "2021-09-02 19:07:00"},
},
},
},
{
// Should be identical to above
name: "parsing H in minute and hour, schedule once a day, with implicit 0",
expr: "H H ? * * *",
fmt: CronFormatStandard,
times: map[string][]crontimes{
"myid1": {
{"2021-09-01 00:00:00", "2021-09-01 11:59:00"},
{"2021-09-01 11:59:00", "2021-09-02 11:59:00"},
},
"myid2": {
{"2021-09-01 00:00:00", "2021-09-01 19:07:00"},
{"2021-09-01 19:07:00", "2021-09-02 19:07:00"},
},
},
},
{
name: "parsing H in seconds, minute and hour, schedule once a day",
expr: "H H H ? * * *",
fmt: CronFormatStandard,
times: map[string][]crontimes{
"myid1": {
{"2021-09-01 00:00:00", "2021-09-01 11:59:59"},
{"2021-09-01 11:59:59", "2021-09-02 11:59:59"},
},
"myid2": {
{"2021-09-01 00:00:00", "2021-09-01 19:07:07"},
{"2021-09-01 19:07:07", "2021-09-02 19:07:07"},
},
},
},
{
// Should be identical to above
name: "parsing H in minute and hour, schedule once a day, using WithHashEmptySeconds",
expr: "H H ? * * *",
fmt: CronFormatStandard,
opts: []ParseOption{WithHashEmptySeconds()},
times: map[string][]crontimes{
"myid1": {
{"2021-09-01 00:00:00", "2021-09-01 11:59:59"},
{"2021-09-01 11:59:59", "2021-09-02 11:59:59"},
},
"myid2": {
{"2021-09-01 00:00:00", "2021-09-01 19:07:07"},
{"2021-09-01 19:07:07", "2021-09-02 19:07:07"},
},
},
},
{
name: "parsing H in minute and hour, schedule once a day, using WithHashFields",
expr: "H H ? * * *",
fmt: CronFormatStandard,
opts: []ParseOption{WithHashEmptySeconds(), WithHashFields()},
times: map[string][]crontimes{
"myid1": {
{"2021-09-01 00:00:00", "2021-09-01 07:36:44"},
{"2021-09-01 07:36:44", "2021-09-02 07:36:44"},
},
"myid2": {
{"2021-09-01 00:00:00", "2021-09-01 04:43:56"},
{"2021-09-01 04:43:56", "2021-09-02 04:43:56"},
},
},
},
{
name: "jenkins example",
expr: "H H(0-7) * * *",
fmt: CronFormatStandard,
times: map[string][]crontimes{
"myid1": {
{"2021-09-01 00:00:00", "2021-09-01 03:59:00"},
{"2021-09-01 03:59:00", "2021-09-02 03:59:00"},
},
"myid2": {
{"2021-09-01 00:00:00", "2021-09-01 03:07:00"},
{"2021-09-01 03:07:00", "2021-09-02 03:07:00"},
},
"myid3": {
{"2021-09-01 00:00:00", "2021-09-01 05:49:00"},
{"2021-09-01 05:49:00", "2021-09-02 05:49:00"},
},
},
},
{
name: "parsing H/5 in minute",
expr: "0 H/5 * ? * * *",
fmt: CronFormatStandard,
times: map[string][]crontimes{
"myid1": { // hash mod 5 = 4
{"2021-09-01 00:00:00", "2021-09-01 00:04:00"},
{"2021-09-01 00:04:00", "2021-09-01 00:09:00"},
{"2021-09-01 00:09:00", "2021-09-01 00:14:00"},
{"2021-09-01 00:59:00", "2021-09-01 01:04:00"},
},
"myid2": { // hash mod 5 = 2
{"2021-09-01 00:00:00", "2021-09-01 00:02:00"},
{"2021-09-01 00:02:00", "2021-09-01 00:07:00"},
{"2021-09-01 00:59:00", "2021-09-01 01:02:00"},
},
},
},
{
name: "parsing H/7 in minute",
expr: "0 H/7 * ? * * *",
fmt: CronFormatStandard,
times: map[string][]crontimes{
"myid1": { // hash mod 7 = 6
{"2021-09-01 00:00:00", "2021-09-01 00:06:00"},
{"2021-09-01 00:06:00", "2021-09-01 00:13:00"},
{"2021-09-01 00:13:00", "2021-09-01 00:20:00"},
{"2021-09-01 00:55:00", "2021-09-01 01:06:00"},
},
"myid2": { // hash mod 7 = 3
{"2021-09-01 00:00:00", "2021-09-01 00:03:00"},
{"2021-09-01 00:03:00", "2021-09-01 00:10:00"},
{"2021-09-01 00:52:00", "2021-09-01 00:59:00"},
{"2021-09-01 00:59:00", "2021-09-01 01:03:00"},
},
},
},
{
name: "parsing H/5 in minute and H in seconds",
expr: "H H/5 * ? * * *",
fmt: CronFormatStandard,
times: map[string][]crontimes{
"myid1": { // hash mod 5 = 4, hash mod 60 = 59
{"2021-09-01 00:00:00", "2021-09-01 00:04:59"},
{"2021-09-01 00:04:59", "2021-09-01 00:09:59"},
{"2021-09-01 00:09:59", "2021-09-01 00:14:59"},
{"2021-09-01 00:59:59", "2021-09-01 01:04:59"},
},
"myid2": { // hash mod 5 = 2, hash mod 60 = 7
{"2021-09-01 00:00:00", "2021-09-01 00:02:07"},
{"2021-09-01 00:02:07", "2021-09-01 00:07:07"},
{"2021-09-01 00:59:07", "2021-09-01 01:02:07"},
},
},
},
{
// Should be identical to above
name: "parsing H/5 in minute and WithHashEmptySeconds",
expr: "H/5 * ? * * *",
fmt: CronFormatStandard,
opts: []ParseOption{WithHashEmptySeconds()},
times: map[string][]crontimes{
"myid1": { // hash mod 5 = 4, hash mod 60 = 59
{"2021-09-01 00:00:00", "2021-09-01 00:04:59"},
{"2021-09-01 00:04:59", "2021-09-01 00:09:59"},
{"2021-09-01 00:09:59", "2021-09-01 00:14:59"},
{"2021-09-01 00:59:59", "2021-09-01 01:04:59"},
},
"myid2": { // hash mod 5 = 2, hash mod 60 = 7
{"2021-09-01 00:00:00", "2021-09-01 00:02:07"},
{"2021-09-01 00:02:07", "2021-09-01 00:07:07"},
{"2021-09-01 00:59:07", "2021-09-01 01:02:07"},
},
},
},
{
name: "parsing H/5 in minute and WithHashFields",
expr: "H/5 * ? * * *",
fmt: CronFormatStandard,
opts: []ParseOption{WithHashEmptySeconds(), WithHashFields()},
times: map[string][]crontimes{
"myid1": {
{"2021-09-01 00:00:00", "2021-09-01 00:01:44"},
{"2021-09-01 00:01:44", "2021-09-01 00:06:44"},
{"2021-09-01 00:06:44", "2021-09-01 00:11:44"},
{"2021-09-01 00:56:44", "2021-09-01 01:01:44"},
},
"myid2": {
{"2021-09-01 00:00:00", "2021-09-01 00:03:56"},
{"2021-09-01 00:03:56", "2021-09-01 00:08:56"},
{"2021-09-01 00:58:56", "2021-09-01 01:03:56"},
},
},
},
{
name: "parsing H(5-20) in minute", // once per hour within 5-20 min range
expr: "0 H(5-20) * ? * * *",
fmt: CronFormatStandard,
times: map[string][]crontimes{
"myid1": { // hash mod 16 = 3
{"2021-09-01 00:00:00", "2021-09-01 00:08:00"},
{"2021-09-01 00:08:00", "2021-09-01 01:08:00"},
},
"myid2": { // hash mod 16 = 3
{"2021-09-01 00:00:00", "2021-09-01 00:08:00"},
{"2021-09-01 00:08:00", "2021-09-01 01:08:00"},
},
"myid3": {
// Negative integer overflow test: int(hash("myid3")) mod 16 = -3
// This test ensures that we don't get 00:02:00 (from 00:05:00 - 00:03:00).
{"2021-09-01 00:00:00", "2021-09-01 00:18:00"},
{"2021-09-01 00:18:00", "2021-09-01 01:18:00"},
},
},
},
{
name: "parsing H(5-20)/5 in minute",
expr: "0 H(5-20)/5 * ? * * *",
fmt: CronFormatStandard,
times: map[string][]crontimes{
"myid1": { // hash mod 5 = 4
{"2021-09-01 00:00:00", "2021-09-01 00:09:00"},
{"2021-09-01 00:09:00", "2021-09-01 00:14:00"},
{"2021-09-01 00:14:00", "2021-09-01 00:19:00"},
{"2021-09-01 00:19:00", "2021-09-01 01:09:00"},
},
"myid2": { // hash mod 5 = 2
{"2021-09-01 00:00:00", "2021-09-01 00:07:00"},
{"2021-09-01 00:07:00", "2021-09-01 00:12:00"},
{"2021-09-01 00:12:00", "2021-09-01 00:17:00"},
{"2021-09-01 00:17:00", "2021-09-01 01:07:00"},
},
},
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
for hashID, ctimes := range tt.times {
for _, times := range ctimes {
from, _ := time.Parse("2006-01-02 15:04:05", times.from)
opts := []ParseOption{WithHash(hashID)}
opts = append(opts, tt.opts...)
expr, err := ParseForFormat(tt.fmt, tt.expr, opts...)
if err != nil {
t.Errorf(`ParseForFormat("%s", "%s", WithHash("%s")) returned "%s"`,
tt.fmt, tt.expr, hashID, err.Error())
return
}
next := expr.Next(from)
nextstr := next.Format("2006-01-02 15:04:05")
if nextstr != times.next {
t.Errorf(`("%s").Next("%s") = "%s", got "%s"`, tt.expr, times.from, times.next, nextstr)
}
}
}
})
}
}