Skip to content

Commit 62a22e7

Browse files
committed
Make tests deterministic
1 parent dab4394 commit 62a22e7

File tree

2 files changed

+49
-21
lines changed

2 files changed

+49
-21
lines changed

codersdk/deployment.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2186,20 +2186,24 @@ func (d DAURequest) asRequestOption() RequestOption {
21862186
}
21872187
}
21882188

2189-
// TimezoneOffsetHour is implemented to match the javascript 'getTimezoneOffset()' function.
2189+
// TimezoneOffsetHourWithTime is implemented to match the javascript 'getTimezoneOffset()' function.
21902190
// This is the amount of time between this date evaluated in UTC and evaluated in the 'loc'
21912191
// The trivial case of times being on the same day is:
21922192
// 'time.Now().UTC().Hour() - time.Now().In(loc).Hour()'
2193-
func TimezoneOffsetHour(loc *time.Location) int {
2193+
func TimezoneOffsetHourWithTime(now time.Time, loc *time.Location) int {
21942194
if loc == nil {
21952195
// Default to UTC time to be consistent across all callers.
21962196
loc = time.UTC
21972197
}
2198-
_, offsetSec := time.Now().In(loc).Zone()
2198+
_, offsetSec := now.In(loc).Zone()
21992199
// Convert to hours and flip the sign
22002200
return -1 * offsetSec / 60 / 60
22012201
}
22022202

2203+
func TimezoneOffsetHour(loc *time.Location) int {
2204+
return TimezoneOffsetHourWithTime(time.Now(), loc)
2205+
}
2206+
22032207
func (c *Client) DeploymentDAUsLocalTZ(ctx context.Context) (*DAUsResponse, error) {
22042208
return c.DeploymentDAUs(ctx, TimezoneOffsetHour(time.Local))
22052209
}

codersdk/deployment_test.go

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ func TestTimezoneOffsets(t *testing.T) {
205205

206206
testCases := []struct {
207207
Name string
208+
Now time.Time
208209
Loc *time.Location
209210
ExpectedOffset int
210211
}{
@@ -213,29 +214,52 @@ func TestTimezoneOffsets(t *testing.T) {
213214
Loc: time.UTC,
214215
ExpectedOffset: 0,
215216
},
216-
// The following test cases are broken re: daylight savings
217-
//{
218-
// Name: "Eastern",
219-
// Loc: must(time.LoadLocation("America/New_York")),
220-
// ExpectedOffset: -4,
221-
// },
222-
//{
223-
// Name: "Central",
224-
// Loc: must(time.LoadLocation("America/Chicago")),
225-
// ExpectedOffset: -5,
226-
// },
227-
//{
228-
// Name: "Ireland",
229-
// Loc: must(time.LoadLocation("Europe/Dublin")),
230-
// ExpectedOffset: 1,
231-
// },
217+
218+
{
219+
Name: "Eastern",
220+
Now: time.Date(2021, 2, 1, 0, 0, 0, 0, time.UTC),
221+
Loc: must(time.LoadLocation("America/New_York")),
222+
ExpectedOffset: 5,
223+
},
224+
{
225+
// Daylight savings is on the 14th of March to Nov 7 in 2021
226+
Name: "EasternDaylightSavings",
227+
Now: time.Date(2021, 3, 16, 0, 0, 0, 0, time.UTC),
228+
Loc: must(time.LoadLocation("America/New_York")),
229+
ExpectedOffset: 4,
230+
},
231+
{
232+
Name: "Central",
233+
Now: time.Date(2021, 2, 1, 0, 0, 0, 0, time.UTC),
234+
Loc: must(time.LoadLocation("America/Chicago")),
235+
ExpectedOffset: 6,
236+
},
237+
{
238+
Name: "CentralDaylightSavings",
239+
Now: time.Date(2021, 3, 16, 0, 0, 0, 0, time.UTC),
240+
Loc: must(time.LoadLocation("America/Chicago")),
241+
ExpectedOffset: 5,
242+
},
243+
{
244+
Name: "Ireland",
245+
Now: time.Date(2021, 2, 1, 0, 0, 0, 0, time.UTC),
246+
Loc: must(time.LoadLocation("Europe/Dublin")),
247+
ExpectedOffset: 0,
248+
},
249+
{
250+
Name: "IrelandDaylightSavings",
251+
Now: time.Date(2021, 4, 3, 0, 0, 0, 0, time.UTC),
252+
Loc: must(time.LoadLocation("Europe/Dublin")),
253+
ExpectedOffset: -1,
254+
},
232255
{
233256
Name: "HalfHourTz",
257+
Now: time.Date(2024, 1, 20, 6, 0, 0, 0, must(time.LoadLocation("Asia/Yangon"))),
234258
// This timezone is +6:30, but the function rounds to the nearest hour.
235259
// This is intentional because our DAUs endpoint only covers 1-hour offsets.
236260
// If the user is in a non-hour timezone, they get the closest hour bucket.
237261
Loc: must(time.LoadLocation("Asia/Yangon")),
238-
ExpectedOffset: 6,
262+
ExpectedOffset: -6,
239263
},
240264
}
241265

@@ -244,7 +268,7 @@ func TestTimezoneOffsets(t *testing.T) {
244268
t.Run(c.Name, func(t *testing.T) {
245269
t.Parallel()
246270

247-
offset := codersdk.TimezoneOffsetHour(c.Loc)
271+
offset := codersdk.TimezoneOffsetHourWithTime(c.Now, c.Loc)
248272
require.Equal(t, c.ExpectedOffset, offset)
249273
})
250274
}

0 commit comments

Comments
 (0)