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 |
– |
|
9 |
|
import junit.framework.*; |
10 |
|
import java.util.*; |
11 |
|
import java.util.concurrent.*; |
12 |
+ |
import java.util.concurrent.atomic.AtomicBoolean; |
13 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
14 |
– |
import java.math.BigInteger; |
14 |
|
import java.security.*; |
15 |
|
|
16 |
|
public class AbstractExecutorServiceTest extends JSR166TestCase { |
45 |
|
*/ |
46 |
|
public void testExecuteRunnable() throws Exception { |
47 |
|
ExecutorService e = new DirectExecutorService(); |
48 |
< |
TrackedShortRunnable task = new TrackedShortRunnable(); |
49 |
< |
assertFalse(task.done); |
48 |
> |
final AtomicBoolean done = new AtomicBoolean(false); |
49 |
> |
CheckedRunnable task = new CheckedRunnable() { |
50 |
> |
public void realRun() { |
51 |
> |
done.set(true); |
52 |
> |
}}; |
53 |
|
Future<?> future = e.submit(task); |
54 |
< |
future.get(); |
55 |
< |
assertTrue(task.done); |
54 |
> |
assertNull(future.get()); |
55 |
> |
assertNull(future.get(0, MILLISECONDS)); |
56 |
> |
assertTrue(done.get()); |
57 |
> |
assertTrue(future.isDone()); |
58 |
> |
assertFalse(future.isCancelled()); |
59 |
|
} |
60 |
|
|
56 |
– |
|
61 |
|
/** |
62 |
|
* Completed submit(callable) returns result |
63 |
|
*/ |
88 |
|
assertSame(TEST_STRING, result); |
89 |
|
} |
90 |
|
|
87 |
– |
|
91 |
|
/** |
92 |
|
* A submitted privileged action runs to completion |
93 |
|
*/ |
160 |
|
} catch (NullPointerException success) {} |
161 |
|
} |
162 |
|
|
160 |
– |
|
163 |
|
/** |
164 |
|
* submit(null callable) throws NPE |
165 |
|
*/ |
172 |
|
} |
173 |
|
|
174 |
|
/** |
173 |
– |
* submit(runnable) throws RejectedExecutionException if |
174 |
– |
* executor is saturated. |
175 |
– |
*/ |
176 |
– |
public void testExecute1() { |
177 |
– |
ThreadPoolExecutor p = |
178 |
– |
new ThreadPoolExecutor(1, 1, |
179 |
– |
60, TimeUnit.SECONDS, |
180 |
– |
new ArrayBlockingQueue<Runnable>(1)); |
181 |
– |
try { |
182 |
– |
for (int i = 0; i < 2; ++i) |
183 |
– |
p.submit(new MediumRunnable()); |
184 |
– |
for (int i = 0; i < 2; ++i) { |
185 |
– |
try { |
186 |
– |
p.submit(new MediumRunnable()); |
187 |
– |
shouldThrow(); |
188 |
– |
} catch (RejectedExecutionException success) {} |
189 |
– |
} |
190 |
– |
} finally { |
191 |
– |
joinPool(p); |
192 |
– |
} |
193 |
– |
} |
194 |
– |
|
195 |
– |
/** |
196 |
– |
* submit(callable) throws RejectedExecutionException |
197 |
– |
* if executor is saturated. |
198 |
– |
*/ |
199 |
– |
public void testExecute2() { |
200 |
– |
ThreadPoolExecutor p = |
201 |
– |
new ThreadPoolExecutor(1, 1, |
202 |
– |
60, TimeUnit.SECONDS, |
203 |
– |
new ArrayBlockingQueue<Runnable>(1)); |
204 |
– |
try { |
205 |
– |
for (int i = 0; i < 2; ++i) |
206 |
– |
p.submit(new MediumRunnable()); |
207 |
– |
for (int i = 0; i < 2; ++i) { |
208 |
– |
try { |
209 |
– |
p.submit(new SmallCallable()); |
210 |
– |
shouldThrow(); |
211 |
– |
} catch (RejectedExecutionException success) {} |
212 |
– |
} |
213 |
– |
} finally { |
214 |
– |
joinPool(p); |
215 |
– |
} |
216 |
– |
} |
217 |
– |
|
218 |
– |
|
219 |
– |
/** |
175 |
|
* submit(callable).get() throws InterruptedException if interrupted |
176 |
|
*/ |
177 |
|
public void testInterruptedSubmit() throws InterruptedException { |
203 |
|
} |
204 |
|
|
205 |
|
/** |
251 |
– |
* get of submitted callable throws InterruptedException if callable |
252 |
– |
* interrupted |
253 |
– |
*/ |
254 |
– |
public void testSubmitIE() throws InterruptedException { |
255 |
– |
final ThreadPoolExecutor p = |
256 |
– |
new ThreadPoolExecutor(1, 1, |
257 |
– |
60, TimeUnit.SECONDS, |
258 |
– |
new ArrayBlockingQueue<Runnable>(10)); |
259 |
– |
|
260 |
– |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
261 |
– |
public void realRun() throws Exception { |
262 |
– |
p.submit(new SmallCallable()).get(); |
263 |
– |
}}); |
264 |
– |
|
265 |
– |
t.start(); |
266 |
– |
Thread.sleep(SHORT_DELAY_MS); |
267 |
– |
t.interrupt(); |
268 |
– |
t.join(); |
269 |
– |
joinPool(p); |
270 |
– |
} |
271 |
– |
|
272 |
– |
/** |
206 |
|
* get of submit(callable) throws ExecutionException if callable |
207 |
|
* throws exception |
208 |
|
*/ |
213 |
|
new ArrayBlockingQueue<Runnable>(10)); |
214 |
|
|
215 |
|
Callable c = new Callable() { |
216 |
< |
public Object call() { return 5/0; }}; |
216 |
> |
public Object call() { throw new ArithmeticException(); }}; |
217 |
|
|
218 |
|
try { |
219 |
|
p.submit(c).get(); |
227 |
|
/** |
228 |
|
* invokeAny(null) throws NPE |
229 |
|
*/ |
230 |
< |
public void testInvokeAny1() |
298 |
< |
throws InterruptedException, ExecutionException { |
230 |
> |
public void testInvokeAny1() throws Exception { |
231 |
|
ExecutorService e = new DirectExecutorService(); |
232 |
|
try { |
233 |
|
e.invokeAny(null); |
241 |
|
/** |
242 |
|
* invokeAny(empty collection) throws IAE |
243 |
|
*/ |
244 |
< |
public void testInvokeAny2() |
313 |
< |
throws InterruptedException, ExecutionException { |
244 |
> |
public void testInvokeAny2() throws Exception { |
245 |
|
ExecutorService e = new DirectExecutorService(); |
246 |
|
try { |
247 |
|
e.invokeAny(new ArrayList<Callable<String>>()); |
257 |
|
*/ |
258 |
|
public void testInvokeAny3() throws Exception { |
259 |
|
ExecutorService e = new DirectExecutorService(); |
260 |
< |
List<Callable<Integer>> l = new ArrayList<Callable<Integer>>(); |
261 |
< |
l.add(new Callable<Integer>() { |
262 |
< |
public Integer call() { return 5/0; }}); |
260 |
> |
List<Callable<Long>> l = new ArrayList<Callable<Long>>(); |
261 |
> |
l.add(new Callable<Long>() { |
262 |
> |
public Long call() { throw new ArithmeticException(); }}); |
263 |
|
l.add(null); |
264 |
|
try { |
265 |
|
e.invokeAny(l); |
386 |
|
} |
387 |
|
} |
388 |
|
|
458 |
– |
|
389 |
|
/** |
390 |
|
* timed invokeAny(null) throws NPE |
391 |
|
*/ |
435 |
|
*/ |
436 |
|
public void testTimedInvokeAny3() throws Exception { |
437 |
|
ExecutorService e = new DirectExecutorService(); |
438 |
< |
List<Callable<Integer>> l = new ArrayList<Callable<Integer>>(); |
439 |
< |
l.add(new Callable<Integer>() { |
440 |
< |
public Integer call() { return 5/0; }}); |
438 |
> |
List<Callable<Long>> l = new ArrayList<Callable<Long>>(); |
439 |
> |
l.add(new Callable<Long>() { |
440 |
> |
public Long call() { throw new ArithmeticException(); }}); |
441 |
|
l.add(null); |
442 |
|
try { |
443 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
590 |
|
try { |
591 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
592 |
|
l.add(new StringTask()); |
593 |
< |
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
593 |
> |
l.add(Executors.callable(possiblyInterruptedRunnable(2 * SHORT_DELAY_MS), TEST_STRING)); |
594 |
|
l.add(new StringTask()); |
595 |
|
List<Future<String>> futures = |
596 |
< |
e.invokeAll(l, SMALL_DELAY_MS, MILLISECONDS); |
597 |
< |
assertEquals(3, futures.size()); |
598 |
< |
Iterator<Future<String>> it = futures.iterator(); |
599 |
< |
Future<String> f1 = it.next(); |
600 |
< |
Future<String> f2 = it.next(); |
601 |
< |
Future<String> f3 = it.next(); |
602 |
< |
assertTrue(f1.isDone()); |
673 |
< |
assertFalse(f1.isCancelled()); |
674 |
< |
assertTrue(f2.isDone()); |
675 |
< |
assertTrue(f3.isDone()); |
676 |
< |
assertTrue(f3.isCancelled()); |
596 |
> |
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
597 |
> |
assertEquals(l.size(), futures.size()); |
598 |
> |
for (Future future : futures) |
599 |
> |
assertTrue(future.isDone()); |
600 |
> |
assertFalse(futures.get(0).isCancelled()); |
601 |
> |
assertFalse(futures.get(1).isCancelled()); |
602 |
> |
assertTrue(futures.get(2).isCancelled()); |
603 |
|
} finally { |
604 |
|
joinPool(e); |
605 |
|
} |