Skip to content

Commit 6d42f41

Browse files
authored
Add retry logic around checkExposedPort pre-flight check for improved robustness (fixes #513). (#516)
Fix erroneous changelog entry for 1.5.0 release
1 parent ed64c1f commit 6d42f41

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

CHANGELOG.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
All notable changes to this project will be documented in this file.
33

44
### Fixed
5-
- Fixed problem with case-sensitivity when checking internal port. ([\#524](https://github.com/testcontainers/testcontainers-java/pull/524))
5+
- Fixed problem with case-sensitivity when checking internal port. ([\#524](https://github.com/testcontainers/testcontainers-java/pull/524))
6+
- Add retry logic around checkExposedPort pre-flight check for improved robustness ([\#513](https://github.com/testcontainers/testcontainers-java/issues/513))
67

78
### Changed
89
- Added `getDatabaseName` method to JdbcDatabaseContainer, MySQLContainer, PostgreSQLContainer ([\#473](https://github.com/testcontainers/testcontainers-java/issues/473))
@@ -18,8 +19,7 @@ All notable changes to this project will be documented in this file.
1819
- Stopping creation of temporary directory prior to creating temporary file ([\#443](https://github.com/testcontainers/testcontainers-java/issues/443))
1920
- Ensure that temp files are created in a temp directory ([\#423](https://github.com/testcontainers/testcontainers-java/issues/423))
2021
- Added `WaitAllStrategy` as a mechanism for composing multiple startup `WaitStrategy` objects together
21-
- 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](https://github.com/testcontainers/testcontainers-java/issues/466)).
22-
- Add retry logic around checkExposedPort pre-flight check for improved robustness.
22+
- 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](https://github.com/testcontainers/testcontainers-java/issues/466)).
2323

2424
### Changed
2525
- Make Network instances reusable (i.e. work with `@ClassRule`) ([\#469](https://github.com/testcontainers/testcontainers-java/issues/469))

core/src/main/java/org/testcontainers/DockerClientFactory.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.apache.commons.io.IOUtils;
1515
import org.hamcrest.BaseMatcher;
1616
import org.hamcrest.Description;
17+
import org.rnorth.ducttape.unreliables.Unreliables;
1718
import org.rnorth.visibleassertions.VisibleAssertions;
1819
import org.testcontainers.dockerclient.*;
1920
import org.testcontainers.utility.ComparableVersion;
@@ -29,6 +30,7 @@
2930
import java.util.List;
3031
import java.util.Optional;
3132
import java.util.ServiceLoader;
33+
import java.util.concurrent.TimeUnit;
3234
import java.util.function.BiFunction;
3335
import java.util.function.Consumer;
3436

@@ -177,16 +179,18 @@ private void checkMountableFile(DockerClient dockerClient, String id) {
177179
}
178180

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

182-
String portSpec = inspectedContainer.getNetworkSettings().getPorts().getBindings().values().iterator().next()[0].getHostPortSpec();
185+
String portSpec = inspectedContainer.getNetworkSettings().getPorts().getBindings().values().iterator().next()[0].getHostPortSpec();
186+
187+
try (Socket socket = new Socket(hostIpAddress, Integer.parseInt(portSpec))) {
188+
return IOUtils.toString(socket.getInputStream(), Charset.defaultCharset());
189+
} catch (IOException e) {
190+
return e.getMessage();
191+
}
192+
});
183193

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

0 commit comments

Comments
 (0)