github.com/jackc/pgx/v5@v5.5.5/pgconn/helper_test.go (about) 1 package pgconn_test 2 3 import ( 4 "context" 5 "testing" 6 "time" 7 8 "github.com/jackc/pgx/v5/pgconn" 9 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/require" 12 ) 13 14 func closeConn(t testing.TB, conn *pgconn.PgConn) { 15 ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) 16 defer cancel() 17 require.NoError(t, conn.Close(ctx)) 18 select { 19 case <-conn.CleanupDone(): 20 case <-time.After(30 * time.Second): 21 t.Fatal("Connection cleanup exceeded maximum time") 22 } 23 } 24 25 // Do a simple query to ensure the connection is still usable 26 func ensureConnValid(t *testing.T, pgConn *pgconn.PgConn) { 27 ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) 28 result := pgConn.ExecParams(ctx, "select generate_series(1,$1)", [][]byte{[]byte("3")}, nil, nil, nil).Read() 29 cancel() 30 31 require.Nil(t, result.Err) 32 assert.Equal(t, 3, len(result.Rows)) 33 assert.Equal(t, "1", string(result.Rows[0][0])) 34 assert.Equal(t, "2", string(result.Rows[1][0])) 35 assert.Equal(t, "3", string(result.Rows[2][0])) 36 }