4 |
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
– |
import junit.framework.*; |
8 |
– |
import java.util.*; |
9 |
– |
import java.util.concurrent.*; |
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; |
19 |
+ |
import java.util.concurrent.Executors; |
20 |
+ |
import java.util.concurrent.ExecutorService; |
21 |
+ |
import java.util.concurrent.Future; |
22 |
+ |
import java.util.concurrent.RejectedExecutionException; |
23 |
+ |
import java.util.concurrent.RejectedExecutionHandler; |
24 |
+ |
import java.util.concurrent.RunnableScheduledFuture; |
25 |
+ |
import java.util.concurrent.ScheduledFuture; |
26 |
+ |
import java.util.concurrent.ScheduledThreadPoolExecutor; |
27 |
+ |
import java.util.concurrent.ThreadFactory; |
28 |
+ |
import java.util.concurrent.ThreadPoolExecutor; |
29 |
+ |
import java.util.concurrent.TimeoutException; |
30 |
+ |
import java.util.concurrent.TimeUnit; |
31 |
|
import java.util.concurrent.atomic.AtomicInteger; |
32 |
|
|
33 |
+ |
import junit.framework.Test; |
34 |
+ |
import junit.framework.TestSuite; |
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); |
59 |
|
} |
60 |
|
public boolean isCancelled() { return task.isCancelled(); } |
61 |
|
public boolean isDone() { return task.isDone(); } |
62 |
< |
public V get() throws InterruptedException, ExecutionException { |
62 |
> |
public V get() throws InterruptedException, ExecutionException { |
63 |
|
V v = task.get(); |
64 |
|
assertTrue(ran); |
65 |
|
return v; |
66 |
|
} |
67 |
< |
public V get(long time, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { |
67 |
> |
public V get(long time, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { |
68 |
|
V v = task.get(time, unit); |
69 |
|
assertTrue(ran); |
70 |
|
return v; |
216 |
|
*/ |
217 |
|
public void testFixedRateSequence() throws InterruptedException { |
218 |
|
CustomExecutor p = new CustomExecutor(1); |
219 |
< |
RunnableCounter counter = new RunnableCounter(); |
220 |
< |
ScheduledFuture h = |
221 |
< |
p.scheduleAtFixedRate(counter, 0, 1, MILLISECONDS); |
222 |
< |
delay(SMALL_DELAY_MS); |
223 |
< |
h.cancel(true); |
224 |
< |
int c = counter.count.get(); |
225 |
< |
// By time scaling conventions, we must have at least |
226 |
< |
// an execution per SHORT delay, but no more than one SHORT more |
227 |
< |
assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS); |
228 |
< |
assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS); |
229 |
< |
joinPool(p); |
219 |
> |
try { |
220 |
> |
for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { |
221 |
> |
long startTime = System.nanoTime(); |
222 |
> |
int cycles = 10; |
223 |
> |
final CountDownLatch done = new CountDownLatch(cycles); |
224 |
> |
Runnable task = new CheckedRunnable() { |
225 |
> |
public void realRun() { done.countDown(); }}; |
226 |
> |
ScheduledFuture h = |
227 |
> |
p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS); |
228 |
> |
done.await(); |
229 |
> |
h.cancel(true); |
230 |
> |
double normalizedTime = |
231 |
> |
(double) millisElapsedSince(startTime) / delay; |
232 |
> |
if (normalizedTime >= cycles - 1 && |
233 |
> |
normalizedTime <= cycles) |
234 |
> |
return; |
235 |
> |
} |
236 |
> |
throw new AssertionError("unexpected execution rate"); |
237 |
> |
} finally { |
238 |
> |
joinPool(p); |
239 |
> |
} |
240 |
|
} |
241 |
|
|
242 |
|
/** |
244 |
|
*/ |
245 |
|
public void testFixedDelaySequence() throws InterruptedException { |
246 |
|
CustomExecutor p = new CustomExecutor(1); |
247 |
< |
RunnableCounter counter = new RunnableCounter(); |
248 |
< |
ScheduledFuture h = |
249 |
< |
p.scheduleWithFixedDelay(counter, 0, 1, MILLISECONDS); |
250 |
< |
delay(SMALL_DELAY_MS); |
251 |
< |
h.cancel(true); |
252 |
< |
int c = counter.count.get(); |
253 |
< |
assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS); |
254 |
< |
assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS); |
255 |
< |
joinPool(p); |
247 |
> |
try { |
248 |
> |
for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { |
249 |
> |
long startTime = System.nanoTime(); |
250 |
> |
int cycles = 10; |
251 |
> |
final CountDownLatch done = new CountDownLatch(cycles); |
252 |
> |
Runnable task = new CheckedRunnable() { |
253 |
> |
public void realRun() { done.countDown(); }}; |
254 |
> |
ScheduledFuture h = |
255 |
> |
p.scheduleWithFixedDelay(task, 0, delay, MILLISECONDS); |
256 |
> |
done.await(); |
257 |
> |
h.cancel(true); |
258 |
> |
double normalizedTime = |
259 |
> |
(double) millisElapsedSince(startTime) / delay; |
260 |
> |
if (normalizedTime >= cycles - 1 && |
261 |
> |
normalizedTime <= cycles) |
262 |
> |
return; |
263 |
> |
} |
264 |
> |
throw new AssertionError("unexpected execution rate"); |
265 |
> |
} finally { |
266 |
> |
joinPool(p); |
267 |
> |
} |
268 |
|
} |
269 |
|
|
270 |
|
/** |
457 |
|
done.await(); |
458 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
459 |
|
}}); |
460 |
< |
assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
460 |
> |
assertTrue(threadsStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
461 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
462 |
|
} finally { |
463 |
|
done.countDown(); |
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() { |
714 |
< |
CustomExecutor p = new CustomExecutor(1); |
715 |
< |
for (int i = 0; i < 5; i++) |
716 |
< |
p.schedule(new SmallPossiblyInterruptedRunnable(), |
717 |
< |
LONG_DELAY_MS, MILLISECONDS); |
718 |
< |
try { |
719 |
< |
List<Runnable> l = p.shutdownNow(); |
720 |
< |
assertTrue(p.isShutdown()); |
721 |
< |
assertEquals(5, l.size()); |
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 |
> |
Runnable 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 |
< |
// Allowed in case test doesn't have privs |
678 |
< |
} finally { |
679 |
< |
joinPool(p); |
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 |
< |
* In default setting, shutdown cancels periodic but not delayed |
748 |
< |
* tasks at shutdown |
747 |
> |
* shutdownNow returns a list containing tasks that were not run, |
748 |
> |
* and those tasks are drained from the queue |
749 |
|
*/ |
750 |
< |
public void testShutdown1() throws InterruptedException { |
750 |
> |
public void testShutdownNow_delayedTasks() throws InterruptedException { |
751 |
|
CustomExecutor p = new CustomExecutor(1); |
752 |
< |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
753 |
< |
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
754 |
< |
|
755 |
< |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
756 |
< |
for (int i = 0; i < tasks.length; i++) |
757 |
< |
tasks[i] = p.schedule(new NoOpRunnable(), |
758 |
< |
SHORT_DELAY_MS, MILLISECONDS); |
759 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
760 |
< |
BlockingQueue<Runnable> q = p.getQueue(); |
761 |
< |
for (ScheduledFuture task : tasks) { |
762 |
< |
assertFalse(task.isDone()); |
763 |
< |
assertFalse(task.isCancelled()); |
764 |
< |
assertTrue(q.contains(task)); |
752 |
> |
List<ScheduledFuture> tasks = new ArrayList<>(); |
753 |
> |
for (int i = 0; i < 3; i++) { |
754 |
> |
Runnable r = new NoOpRunnable(); |
755 |
> |
tasks.add(p.schedule(r, 9, SECONDS)); |
756 |
> |
tasks.add(p.scheduleAtFixedRate(r, 9, 9, SECONDS)); |
757 |
> |
tasks.add(p.scheduleWithFixedDelay(r, 9, 9, SECONDS)); |
758 |
> |
} |
759 |
> |
if (testImplementationDetails) |
760 |
> |
assertEquals(new HashSet(tasks), new HashSet(p.getQueue())); |
761 |
> |
final List<Runnable> queuedTasks; |
762 |
> |
try { |
763 |
> |
queuedTasks = p.shutdownNow(); |
764 |
> |
} catch (SecurityException ok) { |
765 |
> |
return; // Allowed in case test doesn't have privs |
766 |
|
} |
767 |
|
assertTrue(p.isShutdown()); |
768 |
< |
assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS)); |
769 |
< |
assertTrue(p.isTerminated()); |
768 |
> |
assertTrue(p.getQueue().isEmpty()); |
769 |
> |
if (testImplementationDetails) |
770 |
> |
assertEquals(new HashSet(tasks), new HashSet(queuedTasks)); |
771 |
> |
assertEquals(tasks.size(), queuedTasks.size()); |
772 |
|
for (ScheduledFuture task : tasks) { |
773 |
< |
assertTrue(task.isDone()); |
773 |
> |
assertFalse(((CustomTask)task).ran); |
774 |
> |
assertFalse(task.isDone()); |
775 |
|
assertFalse(task.isCancelled()); |
776 |
|
} |
777 |
< |
} |
711 |
< |
|
712 |
< |
/** |
713 |
< |
* If setExecuteExistingDelayedTasksAfterShutdownPolicy is false, |
714 |
< |
* delayed tasks are cancelled at shutdown |
715 |
< |
*/ |
716 |
< |
public void testShutdown2() throws InterruptedException { |
717 |
< |
CustomExecutor p = new CustomExecutor(1); |
718 |
< |
p.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); |
719 |
< |
assertFalse(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
720 |
< |
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
721 |
< |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
722 |
< |
for (int i = 0; i < tasks.length; i++) |
723 |
< |
tasks[i] = p.schedule(new NoOpRunnable(), |
724 |
< |
SHORT_DELAY_MS, MILLISECONDS); |
725 |
< |
BlockingQueue q = p.getQueue(); |
726 |
< |
assertEquals(tasks.length, q.size()); |
727 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
728 |
< |
assertTrue(p.isShutdown()); |
729 |
< |
assertTrue(q.isEmpty()); |
730 |
< |
assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS)); |
777 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
778 |
|
assertTrue(p.isTerminated()); |
732 |
– |
for (ScheduledFuture task : tasks) { |
733 |
– |
assertTrue(task.isDone()); |
734 |
– |
assertTrue(task.isCancelled()); |
735 |
– |
} |
779 |
|
} |
780 |
|
|
781 |
|
/** |
782 |
< |
* If setContinueExistingPeriodicTasksAfterShutdownPolicy is set false, |
783 |
< |
* periodic tasks are cancelled at shutdown |
784 |
< |
*/ |
785 |
< |
public void testShutdown3() throws InterruptedException { |
786 |
< |
CustomExecutor p = new CustomExecutor(1); |
787 |
< |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
788 |
< |
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
789 |
< |
p.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); |
790 |
< |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
791 |
< |
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
792 |
< |
long initialDelay = LONG_DELAY_MS; |
793 |
< |
ScheduledFuture task = |
794 |
< |
p.scheduleAtFixedRate(new NoOpRunnable(), initialDelay, |
795 |
< |
5, MILLISECONDS); |
782 |
> |
* By default, periodic tasks are cancelled at shutdown. |
783 |
> |
* By default, delayed tasks keep running after shutdown. |
784 |
> |
* Check that changing the default values work: |
785 |
> |
* - setExecuteExistingDelayedTasksAfterShutdownPolicy |
786 |
> |
* - setContinueExistingPeriodicTasksAfterShutdownPolicy |
787 |
> |
*/ |
788 |
> |
public void testShutdown_cancellation() throws Exception { |
789 |
> |
Boolean[] allBooleans = { null, Boolean.FALSE, Boolean.TRUE }; |
790 |
> |
for (Boolean policy : allBooleans) |
791 |
> |
{ |
792 |
> |
final int poolSize = 2; |
793 |
> |
final CustomExecutor p = new CustomExecutor(poolSize); |
794 |
> |
final boolean effectiveDelayedPolicy = (policy != Boolean.FALSE); |
795 |
> |
final boolean effectivePeriodicPolicy = (policy == Boolean.TRUE); |
796 |
> |
final boolean effectiveRemovePolicy = (policy == Boolean.TRUE); |
797 |
> |
if (policy != null) { |
798 |
> |
p.setExecuteExistingDelayedTasksAfterShutdownPolicy(policy); |
799 |
> |
p.setContinueExistingPeriodicTasksAfterShutdownPolicy(policy); |
800 |
> |
p.setRemoveOnCancelPolicy(policy); |
801 |
> |
} |
802 |
> |
assertEquals(effectiveDelayedPolicy, |
803 |
> |
p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
804 |
> |
assertEquals(effectivePeriodicPolicy, |
805 |
> |
p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
806 |
> |
assertEquals(effectiveRemovePolicy, |
807 |
> |
p.getRemoveOnCancelPolicy()); |
808 |
> |
// Strategy: Wedge the pool with poolSize "blocker" threads |
809 |
> |
final AtomicInteger ran = new AtomicInteger(0); |
810 |
> |
final CountDownLatch poolBlocked = new CountDownLatch(poolSize); |
811 |
> |
final CountDownLatch unblock = new CountDownLatch(1); |
812 |
> |
final CountDownLatch periodicLatch1 = new CountDownLatch(2); |
813 |
> |
final CountDownLatch periodicLatch2 = new CountDownLatch(2); |
814 |
> |
Runnable task = new CheckedRunnable() { public void realRun() |
815 |
> |
throws InterruptedException { |
816 |
> |
poolBlocked.countDown(); |
817 |
> |
assertTrue(unblock.await(LONG_DELAY_MS, MILLISECONDS)); |
818 |
> |
ran.getAndIncrement(); |
819 |
> |
}}; |
820 |
> |
List<Future<?>> blockers = new ArrayList<>(); |
821 |
> |
List<Future<?>> periodics = new ArrayList<>(); |
822 |
> |
List<Future<?>> delayeds = new ArrayList<>(); |
823 |
> |
for (int i = 0; i < poolSize; i++) |
824 |
> |
blockers.add(p.submit(task)); |
825 |
> |
assertTrue(poolBlocked.await(LONG_DELAY_MS, MILLISECONDS)); |
826 |
> |
|
827 |
> |
periodics.add(p.scheduleAtFixedRate(countDowner(periodicLatch1), |
828 |
> |
1, 1, MILLISECONDS)); |
829 |
> |
periodics.add(p.scheduleWithFixedDelay(countDowner(periodicLatch2), |
830 |
> |
1, 1, MILLISECONDS)); |
831 |
> |
delayeds.add(p.schedule(task, 1, MILLISECONDS)); |
832 |
> |
|
833 |
> |
assertTrue(p.getQueue().containsAll(periodics)); |
834 |
> |
assertTrue(p.getQueue().containsAll(delayeds)); |
835 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
836 |
|
assertTrue(p.isShutdown()); |
837 |
< |
assertTrue(p.getQueue().isEmpty()); |
838 |
< |
assertTrue(task.isDone()); |
839 |
< |
assertTrue(task.isCancelled()); |
840 |
< |
joinPool(p); |
841 |
< |
} |
842 |
< |
|
843 |
< |
/** |
844 |
< |
* if setContinueExistingPeriodicTasksAfterShutdownPolicy is true, |
845 |
< |
* periodic tasks are not cancelled at shutdown |
846 |
< |
*/ |
847 |
< |
public void testShutdown4() throws InterruptedException { |
848 |
< |
CustomExecutor p = new CustomExecutor(1); |
849 |
< |
final CountDownLatch counter = new CountDownLatch(2); |
850 |
< |
try { |
851 |
< |
p.setContinueExistingPeriodicTasksAfterShutdownPolicy(true); |
852 |
< |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
853 |
< |
assertTrue(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
854 |
< |
final Runnable r = new CheckedRunnable() { |
855 |
< |
public void realRun() { |
856 |
< |
counter.countDown(); |
857 |
< |
}}; |
858 |
< |
ScheduledFuture task = |
777 |
< |
p.scheduleAtFixedRate(r, 1, 1, MILLISECONDS); |
778 |
< |
assertFalse(task.isDone()); |
779 |
< |
assertFalse(task.isCancelled()); |
780 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
781 |
< |
assertFalse(task.isCancelled()); |
782 |
< |
assertFalse(p.isTerminated()); |
783 |
< |
assertTrue(p.isShutdown()); |
784 |
< |
assertTrue(counter.await(SMALL_DELAY_MS, MILLISECONDS)); |
785 |
< |
assertFalse(task.isCancelled()); |
786 |
< |
assertTrue(task.cancel(false)); |
787 |
< |
assertTrue(task.isDone()); |
788 |
< |
assertTrue(task.isCancelled()); |
789 |
< |
assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS)); |
790 |
< |
assertTrue(p.isTerminated()); |
837 |
> |
assertFalse(p.isTerminated()); |
838 |
> |
for (Future<?> periodic : periodics) { |
839 |
> |
assertTrue(effectivePeriodicPolicy ^ periodic.isCancelled()); |
840 |
> |
assertTrue(effectivePeriodicPolicy ^ periodic.isDone()); |
841 |
> |
} |
842 |
> |
for (Future<?> delayed : delayeds) { |
843 |
> |
assertTrue(effectiveDelayedPolicy ^ delayed.isCancelled()); |
844 |
> |
assertTrue(effectiveDelayedPolicy ^ delayed.isDone()); |
845 |
> |
} |
846 |
> |
if (testImplementationDetails) { |
847 |
> |
assertEquals(effectivePeriodicPolicy, |
848 |
> |
p.getQueue().containsAll(periodics)); |
849 |
> |
assertEquals(effectiveDelayedPolicy, |
850 |
> |
p.getQueue().containsAll(delayeds)); |
851 |
> |
} |
852 |
> |
// Release all pool threads |
853 |
> |
unblock.countDown(); |
854 |
> |
|
855 |
> |
for (Future<?> delayed : delayeds) { |
856 |
> |
if (effectiveDelayedPolicy) { |
857 |
> |
assertNull(delayed.get()); |
858 |
> |
} |
859 |
|
} |
860 |
< |
finally { |
861 |
< |
joinPool(p); |
860 |
> |
if (effectivePeriodicPolicy) { |
861 |
> |
assertTrue(periodicLatch1.await(LONG_DELAY_MS, MILLISECONDS)); |
862 |
> |
assertTrue(periodicLatch2.await(LONG_DELAY_MS, MILLISECONDS)); |
863 |
> |
for (Future<?> periodic : periodics) { |
864 |
> |
assertTrue(periodic.cancel(false)); |
865 |
> |
assertTrue(periodic.isCancelled()); |
866 |
> |
assertTrue(periodic.isDone()); |
867 |
> |
} |
868 |
|
} |
869 |
< |
} |
869 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
870 |
> |
assertTrue(p.isTerminated()); |
871 |
> |
assertEquals(2 + (effectiveDelayedPolicy ? 1 : 0), ran.get()); |
872 |
> |
}} |
873 |
|
|
874 |
|
/** |
875 |
|
* completed submit of callable returns result |
1260 |
|
l.add(new StringTask()); |
1261 |
|
l.add(new StringTask()); |
1262 |
|
List<Future<String>> futures = |
1263 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1263 |
> |
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
1264 |
|
assertEquals(2, futures.size()); |
1265 |
|
for (Future<String> future : futures) |
1266 |
|
assertSame(TEST_STRING, future.get()); |
1275 |
|
public void testTimedInvokeAll6() throws Exception { |
1276 |
|
ExecutorService e = new CustomExecutor(2); |
1277 |
|
try { |
1278 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1279 |
< |
l.add(new StringTask()); |
1280 |
< |
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
1281 |
< |
l.add(new StringTask()); |
1282 |
< |
List<Future<String>> futures = |
1283 |
< |
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1284 |
< |
assertEquals(l.size(), futures.size()); |
1285 |
< |
for (Future future : futures) |
1286 |
< |
assertTrue(future.isDone()); |
1287 |
< |
assertFalse(futures.get(0).isCancelled()); |
1288 |
< |
assertTrue(futures.get(1).isCancelled()); |
1278 |
> |
for (long timeout = timeoutMillis();;) { |
1279 |
> |
List<Callable<String>> tasks = new ArrayList<>(); |
1280 |
> |
tasks.add(new StringTask("0")); |
1281 |
> |
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1282 |
> |
tasks.add(new StringTask("2")); |
1283 |
> |
long startTime = System.nanoTime(); |
1284 |
> |
List<Future<String>> futures = |
1285 |
> |
e.invokeAll(tasks, timeout, MILLISECONDS); |
1286 |
> |
assertEquals(tasks.size(), futures.size()); |
1287 |
> |
assertTrue(millisElapsedSince(startTime) >= timeout); |
1288 |
> |
for (Future future : futures) |
1289 |
> |
assertTrue(future.isDone()); |
1290 |
> |
assertTrue(futures.get(1).isCancelled()); |
1291 |
> |
try { |
1292 |
> |
assertEquals("0", futures.get(0).get()); |
1293 |
> |
assertEquals("2", futures.get(2).get()); |
1294 |
> |
break; |
1295 |
> |
} catch (CancellationException retryWithLongerTimeout) { |
1296 |
> |
timeout *= 2; |
1297 |
> |
if (timeout >= LONG_DELAY_MS / 2) |
1298 |
> |
fail("expected exactly one task to be cancelled"); |
1299 |
> |
} |
1300 |
> |
} |
1301 |
|
} finally { |
1302 |
|
joinPool(e); |
1303 |
|
} |