Skip to content

Commit 4f1916e

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

File tree

5 files changed

+13
-7
lines changed

5 files changed

+13
-7
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: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,7 @@ func TestServer(t *testing.T) {
121121
)
122122

123123
const superDuperLong = testutil.WaitSuperLong * 3
124-
125-
ctx, cancelFunc := context.WithTimeout(context.Background(), superDuperLong)
126-
defer cancelFunc()
124+
ctx := testutil.Context(t, superDuperLong)
127125
clitest.Start(t, inv.WithContext(ctx))
128126

129127
//nolint:gocritic // Embedded postgres take a while to fire up.
@@ -1431,6 +1429,7 @@ func TestServer(t *testing.T) {
14311429
wantConfig.Options[i].Name,
14321430
)
14331431
}
1432+
w.Cancel()
14341433
w.RequireSuccess()
14351434
})
14361435
})

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)

enterprise/coderd/workspaceproxy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import (
3131
// forceWorkspaceProxyHealthUpdate forces an update of the proxy health.
3232
// This is useful when a proxy is created or deleted. Errors will be logged.
3333
func (api *API) forceWorkspaceProxyHealthUpdate(ctx context.Context) {
34-
if err := api.ProxyHealth.ForceUpdate(ctx); err != nil {
34+
if err := api.ProxyHealth.ForceUpdate(ctx); err != nil && !xerrors.Is(err, context.Canceled) {
3535
api.Logger.Error(ctx, "force proxy health update", slog.Error(err))
3636
}
3737
}

0 commit comments

Comments
 (0)