|
1 | 1 | package com.github.dockerjava.cmd; |
2 | 2 |
|
| 3 | +import com.github.dockerjava.api.DockerClient; |
| 4 | +import com.github.dockerjava.api.async.ResultCallback; |
3 | 5 | import com.github.dockerjava.api.command.CreateContainerResponse; |
4 | 6 | import com.github.dockerjava.api.exception.NotFoundException; |
| 7 | +import com.github.dockerjava.api.model.Frame; |
5 | 8 | import com.github.dockerjava.api.model.StreamType; |
6 | 9 | import com.github.dockerjava.utils.LogContainerTestCallback; |
7 | 10 | import org.junit.Test; |
8 | 11 | import org.slf4j.Logger; |
9 | 12 | import org.slf4j.LoggerFactory; |
10 | 13 |
|
11 | 14 | import java.io.IOException; |
| 15 | +import java.util.List; |
| 16 | +import java.util.concurrent.Callable; |
| 17 | +import java.util.concurrent.CopyOnWriteArrayList; |
| 18 | +import java.util.concurrent.ExecutorService; |
| 19 | +import java.util.concurrent.Executors; |
12 | 20 | import java.util.concurrent.TimeUnit; |
| 21 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 22 | +import java.util.stream.Collectors; |
| 23 | +import java.util.stream.LongStream; |
13 | 24 |
|
| 25 | +import static org.awaitility.Awaitility.await; |
| 26 | +import static org.hamcrest.CoreMatchers.everyItem; |
14 | 27 | import static org.hamcrest.CoreMatchers.is; |
15 | 28 | import static org.hamcrest.MatcherAssert.assertThat; |
16 | 29 | import static org.hamcrest.Matchers.containsString; |
17 | | -import static org.hamcrest.Matchers.equalTo; |
18 | 30 | import static org.hamcrest.Matchers.emptyString; |
| 31 | +import static org.hamcrest.Matchers.equalTo; |
| 32 | +import static org.hamcrest.Matchers.hasSize; |
| 33 | +import static org.hamcrest.Matchers.hasToString; |
19 | 34 | import static org.hamcrest.Matchers.not; |
20 | 35 | import static org.junit.Assert.assertEquals; |
21 | 36 | import static org.junit.Assert.assertTrue; |
@@ -197,4 +212,52 @@ public void asyncLogContainerWithSince() throws Exception { |
197 | 212 |
|
198 | 213 | assertThat(loggingCallback.toString(), containsString(snippet)); |
199 | 214 | } |
| 215 | + |
| 216 | + @Test(timeout = 10_000) |
| 217 | + public void simultaneousCommands() throws Exception { |
| 218 | + // Create a new client to not affect other tests |
| 219 | + DockerClient client = dockerRule.newClient(); |
| 220 | + CreateContainerResponse container = client.createContainerCmd("busybox") |
| 221 | + .withCmd("/bin/sh", "-c", "echo hello world; sleep infinity") |
| 222 | + .exec(); |
| 223 | + |
| 224 | + client.startContainerCmd(container.getId()).exec(); |
| 225 | + |
| 226 | + // Simulate 100 simultaneous connections |
| 227 | + int connections = 100; |
| 228 | + |
| 229 | + ExecutorService executor = Executors.newFixedThreadPool(connections); |
| 230 | + try { |
| 231 | + List<Frame> firstFrames = new CopyOnWriteArrayList<>(); |
| 232 | + executor.invokeAll( |
| 233 | + LongStream.range(0, connections).<Callable<Object>>mapToObj(__ -> { |
| 234 | + return () -> { |
| 235 | + return client.logContainerCmd(container.getId()) |
| 236 | + .withStdOut(true) |
| 237 | + .withFollowStream(true) |
| 238 | + .exec(new ResultCallback.Adapter<Frame>() { |
| 239 | + |
| 240 | + final AtomicBoolean first = new AtomicBoolean(true); |
| 241 | + |
| 242 | + @Override |
| 243 | + public void onNext(Frame object) { |
| 244 | + if (first.compareAndSet(true, false)) { |
| 245 | + firstFrames.add(object); |
| 246 | + } |
| 247 | + super.onNext(object); |
| 248 | + } |
| 249 | + }); |
| 250 | + }; |
| 251 | + }).collect(Collectors.toList()) |
| 252 | + ); |
| 253 | + |
| 254 | + await().atMost(5, TimeUnit.SECONDS).untilAsserted(() -> { |
| 255 | + assertThat(firstFrames, hasSize(connections)); |
| 256 | + }); |
| 257 | + |
| 258 | + assertThat(firstFrames, everyItem(hasToString("STDOUT: hello world"))); |
| 259 | + } finally { |
| 260 | + executor.shutdownNow(); |
| 261 | + } |
| 262 | + } |
200 | 263 | } |
0 commit comments