-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstantdelay_test.go
More file actions
72 lines (58 loc) · 2.56 KB
/
constantdelay_test.go
File metadata and controls
72 lines (58 loc) · 2.56 KB
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
package cron
import (
"testing"
"time"
)
const (
quarterHourDelay = 15 * time.Minute
roundingNanosecondDelay = 50 * time.Nanosecond
thirtyFiveMinuteDelay = 35 * time.Minute
fourteenMinuteDelay = 14 * time.Minute
fortyFourMinuteDelay = 44 * time.Minute
twentyFourSecondDelay = 24 * time.Second
twentyFiveHourDelay = 25 * time.Hour
ninetyOneDayDelay = 91 * 24 * time.Hour
twentyFiveMinuteDelay = 25 * time.Minute
fifteenSecondDelay = 15 * time.Second
fifteenMillisecondDelay = 15 * time.Millisecond
nextQuarterHourTimestamp = "Mon Jul 9 15:00 2012"
)
func TestConstantDelayNext(t *testing.T) {
t.Parallel()
tests := []struct {
time string
delay time.Duration
expected string
}{
// Simple cases
{"Mon Jul 9 14:45 2012", quarterHourDelay + roundingNanosecondDelay, nextQuarterHourTimestamp},
{"Mon Jul 9 14:59 2012", quarterHourDelay, "Mon Jul 9 15:14 2012"},
{"Mon Jul 9 14:59:59 2012", quarterHourDelay, "Mon Jul 9 15:14:59 2012"},
// Wrap around hours
{"Mon Jul 9 15:45 2012", thirtyFiveMinuteDelay, "Mon Jul 9 16:20 2012"},
// Wrap around days
{"Mon Jul 9 23:46 2012", fourteenMinuteDelay, "Tue Jul 10 00:00 2012"},
{"Mon Jul 9 23:45 2012", thirtyFiveMinuteDelay, "Tue Jul 10 00:20 2012"},
{"Mon Jul 9 23:35:51 2012", fortyFourMinuteDelay + twentyFourSecondDelay, "Tue Jul 10 00:20:15 2012"},
{"Mon Jul 9 23:35:51 2012", twentyFiveHourDelay + fortyFourMinuteDelay + twentyFourSecondDelay, "Thu Jul 11 01:20:15 2012"},
// Wrap around months
{"Mon Jul 9 23:35 2012", ninetyOneDayDelay + twentyFiveMinuteDelay, "Thu Oct 9 00:00 2012"},
// Wrap around minute, hour, day, month, and year
{"Mon Dec 31 23:59:45 2012", fifteenSecondDelay, "Tue Jan 1 00:00:00 2013"},
// Round to nearest second on the delay
{"Mon Jul 9 14:45 2012", quarterHourDelay + roundingNanosecondDelay, nextQuarterHourTimestamp},
// Round up to 1 second if the duration is less.
{"Mon Jul 9 14:45:00 2012", fifteenMillisecondDelay, "Mon Jul 9 14:45:01 2012"},
// Round to nearest second when calculating the next time.
{"Mon Jul 9 14:45:00.005 2012", quarterHourDelay, nextQuarterHourTimestamp},
// Round to nearest second for both.
{"Mon Jul 9 14:45:00.005 2012", quarterHourDelay + roundingNanosecondDelay, nextQuarterHourTimestamp},
}
for _, testCase := range tests {
actual := Every(testCase.delay).Next(getTime(testCase.time))
expected := getTime(testCase.expected)
if actual != expected {
t.Errorf("%s, \"%s\": (expected) %v != %v (actual)", testCase.time, testCase.delay, expected, actual)
}
}
}