1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
* Other contributors include Andrew Wright, Jeffrey Hayes, |
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 java.util.concurrent.atomic.*; |
10 |
> |
|
11 |
> |
import java.util.ArrayList; |
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.ExecutionException; |
18 |
> |
import java.util.concurrent.Executors; |
19 |
> |
import java.util.concurrent.ExecutorService; |
20 |
> |
import java.util.concurrent.Future; |
21 |
> |
import java.util.concurrent.RejectedExecutionException; |
22 |
> |
import java.util.concurrent.ScheduledFuture; |
23 |
> |
import java.util.concurrent.ScheduledThreadPoolExecutor; |
24 |
> |
import java.util.concurrent.ThreadFactory; |
25 |
> |
import java.util.concurrent.ThreadPoolExecutor; |
26 |
> |
import java.util.concurrent.atomic.AtomicInteger; |
27 |
> |
|
28 |
> |
import junit.framework.Test; |
29 |
> |
import junit.framework.TestSuite; |
30 |
|
|
31 |
|
public class ScheduledExecutorTest extends JSR166TestCase { |
32 |
|
public static void main(String[] args) { |
33 |
< |
junit.textui.TestRunner.run(suite()); |
33 |
> |
main(suite(), args); |
34 |
|
} |
35 |
|
public static Test suite() { |
36 |
|
return new TestSuite(ScheduledExecutorTest.class); |
37 |
|
} |
38 |
|
|
23 |
– |
|
39 |
|
/** |
40 |
|
* execute successfully executes a runnable |
41 |
|
*/ |
54 |
|
} |
55 |
|
} |
56 |
|
|
42 |
– |
|
57 |
|
/** |
58 |
|
* delayed schedule of callable successfully executes after delay |
59 |
|
*/ |
60 |
|
public void testSchedule1() throws Exception { |
61 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
62 |
< |
final long t0 = System.nanoTime(); |
49 |
< |
final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L; |
62 |
> |
final long startTime = System.nanoTime(); |
63 |
|
final CountDownLatch done = new CountDownLatch(1); |
64 |
|
try { |
65 |
|
Callable task = new CheckedCallable<Boolean>() { |
66 |
|
public Boolean realCall() { |
67 |
|
done.countDown(); |
68 |
< |
assertTrue(System.nanoTime() - t0 >= timeoutNanos); |
68 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
69 |
|
return Boolean.TRUE; |
70 |
|
}}; |
71 |
< |
Future f = p.schedule(task, SHORT_DELAY_MS, MILLISECONDS); |
72 |
< |
assertEquals(Boolean.TRUE, f.get()); |
73 |
< |
assertTrue(System.nanoTime() - t0 >= timeoutNanos); |
71 |
> |
Future f = p.schedule(task, timeoutMillis(), MILLISECONDS); |
72 |
> |
assertSame(Boolean.TRUE, f.get()); |
73 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
74 |
|
assertTrue(done.await(0L, MILLISECONDS)); |
75 |
|
} finally { |
76 |
|
joinPool(p); |
82 |
|
*/ |
83 |
|
public void testSchedule3() throws Exception { |
84 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
85 |
< |
final long t0 = System.nanoTime(); |
73 |
< |
final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L; |
85 |
> |
final long startTime = System.nanoTime(); |
86 |
|
final CountDownLatch done = new CountDownLatch(1); |
87 |
|
try { |
88 |
|
Runnable task = new CheckedRunnable() { |
89 |
|
public void realRun() { |
90 |
|
done.countDown(); |
91 |
< |
assertTrue(System.nanoTime() - t0 >= timeoutNanos); |
91 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
92 |
|
}}; |
93 |
< |
Future f = p.schedule(task, SHORT_DELAY_MS, MILLISECONDS); |
94 |
< |
assertNull(f.get()); |
95 |
< |
assertTrue(System.nanoTime() - t0 >= timeoutNanos); |
96 |
< |
assertTrue(done.await(0L, MILLISECONDS)); |
93 |
> |
Future f = p.schedule(task, timeoutMillis(), MILLISECONDS); |
94 |
> |
await(done); |
95 |
> |
assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); |
96 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
97 |
|
} finally { |
98 |
|
joinPool(p); |
99 |
|
} |
104 |
|
*/ |
105 |
|
public void testSchedule4() throws Exception { |
106 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
107 |
< |
final long t0 = System.nanoTime(); |
96 |
< |
final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L; |
107 |
> |
final long startTime = System.nanoTime(); |
108 |
|
final CountDownLatch done = new CountDownLatch(1); |
109 |
|
try { |
110 |
|
Runnable task = new CheckedRunnable() { |
111 |
|
public void realRun() { |
112 |
|
done.countDown(); |
113 |
< |
assertTrue(System.nanoTime() - t0 >= timeoutNanos); |
113 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
114 |
|
}}; |
115 |
|
ScheduledFuture f = |
116 |
< |
p.scheduleAtFixedRate(task, SHORT_DELAY_MS, |
117 |
< |
SHORT_DELAY_MS, MILLISECONDS); |
118 |
< |
assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS)); |
119 |
< |
assertTrue(System.nanoTime() - t0 >= timeoutNanos); |
116 |
> |
p.scheduleAtFixedRate(task, timeoutMillis(), |
117 |
> |
LONG_DELAY_MS, MILLISECONDS); |
118 |
> |
await(done); |
119 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
120 |
|
f.cancel(true); |
121 |
|
} finally { |
122 |
|
joinPool(p); |
126 |
|
/** |
127 |
|
* scheduleWithFixedDelay executes runnable after given initial delay |
128 |
|
*/ |
129 |
< |
public void testSchedule5() throws InterruptedException { |
129 |
> |
public void testSchedule5() throws Exception { |
130 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
131 |
< |
final long t0 = System.nanoTime(); |
121 |
< |
final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L; |
131 |
> |
final long startTime = System.nanoTime(); |
132 |
|
final CountDownLatch done = new CountDownLatch(1); |
133 |
|
try { |
134 |
|
Runnable task = new CheckedRunnable() { |
135 |
|
public void realRun() { |
136 |
|
done.countDown(); |
137 |
< |
assertTrue(System.nanoTime() - t0 >= timeoutNanos); |
137 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
138 |
|
}}; |
139 |
|
ScheduledFuture f = |
140 |
< |
p.scheduleWithFixedDelay(task, SHORT_DELAY_MS, |
141 |
< |
SHORT_DELAY_MS, MILLISECONDS); |
142 |
< |
assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS)); |
143 |
< |
assertTrue(System.nanoTime() - t0 >= timeoutNanos); |
140 |
> |
p.scheduleWithFixedDelay(task, timeoutMillis(), |
141 |
> |
LONG_DELAY_MS, MILLISECONDS); |
142 |
> |
await(done); |
143 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
144 |
|
f.cancel(true); |
145 |
|
} finally { |
146 |
|
joinPool(p); |
157 |
|
*/ |
158 |
|
public void testFixedRateSequence() throws InterruptedException { |
159 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
160 |
< |
RunnableCounter counter = new RunnableCounter(); |
161 |
< |
ScheduledFuture h = |
162 |
< |
p.scheduleAtFixedRate(counter, 0, 1, MILLISECONDS); |
163 |
< |
Thread.sleep(SMALL_DELAY_MS); |
164 |
< |
h.cancel(true); |
165 |
< |
int c = counter.count.get(); |
166 |
< |
// By time scaling conventions, we must have at least |
167 |
< |
// an execution per SHORT delay, but no more than one SHORT more |
168 |
< |
assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS); |
169 |
< |
assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS); |
170 |
< |
joinPool(p); |
160 |
> |
try { |
161 |
> |
for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { |
162 |
> |
long startTime = System.nanoTime(); |
163 |
> |
int cycles = 10; |
164 |
> |
final CountDownLatch done = new CountDownLatch(cycles); |
165 |
> |
Runnable task = new CheckedRunnable() { |
166 |
> |
public void realRun() { done.countDown(); }}; |
167 |
> |
ScheduledFuture h = |
168 |
> |
p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS); |
169 |
> |
done.await(); |
170 |
> |
h.cancel(true); |
171 |
> |
double normalizedTime = |
172 |
> |
(double) millisElapsedSince(startTime) / delay; |
173 |
> |
if (normalizedTime >= cycles - 1 && |
174 |
> |
normalizedTime <= cycles) |
175 |
> |
return; |
176 |
> |
} |
177 |
> |
throw new AssertionError("unexpected execution rate"); |
178 |
> |
} finally { |
179 |
> |
joinPool(p); |
180 |
> |
} |
181 |
|
} |
182 |
|
|
183 |
|
/** |
185 |
|
*/ |
186 |
|
public void testFixedDelaySequence() throws InterruptedException { |
187 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
188 |
< |
RunnableCounter counter = new RunnableCounter(); |
189 |
< |
ScheduledFuture h = |
190 |
< |
p.scheduleWithFixedDelay(counter, 0, 1, MILLISECONDS); |
191 |
< |
Thread.sleep(SMALL_DELAY_MS); |
192 |
< |
h.cancel(true); |
193 |
< |
int c = counter.count.get(); |
194 |
< |
assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS); |
195 |
< |
assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS); |
196 |
< |
joinPool(p); |
188 |
> |
try { |
189 |
> |
for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { |
190 |
> |
long startTime = System.nanoTime(); |
191 |
> |
int cycles = 10; |
192 |
> |
final CountDownLatch done = new CountDownLatch(cycles); |
193 |
> |
Runnable task = new CheckedRunnable() { |
194 |
> |
public void realRun() { done.countDown(); }}; |
195 |
> |
ScheduledFuture h = |
196 |
> |
p.scheduleWithFixedDelay(task, 0, delay, MILLISECONDS); |
197 |
> |
done.await(); |
198 |
> |
h.cancel(true); |
199 |
> |
double normalizedTime = |
200 |
> |
(double) millisElapsedSince(startTime) / delay; |
201 |
> |
if (normalizedTime >= cycles - 1 && |
202 |
> |
normalizedTime <= cycles) |
203 |
> |
return; |
204 |
> |
} |
205 |
> |
throw new AssertionError("unexpected execution rate"); |
206 |
> |
} finally { |
207 |
> |
joinPool(p); |
208 |
> |
} |
209 |
|
} |
210 |
|
|
179 |
– |
|
211 |
|
/** |
212 |
|
* execute(null) throws NPE |
213 |
|
*/ |
271 |
|
/** |
272 |
|
* schedule callable throws RejectedExecutionException if shutdown |
273 |
|
*/ |
274 |
< |
public void testSchedule3_RejectedExecutionException() throws InterruptedException { |
275 |
< |
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
276 |
< |
try { |
274 |
> |
public void testSchedule3_RejectedExecutionException() throws InterruptedException { |
275 |
> |
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
276 |
> |
try { |
277 |
|
se.shutdown(); |
278 |
|
se.schedule(new NoOpCallable(), |
279 |
|
MEDIUM_DELAY_MS, MILLISECONDS); |
281 |
|
} catch (RejectedExecutionException success) { |
282 |
|
} catch (SecurityException ok) { |
283 |
|
} |
284 |
< |
joinPool(se); |
284 |
> |
joinPool(se); |
285 |
|
} |
286 |
|
|
287 |
|
/** |
358 |
|
threadProceed.await(); |
359 |
|
threadDone.countDown(); |
360 |
|
}}); |
361 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
361 |
> |
await(threadStarted); |
362 |
|
assertEquals(0, p.getCompletedTaskCount()); |
363 |
|
threadProceed.countDown(); |
364 |
|
threadDone.await(); |
365 |
< |
Thread.sleep(SHORT_DELAY_MS); |
366 |
< |
assertEquals(1, p.getCompletedTaskCount()); |
365 |
> |
long startTime = System.nanoTime(); |
366 |
> |
while (p.getCompletedTaskCount() != 1) { |
367 |
> |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
368 |
> |
fail("timed out"); |
369 |
> |
Thread.yield(); |
370 |
> |
} |
371 |
|
} finally { |
372 |
|
joinPool(p); |
373 |
|
} |
494 |
|
} |
495 |
|
|
496 |
|
/** |
497 |
< |
* isShutDown is false before shutdown, true after |
497 |
> |
* isShutdown is false before shutdown, true after |
498 |
|
*/ |
499 |
|
public void testIsShutdown() { |
500 |
|
|
508 |
|
assertTrue(p.isShutdown()); |
509 |
|
} |
510 |
|
|
476 |
– |
|
511 |
|
/** |
512 |
|
* isTerminated is false before termination, true after |
513 |
|
*/ |
519 |
|
try { |
520 |
|
p.execute(new CheckedRunnable() { |
521 |
|
public void realRun() throws InterruptedException { |
488 |
– |
threadStarted.countDown(); |
522 |
|
assertFalse(p.isTerminated()); |
523 |
+ |
threadStarted.countDown(); |
524 |
|
done.await(); |
525 |
|
}}); |
526 |
|
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
527 |
+ |
assertFalse(p.isTerminating()); |
528 |
|
done.countDown(); |
529 |
|
} finally { |
530 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
621 |
|
} |
622 |
|
|
623 |
|
/** |
624 |
< |
* purge removes cancelled tasks from the queue |
624 |
> |
* purge eventually removes cancelled tasks from the queue |
625 |
|
*/ |
626 |
|
public void testPurge() throws InterruptedException { |
627 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
628 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
629 |
< |
for (int i = 0; i < tasks.length; i++) { |
630 |
< |
tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS); |
631 |
< |
} |
629 |
> |
for (int i = 0; i < tasks.length; i++) |
630 |
> |
tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(), |
631 |
> |
LONG_DELAY_MS, MILLISECONDS); |
632 |
|
try { |
633 |
|
int max = tasks.length; |
634 |
|
if (tasks[4].cancel(true)) --max; |
635 |
|
if (tasks[3].cancel(true)) --max; |
636 |
|
// There must eventually be an interference-free point at |
637 |
|
// which purge will not fail. (At worst, when queue is empty.) |
638 |
< |
int k; |
639 |
< |
for (k = 0; k < SMALL_DELAY_MS; ++k) { |
638 |
> |
long startTime = System.nanoTime(); |
639 |
> |
do { |
640 |
|
p.purge(); |
641 |
|
long count = p.getTaskCount(); |
642 |
< |
if (count >= 0 && count <= max) |
643 |
< |
break; |
644 |
< |
Thread.sleep(1); |
645 |
< |
} |
611 |
< |
assertTrue(k < SMALL_DELAY_MS); |
642 |
> |
if (count == max) |
643 |
> |
return; |
644 |
> |
} while (millisElapsedSince(startTime) < MEDIUM_DELAY_MS); |
645 |
> |
fail("Purge failed to remove cancelled tasks"); |
646 |
|
} finally { |
647 |
|
for (ScheduledFuture task : tasks) |
648 |
|
task.cancel(true); |
651 |
|
} |
652 |
|
|
653 |
|
/** |
654 |
< |
* shutDownNow returns a list containing tasks that were not run |
654 |
> |
* shutdownNow returns a list containing tasks that were not run |
655 |
|
*/ |
656 |
< |
public void testShutDownNow() throws InterruptedException { |
656 |
> |
public void testShutdownNow() { |
657 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
658 |
|
for (int i = 0; i < 5; i++) |
659 |
< |
p.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS); |
660 |
< |
List l; |
659 |
> |
p.schedule(new SmallPossiblyInterruptedRunnable(), |
660 |
> |
LONG_DELAY_MS, MILLISECONDS); |
661 |
|
try { |
662 |
< |
l = p.shutdownNow(); |
662 |
> |
List<Runnable> l = p.shutdownNow(); |
663 |
> |
assertTrue(p.isShutdown()); |
664 |
> |
assertEquals(5, l.size()); |
665 |
|
} catch (SecurityException ok) { |
666 |
< |
return; |
666 |
> |
// Allowed in case test doesn't have privs |
667 |
> |
} finally { |
668 |
> |
joinPool(p); |
669 |
|
} |
632 |
– |
assertTrue(p.isShutdown()); |
633 |
– |
assertTrue(l.size() > 0 && l.size() <= 5); |
634 |
– |
joinPool(p); |
670 |
|
} |
671 |
|
|
672 |
|
/** |
673 |
|
* In default setting, shutdown cancels periodic but not delayed |
674 |
|
* tasks at shutdown |
675 |
|
*/ |
676 |
< |
public void testShutDown1() throws InterruptedException { |
676 |
> |
public void testShutdown1() throws InterruptedException { |
677 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
678 |
|
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
679 |
|
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
698 |
|
} |
699 |
|
} |
700 |
|
|
666 |
– |
|
701 |
|
/** |
702 |
|
* If setExecuteExistingDelayedTasksAfterShutdownPolicy is false, |
703 |
|
* delayed tasks are cancelled at shutdown |
704 |
|
*/ |
705 |
< |
public void testShutDown2() throws InterruptedException { |
705 |
> |
public void testShutdown2() throws InterruptedException { |
706 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
707 |
|
p.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); |
708 |
|
assertFalse(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
728 |
|
* If setContinueExistingPeriodicTasksAfterShutdownPolicy is set false, |
729 |
|
* periodic tasks are cancelled at shutdown |
730 |
|
*/ |
731 |
< |
public void testShutDown3() throws InterruptedException { |
731 |
> |
public void testShutdown3() throws InterruptedException { |
732 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
733 |
|
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
734 |
|
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
735 |
|
p.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); |
736 |
|
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
737 |
|
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
738 |
+ |
long initialDelay = LONG_DELAY_MS; |
739 |
|
ScheduledFuture task = |
740 |
< |
p.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, MILLISECONDS); |
740 |
> |
p.scheduleAtFixedRate(new NoOpRunnable(), initialDelay, |
741 |
> |
5, MILLISECONDS); |
742 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
743 |
|
assertTrue(p.isShutdown()); |
708 |
– |
BlockingQueue q = p.getQueue(); |
744 |
|
assertTrue(p.getQueue().isEmpty()); |
745 |
|
assertTrue(task.isDone()); |
746 |
|
assertTrue(task.isCancelled()); |
747 |
< |
assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS)); |
713 |
< |
assertTrue(p.isTerminated()); |
747 |
> |
joinPool(p); |
748 |
|
} |
749 |
|
|
750 |
|
/** |
751 |
|
* if setContinueExistingPeriodicTasksAfterShutdownPolicy is true, |
752 |
|
* periodic tasks are not cancelled at shutdown |
753 |
|
*/ |
754 |
< |
public void testShutDown4() throws InterruptedException { |
754 |
> |
public void testShutdown4() throws InterruptedException { |
755 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
756 |
|
final CountDownLatch counter = new CountDownLatch(2); |
757 |
|
try { |
1187 |
|
public void testTimedInvokeAll6() throws Exception { |
1188 |
|
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1189 |
|
try { |
1190 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1191 |
< |
l.add(new StringTask()); |
1192 |
< |
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
1193 |
< |
l.add(new StringTask()); |
1194 |
< |
List<Future<String>> futures = |
1195 |
< |
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1196 |
< |
assertEquals(3, futures.size()); |
1197 |
< |
Iterator<Future<String>> it = futures.iterator(); |
1198 |
< |
Future<String> f1 = it.next(); |
1199 |
< |
Future<String> f2 = it.next(); |
1200 |
< |
Future<String> f3 = it.next(); |
1201 |
< |
assertTrue(f1.isDone()); |
1202 |
< |
assertTrue(f2.isDone()); |
1203 |
< |
assertTrue(f3.isDone()); |
1204 |
< |
assertFalse(f1.isCancelled()); |
1205 |
< |
assertTrue(f2.isCancelled()); |
1190 |
> |
for (long timeout = timeoutMillis();;) { |
1191 |
> |
List<Callable<String>> tasks = new ArrayList<>(); |
1192 |
> |
tasks.add(new StringTask("0")); |
1193 |
> |
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1194 |
> |
tasks.add(new StringTask("2")); |
1195 |
> |
long startTime = System.nanoTime(); |
1196 |
> |
List<Future<String>> futures = |
1197 |
> |
e.invokeAll(tasks, timeout, MILLISECONDS); |
1198 |
> |
assertEquals(tasks.size(), futures.size()); |
1199 |
> |
assertTrue(millisElapsedSince(startTime) >= timeout); |
1200 |
> |
for (Future future : futures) |
1201 |
> |
assertTrue(future.isDone()); |
1202 |
> |
assertTrue(futures.get(1).isCancelled()); |
1203 |
> |
try { |
1204 |
> |
assertEquals("0", futures.get(0).get()); |
1205 |
> |
assertEquals("2", futures.get(2).get()); |
1206 |
> |
break; |
1207 |
> |
} catch (CancellationException retryWithLongerTimeout) { |
1208 |
> |
timeout *= 2; |
1209 |
> |
if (timeout >= LONG_DELAY_MS / 2) |
1210 |
> |
fail("expected exactly one task to be cancelled"); |
1211 |
> |
} |
1212 |
> |
} |
1213 |
|
} finally { |
1214 |
|
joinPool(e); |
1215 |
|
} |
1216 |
|
} |
1217 |
|
|
1177 |
– |
|
1218 |
|
} |