Skip to content

Commit 591dd91

Browse files
committed
extract awaitDoTick() test helper
1 parent 725eeb0 commit 591dd91

File tree

1 file changed

+33
-23
lines changed

1 file changed

+33
-23
lines changed

coderd/database/dbpurge/dbpurge_test.go

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,11 @@ func TestPurge(t *testing.T) {
4343
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
4444
defer cancel()
4545

46-
clk := quartz.NewMock(t)
47-
4846
// 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)
5149
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.
6051
require.NoError(t, purger.Close())
6152
}
6253

@@ -247,20 +238,11 @@ func TestDeleteOldWorkspaceAgentLogs(t *testing.T) {
247238
// when dbpurge runs
248239

249240
// After dbpurge completes, the ticker is reset. Trap this call.
250-
trapReset := clk.Trap().TickerReset()
251-
defer trapReset.Close()
252241

242+
done := awaitDoTick(ctx, t, clk)
253243
closer := dbpurge.New(ctx, logger, db, clk)
254244
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.
264246

265247
// then logs related to the following agents should be deleted:
266248
// Agent A1 never connected, was created before the threshold, and is not the
@@ -284,6 +266,34 @@ func TestDeleteOldWorkspaceAgentLogs(t *testing.T) {
284266
assertWorkspaceAgentLogs(ctx, t, db, agentE1.ID, "agent e1 logs should be retained")
285267
}
286268

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+
287297
func assertNoWorkspaceAgentLogs(ctx context.Context, t *testing.T, db database.Store, agentID uuid.UUID) {
288298
t.Helper()
289299
agentLogs, err := db.GetWorkspaceAgentLogsAfter(ctx, database.GetWorkspaceAgentLogsAfterParams{

0 commit comments

Comments
 (0)