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