- Notifications
You must be signed in to change notification settings - Fork1k
feat(coderd/database/dbtestutil): set default database timezone to non-UTC in unit tests#9672
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
17 commits Select commitHold shift + click to select a range
355b16b
feat(coderd/database/dbtestutil): set a random timezone in the db
johnstcn986db8b
remove check for tz after set
johnstcnfc3ba52
clitest: fix timestamp regex to match non-UTC timezones
johnstcnb3c559a
set fixed timezone in some existing unit tests
johnstcnf57dc9d
fixup! set fixed timezone in some existing unit tests
johnstcnd3d1274
fix timezone bug in ActivityBumpWorkspace query
johnstcn9f1f5c6
refactor to separate package, allow specifying tz
johnstcn7c2ff2f
set db timezone to canada/newfoundland by default
johnstcneecfcdb
test activity bump in multiple timezones
johnstcnd327131
remove unnecessary IN TIME ZONE UTC in activitybump query
johnstcn7680984
clarify use of multiple timezones in test
johnstcnd051bbb
remove deprecation and add linter rule
johnstcnda059dc
fix typo
johnstcn0d425ba
bump allowed withinDuration for TestWorkpaceActivityBump
johnstcnd97ff2f
make Test_ActivityBumpWorkspace more flake-resistant
johnstcnf18c6b5
replace conditional sleep with a slower unconditional sleep that shou…
johnstcn5a8aa8b
modify logic slightly in TestWorkspaceActivityBump from "deadline mus…
johnstcnFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
2 changes: 1 addition & 1 deletioncli/clitest/golden.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
244 changes: 126 additions & 118 deletionscoderd/activitybump_internal_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -2,7 +2,6 @@ package coderd | ||
import ( | ||
"database/sql" | ||
"testing" | ||
"time" | ||
@@ -22,6 +21,16 @@ import ( | ||
func Test_ActivityBumpWorkspace(t *testing.T) { | ||
t.Parallel() | ||
// We test the below in multiple timezones specifically | ||
// chosen to trigger timezone-related bugs. | ||
timezones := []string{ | ||
"Asia/Kolkata", // No DST, positive fractional offset | ||
"Canada/Newfoundland", // DST, negative fractional offset | ||
"Europe/Paris", // DST, positive offset | ||
"US/Arizona", // No DST, negative offset | ||
"UTC", // Baseline | ||
} | ||
for _, tt := range []struct { | ||
name string | ||
transition database.WorkspaceTransition | ||
@@ -92,117 +101,124 @@ func Test_ActivityBumpWorkspace(t *testing.T) { | ||
}, | ||
} { | ||
tt := tt | ||
for _, tz := range timezones { | ||
johnstcn marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
tz := tz | ||
t.Run(tt.name+"/"+tz, func(t *testing.T) { | ||
t.Parallel() | ||
var ( | ||
now = dbtime.Now() | ||
ctx = testutil.Context(t, testutil.WaitShort) | ||
log = slogtest.Make(t, nil) | ||
db, _ = dbtestutil.NewDB(t, dbtestutil.WithTimezone(tz)) | ||
org = dbgen.Organization(t, db, database.Organization{}) | ||
user = dbgen.User(t, db, database.User{ | ||
Status: database.UserStatusActive, | ||
}) | ||
_ = dbgen.OrganizationMember(t, db, database.OrganizationMember{ | ||
UserID: user.ID, | ||
OrganizationID: org.ID, | ||
}) | ||
templateVersion = dbgen.TemplateVersion(t, db, database.TemplateVersion{ | ||
OrganizationID: org.ID, | ||
CreatedBy: user.ID, | ||
}) | ||
template = dbgen.Template(t, db, database.Template{ | ||
OrganizationID: org.ID, | ||
ActiveVersionID: templateVersion.ID, | ||
CreatedBy: user.ID, | ||
}) | ||
ws = dbgen.Workspace(t, db, database.Workspace{ | ||
OwnerID: user.ID, | ||
OrganizationID: org.ID, | ||
TemplateID: template.ID, | ||
Ttl: sql.NullInt64{Valid: true, Int64: int64(tt.workspaceTTL)}, | ||
}) | ||
job = dbgen.ProvisionerJob(t, db, database.ProvisionerJob{ | ||
OrganizationID: org.ID, | ||
CompletedAt: tt.jobCompletedAt, | ||
}) | ||
_ = dbgen.WorkspaceResource(t, db, database.WorkspaceResource{ | ||
JobID: job.ID, | ||
}) | ||
buildID = uuid.New() | ||
) | ||
var buildNumber int32 = 1 | ||
// Insert a number of previous workspace builds. | ||
for i := 0; i < 5; i++ { | ||
insertPrevWorkspaceBuild(t, db, org.ID, templateVersion.ID, ws.ID, database.WorkspaceTransitionStart, buildNumber) | ||
buildNumber++ | ||
insertPrevWorkspaceBuild(t, db, org.ID, templateVersion.ID, ws.ID, database.WorkspaceTransitionStop, buildNumber) | ||
buildNumber++ | ||
} | ||
// dbgen.WorkspaceBuild automatically sets deadline to now+1 hour if not set | ||
var buildDeadline time.Time | ||
if tt.buildDeadlineOffset != nil { | ||
buildDeadline = now.Add(*tt.buildDeadlineOffset) | ||
} | ||
var maxDeadline time.Time | ||
if tt.maxDeadlineOffset != nil { | ||
maxDeadline = now.Add(*tt.maxDeadlineOffset) | ||
} | ||
err := db.InsertWorkspaceBuild(ctx, database.InsertWorkspaceBuildParams{ | ||
ID: buildID, | ||
CreatedAt: dbtime.Now(), | ||
UpdatedAt: dbtime.Now(), | ||
BuildNumber: buildNumber, | ||
InitiatorID: user.ID, | ||
Reason: database.BuildReasonInitiator, | ||
WorkspaceID: ws.ID, | ||
JobID: job.ID, | ||
TemplateVersionID: templateVersion.ID, | ||
Transition: tt.transition, | ||
Deadline: buildDeadline, | ||
MaxDeadline: maxDeadline, | ||
}) | ||
require.NoError(t, err, "unexpected error inserting workspace build") | ||
bld, err := db.GetWorkspaceBuildByID(ctx, buildID) | ||
require.NoError(t, err, "unexpected error fetching inserted workspace build") | ||
// Validate our initial state before bump | ||
require.Equal(t, tt.transition, bld.Transition, "unexpected transition before bump") | ||
require.Equal(t, tt.jobCompletedAt.Time.UTC(), job.CompletedAt.Time.UTC(), "unexpected job completed at before bump") | ||
require.Equal(t, buildDeadline.UTC(), bld.Deadline.UTC(), "unexpected build deadline before bump") | ||
require.Equal(t, maxDeadline.UTC(), bld.MaxDeadline.UTC(), "unexpected max deadline before bump") | ||
require.Equal(t, tt.workspaceTTL, time.Duration(ws.Ttl.Int64), "unexpected workspace TTL before bump") | ||
// Wait a bit before bumping as dbtime is rounded to the nearest millisecond. | ||
// This should also hopefully be enough for Windows time resolution to register | ||
// a tick (win32 max timer resolution is apparently between 0.5 and 15.6ms) | ||
<-time.After(testutil.IntervalFast) | ||
// Bump duration is measured from the time of the bump, so we measure from here. | ||
start := dbtime.Now() | ||
activityBumpWorkspace(ctx, log, db, bld.WorkspaceID) | ||
end := dbtime.Now() | ||
// Validate our state after bump | ||
updatedBuild, err := db.GetLatestWorkspaceBuildByWorkspaceID(ctx, bld.WorkspaceID) | ||
require.NoError(t, err, "unexpected error getting latest workspace build") | ||
require.Equal(t, bld.MaxDeadline.UTC(), updatedBuild.MaxDeadline.UTC(), "max_deadline should not have changed") | ||
if tt.expectedBump == 0 { | ||
require.Equal(t, bld.UpdatedAt.UTC(), updatedBuild.UpdatedAt.UTC(), "should not have bumped updated_at") | ||
require.Equal(t, bld.Deadline.UTC(), updatedBuild.Deadline.UTC(), "should not have bumped deadline") | ||
return | ||
} | ||
require.NotEqual(t, bld.UpdatedAt.UTC(), updatedBuild.UpdatedAt.UTC(), "should have bumped updated_at") | ||
if tt.maxDeadlineOffset != nil { | ||
require.Equal(t, bld.MaxDeadline.UTC(), updatedBuild.MaxDeadline.UTC(), "new deadline must equal original max deadline") | ||
return | ||
} | ||
// Assert that the bump occurred between start and end. | ||
expectedDeadlineStart := start.Add(tt.expectedBump) | ||
expectedDeadlineEnd := end.Add(tt.expectedBump) | ||
require.GreaterOrEqual(t, updatedBuild.Deadline, expectedDeadlineStart, "new deadline should be greater than or equal to start") | ||
require.LessOrEqual(t, updatedBuild.Deadline, expectedDeadlineEnd, "new deadline should be lesser than or equal to end") | ||
}) | ||
} | ||
} | ||
} | ||
@@ -223,11 +239,3 @@ func insertPrevWorkspaceBuild(t *testing.T, db database.Store, orgID, tvID, work | ||
Transition: transition, | ||
}) | ||
} | ||
26 changes: 15 additions & 11 deletionscoderd/activitybump_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.