6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
– |
import junit.framework.*; |
10 |
– |
import java.util.*; |
11 |
– |
import java.util.concurrent.*; |
9 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
+ |
import static java.util.concurrent.TimeUnit.SECONDS; |
11 |
+ |
|
12 |
+ |
import java.util.ArrayList; |
13 |
+ |
import java.util.HashSet; |
14 |
+ |
import java.util.List; |
15 |
+ |
import java.util.concurrent.BlockingQueue; |
16 |
+ |
import java.util.concurrent.Callable; |
17 |
+ |
import java.util.concurrent.CancellationException; |
18 |
+ |
import java.util.concurrent.CountDownLatch; |
19 |
+ |
import java.util.concurrent.ExecutionException; |
20 |
+ |
import java.util.concurrent.Executors; |
21 |
+ |
import java.util.concurrent.ExecutorService; |
22 |
+ |
import java.util.concurrent.Future; |
23 |
+ |
import java.util.concurrent.RejectedExecutionException; |
24 |
+ |
import java.util.concurrent.ScheduledFuture; |
25 |
+ |
import java.util.concurrent.ScheduledThreadPoolExecutor; |
26 |
+ |
import java.util.concurrent.ThreadFactory; |
27 |
+ |
import java.util.concurrent.ThreadPoolExecutor; |
28 |
|
import java.util.concurrent.atomic.AtomicInteger; |
29 |
|
|
30 |
+ |
import junit.framework.Test; |
31 |
+ |
import junit.framework.TestSuite; |
32 |
+ |
|
33 |
|
public class ScheduledExecutorTest extends JSR166TestCase { |
34 |
|
public static void main(String[] args) { |
35 |
< |
junit.textui.TestRunner.run(suite()); |
35 |
> |
main(suite(), args); |
36 |
|
} |
37 |
|
public static Test suite() { |
38 |
|
return new TestSuite(ScheduledExecutorTest.class); |
653 |
|
} |
654 |
|
|
655 |
|
/** |
656 |
< |
* shutdownNow returns a list containing tasks that were not run |
656 |
> |
* shutdownNow returns a list containing tasks that were not run, |
657 |
> |
* and those tasks are drained from the queue |
658 |
|
*/ |
659 |
< |
public void testShutdownNow() { |
659 |
> |
public void testShutdownNow_delayedTasks() throws InterruptedException { |
660 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
661 |
< |
for (int i = 0; i < 5; i++) |
662 |
< |
p.schedule(new SmallPossiblyInterruptedRunnable(), |
663 |
< |
LONG_DELAY_MS, MILLISECONDS); |
661 |
> |
List<ScheduledFuture> tasks = new ArrayList<>(); |
662 |
> |
for (int i = 0; i < 3; i++) { |
663 |
> |
Runnable r = new NoOpRunnable(); |
664 |
> |
tasks.add(p.schedule(r, 9, SECONDS)); |
665 |
> |
tasks.add(p.scheduleAtFixedRate(r, 9, 9, SECONDS)); |
666 |
> |
tasks.add(p.scheduleWithFixedDelay(r, 9, 9, SECONDS)); |
667 |
> |
} |
668 |
> |
if (testImplementationDetails) |
669 |
> |
assertEquals(new HashSet(tasks), new HashSet(p.getQueue())); |
670 |
> |
final List<Runnable> queuedTasks; |
671 |
|
try { |
672 |
< |
List<Runnable> l = p.shutdownNow(); |
647 |
< |
assertTrue(p.isShutdown()); |
648 |
< |
assertEquals(5, l.size()); |
672 |
> |
queuedTasks = p.shutdownNow(); |
673 |
|
} catch (SecurityException ok) { |
674 |
< |
// Allowed in case test doesn't have privs |
651 |
< |
} finally { |
652 |
< |
joinPool(p); |
674 |
> |
return; // Allowed in case test doesn't have privs |
675 |
|
} |
676 |
+ |
assertTrue(p.isShutdown()); |
677 |
+ |
assertTrue(p.getQueue().isEmpty()); |
678 |
+ |
if (testImplementationDetails) |
679 |
+ |
assertEquals(new HashSet(tasks), new HashSet(queuedTasks)); |
680 |
+ |
assertEquals(tasks.size(), queuedTasks.size()); |
681 |
+ |
for (ScheduledFuture task : tasks) { |
682 |
+ |
assertFalse(task.isDone()); |
683 |
+ |
assertFalse(task.isCancelled()); |
684 |
+ |
} |
685 |
+ |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
686 |
+ |
assertTrue(p.isTerminated()); |
687 |
|
} |
688 |
|
|
689 |
|
/** |
1204 |
|
public void testTimedInvokeAll6() throws Exception { |
1205 |
|
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1206 |
|
try { |
1207 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1208 |
< |
l.add(new StringTask()); |
1209 |
< |
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
1210 |
< |
l.add(new StringTask()); |
1211 |
< |
List<Future<String>> futures = |
1212 |
< |
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1213 |
< |
assertEquals(l.size(), futures.size()); |
1214 |
< |
for (Future future : futures) |
1215 |
< |
assertTrue(future.isDone()); |
1216 |
< |
assertFalse(futures.get(0).isCancelled()); |
1217 |
< |
assertTrue(futures.get(1).isCancelled()); |
1207 |
> |
for (long timeout = timeoutMillis();;) { |
1208 |
> |
List<Callable<String>> tasks = new ArrayList<>(); |
1209 |
> |
tasks.add(new StringTask("0")); |
1210 |
> |
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1211 |
> |
tasks.add(new StringTask("2")); |
1212 |
> |
long startTime = System.nanoTime(); |
1213 |
> |
List<Future<String>> futures = |
1214 |
> |
e.invokeAll(tasks, timeout, MILLISECONDS); |
1215 |
> |
assertEquals(tasks.size(), futures.size()); |
1216 |
> |
assertTrue(millisElapsedSince(startTime) >= timeout); |
1217 |
> |
for (Future future : futures) |
1218 |
> |
assertTrue(future.isDone()); |
1219 |
> |
assertTrue(futures.get(1).isCancelled()); |
1220 |
> |
try { |
1221 |
> |
assertEquals("0", futures.get(0).get()); |
1222 |
> |
assertEquals("2", futures.get(2).get()); |
1223 |
> |
break; |
1224 |
> |
} catch (CancellationException retryWithLongerTimeout) { |
1225 |
> |
timeout *= 2; |
1226 |
> |
if (timeout >= LONG_DELAY_MS / 2) |
1227 |
> |
fail("expected exactly one task to be cancelled"); |
1228 |
> |
} |
1229 |
> |
} |
1230 |
|
} finally { |
1231 |
|
joinPool(e); |
1232 |
|
} |