Skip to content

Commit 4687f2a

Browse files
test: add is-within-range test
1 parent 30a924d commit 4687f2a

File tree

2 files changed

+84
-1
lines changed

2 files changed

+84
-1
lines changed

coderd/prebuilds/global_snapshot.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func (s GlobalSnapshot) FilterByPreset(presetID uuid.UUID) (*PresetSnapshot, err
5050
if !found {
5151
return nil, xerrors.Errorf("no preset found with ID %q", presetID)
5252
}
53-
53+
5454
prebuildSchedules := slice.Filter(s.PrebuildSchedules, func(schedule database.TemplateVersionPresetPrebuildSchedule) bool {
5555
return schedule.PresetID == presetID
5656
})

coderd/schedule/cron/cron_test.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,89 @@ func Test_Weekly(t *testing.T) {
163163
}
164164
}
165165

166+
func TestIsWithinRange(t *testing.T) {
167+
t.Parallel()
168+
testCases := []struct {
169+
name string
170+
spec string
171+
at time.Time
172+
expectedWithinRange bool
173+
}{
174+
{
175+
name: "Right before the start of the time range",
176+
spec: "* 9-18 * * 1-5",
177+
at: mustParseTime(t, time.RFC1123, "Mon, 02 Jun 2025 8:58:59 UTC"),
178+
expectedWithinRange: false,
179+
},
180+
{
181+
name: "Start of the time range",
182+
spec: "* 9-18 * * 1-5",
183+
at: mustParseTime(t, time.RFC1123, "Mon, 02 Jun 2025 8:59:00 UTC"),
184+
expectedWithinRange: true,
185+
},
186+
{
187+
name: "9AM - One minute after the start of the time range",
188+
spec: "* 9-18 * * 1-5",
189+
at: mustParseTime(t, time.RFC1123, "Mon, 02 Jun 2025 9:00:00 UTC"),
190+
expectedWithinRange: true,
191+
},
192+
{
193+
name: "2PM - The middle of the time range",
194+
spec: "* 9-18 * * 1-5",
195+
at: mustParseTime(t, time.RFC1123, "Mon, 02 Jun 2025 14:00:00 UTC"),
196+
expectedWithinRange: true,
197+
},
198+
{
199+
name: "6PM - Around one hour before the end of the time range",
200+
spec: "* 9-18 * * 1-5",
201+
at: mustParseTime(t, time.RFC1123, "Mon, 02 Jun 2025 18:00:00 UTC"),
202+
expectedWithinRange: true,
203+
},
204+
{
205+
name: "End of the time range",
206+
spec: "* 9-18 * * 1-5",
207+
at: mustParseTime(t, time.RFC1123, "Mon, 02 Jun 2025 18:58:59 UTC"),
208+
expectedWithinRange: true,
209+
},
210+
{
211+
name: "Right after the end of the time range",
212+
spec: "* 9-18 * * 1-5",
213+
at: mustParseTime(t, time.RFC1123, "Mon, 02 Jun 2025 18:59:00 UTC"),
214+
expectedWithinRange: false,
215+
},
216+
{
217+
name: "7PM - Around one minute after the end of the time range",
218+
spec: "* 9-18 * * 1-5",
219+
at: mustParseTime(t, time.RFC1123, "Mon, 02 Jun 2025 19:00:00 UTC"),
220+
expectedWithinRange: false,
221+
},
222+
{
223+
name: "2AM - Significantly outside the time range",
224+
spec: "* 9-18 * * 1-5",
225+
at: mustParseTime(t, time.RFC1123, "Mon, 02 Jun 2025 02:00:00 UTC"),
226+
expectedWithinRange: false,
227+
},
228+
}
229+
230+
for _, testCase := range testCases {
231+
testCase := testCase
232+
t.Run(testCase.name, func(t *testing.T) {
233+
t.Parallel()
234+
sched, err := cron.Weekly(testCase.spec)
235+
require.NoError(t, err)
236+
withinRange := sched.IsWithinRange(testCase.at)
237+
require.Equal(t, testCase.expectedWithinRange, withinRange)
238+
})
239+
}
240+
}
241+
242+
func mustParseTime(t *testing.T, layout, value string) time.Time {
243+
t.Helper()
244+
parsedTime, err := time.Parse(layout, value)
245+
require.NoError(t, err)
246+
return parsedTime
247+
}
248+
166249
func mustLocation(t *testing.T, s string) *time.Location {
167250
t.Helper()
168251
loc, err := time.LoadLocation(s)

0 commit comments

Comments
 (0)