[Bug]: PostgreSQLContainer skips the JDBC readiness gate, leaving host-side port reachability unverified

Offen
#11,981 5 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Anfängerfreundlichkeit
75/100
Issue-Typ
Bug
Klarheit
Klar beschrieben
Aktivitätsstatus
Aktiv
Tech-Stack
docker, java, postgresql

Rechercherichtung

Beginne mit den veralteten und neuen PostgreSQLContainer-Implementierungen und vergleiche deren waitUntilContainerStarted()-Methoden mit JdbcDatabaseContainer#waitUntilContainerStarted(). Überprüfe, dass die Wait-Strategie vor dem JDBC-Bereitschaftscheck weiterhin berücksichtigt wird, und füge Tests für die Verbindungsbereitschaft auf der Host-Seite und das benutzerdefinierte Warteverhalten hinzu oder aktualisiere sie. Erledigt ist die Aufgabe, wenn PostgreSQLContainer beide Prüfungen abdeckt, ohne vom Benutzer bereitgestellte Wait-Strategien zu beeinträchtigen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

Module

PostgreSQL

Problem

PostgreSQLContainer — both the deprecated org.testcontainers.containers.PostgreSQLContainer and the new org.testcontainers.postgresql.PostgreSQLContainer — overrides waitUntilContainerStarted() to call only the wait strategy:

@Override
protected void waitUntilContainerStarted() {
    getWaitStrategy().waitUntilReady(this);
}

This deliberately skips JdbcDatabaseContainer#waitUntilContainerStarted(), which repeatedly opens a JDBC connection from the host and runs the test query until it succeeds.

The consequence is that Postgres readiness is gated purely on container-side log output. Nothing verifies that the mapped port is routable from the calling JVM, or that a connection can actually be established. Every other JDBC container gets that host-side gate; Postgres is the exception.

On Docker environments that publish ports through a VM (Colima, Docker Desktop), the mapped host port can become routable slightly after database system is ready to accept connections appears in the log, so the first connection after start() returns Connection refused.

Why the override may no longer be needed

git log -L traces the override to #327, which fixed #317. The motive there was noise, not correctness: the Postgres JDBC driver had just moved to java.util.logging and was printing a stack trace for every failed attempt of the connect-retry loop. The PR description even says:

I've changed the PostgreSQLContainer to use the WaitingConsumer for waiting for Log output, instead of performing SQL queries. It's basically the same code as in LogWaitStrategy, but I've figured we'll refactor the JdbcDatabaseContainers at a later stage, to make use of a WaitStrategy?

That concern is handled independently today. PostgreSQLContainer#configure() sets:

// Disable Postgres driver use of java.util.logging to reduce noise at startup time
withUrlParam("loggerLevel", "OFF");

So the retry loop no longer produces the log spam the override was introduced to avoid.

Proposal

Have the override run the wait strategy and then delegate:

@Override
protected void waitUntilContainerStarted() {
    getWaitStrategy().waitUntilReady(this);
    super.waitUntilContainerStarted();
}

This keeps the property the override was preserving — a user-supplied waitingFor(...) is actually honored, which JdbcDatabaseContainer otherwise ignores entirely — while restoring the JDBC readiness gate that every other JDBC container already has.

Downstream context

Surfaced from testcontainers-jooq-codegen-maven-plugin#59. That plugin worked around the missing host-side gate by replacing the container's wait strategy with HostPortWaitStrategy, which then removed the log gate and let code generation run against a database still reporting FATAL: the database system is starting up. Neither gate alone is sufficient, and a downstream caller cannot compose them: GenericContainer#getWaitStrategy is protected, so external code cannot read a container's default in order to wrap it in a WaitAllStrategy.

Happy to open a PR if this direction looks right.

Vorherrschende Sprache
Java
Sterne
8.7k
Forks
1.9k
Ø Merge
2 T. 17 Std.
Gemergte PRs (30 T.)
9

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus testcontainers/testcontainers-java

Alle Issues in testcontainers/testcontainers-java

Ähnliche Issues

Weitere Issues zu Java

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.