-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add LogMessageWaitStrategy * Change LogMessageWaitStrategy to use matches() * Change LogUtils to follow output since container start * Add 'times' parameter to WaitingConsumer and LogMessageWaitStrategy
- Loading branch information
Showing
4 changed files
with
115 additions
and
6 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
44 changes: 44 additions & 0 deletions
44
core/src/main/java/org/testcontainers/containers/wait/LogMessageWaitStrategy.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,44 @@ | ||
package org.testcontainers.containers.wait; | ||
|
||
import org.testcontainers.containers.ContainerLaunchException; | ||
import org.testcontainers.containers.GenericContainer; | ||
import org.testcontainers.containers.output.OutputFrame; | ||
import org.testcontainers.containers.output.WaitingConsumer; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.TimeoutException; | ||
import java.util.function.Predicate; | ||
|
||
/** | ||
* Waits until containers logs expected content. | ||
*/ | ||
public class LogMessageWaitStrategy extends GenericContainer.AbstractWaitStrategy { | ||
private String regEx; | ||
|
||
private int times = 1; | ||
|
||
@Override | ||
protected void waitUntilReady() { | ||
WaitingConsumer waitingConsumer = new WaitingConsumer(); | ||
container.followOutput(waitingConsumer); | ||
|
||
Predicate<OutputFrame> waitPredicate = outputFrame -> | ||
outputFrame.getUtf8String().matches(regEx); | ||
|
||
try { | ||
waitingConsumer.waitUntil(waitPredicate, startupTimeout.getSeconds(), TimeUnit.SECONDS, times); | ||
} catch (TimeoutException e) { | ||
throw new ContainerLaunchException("Timed out waiting for log output matching '" + regEx + "'"); | ||
} | ||
} | ||
|
||
public LogMessageWaitStrategy withRegEx(String regEx) { | ||
this.regEx = regEx; | ||
return this; | ||
} | ||
|
||
public LogMessageWaitStrategy withTimes(int times) { | ||
this.times = times; | ||
return this; | ||
} | ||
} |
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
43 changes: 43 additions & 0 deletions
43
core/src/test/java/org/testcontainers/junit/wait/LogMessageWaitStrategyTest.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,43 @@ | ||
package org.testcontainers.junit.wait; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
import org.junit.Test; | ||
import org.testcontainers.containers.wait.LogMessageWaitStrategy; | ||
|
||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
/** | ||
* Tests for {@link LogMessageWaitStrategy}. | ||
*/ | ||
public class LogMessageWaitStrategyTest extends AbstractWaitStrategyTest<LogMessageWaitStrategy> { | ||
|
||
private static final String READY_MESSAGE = "I'm ready!"; | ||
|
||
@Test | ||
public void testWaitUntilReady_Success() { | ||
waitUntilReadyAndSucceed("echo -e \"" + READY_MESSAGE + "\";" + | ||
"echo -e \"foobar\";" + | ||
"echo -e \"" + READY_MESSAGE + "\";" + | ||
"sleep 300"); | ||
} | ||
|
||
@Test | ||
public void testWaitUntilReady_Timeout() { | ||
waitUntilReadyAndTimeout("echo -e \"" + READY_MESSAGE + "\";" + | ||
"echo -e \"foobar\";" + | ||
"sleep 300"); | ||
} | ||
|
||
@NotNull | ||
@Override | ||
protected LogMessageWaitStrategy buildWaitStrategy(AtomicBoolean ready) { | ||
|
||
return new LogMessageWaitStrategy() { | ||
@Override | ||
protected void waitUntilReady() { | ||
super.waitUntilReady(); | ||
ready.set(true); | ||
} | ||
}.withRegEx(".*ready.*\\s").withTimes(2); | ||
} | ||
} |