Skip to content

Commit 83ebfdd

Browse files
authored
fix: wait for PgBouncer to be fully operational before running tests (#111)
Added wait_for_pgbouncer() function that: 1. Waits for container health status to be "healthy" 2. Waits for PgBouncer to accept connections and respond to SHOW VERSION This fixes race condition where tests would fail if PgBouncer was still initializing when the test suite started, even though the container was marked as healthy.
1 parent 5300efc commit 83ebfdd

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

tests/test-pgbouncer.sh

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,50 @@ info() {
6969
echo -e "${BLUE}[TEST]${NC} $1"
7070
}
7171

72+
################################################################################
73+
# Wait for PgBouncer to be fully operational
74+
################################################################################
75+
wait_for_pgbouncer() {
76+
local max_attempts=30
77+
local attempt=0
78+
79+
echo -e "${BLUE}[INFO]${NC} Waiting for PgBouncer to be fully operational..."
80+
81+
# First wait for container to be healthy
82+
while [ $attempt -lt $max_attempts ]; do
83+
local health=$(docker inspect dev-pgbouncer --format='{{.State.Health.Status}}' 2>/dev/null)
84+
if [ "$health" == "healthy" ]; then
85+
break
86+
fi
87+
attempt=$((attempt + 1))
88+
sleep 1
89+
done
90+
91+
if [ $attempt -eq $max_attempts ]; then
92+
echo -e "${RED}[ERROR]${NC} PgBouncer container did not become healthy within ${max_attempts}s"
93+
return 1
94+
fi
95+
96+
# Now wait for PgBouncer to accept connections and respond
97+
attempt=0
98+
while [ $attempt -lt $max_attempts ]; do
99+
# Try to connect to the admin console
100+
local result=$(psql -h "$PGBOUNCER_HOST" -p "$PGBOUNCER_PORT" -U "$POSTGRES_USER" \
101+
-d pgbouncer -t -c "SHOW VERSION;" 2>/dev/null | tr -d ' ')
102+
103+
if [ -n "$result" ] && [[ "$result" == *"PgBouncer"* ]]; then
104+
echo -e "${GREEN}[OK]${NC} PgBouncer is ready (${result})"
105+
return 0
106+
fi
107+
108+
attempt=$((attempt + 1))
109+
sleep 1
110+
done
111+
112+
echo -e "${YELLOW}[WARN]${NC} PgBouncer may not be fully ready, proceeding with tests..."
113+
return 0
114+
}
115+
72116
success() {
73117
echo -e "${GREEN}[PASS]${NC} $1"
74118
TESTS_PASSED=$((TESTS_PASSED + 1))
@@ -431,6 +475,9 @@ run_all_tests() {
431475
echo "========================================="
432476
echo
433477

478+
# Wait for PgBouncer to be fully operational before running tests
479+
wait_for_pgbouncer || return 1
480+
434481
test_pgbouncer_health || true
435482
test_pool_statistics || true
436483
test_database_connectivity || true

0 commit comments

Comments
 (0)