forked from cadence-workflow/cadence
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcron_test.go
158 lines (145 loc) · 7.03 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
// Copyright (c) 2019 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package backoff
import (
"fmt"
"math/rand"
"strconv"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestCron(t *testing.T) {
var crontests = []struct {
cron string
startTime string
endTime string
result time.Duration
}{
{"0 10 * * *", "2018-12-17T08:00:00-08:00", "", time.Hour * 18},
{"0 10 * * *", "2018-12-18T02:00:00-08:00", "", time.Hour * 24},
{"0 * * * *", "2018-12-17T08:08:00+00:00", "", time.Minute * 52},
{"0 * * * *", "2018-12-17T09:00:00+00:00", "", time.Hour},
{"* * * * *", "2018-12-17T08:08:18+00:00", "", time.Second * 42},
{"0 * * * *", "2018-12-17T09:00:00+00:00", "", time.Minute * 60},
{"0 10 * * *", "2018-12-17T08:00:00+00:00", "2018-12-20T00:00:00+00:00", time.Hour * 10},
{"0 10 * * *", "2018-12-17T08:00:00+00:00", "2018-12-17T09:00:00+00:00", time.Hour},
{"*/10 * * * *", "2018-12-17T00:04:00+00:00", "2018-12-17T01:02:00+00:00", time.Minute * 8},
{"invalid-cron-spec", "2018-12-17T00:04:00+00:00", "2018-12-17T01:02:00+00:00", NoBackoff},
{"@every 5h", "2018-12-17T08:00:00+00:00", "2018-12-17T09:00:00+00:00", time.Hour * 4},
{"@every 5h", "2018-12-17T08:00:00+00:00", "2018-12-18T00:00:00+00:00", time.Hour * 4},
{"0 3 * * 0-6", "2018-12-17T08:00:00-08:00", "", time.Hour * 11},
}
for idx, tt := range crontests {
t.Run(strconv.Itoa(idx), func(t *testing.T) {
start, _ := time.Parse(time.RFC3339, tt.startTime)
end := start
if tt.endTime != "" {
end, _ = time.Parse(time.RFC3339, tt.endTime)
}
sched, err := ValidateSchedule(tt.cron)
if tt.result == NoBackoff {
// no backoff == error, simplifies the test-struct a bit
require.ErrorContains(t, err, "Invalid CronSchedule")
} else {
require.NoError(t, err)
backoff, err := GetBackoffForNextSchedule(sched, start, end, 0)
require.NoError(t, err)
assert.Equal(t, tt.result, backoff, "The cron spec is %s and the expected result is %s", tt.cron, tt.result)
}
})
}
}
func TestCronWithJitterStart(t *testing.T) {
var cronWithJitterStartTests = []struct {
cron string
startTime string
jitterStartSeconds int32
expectedResultSeconds time.Duration
expectedResultSeconds2 time.Duration
}{
// Note that the cron scheduler we use (robfig) schedules differently depending on the syntax:
// 1) * * * syntax : next run is scheduled on the first second of each minute, starting at next minute
// 2) @every X syntax: next run is scheduled X seconds from the time passed in to Next() call.
{"* * * * *", "2018-12-17T08:00:00+00:00", 10, time.Second * 60, time.Second * 60},
{"* * * * *", "2018-12-17T08:00:10+00:00", 30, time.Second * 50, time.Second * 60},
{"* * * * *", "2018-12-17T08:00:25+00:00", 15, time.Second * 35, time.Second * 60},
{"* * * * *", "2018-12-17T08:00:45+00:00", 0, time.Second * 15, time.Second * 60},
{"@every 60s", "2018-12-17T08:00:45+00:00", 0, time.Second * 60, time.Second * 60},
{"* * * * *", "2018-12-17T08:00:45+00:00", 45, time.Second * 15, time.Second * 60},
{"@every 60s", "2018-12-17T08:00:45+00:00", 45, time.Second * 60, time.Second * 60},
{"* * * * *", "2018-12-17T08:00:00+00:00", 70, time.Second * 60, time.Second * 60},
{"@every 20s", "2018-12-17T08:00:00+00:00", 15, time.Second * 20, time.Second * 20},
{"@every 10s", "2018-12-17T08:00:09+00:00", 0, time.Second * 10, time.Second * 10},
{"@every 20s", "2018-12-17T08:00:09+00:00", 15, time.Second * 20, time.Second * 20},
{"* * * * *", "0001-01-01T00:00:00+00:00", 0, time.Second * 60, time.Second * 60},
{"@every 20s", "0001-01-01T00:00:00+00:00", 0, time.Second * 20, time.Second * 20},
}
rand.Seed(int64(time.Now().Nanosecond()))
for idx, tt := range cronWithJitterStartTests {
t.Run(strconv.Itoa(idx), func(t *testing.T) {
exactCount := 0
start, _ := time.Parse(time.RFC3339, tt.startTime)
end := start
sched, err := ValidateSchedule(tt.cron)
if tt.expectedResultSeconds != NoBackoff {
assert.NoError(t, err)
}
backoff, err := GetBackoffForNextSchedule(sched, start, end, tt.jitterStartSeconds)
require.NoError(t, err)
fmt.Printf("Backoff time for test %d = %v\n", idx, backoff)
delta := time.Duration(tt.jitterStartSeconds) * time.Second
expectedResultTime := start.Add(tt.expectedResultSeconds)
backoffTime := start.Add(backoff)
assert.WithinDuration(t, expectedResultTime, backoffTime, delta,
"The test specs are %v and the expected result in seconds is between %s and %s",
tt, tt.expectedResultSeconds, tt.expectedResultSeconds+delta)
if expectedResultTime == backoffTime {
exactCount++
}
// Also check next X cron times
caseCount := 5
for i := 1; i < caseCount; i++ {
startTime := expectedResultTime
backoff, err := GetBackoffForNextSchedule(sched, startTime, startTime, tt.jitterStartSeconds)
require.NoError(t, err)
expectedResultTime := startTime.Add(tt.expectedResultSeconds2)
backoffTime := startTime.Add(backoff)
assert.WithinDuration(t, expectedResultTime, backoffTime, delta,
"Iteration %d: The test specs are %v and the expected result in seconds is between %s and %s",
i, tt, tt.expectedResultSeconds, tt.expectedResultSeconds+delta)
if expectedResultTime == backoffTime {
exactCount++
}
}
// If jitter is > 0, we want to detect whether jitter is being applied - BUT we don't want the test
// to be flaky if the code randomly chooses a jitter of 0, so we try to have enough data points by
// checking the next X cron times AND by choosing a jitter thats not super low.
if tt.jitterStartSeconds > 0 && exactCount == caseCount {
// Test to make sure a jitter test case sometimes doesn't get exact values
t.Fatalf("FAILED to jitter properly? Test specs = %v\n", tt)
} else if tt.jitterStartSeconds == 0 && exactCount != caseCount {
// Test to make sure a non-jitter test case always gets exact values
t.Fatalf("Jittered when we weren't supposed to? Test specs = %v\n", tt)
}
})
}
}