710 |
|
* shutdownNow returns a list containing tasks that were not run, |
711 |
|
* and those tasks are drained from the queue |
712 |
|
*/ |
713 |
+ |
public void testShutdownNow() throws InterruptedException { |
714 |
+ |
final int poolSize = 2; |
715 |
+ |
final int count = 5; |
716 |
+ |
final AtomicInteger ran = new AtomicInteger(0); |
717 |
+ |
final CustomExecutor p = new CustomExecutor(poolSize); |
718 |
+ |
CountDownLatch threadsStarted = new CountDownLatch(poolSize); |
719 |
+ |
CheckedRunnable waiter = new CheckedRunnable() { public void realRun() { |
720 |
+ |
threadsStarted.countDown(); |
721 |
+ |
try { |
722 |
+ |
MILLISECONDS.sleep(2 * LONG_DELAY_MS); |
723 |
+ |
} catch (InterruptedException success) {} |
724 |
+ |
ran.getAndIncrement(); |
725 |
+ |
}}; |
726 |
+ |
for (int i = 0; i < count; i++) |
727 |
+ |
p.execute(waiter); |
728 |
+ |
assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
729 |
+ |
assertEquals(poolSize, p.getActiveCount()); |
730 |
+ |
assertEquals(0, p.getCompletedTaskCount()); |
731 |
+ |
final List<Runnable> queuedTasks; |
732 |
+ |
try { |
733 |
+ |
queuedTasks = p.shutdownNow(); |
734 |
+ |
} catch (SecurityException ok) { |
735 |
+ |
return; // Allowed in case test doesn't have privs |
736 |
+ |
} |
737 |
+ |
assertTrue(p.isShutdown()); |
738 |
+ |
assertTrue(p.getQueue().isEmpty()); |
739 |
+ |
assertEquals(count - poolSize, queuedTasks.size()); |
740 |
+ |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
741 |
+ |
assertTrue(p.isTerminated()); |
742 |
+ |
assertEquals(poolSize, ran.get()); |
743 |
+ |
assertEquals(poolSize, p.getCompletedTaskCount()); |
744 |
+ |
} |
745 |
+ |
|
746 |
+ |
/** |
747 |
+ |
* shutdownNow returns a list containing tasks that were not run, |
748 |
+ |
* and those tasks are drained from the queue |
749 |
+ |
*/ |
750 |
|
public void testShutdownNow_delayedTasks() throws InterruptedException { |
751 |
|
CustomExecutor p = new CustomExecutor(1); |
752 |
|
List<ScheduledFuture> tasks = new ArrayList<>(); |
756 |
|
tasks.add(p.scheduleAtFixedRate(r, 9, 9, SECONDS)); |
757 |
|
tasks.add(p.scheduleWithFixedDelay(r, 9, 9, SECONDS)); |
758 |
|
} |
759 |
< |
assertEquals(new HashSet(tasks), new HashSet(p.getQueue())); |
759 |
> |
if (testImplementationDetails) |
760 |
> |
assertEquals(new HashSet(tasks), new HashSet(p.getQueue())); |
761 |
|
final List<Runnable> queuedTasks; |
762 |
|
try { |
763 |
|
queuedTasks = p.shutdownNow(); |
766 |
|
} |
767 |
|
assertTrue(p.isShutdown()); |
768 |
|
assertTrue(p.getQueue().isEmpty()); |
769 |
< |
assertEquals(new HashSet(tasks), new HashSet(queuedTasks)); |
769 |
> |
if (testImplementationDetails) |
770 |
> |
assertEquals(new HashSet(tasks), new HashSet(queuedTasks)); |
771 |
|
assertEquals(tasks.size(), queuedTasks.size()); |
772 |
|
for (ScheduledFuture task : tasks) { |
773 |
|
assertFalse(((CustomTask)task).ran); |