From 0ac293cb4588b9778f855e10de6f7b0c28e4bb5e Mon Sep 17 00:00:00 2001 From: Dave Maughan Date: Wed, 3 Aug 2022 17:41:01 +0100 Subject: [PATCH] Allow Pulsar default WaitStrategy to honour startup timeout ## Motivation Currently, the PulsarContainer WaitStrategy is configured after the user has started the container. This means that any modifications that the user makes to the WaitStrategy or startupTimeout are discarded. ## Modifications This change honours the user's modifications to the WaitStrategy or startupTimeout. --- .../containers/PulsarContainer.java | 22 ++++++++----------- .../containers/PulsarContainerTest.java | 9 ++++++++ 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/modules/pulsar/src/main/java/org/testcontainers/containers/PulsarContainer.java b/modules/pulsar/src/main/java/org/testcontainers/containers/PulsarContainer.java index 10dc35dc4c8..950149e3226 100644 --- a/modules/pulsar/src/main/java/org/testcontainers/containers/PulsarContainer.java +++ b/modules/pulsar/src/main/java/org/testcontainers/containers/PulsarContainer.java @@ -2,12 +2,8 @@ import org.testcontainers.containers.wait.strategy.Wait; import org.testcontainers.containers.wait.strategy.WaitAllStrategy; -import org.testcontainers.containers.wait.strategy.WaitStrategy; import org.testcontainers.utility.DockerImageName; -import java.util.ArrayList; -import java.util.List; - /** * This container wraps Apache Pulsar running in standalone mode */ @@ -36,6 +32,8 @@ public class PulsarContainer extends GenericContainer { @Deprecated private static final String DEFAULT_TAG = "2.10.0"; + private final WaitAllStrategy waitAllStrategy = new WaitAllStrategy(); + private boolean functionsWorkerEnabled = false; private boolean transactionsEnabled = false; @@ -60,6 +58,7 @@ public PulsarContainer(final DockerImageName dockerImageName) { super(dockerImageName); dockerImageName.assertCompatibleWith(DockerImageName.parse("apachepulsar/pulsar")); withExposedPorts(BROKER_PORT, BROKER_HTTP_PORT); + setWaitStrategy(waitAllStrategy); } @Override @@ -98,21 +97,18 @@ protected void setupCommandAndEnv() { final String clusterName = getEnvMap().getOrDefault("PULSAR_PREFIX_clusterName", "standalone"); final String response = String.format("[\"%s\"]", clusterName); - - List waitStrategies = new ArrayList<>(); - waitStrategies.add(Wait.defaultWaitStrategy()); - waitStrategies.add( + waitAllStrategy.withStrategy( Wait.forHttp(ADMIN_CLUSTERS_ENDPOINT).forPort(BROKER_HTTP_PORT).forResponsePredicate(response::equals) ); + if (transactionsEnabled) { withEnv("PULSAR_PREFIX_transactionCoordinatorEnabled", "true"); - waitStrategies.add(Wait.forHttp(TRANSACTION_TOPIC_ENDPOINT).forStatusCode(200).forPort(BROKER_HTTP_PORT)); + waitAllStrategy.withStrategy( + Wait.forHttp(TRANSACTION_TOPIC_ENDPOINT).forStatusCode(200).forPort(BROKER_HTTP_PORT) + ); } if (functionsWorkerEnabled) { - waitStrategies.add(Wait.forLogMessage(".*Function worker service started.*", 1)); + waitAllStrategy.withStrategy(Wait.forLogMessage(".*Function worker service started.*", 1)); } - final WaitAllStrategy compoundedWaitStrategy = new WaitAllStrategy(); - waitStrategies.forEach(compoundedWaitStrategy::withStrategy); - waitingFor(compoundedWaitStrategy); } } diff --git a/modules/pulsar/src/test/java/org/testcontainers/containers/PulsarContainerTest.java b/modules/pulsar/src/test/java/org/testcontainers/containers/PulsarContainerTest.java index bf9f3d2201f..b054c30d0ac 100644 --- a/modules/pulsar/src/test/java/org/testcontainers/containers/PulsarContainerTest.java +++ b/modules/pulsar/src/test/java/org/testcontainers/containers/PulsarContainerTest.java @@ -12,6 +12,7 @@ import org.junit.Test; import org.testcontainers.utility.DockerImageName; +import java.time.Duration; import java.util.concurrent.CompletableFuture; import java.util.concurrent.TimeUnit; @@ -144,6 +145,14 @@ public void testClusterFullyInitialized() throws Exception { } } + @Test + public void testStartupTimeoutIsHonored() { + try (PulsarContainer pulsar = new PulsarContainer(PULSAR_IMAGE).withStartupTimeout(Duration.ZERO)) { + assertThatThrownBy(pulsar::start) + .hasRootCauseMessage("Precondition failed: timeout must be greater than zero"); + } + } + protected void testPulsarFunctionality(String pulsarBrokerUrl) throws Exception { try ( PulsarClient client = PulsarClient.builder().serviceUrl(pulsarBrokerUrl).build();