Commit 00901e0e authored by Doug Barrett's avatar Doug Barrett
Browse files

fix(v2/postgres): address review feedback on integration tests

- Use t.Cleanup with context.Background() for client shutdown instead
  of defer with the test context (which may be cancelled by teardown)
- Add readiness wait loop in CI job for PostgreSQL and PgBouncer
  services before running tests
parent 1a9fcb86
Loading
Loading
Loading
Loading
+14 −0
Original line number Diff line number Diff line
@@ -145,6 +145,20 @@ test-integration:postgres:
        MAX_CLIENT_CONN: 100
        DEFAULT_POOL_SIZE: 20
        AUTH_TYPE: plain
  before_script:
    # Wait for PostgreSQL and PgBouncer to accept connections.
    - apt-get update -qq && apt-get install -y -qq postgresql-client > /dev/null
    - |
      echo "Waiting for PostgreSQL..."
      for i in $(seq 1 30); do
        pg_isready -h postgres -p 5432 -U labkit -q && break
        sleep 1
      done
      echo "Waiting for PgBouncer..."
      for i in $(seq 1 30); do
        pg_isready -h pgbouncer -p 5432 -U labkit -q && break
        sleep 1
      done
  script:
    - cd v2
    - go test -v -tags integration -count=1 -timeout=60s ./postgres/
+4 −4
Original line number Diff line number Diff line
@@ -56,7 +56,7 @@ func TestDirectPostgres_SimpleProtocol(t *testing.T) {
	require.Equal(t, pgx.QueryExecModeSimpleProtocol, client.QueryExecMode())

	require.NoError(t, client.Start(ctx))
	defer client.Shutdown(ctx)
	t.Cleanup(func() { require.NoError(t, client.Shutdown(context.Background())) })

	var result int
	err = client.DB().QueryRowContext(ctx, "SELECT 1 + 1").Scan(&result)
@@ -78,7 +78,7 @@ func TestDirectPostgres_CacheStatement(t *testing.T) {
	require.NoError(t, err)

	require.NoError(t, client.Start(ctx))
	defer client.Shutdown(ctx)
	t.Cleanup(func() { require.NoError(t, client.Shutdown(context.Background())) })

	// Execute the same query twice to exercise the statement cache.
	for i := 0; i < 2; i++ {
@@ -105,7 +105,7 @@ func TestPgBouncer_SimpleProtocol(t *testing.T) {
	require.Equal(t, pgx.QueryExecModeSimpleProtocol, client.QueryExecMode())

	require.NoError(t, client.Start(ctx))
	defer client.Shutdown(ctx)
	t.Cleanup(func() { require.NoError(t, client.Shutdown(context.Background())) })

	// Run multiple queries to exercise connection reuse through the pooler.
	for i := 0; i < 5; i++ {
@@ -129,7 +129,7 @@ func TestPgBouncer_ParameterizedQuery(t *testing.T) {
	require.NoError(t, err)

	require.NoError(t, client.Start(ctx))
	defer client.Shutdown(ctx)
	t.Cleanup(func() { require.NoError(t, client.Shutdown(context.Background())) })

	var greeting string
	err = client.DB().QueryRowContext(ctx, "SELECT $1::text || ' ' || $2::text", "hello", "world").Scan(&greeting)