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.math.BigInteger; |
11 |
< |
import java.security.*; |
10 |
> |
|
11 |
> |
import java.security.PrivilegedAction; |
12 |
> |
import java.security.PrivilegedExceptionAction; |
13 |
> |
import java.util.ArrayList; |
14 |
> |
import java.util.Collections; |
15 |
> |
import java.util.List; |
16 |
> |
import java.util.concurrent.AbstractExecutorService; |
17 |
> |
import java.util.concurrent.ArrayBlockingQueue; |
18 |
> |
import java.util.concurrent.Callable; |
19 |
> |
import java.util.concurrent.CancellationException; |
20 |
> |
import java.util.concurrent.CountDownLatch; |
21 |
> |
import java.util.concurrent.ExecutionException; |
22 |
> |
import java.util.concurrent.Executors; |
23 |
> |
import java.util.concurrent.ExecutorService; |
24 |
> |
import java.util.concurrent.Future; |
25 |
> |
import java.util.concurrent.ThreadPoolExecutor; |
26 |
> |
import java.util.concurrent.TimeUnit; |
27 |
> |
import java.util.concurrent.atomic.AtomicBoolean; |
28 |
> |
|
29 |
> |
import junit.framework.Test; |
30 |
> |
import junit.framework.TestSuite; |
31 |
|
|
32 |
|
public class AbstractExecutorServiceTest extends JSR166TestCase { |
33 |
|
public static void main(String[] args) { |
34 |
< |
junit.textui.TestRunner.run(suite()); |
34 |
> |
main(suite(), args); |
35 |
|
} |
36 |
|
public static Test suite() { |
37 |
|
return new TestSuite(AbstractExecutorServiceTest.class); |
61 |
|
*/ |
62 |
|
public void testExecuteRunnable() throws Exception { |
63 |
|
ExecutorService e = new DirectExecutorService(); |
64 |
< |
TrackedShortRunnable task = new TrackedShortRunnable(); |
65 |
< |
assertFalse(task.done); |
66 |
< |
Future<?> future = e.submit(task); |
67 |
< |
future.get(); |
68 |
< |
assertTrue(task.done); |
64 |
> |
final AtomicBoolean done = new AtomicBoolean(false); |
65 |
> |
Future<?> future = e.submit(new CheckedRunnable() { |
66 |
> |
public void realRun() { |
67 |
> |
done.set(true); |
68 |
> |
}}); |
69 |
> |
assertNull(future.get()); |
70 |
> |
assertNull(future.get(0, MILLISECONDS)); |
71 |
> |
assertTrue(done.get()); |
72 |
> |
assertTrue(future.isDone()); |
73 |
> |
assertFalse(future.isCancelled()); |
74 |
|
} |
75 |
|
|
56 |
– |
|
76 |
|
/** |
77 |
|
* Completed submit(callable) returns result |
78 |
|
*/ |
103 |
|
assertSame(TEST_STRING, result); |
104 |
|
} |
105 |
|
|
87 |
– |
|
106 |
|
/** |
107 |
|
* A submitted privileged action runs to completion |
108 |
|
*/ |
168 |
|
* execute(null runnable) throws NPE |
169 |
|
*/ |
170 |
|
public void testExecuteNullRunnable() { |
171 |
+ |
ExecutorService e = new DirectExecutorService(); |
172 |
|
try { |
154 |
– |
ExecutorService e = new DirectExecutorService(); |
173 |
|
e.submit((Runnable) null); |
174 |
|
shouldThrow(); |
175 |
|
} catch (NullPointerException success) {} |
176 |
|
} |
177 |
|
|
160 |
– |
|
178 |
|
/** |
179 |
|
* submit(null callable) throws NPE |
180 |
|
*/ |
181 |
|
public void testSubmitNullCallable() { |
182 |
+ |
ExecutorService e = new DirectExecutorService(); |
183 |
|
try { |
166 |
– |
ExecutorService e = new DirectExecutorService(); |
184 |
|
e.submit((Callable) null); |
185 |
|
shouldThrow(); |
186 |
|
} catch (NullPointerException success) {} |
228 |
|
new ArrayBlockingQueue<Runnable>(10)); |
229 |
|
|
230 |
|
Callable c = new Callable() { |
231 |
< |
public Object call() { return 5/0; }}; |
231 |
> |
public Object call() { throw new ArithmeticException(); }}; |
232 |
|
|
233 |
|
try { |
234 |
|
p.submit(c).get(); |
272 |
|
*/ |
273 |
|
public void testInvokeAny3() throws Exception { |
274 |
|
ExecutorService e = new DirectExecutorService(); |
275 |
< |
List<Callable<Integer>> l = new ArrayList<Callable<Integer>>(); |
276 |
< |
l.add(new Callable<Integer>() { |
277 |
< |
public Integer call() { return 5/0; }}); |
275 |
> |
List<Callable<Long>> l = new ArrayList<Callable<Long>>(); |
276 |
> |
l.add(new Callable<Long>() { |
277 |
> |
public Long call() { throw new ArithmeticException(); }}); |
278 |
|
l.add(null); |
279 |
|
try { |
280 |
|
e.invokeAny(l); |
401 |
|
} |
402 |
|
} |
403 |
|
|
387 |
– |
|
404 |
|
/** |
405 |
|
* timed invokeAny(null) throws NPE |
406 |
|
*/ |
450 |
|
*/ |
451 |
|
public void testTimedInvokeAny3() throws Exception { |
452 |
|
ExecutorService e = new DirectExecutorService(); |
453 |
< |
List<Callable<Integer>> l = new ArrayList<Callable<Integer>>(); |
454 |
< |
l.add(new Callable<Integer>() { |
455 |
< |
public Integer call() { return 5/0; }}); |
453 |
> |
List<Callable<Long>> l = new ArrayList<Callable<Long>>(); |
454 |
> |
l.add(new Callable<Long>() { |
455 |
> |
public Long call() { throw new ArithmeticException(); }}); |
456 |
|
l.add(null); |
457 |
|
try { |
458 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
588 |
|
l.add(new StringTask()); |
589 |
|
l.add(new StringTask()); |
590 |
|
List<Future<String>> futures = |
591 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
591 |
> |
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
592 |
|
assertEquals(2, futures.size()); |
593 |
|
for (Future<String> future : futures) |
594 |
|
assertSame(TEST_STRING, future.get()); |
600 |
|
/** |
601 |
|
* timed invokeAll cancels tasks not completed by timeout |
602 |
|
*/ |
603 |
< |
public void testTimedInvokeAll6() throws InterruptedException { |
603 |
> |
public void testTimedInvokeAll6() throws Exception { |
604 |
|
ExecutorService e = new DirectExecutorService(); |
605 |
|
try { |
606 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
607 |
< |
l.add(new StringTask()); |
608 |
< |
l.add(Executors.callable(possiblyInterruptedRunnable(2 * SHORT_DELAY_MS), TEST_STRING)); |
609 |
< |
l.add(new StringTask()); |
610 |
< |
List<Future<String>> futures = |
611 |
< |
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
612 |
< |
assertEquals(3, futures.size()); |
613 |
< |
Iterator<Future<String>> it = futures.iterator(); |
614 |
< |
Future<String> f1 = it.next(); |
615 |
< |
Future<String> f2 = it.next(); |
616 |
< |
Future<String> f3 = it.next(); |
617 |
< |
assertTrue(f1.isDone()); |
618 |
< |
assertFalse(f1.isCancelled()); |
619 |
< |
assertTrue(f2.isDone()); |
620 |
< |
assertFalse(f2.isCancelled()); |
621 |
< |
assertTrue(f3.isDone()); |
622 |
< |
assertTrue(f3.isCancelled()); |
606 |
> |
for (long timeout = timeoutMillis();;) { |
607 |
> |
List<Callable<String>> tasks = new ArrayList<>(); |
608 |
> |
tasks.add(new StringTask("0")); |
609 |
> |
tasks.add(Executors.callable(possiblyInterruptedRunnable(timeout), |
610 |
> |
TEST_STRING)); |
611 |
> |
tasks.add(new StringTask("2")); |
612 |
> |
long startTime = System.nanoTime(); |
613 |
> |
List<Future<String>> futures = |
614 |
> |
e.invokeAll(tasks, timeout, MILLISECONDS); |
615 |
> |
assertEquals(tasks.size(), futures.size()); |
616 |
> |
assertTrue(millisElapsedSince(startTime) >= timeout); |
617 |
> |
for (Future future : futures) |
618 |
> |
assertTrue(future.isDone()); |
619 |
> |
try { |
620 |
> |
assertEquals("0", futures.get(0).get()); |
621 |
> |
assertEquals(TEST_STRING, futures.get(1).get()); |
622 |
> |
} catch (CancellationException retryWithLongerTimeout) { |
623 |
> |
// unusual delay before starting second task |
624 |
> |
timeout *= 2; |
625 |
> |
if (timeout >= LONG_DELAY_MS / 2) |
626 |
> |
fail("expected exactly one task to be cancelled"); |
627 |
> |
continue; |
628 |
> |
} |
629 |
> |
assertTrue(futures.get(2).isCancelled()); |
630 |
> |
break; |
631 |
> |
} |
632 |
|
} finally { |
633 |
|
joinPool(e); |
634 |
|
} |