Skip to content

Commit 7b9274f

Browse files
committed
Fix workspace tests waiting, erroring on workspace update, and add comments to workspace events
1 parent f2cce85 commit 7b9274f

File tree

4 files changed

+25
-30
lines changed

4 files changed

+25
-30
lines changed

coderd/workspaceagents.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -547,9 +547,7 @@ func (api *API) workspaceAgentCoordinate(rw http.ResponseWriter, r *http.Request
547547
_ = conn.Close(websocket.StatusGoingAway, err.Error())
548548
return
549549
}
550-
if !api.publishWorkspaceUpdate(ctx, rw, build.WorkspaceID) {
551-
return
552-
}
550+
api.publishWorkspaceUpdate(ctx, build.WorkspaceID)
553551

554552
// End span so we don't get long lived trace data.
555553
tracing.EndHTTPSpan(r, http.StatusOK, trace.SpanFromContext(ctx))
@@ -1000,9 +998,7 @@ func (api *API) postWorkspaceAppHealth(rw http.ResponseWriter, r *http.Request)
1000998
})
1001999
return
10021000
}
1003-
if !api.publishWorkspaceUpdate(r.Context(), rw, workspace.ID) {
1004-
return
1005-
}
1001+
api.publishWorkspaceUpdate(r.Context(), workspace.ID)
10061002

10071003
httpapi.Write(r.Context(), rw, http.StatusOK, nil)
10081004
}

coderd/workspacebuilds.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -574,9 +574,7 @@ func (api *API) postWorkspaceBuilds(rw http.ResponseWriter, r *http.Request) {
574574
return
575575
}
576576

577-
if !api.publishWorkspaceUpdate(ctx, rw, workspace.ID) {
578-
return
579-
}
577+
api.publishWorkspaceUpdate(ctx, workspace.ID)
580578

581579
httpapi.Write(ctx, rw, http.StatusCreated, apiBuild)
582580
}
@@ -637,9 +635,7 @@ func (api *API) patchCancelWorkspaceBuild(rw http.ResponseWriter, r *http.Reques
637635
return
638636
}
639637

640-
if !api.publishWorkspaceUpdate(ctx, rw, workspace.ID) {
641-
return
642-
}
638+
api.publishWorkspaceUpdate(ctx, workspace.ID)
643639

644640
httpapi.Write(ctx, rw, http.StatusOK, codersdk.Response{
645641
Message: "Job has been marked as canceled...",

coderd/workspaces.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -634,9 +634,7 @@ func (api *API) patchWorkspace(rw http.ResponseWriter, r *http.Request) {
634634
return
635635
}
636636

637-
if !api.publishWorkspaceUpdate(ctx, rw, workspace.ID) {
638-
return
639-
}
637+
api.publishWorkspaceUpdate(ctx, workspace.ID)
640638

641639
aReq.New = newWorkspace
642640
rw.WriteHeader(http.StatusNoContent)
@@ -931,6 +929,8 @@ func (api *API) watchWorkspace(rw http.ResponseWriter, r *http.Request) {
931929
}
932930
defer cancelSubscribe()
933931

932+
// An initial ping signals to the request that the server is now ready
933+
// and the client can begin servicing a channel with data.
934934
_ = sendEvent(ctx, codersdk.ServerSentEvent{
935935
Type: codersdk.ServerSentEventTypePing,
936936
})
@@ -1234,14 +1234,10 @@ func watchWorkspaceChannel(id uuid.UUID) string {
12341234
return fmt.Sprintf("workspace:%s", id)
12351235
}
12361236

1237-
func (api *API) publishWorkspaceUpdate(ctx context.Context, rw http.ResponseWriter, workspaceID uuid.UUID) bool {
1237+
func (api *API) publishWorkspaceUpdate(ctx context.Context, workspaceID uuid.UUID) {
12381238
err := api.Pubsub.Publish(watchWorkspaceChannel(workspaceID), []byte{})
12391239
if err != nil {
1240-
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
1241-
Message: "Internal error publishing workspace update.",
1242-
Detail: err.Error(),
1243-
})
1244-
return false
1240+
api.Logger.Warn(ctx, "failed to publish workspace update",
1241+
slog.F("workspace_id", workspaceID), slog.Error(err))
12451242
}
1246-
return true
12471243
}

coderd/workspaces_test.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1383,14 +1383,21 @@ func TestWorkspaceWatcher(t *testing.T) {
13831383

13841384
wc, err := client.WatchWorkspace(ctx, workspace.ID)
13851385
require.NoError(t, err)
1386+
wait := func() {
1387+
select {
1388+
case <-ctx.Done():
1389+
t.Fail()
1390+
case <-wc:
1391+
}
1392+
}
13861393

13871394
coderdtest.CreateWorkspaceBuild(t, client, workspace, database.WorkspaceTransitionStart)
13881395
// the workspace build being created
1389-
<-wc
1396+
wait()
13901397
// the workspace build being acquired
1391-
<-wc
1398+
wait()
13921399
// the workspace build completing
1393-
<-wc
1400+
wait()
13941401

13951402
agentClient := codersdk.New(client.URL)
13961403
agentClient.SessionToken = authToken
@@ -1403,25 +1410,25 @@ func TestWorkspaceWatcher(t *testing.T) {
14031410
}()
14041411

14051412
// the agent connected
1406-
<-wc
1413+
wait()
14071414
agentCloser.Close()
14081415
// the agent disconnected
1409-
<-wc
1416+
wait()
14101417

14111418
closeFunc.Close()
14121419
build := coderdtest.CreateWorkspaceBuild(t, client, workspace, database.WorkspaceTransitionStart)
14131420
// First is for the workspace build itself
1414-
<-wc
1421+
wait()
14151422
err = client.CancelWorkspaceBuild(ctx, build.ID)
14161423
require.NoError(t, err)
14171424
// Second is for the build cancel
1418-
<-wc
1425+
wait()
14191426

14201427
err = client.UpdateWorkspace(ctx, workspace.ID, codersdk.UpdateWorkspaceRequest{
14211428
Name: "another",
14221429
})
14231430
require.NoError(t, err)
1424-
<-wc
1431+
wait()
14251432

14261433
cancel()
14271434
}

0 commit comments

Comments
 (0)