Skip to content

Commit d135a26

Browse files
committed
avoid code duplication by using ContainerState in ResourceReaper
1 parent 602343a commit d135a26

File tree

2 files changed

+40
-15
lines changed

2 files changed

+40
-15
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,7 @@ public DockerClient client() {
175175

176176
final DockerClientProviderStrategy strategy = getOrInitializeStrategy();
177177

178-
String hostIpAddress = strategy.getDockerHostIpAddress();
179-
log.info("Docker host IP address is {}", hostIpAddress);
178+
log.info("Docker host IP address is {}", strategy.getDockerHostIpAddress());
180179
final DockerClient client = new DelegatingDockerClient(strategy.getDockerClient()) {
181180
@Override
182181
public void close() {
@@ -200,7 +199,8 @@ public void close() {
200199
if (useRyuk) {
201200
log.debug("Ryuk is enabled");
202201
try {
203-
ryukContainerId = ResourceReaper.start(hostIpAddress, client);
202+
//noinspection deprecation
203+
ryukContainerId = ResourceReaper.start(client);
204204
} catch (RuntimeException e) {
205205
cachedClientFailure = e;
206206
throw e;

core/src/main/java/org/testcontainers/utility/ResourceReaper.java

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import com.github.dockerjava.api.model.Frame;
1010
import com.github.dockerjava.api.model.HostConfig;
1111
import com.github.dockerjava.api.model.Network;
12-
import com.github.dockerjava.api.model.Ports;
1312
import com.github.dockerjava.api.model.Volume;
1413
import com.google.common.annotations.VisibleForTesting;
1514
import com.google.common.base.Throwables;
@@ -21,6 +20,7 @@
2120
import org.slf4j.Logger;
2221
import org.slf4j.LoggerFactory;
2322
import org.testcontainers.DockerClientFactory;
23+
import org.testcontainers.containers.ContainerState;
2424

2525
import java.io.BufferedReader;
2626
import java.io.IOException;
@@ -70,8 +70,23 @@ private ResourceReaper() {
7070
dockerClient = DockerClientFactory.instance().client();
7171
}
7272

73-
@SneakyThrows(InterruptedException.class)
73+
74+
/**
75+
*
76+
* @deprecated internal API
77+
*/
78+
@Deprecated
7479
public static String start(String hostIpAddress, DockerClient client) {
80+
return start(client);
81+
}
82+
83+
/**
84+
*
85+
* @deprecated internal API
86+
*/
87+
@Deprecated
88+
@SneakyThrows(InterruptedException.class)
89+
public static String start(DockerClient client) {
7590
String ryukImage = ImageNameSubstitutor.instance()
7691
.apply(DockerImageName.parse("testcontainers/ryuk:0.3.0"))
7792
.asCanonicalNameString();
@@ -107,14 +122,22 @@ public void onNext(Frame frame) {
107122
}
108123
});
109124

110-
InspectContainerResponse inspectedContainer = client.inspectContainerCmd(ryukContainerId).exec();
125+
ContainerState containerState = new ContainerState() {
111126

112-
Integer ryukPort = inspectedContainer.getNetworkSettings().getPorts().getBindings().values().stream()
113-
.flatMap(Stream::of)
114-
.findFirst()
115-
.map(Ports.Binding::getHostPortSpec)
116-
.map(Integer::parseInt)
117-
.get();
127+
final InspectContainerResponse inspectedContainer = client.inspectContainerCmd(ryukContainerId).exec();
128+
129+
@Override
130+
public List<Integer> getExposedPorts() {
131+
return Stream.of(getContainerInfo().getConfig().getExposedPorts())
132+
.map(ExposedPort::getPort)
133+
.collect(Collectors.toList());
134+
}
135+
136+
@Override
137+
public InspectContainerResponse getContainerInfo() {
138+
return inspectedContainer;
139+
}
140+
};
118141

119142
CountDownLatch ryukScheduledLatch = new CountDownLatch(1);
120143

@@ -126,13 +149,15 @@ public void onNext(Frame frame) {
126149
);
127150
}
128151

152+
String host = containerState.getHost();
153+
Integer ryukPort = containerState.getFirstMappedPort();
129154
Thread kiraThread = new Thread(
130155
DockerClientFactory.TESTCONTAINERS_THREAD_GROUP,
131156
() -> {
132157
while (true) {
133158
RYUK_ACK_RATE_LIMITER.doWhenReady(() -> {
134159
int index = 0;
135-
try(Socket clientSocket = new Socket(hostIpAddress, ryukPort)) {
160+
try(Socket clientSocket = new Socket(host, ryukPort)) {
136161
FilterRegistry registry = new FilterRegistry(clientSocket.getInputStream(), clientSocket.getOutputStream());
137162

138163
synchronized (DEATH_NOTE) {
@@ -157,7 +182,7 @@ public void onNext(Frame frame) {
157182
}
158183
}
159184
} catch (IOException e) {
160-
log.warn("Can not connect to Ryuk at {}:{}", hostIpAddress, ryukPort, e);
185+
log.warn("Can not connect to Ryuk at {}:{}", host, ryukPort, e);
161186
}
162187
});
163188
}
@@ -170,7 +195,7 @@ public void onNext(Frame frame) {
170195
// We need to wait before we can start any containers to make sure that we delete them
171196
if (!ryukScheduledLatch.await(TestcontainersConfiguration.getInstance().getRyukTimeout(), TimeUnit.SECONDS)) {
172197
log.error("Timed out waiting for Ryuk container to start. Ryuk's logs:\n{}", ryukLog);
173-
throw new IllegalStateException(String.format("Could not connect to Ryuk at %s:%s", hostIpAddress, ryukPort));
198+
throw new IllegalStateException(String.format("Could not connect to Ryuk at %s:%s", host, ryukPort));
174199
}
175200
} finally {
176201
try {

0 commit comments

Comments
 (0)