@@ -379,13 +379,15 @@ func TestAgentStats(t *testing.T) {
379
379
t .Cleanup (cancelFunc )
380
380
381
381
db , pubsub := dbtestutil .NewDB (t )
382
- log := slogtest .Make (t , nil )
382
+ log := slogtest .Make (t , nil ). Leveled ( slog . LevelDebug )
383
383
384
384
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.
385
390
batchstats .WithStore (db ),
386
- // We want our stats, and we want them NOW.
387
- batchstats .WithBatchSize (1 ),
388
- batchstats .WithInterval (time .Hour ),
389
391
batchstats .WithLogger (log ),
390
392
)
391
393
require .NoError (t , err , "create stats batcher failed" )
@@ -435,6 +437,11 @@ func TestAgentStats(t *testing.T) {
435
437
require .NoError (t , err )
436
438
}
437
439
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
+
438
445
// when
439
446
//
440
447
// Set initialCreateAfter to some time in the past, so that AgentStats would include all above PostStats,
0 commit comments