testcontainers / testcontainers/testcontainers-java

[Enhancement]: JdbcDatabaseContainer - Improve ``waitUntilContainerStarted``

Open
#10,360 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

type/enhancement
Dominant language
Java
Stars
8.7k
Forks
1.9k
Avg merge
2d 17h
Merged PRs (30d)
9

Description

Module

Core

Proposal

Currently https://github.com/testcontainers/testcontainers-java/blob/7d8301903a6b45591605fd376702d24fc878a61d/modules/jdbc/src/main/java/org/testcontainers/containers/JdbcDatabaseContainer.java#L176-L216 behaves very weird and this also impacts performance:

  • It doesn't utilize WaitStrategy and completely ignores/overrides it
  • It constantly tries to query if the container is running or builds database connections (these are very costly in terms of CPU usage)
    • These attempts use a hardcoded throttling value of 100ms and completely ignore the RateLimiter of WaitStrategy

I would propose that you use - as in all other containers - the WaitStrategy.

  • Remove the override/method JdbcDatabaseContainer#waitUntilContainerStarted
  • Create a custom WaitStrategy called JDBCWaitStrategy. It could look like this:
    class JDBCWaitStrategy extends AbstractWaitStrategy
    {
    	@Override
    	protected void waitUntilReady()
    	{
    		if(!(this.waitStrategyTarget instanceof final JdbcDatabaseContainer<?> container))
    		{
    			throw new IllegalArgumentException(
    				"Container must implement JdbcDatabaseContainer");
    		}
    		
    		try
    		{
    			Unreliables.retryUntilTrue(
    				(int)this.startupTimeout.getSeconds(),
    				TimeUnit.SECONDS,
    				() -> this.getRateLimiter().getWhenReady(() -> {
    					try(final Connection connection = container.createConnection("");
    						final Statement statement = connection.createStatement())
    					{
    						return statement.execute(container.getTestQueryString());
    					}
    				})
    			);
    		}
    		catch(final TimeoutException e)
    		{
    			throw new ContainerLaunchException(
    				"JDBCContainer cannot be accessed by (JDBC URL: "
    					+ container.getJdbcUrl()
    					+ "), please check container logs");
    		}
    	}
    }
    
  • Use the following default WaitStrategy for JDBCDatabaseContainer:
    new WaitAllStrategy()
    	.withStrategy(Wait.defaultWaitStrategy())
    	.withStrategy(new JDBCWaitStrategy())
    

Full example implementation:

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in modules/jdbc/src/main/java/org/testcontainers/containers/JdbcDatabaseContainer.java, especially waitUntilContainerStarted, and read the existing WaitStrategy and AbstractWaitStrategy APIs. Add the JDBC-specific strategy and default combined strategy described in the issue; done means startup uses the configured rate limiter and no longer performs the old hardcoded polling or override behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
databases, testing
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.