Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Add startup strategy information/examples to mkdocs #1157 * Update docs/examples/junit4/generic/src/test/java/org/testcontainers/containers/startupcheck/StartupCheckStrategyTest.java Co-authored-by: Sergei Egorov <segorov@pivotal.io> Co-authored-by: Richard North <rich.north@gmail.com>
- Loading branch information
1 parent
206c5f6
commit c0fc42f
Showing
3 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
...ic/src/test/java/org/testcontainers/containers/startupcheck/StartupCheckStrategyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package org.testcontainers.containers.startupcheck; | ||
|
||
import java.time.Duration; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.TimeoutException; | ||
import lombok.SneakyThrows; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Suite; | ||
import org.testcontainers.containers.GenericContainer; | ||
import org.testcontainers.containers.output.WaitingConsumer; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.testcontainers.containers.output.OutputFrame.OutputType.STDOUT; | ||
import static org.testcontainers.containers.startupcheck.StartupCheckStrategyTest.IndefiniteOneShotStrategyTest; | ||
import static org.testcontainers.containers.startupcheck.StartupCheckStrategyTest.MinimumDurationStrategyTest; | ||
import static org.testcontainers.containers.startupcheck.StartupCheckStrategyTest.OneShotStrategyTest; | ||
|
||
@RunWith(Suite.class) | ||
@Suite.SuiteClasses({OneShotStrategyTest.class, IndefiniteOneShotStrategyTest.class, MinimumDurationStrategyTest.class}) | ||
public class StartupCheckStrategyTest { | ||
|
||
private static final String HELLO_TESTCONTAINERS = "Hello Testcontainers!"; | ||
|
||
private static void waitForHello(GenericContainer container) throws TimeoutException { | ||
WaitingConsumer consumer = new WaitingConsumer(); | ||
container.followOutput(consumer, STDOUT); | ||
|
||
consumer.waitUntil(frame -> | ||
frame.getUtf8String().contains(HELLO_TESTCONTAINERS), 30, TimeUnit.SECONDS); | ||
} | ||
|
||
public static class OneShotStrategyTest { | ||
@Rule | ||
// withOneShotStrategy { | ||
public GenericContainer bboxWithOneShot = new GenericContainer("busybox:1.31.1") | ||
.withCommand(String.format("echo %s", HELLO_TESTCONTAINERS)) | ||
.withStartupCheckStrategy( | ||
new OneShotStartupCheckStrategy().withTimeout(Duration.ofSeconds(3)) | ||
); | ||
// } | ||
|
||
@SneakyThrows | ||
@Test | ||
public void testCommandIsExecuted() { | ||
waitForHello(bboxWithOneShot); | ||
|
||
assertThat(bboxWithOneShot.isRunning()).isFalse(); | ||
} | ||
} | ||
|
||
public static class IndefiniteOneShotStrategyTest { | ||
@Rule | ||
// withIndefiniteOneShotStrategy { | ||
public GenericContainer bboxWithIndefiniteOneShot = new GenericContainer("busybox:1.31.1") | ||
.withCommand("sh", "-c", String.format("sleep 5 && echo \"%s\"", HELLO_TESTCONTAINERS)) | ||
.withStartupCheckStrategy( | ||
new IndefiniteWaitOneShotStartupCheckStrategy() | ||
); | ||
// } | ||
|
||
@SneakyThrows | ||
@Test | ||
public void testCommandIsExecuted() { | ||
waitForHello(bboxWithIndefiniteOneShot); | ||
|
||
assertThat(bboxWithIndefiniteOneShot.isRunning()).isFalse(); | ||
} | ||
} | ||
|
||
public static class MinimumDurationStrategyTest { | ||
@Rule | ||
// withMinimumDurationStrategy { | ||
public GenericContainer bboxWithMinimumDuration = new GenericContainer("busybox:1.31.1") | ||
.withCommand("sh", "-c", String.format("sleep 5 && echo \"%s\"", HELLO_TESTCONTAINERS)) | ||
.withStartupCheckStrategy( | ||
new MinimumDurationRunningStartupCheckStrategy(Duration.ofSeconds(5)) | ||
); | ||
// } | ||
|
||
@SneakyThrows | ||
@Test | ||
public void testCommandIsExecuted() { | ||
waitForHello(bboxWithMinimumDuration); | ||
|
||
assertThat(bboxWithMinimumDuration.isRunning()).isTrue(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters