forked from LeanerCloud/AutoSpotting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schedule_test.go
204 lines (197 loc) · 5.44 KB
/
schedule_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
// Copyright (c) 2016-2019 Cristian Măgherușan-Stanciu
// Licensed under the Open Software License version 3.0
package autospotting
import (
"errors"
"strings"
"testing"
"time"
)
func Test_insideSchedule(t *testing.T) {
tests := []struct {
name string
t time.Time
timezone string
crontab string
want bool
wantErr error
}{
{
name: "All the time",
crontab: "* *",
t: time.Date(2019, time.May, 9, 10, 0, 0, 0, time.UTC),
timezone: "UTC",
want: true,
wantErr: nil,
},
{
name: "Inside business week",
crontab: "9-18 1-5",
t: time.Date(2019, time.May, 9, 10, 0, 0, 0, time.UTC),
timezone: "UTC",
want: true,
wantErr: nil,
},
{
name: "Inside business week, before interval start",
crontab: "9-18 1-5",
t: time.Date(2019, time.May, 9, 4, 0, 0, 0, time.UTC),
timezone: "UTC",
want: false,
wantErr: nil,
},
{
name: "Inside business week, after interval end",
crontab: "9-18 1-5",
t: time.Date(2019, time.May, 9, 21, 0, 0, 0, time.UTC),
timezone: "UTC",
want: false,
wantErr: nil,
},
{
name: "Inside business week, One minute before interval start",
crontab: "9-18 1-5",
t: time.Date(2019, time.May, 9, 8, 59, 0, 0, time.UTC),
timezone: "UTC",
want: false,
wantErr: nil,
},
{
name: "Inside business week, One minute after interval start",
crontab: "9-18 1-5",
t: time.Date(2019, time.May, 9, 9, 1, 0, 0, time.UTC),
timezone: "UTC",
want: true,
wantErr: nil,
},
{
name: "Inside business week, One minute before interval end",
crontab: "9-18 1-5",
t: time.Date(2019, time.May, 9, 17, 59, 0, 0, time.UTC),
timezone: "UTC",
want: true,
wantErr: nil,
},
{
name: "Inside business week, One minute after interval end",
crontab: "9-18 1-5",
t: time.Date(2019, time.May, 9, 18, 1, 0, 0, time.UTC),
timezone: "UTC",
want: false,
wantErr: nil,
},
{
name: "During the weekend",
crontab: "9-18 1-5",
t: time.Date(2019, time.May, 11, 18, 0, 0, 0, time.UTC),
timezone: "UTC",
want: false,
wantErr: nil,
},
{
name: "During the weekend, but incorrect crontab",
crontab: "9- 1-5",
t: time.Date(2019, time.May, 11, 18, 0, 0, 0, time.UTC),
timezone: "UTC",
want: false,
wantErr: errors.New("invalid syntax"),
},
{
name: "Inside business week, inside in timezone, inside in UTC",
crontab: "9-18 1-5",
t: time.Date(2019, time.May, 9, 9, 1, 0, 0, time.UTC),
timezone: "Europe/London",
want: true,
wantErr: nil,
},
{
name: "Inside business week, inside in timezone, outside in UTC",
crontab: "9-18 1-5",
t: time.Date(2019, time.May, 9, 8, 1, 0, 0, time.UTC),
timezone: "Europe/London",
want: true,
wantErr: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got, err := insideSchedule(tt.t, tt.crontab, tt.timezone); got != tt.want ||
// the err is checked for matching wantErr, doesn't need to be identical
!(err == tt.wantErr || strings.Contains(err.Error(), tt.wantErr.Error())) {
t.Errorf("insideSchedule() = %v, %v want %v, %v", got, err, tt.want, tt.wantErr)
}
})
}
}
func Test_runAction(t *testing.T) {
tests := []struct {
name string
crontab string
t time.Time
timezone string
scheduleType string
want bool
}{
{
name: "On inside interval and currently in the interval",
crontab: "9-18 1-5",
t: time.Date(2019, time.May, 9, 10, 0, 0, 0, time.UTC),
timezone: "UTC",
scheduleType: "on",
want: true,
}, {
name: "On inside interval, but currently outside interval",
crontab: "9-18 1-5",
t: time.Date(2019, time.May, 9, 20, 0, 0, 0, time.UTC),
timezone: "UTC",
scheduleType: "on",
want: false,
},
{
name: "On inside interval, but inside timezone, outside UTC",
crontab: "9-18 1-5",
t: time.Date(2019, time.May, 9, 8, 1, 0, 0, time.UTC),
timezone: "Europe/London",
scheduleType: "on",
want: true,
},
{
name: "Off inside interval, and currently in the interval",
crontab: "9-18 1-5",
t: time.Date(2019, time.May, 9, 10, 0, 0, 0, time.UTC),
timezone: "UTC",
scheduleType: "off",
want: false,
}, {
name: "Off inside interval, and currently outside the interval",
crontab: "9-18 1-5",
t: time.Date(2019, time.May, 9, 20, 0, 0, 0, time.UTC),
timezone: "UTC",
scheduleType: "off",
want: true,
},
{
name: "Off inside interval, but inside timezone, outside UTC",
crontab: "9-18 1-5",
t: time.Date(2019, time.May, 9, 8, 1, 0, 0, time.UTC),
timezone: "Europe/London",
scheduleType: "off",
want: false,
},
{
name: "Incorrect cron rule",
crontab: "-18 1-5",
t: time.Date(2019, time.May, 9, 20, 0, 0, 0, time.UTC),
timezone: "UTC",
scheduleType: "off",
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := cronRunAction(tt.t, tt.crontab, tt.timezone, tt.scheduleType); got != tt.want {
t.Errorf("runAction() = %v, want %v", got, tt.want)
}
})
}
}