5 |
|
*/ |
6 |
|
|
7 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
8 |
+ |
import static java.util.concurrent.TimeUnit.SECONDS; |
9 |
|
|
10 |
|
import java.util.ArrayList; |
11 |
+ |
import java.util.HashSet; |
12 |
|
import java.util.List; |
13 |
|
import java.util.concurrent.BlockingQueue; |
14 |
|
import java.util.concurrent.Callable; |
707 |
|
} |
708 |
|
|
709 |
|
/** |
710 |
< |
* shutdownNow returns a list containing tasks that were not run |
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() { |
713 |
> |
public void testShutdownNow_delayedTasks() throws InterruptedException { |
714 |
|
CustomExecutor p = new CustomExecutor(1); |
715 |
< |
for (int i = 0; i < 5; i++) |
716 |
< |
p.schedule(new SmallPossiblyInterruptedRunnable(), |
717 |
< |
LONG_DELAY_MS, MILLISECONDS); |
715 |
> |
List<ScheduledFuture> tasks = new ArrayList<>(); |
716 |
> |
for (int i = 0; i < 3; i++) { |
717 |
> |
Runnable r = new NoOpRunnable(); |
718 |
> |
tasks.add(p.schedule(r, 9, SECONDS)); |
719 |
> |
tasks.add(p.scheduleAtFixedRate(r, 9, 9, SECONDS)); |
720 |
> |
tasks.add(p.scheduleWithFixedDelay(r, 9, 9, SECONDS)); |
721 |
> |
} |
722 |
> |
if (testImplementationDetails) |
723 |
> |
assertEquals(new HashSet(tasks), new HashSet(p.getQueue())); |
724 |
> |
final List<Runnable> queuedTasks; |
725 |
|
try { |
726 |
< |
List<Runnable> l = p.shutdownNow(); |
717 |
< |
assertTrue(p.isShutdown()); |
718 |
< |
assertEquals(5, l.size()); |
726 |
> |
queuedTasks = p.shutdownNow(); |
727 |
|
} catch (SecurityException ok) { |
728 |
< |
// Allowed in case test doesn't have privs |
729 |
< |
} finally { |
730 |
< |
joinPool(p); |
728 |
> |
return; // Allowed in case test doesn't have privs |
729 |
> |
} |
730 |
> |
assertTrue(p.isShutdown()); |
731 |
> |
assertTrue(p.getQueue().isEmpty()); |
732 |
> |
if (testImplementationDetails) |
733 |
> |
assertEquals(new HashSet(tasks), new HashSet(queuedTasks)); |
734 |
> |
assertEquals(tasks.size(), queuedTasks.size()); |
735 |
> |
for (ScheduledFuture task : tasks) { |
736 |
> |
assertFalse(((CustomTask)task).ran); |
737 |
> |
assertFalse(task.isDone()); |
738 |
> |
assertFalse(task.isCancelled()); |
739 |
|
} |
740 |
+ |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
741 |
+ |
assertTrue(p.isTerminated()); |
742 |
|
} |
743 |
|
|
744 |
|
/** |