Skip to content
Prev Previous commit
Next Next commit
maybe fix race conditions?
  • Loading branch information
johnstcn committed Jan 16, 2024
commit 1f9012e4195f61327b8dc32854fd85a6c791fda4
19 changes: 13 additions & 6 deletions coderd/workspaceapps/apptest/apptest.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func Run(t *testing.T, appHostIsPrimary bool, factory DeploymentFactory) {
require.NoError(t, err)
require.True(t, loc.Query().Has("message"))
require.True(t, loc.Query().Has("redirect"))
assertWorkspaceLastUsedAtNotUpdated(t, appDetails)
assertWorkspaceLastUsedAtUpdated(t, appDetails)
})

t.Run("LoginWithoutAuthOnProxy", func(t *testing.T) {
Expand Down Expand Up @@ -185,7 +185,7 @@ func Run(t *testing.T, appHostIsPrimary bool, factory DeploymentFactory) {
// request is getting stripped.
require.Equal(t, u.Path, redirectURI.Path+"/")
require.Equal(t, u.RawQuery, redirectURI.RawQuery)
assertWorkspaceLastUsedAtNotUpdated(t, appDetails)
assertWorkspaceLastUsedAtUpdated(t, appDetails)
})

t.Run("NoAccessShould404", func(t *testing.T) {
Expand Down Expand Up @@ -396,7 +396,7 @@ func Run(t *testing.T, appHostIsPrimary bool, factory DeploymentFactory) {
// TODO(@deansheather): This should be 400. There's a todo in the
// resolve request code to fix this.
require.Equal(t, http.StatusInternalServerError, resp.StatusCode)
assertWorkspaceLastUsedAtNotUpdated(t, appDetails)
assertWorkspaceLastUsedAtUpdated(t, appDetails)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

review: I'm counting this as a successful and valid attempt to access a workspace.

})
})

Expand Down Expand Up @@ -1570,16 +1570,23 @@ func assertWorkspaceLastUsedAtUpdated(t testing.TB, details *Details) {
t.Helper()

details.FlushStats()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, looping flush seems better than waiting an arbitrary time?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A loop-flush here would probably just execute after the first iteration.

ws, err := details.SDKClient.Workspace(context.Background(), details.Workspace.ID)
require.NoError(t, err)
require.Greater(t, ws.LastUsedAt, details.Workspace.LastUsedAt, "workspace LastUsedAt not updated when it should have been")
// Despite our efforts with the flush channel, this is inherently racy.
// Retry a few times to ensure that the stats collection pipeline has flushed.
require.Eventually(t, func() bool {
ws, err := details.SDKClient.Workspace(context.Background(), details.Workspace.ID)
assert.NoError(t, err)
return ws.LastUsedAt.After(details.Workspace.LastUsedAt)
}, testutil.WaitShort, testutil.IntervalMedium, "workspace LastUsedAt not updated when it should have been")
}

// Except when it sometimes shouldn't (e.g. no access)
func assertWorkspaceLastUsedAtNotUpdated(t testing.TB, details *Details) {
t.Helper()

details.FlushStats()
// Despite our efforts with the flush channel, this is inherently racy.
// Wait for a short time to ensure that the stats collection pipeline has flushed.
<-time.After(testutil.IntervalSlow)
ws, err := details.SDKClient.Workspace(context.Background(), details.Workspace.ID)
require.NoError(t, err)
require.Equal(t, ws.LastUsedAt, details.Workspace.LastUsedAt, "workspace LastUsedAt updated when it should not have been")
Expand Down