Skip to content

Commit

Permalink
Add retry logic around checkExposedPort pre-flight check for improved…
Browse files Browse the repository at this point in the history
… robustness (fixes #513).
  • Loading branch information
rnorth committed Dec 11, 2017
1 parent f1ce2b1 commit 86aa381
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ All notable changes to this project will be documented in this file.
- Ensure that temp files are created in a temp directory (#423)
- Added `WaitAllStrategy` as a mechanism for composing multiple startup `WaitStrategy` objects together
- Changed `BrowserWebDriverContainer` to use improved wait strategies, to eliminate race conditions when starting VNC recording containers. This should lead to far fewer 'error' messages logged when starting up selenium containers, and less exposure to race related bugs (fixes #466).
- Add retry logic around checkExposedPort pre-flight check for improved robustness (fixes #513).

### Changed
- Make Network instances reusable (i.e. work with `@ClassRule`) ([\#469](https://github.com/testcontainers/testcontainers-java/issues/469))
Expand Down
20 changes: 12 additions & 8 deletions core/src/main/java/org/testcontainers/DockerClientFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.apache.commons.io.IOUtils;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.rnorth.ducttape.unreliables.Unreliables;
import org.rnorth.visibleassertions.VisibleAssertions;
import org.testcontainers.dockerclient.*;
import org.testcontainers.utility.ComparableVersion;
Expand All @@ -29,6 +30,7 @@
import java.util.List;
import java.util.Optional;
import java.util.ServiceLoader;
import java.util.concurrent.TimeUnit;
import java.util.function.BiFunction;
import java.util.function.Consumer;

Expand Down Expand Up @@ -177,16 +179,18 @@ private void checkMountableFile(DockerClient dockerClient, String id) {
}

private void checkExposedPort(String hostIpAddress, DockerClient dockerClient, String id) {
InspectContainerResponse inspectedContainer = dockerClient.inspectContainerCmd(id).exec();
String response = Unreliables.retryUntilSuccess(3, TimeUnit.SECONDS, () -> {
InspectContainerResponse inspectedContainer = dockerClient.inspectContainerCmd(id).exec();

String portSpec = inspectedContainer.getNetworkSettings().getPorts().getBindings().values().iterator().next()[0].getHostPortSpec();
String portSpec = inspectedContainer.getNetworkSettings().getPorts().getBindings().values().iterator().next()[0].getHostPortSpec();

try (Socket socket = new Socket(hostIpAddress, Integer.parseInt(portSpec))) {
return IOUtils.toString(socket.getInputStream(), Charset.defaultCharset());
} catch (IOException e) {
return e.getMessage();
}
});

String response;
try (Socket socket = new Socket(hostIpAddress, Integer.parseInt(portSpec))) {
response = IOUtils.toString(socket.getInputStream(), Charset.defaultCharset());
} catch (IOException e) {
response = e.getMessage();
}
VisibleAssertions.assertEquals("A port exposed by a docker container should be accessible", "hello", response);
}

Expand Down

0 comments on commit 86aa381

Please sign in to comment.