From 92d42acf6cc0db59f04feda31dfb86ecd42e4b7f Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 26 May 2026 17:27:37 +0000 Subject: [PATCH] fix: wait for PostgreSQL ready before starting metasrv in sqlness tests --- tests/runner/src/util.rs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tests/runner/src/util.rs b/tests/runner/src/util.rs index 6c00c8a9a6..1936093070 100644 --- a/tests/runner/src/util.rs +++ b/tests/runner/src/util.rs @@ -351,6 +351,37 @@ pub fn setup_pg(pg_port: u16, pg_version: Option<&str>) { panic!("Failed to start PostgreSQL: {:?}", status); } } + + // Wait until PostgreSQL is ready to accept connections. The container + // accepts TCP connections before the database system has finished + // initializing, so a plain TCP port check is not sufficient. Use + // `pg_isready` (bundled in the official postgres image) instead. + wait_for_pg_ready("greptimedb_pg", pg_user, Duration::from_secs(60)); +} + +/// Poll `pg_isready` inside the named container until it reports that the +/// server is accepting connections, or until `timeout` is reached. +fn wait_for_pg_ready(container_name: &str, user: &str, timeout: Duration) { + let deadline = std::time::Instant::now() + timeout; + println!("Waiting for PostgreSQL container \"{container_name}\" to be ready..."); + loop { + let ready = std::process::Command::new("docker") + .args(["exec", container_name, "pg_isready", "-U", user]) + .status() + .map(|s| s.success()) + .unwrap_or(false); + if ready { + println!("PostgreSQL is ready"); + return; + } + if std::time::Instant::now() >= deadline { + panic!( + "PostgreSQL container \"{container_name}\" did not become ready within {:?}", + timeout + ); + } + std::thread::sleep(Duration::from_millis(500)); + } } /// Set up a MySql server in docker.