Skip to content

Commit 774c9dd

Browse files
authored
test: add logging to TestWorkspaceActivityBump/Dial (coder#15089)
Added logging information to output timing information of this for loop. If we get another failure, this timing information should be helpful. I also made the time drift allowed (was 10s) to match the maximum waiting time of the for loop. It's not perfect, but now the loop should take max ~15s, and the time comparison will allow 15s leeway.
1 parent 416d67b commit 774c9dd

File tree

1 file changed

+43
-8
lines changed

1 file changed

+43
-8
lines changed

coderd/activitybump_test.go

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,23 +124,58 @@ func TestWorkspaceActivityBump(t *testing.T) {
124124
return
125125
}
126126

127-
var updatedAfter time.Time
127+
// maxTimeDrift is how long we are willing wait for a deadline to
128+
// be increased. Since it could have been bumped at the intial
129+
maxTimeDrift := testutil.WaitMedium
130+
131+
updatedAfter := dbtime.Now()
132+
// waitedFor is purely for debugging failed tests. If a test fails,
133+
// it helps to know how long it took for the deadline bump to be
134+
// detected. The longer this takes, the more likely time drift will
135+
// affect the results.
136+
waitedFor := time.Now()
137+
// lastChecked is for logging within the Eventually loop.
138+
// Debouncing log lines to every second to prevent spam.
139+
lastChecked := time.Time{}
140+
// checks is for keeping track of the average check time.
141+
// If CI is running slow, this could be useful to know checks
142+
// are taking longer than expected.
143+
checks := 0
144+
128145
// The Deadline bump occurs asynchronously.
129146
require.Eventuallyf(t,
130147
func() bool {
148+
checks++
131149
workspace, err = client.Workspace(ctx, workspace.ID)
132150
require.NoError(t, err)
133-
updatedAfter = dbtime.Now()
134-
if workspace.LatestBuild.Deadline.Time.Equal(firstDeadline) {
135-
updatedAfter = time.Now()
136-
return false
151+
152+
hasBumped := !workspace.LatestBuild.Deadline.Time.Equal(firstDeadline)
153+
154+
// Always make sure to log this information, even on the last check.
155+
// The last check is the most important, as if this loop is acting
156+
// slow, the last check could be the cause of the failure.
157+
if time.Since(lastChecked) > time.Second || hasBumped {
158+
avgCheckTime := time.Since(waitedFor) / time.Duration(checks)
159+
t.Logf("deadline detect: bumped=%t since_last_check=%s avg_check_dur=%s checks=%d deadline=%v",
160+
hasBumped, time.Since(updatedAfter), avgCheckTime, checks, workspace.LatestBuild.Deadline.Time)
161+
lastChecked = time.Now()
137162
}
138-
return true
163+
164+
updatedAfter = dbtime.Now()
165+
return hasBumped
139166
},
140-
testutil.WaitLong, testutil.IntervalFast,
167+
//nolint: gocritic // maxTimeDrift is a testutil time
168+
maxTimeDrift, testutil.IntervalFast,
141169
"deadline %v never updated", firstDeadline,
142170
)
143171

172+
// This log line helps establish how long it took for the deadline
173+
// to be detected as bumped.
174+
t.Logf("deadline bump detected: %v, waited for %s",
175+
workspace.LatestBuild.Deadline.Time,
176+
time.Since(waitedFor),
177+
)
178+
144179
require.Greater(t, workspace.LatestBuild.Deadline.Time, updatedAfter)
145180

146181
// If the workspace has a max deadline, the deadline must not exceed
@@ -156,7 +191,7 @@ func TestWorkspaceActivityBump(t *testing.T) {
156191
firstDeadline, workspace.LatestBuild.Deadline.Time, now,
157192
now.Sub(workspace.LatestBuild.Deadline.Time),
158193
)
159-
require.WithinDuration(t, dbtime.Now().Add(ttl), workspace.LatestBuild.Deadline.Time, testutil.WaitShort)
194+
require.WithinDuration(t, now.Add(ttl), workspace.LatestBuild.Deadline.Time, maxTimeDrift)
160195
}
161196
}
162197

0 commit comments

Comments
 (0)