Skip to content

Commit

Permalink
Fix wait strategy definition (testcontainers#8842)
Browse files Browse the repository at this point in the history
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 testcontainers#8578
  • Loading branch information
eddumelendez authored Jul 3, 2024
1 parent e86eb40 commit c491f6a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -893,7 +893,7 @@ protected WaitStrategy getWaitStrategy() {

@Override
public void setWaitStrategy(WaitStrategy waitStrategy) {
this.containerDef.setWaitStrategy(waitStrategy);
this.waitStrategy = waitStrategy;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import org.testcontainers.TestImages;
import org.testcontainers.containers.startupcheck.StartupCheckStrategy;
import org.testcontainers.containers.wait.strategy.AbstractWaitStrategy;
import org.testcontainers.containers.wait.strategy.LogMessageWaitStrategy;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.images.RemoteDockerImage;
import org.testcontainers.images.builder.ImageFromDockerfile;
import org.testcontainers.images.builder.Transferable;
Expand Down Expand Up @@ -256,6 +258,21 @@ public void shouldContainDefaultNetworkAliasWhenUsingContainerDef() {
}
}

@Test
public void shouldRespectWaitStrategy() {
try (
HelloWorldLogStrategyContainer container = new HelloWorldLogStrategyContainer(
"testcontainers/helloworld:1.1.0"
)
) {
container.setWaitStrategy(Wait.forLogMessage(".*Starting server on port.*", 1));
container.start();
assertThat((LogMessageWaitStrategy) container.getWaitStrategy())
.extracting("regEx", "times")
.containsExactly(".*Starting server on port.*", 1);
}
}

static class NoopStartupCheckStrategy extends StartupCheckStrategy {

@Override
Expand Down Expand Up @@ -319,4 +336,13 @@ class HelloWorldContainerDef extends ContainerDef {
}
}
}

static class HelloWorldLogStrategyContainer extends GenericContainer<HelloWorldContainer> {

public HelloWorldLogStrategyContainer(String image) {
super(DockerImageName.parse(image));
withExposedPorts(8080);
waitingFor(Wait.forLogMessage(".*Starting server on port.*", 2));
}
}
}

0 comments on commit c491f6a

Please sign in to comment.