Skip to content

Commit b62b27a

Browse files
committed
fix: automatically cancel tests on cleanup
1 parent 80d319c commit b62b27a

File tree

4 files changed

+11
-3
lines changed

4 files changed

+11
-3
lines changed

cli/clitest/clitest.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ func Start(t *testing.T, inv *clibase.Invocation) {
137137
// before ours.
138138
waiter := StartWithWaiter(t, inv)
139139
t.Cleanup(func() {
140+
waiter.Cancel()
140141
<-closeCh
141142
})
142143

@@ -163,11 +164,16 @@ func Run(t *testing.T, inv *clibase.Invocation) {
163164
type ErrorWaiter struct {
164165
waitOnce sync.Once
165166
cachedError error
167+
cancelFunc context.CancelFunc
166168

167169
c <-chan error
168170
t *testing.T
169171
}
170172

173+
func (w *ErrorWaiter) Cancel() {
174+
w.cancelFunc()
175+
}
176+
171177
func (w *ErrorWaiter) Wait() error {
172178
w.waitOnce.Do(func() {
173179
var ok bool
@@ -241,5 +247,5 @@ func StartWithWaiter(t *testing.T, inv *clibase.Invocation) *ErrorWaiter {
241247
cleaningUp.Store(true)
242248
<-doneCh
243249
})
244-
return &ErrorWaiter{c: errCh, t: t}
250+
return &ErrorWaiter{c: errCh, t: t, cancelFunc: cancel}
245251
}

cli/gitssh_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ func prepareTestGitSSH(ctx context.Context, t *testing.T) (*codersdk.Client, str
5858

5959
// start workspace agent
6060
inv, root := clitest.New(t, "agent", "--agent-token", agentToken, "--agent-url", client.URL.String())
61-
agentClient := client
61+
agentClient := codersdk.New(client.URL)
62+
agentClient.SetSessionToken(agentToken)
6263
clitest.SetupConfig(t, agentClient, root)
63-
6464
clitest.Start(t, inv)
6565

6666
coderdtest.AwaitWorkspaceAgents(t, client, workspace.ID)

cli/server_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1431,6 +1431,7 @@ func TestServer(t *testing.T) {
14311431
wantConfig.Options[i].Name,
14321432
)
14331433
}
1434+
w.Cancel()
14341435
w.RequireSuccess()
14351436
})
14361437
})

cli/vscodessh_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ func TestVSCodeSSH(t *testing.T) {
6868
}
6969
return len(entries) > 0
7070
}, testutil.WaitLong, testutil.IntervalFast)
71+
waiter.Cancel()
7172

7273
if err := waiter.Wait(); err != nil {
7374
waiter.RequireIs(context.Canceled)

0 commit comments

Comments
 (0)