summaryrefslogtreecommitdiffstats
path: root/jsr166-tests/src/test/java/jsr166/ScheduledExecutorTest.java
diff options
context:
space:
mode:
authorNarayan Kamath <narayan@google.com>2015-04-28 11:40:00 +0100
committerNarayan Kamath <narayan@google.com>2015-04-30 12:00:08 +0100
commit8e9a0e92906742b17eb08d7fb83cca91965f9b8e (patch)
tree790a5161ed9c8648a8dacc94c36b5db53f8da4eb /jsr166-tests/src/test/java/jsr166/ScheduledExecutorTest.java
parentedf43d27e240d82106f39ae91404963c23987234 (diff)
downloadlibcore-8e9a0e92906742b17eb08d7fb83cca91965f9b8e.zip
libcore-8e9a0e92906742b17eb08d7fb83cca91965f9b8e.tar.gz
libcore-8e9a0e92906742b17eb08d7fb83cca91965f9b8e.tar.bz2
Update JSR166 tck tests.
The following tests have been omitted because they are unsupported : - Atomic8Test.java - CompletableFutureTest.java - ConcurrentHashMap8Test.java - DoubleAccumulatorTest.java - DoubleAdderTest.java - ForkJoinPool8Test.java - ForkJoinTask8Test.java - LongAccumulatorTest.java - LongAdderTest.java - SplittableRandomTest.java - StampedLockTest.java - ThreadLocalRandom8Test.java - ThreadPoolExecutor9Test.java A package declaration has been added to all files (package jsr166;) and the base-class JSR166Test has been changed not to rely on features that aren't available on android / junit in the android environment. We also avoid using junit4 style TestSuite declarations. While the CTS test runner handles them properly usually, it trips over itself when it encounters a failure and tries to run each test in an invidual process and fails each test for no good reason (needs investigation on the CTS side of things) bug: 20628776 (cherry picked from commit 5da8b2b3cac51f0f592a5e1941bd84eade9648cd) Change-Id: If35be0881ad8da4c604ce6683149b15c1a85f289
Diffstat (limited to 'jsr166-tests/src/test/java/jsr166/ScheduledExecutorTest.java')
-rw-r--r--jsr166-tests/src/test/java/jsr166/ScheduledExecutorTest.java92
1 files changed, 69 insertions, 23 deletions
diff --git a/jsr166-tests/src/test/java/jsr166/ScheduledExecutorTest.java b/jsr166-tests/src/test/java/jsr166/ScheduledExecutorTest.java
index 4eea2c9..a2e83d0 100644
--- a/jsr166-tests/src/test/java/jsr166/ScheduledExecutorTest.java
+++ b/jsr166-tests/src/test/java/jsr166/ScheduledExecutorTest.java
@@ -8,13 +8,37 @@
package jsr166;
-import junit.framework.*;
-import java.util.*;
-import java.util.concurrent.*;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.Callable;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Future;
+import java.util.concurrent.RejectedExecutionException;
+import java.util.concurrent.ScheduledFuture;
+import java.util.concurrent.ScheduledThreadPoolExecutor;
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.atomic.AtomicInteger;
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
public class ScheduledExecutorTest extends JSR166TestCase {
+ // android-note: Removed because the CTS runner does a bad job of
+ // retrying tests that have suite() declarations.
+ //
+ // public static void main(String[] args) {
+ // main(suite(), args);
+ // }
+ // public static Test suite() {
+ // return new TestSuite(...);
+ // }
/**
* execute successfully executes a runnable
@@ -137,17 +161,27 @@ public class ScheduledExecutorTest extends JSR166TestCase {
*/
public void testFixedRateSequence() throws InterruptedException {
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
- RunnableCounter counter = new RunnableCounter();
- ScheduledFuture h =
- p.scheduleAtFixedRate(counter, 0, 1, MILLISECONDS);
- delay(SMALL_DELAY_MS);
- h.cancel(true);
- int c = counter.count.get();
- // By time scaling conventions, we must have at least
- // an execution per SHORT delay, but no more than one SHORT more
- assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS);
- assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS);
- joinPool(p);
+ try {
+ for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) {
+ long startTime = System.nanoTime();
+ int cycles = 10;
+ final CountDownLatch done = new CountDownLatch(cycles);
+ Runnable task = new CheckedRunnable() {
+ public void realRun() { done.countDown(); }};
+ ScheduledFuture h =
+ p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS);
+ done.await();
+ h.cancel(true);
+ double normalizedTime =
+ (double) millisElapsedSince(startTime) / delay;
+ if (normalizedTime >= cycles - 1 &&
+ normalizedTime <= cycles)
+ return;
+ }
+ throw new AssertionError("unexpected execution rate");
+ } finally {
+ joinPool(p);
+ }
}
/**
@@ -155,15 +189,27 @@ public class ScheduledExecutorTest extends JSR166TestCase {
*/
public void testFixedDelaySequence() throws InterruptedException {
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
- RunnableCounter counter = new RunnableCounter();
- ScheduledFuture h =
- p.scheduleWithFixedDelay(counter, 0, 1, MILLISECONDS);
- delay(SMALL_DELAY_MS);
- h.cancel(true);
- int c = counter.count.get();
- assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS);
- assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS);
- joinPool(p);
+ try {
+ for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) {
+ long startTime = System.nanoTime();
+ int cycles = 10;
+ final CountDownLatch done = new CountDownLatch(cycles);
+ Runnable task = new CheckedRunnable() {
+ public void realRun() { done.countDown(); }};
+ ScheduledFuture h =
+ p.scheduleWithFixedDelay(task, 0, delay, MILLISECONDS);
+ done.await();
+ h.cancel(true);
+ double normalizedTime =
+ (double) millisElapsedSince(startTime) / delay;
+ if (normalizedTime >= cycles - 1 &&
+ normalizedTime <= cycles)
+ return;
+ }
+ throw new AssertionError("unexpected execution rate");
+ } finally {
+ joinPool(p);
+ }
}
/**