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; |
15 |
+ |
import java.util.concurrent.CancellationException; |
16 |
|
import java.util.concurrent.CountDownLatch; |
17 |
|
import java.util.concurrent.Delayed; |
18 |
|
import java.util.concurrent.ExecutionException; |
35 |
|
|
36 |
|
public class ScheduledExecutorSubclassTest extends JSR166TestCase { |
37 |
|
public static void main(String[] args) { |
38 |
< |
junit.textui.TestRunner.run(suite()); |
38 |
> |
main(suite(), args); |
39 |
|
} |
40 |
|
public static Test suite() { |
41 |
|
return new TestSuite(ScheduledExecutorSubclassTest.class); |
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() 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 |
> |
final List<Runnable> queuedTasks; |
730 |
> |
try { |
731 |
> |
queuedTasks = p.shutdownNow(); |
732 |
> |
} catch (SecurityException ok) { |
733 |
> |
return; // Allowed in case test doesn't have privs |
734 |
> |
} |
735 |
> |
assertTrue(p.isShutdown()); |
736 |
> |
assertTrue(p.getQueue().isEmpty()); |
737 |
> |
assertEquals(count - poolSize, queuedTasks.size()); |
738 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
739 |
> |
assertTrue(p.isTerminated()); |
740 |
> |
assertEquals(poolSize, ran.get()); |
741 |
> |
} |
742 |
> |
|
743 |
> |
/** |
744 |
> |
* shutdownNow returns a list containing tasks that were not run, |
745 |
> |
* and those tasks are drained from the queue |
746 |
> |
*/ |
747 |
> |
public void testShutdownNow_delayedTasks() throws InterruptedException { |
748 |
|
CustomExecutor p = new CustomExecutor(1); |
749 |
< |
for (int i = 0; i < 5; i++) |
750 |
< |
p.schedule(new SmallPossiblyInterruptedRunnable(), |
751 |
< |
LONG_DELAY_MS, MILLISECONDS); |
749 |
> |
List<ScheduledFuture> tasks = new ArrayList<>(); |
750 |
> |
for (int i = 0; i < 3; i++) { |
751 |
> |
Runnable r = new NoOpRunnable(); |
752 |
> |
tasks.add(p.schedule(r, 9, SECONDS)); |
753 |
> |
tasks.add(p.scheduleAtFixedRate(r, 9, 9, SECONDS)); |
754 |
> |
tasks.add(p.scheduleWithFixedDelay(r, 9, 9, SECONDS)); |
755 |
> |
} |
756 |
> |
if (testImplementationDetails) |
757 |
> |
assertEquals(new HashSet(tasks), new HashSet(p.getQueue())); |
758 |
> |
final List<Runnable> queuedTasks; |
759 |
|
try { |
760 |
< |
List<Runnable> l = p.shutdownNow(); |
716 |
< |
assertTrue(p.isShutdown()); |
717 |
< |
assertEquals(5, l.size()); |
760 |
> |
queuedTasks = p.shutdownNow(); |
761 |
|
} catch (SecurityException ok) { |
762 |
< |
// Allowed in case test doesn't have privs |
720 |
< |
} finally { |
721 |
< |
joinPool(p); |
762 |
> |
return; // Allowed in case test doesn't have privs |
763 |
|
} |
764 |
+ |
assertTrue(p.isShutdown()); |
765 |
+ |
assertTrue(p.getQueue().isEmpty()); |
766 |
+ |
if (testImplementationDetails) |
767 |
+ |
assertEquals(new HashSet(tasks), new HashSet(queuedTasks)); |
768 |
+ |
assertEquals(tasks.size(), queuedTasks.size()); |
769 |
+ |
for (ScheduledFuture task : tasks) { |
770 |
+ |
assertFalse(((CustomTask)task).ran); |
771 |
+ |
assertFalse(task.isDone()); |
772 |
+ |
assertFalse(task.isCancelled()); |
773 |
+ |
} |
774 |
+ |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
775 |
+ |
assertTrue(p.isTerminated()); |
776 |
|
} |
777 |
|
|
778 |
|
/** |
1293 |
|
public void testTimedInvokeAll6() throws Exception { |
1294 |
|
ExecutorService e = new CustomExecutor(2); |
1295 |
|
try { |
1296 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1297 |
< |
l.add(new StringTask()); |
1298 |
< |
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
1299 |
< |
l.add(new StringTask()); |
1300 |
< |
List<Future<String>> futures = |
1301 |
< |
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1302 |
< |
assertEquals(l.size(), futures.size()); |
1303 |
< |
for (Future future : futures) |
1304 |
< |
assertTrue(future.isDone()); |
1305 |
< |
assertFalse(futures.get(0).isCancelled()); |
1306 |
< |
assertTrue(futures.get(1).isCancelled()); |
1296 |
> |
for (long timeout = timeoutMillis();;) { |
1297 |
> |
List<Callable<String>> tasks = new ArrayList<>(); |
1298 |
> |
tasks.add(new StringTask("0")); |
1299 |
> |
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1300 |
> |
tasks.add(new StringTask("2")); |
1301 |
> |
long startTime = System.nanoTime(); |
1302 |
> |
List<Future<String>> futures = |
1303 |
> |
e.invokeAll(tasks, timeout, MILLISECONDS); |
1304 |
> |
assertEquals(tasks.size(), futures.size()); |
1305 |
> |
assertTrue(millisElapsedSince(startTime) >= timeout); |
1306 |
> |
for (Future future : futures) |
1307 |
> |
assertTrue(future.isDone()); |
1308 |
> |
assertTrue(futures.get(1).isCancelled()); |
1309 |
> |
try { |
1310 |
> |
assertEquals("0", futures.get(0).get()); |
1311 |
> |
assertEquals("2", futures.get(2).get()); |
1312 |
> |
break; |
1313 |
> |
} catch (CancellationException retryWithLongerTimeout) { |
1314 |
> |
timeout *= 2; |
1315 |
> |
if (timeout >= LONG_DELAY_MS / 2) |
1316 |
> |
fail("expected exactly one task to be cancelled"); |
1317 |
> |
} |
1318 |
> |
} |
1319 |
|
} finally { |
1320 |
|
joinPool(e); |
1321 |
|
} |