diff --git a/coderd/workspaceagents.go b/coderd/workspaceagents.go index 7cf62d07ebd91..96f8d570e8152 100644 --- a/coderd/workspaceagents.go +++ b/coderd/workspaceagents.go @@ -915,7 +915,9 @@ func convertWorkspaceAgent(derpMap *tailcfg.DERPMap, coordinator tailnet.Coordin // to start up. workspaceAgent.Status = codersdk.WorkspaceAgentConnecting } - case dbAgent.DisconnectedAt.Time.After(dbAgent.LastConnectedAt.Time): + // We check before instead of after because last connected at and + // disconnected at can be equal timestamps in tight-timed tests. + case !dbAgent.DisconnectedAt.Time.Before(dbAgent.LastConnectedAt.Time): // If we've disconnected after our last connection, we know the // agent is no longer connected. workspaceAgent.Status = codersdk.WorkspaceAgentDisconnected diff --git a/coderd/workspaces_test.go b/coderd/workspaces_test.go index 54b9aaf3cf049..682335dc6d5c8 100644 --- a/coderd/workspaces_test.go +++ b/coderd/workspaces_test.go @@ -1402,8 +1402,12 @@ func TestWorkspaceUpdateTTL(t *testing.T) { require.Equal(t, testCase.ttlMillis, updated.TTLMillis, "expected autostop ttl to equal requested") - require.Len(t, auditor.AuditLogs, 7) - assert.Equal(t, database.AuditActionWrite, auditor.AuditLogs[6].Action) + require.Eventually(t, func() bool { + if len(auditor.AuditLogs) != 7 { + return false + } + return auditor.AuditLogs[6].Action == database.AuditActionWrite + }, testutil.WaitMedium, testutil.IntervalFast, "expected audit log to be written") }) }