Skip to content

Commit c491f6a

Browse files
authored
Fix wait strategy definition (#8842)
Currently, `waitingFor` takes precedence over `setWaitStrategy`. This was introduced with ContainerDef in 1dba8d1. This commit uses `waitStrategy` defined in GenericContainer for both methods `waitingFor` and `setWaitStrategy`. Fixes #8578
1 parent e86eb40 commit c491f6a

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

core/src/main/java/org/testcontainers/containers/GenericContainer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -893,7 +893,7 @@ protected WaitStrategy getWaitStrategy() {
893893

894894
@Override
895895
public void setWaitStrategy(WaitStrategy waitStrategy) {
896-
this.containerDef.setWaitStrategy(waitStrategy);
896+
this.waitStrategy = waitStrategy;
897897
}
898898

899899
/**

core/src/test/java/org/testcontainers/containers/GenericContainerTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import org.testcontainers.TestImages;
2121
import org.testcontainers.containers.startupcheck.StartupCheckStrategy;
2222
import org.testcontainers.containers.wait.strategy.AbstractWaitStrategy;
23+
import org.testcontainers.containers.wait.strategy.LogMessageWaitStrategy;
24+
import org.testcontainers.containers.wait.strategy.Wait;
2325
import org.testcontainers.images.RemoteDockerImage;
2426
import org.testcontainers.images.builder.ImageFromDockerfile;
2527
import org.testcontainers.images.builder.Transferable;
@@ -256,6 +258,21 @@ public void shouldContainDefaultNetworkAliasWhenUsingContainerDef() {
256258
}
257259
}
258260

261+
@Test
262+
public void shouldRespectWaitStrategy() {
263+
try (
264+
HelloWorldLogStrategyContainer container = new HelloWorldLogStrategyContainer(
265+
"testcontainers/helloworld:1.1.0"
266+
)
267+
) {
268+
container.setWaitStrategy(Wait.forLogMessage(".*Starting server on port.*", 1));
269+
container.start();
270+
assertThat((LogMessageWaitStrategy) container.getWaitStrategy())
271+
.extracting("regEx", "times")
272+
.containsExactly(".*Starting server on port.*", 1);
273+
}
274+
}
275+
259276
static class NoopStartupCheckStrategy extends StartupCheckStrategy {
260277

261278
@Override
@@ -319,4 +336,13 @@ class HelloWorldContainerDef extends ContainerDef {
319336
}
320337
}
321338
}
339+
340+
static class HelloWorldLogStrategyContainer extends GenericContainer<HelloWorldContainer> {
341+
342+
public HelloWorldLogStrategyContainer(String image) {
343+
super(DockerImageName.parse(image));
344+
withExposedPorts(8080);
345+
waitingFor(Wait.forLogMessage(".*Starting server on port.*", 2));
346+
}
347+
}
322348
}

0 commit comments

Comments
 (0)