|
1 | 1 | package com.github.dockerjava.core.command; |
2 | 2 |
|
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; |
11 | 10 | import org.testng.ITestResult; |
12 | 11 | import org.testng.annotations.AfterMethod; |
13 | 12 | import org.testng.annotations.AfterTest; |
14 | 13 | import org.testng.annotations.BeforeMethod; |
15 | 14 | import org.testng.annotations.BeforeTest; |
16 | 15 | import org.testng.annotations.Test; |
17 | 16 |
|
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.*; |
24 | 22 |
|
25 | 23 | @Test(groups = "integration") |
26 | 24 | public class WaitContainerCmdImplTest extends AbstractDockerClientTest { |
@@ -74,7 +72,7 @@ public void testWaitNonExistingContainer() throws DockerException { |
74 | 72 | WaitContainerResultCallback callback = new WaitContainerResultCallback() { |
75 | 73 | public void onNext(WaitResponse waitResponse) { |
76 | 74 | fail("expected NotFoundException"); |
77 | | - }; |
| 75 | + } |
78 | 76 | }; |
79 | 77 |
|
80 | 78 | dockerClient.waitContainerCmd("non-existing").exec(callback).awaitStatusCode(); |
@@ -104,4 +102,31 @@ public void testWaitContainerAbort() throws Exception { |
104 | 102 |
|
105 | 103 | assertThat(inspectContainerResponse.getState().getRunning(), is(equalTo(false))); |
106 | 104 | } |
| 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 | + } |
107 | 132 | } |
0 commit comments