Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
`ShellStrategy` can also be used as `Wait.forSuccessfulCommand(...)`. Co-authored-by: Eddú Meléndez <eddu.melendez@gmail.com>
- Loading branch information
1 parent
48083c1
commit 8f49046
Showing
3 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
core/src/main/java/org/testcontainers/containers/wait/strategy/ShellStrategy.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,32 @@ | ||
package org.testcontainers.containers.wait.strategy; | ||
|
||
import org.rnorth.ducttape.TimeoutException; | ||
import org.rnorth.ducttape.unreliables.Unreliables; | ||
import org.testcontainers.containers.ContainerLaunchException; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
public class ShellStrategy extends AbstractWaitStrategy { | ||
|
||
private String command; | ||
|
||
public ShellStrategy withCommand(String command) { | ||
this.command = command; | ||
return this; | ||
} | ||
|
||
@Override | ||
protected void waitUntilReady() { | ||
try { | ||
Unreliables.retryUntilTrue( | ||
(int) startupTimeout.getSeconds(), | ||
TimeUnit.SECONDS, | ||
() -> waitStrategyTarget.execInContainer("/bin/sh", "-c", this.command).getExitCode() == 0 | ||
); | ||
} catch (TimeoutException e) { | ||
throw new ContainerLaunchException( | ||
"Timed out waiting for container to execute `" + this.command + "` successfully." | ||
); | ||
} | ||
} | ||
} |
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
42 changes: 42 additions & 0 deletions
42
core/src/test/java/org/testcontainers/junit/wait/strategy/ShellStrategyTest.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,42 @@ | ||
package org.testcontainers.junit.wait.strategy; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
import org.junit.Test; | ||
import org.testcontainers.containers.wait.strategy.ShellStrategy; | ||
|
||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
/** | ||
* Tests for {@link ShellStrategy}. | ||
*/ | ||
public class ShellStrategyTest extends AbstractWaitStrategyTest<ShellStrategy> { | ||
|
||
private static final String LOCK_FILE = "/tmp/ready.lock"; | ||
|
||
@Test | ||
public void testWaitUntilReady_Success() { | ||
waitUntilReadyAndSucceed(String.format("touch %s; sleep 300", LOCK_FILE)); | ||
} | ||
|
||
@Test | ||
public void testWaitUntilReady_Timeout() { | ||
waitUntilReadyAndTimeout("sleep 300"); | ||
} | ||
|
||
@NotNull | ||
@Override | ||
protected ShellStrategy buildWaitStrategy(AtomicBoolean ready) { | ||
return createShellStrategy(ready).withCommand(String.format("stat %s", LOCK_FILE)); | ||
} | ||
|
||
@NotNull | ||
private static ShellStrategy createShellStrategy(AtomicBoolean ready) { | ||
return new ShellStrategy() { | ||
@Override | ||
protected void waitUntilReady() { | ||
super.waitUntilReady(); | ||
ready.set(true); | ||
} | ||
}; | ||
} | ||
} |