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