Skip to content

Commit dee134d

Browse files
committed
Fix the code with unit test
1 parent 0510ef4 commit dee134d

2 files changed

Lines changed: 56 additions & 80 deletions

File tree

docker-java-core/src/main/java/com/github/dockerjava/core/FramedInputStreamConsumer.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,20 +66,17 @@ public void accept(DockerHttpClient.Response response) {
6666
}
6767

6868
byte[] payload = new byte[bytesToRead];
69+
int actualPayloadSize = 0;
6970
do {
70-
int readBytes = body.read(payload, 0, Math.min(payload.length, bytesToRead));
71+
int readBytes = body.read(payload, actualPayloadSize, bytesToRead - actualPayloadSize);
7172
if (readBytes < 0) {
7273
// TODO log?
7374
return;
7475
}
7576

76-
if (readBytes == payload.length) {
77-
resultCallback.onNext(new Frame(streamType, payload));
78-
} else {
79-
resultCallback.onNext(new Frame(streamType, Arrays.copyOf(payload, readBytes)));
80-
}
81-
bytesToRead -= readBytes;
82-
} while (bytesToRead > 0);
77+
actualPayloadSize += readBytes;
78+
} while (actualPayloadSize < bytesToRead);
79+
resultCallback.onNext(new Frame(streamType, payload));
8380
}
8481
} catch (Exception e) {
8582
resultCallback.onError(e);

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

Lines changed: 51 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ public class LogContainerCmdIT extends CmdIT {
4242
public void asyncLogContainerWithTtyEnabled() throws Exception {
4343

4444
CreateContainerResponse container = dockerRule.getClient().createContainerCmd("busybox")
45-
.withCmd("/bin/sh", "-c", "while true; do echo hello; sleep 1; done")
46-
.withTty(true)
47-
.exec();
45+
.withCmd("/bin/sh", "-c", "while true; do echo hello; sleep 1; done")
46+
.withTty(true)
47+
.exec();
4848

4949
LOG.info("Created container: {}", container.toString());
5050
assertThat(container.getId(), not(is(emptyString())));
@@ -73,9 +73,9 @@ public void asyncLogContainerWithTtyEnabled() throws Exception {
7373
public void asyncLogContainerWithTtyDisabled() throws Exception {
7474

7575
CreateContainerResponse container = dockerRule.getClient().createContainerCmd("busybox")
76-
.withCmd("/bin/sh", "-c", "while true; do echo hello; sleep 1; done")
77-
.withTty(false)
78-
.exec();
76+
.withCmd("/bin/sh", "-c", "while true; do echo hello; sleep 1; done")
77+
.withTty(false)
78+
.exec();
7979

8080
LOG.info("Created container: {}", container.toString());
8181
assertThat(container.getId(), not(is(emptyString())));
@@ -122,11 +122,13 @@ public void onError(Throwable throwable) {
122122
public void onComplete() {
123123
super.onComplete();
124124
throw new AssertionError("expected NotFoundException");
125-
};
125+
}
126+
127+
;
126128
};
127129

128130
dockerRule.getClient().logContainerCmd("non-existing").withStdErr(true).withStdOut(true).exec(loggingCallback)
129-
.awaitCompletion();
131+
.awaitCompletion();
130132
}
131133

132134
@Test
@@ -135,44 +137,44 @@ public void asyncMultipleLogContainer() throws Exception {
135137
String snippet = "hello world";
136138

137139
CreateContainerResponse container = dockerRule.getClient().createContainerCmd("busybox")
138-
.withCmd("/bin/echo", snippet)
139-
.exec();
140+
.withCmd("/bin/echo", snippet)
141+
.exec();
140142

141143
LOG.info("Created container: {}", container.toString());
142144
assertThat(container.getId(), not(is(emptyString())));
143145

144146
dockerRule.getClient().startContainerCmd(container.getId()).exec();
145147

146148
int exitCode = dockerRule.getClient().waitContainerCmd(container.getId())
147-
.start()
148-
.awaitStatusCode();
149+
.start()
150+
.awaitStatusCode();
149151

150152
assertThat(exitCode, equalTo(0));
151153

152154
LogContainerTestCallback loggingCallback = new LogContainerTestCallback();
153155

154156
dockerRule.getClient().logContainerCmd(container.getId())
155-
.withStdErr(true)
156-
.withStdOut(true)
157-
.exec(loggingCallback);
157+
.withStdErr(true)
158+
.withStdOut(true)
159+
.exec(loggingCallback);
158160

159161
loggingCallback.close();
160162

161163
loggingCallback = new LogContainerTestCallback();
162164

163165
dockerRule.getClient().logContainerCmd(container.getId())
164-
.withStdErr(true)
165-
.withStdOut(true)
166-
.exec(loggingCallback);
166+
.withStdErr(true)
167+
.withStdOut(true)
168+
.exec(loggingCallback);
167169

168170
loggingCallback.close();
169171

170172
loggingCallback = new LogContainerTestCallback();
171173

172174
dockerRule.getClient().logContainerCmd(container.getId())
173-
.withStdErr(true)
174-
.withStdOut(true)
175-
.exec(loggingCallback);
175+
.withStdErr(true)
176+
.withStdOut(true)
177+
.exec(loggingCallback);
176178

177179
loggingCallback.awaitCompletion();
178180

@@ -184,8 +186,8 @@ public void asyncLogContainerWithSince() throws Exception {
184186
String snippet = "hello world";
185187

186188
CreateContainerResponse container = dockerRule.getClient().createContainerCmd("busybox")
187-
.withCmd("/bin/echo", snippet)
188-
.exec();
189+
.withCmd("/bin/echo", snippet)
190+
.exec();
189191

190192
LOG.info("Created container: {}", container.toString());
191193
assertThat(container.getId(), not(is(emptyString())));
@@ -195,18 +197,18 @@ public void asyncLogContainerWithSince() throws Exception {
195197
dockerRule.getClient().startContainerCmd(container.getId()).exec();
196198

197199
int exitCode = dockerRule.getClient().waitContainerCmd(container.getId())
198-
.start()
199-
.awaitStatusCode();
200+
.start()
201+
.awaitStatusCode();
200202

201203
assertThat(exitCode, equalTo(0));
202204

203205
LogContainerTestCallback loggingCallback = new LogContainerTestCallback();
204206

205207
dockerRule.getClient().logContainerCmd(container.getId())
206-
.withStdErr(true)
207-
.withStdOut(true)
208-
.withSince(timestamp)
209-
.exec(loggingCallback);
208+
.withStdErr(true)
209+
.withStdOut(true)
210+
.withSince(timestamp)
211+
.exec(loggingCallback);
210212

211213
loggingCallback.awaitCompletion();
212214

@@ -218,8 +220,8 @@ public void simultaneousCommands() throws Exception {
218220
// Create a new client to not affect other tests
219221
DockerClient client = dockerRule.newClient();
220222
CreateContainerResponse container = client.createContainerCmd("busybox")
221-
.withCmd("/bin/sh", "-c", "echo hello world; sleep infinity")
222-
.exec();
223+
.withCmd("/bin/sh", "-c", "echo hello world; sleep infinity")
224+
.exec();
223225

224226
client.startContainerCmd(container.getId()).exec();
225227

@@ -261,64 +263,41 @@ public void onNext(Frame object) {
261263
}
262264
}
263265

264-
@Test(timeout = 10_000)
265-
public void asyncLongDockerLogCmd() throws Exception {
266+
267+
@Test
268+
public void asyncLogContainerWithTailAll() throws Exception {
266269
// Create a new client to not affect other tests
267-
DockerClient client = dockerRule.newClient();
268270
String testImage = "icevivek/logreader";
269271

270272
// Pulling image icevivek/logreader
271273
try {
272-
client.inspectImageCmd(testImage).exec();
274+
dockerRule.getClient().inspectImageCmd(testImage).exec();
273275
} catch (NotFoundException e) {
274276
LOG.info("Pulling image ");
275277
// need to block until image is pulled completely
276-
client.pullImageCmd("icevivek/logreader")
278+
dockerRule.getClient().pullImageCmd("testImage")
277279
.withTag("latest")
278280
.start()
279-
.awaitCompletion();
281+
.awaitCompletion(30, TimeUnit.SECONDS);
280282
}
281283

282-
CreateContainerResponse container = client.createContainerCmd("icevivek/logreader")
284+
CreateContainerResponse container = dockerRule.getClient().createContainerCmd("icevivek/logreader")
283285
.exec();
284286

285-
client.startContainerCmd(container.getId()).exec();
286-
287-
// Simulate 100 simultaneous connections
288-
int connections = 100;
289-
290-
ExecutorService executor = Executors.newFixedThreadPool(connections);
291-
try {
292-
List<Frame> firstFrames = new CopyOnWriteArrayList<>();
293-
executor.invokeAll(
294-
LongStream.range(0, connections).<Callable<Object>>mapToObj(__ -> {
295-
return () -> {
296-
return client.logContainerCmd(container.getId())
297-
.withStdOut(true)
298-
.withFollowStream(true)
299-
.exec(new ResultCallback.Adapter<Frame>() {
287+
dockerRule.getClient().startContainerCmd(container.getId()).exec();
300288

301-
final AtomicBoolean first = new AtomicBoolean(true);
289+
LogContainerTestCallback loggingCallback = new LogContainerTestCallback(true);
302290

303-
@Override
304-
public void onNext(Frame object) {
305-
if (first.compareAndSet(true, false)) {
306-
firstFrames.add(object);
307-
}
308-
super.onNext(object);
309-
}
310-
});
311-
};
312-
}).collect(Collectors.toList())
313-
);
291+
// this essentially test the since=0 case
292+
dockerRule.getClient().logContainerCmd(container.getId())
293+
.withStdErr(true)
294+
.withStdOut(true)
295+
.withFollowStream(true)
296+
.withTailAll()
297+
.exec(loggingCallback);
314298

315-
await().atMost(30, TimeUnit.SECONDS).untilAsserted(() -> {
316-
assertThat(firstFrames, hasSize(connections));
317-
});
299+
loggingCallback.awaitCompletion(30, TimeUnit.SECONDS);
318300

319-
assertThat(firstFrames.size(), is(187));
320-
} finally {
321-
executor.shutdownNow();
322-
}
301+
assertThat(loggingCallback.getCollectedFrames(), hasSize(187));
323302
}
324303
}

0 commit comments

Comments
 (0)