-
Notifications
You must be signed in to change notification settings - Fork 601
/
Copy pathschedule_test.go
262 lines (242 loc) · 7.64 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
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
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2017 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package timeutil_test
import (
"strings"
"testing"
"time"
. "gopkg.in/check.v1"
"github.com/snapcore/snapd/timeutil"
)
func Test(t *testing.T) { TestingT(t) }
type timeutilSuite struct{}
var _ = Suite(&timeutilSuite{})
func (ts *timeutilSuite) TestParseTimeOfDay(c *C) {
for _, t := range []struct {
timeStr string
hour, minute int
errStr string
}{
{"8:59", 8, 59, ""},
{"08:59", 8, 59, ""},
{"12:00", 12, 0, ""},
{"xx", 0, 0, `cannot parse "xx"`},
{"11:61", 0, 0, `cannot parse "11:61"`},
{"25:00", 0, 0, `cannot parse "25:00"`},
} {
ti, err := timeutil.ParseTime(t.timeStr)
if t.errStr != "" {
c.Check(err, ErrorMatches, t.errStr)
} else {
c.Check(err, IsNil)
c.Check(ti.Hour, Equals, t.hour)
c.Check(ti.Minute, Equals, t.minute)
}
}
}
func (ts *timeutilSuite) TestScheduleString(c *C) {
for _, t := range []struct {
sched timeutil.Schedule
str string
}{
{timeutil.Schedule{Start: timeutil.TimeOfDay{Hour: 13, Minute: 41}, End: timeutil.TimeOfDay{Hour: 14, Minute: 59}}, "13:41-14:59"},
{timeutil.Schedule{Start: timeutil.TimeOfDay{Hour: 13, Minute: 41}, End: timeutil.TimeOfDay{Hour: 14, Minute: 59}, Weekday: "mon"}, "mon@13:41-14:59"},
} {
c.Check(t.sched.String(), Equals, t.str)
}
}
func (ts *timeutilSuite) TestParseSchedule(c *C) {
for _, t := range []struct {
in string
expected []*timeutil.Schedule
errStr string
}{
// invalid
{"", nil, `cannot parse "": not a valid interval`},
{"invalid-11:00", nil, `cannot parse "invalid": not a valid time`},
{"9:00-11:00/invalid", nil, `cannot parse "invalid": not a valid interval`},
{"09:00-25:00", nil, `cannot parse "25:00": not a valid time`},
// moving backwards
{"11:00-09:00", nil, `cannot parse "11:00-09:00": time in an interval cannot go backwards`},
{"23:00-01:00", nil, `cannot parse "23:00-01:00": time in an interval cannot go backwards`},
// FIXME: error message sucks
{"9:00-mon@11:00", nil, `cannot parse "9:00-mon", want "mon", "tue", etc`},
// valid
{"9:00-11:00", []*timeutil.Schedule{{Start: timeutil.TimeOfDay{Hour: 9}, End: timeutil.TimeOfDay{Hour: 11}}}, ""},
{"mon@9:00-11:00", []*timeutil.Schedule{{Weekday: "mon", Start: timeutil.TimeOfDay{Hour: 9}, End: timeutil.TimeOfDay{Hour: 11}}}, ""},
{"9:00-11:00/20:00-22:00", []*timeutil.Schedule{{Start: timeutil.TimeOfDay{Hour: 9}, End: timeutil.TimeOfDay{Hour: 11}}, {Start: timeutil.TimeOfDay{Hour: 20}, End: timeutil.TimeOfDay{Hour: 22}}}, ""},
{"mon@9:00-11:00/Wed@22:00-23:00", []*timeutil.Schedule{{Weekday: "mon", Start: timeutil.TimeOfDay{Hour: 9}, End: timeutil.TimeOfDay{Hour: 11}}, {Weekday: "wed", Start: timeutil.TimeOfDay{Hour: 22}, End: timeutil.TimeOfDay{Hour: 23}}}, ""},
} {
schedule, err := timeutil.ParseSchedule(t.in)
if t.errStr != "" {
c.Check(err, ErrorMatches, t.errStr, Commentf("%q returned unexpected error: %s", err))
} else {
c.Check(err, IsNil, Commentf("%q returned error: %s", t.in, err))
c.Check(schedule, DeepEquals, t.expected, Commentf("%q failed", t.in))
}
}
}
func parse(c *C, s string) (time.Duration, time.Duration) {
l := strings.Split(s, "-")
c.Assert(l, HasLen, 2)
a, err := time.ParseDuration(l[0])
c.Assert(err, IsNil)
b, err := time.ParseDuration(l[1])
c.Assert(err, IsNil)
return a, b
}
func (ts *timeutilSuite) TestScheduleNext(c *C) {
const shortForm = "2006-01-02 15:04"
for _, t := range []struct {
schedule string
last string
now string
next string
}{
{
// daily schedule, missed one window
// -> run next daily window
schedule: "9:00-11:00/21:00-23:00",
last: "2017-02-05 22:00",
now: "2017-02-06 20:00",
next: "1h-3h",
},
{
// daily schedule, used one window
// -> run next daily window
schedule: "9:00-11:00/21:00-23:00",
last: "2017-02-06 10:00",
now: "2017-02-06 20:00",
next: "1h-3h",
},
{
// daily schedule, missed all todays windows
// run tomorrow
schedule: "9:00-11:00/21:00-22:00",
last: "2017-02-04 21:30",
now: "2017-02-06 23:00",
next: "10h-12h",
},
{
// single daily schedule, already updated today
schedule: "9:00-11:00",
last: "2017-02-06 09:30",
now: "2017-02-06 10:00",
next: "23h-25h",
},
{
// single daily schedule, already updated today
// (at exactly the edge)
schedule: "9:00-11:00",
last: "2017-02-06 09:00",
now: "2017-02-06 09:00",
next: "24h-26h",
},
{
// single daily schedule, last update a day ago
// now is within the update window so randomize
// (run within remaining time delta)
schedule: "9:00-11:00",
last: "2017-02-05 09:30",
now: "2017-02-06 10:00",
next: "0-55m",
},
{
// multi daily schedule, already updated today
schedule: "9:00-11:00/21:00-22:00",
last: "2017-02-06 21:30",
now: "2017-02-06 23:00",
next: "10h-12h",
},
{
// weekly schedule, next window today
schedule: "tue@9:00-11:00/wed@9:00-11:00",
last: "2017-02-01 10:00",
now: "2017-02-07 05:00",
next: "4h-6h",
},
{
// weekly schedule, next window tomorrow
// (2017-02-06 is a monday)
schedule: "tue@9:00-11:00/wed@9:00-11:00",
last: "2017-02-06 03:00",
now: "2017-02-06 05:00",
next: "28h-30h",
},
{
// weekly schedule, next window in 2 days
// (2017-02-06 is a monday)
schedule: "wed@9:00-11:00/thu@9:00-11:00",
last: "2017-02-06 03:00",
now: "2017-02-06 05:00",
next: "52h-54h",
},
{
// weekly schedule, missed weekly window
// run next monday
schedule: "mon@9:00-11:00",
last: "2017-01-30 10:00",
now: "2017-02-06 12:00",
// 7*24h - 3h
next: "165h-167h",
},
{
// multi day schedule, next window soon
schedule: "mon@9:00-11:00/tue@21:00-23:00",
last: "2017-01-31 22:00",
now: "2017-02-06 5:00",
next: "4h-6h",
},
{
// weekly schedule, missed weekly window
// by more than 14 days
schedule: "mon@9:00-11:00",
last: "2017-01-01 10:00",
now: "2017-02-06 12:00",
next: "0s-0s",
},
{
// daily schedule, very small window
schedule: "9:00-9:03",
last: "2017-02-05 09:02",
now: "2017-02-06 08:58",
next: "2m-5m",
},
{
// daily schedule, zero window
schedule: "9:00-9:00",
last: "2017-02-05 09:02",
now: "2017-02-06 08:58",
next: "2m-2m",
},
} {
last, err := time.ParseInLocation(shortForm, t.last, time.Local)
c.Assert(err, IsNil)
fakeNow, err := time.ParseInLocation(shortForm, t.now, time.Local)
c.Assert(err, IsNil)
restorer := timeutil.MockTimeNow(func() time.Time {
return fakeNow
})
defer restorer()
sched, err := timeutil.ParseSchedule(t.schedule)
c.Assert(err, IsNil)
minDist, maxDist := parse(c, t.next)
next := timeutil.Next(sched, last)
c.Check(next >= minDist && next <= maxDist, Equals, true, Commentf("invalid distance for schedule %q with last refresh %q, now %q, expected %v, got %v", t.schedule, t.last, t.now, t.next, next))
}
}