-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExecutorCompletionServiceTest.java
219 lines (202 loc) · 7.88 KB
/
ExecutorCompletionServiceTest.java
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
* Written by Doug Lea with assistance from members of JCP JSR-166
* Expert Group and released to the public domain, as explained at
* http://creativecommons.org/publicdomain/zero/1.0/
* Other contributors include Andrew Wright, Jeffrey Hayes,
* Pat Fisher, Mike Judd.
*/
package jsr166;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
import java.util.concurrent.RunnableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import junit.framework.Test;
import junit.framework.TestSuite;
public class ExecutorCompletionServiceTest extends JSR166TestCase {
// android-note: Removed because the CTS runner does a bad job of
// retrying tests that have suite() declarations.
//
// public static void main(String[] args) {
// main(suite(), args);
// }
// public static Test suite() {
// return new TestSuite(ExecutorCompletionServiceTest.class);
// }
/**
* Creating a new ECS with null Executor throw NPE
*/
public void testConstructorNPE() {
try {
new ExecutorCompletionService(null);
shouldThrow();
} catch (NullPointerException success) {}
}
/**
* Creating a new ECS with null queue throw NPE
*/
public void testConstructorNPE2() {
try {
ExecutorService e = Executors.newCachedThreadPool();
new ExecutorCompletionService(e, null);
shouldThrow();
} catch (NullPointerException success) {}
}
/**
* Submitting a null callable throws NPE
*/
public void testSubmitNPE() {
final ExecutorService e = Executors.newCachedThreadPool();
final ExecutorCompletionService ecs = new ExecutorCompletionService(e);
try (PoolCleaner cleaner = cleaner(e)) {
Callable c = null;
try {
ecs.submit(c);
shouldThrow();
} catch (NullPointerException success) {}
}
}
/**
* Submitting a null runnable throws NPE
*/
public void testSubmitNPE2() {
final ExecutorService e = Executors.newCachedThreadPool();
final ExecutorCompletionService ecs = new ExecutorCompletionService(e);
try (PoolCleaner cleaner = cleaner(e)) {
Runnable r = null;
try {
ecs.submit(r, Boolean.TRUE);
shouldThrow();
} catch (NullPointerException success) {}
}
}
/**
* A taken submitted task is completed
*/
public void testTake() throws InterruptedException {
final ExecutorService e = Executors.newCachedThreadPool();
final ExecutorCompletionService ecs = new ExecutorCompletionService(e);
try (PoolCleaner cleaner = cleaner(e)) {
Callable c = new StringTask();
ecs.submit(c);
Future f = ecs.take();
assertTrue(f.isDone());
}
}
/**
* Take returns the same future object returned by submit
*/
public void testTake2() throws InterruptedException {
final ExecutorService e = Executors.newCachedThreadPool();
final ExecutorCompletionService ecs = new ExecutorCompletionService(e);
try (PoolCleaner cleaner = cleaner(e)) {
Callable c = new StringTask();
Future f1 = ecs.submit(c);
Future f2 = ecs.take();
assertSame(f1, f2);
}
}
/**
* If poll returns non-null, the returned task is completed
*/
public void testPoll1() throws Exception {
final ExecutorService e = Executors.newCachedThreadPool();
final ExecutorCompletionService ecs = new ExecutorCompletionService(e);
try (PoolCleaner cleaner = cleaner(e)) {
assertNull(ecs.poll());
Callable c = new StringTask();
ecs.submit(c);
long startTime = System.nanoTime();
Future f;
while ((f = ecs.poll()) == null) {
if (millisElapsedSince(startTime) > LONG_DELAY_MS)
fail("timed out");
Thread.yield();
}
assertTrue(f.isDone());
assertSame(TEST_STRING, f.get());
}
}
/**
* If timed poll returns non-null, the returned task is completed
*/
public void testPoll2() throws InterruptedException {
final ExecutorService e = Executors.newCachedThreadPool();
final ExecutorCompletionService ecs = new ExecutorCompletionService(e);
try (PoolCleaner cleaner = cleaner(e)) {
assertNull(ecs.poll());
Callable c = new StringTask();
ecs.submit(c);
Future f = ecs.poll(SHORT_DELAY_MS, MILLISECONDS);
if (f != null)
assertTrue(f.isDone());
}
}
/**
* Submitting to underlying AES that overrides newTaskFor(Callable)
* returns and eventually runs Future returned by newTaskFor.
*/
public void testNewTaskForCallable() throws InterruptedException {
final AtomicBoolean done = new AtomicBoolean(false);
class MyCallableFuture<V> extends FutureTask<V> {
MyCallableFuture(Callable<V> c) { super(c); }
protected void done() { done.set(true); }
}
final ExecutorService e =
new ThreadPoolExecutor(1, 1,
30L, TimeUnit.SECONDS,
new ArrayBlockingQueue<Runnable>(1)) {
protected <T> RunnableFuture<T> newTaskFor(Callable<T> c) {
return new MyCallableFuture<T>(c);
}};
ExecutorCompletionService<String> ecs =
new ExecutorCompletionService<String>(e);
try (PoolCleaner cleaner = cleaner(e)) {
assertNull(ecs.poll());
Callable<String> c = new StringTask();
Future f1 = ecs.submit(c);
assertTrue("submit must return MyCallableFuture",
f1 instanceof MyCallableFuture);
Future f2 = ecs.take();
assertSame("submit and take must return same objects", f1, f2);
assertTrue("completed task must have set done", done.get());
}
}
/**
* Submitting to underlying AES that overrides newTaskFor(Runnable,T)
* returns and eventually runs Future returned by newTaskFor.
*/
public void testNewTaskForRunnable() throws InterruptedException {
final AtomicBoolean done = new AtomicBoolean(false);
class MyRunnableFuture<V> extends FutureTask<V> {
MyRunnableFuture(Runnable t, V r) { super(t, r); }
protected void done() { done.set(true); }
}
final ExecutorService e =
new ThreadPoolExecutor(1, 1,
30L, TimeUnit.SECONDS,
new ArrayBlockingQueue<Runnable>(1)) {
protected <T> RunnableFuture<T> newTaskFor(Runnable t, T r) {
return new MyRunnableFuture<T>(t, r);
}};
final ExecutorCompletionService<String> ecs =
new ExecutorCompletionService<String>(e);
try (PoolCleaner cleaner = cleaner(e)) {
assertNull(ecs.poll());
Runnable r = new NoOpRunnable();
Future f1 = ecs.submit(r, null);
assertTrue("submit must return MyRunnableFuture",
f1 instanceof MyRunnableFuture);
Future f2 = ecs.take();
assertSame("submit and take must return same objects", f1, f2);
assertTrue("completed task must have set done", done.get());
}
}
}