diff --git a/pgconn/pgconn_test.go b/pgconn/pgconn_test.go index fdce6e7d..a84763a0 100644 --- a/pgconn/pgconn_test.go +++ b/pgconn/pgconn_test.go @@ -1849,13 +1849,14 @@ func TestConnCancelRequest(t *testing.T) { multiResult := pgConn.Exec(context.Background(), "select 'Hello, world', pg_sleep(2)") - // This test flickers without the Sleep. It appears that since Exec only sends the query and returns without awaiting a - // response that the CancelRequest can race it and be received before the query is running and cancellable. So wait a - // few milliseconds. - time.Sleep(50 * time.Millisecond) + go func() { + // The query is actually sent when multiResult.NextResult() is called. So wait to ensure it is sent. + // Once Flush is available this could use that instead. + time.Sleep(500 * time.Millisecond) - err = pgConn.CancelRequest(context.Background()) - require.NoError(t, err) + err = pgConn.CancelRequest(context.Background()) + require.NoError(t, err) + }() for multiResult.NextResult() { }