@@ -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 {
@@ -486,7 +487,9 @@ func TestServer(t *testing.T) {
486
487
"--cache-dir" ,t .TempDir (),
487
488
)
488
489
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 )
490
493
491
494
// Just wait for startup
492
495
_ = waitAccessURL (t ,cfg )
@@ -510,7 +513,9 @@ func TestServer(t *testing.T) {
510
513
)
511
514
pty := ptytest .New (t ).Attach (inv )
512
515
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 )
514
519
515
520
// Just wait for startup
516
521
_ = waitAccessURL (t ,cfg )
@@ -530,7 +535,9 @@ func TestServer(t *testing.T) {
530
535
"--cache-dir" ,t .TempDir (),
531
536
)
532
537
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 )
534
541
535
542
// Just wait for startup
536
543
_ = waitAccessURL (t ,cfg )
@@ -1015,7 +1022,9 @@ func TestServer(t *testing.T) {
1015
1022
)
1016
1023
1017
1024
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 )
1019
1028
1020
1029
pty .ExpectMatch ("Started HTTP listener" )
1021
1030
pty .ExpectMatch ("http://0.0.0.0:" )
@@ -1032,7 +1041,9 @@ func TestServer(t *testing.T) {
1032
1041
)
1033
1042
1034
1043
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 )
1036
1047
1037
1048
pty .ExpectMatch ("Started HTTP listener at" )
1038
1049
pty .ExpectMatch ("http://[::]:" )
@@ -1157,7 +1168,10 @@ func TestServer(t *testing.T) {
1157
1168
)
1158
1169
ctx ,cancel := context .WithCancel (context .Background ())
1159
1170
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
+
1161
1175
cancel ()
1162
1176
require .Error (t ,goleak .Find ())
1163
1177
})
@@ -2370,3 +2384,16 @@ func mockTelemetryServer(t *testing.T) (*url.URL, chan *telemetry.Deployment, ch
2370
2384
2371
2385
return serverURL ,deployment ,snapshot
2372
2386
}
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
+ }