ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ScheduledExecutorSubclassTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ScheduledExecutorSubclassTest.java (file contents):
Revision 1.19 by jsr166, Tue Mar 15 19:47:07 2011 UTC vs.
Revision 1.41 by jsr166, Mon Sep 28 03:05:23 2015 UTC

# Line 4 | Line 4
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 java.util.concurrent.atomic.*;
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);
# Line 36 | Line 59 | public class ScheduledExecutorSubclassTe
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;
71          }
72      }
73  
51
74      public class CustomExecutor extends ScheduledThreadPoolExecutor {
75  
76          protected <V> RunnableScheduledFuture<V> decorateTask(Runnable r, RunnableScheduledFuture<V> task) {
# Line 58 | Line 80 | public class ScheduledExecutorSubclassTe
80          protected <V> RunnableScheduledFuture<V> decorateTask(Callable<V> c, RunnableScheduledFuture<V> task) {
81              return new CustomTask<V>(task);
82          }
83 <        CustomExecutor(int corePoolSize) { super(corePoolSize);}
83 >        CustomExecutor(int corePoolSize) { super(corePoolSize); }
84          CustomExecutor(int corePoolSize, RejectedExecutionHandler handler) {
85              super(corePoolSize, handler);
86          }
# Line 73 | Line 95 | public class ScheduledExecutorSubclassTe
95  
96      }
97  
76
98      /**
99       * execute successfully executes a runnable
100       */
# Line 92 | Line 113 | public class ScheduledExecutorSubclassTe
113          }
114      }
115  
95
116      /**
117       * delayed schedule of callable successfully executes after delay
118       */
119      public void testSchedule1() throws Exception {
120          CustomExecutor p = new CustomExecutor(1);
121 <        final long t0 = System.nanoTime();
102 <        final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L;
121 >        final long startTime = System.nanoTime();
122          final CountDownLatch done = new CountDownLatch(1);
123          try {
124              Callable task = new CheckedCallable<Boolean>() {
125                  public Boolean realCall() {
126                      done.countDown();
127 <                    assertTrue(System.nanoTime() - t0 >= timeoutNanos);
127 >                    assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
128                      return Boolean.TRUE;
129                  }};
130 <            Future f = p.schedule(task, SHORT_DELAY_MS, MILLISECONDS);
131 <            assertEquals(Boolean.TRUE, f.get());
132 <            assertTrue(System.nanoTime() - t0 >= timeoutNanos);
130 >            Future f = p.schedule(task, timeoutMillis(), MILLISECONDS);
131 >            assertSame(Boolean.TRUE, f.get());
132 >            assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
133              assertTrue(done.await(0L, MILLISECONDS));
134          } finally {
135              joinPool(p);
# Line 122 | Line 141 | public class ScheduledExecutorSubclassTe
141       */
142      public void testSchedule3() throws Exception {
143          CustomExecutor p = new CustomExecutor(1);
144 <        final long t0 = System.nanoTime();
126 <        final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L;
144 >        final long startTime = System.nanoTime();
145          final CountDownLatch done = new CountDownLatch(1);
146          try {
147              Runnable task = new CheckedRunnable() {
148                  public void realRun() {
149                      done.countDown();
150 <                    assertTrue(System.nanoTime() - t0 >= timeoutNanos);
150 >                    assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
151                  }};
152 <            Future f = p.schedule(task, SHORT_DELAY_MS, MILLISECONDS);
153 <            assertNull(f.get());
154 <            assertTrue(System.nanoTime() - t0 >= timeoutNanos);
155 <            assertTrue(done.await(0L, MILLISECONDS));
152 >            Future f = p.schedule(task, timeoutMillis(), MILLISECONDS);
153 >            await(done);
154 >            assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
155 >            assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
156          } finally {
157              joinPool(p);
158          }
# Line 145 | Line 163 | public class ScheduledExecutorSubclassTe
163       */
164      public void testSchedule4() throws InterruptedException {
165          CustomExecutor p = new CustomExecutor(1);
166 <        final long t0 = System.nanoTime();
149 <        final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L;
166 >        final long startTime = System.nanoTime();
167          final CountDownLatch done = new CountDownLatch(1);
168          try {
169              Runnable task = new CheckedRunnable() {
170                  public void realRun() {
171                      done.countDown();
172 <                    assertTrue(System.nanoTime() - t0 >= timeoutNanos);
172 >                    assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
173                  }};
174              ScheduledFuture f =
175 <                p.scheduleAtFixedRate(task, SHORT_DELAY_MS,
176 <                                      SHORT_DELAY_MS, MILLISECONDS);
177 <            assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS));
178 <            assertTrue(System.nanoTime() - t0 >= timeoutNanos);
175 >                p.scheduleAtFixedRate(task, timeoutMillis(),
176 >                                      LONG_DELAY_MS, MILLISECONDS);
177 >            await(done);
178 >            assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
179              f.cancel(true);
180          } finally {
181              joinPool(p);
# Line 170 | Line 187 | public class ScheduledExecutorSubclassTe
187       */
188      public void testSchedule5() throws InterruptedException {
189          CustomExecutor p = new CustomExecutor(1);
190 <        final long t0 = System.nanoTime();
174 <        final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L;
190 >        final long startTime = System.nanoTime();
191          final CountDownLatch done = new CountDownLatch(1);
192          try {
193              Runnable task = new CheckedRunnable() {
194                  public void realRun() {
195                      done.countDown();
196 <                    assertTrue(System.nanoTime() - t0 >= timeoutNanos);
196 >                    assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
197                  }};
198              ScheduledFuture f =
199 <                p.scheduleWithFixedDelay(task, SHORT_DELAY_MS,
200 <                                         SHORT_DELAY_MS, MILLISECONDS);
201 <            assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS));
202 <            assertTrue(System.nanoTime() - t0 >= timeoutNanos);
199 >                p.scheduleWithFixedDelay(task, timeoutMillis(),
200 >                                         LONG_DELAY_MS, MILLISECONDS);
201 >            await(done);
202 >            assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
203              f.cancel(true);
204          } finally {
205              joinPool(p);
# Line 200 | Line 216 | public class ScheduledExecutorSubclassTe
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 <        Thread.sleep(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      /**
# Line 218 | Line 244 | public class ScheduledExecutorSubclassTe
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 <        Thread.sleep(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  
232
270      /**
271       * execute(null) throws NPE
272       */
# Line 291 | Line 328 | public class ScheduledExecutorSubclassTe
328      /**
329       * schedule callable throws RejectedExecutionException if shutdown
330       */
331 <     public void testSchedule3_RejectedExecutionException() {
332 <         CustomExecutor se = new CustomExecutor(1);
333 <         try {
334 <             se.shutdown();
335 <             se.schedule(new NoOpCallable(),
336 <                         MEDIUM_DELAY_MS, MILLISECONDS);
337 <             shouldThrow();
338 <         } catch (RejectedExecutionException success) {
339 <         } catch (SecurityException ok) {
340 <         }
341 <         joinPool(se);
331 >    public void testSchedule3_RejectedExecutionException() {
332 >        CustomExecutor se = new CustomExecutor(1);
333 >        try {
334 >            se.shutdown();
335 >            se.schedule(new NoOpCallable(),
336 >                        MEDIUM_DELAY_MS, MILLISECONDS);
337 >            shouldThrow();
338 >        } catch (RejectedExecutionException success) {
339 >        } catch (SecurityException ok) {
340 >        }
341 >        joinPool(se);
342      }
343  
344      /**
# Line 378 | Line 415 | public class ScheduledExecutorSubclassTe
415                      threadProceed.await();
416                      threadDone.countDown();
417                  }});
418 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
418 >            await(threadStarted);
419              assertEquals(0, p.getCompletedTaskCount());
420              threadProceed.countDown();
421              threadDone.await();
422 <            Thread.sleep(SHORT_DELAY_MS);
423 <            assertEquals(1, p.getCompletedTaskCount());
422 >            long startTime = System.nanoTime();
423 >            while (p.getCompletedTaskCount() != 1) {
424 >                if (millisElapsedSince(startTime) > LONG_DELAY_MS)
425 >                    fail("timed out");
426 >                Thread.yield();
427 >            }
428          } finally {
429              joinPool(p);
430          }
# Line 510 | Line 551 | public class ScheduledExecutorSubclassTe
551      }
552  
553      /**
554 <     * isShutDown is false before shutdown, true after
554 >     * isShutdown is false before shutdown, true after
555       */
556      public void testIsShutdown() {
557          CustomExecutor p = new CustomExecutor(1);
# Line 523 | Line 564 | public class ScheduledExecutorSubclassTe
564          assertTrue(p.isShutdown());
565      }
566  
526
567      /**
568       * isTerminated is false before termination, true after
569       */
# Line 642 | Line 682 | public class ScheduledExecutorSubclassTe
682      public void testPurge() throws InterruptedException {
683          CustomExecutor p = new CustomExecutor(1);
684          ScheduledFuture[] tasks = new ScheduledFuture[5];
685 <        for (int i = 0; i < tasks.length; i++) {
686 <            tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS);
687 <        }
685 >        for (int i = 0; i < tasks.length; i++)
686 >            tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(),
687 >                                  LONG_DELAY_MS, MILLISECONDS);
688          try {
689              int max = tasks.length;
690              if (tasks[4].cancel(true)) --max;
691              if (tasks[3].cancel(true)) --max;
692              // There must eventually be an interference-free point at
693              // which purge will not fail. (At worst, when queue is empty.)
694 <            int k;
695 <            for (k = 0; k < SMALL_DELAY_MS; ++k) {
694 >            long startTime = System.nanoTime();
695 >            do {
696                  p.purge();
697                  long count = p.getTaskCount();
698 <                if (count >= 0 && count <= max)
699 <                    break;
700 <                Thread.sleep(1);
701 <            }
662 <            assertTrue(k < SMALL_DELAY_MS);
698 >                if (count == max)
699 >                    return;
700 >            } while (millisElapsedSince(startTime) < MEDIUM_DELAY_MS);
701 >            fail("Purge failed to remove cancelled tasks");
702          } finally {
703              for (ScheduledFuture task : tasks)
704                  task.cancel(true);
# Line 668 | Line 707 | public class ScheduledExecutorSubclassTe
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 >        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 >            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 >     * shutdownNow returns a list containing tasks that were not run,
748 >     * and those tasks are drained from the queue
749 >     */
750 >    public void testShutdownNow_delayedTasks() throws InterruptedException {
751          CustomExecutor p = new CustomExecutor(1);
752 <        for (int i = 0; i < 5; i++)
753 <            p.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS);
754 <        List l;
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 <            l = p.shutdownNow();
763 >            queuedTasks = p.shutdownNow();
764          } catch (SecurityException ok) {
765 <            return;
765 >            return; // Allowed in case test doesn't have privs
766          }
767          assertTrue(p.isShutdown());
768 <        assertTrue(l.size() > 0 && l.size() <= 5);
769 <        joinPool(p);
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 >            assertFalse(((CustomTask)task).ran);
774 >            assertFalse(task.isDone());
775 >            assertFalse(task.isCancelled());
776 >        }
777 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
778 >        assertTrue(p.isTerminated());
779      }
780  
781      /**
782       * In default setting, shutdown cancels periodic but not delayed
783       * tasks at shutdown
784       */
785 <    public void testShutDown1() throws InterruptedException {
785 >    public void testShutdown1() throws InterruptedException {
786          CustomExecutor p = new CustomExecutor(1);
787          assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
788          assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
# Line 714 | Line 807 | public class ScheduledExecutorSubclassTe
807          }
808      }
809  
717
810      /**
811       * If setExecuteExistingDelayedTasksAfterShutdownPolicy is false,
812       * delayed tasks are cancelled at shutdown
813       */
814 <    public void testShutDown2() throws InterruptedException {
814 >    public void testShutdown2() throws InterruptedException {
815          CustomExecutor p = new CustomExecutor(1);
816          p.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
817          assertFalse(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
# Line 741 | Line 833 | public class ScheduledExecutorSubclassTe
833          }
834      }
835  
744
836      /**
837       * If setContinueExistingPeriodicTasksAfterShutdownPolicy is set false,
838       * periodic tasks are cancelled at shutdown
839       */
840 <    public void testShutDown3() throws InterruptedException {
840 >    public void testShutdown3() throws InterruptedException {
841          CustomExecutor p = new CustomExecutor(1);
842          assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
843          assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
844          p.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
845          assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
846          assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
847 +        long initialDelay = LONG_DELAY_MS;
848          ScheduledFuture task =
849 <            p.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, MILLISECONDS);
849 >            p.scheduleAtFixedRate(new NoOpRunnable(), initialDelay,
850 >                                  5, MILLISECONDS);
851          try { p.shutdown(); } catch (SecurityException ok) { return; }
852          assertTrue(p.isShutdown());
760        BlockingQueue q = p.getQueue();
853          assertTrue(p.getQueue().isEmpty());
854          assertTrue(task.isDone());
855          assertTrue(task.isCancelled());
856 <        assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS));
765 <        assertTrue(p.isTerminated());
856 >        joinPool(p);
857      }
858  
859      /**
860       * if setContinueExistingPeriodicTasksAfterShutdownPolicy is true,
861       * periodic tasks are not cancelled at shutdown
862       */
863 <    public void testShutDown4() throws InterruptedException {
863 >    public void testShutdown4() throws InterruptedException {
864          CustomExecutor p = new CustomExecutor(1);
865          final CountDownLatch counter = new CountDownLatch(2);
866          try {
# Line 1205 | Line 1296 | public class ScheduledExecutorSubclassTe
1296      public void testTimedInvokeAll6() throws Exception {
1297          ExecutorService e = new CustomExecutor(2);
1298          try {
1299 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1300 <            l.add(new StringTask());
1301 <            l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1302 <            l.add(new StringTask());
1303 <            List<Future<String>> futures =
1304 <                e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1305 <            assertEquals(3, futures.size());
1306 <            Iterator<Future<String>> it = futures.iterator();
1307 <            Future<String> f1 = it.next();
1308 <            Future<String> f2 = it.next();
1309 <            Future<String> f3 = it.next();
1310 <            assertTrue(f1.isDone());
1311 <            assertTrue(f2.isDone());
1312 <            assertTrue(f3.isDone());
1313 <            assertFalse(f1.isCancelled());
1314 <            assertTrue(f2.isCancelled());
1299 >            for (long timeout = timeoutMillis();;) {
1300 >                List<Callable<String>> tasks = new ArrayList<>();
1301 >                tasks.add(new StringTask("0"));
1302 >                tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1303 >                tasks.add(new StringTask("2"));
1304 >                long startTime = System.nanoTime();
1305 >                List<Future<String>> futures =
1306 >                    e.invokeAll(tasks, timeout, MILLISECONDS);
1307 >                assertEquals(tasks.size(), futures.size());
1308 >                assertTrue(millisElapsedSince(startTime) >= timeout);
1309 >                for (Future future : futures)
1310 >                    assertTrue(future.isDone());
1311 >                assertTrue(futures.get(1).isCancelled());
1312 >                try {
1313 >                    assertEquals("0", futures.get(0).get());
1314 >                    assertEquals("2", futures.get(2).get());
1315 >                    break;
1316 >                } catch (CancellationException retryWithLongerTimeout) {
1317 >                    timeout *= 2;
1318 >                    if (timeout >= LONG_DELAY_MS / 2)
1319 >                        fail("expected exactly one task to be cancelled");
1320 >                }
1321 >            }
1322          } finally {
1323              joinPool(e);
1324          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines