1 |
dl |
1.1 |
/* |
2 |
dl |
1.2 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
|
* Expert Group and released to the public domain, as explained at |
4 |
jsr166 |
1.13 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
jsr166 |
1.7 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
|
* Pat Fisher, Mike Judd. |
7 |
dl |
1.1 |
*/ |
8 |
|
|
|
9 |
jsr166 |
1.19 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
|
|
|
11 |
jsr166 |
1.18 |
import java.util.concurrent.ArrayBlockingQueue; |
12 |
|
|
import java.util.concurrent.Callable; |
13 |
jsr166 |
1.23 |
import java.util.concurrent.CompletionService; |
14 |
jsr166 |
1.24 |
import java.util.concurrent.CountDownLatch; |
15 |
|
|
import java.util.concurrent.ExecutionException; |
16 |
jsr166 |
1.18 |
import java.util.concurrent.ExecutorCompletionService; |
17 |
jsr166 |
1.24 |
import java.util.concurrent.Executor; |
18 |
jsr166 |
1.19 |
import java.util.concurrent.Executors; |
19 |
jsr166 |
1.18 |
import java.util.concurrent.ExecutorService; |
20 |
jsr166 |
1.24 |
import java.util.concurrent.ForkJoinPool; |
21 |
jsr166 |
1.18 |
import java.util.concurrent.Future; |
22 |
|
|
import java.util.concurrent.FutureTask; |
23 |
|
|
import java.util.concurrent.RunnableFuture; |
24 |
|
|
import java.util.concurrent.ThreadPoolExecutor; |
25 |
|
|
import java.util.concurrent.TimeUnit; |
26 |
|
|
import java.util.concurrent.atomic.AtomicBoolean; |
27 |
jsr166 |
1.19 |
|
28 |
|
|
import junit.framework.Test; |
29 |
|
|
import junit.framework.TestSuite; |
30 |
dl |
1.1 |
|
31 |
jsr166 |
1.8 |
public class ExecutorCompletionServiceTest extends JSR166TestCase { |
32 |
dl |
1.1 |
public static void main(String[] args) { |
33 |
jsr166 |
1.21 |
main(suite(), args); |
34 |
dl |
1.1 |
} |
35 |
|
|
public static Test suite() { |
36 |
|
|
return new TestSuite(ExecutorCompletionServiceTest.class); |
37 |
|
|
} |
38 |
|
|
|
39 |
|
|
/** |
40 |
jsr166 |
1.24 |
* new ExecutorCompletionService(null) throws NullPointerException |
41 |
jsr166 |
1.7 |
*/ |
42 |
dl |
1.1 |
public void testConstructorNPE() { |
43 |
|
|
try { |
44 |
jsr166 |
1.20 |
new ExecutorCompletionService(null); |
45 |
dl |
1.1 |
shouldThrow(); |
46 |
jsr166 |
1.10 |
} catch (NullPointerException success) {} |
47 |
dl |
1.1 |
} |
48 |
|
|
|
49 |
|
|
/** |
50 |
jsr166 |
1.24 |
* new ExecutorCompletionService(e, null) throws NullPointerException |
51 |
jsr166 |
1.7 |
*/ |
52 |
dl |
1.1 |
public void testConstructorNPE2() { |
53 |
jsr166 |
1.24 |
final Executor e = ForkJoinPool.commonPool(); |
54 |
dl |
1.1 |
try { |
55 |
jsr166 |
1.20 |
new ExecutorCompletionService(e, null); |
56 |
dl |
1.1 |
shouldThrow(); |
57 |
jsr166 |
1.10 |
} catch (NullPointerException success) {} |
58 |
dl |
1.1 |
} |
59 |
|
|
|
60 |
|
|
/** |
61 |
jsr166 |
1.24 |
* ecs.submit(null) throws NullPointerException |
62 |
jsr166 |
1.7 |
*/ |
63 |
jsr166 |
1.24 |
public void testSubmitNullCallable() { |
64 |
|
|
final ExecutorCompletionService ecs = |
65 |
|
|
new ExecutorCompletionService(ForkJoinPool.commonPool()); |
66 |
|
|
try { |
67 |
|
|
ecs.submit((Callable) null); |
68 |
|
|
shouldThrow(); |
69 |
|
|
} catch (NullPointerException success) {} |
70 |
dl |
1.1 |
} |
71 |
|
|
|
72 |
|
|
/** |
73 |
jsr166 |
1.24 |
* ecs.submit(null, val) throws NullPointerException |
74 |
jsr166 |
1.7 |
*/ |
75 |
jsr166 |
1.24 |
public void testSubmitNullRunnable() { |
76 |
|
|
final ExecutorCompletionService ecs = |
77 |
|
|
new ExecutorCompletionService(ForkJoinPool.commonPool()); |
78 |
|
|
try { |
79 |
|
|
ecs.submit((Runnable) null, Boolean.TRUE); |
80 |
|
|
shouldThrow(); |
81 |
|
|
} catch (NullPointerException success) {} |
82 |
dl |
1.1 |
} |
83 |
|
|
|
84 |
|
|
/** |
85 |
|
|
* A taken submitted task is completed |
86 |
jsr166 |
1.7 |
*/ |
87 |
jsr166 |
1.24 |
public void testTake() |
88 |
|
|
throws InterruptedException, ExecutionException { |
89 |
|
|
final ExecutorCompletionService ecs = |
90 |
|
|
new ExecutorCompletionService(ForkJoinPool.commonPool()); |
91 |
|
|
ecs.submit(new StringTask()); |
92 |
|
|
Future f = ecs.take(); |
93 |
|
|
assertTrue(f.isDone()); |
94 |
|
|
assertSame(TEST_STRING, f.get()); |
95 |
dl |
1.1 |
} |
96 |
|
|
|
97 |
|
|
/** |
98 |
|
|
* Take returns the same future object returned by submit |
99 |
jsr166 |
1.7 |
*/ |
100 |
jsr166 |
1.10 |
public void testTake2() throws InterruptedException { |
101 |
jsr166 |
1.24 |
final ExecutorCompletionService ecs = |
102 |
|
|
new ExecutorCompletionService(ForkJoinPool.commonPool()); |
103 |
|
|
Future f1 = ecs.submit(new StringTask()); |
104 |
|
|
Future f2 = ecs.take(); |
105 |
|
|
assertSame(f1, f2); |
106 |
|
|
} |
107 |
|
|
|
108 |
|
|
/** |
109 |
|
|
* poll returns non-null when the returned task is completed |
110 |
|
|
*/ |
111 |
|
|
public void testPoll1() |
112 |
|
|
throws InterruptedException, ExecutionException { |
113 |
|
|
final ExecutorCompletionService ecs = |
114 |
|
|
new ExecutorCompletionService(ForkJoinPool.commonPool()); |
115 |
|
|
assertNull(ecs.poll()); |
116 |
|
|
ecs.submit(new StringTask()); |
117 |
|
|
|
118 |
|
|
long startTime = System.nanoTime(); |
119 |
|
|
Future f; |
120 |
|
|
while ((f = ecs.poll()) == null) { |
121 |
|
|
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
122 |
|
|
fail("timed out"); |
123 |
|
|
Thread.yield(); |
124 |
dl |
1.1 |
} |
125 |
jsr166 |
1.24 |
assertTrue(f.isDone()); |
126 |
|
|
assertSame(TEST_STRING, f.get()); |
127 |
dl |
1.1 |
} |
128 |
|
|
|
129 |
|
|
/** |
130 |
jsr166 |
1.24 |
* timed poll returns non-null when the returned task is completed |
131 |
jsr166 |
1.7 |
*/ |
132 |
jsr166 |
1.24 |
public void testPoll2() |
133 |
|
|
throws InterruptedException, ExecutionException { |
134 |
|
|
final ExecutorCompletionService ecs = |
135 |
|
|
new ExecutorCompletionService(ForkJoinPool.commonPool()); |
136 |
|
|
assertNull(ecs.poll()); |
137 |
|
|
ecs.submit(new StringTask()); |
138 |
|
|
|
139 |
|
|
long startTime = System.nanoTime(); |
140 |
|
|
Future f; |
141 |
|
|
while ((f = ecs.poll(SHORT_DELAY_MS, MILLISECONDS)) == null) { |
142 |
|
|
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
143 |
|
|
fail("timed out"); |
144 |
|
|
Thread.yield(); |
145 |
dl |
1.1 |
} |
146 |
jsr166 |
1.24 |
assertTrue(f.isDone()); |
147 |
|
|
assertSame(TEST_STRING, f.get()); |
148 |
|
|
} |
149 |
|
|
|
150 |
|
|
/** |
151 |
|
|
* poll returns null before the returned task is completed |
152 |
|
|
*/ |
153 |
|
|
public void testPollReturnsNull() |
154 |
|
|
throws InterruptedException, ExecutionException { |
155 |
|
|
final ExecutorCompletionService ecs = |
156 |
|
|
new ExecutorCompletionService(ForkJoinPool.commonPool()); |
157 |
|
|
final CountDownLatch proceed = new CountDownLatch(1); |
158 |
|
|
ecs.submit(new Callable() { public String call() throws Exception { |
159 |
|
|
proceed.await(); |
160 |
|
|
return TEST_STRING; |
161 |
|
|
}}); |
162 |
|
|
assertNull(ecs.poll()); |
163 |
|
|
assertNull(ecs.poll(0L, MILLISECONDS)); |
164 |
|
|
assertNull(ecs.poll(Long.MIN_VALUE, MILLISECONDS)); |
165 |
|
|
long startTime = System.nanoTime(); |
166 |
|
|
assertNull(ecs.poll(timeoutMillis(), MILLISECONDS)); |
167 |
|
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
168 |
|
|
proceed.countDown(); |
169 |
|
|
assertSame(TEST_STRING, ecs.take().get()); |
170 |
dl |
1.1 |
} |
171 |
|
|
|
172 |
|
|
/** |
173 |
jsr166 |
1.24 |
* successful and failed tasks are both returned |
174 |
jsr166 |
1.7 |
*/ |
175 |
jsr166 |
1.24 |
public void testTaskAssortment() |
176 |
|
|
throws InterruptedException, ExecutionException { |
177 |
jsr166 |
1.22 |
final ExecutorService e = Executors.newCachedThreadPool(); |
178 |
jsr166 |
1.24 |
final CompletionService cs = new ExecutorCompletionService(e); |
179 |
|
|
final ArithmeticException ex = new ArithmeticException(); |
180 |
jsr166 |
1.22 |
try (PoolCleaner cleaner = cleaner(e)) { |
181 |
jsr166 |
1.24 |
for (int i = 0; i < 2; i++) { |
182 |
|
|
cs.submit(new StringTask()); |
183 |
|
|
cs.submit(callableThrowing(ex)); |
184 |
|
|
cs.submit(runnableThrowing(ex), null); |
185 |
|
|
} |
186 |
|
|
int normalCompletions = 0; |
187 |
|
|
int exceptionalCompletions = 0; |
188 |
|
|
for (int i = 0; i < 3 * 2; i++) { |
189 |
|
|
try { |
190 |
|
|
if (cs.take().get() == TEST_STRING) |
191 |
|
|
normalCompletions++; |
192 |
|
|
} |
193 |
|
|
catch (ExecutionException expected) { |
194 |
|
|
assertTrue(expected.getCause() instanceof ArithmeticException); |
195 |
|
|
exceptionalCompletions++; |
196 |
|
|
} |
197 |
|
|
} |
198 |
|
|
assertEquals(2 * 1, normalCompletions); |
199 |
|
|
assertEquals(2 * 2, exceptionalCompletions); |
200 |
|
|
assertNull(cs.poll()); |
201 |
dl |
1.1 |
} |
202 |
|
|
} |
203 |
jsr166 |
1.12 |
|
204 |
|
|
/** |
205 |
|
|
* Submitting to underlying AES that overrides newTaskFor(Callable) |
206 |
|
|
* returns and eventually runs Future returned by newTaskFor. |
207 |
|
|
*/ |
208 |
|
|
public void testNewTaskForCallable() throws InterruptedException { |
209 |
|
|
final AtomicBoolean done = new AtomicBoolean(false); |
210 |
|
|
class MyCallableFuture<V> extends FutureTask<V> { |
211 |
|
|
MyCallableFuture(Callable<V> c) { super(c); } |
212 |
jsr166 |
1.24 |
@Override protected void done() { done.set(true); } |
213 |
jsr166 |
1.12 |
} |
214 |
jsr166 |
1.22 |
final ExecutorService e = |
215 |
|
|
new ThreadPoolExecutor(1, 1, |
216 |
|
|
30L, TimeUnit.SECONDS, |
217 |
|
|
new ArrayBlockingQueue<Runnable>(1)) { |
218 |
|
|
protected <T> RunnableFuture<T> newTaskFor(Callable<T> c) { |
219 |
|
|
return new MyCallableFuture<T>(c); |
220 |
|
|
}}; |
221 |
jsr166 |
1.24 |
CompletionService<String> cs = new ExecutorCompletionService<>(e); |
222 |
jsr166 |
1.22 |
try (PoolCleaner cleaner = cleaner(e)) { |
223 |
jsr166 |
1.24 |
assertNull(cs.poll()); |
224 |
jsr166 |
1.12 |
Callable<String> c = new StringTask(); |
225 |
jsr166 |
1.24 |
Future f1 = cs.submit(c); |
226 |
jsr166 |
1.12 |
assertTrue("submit must return MyCallableFuture", |
227 |
|
|
f1 instanceof MyCallableFuture); |
228 |
jsr166 |
1.24 |
Future f2 = cs.take(); |
229 |
jsr166 |
1.12 |
assertSame("submit and take must return same objects", f1, f2); |
230 |
|
|
assertTrue("completed task must have set done", done.get()); |
231 |
|
|
} |
232 |
|
|
} |
233 |
dl |
1.4 |
|
234 |
jsr166 |
1.12 |
/** |
235 |
|
|
* Submitting to underlying AES that overrides newTaskFor(Runnable,T) |
236 |
|
|
* returns and eventually runs Future returned by newTaskFor. |
237 |
|
|
*/ |
238 |
|
|
public void testNewTaskForRunnable() throws InterruptedException { |
239 |
|
|
final AtomicBoolean done = new AtomicBoolean(false); |
240 |
|
|
class MyRunnableFuture<V> extends FutureTask<V> { |
241 |
|
|
MyRunnableFuture(Runnable t, V r) { super(t, r); } |
242 |
jsr166 |
1.24 |
@Override protected void done() { done.set(true); } |
243 |
jsr166 |
1.12 |
} |
244 |
jsr166 |
1.22 |
final ExecutorService e = |
245 |
|
|
new ThreadPoolExecutor(1, 1, |
246 |
|
|
30L, TimeUnit.SECONDS, |
247 |
|
|
new ArrayBlockingQueue<Runnable>(1)) { |
248 |
|
|
protected <T> RunnableFuture<T> newTaskFor(Runnable t, T r) { |
249 |
|
|
return new MyRunnableFuture<T>(t, r); |
250 |
|
|
}}; |
251 |
jsr166 |
1.24 |
CompletionService<String> cs = new ExecutorCompletionService<>(e); |
252 |
jsr166 |
1.22 |
try (PoolCleaner cleaner = cleaner(e)) { |
253 |
jsr166 |
1.24 |
assertNull(cs.poll()); |
254 |
jsr166 |
1.12 |
Runnable r = new NoOpRunnable(); |
255 |
jsr166 |
1.24 |
Future f1 = cs.submit(r, null); |
256 |
jsr166 |
1.12 |
assertTrue("submit must return MyRunnableFuture", |
257 |
|
|
f1 instanceof MyRunnableFuture); |
258 |
jsr166 |
1.24 |
Future f2 = cs.take(); |
259 |
jsr166 |
1.12 |
assertSame("submit and take must return same objects", f1, f2); |
260 |
|
|
assertTrue("completed task must have set done", done.get()); |
261 |
|
|
} |
262 |
|
|
} |
263 |
dl |
1.4 |
|
264 |
dl |
1.1 |
} |