@@ -56,6 +56,7 @@ import (
56
56
"github.com/coder/coder/v2/pty/ptytest"
57
57
"github.com/coder/coder/v2/tailnet/tailnettest"
58
58
"github.com/coder/coder/v2/testutil"
59
+ "github.com/coder/serpent"
59
60
)
60
61
61
62
func dbArg (t * testing.T )string {
@@ -1015,7 +1016,9 @@ func TestServer(t *testing.T) {
1015
1016
)
1016
1017
1017
1018
pty := ptytest .New (t ).Attach (inv )
1018
- clitest .Start (t ,inv )
1019
+ // Since we end the test after seeing the log lines about the HTTP listener, we could cancel the test before
1020
+ // our initial interactions with PostgreSQL are complete. So, ignore errors of that type for this test.
1021
+ startIgnoringPostgresQueryCancel (t ,inv )
1019
1022
1020
1023
pty .ExpectMatch ("Started HTTP listener" )
1021
1024
pty .ExpectMatch ("http://0.0.0.0:" )
@@ -1032,7 +1035,9 @@ func TestServer(t *testing.T) {
1032
1035
)
1033
1036
1034
1037
pty := ptytest .New (t ).Attach (inv )
1035
- clitest .Start (t ,inv )
1038
+ // Since we end the test after seeing the log lines about the HTTP listener, we could cancel the test before
1039
+ // our initial interactions with PostgreSQL are complete. So, ignore errors of that type for this test.
1040
+ startIgnoringPostgresQueryCancel (t ,inv )
1036
1041
1037
1042
pty .ExpectMatch ("Started HTTP listener at" )
1038
1043
pty .ExpectMatch ("http://[::]:" )
@@ -1157,7 +1162,10 @@ func TestServer(t *testing.T) {
1157
1162
)
1158
1163
ctx ,cancel := context .WithCancel (context .Background ())
1159
1164
defer cancel ()
1160
- clitest .Start (t ,inv .WithContext (ctx ))
1165
+ // Since we cancel the context before our initial interactions with PostgreSQL are complete, we need to ignore
1166
+ // errors about queries being cancelled.
1167
+ startIgnoringPostgresQueryCancel (t ,inv .WithContext (ctx ))
1168
+
1161
1169
cancel ()
1162
1170
require .Error (t ,goleak .Find ())
1163
1171
})
@@ -2370,3 +2378,16 @@ func mockTelemetryServer(t *testing.T) (*url.URL, chan *telemetry.Deployment, ch
2370
2378
2371
2379
return serverURL ,deployment ,snapshot
2372
2380
}
2381
+
2382
+ // startIgnoringPostgresQueryCancel starts the Invocation, but excludes PostgreSQL query canceled and context
2383
+ // cancellation errors. This prevents flakes in tests that only assert things that happen before PostgreSQL is fully
2384
+ // initialized in the server.
2385
+ func startIgnoringPostgresQueryCancel (t * testing.T ,inv * serpent.Invocation ) {
2386
+ t .Helper ()
2387
+ clitest .StartWithAssert (t ,inv ,func (t * testing.T ,err error ) {
2388
+ if database .IsQueryCanceledError (err ) {
2389
+ return
2390
+ }
2391
+ assert .NoError (t ,err )
2392
+ })
2393
+ }