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

Comparing jsr166/src/test/tck/ScheduledExecutorTest.java (file contents):
Revision 1.48 by jsr166, Wed Sep 25 06:59:34 2013 UTC vs.
Revision 1.52 by jsr166, Mon Sep 14 03:27:11 2015 UTC

# Line 6 | Line 6
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 +
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);
# Line 146 | Line 162 | public class ScheduledExecutorTest exten
162                  long startTime = System.nanoTime();
163                  int cycles = 10;
164                  final CountDownLatch done = new CountDownLatch(cycles);
165 <                CheckedRunnable task = new CheckedRunnable() {
165 >                Runnable task = new CheckedRunnable() {
166                      public void realRun() { done.countDown(); }};
167                  ScheduledFuture h =
168                      p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS);
# Line 174 | Line 190 | public class ScheduledExecutorTest exten
190                  long startTime = System.nanoTime();
191                  int cycles = 10;
192                  final CountDownLatch done = new CountDownLatch(cycles);
193 <                CheckedRunnable task = new CheckedRunnable() {
193 >                Runnable task = new CheckedRunnable() {
194                      public void realRun() { done.countDown(); }};
195                  ScheduledFuture h =
196                      p.scheduleWithFixedDelay(task, 0, delay, MILLISECONDS);
# Line 1171 | Line 1187 | public class ScheduledExecutorTest exten
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(l.size(), futures.size());
1197 <            for (Future future : futures)
1198 <                assertTrue(future.isDone());
1199 <            assertFalse(futures.get(0).isCancelled());
1200 <            assertTrue(futures.get(1).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          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines