forked from docker-java/docker-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerClientBuilderTest.java
More file actions
82 lines (69 loc) · 2.51 KB
/
Copy pathDockerClientBuilderTest.java
File metadata and controls
82 lines (69 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package com.github.dockerjava.core;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotNull;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.testng.annotations.Test;
import com.github.dockerjava.api.command.DockerCmdExecFactory;
public class DockerClientBuilderTest {
// Amount of instances created in test
private static final int AMOUNT = 100;
@Test
public void testConcurrentClientBuilding() throws Exception {
// we use it to check instance uniqueness
final Set<DockerCmdExecFactory> instances = Collections.synchronizedSet(new HashSet<DockerCmdExecFactory>());
Runnable runnable = new Runnable() {
@Override
public void run() {
DockerCmdExecFactory factory = DockerClientBuilder.getDefaultDockerCmdExecFactory();
// factory created
assertNotNull(factory);
// and is unique
assertFalse(instances.contains(factory));
instances.add(factory);
}
};
parallel(AMOUNT, runnable);
// set contains all required unique instances
assertEquals(instances.size(), AMOUNT);
}
public static void parallel(int threads, final Runnable task) throws Exception {
final ExceptionListener exceptionListener = new ExceptionListener();
Runnable runnable = new Runnable() {
@Override
public void run() {
try {
task.run();
} catch (Throwable e) {
exceptionListener.onException(e);
}
}
};
List<Thread> threadList = new ArrayList<>(threads);
for (int i = 0; i < threads; i++) {
Thread thread = new Thread(runnable);
thread.start();
threadList.add(thread);
}
for (Thread thread : threadList) {
thread.join();
}
Throwable exception = exceptionListener.getException();
if (exception != null) {
throw new RuntimeException(exception);
}
}
private static class ExceptionListener {
private Throwable exception;
private synchronized void onException(Throwable e) {
exception = e;
}
private synchronized Throwable getException() {
return exception;
}
}
}