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; |
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); |
168 |
|
* execute(null runnable) throws NPE |
169 |
|
*/ |
170 |
|
public void testExecuteNullRunnable() { |
171 |
+ |
ExecutorService e = new DirectExecutorService(); |
172 |
|
try { |
171 |
– |
ExecutorService e = new DirectExecutorService(); |
173 |
|
e.submit((Runnable) null); |
174 |
|
shouldThrow(); |
175 |
|
} catch (NullPointerException success) {} |
179 |
|
* submit(null callable) throws NPE |
180 |
|
*/ |
181 |
|
public void testSubmitNullCallable() { |
182 |
+ |
ExecutorService e = new DirectExecutorService(); |
183 |
|
try { |
182 |
– |
ExecutorService e = new DirectExecutorService(); |
184 |
|
e.submit((Callable) null); |
185 |
|
shouldThrow(); |
186 |
|
} catch (NullPointerException success) {} |
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()); |
606 |
> |
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(l, SHORT_DELAY_MS, MILLISECONDS); |
615 |
< |
assertEquals(l.size(), futures.size()); |
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 |
< |
assertFalse(futures.get(0).isCancelled()); |
620 |
< |
assertFalse(futures.get(1).isCancelled()); |
619 |
> |
assertEquals("0", futures.get(0).get()); |
620 |
> |
assertEquals(TEST_STRING, futures.get(1).get()); |
621 |
|
assertTrue(futures.get(2).isCancelled()); |
622 |
|
} finally { |
623 |
|
joinPool(e); |