From 7753651dd665f2255d706b7e9354da939dcf143c Mon Sep 17 00:00:00 2001 From: Ali Zarinkolah Date: Tue, 18 Aug 2026 10:40:32 +0330 Subject: [PATCH] fix(tests): pin the Postgres container URL to IPv4 so asyncpg can connect Testcontainers reports the container host as `localhost`, which resolves to ::1 before 127.0.0.1 on this machine. Docker publishes the mapped port on IPv4 only, and the IPv6 SYN is dropped rather than refused, so asyncpg blocked on the first address until its connect timeout instead of falling back to the second -- the migration fixture hung rather than failing. 127.0.0.1 connects in 0.07s where localhost timed out at 15s; the integration suite now runs in 3.6s, inside the 10s pytest-timeout. --- tests/integration/postgres/conftest.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/tests/integration/postgres/conftest.py b/tests/integration/postgres/conftest.py index 850260f..49155d1 100644 --- a/tests/integration/postgres/conftest.py +++ b/tests/integration/postgres/conftest.py @@ -36,7 +36,15 @@ def postgres_container() -> Iterator[PostgresContainer]: @pytest.fixture(scope="session") def postgres_url(postgres_container: PostgresContainer) -> str: - return postgres_container.get_connection_url() + """The container's URL, pinned to IPv4. + + Testcontainers reports the host as `localhost`, which resolves to `::1` + before `127.0.0.1`. Docker publishes the mapped port on IPv4 only, and the + IPv6 SYN is dropped rather than refused, so asyncpg blocks on the first + address until its connect timeout instead of falling back to the second -- + the connection does not fail, it hangs. + """ + return postgres_container.get_connection_url().replace("@localhost:", "@127.0.0.1:") @pytest.fixture(scope="session")