From 80d319ca4c9aa5ae9eec173a70c771be7f3cf3f3 Mon Sep 17 00:00:00 2001 From: Kyle Carberry Date: Tue, 16 May 2023 16:31:09 +0000 Subject: [PATCH 1/2] chore: fix lengthy tests in psql This was adding at a minimum 3mins to our psql tests! --- cli/server_test.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/cli/server_test.go b/cli/server_test.go index 018baeda31229..4644393552f46 100644 --- a/cli/server_test.go +++ b/cli/server_test.go @@ -122,7 +122,8 @@ func TestServer(t *testing.T) { const superDuperLong = testutil.WaitSuperLong * 3 - ctx := testutil.Context(t, superDuperLong) + ctx, cancelFunc := context.WithTimeout(context.Background(), superDuperLong) + defer cancelFunc() clitest.Start(t, inv.WithContext(ctx)) //nolint:gocritic // Embedded postgres take a while to fire up. @@ -1460,8 +1461,8 @@ func TestServer(t *testing.T) { }) } -//nolint:tparallel,paralleltest // This test spawns or connects to an existing PostgreSQL instance. func TestServer_Production(t *testing.T) { + t.Parallel() if runtime.GOOS != "linux" || testing.Short() { // Skip on non-Linux because it spawns a PostgreSQL instance. t.SkipNow() @@ -1471,7 +1472,8 @@ func TestServer_Production(t *testing.T) { defer closeFunc() // Postgres + race detector + CI = slow. - ctx := testutil.Context(t, testutil.WaitSuperLong*3) + ctx, cancelFunc := context.WithTimeout(context.Background(), testutil.WaitSuperLong*3) + defer cancelFunc() inv, cfg := clitest.New(t, "server", From 4f1916e470c6206b147b6dda2c68f604079251ec Mon Sep 17 00:00:00 2001 From: Kyle Carberry Date: Tue, 16 May 2023 17:00:16 +0000 Subject: [PATCH 2/2] fix: automatically cancel tests on cleanup --- cli/clitest/clitest.go | 8 +++++++- cli/gitssh_test.go | 4 ++-- cli/server_test.go | 5 ++--- cli/vscodessh_test.go | 1 + enterprise/coderd/workspaceproxy.go | 2 +- 5 files changed, 13 insertions(+), 7 deletions(-) diff --git a/cli/clitest/clitest.go b/cli/clitest/clitest.go index 4b9a49794f5d2..5408b6db4fb90 100644 --- a/cli/clitest/clitest.go +++ b/cli/clitest/clitest.go @@ -137,6 +137,7 @@ func Start(t *testing.T, inv *clibase.Invocation) { // before ours. waiter := StartWithWaiter(t, inv) t.Cleanup(func() { + waiter.Cancel() <-closeCh }) @@ -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 @@ -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} } diff --git a/cli/gitssh_test.go b/cli/gitssh_test.go index 6d7dfe7518e22..39daab430c01c 100644 --- a/cli/gitssh_test.go +++ b/cli/gitssh_test.go @@ -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) diff --git a/cli/server_test.go b/cli/server_test.go index 4644393552f46..d5620b261b4c3 100644 --- a/cli/server_test.go +++ b/cli/server_test.go @@ -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. @@ -1431,6 +1429,7 @@ func TestServer(t *testing.T) { wantConfig.Options[i].Name, ) } + w.Cancel() w.RequireSuccess() }) }) diff --git a/cli/vscodessh_test.go b/cli/vscodessh_test.go index f40fa10de08ef..a134903005c4a 100644 --- a/cli/vscodessh_test.go +++ b/cli/vscodessh_test.go @@ -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) diff --git a/enterprise/coderd/workspaceproxy.go b/enterprise/coderd/workspaceproxy.go index f0e6da58618c0..07ac38a990bae 100644 --- a/enterprise/coderd/workspaceproxy.go +++ b/enterprise/coderd/workspaceproxy.go @@ -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)) } }