test: fix TestServer cases that cancel before PostgreSQL work is done (#19872)

fixes https://github.com/coder/internal/issues/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.
This commit is contained in:
Spike Curtis
2025-09-19 15:14:48 +04:00
committed by GitHub
parent 4d8dc221bf
commit eb74d902bf
+33 -6
View File
@@ -56,6 +56,7 @@ import (
"github.com/coder/coder/v2/pty/ptytest"
"github.com/coder/coder/v2/tailnet/tailnettest"
"github.com/coder/coder/v2/testutil"
"github.com/coder/serpent"
)
func dbArg(t *testing.T) string {
@@ -486,7 +487,9 @@ func TestServer(t *testing.T) {
"--cache-dir", t.TempDir(),
)
pty := ptytest.New(t).Attach(inv)
clitest.Start(t, inv)
// Since we end the test after seeing the log lines about the access url, we could cancel the test before
// our initial interactions with PostgreSQL are complete. So, ignore errors of that type for this test.
startIgnoringPostgresQueryCancel(t, inv)
// Just wait for startup
_ = waitAccessURL(t, cfg)
@@ -510,7 +513,9 @@ func TestServer(t *testing.T) {
)
pty := ptytest.New(t).Attach(inv)
clitest.Start(t, inv)
// Since we end the test after seeing the log lines about the access url, we could cancel the test before
// our initial interactions with PostgreSQL are complete. So, ignore errors of that type for this test.
startIgnoringPostgresQueryCancel(t, inv)
// Just wait for startup
_ = waitAccessURL(t, cfg)
@@ -530,7 +535,9 @@ func TestServer(t *testing.T) {
"--cache-dir", t.TempDir(),
)
pty := ptytest.New(t).Attach(inv)
clitest.Start(t, inv)
// Since we end the test after seeing the log lines about the access url, we could cancel the test before
// our initial interactions with PostgreSQL are complete. So, ignore errors of that type for this test.
startIgnoringPostgresQueryCancel(t, inv)
// Just wait for startup
_ = waitAccessURL(t, cfg)
@@ -1015,7 +1022,9 @@ func TestServer(t *testing.T) {
)
pty := ptytest.New(t).Attach(inv)
clitest.Start(t, inv)
// Since we end the test after seeing the log lines about the HTTP listener, we could cancel the test before
// our initial interactions with PostgreSQL are complete. So, ignore errors of that type for this test.
startIgnoringPostgresQueryCancel(t, inv)
pty.ExpectMatch("Started HTTP listener")
pty.ExpectMatch("http://0.0.0.0:")
@@ -1032,7 +1041,9 @@ func TestServer(t *testing.T) {
)
pty := ptytest.New(t).Attach(inv)
clitest.Start(t, inv)
// Since we end the test after seeing the log lines about the HTTP listener, we could cancel the test before
// our initial interactions with PostgreSQL are complete. So, ignore errors of that type for this test.
startIgnoringPostgresQueryCancel(t, inv)
pty.ExpectMatch("Started HTTP listener at")
pty.ExpectMatch("http://[::]:")
@@ -1157,7 +1168,10 @@ func TestServer(t *testing.T) {
)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
clitest.Start(t, inv.WithContext(ctx))
// Since we cancel the context before our initial interactions with PostgreSQL are complete, we need to ignore
// errors about queries being canceled.
startIgnoringPostgresQueryCancel(t, inv.WithContext(ctx))
cancel()
require.Error(t, goleak.Find())
})
@@ -2370,3 +2384,16 @@ func mockTelemetryServer(t *testing.T) (*url.URL, chan *telemetry.Deployment, ch
return serverURL, deployment, snapshot
}
// startIgnoringPostgresQueryCancel starts the Invocation, but excludes PostgreSQL query canceled and context
// cancellation errors. This prevents flakes in tests that only assert things that happen before PostgreSQL is fully
// initialized in the server.
func startIgnoringPostgresQueryCancel(t *testing.T, inv *serpent.Invocation) {
t.Helper()
clitest.StartWithAssert(t, inv, func(t *testing.T, err error) {
if database.IsQueryCanceledError(err) {
return
}
assert.NoError(t, err)
})
}