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