@@ -43,20 +43,11 @@ func TestPurge(t *testing.T) {
43
43
ctx ,cancel := context .WithTimeout (context .Background (),testutil .WaitShort )
44
44
defer cancel ()
45
45
46
- clk := quartz .NewMock (t )
47
-
48
46
// We want to make sure dbpurge is actually started so that this test is meaningful.
49
- trapStop := clk . Trap (). TickerStop ( )
50
-
47
+ clk := quartz . NewMock ( t )
48
+ done := awaitDoTick ( ctx , t , clk )
51
49
purger := dbpurge .New (context .Background (),slogtest .Make (t ,nil ),dbmem .New (),clk )
52
-
53
- // Wait for the initial nanosecond tick.
54
- clk .Advance (time .Nanosecond ).MustWait (ctx )
55
- // Wait for ticker.Stop call that happens in the goroutine.
56
- trapStop .MustWait (ctx ).Release ()
57
- // Stop the trap now to avoid blocking further.
58
- trapStop .Close ()
59
-
50
+ <- done // wait for doTick() to run.
60
51
require .NoError (t ,purger .Close ())
61
52
}
62
53
@@ -247,20 +238,11 @@ func TestDeleteOldWorkspaceAgentLogs(t *testing.T) {
247
238
// when dbpurge runs
248
239
249
240
// After dbpurge completes, the ticker is reset. Trap this call.
250
- trapReset := clk .Trap ().TickerReset ()
251
- defer trapReset .Close ()
252
241
242
+ done := awaitDoTick (ctx ,t ,clk )
253
243
closer := dbpurge .New (ctx ,logger ,db ,clk )
254
244
defer closer .Close ()
255
- // Wait for the initial nanosecond tick.
256
- clk .Advance (time .Nanosecond ).MustWait (ctx )
257
-
258
- trapReset .MustWait (ctx ).Release ()// Wait for ticker.Reset()
259
- d ,w := clk .AdvanceNext ()
260
- require .Equal (t ,10 * time .Minute ,d )
261
-
262
- closer .Close ()// doTick() has now run.
263
- w .MustWait (ctx )
245
+ <- done // doTick() has now run.
264
246
265
247
// then logs related to the following agents should be deleted:
266
248
// Agent A1 never connected, was created before the threshold, and is not the
@@ -284,6 +266,34 @@ func TestDeleteOldWorkspaceAgentLogs(t *testing.T) {
284
266
assertWorkspaceAgentLogs (ctx ,t ,db ,agentE1 .ID ,"agent e1 logs should be retained" )
285
267
}
286
268
269
+ func awaitDoTick (ctx context.Context ,t * testing.T ,clk * quartz.Mock )chan struct {} {
270
+ t .Helper ()
271
+ ch := make (chan struct {})
272
+ trapStop := clk .Trap ().TickerStop ()
273
+ trapReset := clk .Trap ().TickerReset ()
274
+ go func () {
275
+ defer close (ch )
276
+ defer trapStop .Close ()
277
+ defer trapReset .Close ()
278
+ // Wait for the initial nanosecond tick.
279
+ trapReset .MustWait (ctx ).Release ()
280
+ clk .Advance (time .Nanosecond ).MustWait (ctx )
281
+ // Wait for the ticker stop event.
282
+ trapStop .MustWait (ctx ).Release ()
283
+ // doTick runs here. Wait for the next
284
+ // ticker reset event that signifies it's completed.
285
+ trapReset .MustWait (ctx ).Release ()
286
+ // Ensure that the duration is reset to the original delay.
287
+ d ,w := clk .AdvanceNext ()
288
+ assert .Equal (t ,10 * time .Minute ,d )
289
+ if ! assert .NoError (t ,w .Wait (ctx )) {
290
+ return
291
+ }
292
+ }()
293
+
294
+ return ch
295
+ }
296
+
287
297
func assertNoWorkspaceAgentLogs (ctx context.Context ,t * testing.T ,db database.Store ,agentID uuid.UUID ) {
288
298
t .Helper ()
289
299
agentLogs ,err := db .GetWorkspaceAgentLogsAfter (ctx , database.GetWorkspaceAgentLogsAfterParams {