Skip to content

Commit 8c44cd3

Browse files
authored
test(cli/ssh): fix ssh start conflict test by faking API response (coder#16082)
1 parent 899836d commit 8c44cd3

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

cli/ssh_test.go

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -154,16 +154,25 @@ func TestSSH(t *testing.T) {
154154
// a start build of the workspace.
155155
isFirstBuild := true
156156
buildURL := regexp.MustCompile("/api/v2/workspaces/.*/builds")
157-
buildReq := make(chan struct{})
158-
buildResume := make(chan struct{})
157+
buildPause := make(chan bool)
158+
buildDone := make(chan struct{})
159159
buildSyncMW := func(next http.Handler) http.Handler {
160160
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
161161
if r.Method == http.MethodPost && buildURL.MatchString(r.URL.Path) {
162162
if !isFirstBuild {
163-
t.Log("buildSyncMW: blocking build")
164-
buildReq <- struct{}{}
165-
<-buildResume
163+
t.Log("buildSyncMW: pausing build")
164+
if shouldContinue := <-buildPause; !shouldContinue {
165+
// We can't force the API to trigger a build conflict (racy) so we fake it.
166+
t.Log("buildSyncMW: return conflict")
167+
w.WriteHeader(http.StatusConflict)
168+
return
169+
}
166170
t.Log("buildSyncMW: resuming build")
171+
defer func() {
172+
t.Log("buildSyncMW: sending build done")
173+
buildDone <- struct{}{}
174+
t.Log("buildSyncMW: done")
175+
}()
167176
} else {
168177
isFirstBuild = false
169178
}
@@ -211,10 +220,15 @@ func TestSSH(t *testing.T) {
211220
for _, pty := range ptys {
212221
pty.ExpectMatchContext(ctx, "Workspace was stopped, starting workspace to allow connecting to")
213222
}
214-
for range ptys {
215-
testutil.RequireRecvCtx(ctx, t, buildReq)
223+
224+
// Allow one build to complete.
225+
testutil.RequireSendCtx(ctx, t, buildPause, true)
226+
testutil.RequireRecvCtx(ctx, t, buildDone)
227+
228+
// Allow the remaining builds to continue.
229+
for i := 0; i < len(ptys)-1; i++ {
230+
testutil.RequireSendCtx(ctx, t, buildPause, false)
216231
}
217-
close(buildResume)
218232

219233
var foundConflict int
220234
for _, pty := range ptys {

0 commit comments

Comments
 (0)