testcontainers / testcontainers/testcontainers-java

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

Aperta
#11,981 5 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Lingua principale
Java
Stelle
8.7k
Fork
1.9k
Merge medio
2g 17h
PR unite (30g)
9

Descrizione

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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con le implementazioni deprecata e nuova di PostgreSQLContainer e confronta i relativi metodi waitUntilContainerStarted() con JdbcDatabaseContainer#waitUntilContainerStarted(). Verifica che la strategia di attesa continui a essere rispettata prima del controllo di disponibilità JDBC, quindi aggiungi o aggiorna i test per la disponibilità della connessione lato host e per il comportamento di attesa personalizzato. Il lavoro è completato quando PostgreSQLContainer copre entrambe le condizioni senza introdurre regressioni nelle strategie di attesa fornite dall’utente.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
docker, java, postgresql
Ambito
databases, testing-qa
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Attiva
Chiarezza
Specificata chiaramente
Idoneità per principianti
75/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.