|
1 | 1 | package com.github.dockerjava.netty.exec; |
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 | | - |
11 | | -import org.testng.ITestResult; |
12 | | -import org.testng.annotations.AfterMethod; |
13 | | -import org.testng.annotations.AfterTest; |
14 | | -import org.testng.annotations.BeforeMethod; |
15 | | -import org.testng.annotations.BeforeTest; |
16 | | -import org.testng.annotations.Test; |
17 | | - |
18 | 3 | import com.github.dockerjava.api.command.CreateContainerResponse; |
19 | 4 | import com.github.dockerjava.api.command.InspectContainerResponse; |
| 5 | +import com.github.dockerjava.api.exception.DockerClientException; |
20 | 6 | import com.github.dockerjava.api.exception.DockerException; |
21 | 7 | import com.github.dockerjava.api.exception.NotFoundException; |
22 | 8 | import com.github.dockerjava.api.model.WaitResponse; |
23 | 9 | import com.github.dockerjava.core.command.WaitContainerResultCallback; |
24 | 10 | import com.github.dockerjava.netty.AbstractNettyDockerClientTest; |
| 11 | +import org.testng.ITestResult; |
| 12 | +import org.testng.annotations.AfterMethod; |
| 13 | +import org.testng.annotations.AfterTest; |
| 14 | +import org.testng.annotations.BeforeMethod; |
| 15 | +import org.testng.annotations.BeforeTest; |
| 16 | +import org.testng.annotations.Test; |
| 17 | + |
| 18 | +import java.lang.reflect.Method; |
| 19 | +import java.util.concurrent.TimeUnit; |
| 20 | + |
| 21 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 22 | +import static org.hamcrest.Matchers.*; |
25 | 23 |
|
26 | 24 | @Test(groups = "integration") |
27 | 25 | public class WaitContainerCmdExecTest extends AbstractNettyDockerClientTest { |
@@ -75,7 +73,7 @@ public void testWaitNonExistingContainer() throws DockerException { |
75 | 73 | WaitContainerResultCallback callback = new WaitContainerResultCallback() { |
76 | 74 | public void onNext(WaitResponse waitResponse) { |
77 | 75 | fail("expected NotFoundException"); |
78 | | - }; |
| 76 | + } |
79 | 77 | }; |
80 | 78 |
|
81 | 79 | dockerClient.waitContainerCmd("non-existing").exec(callback).awaitStatusCode(); |
@@ -107,4 +105,31 @@ public void testWaitContainerAbort() throws Exception { |
107 | 105 |
|
108 | 106 | assertThat(inspectContainerResponse.getState().getRunning(), is(equalTo(false))); |
109 | 107 | } |
| 108 | + |
| 109 | + @Test |
| 110 | + public void testWaitContainerTimeout() throws Exception { |
| 111 | + |
| 112 | + CreateContainerResponse container = dockerClient.createContainerCmd("busybox").withCmd("sleep", "9999").exec(); |
| 113 | + |
| 114 | + LOG.info("Created container: {}", container.toString()); |
| 115 | + assertThat(container.getId(), not(isEmptyString())); |
| 116 | + |
| 117 | + dockerClient.startContainerCmd(container.getId()).exec(); |
| 118 | + |
| 119 | + WaitContainerResultCallback callback = dockerClient.waitContainerCmd(container.getId()).exec( |
| 120 | + new WaitContainerResultCallback()); |
| 121 | + try { |
| 122 | + callback.awaitStatusCode(100, TimeUnit.MILLISECONDS); |
| 123 | + fail("Should throw exception on timeout."); |
| 124 | + } catch(DockerClientException e){ |
| 125 | + LOG.info(e.getMessage()); |
| 126 | + } |
| 127 | + |
| 128 | + dockerClient.killContainerCmd(container.getId()).exec(); |
| 129 | + |
| 130 | + InspectContainerResponse inspectContainerResponse = dockerClient.inspectContainerCmd(container.getId()).exec(); |
| 131 | + LOG.info("Container Inspect: {}", inspectContainerResponse.toString()); |
| 132 | + |
| 133 | + assertThat(inspectContainerResponse.getState().getRunning(), is(equalTo(false))); |
| 134 | + } |
110 | 135 | } |
0 commit comments