Skip to content

Commit 5d0f904

Browse files
levakinjackc
authored andcommitted
update TestConnContextCanceledCancelsRunningQueryOnServer
Check cancellation of the request for pgbouncer
1 parent 6ca3d8e commit 5d0f904

File tree

1 file changed

+38
-9
lines changed

1 file changed

+38
-9
lines changed

pgconn/pgconn_test.go

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,19 @@ import (
1717
"testing"
1818
"time"
1919

20+
"github.com/stretchr/testify/assert"
21+
"github.com/stretchr/testify/require"
22+
2023
"github.com/jackc/pgx/v5"
2124
"github.com/jackc/pgx/v5/internal/pgio"
2225
"github.com/jackc/pgx/v5/internal/pgmock"
2326
"github.com/jackc/pgx/v5/pgconn"
2427
"github.com/jackc/pgx/v5/pgproto3"
2528
"github.com/jackc/pgx/v5/pgtype"
26-
"github.com/stretchr/testify/assert"
27-
"github.com/stretchr/testify/require"
2829
)
2930

31+
const pgbouncerConnStringEnvVar = "PGX_TEST_PGBOUNCER_CONN_STRING"
32+
3033
func TestConnect(t *testing.T) {
3134
tests := []struct {
3235
name string
@@ -2256,18 +2259,44 @@ func TestConnCancelRequest(t *testing.T) {
22562259
func TestConnContextCanceledCancelsRunningQueryOnServer(t *testing.T) {
22572260
t.Parallel()
22582261

2262+
t.Run("postgres", func(t *testing.T) {
2263+
t.Parallel()
2264+
2265+
testConnContextCanceledCancelsRunningQueryOnServer(t, os.Getenv("PGX_TEST_DATABASE"), "postgres")
2266+
})
2267+
2268+
t.Run("pgbouncer", func(t *testing.T) {
2269+
t.Parallel()
2270+
2271+
connString := os.Getenv(pgbouncerConnStringEnvVar)
2272+
if connString == "" {
2273+
t.Skipf("Skipping due to missing environment variable %v", pgbouncerConnStringEnvVar)
2274+
}
2275+
2276+
testConnContextCanceledCancelsRunningQueryOnServer(t, connString, "pgbouncer")
2277+
})
2278+
}
2279+
2280+
func testConnContextCanceledCancelsRunningQueryOnServer(t *testing.T, connString, dbType string) {
22592281
ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)
22602282
defer cancel()
22612283

2262-
pgConn, err := pgconn.Connect(ctx, os.Getenv("PGX_TEST_DATABASE"))
2284+
pgConn, err := pgconn.Connect(ctx, connString)
22632285
require.NoError(t, err)
22642286
defer closeConn(t, pgConn)
22652287

2266-
pid := pgConn.PID()
2267-
22682288
ctx, cancel = context.WithTimeout(ctx, 100*time.Millisecond)
22692289
defer cancel()
2270-
multiResult := pgConn.Exec(ctx, "select 'Hello, world', pg_sleep(30)")
2290+
2291+
// Getting the actual PostgreSQL server process ID (PID) from a query executed through pgbouncer is not straightforward
2292+
// because pgbouncer abstracts the underlying database connections, and it doesn't expose the PID of the PostgreSQL
2293+
// server process to clients. However, we can check if the query is running by checking the generated query ID.
2294+
queryID := fmt.Sprintf("%s testConnContextCanceled %d", dbType, time.Now().UnixNano())
2295+
2296+
multiResult := pgConn.Exec(ctx, fmt.Sprintf(`
2297+
-- %v
2298+
select 'Hello, world', pg_sleep(30)
2299+
`, queryID))
22712300

22722301
for multiResult.NextResult() {
22732302
}
@@ -2283,7 +2312,7 @@ func TestConnContextCanceledCancelsRunningQueryOnServer(t *testing.T) {
22832312
ctx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
22842313
defer cancel()
22852314

2286-
otherConn, err := pgconn.Connect(ctx, os.Getenv("PGX_TEST_DATABASE"))
2315+
otherConn, err := pgconn.Connect(ctx, connString)
22872316
require.NoError(t, err)
22882317
defer closeConn(t, otherConn)
22892318

@@ -2292,8 +2321,8 @@ func TestConnContextCanceledCancelsRunningQueryOnServer(t *testing.T) {
22922321

22932322
for {
22942323
result := otherConn.ExecParams(ctx,
2295-
`select 1 from pg_stat_activity where pid=$1`,
2296-
[][]byte{[]byte(strconv.FormatInt(int64(pid), 10))},
2324+
`select 1 from pg_stat_activity where query like $1`,
2325+
[][]byte{[]byte("%" + queryID + "%")},
22972326
nil,
22982327
nil,
22992328
nil,

0 commit comments

Comments
 (0)