Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit591dd91

Browse files
committed
extract awaitDoTick() test helper
1 parent725eeb0 commit591dd91

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
defercancel()
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-
defertrapReset.Close()
252241

242+
done:=awaitDoTick(ctx,t,clk)
253243
closer:=dbpurge.New(ctx,logger,db,clk)
254244
defercloser.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+
funcawaitDoTick(ctx context.Context,t*testing.T,clk*quartz.Mock)chanstruct{} {
270+
t.Helper()
271+
ch:=make(chanstruct{})
272+
trapStop:=clk.Trap().TickerStop()
273+
trapReset:=clk.Trap().TickerReset()
274+
gofunc() {
275+
deferclose(ch)
276+
defertrapStop.Close()
277+
defertrapReset.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+
returnch
295+
}
296+
287297
funcassertNoWorkspaceAgentLogs(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)

[8]ページ先頭

©2009-2025 Movatter.jp