From 3b7380fa006510c1d8931711a8b30662d99754d7 Mon Sep 17 00:00:00 2001 From: Spike Curtis Date: Mon, 8 Apr 2024 16:22:33 +0400 Subject: [PATCH] fix: fix race in assertWorkspaceLastUsedAtUpdated (#12899) fixes #12789 Stats are collected asynchronously with respect to sessions ending. Flush repeatedly so that we pick up the collection if we missed it. --- coderd/workspaceapps/apptest/apptest.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/coderd/workspaceapps/apptest/apptest.go b/coderd/workspaceapps/apptest/apptest.go index dea232c867..2e91953d67 100644 --- a/coderd/workspaceapps/apptest/apptest.go +++ b/coderd/workspaceapps/apptest/apptest.go @@ -1765,9 +1765,11 @@ func assertWorkspaceLastUsedAtUpdated(t testing.TB, details *Details) { require.NotNil(t, details.Workspace, "can't assert LastUsedAt on a nil workspace!") before, err := details.SDKClient.Workspace(context.Background(), details.Workspace.ID) require.NoError(t, err) - // Wait for stats to fully flush. - details.FlushStats() require.Eventually(t, func() bool { + // We may need to flush multiple times, since the stats from the app we are testing might be + // collected asynchronously from when we see the connection close, and thus, could race + // against being flushed. + details.FlushStats() after, err := details.SDKClient.Workspace(context.Background(), details.Workspace.ID) return assert.NoError(t, err) && after.LastUsedAt.After(before.LastUsedAt) }, testutil.WaitShort, testutil.IntervalMedium)