diff --git a/coderd/workspacebuilds_test.go b/coderd/workspacebuilds_test.go index 800076eaff..b625bb6f7c 100644 --- a/coderd/workspacebuilds_test.go +++ b/coderd/workspacebuilds_test.go @@ -1258,8 +1258,15 @@ func TestWorkspaceBuildStatus(t *testing.T) { // assert an audit log has been created for workspace stopping numLogs++ // add an audit log for workspace_build stop - require.Len(t, auditor.AuditLogs(), numLogs) - require.Equal(t, database.AuditActionStop, auditor.AuditLogs()[numLogs-1].Action) + // Audit logs are written asynchronously to build completion, so poll + // until the expected log appears. + require.Eventually(t, func() bool { + return len(auditor.AuditLogs()) == numLogs && + auditor.Contains(t, database.AuditLog{ + Action: database.AuditActionStop, + ResourceType: database.ResourceTypeWorkspaceBuild, + }) + }, testutil.WaitShort, testutil.IntervalFast) _ = closeDaemon.Close() // after successful cancel is "canceled" diff --git a/coderd/workspaces_test.go b/coderd/workspaces_test.go index b1c8136b07..09ad56ca66 100644 --- a/coderd/workspaces_test.go +++ b/coderd/workspaces_test.go @@ -4610,16 +4610,20 @@ func TestWorkspaceDormant(t *testing.T) { require.NoError(t, err, "fetch updated workspace") require.Nil(t, updatedWs.DormantAt) - // There should be an audit log for both the dormancy update and the start. - require.Len(t, auditor.AuditLogs(), 2) - require.True(t, auditor.Contains(t, database.AuditLog{ - Action: database.AuditActionWrite, - ResourceType: database.ResourceTypeWorkspace, - })) - require.True(t, auditor.Contains(t, database.AuditLog{ - Action: database.AuditActionStart, - ResourceType: database.ResourceTypeWorkspaceBuild, - })) + // There should be an audit log for both the dormancy update and the + // start. Audit logs are written asynchronously to build completion, + // so poll until both appear. + require.Eventually(t, func() bool { + return len(auditor.AuditLogs()) == 2 && + auditor.Contains(t, database.AuditLog{ + Action: database.AuditActionWrite, + ResourceType: database.ResourceTypeWorkspace, + }) && + auditor.Contains(t, database.AuditLog{ + Action: database.AuditActionStart, + ResourceType: database.ResourceTypeWorkspaceBuild, + }) + }, testutil.WaitShort, testutil.IntervalFast) }) }