Skip to content

Commit eb74d90

Browse files
authored
test: fix TestServer cases that cancel before PostgreSQL work is done (coder#19872)
fixes coder/internal#946 Some tests tear down the server before we are done with PostgreSQL work, and the default `clitest` infrastructure fails the test if any errors like that are thrown. This PR modifies the tests like that to ignore postgreSQL errors like this.
1 parent 4d8dc22 commit eb74d90

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

cli/server_test.go

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ import (
5656
"github.com/coder/coder/v2/pty/ptytest"
5757
"github.com/coder/coder/v2/tailnet/tailnettest"
5858
"github.com/coder/coder/v2/testutil"
59+
"github.com/coder/serpent"
5960
)
6061

6162
func dbArg(t *testing.T) string {
@@ -486,7 +487,9 @@ func TestServer(t *testing.T) {
486487
"--cache-dir", t.TempDir(),
487488
)
488489
pty := ptytest.New(t).Attach(inv)
489-
clitest.Start(t, inv)
490+
// Since we end the test after seeing the log lines about the access url, we could cancel the test before
491+
// our initial interactions with PostgreSQL are complete. So, ignore errors of that type for this test.
492+
startIgnoringPostgresQueryCancel(t, inv)
490493

491494
// Just wait for startup
492495
_ = waitAccessURL(t, cfg)
@@ -510,7 +513,9 @@ func TestServer(t *testing.T) {
510513
)
511514
pty := ptytest.New(t).Attach(inv)
512515

513-
clitest.Start(t, inv)
516+
// Since we end the test after seeing the log lines about the access url, we could cancel the test before
517+
// our initial interactions with PostgreSQL are complete. So, ignore errors of that type for this test.
518+
startIgnoringPostgresQueryCancel(t, inv)
514519

515520
// Just wait for startup
516521
_ = waitAccessURL(t, cfg)
@@ -530,7 +535,9 @@ func TestServer(t *testing.T) {
530535
"--cache-dir", t.TempDir(),
531536
)
532537
pty := ptytest.New(t).Attach(inv)
533-
clitest.Start(t, inv)
538+
// Since we end the test after seeing the log lines about the access url, we could cancel the test before
539+
// our initial interactions with PostgreSQL are complete. So, ignore errors of that type for this test.
540+
startIgnoringPostgresQueryCancel(t, inv)
534541

535542
// Just wait for startup
536543
_ = waitAccessURL(t, cfg)
@@ -1015,7 +1022,9 @@ func TestServer(t *testing.T) {
10151022
)
10161023

10171024
pty := ptytest.New(t).Attach(inv)
1018-
clitest.Start(t, inv)
1025+
// Since we end the test after seeing the log lines about the HTTP listener, we could cancel the test before
1026+
// our initial interactions with PostgreSQL are complete. So, ignore errors of that type for this test.
1027+
startIgnoringPostgresQueryCancel(t, inv)
10191028

10201029
pty.ExpectMatch("Started HTTP listener")
10211030
pty.ExpectMatch("http://0.0.0.0:")
@@ -1032,7 +1041,9 @@ func TestServer(t *testing.T) {
10321041
)
10331042

10341043
pty := ptytest.New(t).Attach(inv)
1035-
clitest.Start(t, inv)
1044+
// Since we end the test after seeing the log lines about the HTTP listener, we could cancel the test before
1045+
// our initial interactions with PostgreSQL are complete. So, ignore errors of that type for this test.
1046+
startIgnoringPostgresQueryCancel(t, inv)
10361047

10371048
pty.ExpectMatch("Started HTTP listener at")
10381049
pty.ExpectMatch("http://[::]:")
@@ -1157,7 +1168,10 @@ func TestServer(t *testing.T) {
11571168
)
11581169
ctx, cancel := context.WithCancel(context.Background())
11591170
defer cancel()
1160-
clitest.Start(t, inv.WithContext(ctx))
1171+
// Since we cancel the context before our initial interactions with PostgreSQL are complete, we need to ignore
1172+
// errors about queries being canceled.
1173+
startIgnoringPostgresQueryCancel(t, inv.WithContext(ctx))
1174+
11611175
cancel()
11621176
require.Error(t, goleak.Find())
11631177
})
@@ -2370,3 +2384,16 @@ func mockTelemetryServer(t *testing.T) (*url.URL, chan *telemetry.Deployment, ch
23702384

23712385
return serverURL, deployment, snapshot
23722386
}
2387+
2388+
// startIgnoringPostgresQueryCancel starts the Invocation, but excludes PostgreSQL query canceled and context
2389+
// cancellation errors. This prevents flakes in tests that only assert things that happen before PostgreSQL is fully
2390+
// initialized in the server.
2391+
func startIgnoringPostgresQueryCancel(t *testing.T, inv *serpent.Invocation) {
2392+
t.Helper()
2393+
clitest.StartWithAssert(t, inv, func(t *testing.T, err error) {
2394+
if database.IsQueryCanceledError(err) {
2395+
return
2396+
}
2397+
assert.NoError(t, err)
2398+
})
2399+
}

0 commit comments

Comments
 (0)