Skip to content

Commit 22f31e7

Browse files
authored
fix(coderd/prometheusmetrics): close batcher to force flush before asserting agent stats (#9465)
1 parent fd17857 commit 22f31e7

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

coderd/prometheusmetrics/prometheusmetrics_test.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -379,13 +379,15 @@ func TestAgentStats(t *testing.T) {
379379
t.Cleanup(cancelFunc)
380380

381381
db, pubsub := dbtestutil.NewDB(t)
382-
log := slogtest.Make(t, nil)
382+
log := slogtest.Make(t, nil).Leveled(slog.LevelDebug)
383383

384384
batcher, closeBatcher, err := batchstats.New(ctx,
385+
// We had previously set the batch size to 1 here, but that caused
386+
// intermittent test flakes due to a race between the batcher completing
387+
// its flush and the test asserting that the metrics were collected.
388+
// Instead, we close the batcher after all stats have been posted, which
389+
// forces a flush.
385390
batchstats.WithStore(db),
386-
// We want our stats, and we want them NOW.
387-
batchstats.WithBatchSize(1),
388-
batchstats.WithInterval(time.Hour),
389391
batchstats.WithLogger(log),
390392
)
391393
require.NoError(t, err, "create stats batcher failed")
@@ -435,6 +437,11 @@ func TestAgentStats(t *testing.T) {
435437
require.NoError(t, err)
436438
}
437439

440+
// Ensure that all stats are flushed to the database
441+
// before we query them. We do not expect any more stats
442+
// to be posted after this.
443+
closeBatcher()
444+
438445
// when
439446
//
440447
// Set initialCreateAfter to some time in the past, so that AgentStats would include all above PostStats,

0 commit comments

Comments
 (0)