Skip to content

Commit 0f0353a

Browse files
author
Łukasz Warchał
committed
Throws exception when WaitContainerResultCallback timeouts
Fixes docker-java#570
1 parent 1ab6e63 commit 0f0353a

2 files changed

Lines changed: 43 additions & 16 deletions

File tree

src/main/java/com/github/dockerjava/core/command/WaitContainerResultCallback.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ public Integer awaitStatusCode() {
5656
*/
5757
public Integer awaitStatusCode(long timeout, TimeUnit timeUnit) {
5858
try {
59-
awaitCompletion(timeout, timeUnit);
59+
if (!awaitCompletion(timeout, timeUnit)) {
60+
throw new DockerClientException("Awaiting status code timeout.");
61+
}
6062
} catch (InterruptedException e) {
6163
throw new DockerClientException("Awaiting status code interrupted: ", e);
6264
}

src/test/java/com/github/dockerjava/core/command/WaitContainerCmdImplTest.java

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
11
package com.github.dockerjava.core.command;
22

3-
import static org.hamcrest.MatcherAssert.assertThat;
4-
import static org.hamcrest.Matchers.equalTo;
5-
import static org.hamcrest.Matchers.is;
6-
import static org.hamcrest.Matchers.isEmptyString;
7-
import static org.hamcrest.Matchers.not;
8-
9-
import java.lang.reflect.Method;
10-
3+
import com.github.dockerjava.api.command.CreateContainerResponse;
4+
import com.github.dockerjava.api.command.InspectContainerResponse;
5+
import com.github.dockerjava.api.exception.DockerClientException;
6+
import com.github.dockerjava.api.exception.DockerException;
7+
import com.github.dockerjava.api.exception.NotFoundException;
8+
import com.github.dockerjava.api.model.WaitResponse;
9+
import com.github.dockerjava.client.AbstractDockerClientTest;
1110
import org.testng.ITestResult;
1211
import org.testng.annotations.AfterMethod;
1312
import org.testng.annotations.AfterTest;
1413
import org.testng.annotations.BeforeMethod;
1514
import org.testng.annotations.BeforeTest;
1615
import org.testng.annotations.Test;
1716

18-
import com.github.dockerjava.api.exception.DockerException;
19-
import com.github.dockerjava.api.exception.NotFoundException;
20-
import com.github.dockerjava.api.command.CreateContainerResponse;
21-
import com.github.dockerjava.api.command.InspectContainerResponse;
22-
import com.github.dockerjava.api.model.WaitResponse;
23-
import com.github.dockerjava.client.AbstractDockerClientTest;
17+
import java.lang.reflect.Method;
18+
import java.util.concurrent.TimeUnit;
19+
20+
import static org.hamcrest.MatcherAssert.assertThat;
21+
import static org.hamcrest.Matchers.*;
2422

2523
@Test(groups = "integration")
2624
public class WaitContainerCmdImplTest extends AbstractDockerClientTest {
@@ -74,7 +72,7 @@ public void testWaitNonExistingContainer() throws DockerException {
7472
WaitContainerResultCallback callback = new WaitContainerResultCallback() {
7573
public void onNext(WaitResponse waitResponse) {
7674
fail("expected NotFoundException");
77-
};
75+
}
7876
};
7977

8078
dockerClient.waitContainerCmd("non-existing").exec(callback).awaitStatusCode();
@@ -104,4 +102,31 @@ public void testWaitContainerAbort() throws Exception {
104102

105103
assertThat(inspectContainerResponse.getState().getRunning(), is(equalTo(false)));
106104
}
105+
106+
@Test
107+
public void testWaitContainerTimeout() throws Exception {
108+
109+
CreateContainerResponse container = dockerClient.createContainerCmd("busybox").withCmd("sleep", "9999").exec();
110+
111+
LOG.info("Created container: {}", container.toString());
112+
assertThat(container.getId(), not(isEmptyString()));
113+
114+
dockerClient.startContainerCmd(container.getId()).exec();
115+
116+
WaitContainerResultCallback callback = dockerClient.waitContainerCmd(container.getId()).exec(
117+
new WaitContainerResultCallback());
118+
try {
119+
callback.awaitStatusCode(100, TimeUnit.MILLISECONDS);
120+
fail("Should throw exception on timeout.");
121+
} catch(DockerClientException e){
122+
LOG.info(e.getMessage());
123+
}
124+
125+
dockerClient.killContainerCmd(container.getId()).exec();
126+
127+
InspectContainerResponse inspectContainerResponse = dockerClient.inspectContainerCmd(container.getId()).exec();
128+
LOG.info("Container Inspect: {}", inspectContainerResponse.toString());
129+
130+
assertThat(inspectContainerResponse.getState().getRunning(), is(equalTo(false)));
131+
}
107132
}

0 commit comments

Comments
 (0)