Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fix: automatically cancel tests on cleanup
  • Loading branch information
kylecarbs committed May 16, 2023
commit 4f1916e470c6206b147b6dda2c68f604079251ec
8 changes: 7 additions & 1 deletion cli/clitest/clitest.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ func Start(t *testing.T, inv *clibase.Invocation) {
// before ours.
waiter := StartWithWaiter(t, inv)
t.Cleanup(func() {
waiter.Cancel()
<-closeCh
})

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

c <-chan error
t *testing.T
}

func (w *ErrorWaiter) Cancel() {
w.cancelFunc()
}

func (w *ErrorWaiter) Wait() error {
w.waitOnce.Do(func() {
var ok bool
Expand Down Expand Up @@ -241,5 +247,5 @@ func StartWithWaiter(t *testing.T, inv *clibase.Invocation) *ErrorWaiter {
cleaningUp.Store(true)
<-doneCh
})
return &ErrorWaiter{c: errCh, t: t}
return &ErrorWaiter{c: errCh, t: t, cancelFunc: cancel}
}
4 changes: 2 additions & 2 deletions cli/gitssh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ func prepareTestGitSSH(ctx context.Context, t *testing.T) (*codersdk.Client, str

// start workspace agent
inv, root := clitest.New(t, "agent", "--agent-token", agentToken, "--agent-url", client.URL.String())
agentClient := client
agentClient := codersdk.New(client.URL)
agentClient.SetSessionToken(agentToken)
clitest.SetupConfig(t, agentClient, root)

clitest.Start(t, inv)

coderdtest.AwaitWorkspaceAgents(t, client, workspace.ID)
Expand Down
5 changes: 2 additions & 3 deletions cli/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,7 @@ func TestServer(t *testing.T) {
)

const superDuperLong = testutil.WaitSuperLong * 3

ctx, cancelFunc := context.WithTimeout(context.Background(), superDuperLong)
defer cancelFunc()
ctx := testutil.Context(t, superDuperLong)
clitest.Start(t, inv.WithContext(ctx))

//nolint:gocritic // Embedded postgres take a while to fire up.
Expand Down Expand Up @@ -1431,6 +1429,7 @@ func TestServer(t *testing.T) {
wantConfig.Options[i].Name,
)
}
w.Cancel()
w.RequireSuccess()
})
})
Expand Down
1 change: 1 addition & 0 deletions cli/vscodessh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func TestVSCodeSSH(t *testing.T) {
}
return len(entries) > 0
}, testutil.WaitLong, testutil.IntervalFast)
waiter.Cancel()

if err := waiter.Wait(); err != nil {
waiter.RequireIs(context.Canceled)
Expand Down
2 changes: 1 addition & 1 deletion enterprise/coderd/workspaceproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
// forceWorkspaceProxyHealthUpdate forces an update of the proxy health.
// This is useful when a proxy is created or deleted. Errors will be logged.
func (api *API) forceWorkspaceProxyHealthUpdate(ctx context.Context) {
if err := api.ProxyHealth.ForceUpdate(ctx); err != nil {
if err := api.ProxyHealth.ForceUpdate(ctx); err != nil && !xerrors.Is(err, context.Canceled) {
api.Logger.Error(ctx, "force proxy health update", slog.Error(err))
}
}
Expand Down