Skip to content

Commit ffec4f5

Browse files
Introduce boolean nostream parameter to StatsCmd. (#1287)
* BUG-1278: Introduce boolean nostream parameter to StatsCmd. * BUG-1278: Fix review. User try-with-resources. Remove sleep. Use null-safe check. * BUG-1278: Fix review. Removed useless assertions and logs. Fix namings. Fix container names. Revert encodings.xml. * BUG-1278: Remove not used imports. Fixes #1278
1 parent aec7efa commit ffec4f5

File tree

4 files changed

+52
-18
lines changed

4 files changed

+52
-18
lines changed

docker-java-api/src/main/java/com/github/dockerjava/api/command/StatsCmd.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ public interface StatsCmd extends AsyncDockerCmd<StatsCmd, Statistics> {
1616

1717
StatsCmd withContainerId(@Nonnull String containerId);
1818

19+
@CheckForNull
20+
Boolean hasNoStream();
21+
22+
StatsCmd withNoStream(boolean noStream);
23+
1924
interface Exec extends DockerCmdAsyncExec<StatsCmd, Statistics> {
2025
}
2126
}

docker-java-core/src/main/java/com/github/dockerjava/core/command/StatsCmdImpl.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ public class StatsCmdImpl extends AbstrAsyncDockerCmd<StatsCmd, Statistics> impl
1212

1313
private String containerId;
1414

15+
private Boolean noStream;
16+
1517
public StatsCmdImpl(StatsCmd.Exec exec, String containerId) {
1618
super(exec);
1719
withContainerId(containerId);
@@ -29,4 +31,14 @@ public String getContainerId() {
2931
return containerId;
3032
}
3133

34+
@Override
35+
public Boolean hasNoStream() {
36+
return noStream;
37+
}
38+
39+
@Override
40+
public StatsCmd withNoStream(boolean noStream) {
41+
this.noStream = noStream;
42+
return this;
43+
}
3244
}

docker-java-core/src/main/java/com/github/dockerjava/core/exec/StatsCmdExec.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ protected Void execute0(StatsCmd command, ResultCallback<Statistics> resultCallb
2323
WebTarget webTarget = getBaseResource().path("/containers/{id}/stats").resolveTemplate("id",
2424
command.getContainerId());
2525

26+
if (Boolean.TRUE.equals(command.hasNoStream())) {
27+
webTarget = webTarget.queryParam("stream", "0");
28+
}
29+
2630
LOGGER.trace("GET: {}", webTarget);
2731

2832
webTarget.request().get(new TypeReference<Statistics>() {

docker-java/src/test/java/com/github/dockerjava/cmd/StatsCmdIT.java

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,10 @@
88
import org.slf4j.LoggerFactory;
99

1010
import java.io.IOException;
11-
import java.security.SecureRandom;
1211
import java.util.concurrent.CountDownLatch;
1312
import java.util.concurrent.TimeUnit;
1413

15-
import static org.hamcrest.MatcherAssert.assertThat;
16-
import static org.hamcrest.Matchers.isEmptyString;
17-
import static org.hamcrest.Matchers.not;
14+
import static org.junit.Assert.assertEquals;
1815
import static org.junit.Assert.assertTrue;
1916

2017
public class StatsCmdIT extends CmdIT {
@@ -26,32 +23,48 @@ public class StatsCmdIT extends CmdIT {
2623
public void testStatsStreaming() throws InterruptedException, IOException {
2724
CountDownLatch countDownLatch = new CountDownLatch(NUM_STATS);
2825

29-
String containerName = "generated_" + new SecureRandom().nextInt();
30-
31-
CreateContainerResponse container = dockerRule.getClient().createContainerCmd("busybox").withCmd("top")
32-
.withName(containerName).exec();
33-
LOG.info("Created container {}", container.toString());
34-
assertThat(container.getId(), not(isEmptyString()));
26+
CreateContainerResponse container = dockerRule.getClient().createContainerCmd("busybox").withCmd("top").exec();
3527

3628
dockerRule.getClient().startContainerCmd(container.getId()).exec();
3729

38-
StatsCallbackTest statsCallback = dockerRule.getClient().statsCmd(container.getId()).exec(
39-
new StatsCallbackTest(countDownLatch));
40-
41-
assertTrue(countDownLatch.await(10, TimeUnit.SECONDS));
42-
Boolean gotStats = statsCallback.gotStats();
43-
44-
LOG.info("Stop stats collection");
30+
boolean gotStats = false;
31+
try (StatsCallbackTest statsCallback = dockerRule.getClient().statsCmd(container.getId()).exec(
32+
new StatsCallbackTest(countDownLatch))) {
33+
assertTrue(countDownLatch.await(10, TimeUnit.SECONDS));
34+
gotStats = statsCallback.gotStats();
4535

46-
statsCallback.close();
36+
LOG.info("Stop stats collection");
37+
}
4738

4839
LOG.info("Stopping container");
4940
dockerRule.getClient().stopContainerCmd(container.getId()).exec();
5041
dockerRule.getClient().removeContainerCmd(container.getId()).exec();
5142

5243
LOG.info("Completed test");
5344
assertTrue("Expected true", gotStats);
45+
}
5446

47+
@Test
48+
public void testStatsNoStreaming() throws InterruptedException, IOException {
49+
CountDownLatch countDownLatch = new CountDownLatch(NUM_STATS);
50+
51+
CreateContainerResponse container = dockerRule.getClient().createContainerCmd("busybox").withCmd("top").exec();
52+
53+
dockerRule.getClient().startContainerCmd(container.getId()).exec();
54+
55+
try (StatsCallbackTest statsCallback = dockerRule.getClient().statsCmd(container.getId()).withNoStream(true).exec(
56+
new StatsCallbackTest(countDownLatch))) {
57+
countDownLatch.await(5, TimeUnit.SECONDS);
58+
59+
LOG.info("Stop stats collection");
60+
}
61+
62+
LOG.info("Stopping container");
63+
dockerRule.getClient().stopContainerCmd(container.getId()).exec();
64+
dockerRule.getClient().removeContainerCmd(container.getId()).exec();
65+
66+
LOG.info("Completed test");
67+
assertEquals("Expected stats called only once", countDownLatch.getCount(), NUM_STATS - 1);
5568
}
5669

5770
private class StatsCallbackTest extends ResultCallbackTemplate<StatsCallbackTest, Statistics> {

0 commit comments

Comments
 (0)