testcontainers-java icon indicating copy to clipboard operation
testcontainers-java copied to clipboard

[Bug]: GenericContainer.waitingFor takes precedence over setWaitStrategy

Open happyherp opened this issue 1 year ago • 2 comments

Module

Core

Testcontainers version

1.19.7

Using the latest Testcontainers version?

Yes

Host OS

MacOs

Host Arch

ARM

Docker version

Docker version 20.10.14, build a224086

What happened?

We were previously using test-containers version 1.17.3. When we switched to 1.19.7 some of our tests stopped working because the behaviour of GenericContainer.setWaitStrategy changed.

Our Code looked roughly like this:

postgreDBContainer = new PostgreSQLContainer<>(postgresImage).withExposedPorts(5432).withUsername("feature")
				.withPassword("xxx").withDatabaseName("xxx");
		postgreDBContainer.setWaitStrategy( 
				new LogMessageWaitStrategy().withRegEx(".*database system is ready to accept connections.*")
						.withTimes(1).withStartupTimeout(Duration.of(60, SECONDS)));

After the upgrade, the waitstrategy provided is no longer used.

I wrote a Test for this that can be copy-pasted into GenericContainerTest.java

    @Test
    public void shouldUseLatestWaitStrategy(){
        try (GenericContainer<?> container = new GenericContainer<>("testcontainers/helloworld:1.1.0")) {

            WaitForExitedState strategy1 = new WaitForExitedState(state -> false);
            WaitForExitedState strategy2 = new WaitForExitedState(state -> true);

            container.waitingFor(strategy1);
            container.setWaitStrategy(strategy2);
            assertThat(container.getWaitStrategy()).isEqualTo(strategy2);
        }
    }

I think the problem is because .waitingFor changes the field in GenericContainer, but setWaitStrategy changes it in GenericContainer.containerDef.

The PostgreSQLContainer-Constructor sets this.waitStrategy. After that it can no longer be changed by calling setWaitStrategy.

Relevant log output

No response

Additional Information

No response

happyherp avatar Apr 29 '24 13:04 happyherp

Workaround: use .waitingFor instead of ** setWaitStrategy**.

happyherp avatar Apr 29 '24 13:04 happyherp

Seems to be caused by this PR: https://github.com/testcontainers/testcontainers-java/pull/7714

illabefat avatar Jun 14 '24 07:06 illabefat