-
-
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.
Adds pluggable wait strategy, to enable containers to be checked for …
…readiness using arbitrary logic. Adds HTTP(S) wait strategy to wait for a particular endpoint to be available.
- Loading branch information
1 parent
e1c96be
commit 1613efe
Showing
11 changed files
with
589 additions
and
39 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,5 @@ Matthieu Baechler <[email protected]> | |
Krystian Nowak <[email protected]> | ||
Viktor Schulz <[email protected]> | ||
Asaf Mesika <[email protected]> | ||
Sergei Egorov <[email protected]> | ||
Sergei Egorov <[email protected]> | ||
Pete Cornish <[email protected]> |
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
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/HostPortWaitStrategy.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.rnorth.ducttape.TimeoutException; | ||
import org.rnorth.ducttape.unreliables.Unreliables; | ||
import org.testcontainers.DockerClientFactory; | ||
import org.testcontainers.containers.ContainerLaunchException; | ||
import org.testcontainers.containers.GenericContainer; | ||
|
||
import java.io.IOException; | ||
import java.net.Socket; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* Waits until a socket connection can be established on a port exposed or mapped by the container. | ||
* | ||
* @author richardnorth | ||
*/ | ||
public class HostPortWaitStrategy extends GenericContainer.AbstractWaitStrategy { | ||
@Override | ||
protected void waitUntilReady() { | ||
final Integer port = getLivenessCheckPort(); | ||
if (null == port) { | ||
return; | ||
} | ||
|
||
final String ipAddress = DockerClientFactory.instance().dockerHostIpAddress(); | ||
try { | ||
Unreliables.retryUntilSuccess((int) startupTimeout.getSeconds(), TimeUnit.SECONDS, () -> { | ||
getRateLimiter().doWhenReady(() -> { | ||
try { | ||
new Socket(ipAddress, port).close(); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
}); | ||
return true; | ||
}); | ||
|
||
} catch (TimeoutException e) { | ||
throw new ContainerLaunchException("Timed out waiting for container port to open (" + | ||
ipAddress + ":" + port + " should be listening)"); | ||
} | ||
} | ||
} |
Oops, something went wrong.