Skip to content

Commit

Permalink
Fix workspace tests waiting, erroring on workspace update, and add co…
Browse files Browse the repository at this point in the history
…mments to workspace events
  • Loading branch information
kylecarbs committed Nov 7, 2022
1 parent f2cce85 commit febe12d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 33 deletions.
8 changes: 2 additions & 6 deletions coderd/workspaceagents.go
Original file line number Diff line number Diff line change
Expand Up @@ -547,9 +547,7 @@ func (api *API) workspaceAgentCoordinate(rw http.ResponseWriter, r *http.Request
_ = conn.Close(websocket.StatusGoingAway, err.Error())
return
}
if !api.publishWorkspaceUpdate(ctx, rw, build.WorkspaceID) {
return
}
api.publishWorkspaceUpdate(ctx, build.WorkspaceID)

// End span so we don't get long lived trace data.
tracing.EndHTTPSpan(r, http.StatusOK, trace.SpanFromContext(ctx))
Expand Down Expand Up @@ -1000,9 +998,7 @@ func (api *API) postWorkspaceAppHealth(rw http.ResponseWriter, r *http.Request)
})
return
}
if !api.publishWorkspaceUpdate(r.Context(), rw, workspace.ID) {
return
}
api.publishWorkspaceUpdate(r.Context(), workspace.ID)

httpapi.Write(r.Context(), rw, http.StatusOK, nil)
}
Expand Down
8 changes: 2 additions & 6 deletions coderd/workspacebuilds.go
Original file line number Diff line number Diff line change
Expand Up @@ -574,9 +574,7 @@ func (api *API) postWorkspaceBuilds(rw http.ResponseWriter, r *http.Request) {
return
}

if !api.publishWorkspaceUpdate(ctx, rw, workspace.ID) {
return
}
api.publishWorkspaceUpdate(ctx, workspace.ID)

httpapi.Write(ctx, rw, http.StatusCreated, apiBuild)
}
Expand Down Expand Up @@ -637,9 +635,7 @@ func (api *API) patchCancelWorkspaceBuild(rw http.ResponseWriter, r *http.Reques
return
}

if !api.publishWorkspaceUpdate(ctx, rw, workspace.ID) {
return
}
api.publishWorkspaceUpdate(ctx, workspace.ID)

httpapi.Write(ctx, rw, http.StatusOK, codersdk.Response{
Message: "Job has been marked as canceled...",
Expand Down
25 changes: 12 additions & 13 deletions coderd/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -634,9 +634,7 @@ func (api *API) patchWorkspace(rw http.ResponseWriter, r *http.Request) {
return
}

if !api.publishWorkspaceUpdate(ctx, rw, workspace.ID) {
return
}
api.publishWorkspaceUpdate(ctx, workspace.ID)

aReq.New = newWorkspace
rw.WriteHeader(http.StatusNoContent)
Expand Down Expand Up @@ -923,14 +921,19 @@ func (api *API) watchWorkspace(rw http.ResponseWriter, r *http.Request) {
})
})
if err != nil {
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
Message: "Internal error subscribing to workspace events.",
Detail: err.Error(),
_ = sendEvent(ctx, codersdk.ServerSentEvent{
Type: codersdk.ServerSentEventTypeError,
Data: codersdk.Response{
Message: "Internal error subscribing to workspace events.",
Detail: err.Error(),
},
})
return
}
defer cancelSubscribe()

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

func (api *API) publishWorkspaceUpdate(ctx context.Context, rw http.ResponseWriter, workspaceID uuid.UUID) bool {
func (api *API) publishWorkspaceUpdate(ctx context.Context, workspaceID uuid.UUID) {
err := api.Pubsub.Publish(watchWorkspaceChannel(workspaceID), []byte{})
if err != nil {
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
Message: "Internal error publishing workspace update.",
Detail: err.Error(),
})
return false
api.Logger.Warn(ctx, "failed to publish workspace update",
slog.F("workspace_id", workspaceID), slog.Error(err))
}
return true
}
23 changes: 15 additions & 8 deletions coderd/workspaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1383,14 +1383,21 @@ func TestWorkspaceWatcher(t *testing.T) {

wc, err := client.WatchWorkspace(ctx, workspace.ID)
require.NoError(t, err)
wait := func() {
select {
case <-ctx.Done():
t.Fail()
case <-wc:
}
}

coderdtest.CreateWorkspaceBuild(t, client, workspace, database.WorkspaceTransitionStart)
// the workspace build being created
<-wc
wait()
// the workspace build being acquired
<-wc
wait()
// the workspace build completing
<-wc
wait()

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

// the agent connected
<-wc
wait()
agentCloser.Close()
// the agent disconnected
<-wc
wait()

closeFunc.Close()
build := coderdtest.CreateWorkspaceBuild(t, client, workspace, database.WorkspaceTransitionStart)
// First is for the workspace build itself
<-wc
wait()
err = client.CancelWorkspaceBuild(ctx, build.ID)
require.NoError(t, err)
// Second is for the build cancel
<-wc
wait()

err = client.UpdateWorkspace(ctx, workspace.ID, codersdk.UpdateWorkspaceRequest{
Name: "another",
})
require.NoError(t, err)
<-wc
wait()

cancel()
}
Expand Down

0 comments on commit febe12d

Please sign in to comment.