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.44 by jsr166, Sat May 28 15:33:20 2011 UTC vs.
Revision 1.53 by jsr166, Sun Sep 27 18:50:50 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 < 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);
# Line 141 | Line 157 | public class ScheduledExecutorTest exten
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 <        delay(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      /**
# Line 159 | Line 185 | public class ScheduledExecutorTest exten
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 <        delay(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  
211      /**
# Line 320 | Line 358 | public class ScheduledExecutorTest exten
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();
# Line 613 | Line 651 | public class ScheduledExecutorTest exten
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 >     * and those tasks are drained from the queue
656       */
657      public void testShutdownNow() {
658          ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
# Line 623 | Line 662 | public class ScheduledExecutorTest exten
662          try {
663              List<Runnable> l = p.shutdownNow();
664              assertTrue(p.isShutdown());
665 +            assertTrue(p.getQueue().isEmpty());
666              assertEquals(5, l.size());
667          } catch (SecurityException ok) {
668              // Allowed in case test doesn't have privs
# Line 1149 | Line 1189 | public class ScheduledExecutorTest exten
1189      public void testTimedInvokeAll6() throws Exception {
1190          ExecutorService e = new ScheduledThreadPoolExecutor(2);
1191          try {
1192 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1193 <            l.add(new StringTask());
1194 <            l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1195 <            l.add(new StringTask());
1196 <            List<Future<String>> futures =
1197 <                e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1198 <            assertEquals(3, futures.size());
1199 <            Iterator<Future<String>> it = futures.iterator();
1200 <            Future<String> f1 = it.next();
1201 <            Future<String> f2 = it.next();
1202 <            Future<String> f3 = it.next();
1203 <            assertTrue(f1.isDone());
1204 <            assertTrue(f2.isDone());
1205 <            assertTrue(f3.isDone());
1206 <            assertFalse(f1.isCancelled());
1207 <            assertTrue(f2.isCancelled());
1192 >            for (long timeout = timeoutMillis();;) {
1193 >                List<Callable<String>> tasks = new ArrayList<>();
1194 >                tasks.add(new StringTask("0"));
1195 >                tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1196 >                tasks.add(new StringTask("2"));
1197 >                long startTime = System.nanoTime();
1198 >                List<Future<String>> futures =
1199 >                    e.invokeAll(tasks, timeout, MILLISECONDS);
1200 >                assertEquals(tasks.size(), futures.size());
1201 >                assertTrue(millisElapsedSince(startTime) >= timeout);
1202 >                for (Future future : futures)
1203 >                    assertTrue(future.isDone());
1204 >                assertTrue(futures.get(1).isCancelled());
1205 >                try {
1206 >                    assertEquals("0", futures.get(0).get());
1207 >                    assertEquals("2", futures.get(2).get());
1208 >                    break;
1209 >                } catch (CancellationException retryWithLongerTimeout) {
1210 >                    timeout *= 2;
1211 >                    if (timeout >= LONG_DELAY_MS / 2)
1212 >                        fail("expected exactly one task to be cancelled");
1213 >                }
1214 >            }
1215          } finally {
1216              joinPool(e);
1217          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines