@@ -47,6 +47,29 @@ func newMetricsCache(t *testing.T, log slog.Logger, clock quartz.Clock, interval
4747return cache ,db
4848}
4949
50+ func awaitCacheRefresh (ctx context.Context ,t * testing.T ,clock * quartz.Mock )chan struct {} {
51+ t .Helper ()
52+ ch := make (chan struct {})
53+ trapNow := clock .Trap ().Now ()
54+ trapReset := clock .Trap ().TickerReset ()
55+ go func () {
56+ defer close (ch )
57+ defer trapReset .Close ()
58+ defer trapNow .Close ()
59+
60+ // Wait for first Now() call
61+ trapNow .MustWait (ctx ).MustRelease (ctx )
62+ // Wait for second Now() call
63+ trapNow .MustWait (ctx ).MustRelease (ctx )
64+ // Wait for first Reset() call
65+ trapReset .MustWait (ctx ).MustRelease (ctx )
66+ // Wait for second Reset() call
67+ trapReset .MustWait (ctx ).MustRelease (ctx )
68+ }()
69+
70+ return ch
71+ }
72+
5073func TestCache_TemplateWorkspaceOwners (t * testing.T ) {
5174t .Parallel ()
5275var ()
@@ -292,11 +315,14 @@ func TestCache_BuildTime(t *testing.T) {
292315func TestCache_DeploymentStats (t * testing.T ) {
293316t .Parallel ()
294317
318+ ctx := testutil .Context (t ,testutil .WaitShort )
319+
295320var (
296321log = testutil .Logger (t )
297322clock = quartz .NewMock (t )
298323cache ,db = newMetricsCache (t ,log ,clock , metricscache.Intervals {
299- DeploymentStats :testutil .IntervalFast ,
324+ TemplateBuildTimes :testutil .IntervalFast ,
325+ DeploymentStats :testutil .IntervalFast ,
300326},false )
301327)
302328
@@ -323,9 +349,10 @@ func TestCache_DeploymentStats(t *testing.T) {
323349})
324350require .NoError (t ,err )
325351
326- require .Eventually (t ,func ()bool {
327- var ok bool
328- stat ,ok := cache .DeploymentStats ()
329- return ok && stat .SessionCount .VSCode == 1
330- },testutil .WaitLong ,testutil .IntervalMedium )
352+ done := awaitCacheRefresh (ctx ,t ,clock )
353+ testutil .TryReceive (ctx ,t ,done )
354+
355+ stat ,ok := cache .DeploymentStats ()
356+ require .True (t ,ok ,"cache should be populated after refresh" )
357+ require .Equal (t ,int64 (1 ),stat .SessionCount .VSCode )
331358}