1 package pgx_test
2
3 import (
4 "context"
5 "os"
6 "testing"
7
8 "github.com/jackc/pgx/v5"
9 "github.com/stretchr/testify/assert"
10 "github.com/stretchr/testify/require"
11 )
12
13 func TestPgbouncerStatementCacheDescribe(t *testing.T) {
14 connString := os.Getenv("PGX_TEST_PGBOUNCER_CONN_STRING")
15 if connString == "" {
16 t.Skipf("Skipping due to missing environment variable %v", "PGX_TEST_PGBOUNCER_CONN_STRING")
17 }
18
19 config := mustParseConfig(t, connString)
20 config.DefaultQueryExecMode = pgx.QueryExecModeCacheDescribe
21 config.DescriptionCacheCapacity = 1024
22
23 testPgbouncer(t, config, 10, 100)
24 }
25
26 func TestPgbouncerSimpleProtocol(t *testing.T) {
27 connString := os.Getenv("PGX_TEST_PGBOUNCER_CONN_STRING")
28 if connString == "" {
29 t.Skipf("Skipping due to missing environment variable %v", "PGX_TEST_PGBOUNCER_CONN_STRING")
30 }
31
32 config := mustParseConfig(t, connString)
33 config.DefaultQueryExecMode = pgx.QueryExecModeSimpleProtocol
34
35 testPgbouncer(t, config, 10, 100)
36 }
37
38 func testPgbouncer(t *testing.T, config *pgx.ConnConfig, workers, iterations int) {
39 doneChan := make(chan struct{})
40
41 for i := 0; i < workers; i++ {
42 go func() {
43 defer func() { doneChan <- struct{}{} }()
44 conn, err := pgx.ConnectConfig(context.Background(), config)
45 require.Nil(t, err)
46 defer closeConn(t, conn)
47
48 for i := 0; i < iterations; i++ {
49 var i32 int32
50 var i64 int64
51 var f32 float32
52 var s string
53 var s2 string
54 err = conn.QueryRow(context.Background(), "select 1::int4, 2::int8, 3::float4, 'hi'::text").Scan(&i32, &i64, &f32, &s)
55 require.NoError(t, err)
56 assert.Equal(t, int32(1), i32)
57 assert.Equal(t, int64(2), i64)
58 assert.Equal(t, float32(3), f32)
59 assert.Equal(t, "hi", s)
60
61 err = conn.QueryRow(context.Background(), "select 1::int8, 2::float4, 'bye'::text, 4::int4, 'whatever'::text").Scan(&i64, &f32, &s, &i32, &s2)
62 require.NoError(t, err)
63 assert.Equal(t, int64(1), i64)
64 assert.Equal(t, float32(2), f32)
65 assert.Equal(t, "bye", s)
66 assert.Equal(t, int32(4), i32)
67 assert.Equal(t, "whatever", s2)
68 }
69 }()
70 }
71
72 for i := 0; i < workers; i++ {
73 <-doneChan
74 }
75
76 }
77
View as plain text