JSR-166 update without java 1.9 method/classes
Second attempt, in frist one I've submitted some code from openJdk 1.9
that shouldn't be here, orignial change can be found at
5328e07d282bef36ac8b757bbee16a761415b2c4
Adapted from sources taken from CVS using:
cvs -d ':pserver:anonymous@gee.cs.oswego.edu/home/jsr166/jsr166' checkout -D "03/03/2016 10:00:00 GMT" jsr166
This time with hidden/removed "@since 9" methods and classes
Bug: 27426599
Change-Id: Ibd8d26e13cba091bfd983c73d005e4f8d8f5946d
diff --git a/jsr166-tests/src/test/java/jsr166/AbstractExecutorServiceTest.java b/jsr166-tests/src/test/java/jsr166/AbstractExecutorServiceTest.java
index 9e83de2..c293f13 100644
--- a/jsr166-tests/src/test/java/jsr166/AbstractExecutorServiceTest.java
+++ b/jsr166-tests/src/test/java/jsr166/AbstractExecutorServiceTest.java
@@ -18,6 +18,7 @@
import java.util.concurrent.AbstractExecutorService;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Callable;
+import java.util.concurrent.CancellationException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
@@ -38,7 +39,7 @@
// main(suite(), args);
// }
// public static Test suite() {
- // return new TestSuite(...);
+ // return new TestSuite(AbstractExecutorServiceTest.class);
// }
/**
@@ -196,28 +197,25 @@
public void testInterruptedSubmit() throws InterruptedException {
final CountDownLatch submitted = new CountDownLatch(1);
final CountDownLatch quittingTime = new CountDownLatch(1);
+ final Callable<Void> awaiter = new CheckedCallable<Void>() {
+ public Void realCall() throws InterruptedException {
+ assertTrue(quittingTime.await(2*LONG_DELAY_MS, MILLISECONDS));
+ return null;
+ }};
final ExecutorService p
= new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS,
new ArrayBlockingQueue<Runnable>(10));
- final Callable<Void> awaiter = new CheckedCallable<Void>() {
- public Void realCall() throws InterruptedException {
- quittingTime.await();
- return null;
- }};
- try {
- Thread t = new Thread(new CheckedInterruptedRunnable() {
+ try (PoolCleaner cleaner = cleaner(p, quittingTime)) {
+ Thread t = newStartedThread(new CheckedInterruptedRunnable() {
public void realRun() throws Exception {
Future<Void> future = p.submit(awaiter);
submitted.countDown();
future.get();
}});
- t.start();
- submitted.await();
+
+ await(submitted);
t.interrupt();
- t.join();
- } finally {
- quittingTime.countDown();
- joinPool(p);
+ awaitTermination(t);
}
}
@@ -226,34 +224,32 @@
* throws exception
*/
public void testSubmitEE() throws InterruptedException {
- ThreadPoolExecutor p =
+ final ThreadPoolExecutor p =
new ThreadPoolExecutor(1, 1,
60, TimeUnit.SECONDS,
new ArrayBlockingQueue<Runnable>(10));
-
- Callable c = new Callable() {
- public Object call() { throw new ArithmeticException(); }};
-
- try {
- p.submit(c).get();
- shouldThrow();
- } catch (ExecutionException success) {
- assertTrue(success.getCause() instanceof ArithmeticException);
+ try (PoolCleaner cleaner = cleaner(p)) {
+ Callable c = new Callable() {
+ public Object call() { throw new ArithmeticException(); }};
+ try {
+ p.submit(c).get();
+ shouldThrow();
+ } catch (ExecutionException success) {
+ assertTrue(success.getCause() instanceof ArithmeticException);
+ }
}
- joinPool(p);
}
/**
* invokeAny(null) throws NPE
*/
public void testInvokeAny1() throws Exception {
- ExecutorService e = new DirectExecutorService();
- try {
- e.invokeAny(null);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
- joinPool(e);
+ final ExecutorService e = new DirectExecutorService();
+ try (PoolCleaner cleaner = cleaner(e)) {
+ try {
+ e.invokeAny(null);
+ shouldThrow();
+ } catch (NullPointerException success) {}
}
}
@@ -261,13 +257,12 @@
* invokeAny(empty collection) throws IAE
*/
public void testInvokeAny2() throws Exception {
- ExecutorService e = new DirectExecutorService();
- try {
- e.invokeAny(new ArrayList<Callable<String>>());
- shouldThrow();
- } catch (IllegalArgumentException success) {
- } finally {
- joinPool(e);
+ final ExecutorService e = new DirectExecutorService();
+ try (PoolCleaner cleaner = cleaner(e)) {
+ try {
+ e.invokeAny(new ArrayList<Callable<String>>());
+ shouldThrow();
+ } catch (IllegalArgumentException success) {}
}
}
@@ -275,17 +270,16 @@
* invokeAny(c) throws NPE if c has null elements
*/
public void testInvokeAny3() throws Exception {
- ExecutorService e = new DirectExecutorService();
- List<Callable<Long>> l = new ArrayList<Callable<Long>>();
- l.add(new Callable<Long>() {
- public Long call() { throw new ArithmeticException(); }});
- l.add(null);
- try {
- e.invokeAny(l);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
- joinPool(e);
+ final ExecutorService e = new DirectExecutorService();
+ try (PoolCleaner cleaner = cleaner(e)) {
+ List<Callable<Long>> l = new ArrayList<Callable<Long>>();
+ l.add(new Callable<Long>() {
+ public Long call() { throw new ArithmeticException(); }});
+ l.add(null);
+ try {
+ e.invokeAny(l);
+ shouldThrow();
+ } catch (NullPointerException success) {}
}
}
@@ -293,16 +287,16 @@
* invokeAny(c) throws ExecutionException if no task in c completes
*/
public void testInvokeAny4() throws InterruptedException {
- ExecutorService e = new DirectExecutorService();
- List<Callable<String>> l = new ArrayList<Callable<String>>();
- l.add(new NPETask());
- try {
- e.invokeAny(l);
- shouldThrow();
- } catch (ExecutionException success) {
- assertTrue(success.getCause() instanceof NullPointerException);
- } finally {
- joinPool(e);
+ final ExecutorService e = new DirectExecutorService();
+ try (PoolCleaner cleaner = cleaner(e)) {
+ List<Callable<String>> l = new ArrayList<Callable<String>>();
+ l.add(new NPETask());
+ try {
+ e.invokeAny(l);
+ shouldThrow();
+ } catch (ExecutionException success) {
+ assertTrue(success.getCause() instanceof NullPointerException);
+ }
}
}
@@ -310,15 +304,13 @@
* invokeAny(c) returns result of some task in c if at least one completes
*/
public void testInvokeAny5() throws Exception {
- ExecutorService e = new DirectExecutorService();
- try {
+ final ExecutorService e = new DirectExecutorService();
+ try (PoolCleaner cleaner = cleaner(e)) {
List<Callable<String>> l = new ArrayList<Callable<String>>();
l.add(new StringTask());
l.add(new StringTask());
String result = e.invokeAny(l);
assertSame(TEST_STRING, result);
- } finally {
- joinPool(e);
}
}
@@ -326,13 +318,12 @@
* invokeAll(null) throws NPE
*/
public void testInvokeAll1() throws InterruptedException {
- ExecutorService e = new DirectExecutorService();
- try {
- e.invokeAll(null);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
- joinPool(e);
+ final ExecutorService e = new DirectExecutorService();
+ try (PoolCleaner cleaner = cleaner(e)) {
+ try {
+ e.invokeAll(null);
+ shouldThrow();
+ } catch (NullPointerException success) {}
}
}
@@ -340,12 +331,10 @@
* invokeAll(empty collection) returns empty collection
*/
public void testInvokeAll2() throws InterruptedException {
- ExecutorService e = new DirectExecutorService();
- try {
+ final ExecutorService e = new DirectExecutorService();
+ try (PoolCleaner cleaner = cleaner(e)) {
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
assertTrue(r.isEmpty());
- } finally {
- joinPool(e);
}
}
@@ -353,16 +342,15 @@
* invokeAll(c) throws NPE if c has null elements
*/
public void testInvokeAll3() throws InterruptedException {
- ExecutorService e = new DirectExecutorService();
- List<Callable<String>> l = new ArrayList<Callable<String>>();
- l.add(new StringTask());
- l.add(null);
- try {
- e.invokeAll(l);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
- joinPool(e);
+ final ExecutorService e = new DirectExecutorService();
+ try (PoolCleaner cleaner = cleaner(e)) {
+ List<Callable<String>> l = new ArrayList<Callable<String>>();
+ l.add(new StringTask());
+ l.add(null);
+ try {
+ e.invokeAll(l);
+ shouldThrow();
+ } catch (NullPointerException success) {}
}
}
@@ -370,8 +358,8 @@
* get of returned element of invokeAll(c) throws exception on failed task
*/
public void testInvokeAll4() throws Exception {
- ExecutorService e = new DirectExecutorService();
- try {
+ final ExecutorService e = new DirectExecutorService();
+ try (PoolCleaner cleaner = cleaner(e)) {
List<Callable<String>> l = new ArrayList<Callable<String>>();
l.add(new NPETask());
List<Future<String>> futures = e.invokeAll(l);
@@ -382,8 +370,6 @@
} catch (ExecutionException success) {
assertTrue(success.getCause() instanceof NullPointerException);
}
- } finally {
- joinPool(e);
}
}
@@ -391,8 +377,8 @@
* invokeAll(c) returns results of all completed tasks in c
*/
public void testInvokeAll5() throws Exception {
- ExecutorService e = new DirectExecutorService();
- try {
+ final ExecutorService e = new DirectExecutorService();
+ try (PoolCleaner cleaner = cleaner(e)) {
List<Callable<String>> l = new ArrayList<Callable<String>>();
l.add(new StringTask());
l.add(new StringTask());
@@ -400,8 +386,6 @@
assertEquals(2, futures.size());
for (Future<String> future : futures)
assertSame(TEST_STRING, future.get());
- } finally {
- joinPool(e);
}
}
@@ -409,13 +393,12 @@
* timed invokeAny(null) throws NPE
*/
public void testTimedInvokeAny1() throws Exception {
- ExecutorService e = new DirectExecutorService();
- try {
- e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
- joinPool(e);
+ final ExecutorService e = new DirectExecutorService();
+ try (PoolCleaner cleaner = cleaner(e)) {
+ try {
+ e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
+ shouldThrow();
+ } catch (NullPointerException success) {}
}
}
@@ -423,15 +406,14 @@
* timed invokeAny(null time unit) throws NPE
*/
public void testTimedInvokeAnyNullTimeUnit() throws Exception {
- ExecutorService e = new DirectExecutorService();
- List<Callable<String>> l = new ArrayList<Callable<String>>();
- l.add(new StringTask());
- try {
- e.invokeAny(l, MEDIUM_DELAY_MS, null);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
- joinPool(e);
+ final ExecutorService e = new DirectExecutorService();
+ try (PoolCleaner cleaner = cleaner(e)) {
+ List<Callable<String>> l = new ArrayList<Callable<String>>();
+ l.add(new StringTask());
+ try {
+ e.invokeAny(l, MEDIUM_DELAY_MS, null);
+ shouldThrow();
+ } catch (NullPointerException success) {}
}
}
@@ -439,13 +421,13 @@
* timed invokeAny(empty collection) throws IAE
*/
public void testTimedInvokeAny2() throws Exception {
- ExecutorService e = new DirectExecutorService();
- try {
- e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
- shouldThrow();
- } catch (IllegalArgumentException success) {
- } finally {
- joinPool(e);
+ final ExecutorService e = new DirectExecutorService();
+ try (PoolCleaner cleaner = cleaner(e)) {
+ try {
+ e.invokeAny(new ArrayList<Callable<String>>(),
+ MEDIUM_DELAY_MS, MILLISECONDS);
+ shouldThrow();
+ } catch (IllegalArgumentException success) {}
}
}
@@ -453,17 +435,16 @@
* timed invokeAny(c) throws NPE if c has null elements
*/
public void testTimedInvokeAny3() throws Exception {
- ExecutorService e = new DirectExecutorService();
- List<Callable<Long>> l = new ArrayList<Callable<Long>>();
- l.add(new Callable<Long>() {
- public Long call() { throw new ArithmeticException(); }});
- l.add(null);
- try {
- e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
- joinPool(e);
+ final ExecutorService e = new DirectExecutorService();
+ try (PoolCleaner cleaner = cleaner(e)) {
+ List<Callable<Long>> l = new ArrayList<Callable<Long>>();
+ l.add(new Callable<Long>() {
+ public Long call() { throw new ArithmeticException(); }});
+ l.add(null);
+ try {
+ e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
+ shouldThrow();
+ } catch (NullPointerException success) {}
}
}
@@ -471,16 +452,18 @@
* timed invokeAny(c) throws ExecutionException if no task completes
*/
public void testTimedInvokeAny4() throws Exception {
- ExecutorService e = new DirectExecutorService();
- List<Callable<String>> l = new ArrayList<Callable<String>>();
- l.add(new NPETask());
- try {
- e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
- shouldThrow();
- } catch (ExecutionException success) {
- assertTrue(success.getCause() instanceof NullPointerException);
- } finally {
- joinPool(e);
+ final ExecutorService e = new DirectExecutorService();
+ try (PoolCleaner cleaner = cleaner(e)) {
+ long startTime = System.nanoTime();
+ List<Callable<String>> l = new ArrayList<Callable<String>>();
+ l.add(new NPETask());
+ try {
+ e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
+ shouldThrow();
+ } catch (ExecutionException success) {
+ assertTrue(success.getCause() instanceof NullPointerException);
+ }
+ assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
}
}
@@ -488,15 +471,15 @@
* timed invokeAny(c) returns result of some task in c
*/
public void testTimedInvokeAny5() throws Exception {
- ExecutorService e = new DirectExecutorService();
- try {
+ final ExecutorService e = new DirectExecutorService();
+ try (PoolCleaner cleaner = cleaner(e)) {
+ long startTime = System.nanoTime();
List<Callable<String>> l = new ArrayList<Callable<String>>();
l.add(new StringTask());
l.add(new StringTask());
- String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
+ String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
assertSame(TEST_STRING, result);
- } finally {
- joinPool(e);
+ assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
}
}
@@ -504,13 +487,12 @@
* timed invokeAll(null) throws NPE
*/
public void testTimedInvokeAll1() throws InterruptedException {
- ExecutorService e = new DirectExecutorService();
- try {
- e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
- joinPool(e);
+ final ExecutorService e = new DirectExecutorService();
+ try (PoolCleaner cleaner = cleaner(e)) {
+ try {
+ e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
+ shouldThrow();
+ } catch (NullPointerException success) {}
}
}
@@ -518,15 +500,14 @@
* timed invokeAll(null time unit) throws NPE
*/
public void testTimedInvokeAllNullTimeUnit() throws InterruptedException {
- ExecutorService e = new DirectExecutorService();
- List<Callable<String>> l = new ArrayList<Callable<String>>();
- l.add(new StringTask());
- try {
- e.invokeAll(l, MEDIUM_DELAY_MS, null);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
- joinPool(e);
+ final ExecutorService e = new DirectExecutorService();
+ try (PoolCleaner cleaner = cleaner(e)) {
+ List<Callable<String>> l = new ArrayList<Callable<String>>();
+ l.add(new StringTask());
+ try {
+ e.invokeAll(l, MEDIUM_DELAY_MS, null);
+ shouldThrow();
+ } catch (NullPointerException success) {}
}
}
@@ -534,12 +515,10 @@
* timed invokeAll(empty collection) returns empty collection
*/
public void testTimedInvokeAll2() throws InterruptedException {
- ExecutorService e = new DirectExecutorService();
- try {
+ final ExecutorService e = new DirectExecutorService();
+ try (PoolCleaner cleaner = cleaner(e)) {
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
assertTrue(r.isEmpty());
- } finally {
- joinPool(e);
}
}
@@ -547,16 +526,15 @@
* timed invokeAll(c) throws NPE if c has null elements
*/
public void testTimedInvokeAll3() throws InterruptedException {
- ExecutorService e = new DirectExecutorService();
- List<Callable<String>> l = new ArrayList<Callable<String>>();
- l.add(new StringTask());
- l.add(null);
- try {
- e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
- joinPool(e);
+ final ExecutorService e = new DirectExecutorService();
+ try (PoolCleaner cleaner = cleaner(e)) {
+ List<Callable<String>> l = new ArrayList<Callable<String>>();
+ l.add(new StringTask());
+ l.add(null);
+ try {
+ e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
+ shouldThrow();
+ } catch (NullPointerException success) {}
}
}
@@ -564,12 +542,12 @@
* get of returned element of invokeAll(c) throws exception on failed task
*/
public void testTimedInvokeAll4() throws Exception {
- ExecutorService e = new DirectExecutorService();
- try {
+ final ExecutorService e = new DirectExecutorService();
+ try (PoolCleaner cleaner = cleaner(e)) {
List<Callable<String>> l = new ArrayList<Callable<String>>();
l.add(new NPETask());
List<Future<String>> futures =
- e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
+ e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
assertEquals(1, futures.size());
try {
futures.get(0).get();
@@ -577,8 +555,6 @@
} catch (ExecutionException success) {
assertTrue(success.getCause() instanceof NullPointerException);
}
- } finally {
- joinPool(e);
}
}
@@ -586,41 +562,51 @@
* timed invokeAll(c) returns results of all completed tasks in c
*/
public void testTimedInvokeAll5() throws Exception {
- ExecutorService e = new DirectExecutorService();
- try {
+ final ExecutorService e = new DirectExecutorService();
+ try (PoolCleaner cleaner = cleaner(e)) {
List<Callable<String>> l = new ArrayList<Callable<String>>();
l.add(new StringTask());
l.add(new StringTask());
List<Future<String>> futures =
- e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
+ e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
assertEquals(2, futures.size());
for (Future<String> future : futures)
assertSame(TEST_STRING, future.get());
- } finally {
- joinPool(e);
}
}
/**
* timed invokeAll cancels tasks not completed by timeout
*/
- public void testTimedInvokeAll6() throws InterruptedException {
- ExecutorService e = new DirectExecutorService();
- try {
- List<Callable<String>> l = new ArrayList<Callable<String>>();
- l.add(new StringTask());
- l.add(Executors.callable(possiblyInterruptedRunnable(2 * SHORT_DELAY_MS), TEST_STRING));
- l.add(new StringTask());
- List<Future<String>> futures =
- e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
- assertEquals(l.size(), futures.size());
- for (Future future : futures)
- assertTrue(future.isDone());
- assertFalse(futures.get(0).isCancelled());
- assertFalse(futures.get(1).isCancelled());
- assertTrue(futures.get(2).isCancelled());
- } finally {
- joinPool(e);
+ public void testTimedInvokeAll6() throws Exception {
+ final ExecutorService e = new DirectExecutorService();
+ try (PoolCleaner cleaner = cleaner(e)) {
+ for (long timeout = timeoutMillis();;) {
+ List<Callable<String>> tasks = new ArrayList<>();
+ tasks.add(new StringTask("0"));
+ tasks.add(Executors.callable(possiblyInterruptedRunnable(timeout),
+ TEST_STRING));
+ tasks.add(new StringTask("2"));
+ long startTime = System.nanoTime();
+ List<Future<String>> futures =
+ e.invokeAll(tasks, timeout, MILLISECONDS);
+ assertEquals(tasks.size(), futures.size());
+ assertTrue(millisElapsedSince(startTime) >= timeout);
+ for (Future future : futures)
+ assertTrue(future.isDone());
+ try {
+ assertEquals("0", futures.get(0).get());
+ assertEquals(TEST_STRING, futures.get(1).get());
+ } catch (CancellationException retryWithLongerTimeout) {
+ // unusual delay before starting second task
+ timeout *= 2;
+ if (timeout >= LONG_DELAY_MS / 2)
+ fail("expected exactly one task to be cancelled");
+ continue;
+ }
+ assertTrue(futures.get(2).isCancelled());
+ break;
+ }
}
}
diff --git a/jsr166-tests/src/test/java/jsr166/AbstractQueueTest.java b/jsr166-tests/src/test/java/jsr166/AbstractQueueTest.java
index 2aa7326..bf25668 100644
--- a/jsr166-tests/src/test/java/jsr166/AbstractQueueTest.java
+++ b/jsr166-tests/src/test/java/jsr166/AbstractQueueTest.java
@@ -24,7 +24,7 @@
// main(suite(), args);
// }
// public static Test suite() {
- // return new TestSuite(...);
+ // return new TestSuite(AbstractQueueTest.class);
// }
static class Succeed extends AbstractQueue<Integer> {
@@ -158,7 +158,7 @@
public void testAddAll3() {
Succeed q = new Succeed();
Integer[] ints = new Integer[SIZE];
- for (int i = 0; i < SIZE-1; ++i)
+ for (int i = 0; i < SIZE - 1; ++i)
ints[i] = new Integer(i);
try {
q.addAll(Arrays.asList(ints));
diff --git a/jsr166-tests/src/test/java/jsr166/AbstractQueuedLongSynchronizerTest.java b/jsr166-tests/src/test/java/jsr166/AbstractQueuedLongSynchronizerTest.java
index 8604d86..c462c73 100644
--- a/jsr166-tests/src/test/java/jsr166/AbstractQueuedLongSynchronizerTest.java
+++ b/jsr166-tests/src/test/java/jsr166/AbstractQueuedLongSynchronizerTest.java
@@ -29,7 +29,7 @@
// main(suite(), args);
// }
// public static Test suite() {
- // return new TestSuite(...);
+ // return new TestSuite(AbstractQueuedLongSynchronizerTest.class);
// }
/**
@@ -241,25 +241,33 @@
*/
void assertAwaitTimesOut(ConditionObject c, AwaitMethod awaitMethod) {
long timeoutMillis = timeoutMillis();
- long startTime = System.nanoTime();
+ long startTime;
try {
switch (awaitMethod) {
case awaitTimed:
+ startTime = System.nanoTime();
assertFalse(c.await(timeoutMillis, MILLISECONDS));
+ assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
break;
case awaitNanos:
+ startTime = System.nanoTime();
long nanosTimeout = MILLISECONDS.toNanos(timeoutMillis);
long nanosRemaining = c.awaitNanos(nanosTimeout);
assertTrue(nanosRemaining <= 0);
+ assertTrue(nanosRemaining > -MILLISECONDS.toNanos(LONG_DELAY_MS));
+ assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
break;
case awaitUntil:
+ // We shouldn't assume that nanoTime and currentTimeMillis
+ // use the same time source, so don't use nanoTime here.
+ java.util.Date delayedDate = delayedDate(timeoutMillis());
assertFalse(c.awaitUntil(delayedDate(timeoutMillis)));
+ assertTrue(new java.util.Date().getTime() >= delayedDate.getTime());
break;
default:
throw new UnsupportedOperationException();
}
} catch (InterruptedException ie) { threadUnexpectedException(ie); }
- assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
}
/**
diff --git a/jsr166-tests/src/test/java/jsr166/AbstractQueuedSynchronizerTest.java b/jsr166-tests/src/test/java/jsr166/AbstractQueuedSynchronizerTest.java
index b3c4110..d102fc6 100644
--- a/jsr166-tests/src/test/java/jsr166/AbstractQueuedSynchronizerTest.java
+++ b/jsr166-tests/src/test/java/jsr166/AbstractQueuedSynchronizerTest.java
@@ -29,7 +29,7 @@
// main(suite(), args);
// }
// public static Test suite() {
- // return new TestSuite(...);
+ // return new TestSuite(AbstractQueuedSynchronizerTest.class);
// }
/**
@@ -244,25 +244,33 @@
*/
void assertAwaitTimesOut(ConditionObject c, AwaitMethod awaitMethod) {
long timeoutMillis = timeoutMillis();
- long startTime = System.nanoTime();
+ long startTime;
try {
switch (awaitMethod) {
case awaitTimed:
+ startTime = System.nanoTime();
assertFalse(c.await(timeoutMillis, MILLISECONDS));
+ assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
break;
case awaitNanos:
+ startTime = System.nanoTime();
long nanosTimeout = MILLISECONDS.toNanos(timeoutMillis);
long nanosRemaining = c.awaitNanos(nanosTimeout);
assertTrue(nanosRemaining <= 0);
+ assertTrue(nanosRemaining > -MILLISECONDS.toNanos(LONG_DELAY_MS));
+ assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
break;
case awaitUntil:
+ // We shouldn't assume that nanoTime and currentTimeMillis
+ // use the same time source, so don't use nanoTime here.
+ java.util.Date delayedDate = delayedDate(timeoutMillis());
assertFalse(c.awaitUntil(delayedDate(timeoutMillis)));
+ assertTrue(new java.util.Date().getTime() >= delayedDate.getTime());
break;
default:
throw new UnsupportedOperationException();
}
} catch (InterruptedException ie) { threadUnexpectedException(ie); }
- assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
}
/**
diff --git a/jsr166-tests/src/test/java/jsr166/ArrayBlockingQueueTest.java b/jsr166-tests/src/test/java/jsr166/ArrayBlockingQueueTest.java
index 247c90e..902ae40 100644
--- a/jsr166-tests/src/test/java/jsr166/ArrayBlockingQueueTest.java
+++ b/jsr166-tests/src/test/java/jsr166/ArrayBlockingQueueTest.java
@@ -26,7 +26,7 @@
public class ArrayBlockingQueueTest extends JSR166TestCase {
- // android-note: These tests have been moved into their own separate
+ // android-note: These tests have been moved into their own separate
// classes to work around CTS issues.
//
// public static class Fair extends BlockingQueueTest {
@@ -34,17 +34,19 @@
// return new ArrayBlockingQueue(SIZE, true);
// }
// }
- //
+
// public static class NonFair extends BlockingQueueTest {
// protected BlockingQueue emptyCollection() {
// return new ArrayBlockingQueue(SIZE, false);
// }
// }
+
+ // 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 newTestSuite(ArrayBlockingQueueTest.class,
// new Fair().testSuite(),
@@ -109,7 +111,7 @@
*/
public void testConstructor5() {
Integer[] ints = new Integer[SIZE];
- for (int i = 0; i < SIZE-1; ++i)
+ for (int i = 0; i < SIZE - 1; ++i)
ints[i] = i;
Collection<Integer> elements = Arrays.asList(ints);
try {
@@ -171,7 +173,7 @@
assertEquals(i, q.remove());
}
for (int i = 0; i < SIZE; ++i) {
- assertEquals(SIZE-i, q.remainingCapacity());
+ assertEquals(SIZE - i, q.remainingCapacity());
assertEquals(SIZE, q.size() + q.remainingCapacity());
assertTrue(q.add(i));
}
@@ -219,7 +221,7 @@
public void testAddAll3() {
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
Integer[] ints = new Integer[SIZE];
- for (int i = 0; i < SIZE-1; ++i)
+ for (int i = 0; i < SIZE - 1; ++i)
ints[i] = new Integer(i);
try {
q.addAll(Arrays.asList(ints));
@@ -456,25 +458,23 @@
final CountDownLatch aboutToWait = new CountDownLatch(1);
Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
+ long startTime = System.nanoTime();
for (int i = 0; i < SIZE; ++i) {
- long t0 = System.nanoTime();
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
- assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
}
- long t0 = System.nanoTime();
aboutToWait.countDown();
try {
- q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
+ q.poll(LONG_DELAY_MS, MILLISECONDS);
shouldThrow();
} catch (InterruptedException success) {
- assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
+ assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
}
}});
- aboutToWait.await();
- waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
+ await(aboutToWait);
+ waitForThreadToEnterWaitState(t, LONG_DELAY_MS);
t.interrupt();
- awaitTermination(t, MEDIUM_DELAY_MS);
+ awaitTermination(t);
checkEmpty(q);
}
@@ -577,7 +577,7 @@
assertTrue(changed);
assertTrue(q.containsAll(p));
- assertEquals(SIZE-i, q.size());
+ assertEquals(SIZE - i, q.size());
p.remove();
}
}
@@ -590,7 +590,7 @@
ArrayBlockingQueue q = populatedQueue(SIZE);
ArrayBlockingQueue p = populatedQueue(i);
assertTrue(q.removeAll(p));
- assertEquals(SIZE-i, q.size());
+ assertEquals(SIZE - i, q.size());
for (int j = 0; j < i; ++j) {
Integer x = (Integer)(p.remove());
assertFalse(q.contains(x));
@@ -624,23 +624,23 @@
checkToArray(q);
assertEquals(i, q.poll());
checkToArray(q);
- q.add(SIZE+i);
+ q.add(SIZE + i);
}
for (int i = 0; i < SIZE; i++) {
checkToArray(q);
- assertEquals(SIZE+i, q.poll());
+ assertEquals(SIZE + i, q.poll());
}
}
void checkToArray2(ArrayBlockingQueue q) {
int size = q.size();
- Integer[] a1 = size == 0 ? null : new Integer[size-1];
+ Integer[] a1 = (size == 0) ? null : new Integer[size - 1];
Integer[] a2 = new Integer[size];
- Integer[] a3 = new Integer[size+2];
+ Integer[] a3 = new Integer[size + 2];
if (size > 0) Arrays.fill(a1, 42);
Arrays.fill(a2, 42);
Arrays.fill(a3, 42);
- Integer[] b1 = size == 0 ? null : (Integer[]) q.toArray(a1);
+ Integer[] b1 = (size == 0) ? null : (Integer[]) q.toArray(a1);
Integer[] b2 = (Integer[]) q.toArray(a2);
Integer[] b3 = (Integer[]) q.toArray(a3);
assertSame(a2, b2);
@@ -654,7 +654,7 @@
assertSame(b3[i], x);
}
assertNull(a3[size]);
- assertEquals(42, (int) a3[size+1]);
+ assertEquals(42, (int) a3[size + 1]);
if (size > 0) {
assertNotSame(a1, b1);
assertEquals(size, b1.length);
@@ -678,11 +678,11 @@
checkToArray2(q);
assertEquals(i, q.poll());
checkToArray2(q);
- q.add(SIZE+i);
+ q.add(SIZE + i);
}
for (int i = 0; i < SIZE; i++) {
checkToArray2(q);
- assertEquals(SIZE+i, q.poll());
+ assertEquals(SIZE + i, q.poll());
}
}
@@ -793,24 +793,24 @@
final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
q.add(one);
q.add(two);
- ExecutorService executor = Executors.newFixedThreadPool(2);
final CheckedBarrier threadsStarted = new CheckedBarrier(2);
- executor.execute(new CheckedRunnable() {
- public void realRun() throws InterruptedException {
- assertFalse(q.offer(three));
- threadsStarted.await();
- assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
- assertEquals(0, q.remainingCapacity());
- }});
+ final ExecutorService executor = Executors.newFixedThreadPool(2);
+ try (PoolCleaner cleaner = cleaner(executor)) {
+ executor.execute(new CheckedRunnable() {
+ public void realRun() throws InterruptedException {
+ assertFalse(q.offer(three));
+ threadsStarted.await();
+ assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
+ assertEquals(0, q.remainingCapacity());
+ }});
- executor.execute(new CheckedRunnable() {
- public void realRun() throws InterruptedException {
- threadsStarted.await();
- assertEquals(0, q.remainingCapacity());
- assertSame(one, q.take());
- }});
-
- joinPool(executor);
+ executor.execute(new CheckedRunnable() {
+ public void realRun() throws InterruptedException {
+ threadsStarted.await();
+ assertEquals(0, q.remainingCapacity());
+ assertSame(one, q.take());
+ }});
+ }
}
/**
@@ -819,22 +819,22 @@
public void testPollInExecutor() {
final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
final CheckedBarrier threadsStarted = new CheckedBarrier(2);
- ExecutorService executor = Executors.newFixedThreadPool(2);
- executor.execute(new CheckedRunnable() {
- public void realRun() throws InterruptedException {
- assertNull(q.poll());
- threadsStarted.await();
- assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
- checkEmpty(q);
- }});
+ final ExecutorService executor = Executors.newFixedThreadPool(2);
+ try (PoolCleaner cleaner = cleaner(executor)) {
+ executor.execute(new CheckedRunnable() {
+ public void realRun() throws InterruptedException {
+ assertNull(q.poll());
+ threadsStarted.await();
+ assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
+ checkEmpty(q);
+ }});
- executor.execute(new CheckedRunnable() {
- public void realRun() throws InterruptedException {
- threadsStarted.await();
- q.put(one);
- }});
-
- joinPool(executor);
+ executor.execute(new CheckedRunnable() {
+ public void realRun() throws InterruptedException {
+ threadsStarted.await();
+ q.put(one);
+ }});
+ }
}
/**
@@ -886,7 +886,7 @@
final ArrayBlockingQueue q = populatedQueue(SIZE);
Thread t = new Thread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
- q.put(new Integer(SIZE+1));
+ q.put(new Integer(SIZE + 1));
}});
t.start();
@@ -903,7 +903,7 @@
* drainTo(c, n) empties first min(n, size) elements of queue into c
*/
public void testDrainToN() {
- ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE*2);
+ ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE * 2);
for (int i = 0; i < SIZE + 2; ++i) {
for (int j = 0; j < SIZE; j++)
assertTrue(q.offer(new Integer(j)));
@@ -911,7 +911,7 @@
q.drainTo(l, i);
int k = (i < SIZE) ? i : SIZE;
assertEquals(k, l.size());
- assertEquals(SIZE-k, q.size());
+ assertEquals(SIZE - k, q.size());
for (int j = 0; j < k; ++j)
assertEquals(l.get(j), new Integer(j));
do {} while (q.poll() != null);
diff --git a/jsr166-tests/src/test/java/jsr166/ArrayDequeTest.java b/jsr166-tests/src/test/java/jsr166/ArrayDequeTest.java
index 16290e9..23cc6b9 100644
--- a/jsr166-tests/src/test/java/jsr166/ArrayDequeTest.java
+++ b/jsr166-tests/src/test/java/jsr166/ArrayDequeTest.java
@@ -26,7 +26,7 @@
// main(suite(), args);
// }
// public static Test suite() {
- // return new TestSuite(...);
+ // return new TestSuite(ArrayDequeTest.class);
// }
/**
@@ -65,8 +65,7 @@
*/
public void testConstructor4() {
try {
- Integer[] ints = new Integer[SIZE];
- new ArrayDeque(Arrays.asList(ints));
+ new ArrayDeque(Arrays.asList(new Integer[SIZE]));
shouldThrow();
} catch (NullPointerException success) {}
}
@@ -75,10 +74,10 @@
* Initializing from Collection with some null elements throws NPE
*/
public void testConstructor5() {
+ Integer[] ints = new Integer[SIZE];
+ for (int i = 0; i < SIZE - 1; ++i)
+ ints[i] = new Integer(i);
try {
- Integer[] ints = new Integer[SIZE];
- for (int i = 0; i < SIZE-1; ++i)
- ints[i] = new Integer(i);
new ArrayDeque(Arrays.asList(ints));
shouldThrow();
} catch (NullPointerException success) {}
@@ -116,7 +115,7 @@
public void testSize() {
ArrayDeque q = populatedDeque(SIZE);
for (int i = 0; i < SIZE; ++i) {
- assertEquals(SIZE-i, q.size());
+ assertEquals(SIZE - i, q.size());
q.removeFirst();
}
for (int i = 0; i < SIZE; ++i) {
@@ -129,8 +128,8 @@
* push(null) throws NPE
*/
public void testPushNull() {
+ ArrayDeque q = new ArrayDeque(1);
try {
- ArrayDeque q = new ArrayDeque(1);
q.push(null);
shouldThrow();
} catch (NullPointerException success) {}
@@ -164,8 +163,8 @@
* offer(null) throws NPE
*/
public void testOfferNull() {
+ ArrayDeque q = new ArrayDeque();
try {
- ArrayDeque q = new ArrayDeque();
q.offer(null);
shouldThrow();
} catch (NullPointerException success) {}
@@ -175,8 +174,8 @@
* offerFirst(null) throws NPE
*/
public void testOfferFirstNull() {
+ ArrayDeque q = new ArrayDeque();
try {
- ArrayDeque q = new ArrayDeque();
q.offerFirst(null);
shouldThrow();
} catch (NullPointerException success) {}
@@ -186,8 +185,8 @@
* offerLast(null) throws NPE
*/
public void testOfferLastNull() {
+ ArrayDeque q = new ArrayDeque();
try {
- ArrayDeque q = new ArrayDeque();
q.offerLast(null);
shouldThrow();
} catch (NullPointerException success) {}
@@ -230,8 +229,8 @@
* add(null) throws NPE
*/
public void testAddNull() {
+ ArrayDeque q = new ArrayDeque();
try {
- ArrayDeque q = new ArrayDeque();
q.add(null);
shouldThrow();
} catch (NullPointerException success) {}
@@ -241,8 +240,8 @@
* addFirst(null) throws NPE
*/
public void testAddFirstNull() {
+ ArrayDeque q = new ArrayDeque();
try {
- ArrayDeque q = new ArrayDeque();
q.addFirst(null);
shouldThrow();
} catch (NullPointerException success) {}
@@ -252,8 +251,8 @@
* addLast(null) throws NPE
*/
public void testAddLastNull() {
+ ArrayDeque q = new ArrayDeque();
try {
- ArrayDeque q = new ArrayDeque();
q.addLast(null);
shouldThrow();
} catch (NullPointerException success) {}
@@ -296,8 +295,8 @@
* addAll(null) throws NPE
*/
public void testAddAll1() {
+ ArrayDeque q = new ArrayDeque();
try {
- ArrayDeque q = new ArrayDeque();
q.addAll(null);
shouldThrow();
} catch (NullPointerException success) {}
@@ -307,10 +306,9 @@
* addAll of a collection with null elements throws NPE
*/
public void testAddAll2() {
+ ArrayDeque q = new ArrayDeque();
try {
- ArrayDeque q = new ArrayDeque();
- Integer[] ints = new Integer[SIZE];
- q.addAll(Arrays.asList(ints));
+ q.addAll(Arrays.asList(new Integer[SIZE]));
shouldThrow();
} catch (NullPointerException success) {}
}
@@ -320,11 +318,11 @@
* possibly adding some elements
*/
public void testAddAll3() {
+ ArrayDeque q = new ArrayDeque();
+ Integer[] ints = new Integer[SIZE];
+ for (int i = 0; i < SIZE - 1; ++i)
+ ints[i] = new Integer(i);
try {
- ArrayDeque q = new ArrayDeque();
- Integer[] ints = new Integer[SIZE];
- for (int i = 0; i < SIZE-1; ++i)
- ints[i] = new Integer(i);
q.addAll(Arrays.asList(ints));
shouldThrow();
} catch (NullPointerException success) {}
@@ -361,7 +359,7 @@
*/
public void testPollLast() {
ArrayDeque q = populatedDeque(SIZE);
- for (int i = SIZE-1; i >= 0; --i) {
+ for (int i = SIZE - 1; i >= 0; --i) {
assertEquals(i, q.pollLast());
}
assertNull(q.pollLast());
@@ -401,14 +399,14 @@
assertTrue(q.contains(i));
assertTrue(q.remove(i));
assertFalse(q.contains(i));
- assertTrue(q.contains(i-1));
+ assertTrue(q.contains(i - 1));
}
for (int i = 0; i < SIZE; i += 2) {
assertTrue(q.contains(i));
assertTrue(q.remove(i));
assertFalse(q.contains(i));
- assertFalse(q.remove(i+1));
- assertFalse(q.contains(i+1));
+ assertFalse(q.remove(i + 1));
+ assertFalse(q.contains(i + 1));
}
assertTrue(q.isEmpty());
}
@@ -446,7 +444,7 @@
*/
public void testPeekLast() {
ArrayDeque q = populatedDeque(SIZE);
- for (int i = SIZE-1; i >= 0; --i) {
+ for (int i = SIZE - 1; i >= 0; --i) {
assertEquals(i, q.peekLast());
assertEquals(i, q.pollLast());
assertTrue(q.peekLast() == null ||
@@ -490,7 +488,7 @@
*/
public void testLastElement() {
ArrayDeque q = populatedDeque(SIZE);
- for (int i = SIZE-1; i >= 0; --i) {
+ for (int i = SIZE - 1; i >= 0; --i) {
assertEquals(i, q.getLast());
assertEquals(i, q.pollLast());
}
@@ -541,7 +539,7 @@
}
for (int i = 0; i < SIZE; i += 2) {
assertTrue(q.removeFirstOccurrence(new Integer(i)));
- assertFalse(q.removeFirstOccurrence(new Integer(i+1)));
+ assertFalse(q.removeFirstOccurrence(new Integer(i + 1)));
}
assertTrue(q.isEmpty());
}
@@ -556,7 +554,7 @@
}
for (int i = 0; i < SIZE; i += 2) {
assertTrue(q.removeLastOccurrence(new Integer(i)));
- assertFalse(q.removeLastOccurrence(new Integer(i+1)));
+ assertFalse(q.removeLastOccurrence(new Integer(i + 1)));
}
assertTrue(q.isEmpty());
}
@@ -611,7 +609,7 @@
boolean changed = q.retainAll(p);
assertEquals(changed, (i > 0));
assertTrue(q.containsAll(p));
- assertEquals(SIZE-i, q.size());
+ assertEquals(SIZE - i, q.size());
p.removeFirst();
}
}
@@ -624,7 +622,7 @@
ArrayDeque q = populatedDeque(SIZE);
ArrayDeque p = populatedDeque(i);
assertTrue(q.removeAll(p));
- assertEquals(SIZE-i, q.size());
+ assertEquals(SIZE - i, q.size());
for (int j = 0; j < i; ++j) {
assertFalse(q.contains(p.removeFirst()));
}
@@ -656,23 +654,23 @@
for (int i = 0; i < SIZE; i++) {
checkToArray(q);
assertEquals(i, q.poll());
- q.addLast(SIZE+i);
+ q.addLast(SIZE + i);
}
for (int i = 0; i < SIZE; i++) {
checkToArray(q);
- assertEquals(SIZE+i, q.poll());
+ assertEquals(SIZE + i, q.poll());
}
}
void checkToArray2(ArrayDeque q) {
int size = q.size();
- Integer[] a1 = size == 0 ? null : new Integer[size-1];
+ Integer[] a1 = (size == 0) ? null : new Integer[size - 1];
Integer[] a2 = new Integer[size];
- Integer[] a3 = new Integer[size+2];
+ Integer[] a3 = new Integer[size + 2];
if (size > 0) Arrays.fill(a1, 42);
Arrays.fill(a2, 42);
Arrays.fill(a3, 42);
- Integer[] b1 = size == 0 ? null : (Integer[]) q.toArray(a1);
+ Integer[] b1 = (size == 0) ? null : (Integer[]) q.toArray(a1);
Integer[] b2 = (Integer[]) q.toArray(a2);
Integer[] b3 = (Integer[]) q.toArray(a3);
assertSame(a2, b2);
@@ -686,7 +684,7 @@
assertSame(b3[i], x);
}
assertNull(a3[size]);
- assertEquals(42, (int) a3[size+1]);
+ assertEquals(42, (int) a3[size + 1]);
if (size > 0) {
assertNotSame(a1, b1);
assertEquals(size, b1.length);
@@ -709,11 +707,11 @@
for (int i = 0; i < SIZE; i++) {
checkToArray2(q);
assertEquals(i, q.poll());
- q.addLast(SIZE+i);
+ q.addLast(SIZE + i);
}
for (int i = 0; i < SIZE; i++) {
checkToArray2(q);
- assertEquals(SIZE+i, q.poll());
+ assertEquals(SIZE + i, q.poll());
}
}
@@ -787,18 +785,18 @@
final Random rng = new Random();
for (int iters = 0; iters < 100; ++iters) {
int max = rng.nextInt(5) + 2;
- int split = rng.nextInt(max-1) + 1;
+ int split = rng.nextInt(max - 1) + 1;
for (int j = 1; j <= max; ++j)
q.add(new Integer(j));
Iterator it = q.iterator();
for (int j = 1; j <= split; ++j)
assertEquals(it.next(), new Integer(j));
it.remove();
- assertEquals(it.next(), new Integer(split+1));
+ assertEquals(it.next(), new Integer(split + 1));
for (int j = 1; j <= split; ++j)
q.remove(new Integer(j));
it = q.iterator();
- for (int j = split+1; j <= max; ++j) {
+ for (int j = split + 1; j <= max; ++j) {
assertEquals(it.next(), new Integer(j));
it.remove();
}
@@ -855,18 +853,18 @@
final Random rng = new Random();
for (int iters = 0; iters < 100; ++iters) {
int max = rng.nextInt(5) + 2;
- int split = rng.nextInt(max-1) + 1;
+ int split = rng.nextInt(max - 1) + 1;
for (int j = max; j >= 1; --j)
q.add(new Integer(j));
Iterator it = q.descendingIterator();
for (int j = 1; j <= split; ++j)
assertEquals(it.next(), new Integer(j));
it.remove();
- assertEquals(it.next(), new Integer(split+1));
+ assertEquals(it.next(), new Integer(split + 1));
for (int j = 1; j <= split; ++j)
q.remove(new Integer(j));
it = q.descendingIterator();
- for (int j = split+1; j <= max; ++j) {
+ for (int j = split + 1; j <= max; ++j) {
assertEquals(it.next(), new Integer(j));
it.remove();
}
diff --git a/jsr166-tests/src/test/java/jsr166/Atomic8Test.java b/jsr166-tests/src/test/java/jsr166/Atomic8Test.java
new file mode 100644
index 0000000..f81c44f
--- /dev/null
+++ b/jsr166-tests/src/test/java/jsr166/Atomic8Test.java
@@ -0,0 +1,574 @@
+/*
+ * Written by Doug Lea and Martin Buchholz with assistance from
+ * members of JCP JSR-166 Expert Group and released to the public
+ * domain, as explained at
+ * http://creativecommons.org/publicdomain/zero/1.0/
+ */
+
+package jsr166;
+
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.AtomicIntegerArray;
+import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.atomic.AtomicLongArray;
+import java.util.concurrent.atomic.AtomicLongFieldUpdater;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.concurrent.atomic.AtomicReferenceArray;
+import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+public class Atomic8Test 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(Atomic8Test.class);
+ // }
+
+ /*
+ * Tests of atomic class methods accepting lambdas
+ * introduced in JDK8.
+ */
+
+ static long addLong17(long x) { return x + 17; }
+ static int addInt17(int x) { return x + 17; }
+ static Integer addInteger17(Integer x) {
+ return new Integer(x.intValue() + 17);
+ }
+ static Integer sumInteger(Integer x, Integer y) {
+ return new Integer(x.intValue() + y.intValue());
+ }
+
+ volatile long aLongField;
+ volatile int anIntField;
+ volatile Integer anIntegerField;
+
+ AtomicLongFieldUpdater aLongFieldUpdater() {
+ return AtomicLongFieldUpdater.newUpdater
+ (Atomic8Test.class, "aLongField");
+ }
+
+ AtomicIntegerFieldUpdater anIntFieldUpdater() {
+ return AtomicIntegerFieldUpdater.newUpdater
+ (Atomic8Test.class, "anIntField");
+ }
+
+ AtomicReferenceFieldUpdater<Atomic8Test,Integer> anIntegerFieldUpdater() {
+ return AtomicReferenceFieldUpdater.newUpdater
+ (Atomic8Test.class, Integer.class, "anIntegerField");
+ }
+
+ /**
+ * AtomicLong getAndUpdate returns previous value and updates
+ * result of supplied function
+ */
+ public void testLongGetAndUpdate() {
+ AtomicLong a = new AtomicLong(1L);
+ assertEquals(1L, a.getAndUpdate(Atomic8Test::addLong17));
+ assertEquals(18L, a.getAndUpdate(Atomic8Test::addLong17));
+ assertEquals(35L, a.get());
+ }
+
+ /**
+ * AtomicLong updateAndGet updates with supplied function and
+ * returns result.
+ */
+ public void testLongUpdateAndGet() {
+ AtomicLong a = new AtomicLong(1L);
+ assertEquals(18L, a.updateAndGet(Atomic8Test::addLong17));
+ assertEquals(35L, a.updateAndGet(Atomic8Test::addLong17));
+ }
+
+ /**
+ * AtomicLong getAndAccumulate returns previous value and updates
+ * with supplied function.
+ */
+ public void testLongGetAndAccumulate() {
+ AtomicLong a = new AtomicLong(1L);
+ assertEquals(1L, a.getAndAccumulate(2L, Long::sum));
+ assertEquals(3L, a.getAndAccumulate(3L, Long::sum));
+ assertEquals(6L, a.get());
+ }
+
+ /**
+ * AtomicLong accumulateAndGet updates with supplied function and
+ * returns result.
+ */
+ public void testLongAccumulateAndGet() {
+ AtomicLong a = new AtomicLong(1L);
+ assertEquals(7L, a.accumulateAndGet(6L, Long::sum));
+ assertEquals(10L, a.accumulateAndGet(3L, Long::sum));
+ assertEquals(10L, a.get());
+ }
+
+ /**
+ * AtomicInteger getAndUpdate returns previous value and updates
+ * result of supplied function
+ */
+ public void testIntGetAndUpdate() {
+ AtomicInteger a = new AtomicInteger(1);
+ assertEquals(1, a.getAndUpdate(Atomic8Test::addInt17));
+ assertEquals(18, a.getAndUpdate(Atomic8Test::addInt17));
+ assertEquals(35, a.get());
+ }
+
+ /**
+ * AtomicInteger updateAndGet updates with supplied function and
+ * returns result.
+ */
+ public void testIntUpdateAndGet() {
+ AtomicInteger a = new AtomicInteger(1);
+ assertEquals(18, a.updateAndGet(Atomic8Test::addInt17));
+ assertEquals(35, a.updateAndGet(Atomic8Test::addInt17));
+ assertEquals(35, a.get());
+ }
+
+ /**
+ * AtomicInteger getAndAccumulate returns previous value and updates
+ * with supplied function.
+ */
+ public void testIntGetAndAccumulate() {
+ AtomicInteger a = new AtomicInteger(1);
+ assertEquals(1, a.getAndAccumulate(2, Integer::sum));
+ assertEquals(3, a.getAndAccumulate(3, Integer::sum));
+ assertEquals(6, a.get());
+ }
+
+ /**
+ * AtomicInteger accumulateAndGet updates with supplied function and
+ * returns result.
+ */
+ public void testIntAccumulateAndGet() {
+ AtomicInteger a = new AtomicInteger(1);
+ assertEquals(7, a.accumulateAndGet(6, Integer::sum));
+ assertEquals(10, a.accumulateAndGet(3, Integer::sum));
+ assertEquals(10, a.get());
+ }
+
+ /**
+ * AtomicReference getAndUpdate returns previous value and updates
+ * result of supplied function
+ */
+ public void testReferenceGetAndUpdate() {
+ AtomicReference<Integer> a = new AtomicReference<Integer>(one);
+ assertEquals(new Integer(1), a.getAndUpdate(Atomic8Test::addInteger17));
+ assertEquals(new Integer(18), a.getAndUpdate(Atomic8Test::addInteger17));
+ assertEquals(new Integer(35), a.get());
+ }
+
+ /**
+ * AtomicReference updateAndGet updates with supplied function and
+ * returns result.
+ */
+ public void testReferenceUpdateAndGet() {
+ AtomicReference<Integer> a = new AtomicReference<Integer>(one);
+ assertEquals(new Integer(18), a.updateAndGet(Atomic8Test::addInteger17));
+ assertEquals(new Integer(35), a.updateAndGet(Atomic8Test::addInteger17));
+ assertEquals(new Integer(35), a.get());
+ }
+
+ /**
+ * AtomicReference getAndAccumulate returns previous value and updates
+ * with supplied function.
+ */
+ public void testReferenceGetAndAccumulate() {
+ AtomicReference<Integer> a = new AtomicReference<Integer>(one);
+ assertEquals(new Integer(1), a.getAndAccumulate(2, Atomic8Test::sumInteger));
+ assertEquals(new Integer(3), a.getAndAccumulate(3, Atomic8Test::sumInteger));
+ assertEquals(new Integer(6), a.get());
+ }
+
+ /**
+ * AtomicReference accumulateAndGet updates with supplied function and
+ * returns result.
+ */
+ public void testReferenceAccumulateAndGet() {
+ AtomicReference<Integer> a = new AtomicReference<Integer>(one);
+ assertEquals(new Integer(7), a.accumulateAndGet(6, Atomic8Test::sumInteger));
+ assertEquals(new Integer(10), a.accumulateAndGet(3, Atomic8Test::sumInteger));
+ assertEquals(new Integer(10), a.get());
+ }
+
+ /**
+ * AtomicLongArray getAndUpdate returns previous value and updates
+ * result of supplied function
+ */
+ public void testLongArrayGetAndUpdate() {
+ AtomicLongArray a = new AtomicLongArray(1);
+ a.set(0, 1);
+ assertEquals(1L, a.getAndUpdate(0, Atomic8Test::addLong17));
+ assertEquals(18L, a.getAndUpdate(0, Atomic8Test::addLong17));
+ assertEquals(35L, a.get(0));
+ }
+
+ /**
+ * AtomicLongArray updateAndGet updates with supplied function and
+ * returns result.
+ */
+ public void testLongArrayUpdateAndGet() {
+ AtomicLongArray a = new AtomicLongArray(1);
+ a.set(0, 1);
+ assertEquals(18L, a.updateAndGet(0, Atomic8Test::addLong17));
+ assertEquals(35L, a.updateAndGet(0, Atomic8Test::addLong17));
+ assertEquals(35L, a.get(0));
+ }
+
+ /**
+ * AtomicLongArray getAndAccumulate returns previous value and updates
+ * with supplied function.
+ */
+ public void testLongArrayGetAndAccumulate() {
+ AtomicLongArray a = new AtomicLongArray(1);
+ a.set(0, 1);
+ assertEquals(1L, a.getAndAccumulate(0, 2L, Long::sum));
+ assertEquals(3L, a.getAndAccumulate(0, 3L, Long::sum));
+ assertEquals(6L, a.get(0));
+ }
+
+ /**
+ * AtomicLongArray accumulateAndGet updates with supplied function and
+ * returns result.
+ */
+ public void testLongArrayAccumulateAndGet() {
+ AtomicLongArray a = new AtomicLongArray(1);
+ a.set(0, 1);
+ assertEquals(7L, a.accumulateAndGet(0, 6L, Long::sum));
+ assertEquals(10L, a.accumulateAndGet(0, 3L, Long::sum));
+ assertEquals(10L, a.get(0));
+ }
+
+ /**
+ * AtomicIntegerArray getAndUpdate returns previous value and updates
+ * result of supplied function
+ */
+ public void testIntArrayGetAndUpdate() {
+ AtomicIntegerArray a = new AtomicIntegerArray(1);
+ a.set(0, 1);
+ assertEquals(1, a.getAndUpdate(0, Atomic8Test::addInt17));
+ assertEquals(18, a.getAndUpdate(0, Atomic8Test::addInt17));
+ assertEquals(35, a.get(0));
+ }
+
+ /**
+ * AtomicIntegerArray updateAndGet updates with supplied function and
+ * returns result.
+ */
+ public void testIntArrayUpdateAndGet() {
+ AtomicIntegerArray a = new AtomicIntegerArray(1);
+ a.set(0, 1);
+ assertEquals(18, a.updateAndGet(0, Atomic8Test::addInt17));
+ assertEquals(35, a.updateAndGet(0, Atomic8Test::addInt17));
+ assertEquals(35, a.get(0));
+ }
+
+ /**
+ * AtomicIntegerArray getAndAccumulate returns previous value and updates
+ * with supplied function.
+ */
+ public void testIntArrayGetAndAccumulate() {
+ AtomicIntegerArray a = new AtomicIntegerArray(1);
+ a.set(0, 1);
+ assertEquals(1, a.getAndAccumulate(0, 2, Integer::sum));
+ assertEquals(3, a.getAndAccumulate(0, 3, Integer::sum));
+ assertEquals(6, a.get(0));
+ }
+
+ /**
+ * AtomicIntegerArray accumulateAndGet updates with supplied function and
+ * returns result.
+ */
+ public void testIntArrayAccumulateAndGet() {
+ AtomicIntegerArray a = new AtomicIntegerArray(1);
+ a.set(0, 1);
+ assertEquals(7, a.accumulateAndGet(0, 6, Integer::sum));
+ assertEquals(10, a.accumulateAndGet(0, 3, Integer::sum));
+ }
+
+ /**
+ * AtomicReferenceArray getAndUpdate returns previous value and updates
+ * result of supplied function
+ */
+ public void testReferenceArrayGetAndUpdate() {
+ AtomicReferenceArray<Integer> a = new AtomicReferenceArray<Integer>(1);
+ a.set(0, one);
+ assertEquals(new Integer(1), a.getAndUpdate(0, Atomic8Test::addInteger17));
+ assertEquals(new Integer(18), a.getAndUpdate(0, Atomic8Test::addInteger17));
+ assertEquals(new Integer(35), a.get(0));
+ }
+
+ /**
+ * AtomicReferenceArray updateAndGet updates with supplied function and
+ * returns result.
+ */
+ public void testReferenceArrayUpdateAndGet() {
+ AtomicReferenceArray<Integer> a = new AtomicReferenceArray<Integer>(1);
+ a.set(0, one);
+ assertEquals(new Integer(18), a.updateAndGet(0, Atomic8Test::addInteger17));
+ assertEquals(new Integer(35), a.updateAndGet(0, Atomic8Test::addInteger17));
+ }
+
+ /**
+ * AtomicReferenceArray getAndAccumulate returns previous value and updates
+ * with supplied function.
+ */
+ public void testReferenceArrayGetAndAccumulate() {
+ AtomicReferenceArray<Integer> a = new AtomicReferenceArray<Integer>(1);
+ a.set(0, one);
+ assertEquals(new Integer(1), a.getAndAccumulate(0, 2, Atomic8Test::sumInteger));
+ assertEquals(new Integer(3), a.getAndAccumulate(0, 3, Atomic8Test::sumInteger));
+ assertEquals(new Integer(6), a.get(0));
+ }
+
+ /**
+ * AtomicReferenceArray accumulateAndGet updates with supplied function and
+ * returns result.
+ */
+ public void testReferenceArrayAccumulateAndGet() {
+ AtomicReferenceArray<Integer> a = new AtomicReferenceArray<Integer>(1);
+ a.set(0, one);
+ assertEquals(new Integer(7), a.accumulateAndGet(0, 6, Atomic8Test::sumInteger));
+ assertEquals(new Integer(10), a.accumulateAndGet(0, 3, Atomic8Test::sumInteger));
+ }
+
+ /**
+ * AtomicLongFieldUpdater getAndUpdate returns previous value and updates
+ * result of supplied function
+ */
+ public void testLongFieldUpdaterGetAndUpdate() {
+ AtomicLongFieldUpdater a = aLongFieldUpdater();
+ a.set(this, 1);
+ assertEquals(1L, a.getAndUpdate(this, Atomic8Test::addLong17));
+ assertEquals(18L, a.getAndUpdate(this, Atomic8Test::addLong17));
+ assertEquals(35L, a.get(this));
+ assertEquals(35L, aLongField);
+ }
+
+ /**
+ * AtomicLongFieldUpdater updateAndGet updates with supplied function and
+ * returns result.
+ */
+ public void testLongFieldUpdaterUpdateAndGet() {
+ AtomicLongFieldUpdater a = aLongFieldUpdater();
+ a.set(this, 1);
+ assertEquals(18L, a.updateAndGet(this, Atomic8Test::addLong17));
+ assertEquals(35L, a.updateAndGet(this, Atomic8Test::addLong17));
+ assertEquals(35L, a.get(this));
+ assertEquals(35L, aLongField);
+ }
+
+ /**
+ * AtomicLongFieldUpdater getAndAccumulate returns previous value
+ * and updates with supplied function.
+ */
+ public void testLongFieldUpdaterGetAndAccumulate() {
+ AtomicLongFieldUpdater a = aLongFieldUpdater();
+ a.set(this, 1);
+ assertEquals(1L, a.getAndAccumulate(this, 2L, Long::sum));
+ assertEquals(3L, a.getAndAccumulate(this, 3L, Long::sum));
+ assertEquals(6L, a.get(this));
+ assertEquals(6L, aLongField);
+ }
+
+ /**
+ * AtomicLongFieldUpdater accumulateAndGet updates with supplied
+ * function and returns result.
+ */
+ public void testLongFieldUpdaterAccumulateAndGet() {
+ AtomicLongFieldUpdater a = aLongFieldUpdater();
+ a.set(this, 1);
+ assertEquals(7L, a.accumulateAndGet(this, 6L, Long::sum));
+ assertEquals(10L, a.accumulateAndGet(this, 3L, Long::sum));
+ assertEquals(10L, a.get(this));
+ assertEquals(10L, aLongField);
+ }
+
+ /**
+ * AtomicIntegerFieldUpdater getAndUpdate returns previous value and updates
+ * result of supplied function
+ */
+ public void testIntegerFieldUpdaterGetAndUpdate() {
+ AtomicIntegerFieldUpdater a = anIntFieldUpdater();
+ a.set(this, 1);
+ assertEquals(1, a.getAndUpdate(this, Atomic8Test::addInt17));
+ assertEquals(18, a.getAndUpdate(this, Atomic8Test::addInt17));
+ assertEquals(35, a.get(this));
+ assertEquals(35, anIntField);
+ }
+
+ /**
+ * AtomicIntegerFieldUpdater updateAndGet updates with supplied function and
+ * returns result.
+ */
+ public void testIntegerFieldUpdaterUpdateAndGet() {
+ AtomicIntegerFieldUpdater a = anIntFieldUpdater();
+ a.set(this, 1);
+ assertEquals(18, a.updateAndGet(this, Atomic8Test::addInt17));
+ assertEquals(35, a.updateAndGet(this, Atomic8Test::addInt17));
+ assertEquals(35, a.get(this));
+ assertEquals(35, anIntField);
+ }
+
+ /**
+ * AtomicIntegerFieldUpdater getAndAccumulate returns previous value
+ * and updates with supplied function.
+ */
+ public void testIntegerFieldUpdaterGetAndAccumulate() {
+ AtomicIntegerFieldUpdater a = anIntFieldUpdater();
+ a.set(this, 1);
+ assertEquals(1, a.getAndAccumulate(this, 2, Integer::sum));
+ assertEquals(3, a.getAndAccumulate(this, 3, Integer::sum));
+ assertEquals(6, a.get(this));
+ assertEquals(6, anIntField);
+ }
+
+ /**
+ * AtomicIntegerFieldUpdater accumulateAndGet updates with supplied
+ * function and returns result.
+ */
+ public void testIntegerFieldUpdaterAccumulateAndGet() {
+ AtomicIntegerFieldUpdater a = anIntFieldUpdater();
+ a.set(this, 1);
+ assertEquals(7, a.accumulateAndGet(this, 6, Integer::sum));
+ assertEquals(10, a.accumulateAndGet(this, 3, Integer::sum));
+ assertEquals(10, a.get(this));
+ assertEquals(10, anIntField);
+ }
+
+ /**
+ * AtomicReferenceFieldUpdater getAndUpdate returns previous value
+ * and updates result of supplied function
+ */
+ public void testReferenceFieldUpdaterGetAndUpdate() {
+ AtomicReferenceFieldUpdater<Atomic8Test,Integer> a = anIntegerFieldUpdater();
+ a.set(this, one);
+ assertEquals(new Integer(1), a.getAndUpdate(this, Atomic8Test::addInteger17));
+ assertEquals(new Integer(18), a.getAndUpdate(this, Atomic8Test::addInteger17));
+ assertEquals(new Integer(35), a.get(this));
+ assertEquals(new Integer(35), anIntegerField);
+ }
+
+ /**
+ * AtomicReferenceFieldUpdater updateAndGet updates with supplied
+ * function and returns result.
+ */
+ public void testReferenceFieldUpdaterUpdateAndGet() {
+ AtomicReferenceFieldUpdater<Atomic8Test,Integer> a = anIntegerFieldUpdater();
+ a.set(this, one);
+ assertEquals(new Integer(18), a.updateAndGet(this, Atomic8Test::addInteger17));
+ assertEquals(new Integer(35), a.updateAndGet(this, Atomic8Test::addInteger17));
+ assertEquals(new Integer(35), a.get(this));
+ assertEquals(new Integer(35), anIntegerField);
+ }
+
+ /**
+ * AtomicReferenceFieldUpdater returns previous value and updates
+ * with supplied function.
+ */
+ public void testReferenceFieldUpdaterGetAndAccumulate() {
+ AtomicReferenceFieldUpdater<Atomic8Test,Integer> a = anIntegerFieldUpdater();
+ a.set(this, one);
+ assertEquals(new Integer(1), a.getAndAccumulate(this, 2, Atomic8Test::sumInteger));
+ assertEquals(new Integer(3), a.getAndAccumulate(this, 3, Atomic8Test::sumInteger));
+ assertEquals(new Integer(6), a.get(this));
+ assertEquals(new Integer(6), anIntegerField);
+ }
+
+ /**
+ * AtomicReferenceFieldUpdater accumulateAndGet updates with
+ * supplied function and returns result.
+ */
+ public void testReferenceFieldUpdaterAccumulateAndGet() {
+ AtomicReferenceFieldUpdater<Atomic8Test,Integer> a = anIntegerFieldUpdater();
+ a.set(this, one);
+ assertEquals(new Integer(7), a.accumulateAndGet(this, 6, Atomic8Test::sumInteger));
+ assertEquals(new Integer(10), a.accumulateAndGet(this, 3, Atomic8Test::sumInteger));
+ assertEquals(new Integer(10), a.get(this));
+ assertEquals(new Integer(10), anIntegerField);
+ }
+
+ /**
+ * All Atomic getAndUpdate methods throw NullPointerException on
+ * null function argument
+ */
+ public void testGetAndUpdateNPE() {
+ Runnable[] throwingActions = {
+ () -> new AtomicLong().getAndUpdate(null),
+ () -> new AtomicInteger().getAndUpdate(null),
+ () -> new AtomicReference().getAndUpdate(null),
+ () -> new AtomicLongArray(1).getAndUpdate(0, null),
+ () -> new AtomicIntegerArray(1).getAndUpdate(0, null),
+ () -> new AtomicReferenceArray(1).getAndUpdate(0, null),
+ () -> aLongFieldUpdater().getAndUpdate(this, null),
+ () -> anIntFieldUpdater().getAndUpdate(this, null),
+ () -> anIntegerFieldUpdater().getAndUpdate(this, null),
+ ////() -> aLongFieldUpdater().getAndUpdate(null, Atomic8Test::addLong17),
+ ////() -> anIntFieldUpdater().getAndUpdate(null, Atomic8Test::addInt17),
+ ////() -> anIntegerFieldUpdater().getAndUpdate(null, Atomic8Test::addInteger17),
+ };
+ assertThrows(NullPointerException.class, throwingActions);
+ }
+
+ /**
+ * All Atomic updateAndGet methods throw NullPointerException on null function argument
+ */
+ public void testUpdateAndGetNPE() {
+ Runnable[] throwingActions = {
+ () -> new AtomicLong().updateAndGet(null),
+ () -> new AtomicInteger().updateAndGet(null),
+ () -> new AtomicReference().updateAndGet(null),
+ () -> new AtomicLongArray(1).updateAndGet(0, null),
+ () -> new AtomicIntegerArray(1).updateAndGet(0, null),
+ () -> new AtomicReferenceArray(1).updateAndGet(0, null),
+ () -> aLongFieldUpdater().updateAndGet(this, null),
+ () -> anIntFieldUpdater().updateAndGet(this, null),
+ () -> anIntegerFieldUpdater().updateAndGet(this, null),
+ };
+ assertThrows(NullPointerException.class, throwingActions);
+ }
+
+ /**
+ * All Atomic getAndAccumulate methods throw NullPointerException
+ * on null function argument
+ */
+ public void testGetAndAccumulateNPE() {
+ Runnable[] throwingActions = {
+ () -> new AtomicLong().getAndAccumulate(1L, null),
+ () -> new AtomicInteger().getAndAccumulate(1, null),
+ () -> new AtomicReference().getAndAccumulate(one, null),
+ () -> new AtomicLongArray(1).getAndAccumulate(0, 1L, null),
+ () -> new AtomicIntegerArray(1).getAndAccumulate(0, 1, null),
+ () -> new AtomicReferenceArray(1).getAndAccumulate(0, one, null),
+ () -> aLongFieldUpdater().getAndAccumulate(this, 1L, null),
+ () -> anIntFieldUpdater().getAndAccumulate(this, 1, null),
+ () -> anIntegerFieldUpdater().getAndAccumulate(this, one, null),
+ };
+ assertThrows(NullPointerException.class, throwingActions);
+ }
+
+ /**
+ * All Atomic accumulateAndGet methods throw NullPointerException
+ * on null function argument
+ */
+ public void testAccumulateAndGetNPE() {
+ Runnable[] throwingActions = {
+ () -> new AtomicLong().accumulateAndGet(1L, null),
+ () -> new AtomicInteger().accumulateAndGet(1, null),
+ () -> new AtomicReference().accumulateAndGet(one, null),
+ () -> new AtomicLongArray(1).accumulateAndGet(0, 1L, null),
+ () -> new AtomicIntegerArray(1).accumulateAndGet(0, 1, null),
+ () -> new AtomicReferenceArray(1).accumulateAndGet(0, one, null),
+ () -> aLongFieldUpdater().accumulateAndGet(this, 1L, null),
+ () -> anIntFieldUpdater().accumulateAndGet(this, 1, null),
+ () -> anIntegerFieldUpdater().accumulateAndGet(this, one, null),
+ };
+ assertThrows(NullPointerException.class, throwingActions);
+ }
+
+}
diff --git a/jsr166-tests/src/test/java/jsr166/AtomicBooleanTest.java b/jsr166-tests/src/test/java/jsr166/AtomicBooleanTest.java
index bfe3fc6..6f5decf 100644
--- a/jsr166-tests/src/test/java/jsr166/AtomicBooleanTest.java
+++ b/jsr166-tests/src/test/java/jsr166/AtomicBooleanTest.java
@@ -21,7 +21,7 @@
// main(suite(), args);
// }
// public static Test suite() {
- // return new TestSuite(...);
+ // return new TestSuite(AtomicBooleanTest.class);
// }
/**
diff --git a/jsr166-tests/src/test/java/jsr166/AtomicIntegerArrayTest.java b/jsr166-tests/src/test/java/jsr166/AtomicIntegerArrayTest.java
index 670b9ce..c9e32aa 100644
--- a/jsr166-tests/src/test/java/jsr166/AtomicIntegerArrayTest.java
+++ b/jsr166-tests/src/test/java/jsr166/AtomicIntegerArrayTest.java
@@ -15,6 +15,7 @@
import junit.framework.TestSuite;
public class AtomicIntegerArrayTest extends JSR166TestCase {
+
// android-note: Removed because the CTS runner does a bad job of
// retrying tests that have suite() declarations.
//
@@ -22,7 +23,7 @@
// main(suite(), args);
// }
// public static Test suite() {
- // return new TestSuite(...);
+ // return new TestSuite(AtomicIntegerArrayTest.class);
// }
/**
@@ -290,7 +291,7 @@
assertTrue(v >= 0);
if (v != 0) {
done = false;
- if (aa.compareAndSet(i, v, v-1))
+ if (aa.compareAndSet(i, v, v - 1))
++counts;
}
}
diff --git a/jsr166-tests/src/test/java/jsr166/AtomicIntegerFieldUpdaterTest.java b/jsr166-tests/src/test/java/jsr166/AtomicIntegerFieldUpdaterTest.java
index ef75b46..c8d3856 100644
--- a/jsr166-tests/src/test/java/jsr166/AtomicIntegerFieldUpdaterTest.java
+++ b/jsr166-tests/src/test/java/jsr166/AtomicIntegerFieldUpdaterTest.java
@@ -15,8 +15,10 @@
public class AtomicIntegerFieldUpdaterTest extends JSR166TestCase {
volatile int x = 0;
+ protected volatile int protectedField;
+ private volatile int privateField;
int w;
- long z;
+ float z;
// android-note: Removed because the CTS runner does a bad job of
// retrying tests that have suite() declarations.
//
@@ -24,7 +26,59 @@
// main(suite(), args);
// }
// public static Test suite() {
- // return new TestSuite(...);
+ // return new TestSuite(AtomicIntegerFieldUpdaterTest.class);
+ // }
+
+ // for testing subclass access
+ // android-note: Removed because android doesn't restrict reflection access
+ // static class AtomicIntegerFieldUpdaterTestSubclass extends AtomicIntegerFieldUpdaterTest {
+ // public void checkPrivateAccess() {
+ // try {
+ // AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a =
+ // AtomicIntegerFieldUpdater.newUpdater
+ // (AtomicIntegerFieldUpdaterTest.class, "privateField");
+ // shouldThrow();
+ // } catch (RuntimeException success) {
+ // assertNotNull(success.getCause());
+ // }
+ // }
+
+ // public void checkCompareAndSetProtectedSub() {
+ // AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a =
+ // AtomicIntegerFieldUpdater.newUpdater
+ // (AtomicIntegerFieldUpdaterTest.class, "protectedField");
+ // this.protectedField = 1;
+ // assertTrue(a.compareAndSet(this, 1, 2));
+ // assertTrue(a.compareAndSet(this, 2, -4));
+ // assertEquals(-4, a.get(this));
+ // assertFalse(a.compareAndSet(this, -5, 7));
+ // assertEquals(-4, a.get(this));
+ // assertTrue(a.compareAndSet(this, -4, 7));
+ // assertEquals(7, a.get(this));
+ // }
+ // }
+
+ // static class UnrelatedClass {
+ // public void checkPackageAccess(AtomicIntegerFieldUpdaterTest obj) {
+ // obj.x = 72;
+ // AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a =
+ // AtomicIntegerFieldUpdater.newUpdater
+ // (AtomicIntegerFieldUpdaterTest.class, "x");
+ // assertEquals(72, a.get(obj));
+ // assertTrue(a.compareAndSet(obj, 72, 73));
+ // assertEquals(73, a.get(obj));
+ // }
+
+ // public void checkPrivateAccess(AtomicIntegerFieldUpdaterTest obj) {
+ // try {
+ // AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a =
+ // AtomicIntegerFieldUpdater.newUpdater
+ // (AtomicIntegerFieldUpdaterTest.class, "privateField");
+ // throw new AssertionError("should throw");
+ // } catch (RuntimeException success) {
+ // assertNotNull(success.getCause());
+ // }
+ // }
// }
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> updaterFor(String fieldName) {
@@ -65,6 +119,26 @@
}
/**
+ * construction using private field from subclass throws RuntimeException
+ */
+ // android-note: Removed because android doesn't restrict reflection access
+ // public void testPrivateFieldInSubclass() {
+ // AtomicIntegerFieldUpdaterTestSubclass s =
+ // new AtomicIntegerFieldUpdaterTestSubclass();
+ // s.checkPrivateAccess();
+ // }
+
+ /**
+ * construction from unrelated class; package access is allowed,
+ * private access is not
+ */
+ // android-note: Removed because android doesn't restrict reflection access
+ // public void testUnrelatedClassAccess() {
+ // new UnrelatedClass().checkPackageAccess(this);
+ // new UnrelatedClass().checkPrivateAccess(this);
+ // }
+
+ /**
* get returns the last value set or assigned
*/
public void testGetSet() {
@@ -109,6 +183,34 @@
}
/**
+ * compareAndSet succeeds in changing protected field value if
+ * equal to expected else fails
+ */
+ public void testCompareAndSetProtected() {
+ AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
+ a = updaterFor("protectedField");
+ protectedField = 1;
+ assertTrue(a.compareAndSet(this, 1, 2));
+ assertTrue(a.compareAndSet(this, 2, -4));
+ assertEquals(-4, a.get(this));
+ assertFalse(a.compareAndSet(this, -5, 7));
+ assertEquals(-4, a.get(this));
+ assertTrue(a.compareAndSet(this, -4, 7));
+ assertEquals(7, a.get(this));
+ }
+
+ /**
+ * compareAndSet succeeds in changing protected field value if
+ * equal to expected else fails
+ */
+ // android-note: Removed because android doesn't restrict reflection access
+ // public void testCompareAndSetProtectedInSubclass() {
+ // AtomicIntegerFieldUpdaterTestSubclass s =
+ // new AtomicIntegerFieldUpdaterTestSubclass();
+ // s.checkCompareAndSetProtectedSub();
+ // }
+
+ /**
* compareAndSet in one thread enables another waiting for value
* to succeed
*/
diff --git a/jsr166-tests/src/test/java/jsr166/AtomicIntegerTest.java b/jsr166-tests/src/test/java/jsr166/AtomicIntegerTest.java
index cf73810..6392c54 100644
--- a/jsr166-tests/src/test/java/jsr166/AtomicIntegerTest.java
+++ b/jsr166-tests/src/test/java/jsr166/AtomicIntegerTest.java
@@ -21,7 +21,7 @@
// main(suite(), args);
// }
// public static Test suite() {
- // return new TestSuite(...);
+ // return new TestSuite(AtomicIntegerTest.class);
// }
final int[] VALUES = {
diff --git a/jsr166-tests/src/test/java/jsr166/AtomicLongArrayTest.java b/jsr166-tests/src/test/java/jsr166/AtomicLongArrayTest.java
index 08df01e..a60ecfd 100644
--- a/jsr166-tests/src/test/java/jsr166/AtomicLongArrayTest.java
+++ b/jsr166-tests/src/test/java/jsr166/AtomicLongArrayTest.java
@@ -22,7 +22,7 @@
// main(suite(), args);
// }
// public static Test suite() {
- // return new TestSuite(...);
+ // return new TestSuite(AtomicLongArrayTest.class);
// }
/**
@@ -290,7 +290,7 @@
assertTrue(v >= 0);
if (v != 0) {
done = false;
- if (aa.compareAndSet(i, v, v-1))
+ if (aa.compareAndSet(i, v, v - 1))
++counts;
}
}
diff --git a/jsr166-tests/src/test/java/jsr166/AtomicLongFieldUpdaterTest.java b/jsr166-tests/src/test/java/jsr166/AtomicLongFieldUpdaterTest.java
index 204f814..d46280b 100644
--- a/jsr166-tests/src/test/java/jsr166/AtomicLongFieldUpdaterTest.java
+++ b/jsr166-tests/src/test/java/jsr166/AtomicLongFieldUpdaterTest.java
@@ -15,9 +15,10 @@
public class AtomicLongFieldUpdaterTest extends JSR166TestCase {
volatile long x = 0;
- int z;
+ protected volatile long protectedField;
+ private volatile long privateField;
long w;
-
+ float z;
// android-note: Removed because the CTS runner does a bad job of
// retrying tests that have suite() declarations.
//
@@ -25,7 +26,59 @@
// main(suite(), args);
// }
// public static Test suite() {
- // return new TestSuite(...);
+ // return new TestSuite(AtomicLongFieldUpdaterTest.class);
+ // }
+
+ // for testing subclass access
+ // android-note: Removed because android doesn't restrict reflection access
+ // static class AtomicLongFieldUpdaterTestSubclass extends AtomicLongFieldUpdaterTest {
+ // public void checkPrivateAccess() {
+ // try {
+ // AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a =
+ // AtomicLongFieldUpdater.newUpdater
+ // (AtomicLongFieldUpdaterTest.class, "privateField");
+ // shouldThrow();
+ // } catch (RuntimeException success) {
+ // assertNotNull(success.getCause());
+ // }
+ // }
+
+ // public void checkCompareAndSetProtectedSub() {
+ // AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a =
+ // AtomicLongFieldUpdater.newUpdater
+ // (AtomicLongFieldUpdaterTest.class, "protectedField");
+ // this.protectedField = 1;
+ // assertTrue(a.compareAndSet(this, 1, 2));
+ // assertTrue(a.compareAndSet(this, 2, -4));
+ // assertEquals(-4, a.get(this));
+ // assertFalse(a.compareAndSet(this, -5, 7));
+ // assertEquals(-4, a.get(this));
+ // assertTrue(a.compareAndSet(this, -4, 7));
+ // assertEquals(7, a.get(this));
+ // }
+ // }
+
+ // static class UnrelatedClass {
+ // public void checkPackageAccess(AtomicLongFieldUpdaterTest obj) {
+ // obj.x = 72L;
+ // AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a =
+ // AtomicLongFieldUpdater.newUpdater
+ // (AtomicLongFieldUpdaterTest.class, "x");
+ // assertEquals(72L, a.get(obj));
+ // assertTrue(a.compareAndSet(obj, 72L, 73L));
+ // assertEquals(73L, a.get(obj));
+ // }
+
+ // public void checkPrivateAccess(AtomicLongFieldUpdaterTest obj) {
+ // try {
+ // AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a =
+ // AtomicLongFieldUpdater.newUpdater
+ // (AtomicLongFieldUpdaterTest.class, "privateField");
+ // throw new AssertionError("should throw");
+ // } catch (RuntimeException success) {
+ // assertNotNull(success.getCause());
+ // }
+ // }
// }
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> updaterFor(String fieldName) {
@@ -66,6 +119,26 @@
}
/**
+ * construction using private field from subclass throws RuntimeException
+ */
+ // android-note: Removed because android doesn't restrict reflection access
+ // public void testPrivateFieldInSubclass() {
+ // AtomicLongFieldUpdaterTestSubclass s =
+ // new AtomicLongFieldUpdaterTestSubclass();
+ // s.checkPrivateAccess();
+ // }
+
+ /**
+ * construction from unrelated class; package access is allowed,
+ * private access is not
+ */
+ // android-note: Removed because android doesn't restrict reflection access
+ // public void testUnrelatedClassAccess() {
+ // new UnrelatedClass().checkPackageAccess(this);
+ // new UnrelatedClass().checkPrivateAccess(this);
+ // }
+
+ /**
* get returns the last value set or assigned
*/
public void testGetSet() {
@@ -110,6 +183,34 @@
}
/**
+ * compareAndSet succeeds in changing protected field value if
+ * equal to expected else fails
+ */
+ public void testCompareAndSetProtected() {
+ AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
+ a = updaterFor("protectedField");
+ protectedField = 1;
+ assertTrue(a.compareAndSet(this, 1, 2));
+ assertTrue(a.compareAndSet(this, 2, -4));
+ assertEquals(-4, a.get(this));
+ assertFalse(a.compareAndSet(this, -5, 7));
+ assertEquals(-4, a.get(this));
+ assertTrue(a.compareAndSet(this, -4, 7));
+ assertEquals(7, a.get(this));
+ }
+
+ /**
+ * compareAndSet succeeds in changing protected field value if
+ * equal to expected else fails
+ */
+ // android-note: Removed because android doesn't restrict reflection access
+ // public void testCompareAndSetProtectedInSubclass() {
+ // AtomicLongFieldUpdaterTestSubclass s =
+ // new AtomicLongFieldUpdaterTestSubclass();
+ // s.checkCompareAndSetProtectedSub();
+ // }
+
+ /**
* compareAndSet in one thread enables another waiting for value
* to succeed
*/
diff --git a/jsr166-tests/src/test/java/jsr166/AtomicLongTest.java b/jsr166-tests/src/test/java/jsr166/AtomicLongTest.java
index b9c1722..a8ee7c6 100644
--- a/jsr166-tests/src/test/java/jsr166/AtomicLongTest.java
+++ b/jsr166-tests/src/test/java/jsr166/AtomicLongTest.java
@@ -21,7 +21,7 @@
// main(suite(), args);
// }
// public static Test suite() {
- // return new TestSuite(...);
+ // return new TestSuite(AtomicLongTest.class);
// }
final long[] VALUES = {
diff --git a/jsr166-tests/src/test/java/jsr166/AtomicMarkableReferenceTest.java b/jsr166-tests/src/test/java/jsr166/AtomicMarkableReferenceTest.java
index 61b6b1b..bd4e8cb 100644
--- a/jsr166-tests/src/test/java/jsr166/AtomicMarkableReferenceTest.java
+++ b/jsr166-tests/src/test/java/jsr166/AtomicMarkableReferenceTest.java
@@ -21,7 +21,7 @@
// main(suite(), args);
// }
// public static Test suite() {
- // return new TestSuite(...);
+ // return new TestSuite(AtomicMarkableReferenceTest.class);
// }
/**
diff --git a/jsr166-tests/src/test/java/jsr166/AtomicReferenceArrayTest.java b/jsr166-tests/src/test/java/jsr166/AtomicReferenceArrayTest.java
index 1df2f9f..f3aab44 100644
--- a/jsr166-tests/src/test/java/jsr166/AtomicReferenceArrayTest.java
+++ b/jsr166-tests/src/test/java/jsr166/AtomicReferenceArrayTest.java
@@ -22,7 +22,7 @@
// main(suite(), args);
// }
// public static Test suite() {
- // return new TestSuite(...);
+ // return new TestSuite(AtomicReferenceArrayTest.class);
// }
/**
diff --git a/jsr166-tests/src/test/java/jsr166/AtomicReferenceFieldUpdaterTest.java b/jsr166-tests/src/test/java/jsr166/AtomicReferenceFieldUpdaterTest.java
index 4b0d946..9b2e9a9 100644
--- a/jsr166-tests/src/test/java/jsr166/AtomicReferenceFieldUpdaterTest.java
+++ b/jsr166-tests/src/test/java/jsr166/AtomicReferenceFieldUpdaterTest.java
@@ -15,6 +15,8 @@
public class AtomicReferenceFieldUpdaterTest extends JSR166TestCase {
volatile Integer x = null;
+ protected volatile Integer protectedField;
+ private volatile Integer privateField;
Object z;
Integer w;
volatile int i;
@@ -26,10 +28,62 @@
// main(suite(), args);
// }
// public static Test suite() {
- // return new TestSuite(...);
+ // return new TestSuite(AtomicReferenceFieldUpdaterTest.class);
// }
- AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> updaterFor(String fieldName) {
+ // for testing subclass access
+ // android-note: Removed because android doesn't restrict reflection access
+ // static class AtomicReferenceFieldUpdaterTestSubclass extends AtomicReferenceFieldUpdaterTest {
+ // public void checkPrivateAccess() {
+ // try {
+ // AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest,Integer> a =
+ // AtomicReferenceFieldUpdater.newUpdater
+ // (AtomicReferenceFieldUpdaterTest.class, Integer.class, "privateField");
+ // shouldThrow();
+ // } catch (RuntimeException success) {
+ // assertNotNull(success.getCause());
+ // }
+ // }
+
+ // public void checkCompareAndSetProtectedSub() {
+ // AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest,Integer> a =
+ // AtomicReferenceFieldUpdater.newUpdater
+ // (AtomicReferenceFieldUpdaterTest.class, Integer.class, "protectedField");
+ // this.protectedField = one;
+ // assertTrue(a.compareAndSet(this, one, two));
+ // assertTrue(a.compareAndSet(this, two, m4));
+ // assertSame(m4, a.get(this));
+ // assertFalse(a.compareAndSet(this, m5, seven));
+ // assertFalse(seven == a.get(this));
+ // assertTrue(a.compareAndSet(this, m4, seven));
+ // assertSame(seven, a.get(this));
+ // }
+ // }
+
+ // static class UnrelatedClass {
+ // public void checkPackageAccess(AtomicReferenceFieldUpdaterTest obj) {
+ // obj.x = one;
+ // AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest,Integer> a =
+ // AtomicReferenceFieldUpdater.newUpdater
+ // (AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
+ // assertSame(one, a.get(obj));
+ // assertTrue(a.compareAndSet(obj, one, two));
+ // assertSame(two, a.get(obj));
+ // }
+
+ // public void checkPrivateAccess(AtomicReferenceFieldUpdaterTest obj) {
+ // try {
+ // AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest,Integer> a =
+ // AtomicReferenceFieldUpdater.newUpdater
+ // (AtomicReferenceFieldUpdaterTest.class, Integer.class, "privateField");
+ // throw new AssertionError("should throw");
+ // } catch (RuntimeException success) {
+ // assertNotNull(success.getCause());
+ // }
+ // }
+ // }
+
+ static AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> updaterFor(String fieldName) {
return AtomicReferenceFieldUpdater.newUpdater
(AtomicReferenceFieldUpdaterTest.class, Integer.class, fieldName);
}
@@ -77,6 +131,26 @@
}
/**
+ * construction using private field from subclass throws RuntimeException
+ */
+ // android-note: Removed because android doesn't restrict reflection access
+ // public void testPrivateFieldInSubclass() {
+ // AtomicReferenceFieldUpdaterTestSubclass s =
+ // new AtomicReferenceFieldUpdaterTestSubclass();
+ // s.checkPrivateAccess();
+ // }
+
+ /**
+ * construction from unrelated class; package access is allowed,
+ * private access is not
+ */
+ // android-note: Removed because android doesn't restrict reflection access
+ // public void testUnrelatedClassAccess() {
+ // new UnrelatedClass().checkPackageAccess(this);
+ // new UnrelatedClass().checkPrivateAccess(this);
+ // }
+
+ /**
* get returns the last value set or assigned
*/
public void testGetSet() {
diff --git a/jsr166-tests/src/test/java/jsr166/AtomicReferenceTest.java b/jsr166-tests/src/test/java/jsr166/AtomicReferenceTest.java
index 457182f..4728970 100644
--- a/jsr166-tests/src/test/java/jsr166/AtomicReferenceTest.java
+++ b/jsr166-tests/src/test/java/jsr166/AtomicReferenceTest.java
@@ -21,7 +21,7 @@
// main(suite(), args);
// }
// public static Test suite() {
- // return new TestSuite(...);
+ // return new TestSuite(AtomicReferenceTest.class);
// }
/**
diff --git a/jsr166-tests/src/test/java/jsr166/AtomicStampedReferenceTest.java b/jsr166-tests/src/test/java/jsr166/AtomicStampedReferenceTest.java
index b3ff06a..a2e8c7f 100644
--- a/jsr166-tests/src/test/java/jsr166/AtomicStampedReferenceTest.java
+++ b/jsr166-tests/src/test/java/jsr166/AtomicStampedReferenceTest.java
@@ -21,7 +21,7 @@
// main(suite(), args);
// }
// public static Test suite() {
- // return new TestSuite(...);
+ // return new TestSuite(AtomicStampedReferenceTest.class);
// }
/**
diff --git a/jsr166-tests/src/test/java/jsr166/BlockingQueueTest.java b/jsr166-tests/src/test/java/jsr166/BlockingQueueTest.java
index db0f03d..1a188e1 100644
--- a/jsr166-tests/src/test/java/jsr166/BlockingQueueTest.java
+++ b/jsr166-tests/src/test/java/jsr166/BlockingQueueTest.java
@@ -39,9 +39,9 @@
// android-note: Explicitly instantiated.
//
// public Test testSuite() {
- // // TODO: filter the returned tests using the configuration
- // // information provided by the subclass via protected methods.
- // return new TestSuite(this.getClass());
+ // // TODO: filter the returned tests using the configuration
+ // // information provided by the subclass via protected methods.
+ // return new TestSuite(this.getClass());
// }
//----------------------------------------------------------------
@@ -239,6 +239,8 @@
shouldThrow();
} catch (InterruptedException success) {}
assertFalse(Thread.interrupted());
+
+ assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
}});
barrier.await();
@@ -352,20 +354,20 @@
assertEquals((pass == 0), q.contains(elts[i]));
assertEquals((pass == 0), q.remove(elts[i]));
assertFalse(q.contains(elts[i]));
- assertTrue(q.contains(elts[i-1]));
+ assertTrue(q.contains(elts[i - 1]));
if (i < size - 1)
- assertTrue(q.contains(elts[i+1]));
+ assertTrue(q.contains(elts[i + 1]));
}
}
if (size > 0)
assertTrue(q.contains(elts[0]));
- for (int i = size-2; i >= 0; i -= 2) {
+ for (int i = size - 2; i >= 0; i -= 2) {
assertTrue(q.contains(elts[i]));
- assertFalse(q.contains(elts[i+1]));
+ assertFalse(q.contains(elts[i + 1]));
assertTrue(q.remove(elts[i]));
assertFalse(q.contains(elts[i]));
- assertFalse(q.remove(elts[i+1]));
- assertFalse(q.contains(elts[i+1]));
+ assertFalse(q.remove(elts[i + 1]));
+ assertFalse(q.contains(elts[i + 1]));
}
checkEmpty(q);
}
diff --git a/jsr166-tests/src/test/java/jsr166/Collection8Test.java b/jsr166-tests/src/test/java/jsr166/Collection8Test.java
new file mode 100644
index 0000000..634182b
--- /dev/null
+++ b/jsr166-tests/src/test/java/jsr166/Collection8Test.java
@@ -0,0 +1,104 @@
+/*
+ * Written by Doug Lea and Martin Buchholz with assistance from
+ * members of JCP JSR-166 Expert Group and released to the public
+ * domain, as explained at
+ * http://creativecommons.org/publicdomain/zero/1.0/
+ */
+
+package jsr166;
+
+import static java.util.concurrent.TimeUnit.MILLISECONDS;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Future;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.function.Consumer;
+
+import junit.framework.Test;
+
+/**
+ * Contains tests applicable to all jdk8+ Collection implementations.
+ * An extension of CollectionTest.
+ */
+public class Collection8Test extends JSR166TestCase {
+ final CollectionImplementation impl;
+
+ /** Tests are parameterized by a Collection implementation. */
+ Collection8Test(CollectionImplementation impl, String methodName) {
+ super(methodName);
+ this.impl = impl;
+ }
+
+ public static Test testSuite(CollectionImplementation impl) {
+ return parameterizedTestSuite(Collection8Test.class,
+ CollectionImplementation.class,
+ impl);
+ }
+
+ /**
+ * stream().forEach returns elements in the collection
+ */
+ // TODO(streams):
+ // public void testForEach() throws Throwable {
+ // final Collection c = impl.emptyCollection();
+ // final AtomicLong count = new AtomicLong(0L);
+ // final Object x = impl.makeElement(1);
+ // final Object y = impl.makeElement(2);
+ // final ArrayList found = new ArrayList();
+ // Consumer<Object> spy = (o) -> { found.add(o); };
+ // c.stream().forEach(spy);
+ // assertTrue(found.isEmpty());
+
+ // assertTrue(c.add(x));
+ // c.stream().forEach(spy);
+ // assertEquals(Collections.singletonList(x), found);
+ // found.clear();
+
+ // assertTrue(c.add(y));
+ // c.stream().forEach(spy);
+ // assertEquals(2, found.size());
+ // assertTrue(found.contains(x));
+ // assertTrue(found.contains(y));
+ // found.clear();
+
+ // c.clear();
+ // c.stream().forEach(spy);
+ // assertTrue(found.isEmpty());
+ // }
+
+ // public void testForEachConcurrentStressTest() throws Throwable {
+ // if (!impl.isConcurrent()) return;
+ // final Collection c = impl.emptyCollection();
+ // final long testDurationMillis = timeoutMillis();
+ // final AtomicBoolean done = new AtomicBoolean(false);
+ // final Object elt = impl.makeElement(1);
+ // final Future<?> f1, f2;
+ // final ExecutorService pool = Executors.newCachedThreadPool();
+ // try (PoolCleaner cleaner = cleaner(pool, done)) {
+ // final CountDownLatch threadsStarted = new CountDownLatch(2);
+ // Runnable checkElt = () -> {
+ // threadsStarted.countDown();
+ // while (!done.get())
+ // c.stream().forEach((x) -> { assertSame(x, elt); }); };
+ // Runnable addRemove = () -> {
+ // threadsStarted.countDown();
+ // while (!done.get()) {
+ // assertTrue(c.add(elt));
+ // assertTrue(c.remove(elt));
+ // }};
+ // f1 = pool.submit(checkElt);
+ // f2 = pool.submit(addRemove);
+ // Thread.sleep(testDurationMillis);
+ // }
+ // assertNull(f1.get(0L, MILLISECONDS));
+ // assertNull(f2.get(0L, MILLISECONDS));
+ // }
+
+ // public void testCollection8DebugFail() { fail(); }
+}
diff --git a/jsr166-tests/src/test/java/jsr166/CollectionImplementation.java b/jsr166-tests/src/test/java/jsr166/CollectionImplementation.java
new file mode 100644
index 0000000..4ba5bda
--- /dev/null
+++ b/jsr166-tests/src/test/java/jsr166/CollectionImplementation.java
@@ -0,0 +1,21 @@
+/*
+ * Written by Doug Lea and Martin Buchholz with assistance from
+ * members of JCP JSR-166 Expert Group and released to the public
+ * domain, as explained at
+ * http://creativecommons.org/publicdomain/zero/1.0/
+ */
+
+package jsr166;
+
+import java.util.Collection;
+
+/** Allows tests to work with different Collection implementations. */
+public interface CollectionImplementation {
+ /** Returns the Collection class. */
+ public Class<?> klazz();
+ /** Returns an empty collection. */
+ public Collection emptyCollection();
+ public Object makeElement(int i);
+ public boolean isConcurrent();
+ public boolean permitsNulls();
+}
diff --git a/jsr166-tests/src/test/java/jsr166/CollectionTest.java b/jsr166-tests/src/test/java/jsr166/CollectionTest.java
new file mode 100644
index 0000000..44ef66d
--- /dev/null
+++ b/jsr166-tests/src/test/java/jsr166/CollectionTest.java
@@ -0,0 +1,43 @@
+/*
+ * Written by Doug Lea and Martin Buchholz with assistance from
+ * members of JCP JSR-166 Expert Group and released to the public
+ * domain, as explained at
+ * http://creativecommons.org/publicdomain/zero/1.0/
+ */
+
+package jsr166;
+
+import java.util.Collection;
+
+import junit.framework.Test;
+
+/**
+ * Contains tests applicable to all Collection implementations.
+ */
+public class CollectionTest extends JSR166TestCase {
+ final CollectionImplementation impl;
+
+ /** Tests are parameterized by a Collection implementation. */
+ CollectionTest(CollectionImplementation impl, String methodName) {
+ super(methodName);
+ this.impl = impl;
+ }
+
+ public static Test testSuite(CollectionImplementation impl) {
+ return newTestSuite
+ (parameterizedTestSuite(CollectionTest.class,
+ CollectionImplementation.class,
+ impl),
+ jdk8ParameterizedTestSuite(CollectionTest.class,
+ CollectionImplementation.class,
+ impl));
+ }
+
+ /** A test of the CollectionImplementation implementation ! */
+ public void testEmptyMeansEmpty() {
+ assertTrue(impl.emptyCollection().isEmpty());
+ assertEquals(0, impl.emptyCollection().size());
+ }
+
+ // public void testCollectionDebugFail() { fail(); }
+}
diff --git a/jsr166-tests/src/test/java/jsr166/CompletableFutureTest.java b/jsr166-tests/src/test/java/jsr166/CompletableFutureTest.java
new file mode 100644
index 0000000..1372cc4
--- /dev/null
+++ b/jsr166-tests/src/test/java/jsr166/CompletableFutureTest.java
@@ -0,0 +1,3959 @@
+/*
+ * Written by Doug Lea and Martin Buchholz with assistance from
+ * members of JCP JSR-166 Expert Group and released to the public
+ * domain, as explained at
+ * http://creativecommons.org/publicdomain/zero/1.0/
+ */
+
+package jsr166;
+
+import static java.util.concurrent.TimeUnit.MILLISECONDS;
+import static java.util.concurrent.TimeUnit.SECONDS;
+import static java.util.concurrent.CompletableFuture.completedFuture;
+import static java.util.concurrent.CompletableFuture.failedFuture;
+
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+
+// TODO(streams):
+//import java.util.stream.Collectors;
+//import java.util.stream.Stream;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Objects;
+import java.util.Set;
+import java.util.concurrent.Callable;
+import java.util.concurrent.CancellationException;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.CompletionException;
+import java.util.concurrent.CompletionStage;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Executor;
+import java.util.concurrent.ForkJoinPool;
+import java.util.concurrent.ForkJoinTask;
+import java.util.concurrent.TimeoutException;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.function.BiConsumer;
+import java.util.function.BiFunction;
+import java.util.function.Consumer;
+import java.util.function.Function;
+import java.util.function.Predicate;
+import java.util.function.Supplier;
+
+import junit.framework.AssertionFailedError;
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+public class CompletableFutureTest 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(CompletableFutureTest.class);
+ // }
+
+ static class CFException extends RuntimeException {}
+
+ void checkIncomplete(CompletableFuture<?> f) {
+ assertFalse(f.isDone());
+ assertFalse(f.isCancelled());
+ assertTrue(f.toString().contains("Not completed"));
+ try {
+ assertNull(f.getNow(null));
+ } catch (Throwable fail) { threadUnexpectedException(fail); }
+ try {
+ f.get(0L, SECONDS);
+ shouldThrow();
+ }
+ catch (TimeoutException success) {}
+ catch (Throwable fail) { threadUnexpectedException(fail); }
+ }
+
+ <T> void checkCompletedNormally(CompletableFuture<T> f, T value) {
+ checkTimedGet(f, value);
+
+ try {
+ assertEquals(value, f.join());
+ } catch (Throwable fail) { threadUnexpectedException(fail); }
+ try {
+ assertEquals(value, f.getNow(null));
+ } catch (Throwable fail) { threadUnexpectedException(fail); }
+ try {
+ assertEquals(value, f.get());
+ } catch (Throwable fail) { threadUnexpectedException(fail); }
+ assertTrue(f.isDone());
+ assertFalse(f.isCancelled());
+ assertFalse(f.isCompletedExceptionally());
+ assertTrue(f.toString().contains("[Completed normally]"));
+ }
+
+ /**
+ * Returns the "raw" internal exceptional completion of f,
+ * without any additional wrapping with CompletionException.
+ */
+ <U> Throwable exceptionalCompletion(CompletableFuture<U> f) {
+ // handle (and whenComplete) can distinguish between "direct"
+ // and "wrapped" exceptional completion
+ return f.handle((U u, Throwable t) -> t).join();
+ }
+
+ void checkCompletedExceptionally(CompletableFuture<?> f,
+ boolean wrapped,
+ Consumer<Throwable> checker) {
+ Throwable cause = exceptionalCompletion(f);
+ if (wrapped) {
+ assertTrue(cause instanceof CompletionException);
+ cause = cause.getCause();
+ }
+ checker.accept(cause);
+
+ long startTime = System.nanoTime();
+ try {
+ f.get(LONG_DELAY_MS, MILLISECONDS);
+ shouldThrow();
+ } catch (ExecutionException success) {
+ assertSame(cause, success.getCause());
+ } catch (Throwable fail) { threadUnexpectedException(fail); }
+ assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
+
+ try {
+ f.join();
+ shouldThrow();
+ } catch (CompletionException success) {
+ assertSame(cause, success.getCause());
+ } catch (Throwable fail) { threadUnexpectedException(fail); }
+
+ try {
+ f.getNow(null);
+ shouldThrow();
+ } catch (CompletionException success) {
+ assertSame(cause, success.getCause());
+ } catch (Throwable fail) { threadUnexpectedException(fail); }
+
+ try {
+ f.get();
+ shouldThrow();
+ } catch (ExecutionException success) {
+ assertSame(cause, success.getCause());
+ } catch (Throwable fail) { threadUnexpectedException(fail); }
+
+ assertFalse(f.isCancelled());
+ assertTrue(f.isDone());
+ assertTrue(f.isCompletedExceptionally());
+ assertTrue(f.toString().contains("[Completed exceptionally]"));
+ }
+
+ void checkCompletedWithWrappedCFException(CompletableFuture<?> f) {
+ checkCompletedExceptionally(f, true,
+ (t) -> assertTrue(t instanceof CFException));
+ }
+
+ void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) {
+ checkCompletedExceptionally(f, true,
+ (t) -> assertTrue(t instanceof CancellationException));
+ }
+
+ void checkCompletedWithTimeoutException(CompletableFuture<?> f) {
+ checkCompletedExceptionally(f, false,
+ (t) -> assertTrue(t instanceof TimeoutException));
+ }
+
+ void checkCompletedWithWrappedException(CompletableFuture<?> f,
+ Throwable ex) {
+ checkCompletedExceptionally(f, true, (t) -> assertSame(t, ex));
+ }
+
+ void checkCompletedExceptionally(CompletableFuture<?> f, Throwable ex) {
+ checkCompletedExceptionally(f, false, (t) -> assertSame(t, ex));
+ }
+
+ void checkCancelled(CompletableFuture<?> f) {
+ long startTime = System.nanoTime();
+ try {
+ f.get(LONG_DELAY_MS, MILLISECONDS);
+ shouldThrow();
+ } catch (CancellationException success) {
+ } catch (Throwable fail) { threadUnexpectedException(fail); }
+ assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
+
+ try {
+ f.join();
+ shouldThrow();
+ } catch (CancellationException success) {}
+ try {
+ f.getNow(null);
+ shouldThrow();
+ } catch (CancellationException success) {}
+ try {
+ f.get();
+ shouldThrow();
+ } catch (CancellationException success) {
+ } catch (Throwable fail) { threadUnexpectedException(fail); }
+
+ assertTrue(exceptionalCompletion(f) instanceof CancellationException);
+
+ assertTrue(f.isDone());
+ assertTrue(f.isCompletedExceptionally());
+ assertTrue(f.isCancelled());
+ assertTrue(f.toString().contains("[Completed exceptionally]"));
+ }
+
+ /**
+ * A newly constructed CompletableFuture is incomplete, as indicated
+ * by methods isDone, isCancelled, and getNow
+ */
+ public void testConstructor() {
+ CompletableFuture<Integer> f = new CompletableFuture<>();
+ checkIncomplete(f);
+ }
+
+ /**
+ * complete completes normally, as indicated by methods isDone,
+ * isCancelled, join, get, and getNow
+ */
+ public void testComplete() {
+ for (Integer v1 : new Integer[] { 1, null })
+ {
+ CompletableFuture<Integer> f = new CompletableFuture<>();
+ checkIncomplete(f);
+ assertTrue(f.complete(v1));
+ assertFalse(f.complete(v1));
+ checkCompletedNormally(f, v1);
+ }}
+
+ /**
+ * completeExceptionally completes exceptionally, as indicated by
+ * methods isDone, isCancelled, join, get, and getNow
+ */
+ public void testCompleteExceptionally() {
+ CompletableFuture<Integer> f = new CompletableFuture<>();
+ CFException ex = new CFException();
+ checkIncomplete(f);
+ f.completeExceptionally(ex);
+ checkCompletedExceptionally(f, ex);
+ }
+
+ /**
+ * cancel completes exceptionally and reports cancelled, as indicated by
+ * methods isDone, isCancelled, join, get, and getNow
+ */
+ public void testCancel() {
+ for (boolean mayInterruptIfRunning : new boolean[] { true, false })
+ {
+ CompletableFuture<Integer> f = new CompletableFuture<>();
+ checkIncomplete(f);
+ assertTrue(f.cancel(mayInterruptIfRunning));
+ assertTrue(f.cancel(mayInterruptIfRunning));
+ assertTrue(f.cancel(!mayInterruptIfRunning));
+ checkCancelled(f);
+ }}
+
+ /**
+ * obtrudeValue forces completion with given value
+ */
+ public void testObtrudeValue() {
+ CompletableFuture<Integer> f = new CompletableFuture<>();
+ checkIncomplete(f);
+ assertTrue(f.complete(one));
+ checkCompletedNormally(f, one);
+ f.obtrudeValue(three);
+ checkCompletedNormally(f, three);
+ f.obtrudeValue(two);
+ checkCompletedNormally(f, two);
+ f = new CompletableFuture<>();
+ f.obtrudeValue(three);
+ checkCompletedNormally(f, three);
+ f.obtrudeValue(null);
+ checkCompletedNormally(f, null);
+ f = new CompletableFuture<>();
+ f.completeExceptionally(new CFException());
+ f.obtrudeValue(four);
+ checkCompletedNormally(f, four);
+ }
+
+ /**
+ * obtrudeException forces completion with given exception
+ */
+ public void testObtrudeException() {
+ for (Integer v1 : new Integer[] { 1, null })
+ {
+ CFException ex;
+ CompletableFuture<Integer> f;
+
+ f = new CompletableFuture<>();
+ assertTrue(f.complete(v1));
+ for (int i = 0; i < 2; i++) {
+ f.obtrudeException(ex = new CFException());
+ checkCompletedExceptionally(f, ex);
+ }
+
+ f = new CompletableFuture<>();
+ for (int i = 0; i < 2; i++) {
+ f.obtrudeException(ex = new CFException());
+ checkCompletedExceptionally(f, ex);
+ }
+
+ f = new CompletableFuture<>();
+ f.completeExceptionally(ex = new CFException());
+ f.obtrudeValue(v1);
+ checkCompletedNormally(f, v1);
+ f.obtrudeException(ex = new CFException());
+ checkCompletedExceptionally(f, ex);
+ f.completeExceptionally(new CFException());
+ checkCompletedExceptionally(f, ex);
+ assertFalse(f.complete(v1));
+ checkCompletedExceptionally(f, ex);
+ }}
+
+ /**
+ * getNumberOfDependents returns number of dependent tasks
+ */
+ public void testGetNumberOfDependents() {
+ for (ExecutionMode m : ExecutionMode.values())
+ for (Integer v1 : new Integer[] { 1, null })
+ {
+ CompletableFuture<Integer> f = new CompletableFuture<>();
+ assertEquals(0, f.getNumberOfDependents());
+ final CompletableFuture<Void> g = m.thenRun(f, new Noop(m));
+ assertEquals(1, f.getNumberOfDependents());
+ assertEquals(0, g.getNumberOfDependents());
+ final CompletableFuture<Void> h = m.thenRun(f, new Noop(m));
+ assertEquals(2, f.getNumberOfDependents());
+ assertEquals(0, h.getNumberOfDependents());
+ assertTrue(f.complete(v1));
+ checkCompletedNormally(g, null);
+ checkCompletedNormally(h, null);
+ assertEquals(0, f.getNumberOfDependents());
+ assertEquals(0, g.getNumberOfDependents());
+ assertEquals(0, h.getNumberOfDependents());
+ }}
+
+ /**
+ * toString indicates current completion state
+ */
+ public void testToString() {
+ CompletableFuture<String> f;
+
+ f = new CompletableFuture<String>();
+ assertTrue(f.toString().contains("[Not completed]"));
+
+ assertTrue(f.complete("foo"));
+ assertTrue(f.toString().contains("[Completed normally]"));
+
+ f = new CompletableFuture<String>();
+ assertTrue(f.completeExceptionally(new IndexOutOfBoundsException()));
+ assertTrue(f.toString().contains("[Completed exceptionally]"));
+
+ for (boolean mayInterruptIfRunning : new boolean[] { true, false }) {
+ f = new CompletableFuture<String>();
+ assertTrue(f.cancel(mayInterruptIfRunning));
+ assertTrue(f.toString().contains("[Completed exceptionally]"));
+ }
+ }
+
+ /**
+ * completedFuture returns a completed CompletableFuture with given value
+ */
+ public void testCompletedFuture() {
+ CompletableFuture<String> f = CompletableFuture.completedFuture("test");
+ checkCompletedNormally(f, "test");
+ }
+
+ abstract class CheckedAction {
+ int invocationCount = 0;
+ final ExecutionMode m;
+ CheckedAction(ExecutionMode m) { this.m = m; }
+ void invoked() {
+ m.checkExecutionMode();
+ assertEquals(0, invocationCount++);
+ }
+ void assertNotInvoked() { assertEquals(0, invocationCount); }
+ void assertInvoked() { assertEquals(1, invocationCount); }
+ }
+
+ abstract class CheckedIntegerAction extends CheckedAction {
+ Integer value;
+ CheckedIntegerAction(ExecutionMode m) { super(m); }
+ void assertValue(Integer expected) {
+ assertInvoked();
+ assertEquals(expected, value);
+ }
+ }
+
+ class IntegerSupplier extends CheckedAction
+ implements Supplier<Integer>
+ {
+ final Integer value;
+ IntegerSupplier(ExecutionMode m, Integer value) {
+ super(m);
+ this.value = value;
+ }
+ public Integer get() {
+ invoked();
+ return value;
+ }
+ }
+
+ // A function that handles and produces null values as well.
+ static Integer inc(Integer x) {
+ return (x == null) ? null : x + 1;
+ }
+
+ class NoopConsumer extends CheckedIntegerAction
+ implements Consumer<Integer>
+ {
+ NoopConsumer(ExecutionMode m) { super(m); }
+ public void accept(Integer x) {
+ invoked();
+ value = x;
+ }
+ }
+
+ class IncFunction extends CheckedIntegerAction
+ implements Function<Integer,Integer>
+ {
+ IncFunction(ExecutionMode m) { super(m); }
+ public Integer apply(Integer x) {
+ invoked();
+ return value = inc(x);
+ }
+ }
+
+ // Choose non-commutative actions for better coverage
+ // A non-commutative function that handles and produces null values as well.
+ static Integer subtract(Integer x, Integer y) {
+ return (x == null && y == null) ? null :
+ ((x == null) ? 42 : x.intValue())
+ - ((y == null) ? 99 : y.intValue());
+ }
+
+ class SubtractAction extends CheckedIntegerAction
+ implements BiConsumer<Integer, Integer>
+ {
+ SubtractAction(ExecutionMode m) { super(m); }
+ public void accept(Integer x, Integer y) {
+ invoked();
+ value = subtract(x, y);
+ }
+ }
+
+ class SubtractFunction extends CheckedIntegerAction
+ implements BiFunction<Integer, Integer, Integer>
+ {
+ SubtractFunction(ExecutionMode m) { super(m); }
+ public Integer apply(Integer x, Integer y) {
+ invoked();
+ return value = subtract(x, y);
+ }
+ }
+
+ class Noop extends CheckedAction implements Runnable {
+ Noop(ExecutionMode m) { super(m); }
+ public void run() {
+ invoked();
+ }
+ }
+
+ class FailingSupplier extends CheckedAction
+ implements Supplier<Integer>
+ {
+ FailingSupplier(ExecutionMode m) { super(m); }
+ public Integer get() {
+ invoked();
+ throw new CFException();
+ }
+ }
+
+ class FailingConsumer extends CheckedIntegerAction
+ implements Consumer<Integer>
+ {
+ FailingConsumer(ExecutionMode m) { super(m); }
+ public void accept(Integer x) {
+ invoked();
+ value = x;
+ throw new CFException();
+ }
+ }
+
+ class FailingBiConsumer extends CheckedIntegerAction
+ implements BiConsumer<Integer, Integer>
+ {
+ FailingBiConsumer(ExecutionMode m) { super(m); }
+ public void accept(Integer x, Integer y) {
+ invoked();
+ value = subtract(x, y);
+ throw new CFException();
+ }
+ }
+
+ class FailingFunction extends CheckedIntegerAction
+ implements Function<Integer, Integer>
+ {
+ FailingFunction(ExecutionMode m) { super(m); }
+ public Integer apply(Integer x) {
+ invoked();
+ value = x;
+ throw new CFException();
+ }
+ }
+
+ class FailingBiFunction extends CheckedIntegerAction
+ implements BiFunction<Integer, Integer, Integer>
+ {
+ FailingBiFunction(ExecutionMode m) { super(m); }
+ public Integer apply(Integer x, Integer y) {
+ invoked();
+ value = subtract(x, y);
+ throw new CFException();
+ }
+ }
+
+ class FailingRunnable extends CheckedAction implements Runnable {
+ FailingRunnable(ExecutionMode m) { super(m); }
+ public void run() {
+ invoked();
+ throw new CFException();
+ }
+ }
+
+ class CompletableFutureInc extends CheckedIntegerAction
+ implements Function<Integer, CompletableFuture<Integer>>
+ {
+ CompletableFutureInc(ExecutionMode m) { super(m); }
+ public CompletableFuture<Integer> apply(Integer x) {
+ invoked();
+ value = x;
+ CompletableFuture<Integer> f = new CompletableFuture<>();
+ assertTrue(f.complete(inc(x)));
+ return f;
+ }
+ }
+
+ class FailingCompletableFutureFunction extends CheckedIntegerAction
+ implements Function<Integer, CompletableFuture<Integer>>
+ {
+ FailingCompletableFutureFunction(ExecutionMode m) { super(m); }
+ public CompletableFuture<Integer> apply(Integer x) {
+ invoked();
+ value = x;
+ throw new CFException();
+ }
+ }
+
+ // Used for explicit executor tests
+ static final class ThreadExecutor implements Executor {
+ final AtomicInteger count = new AtomicInteger(0);
+ static final ThreadGroup tg = new ThreadGroup("ThreadExecutor");
+ static boolean startedCurrentThread() {
+ return Thread.currentThread().getThreadGroup() == tg;
+ }
+
+ public void execute(Runnable r) {
+ count.getAndIncrement();
+ new Thread(tg, r).start();
+ }
+ }
+
+ static final boolean defaultExecutorIsCommonPool
+ = ForkJoinPool.getCommonPoolParallelism() > 1;
+
+ /**
+ * Permits the testing of parallel code for the 3 different
+ * execution modes without copy/pasting all the test methods.
+ */
+ enum ExecutionMode {
+ SYNC {
+ public void checkExecutionMode() {
+ assertFalse(ThreadExecutor.startedCurrentThread());
+ assertNull(ForkJoinTask.getPool());
+ }
+ public CompletableFuture<Void> runAsync(Runnable a) {
+ throw new UnsupportedOperationException();
+ }
+ public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) {
+ throw new UnsupportedOperationException();
+ }
+ public <T> CompletableFuture<Void> thenRun
+ (CompletableFuture<T> f, Runnable a) {
+ return f.thenRun(a);
+ }
+ public <T> CompletableFuture<Void> thenAccept
+ (CompletableFuture<T> f, Consumer<? super T> a) {
+ return f.thenAccept(a);
+ }
+ public <T,U> CompletableFuture<U> thenApply
+ (CompletableFuture<T> f, Function<? super T,U> a) {
+ return f.thenApply(a);
+ }
+ public <T,U> CompletableFuture<U> thenCompose
+ (CompletableFuture<T> f,
+ Function<? super T,? extends CompletionStage<U>> a) {
+ return f.thenCompose(a);
+ }
+ public <T,U> CompletableFuture<U> handle
+ (CompletableFuture<T> f,
+ BiFunction<? super T,Throwable,? extends U> a) {
+ return f.handle(a);
+ }
+ public <T> CompletableFuture<T> whenComplete
+ (CompletableFuture<T> f,
+ BiConsumer<? super T,? super Throwable> a) {
+ return f.whenComplete(a);
+ }
+ public <T,U> CompletableFuture<Void> runAfterBoth
+ (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
+ return f.runAfterBoth(g, a);
+ }
+ public <T,U> CompletableFuture<Void> thenAcceptBoth
+ (CompletableFuture<T> f,
+ CompletionStage<? extends U> g,
+ BiConsumer<? super T,? super U> a) {
+ return f.thenAcceptBoth(g, a);
+ }
+ public <T,U,V> CompletableFuture<V> thenCombine
+ (CompletableFuture<T> f,
+ CompletionStage<? extends U> g,
+ BiFunction<? super T,? super U,? extends V> a) {
+ return f.thenCombine(g, a);
+ }
+ public <T> CompletableFuture<Void> runAfterEither
+ (CompletableFuture<T> f,
+ CompletionStage<?> g,
+ java.lang.Runnable a) {
+ return f.runAfterEither(g, a);
+ }
+ public <T> CompletableFuture<Void> acceptEither
+ (CompletableFuture<T> f,
+ CompletionStage<? extends T> g,
+ Consumer<? super T> a) {
+ return f.acceptEither(g, a);
+ }
+ public <T,U> CompletableFuture<U> applyToEither
+ (CompletableFuture<T> f,
+ CompletionStage<? extends T> g,
+ Function<? super T,U> a) {
+ return f.applyToEither(g, a);
+ }
+ },
+
+ ASYNC {
+ public void checkExecutionMode() {
+ assertEquals(defaultExecutorIsCommonPool,
+ (ForkJoinPool.commonPool() == ForkJoinTask.getPool()));
+ }
+ public CompletableFuture<Void> runAsync(Runnable a) {
+ return CompletableFuture.runAsync(a);
+ }
+ public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) {
+ return CompletableFuture.supplyAsync(a);
+ }
+ public <T> CompletableFuture<Void> thenRun
+ (CompletableFuture<T> f, Runnable a) {
+ return f.thenRunAsync(a);
+ }
+ public <T> CompletableFuture<Void> thenAccept
+ (CompletableFuture<T> f, Consumer<? super T> a) {
+ return f.thenAcceptAsync(a);
+ }
+ public <T,U> CompletableFuture<U> thenApply
+ (CompletableFuture<T> f, Function<? super T,U> a) {
+ return f.thenApplyAsync(a);
+ }
+ public <T,U> CompletableFuture<U> thenCompose
+ (CompletableFuture<T> f,
+ Function<? super T,? extends CompletionStage<U>> a) {
+ return f.thenComposeAsync(a);
+ }
+ public <T,U> CompletableFuture<U> handle
+ (CompletableFuture<T> f,
+ BiFunction<? super T,Throwable,? extends U> a) {
+ return f.handleAsync(a);
+ }
+ public <T> CompletableFuture<T> whenComplete
+ (CompletableFuture<T> f,
+ BiConsumer<? super T,? super Throwable> a) {
+ return f.whenCompleteAsync(a);
+ }
+ public <T,U> CompletableFuture<Void> runAfterBoth
+ (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
+ return f.runAfterBothAsync(g, a);
+ }
+ public <T,U> CompletableFuture<Void> thenAcceptBoth
+ (CompletableFuture<T> f,
+ CompletionStage<? extends U> g,
+ BiConsumer<? super T,? super U> a) {
+ return f.thenAcceptBothAsync(g, a);
+ }
+ public <T,U,V> CompletableFuture<V> thenCombine
+ (CompletableFuture<T> f,
+ CompletionStage<? extends U> g,
+ BiFunction<? super T,? super U,? extends V> a) {
+ return f.thenCombineAsync(g, a);
+ }
+ public <T> CompletableFuture<Void> runAfterEither
+ (CompletableFuture<T> f,
+ CompletionStage<?> g,
+ java.lang.Runnable a) {
+ return f.runAfterEitherAsync(g, a);
+ }
+ public <T> CompletableFuture<Void> acceptEither
+ (CompletableFuture<T> f,
+ CompletionStage<? extends T> g,
+ Consumer<? super T> a) {
+ return f.acceptEitherAsync(g, a);
+ }
+ public <T,U> CompletableFuture<U> applyToEither
+ (CompletableFuture<T> f,
+ CompletionStage<? extends T> g,
+ Function<? super T,U> a) {
+ return f.applyToEitherAsync(g, a);
+ }
+ },
+
+ EXECUTOR {
+ public void checkExecutionMode() {
+ assertTrue(ThreadExecutor.startedCurrentThread());
+ }
+ public CompletableFuture<Void> runAsync(Runnable a) {
+ return CompletableFuture.runAsync(a, new ThreadExecutor());
+ }
+ public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) {
+ return CompletableFuture.supplyAsync(a, new ThreadExecutor());
+ }
+ public <T> CompletableFuture<Void> thenRun
+ (CompletableFuture<T> f, Runnable a) {
+ return f.thenRunAsync(a, new ThreadExecutor());
+ }
+ public <T> CompletableFuture<Void> thenAccept
+ (CompletableFuture<T> f, Consumer<? super T> a) {
+ return f.thenAcceptAsync(a, new ThreadExecutor());
+ }
+ public <T,U> CompletableFuture<U> thenApply
+ (CompletableFuture<T> f, Function<? super T,U> a) {
+ return f.thenApplyAsync(a, new ThreadExecutor());
+ }
+ public <T,U> CompletableFuture<U> thenCompose
+ (CompletableFuture<T> f,
+ Function<? super T,? extends CompletionStage<U>> a) {
+ return f.thenComposeAsync(a, new ThreadExecutor());
+ }
+ public <T,U> CompletableFuture<U> handle
+ (CompletableFuture<T> f,
+ BiFunction<? super T,Throwable,? extends U> a) {
+ return f.handleAsync(a, new ThreadExecutor());
+ }
+ public <T> CompletableFuture<T> whenComplete
+ (CompletableFuture<T> f,
+ BiConsumer<? super T,? super Throwable> a) {
+ return f.whenCompleteAsync(a, new ThreadExecutor());
+ }
+ public <T,U> CompletableFuture<Void> runAfterBoth
+ (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
+ return f.runAfterBothAsync(g, a, new ThreadExecutor());
+ }
+ public <T,U> CompletableFuture<Void> thenAcceptBoth
+ (CompletableFuture<T> f,
+ CompletionStage<? extends U> g,
+ BiConsumer<? super T,? super U> a) {
+ return f.thenAcceptBothAsync(g, a, new ThreadExecutor());
+ }
+ public <T,U,V> CompletableFuture<V> thenCombine
+ (CompletableFuture<T> f,
+ CompletionStage<? extends U> g,
+ BiFunction<? super T,? super U,? extends V> a) {
+ return f.thenCombineAsync(g, a, new ThreadExecutor());
+ }
+ public <T> CompletableFuture<Void> runAfterEither
+ (CompletableFuture<T> f,
+ CompletionStage<?> g,
+ java.lang.Runnable a) {
+ return f.runAfterEitherAsync(g, a, new ThreadExecutor());
+ }
+ public <T> CompletableFuture<Void> acceptEither
+ (CompletableFuture<T> f,
+ CompletionStage<? extends T> g,
+ Consumer<? super T> a) {
+ return f.acceptEitherAsync(g, a, new ThreadExecutor());
+ }
+ public <T,U> CompletableFuture<U> applyToEither
+ (CompletableFuture<T> f,
+ CompletionStage<? extends T> g,
+ Function<? super T,U> a) {
+ return f.applyToEitherAsync(g, a, new ThreadExecutor());
+ }
+ };
+
+ public abstract void checkExecutionMode();
+ public abstract CompletableFuture<Void> runAsync(Runnable a);
+ public abstract <U> CompletableFuture<U> supplyAsync(Supplier<U> a);
+ public abstract <T> CompletableFuture<Void> thenRun
+ (CompletableFuture<T> f, Runnable a);
+ public abstract <T> CompletableFuture<Void> thenAccept
+ (CompletableFuture<T> f, Consumer<? super T> a);
+ public abstract <T,U> CompletableFuture<U> thenApply
+ (CompletableFuture<T> f, Function<? super T,U> a);
+ public abstract <T,U> CompletableFuture<U> thenCompose
+ (CompletableFuture<T> f,
+ Function<? super T,? extends CompletionStage<U>> a);
+ public abstract <T,U> CompletableFuture<U> handle
+ (CompletableFuture<T> f,
+ BiFunction<? super T,Throwable,? extends U> a);
+ public abstract <T> CompletableFuture<T> whenComplete
+ (CompletableFuture<T> f,
+ BiConsumer<? super T,? super Throwable> a);
+ public abstract <T,U> CompletableFuture<Void> runAfterBoth
+ (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a);
+ public abstract <T,U> CompletableFuture<Void> thenAcceptBoth
+ (CompletableFuture<T> f,
+ CompletionStage<? extends U> g,
+ BiConsumer<? super T,? super U> a);
+ public abstract <T,U,V> CompletableFuture<V> thenCombine
+ (CompletableFuture<T> f,
+ CompletionStage<? extends U> g,
+ BiFunction<? super T,? super U,? extends V> a);
+ public abstract <T> CompletableFuture<Void> runAfterEither
+ (CompletableFuture<T> f,
+ CompletionStage<?> g,
+ java.lang.Runnable a);
+ public abstract <T> CompletableFuture<Void> acceptEither
+ (CompletableFuture<T> f,
+ CompletionStage<? extends T> g,
+ Consumer<? super T> a);
+ public abstract <T,U> CompletableFuture<U> applyToEither
+ (CompletableFuture<T> f,
+ CompletionStage<? extends T> g,
+ Function<? super T,U> a);
+ }
+
+ /**
+ * exceptionally action is not invoked when source completes
+ * normally, and source result is propagated
+ */
+ public void testExceptionally_normalCompletion() {
+ for (boolean createIncomplete : new boolean[] { true, false })
+ for (Integer v1 : new Integer[] { 1, null })
+ {
+ final AtomicInteger a = new AtomicInteger(0);
+ final CompletableFuture<Integer> f = new CompletableFuture<>();
+ if (!createIncomplete) assertTrue(f.complete(v1));
+ final CompletableFuture<Integer> g = f.exceptionally
+ ((Throwable t) -> {
+ a.getAndIncrement();
+ threadFail("should not be called");
+ return null; // unreached
+ });
+ if (createIncomplete) assertTrue(f.complete(v1));
+
+ checkCompletedNormally(g, v1);
+ checkCompletedNormally(f, v1);
+ assertEquals(0, a.get());
+ }}
+
+ /**
+ * exceptionally action completes with function value on source
+ * exception
+ */
+ public void testExceptionally_exceptionalCompletion() {
+ for (boolean createIncomplete : new boolean[] { true, false })
+ for (Integer v1 : new Integer[] { 1, null })
+ {
+ final AtomicInteger a = new AtomicInteger(0);
+ final CFException ex = new CFException();
+ final CompletableFuture<Integer> f = new CompletableFuture<>();
+ if (!createIncomplete) f.completeExceptionally(ex);
+ final CompletableFuture<Integer> g = f.exceptionally
+ ((Throwable t) -> {
+ ExecutionMode.SYNC.checkExecutionMode();
+ threadAssertSame(t, ex);
+ a.getAndIncrement();
+ return v1;
+ });
+ if (createIncomplete) f.completeExceptionally(ex);
+
+ checkCompletedNormally(g, v1);
+ assertEquals(1, a.get());
+ }}
+
+ /**
+ * If an "exceptionally action" throws an exception, it completes
+ * exceptionally with that exception
+ */
+ public void testExceptionally_exceptionalCompletionActionFailed() {
+ for (boolean createIncomplete : new boolean[] { true, false })
+ {
+ final AtomicInteger a = new AtomicInteger(0);
+ final CFException ex1 = new CFException();
+ final CFException ex2 = new CFException();
+ final CompletableFuture<Integer> f = new CompletableFuture<>();
+ if (!createIncomplete) f.completeExceptionally(ex1);
+ final CompletableFuture<Integer> g = f.exceptionally
+ ((Throwable t) -> {
+ ExecutionMode.SYNC.checkExecutionMode();
+ threadAssertSame(t, ex1);
+ a.getAndIncrement();
+ throw ex2;
+ });
+ if (createIncomplete) f.completeExceptionally(ex1);
+
+ checkCompletedWithWrappedException(g, ex2);
+ checkCompletedExceptionally(f, ex1);
+ assertEquals(1, a.get());
+ }}
+
+ /**
+ * whenComplete action executes on normal completion, propagating
+ * source result.
+ */
+ public void testWhenComplete_normalCompletion() {
+ for (ExecutionMode m : ExecutionMode.values())
+ for (boolean createIncomplete : new boolean[] { true, false })
+ for (Integer v1 : new Integer[] { 1, null })
+ {
+ final AtomicInteger a = new AtomicInteger(0);
+ final CompletableFuture<Integer> f = new CompletableFuture<>();
+ if (!createIncomplete) assertTrue(f.complete(v1));
+ final CompletableFuture<Integer> g = m.whenComplete
+ (f,
+ (Integer result, Throwable t) -> {
+ m.checkExecutionMode();
+ threadAssertSame(result, v1);
+ threadAssertNull(t);
+ a.getAndIncrement();
+ });
+ if (createIncomplete) assertTrue(f.complete(v1));
+
+ checkCompletedNormally(g, v1);
+ checkCompletedNormally(f, v1);
+ assertEquals(1, a.get());
+ }}
+
+ /**
+ * whenComplete action executes on exceptional completion, propagating
+ * source result.
+ */
+ public void testWhenComplete_exceptionalCompletion() {
+ for (ExecutionMode m : ExecutionMode.values())
+ for (boolean createIncomplete : new boolean[] { true, false })
+ {
+ final AtomicInteger a = new AtomicInteger(0);
+ final CFException ex = new CFException();
+ final CompletableFuture<Integer> f = new CompletableFuture<>();
+ if (!createIncomplete) f.completeExceptionally(ex);
+ final CompletableFuture<Integer> g = m.whenComplete
+ (f,
+ (Integer result, Throwable t) -> {
+ m.checkExecutionMode();
+ threadAssertNull(result);
+ threadAssertSame(t, ex);
+ a.getAndIncrement();
+ });
+ if (createIncomplete) f.completeExceptionally(ex);
+
+ checkCompletedWithWrappedException(g, ex);
+ checkCompletedExceptionally(f, ex);
+ assertEquals(1, a.get());
+ }}
+
+ /**
+ * whenComplete action executes on cancelled source, propagating
+ * CancellationException.
+ */
+ public void testWhenComplete_sourceCancelled() {
+ for (ExecutionMode m : ExecutionMode.values())
+ for (boolean mayInterruptIfRunning : new boolean[] { true, false })
+ for (boolean createIncomplete : new boolean[] { true, false })
+ {
+ final AtomicInteger a = new AtomicInteger(0);
+ final CompletableFuture<Integer> f = new CompletableFuture<>();
+ if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
+ final CompletableFuture<Integer> g = m.whenComplete
+ (f,
+ (Integer result, Throwable t) -> {
+ m.checkExecutionMode();
+ threadAssertNull(result);
+ threadAssertTrue(t instanceof CancellationException);
+ a.getAndIncrement();
+ });
+ if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
+
+ checkCompletedWithWrappedCancellationException(g);
+ checkCancelled(f);
+ assertEquals(1, a.get());
+ }}
+
+ /**
+ * If a whenComplete action throws an exception when triggered by
+ * a normal completion, it completes exceptionally
+ */
+ public void testWhenComplete_sourceCompletedNormallyActionFailed() {
+ for (boolean createIncomplete : new boolean[] { true, false })
+ for (ExecutionMode m : ExecutionMode.values())
+ for (Integer v1 : new Integer[] { 1, null })
+ {
+ final AtomicInteger a = new AtomicInteger(0);
+ final CFException ex = new CFException();
+ final CompletableFuture<Integer> f = new CompletableFuture<>();
+ if (!createIncomplete) assertTrue(f.complete(v1));
+ final CompletableFuture<Integer> g = m.whenComplete
+ (f,
+ (Integer result, Throwable t) -> {
+ m.checkExecutionMode();
+ threadAssertSame(result, v1);
+ threadAssertNull(t);
+ a.getAndIncrement();
+ throw ex;
+ });
+ if (createIncomplete) assertTrue(f.complete(v1));
+
+ checkCompletedWithWrappedException(g, ex);
+ checkCompletedNormally(f, v1);
+ assertEquals(1, a.get());
+ }}
+
+ /**
+ * If a whenComplete action throws an exception when triggered by
+ * a source completion that also throws an exception, the source
+ * exception takes precedence (unlike handle)
+ */
+ public void testWhenComplete_sourceFailedActionFailed() {
+ for (boolean createIncomplete : new boolean[] { true, false })
+ for (ExecutionMode m : ExecutionMode.values())
+ {
+ final AtomicInteger a = new AtomicInteger(0);
+ final CFException ex1 = new CFException();
+ final CFException ex2 = new CFException();
+ final CompletableFuture<Integer> f = new CompletableFuture<>();
+
+ if (!createIncomplete) f.completeExceptionally(ex1);
+ final CompletableFuture<Integer> g = m.whenComplete
+ (f,
+ (Integer result, Throwable t) -> {
+ m.checkExecutionMode();
+ threadAssertSame(t, ex1);
+ threadAssertNull(result);
+ a.getAndIncrement();
+ throw ex2;
+ });
+ if (createIncomplete) f.completeExceptionally(ex1);
+
+ checkCompletedWithWrappedException(g, ex1);
+ checkCompletedExceptionally(f, ex1);
+ if (testImplementationDetails) {
+ assertEquals(1, ex1.getSuppressed().length);
+ assertSame(ex2, ex1.getSuppressed()[0]);
+ }
+ assertEquals(1, a.get());
+ }}
+
+ /**
+ * handle action completes normally with function value on normal
+ * completion of source
+ */
+ public void testHandle_normalCompletion() {
+ for (ExecutionMode m : ExecutionMode.values())
+ for (boolean createIncomplete : new boolean[] { true, false })
+ for (Integer v1 : new Integer[] { 1, null })
+ {
+ final CompletableFuture<Integer> f = new CompletableFuture<>();
+ final AtomicInteger a = new AtomicInteger(0);
+ if (!createIncomplete) assertTrue(f.complete(v1));
+ final CompletableFuture<Integer> g = m.handle
+ (f,
+ (Integer result, Throwable t) -> {
+ m.checkExecutionMode();
+ threadAssertSame(result, v1);
+ threadAssertNull(t);
+ a.getAndIncrement();
+ return inc(v1);
+ });
+ if (createIncomplete) assertTrue(f.complete(v1));
+
+ checkCompletedNormally(g, inc(v1));
+ checkCompletedNormally(f, v1);
+ assertEquals(1, a.get());
+ }}
+
+ /**
+ * handle action completes normally with function value on
+ * exceptional completion of source
+ */
+ public void testHandle_exceptionalCompletion() {
+ for (ExecutionMode m : ExecutionMode.values())
+ for (boolean createIncomplete : new boolean[] { true, false })
+ for (Integer v1 : new Integer[] { 1, null })
+ {
+ final CompletableFuture<Integer> f = new CompletableFuture<>();
+ final AtomicInteger a = new AtomicInteger(0);
+ final CFException ex = new CFException();
+ if (!createIncomplete) f.completeExceptionally(ex);
+ final CompletableFuture<Integer> g = m.handle
+ (f,
+ (Integer result, Throwable t) -> {
+ m.checkExecutionMode();
+ threadAssertNull(result);
+ threadAssertSame(t, ex);
+ a.getAndIncrement();
+ return v1;
+ });
+ if (createIncomplete) f.completeExceptionally(ex);
+
+ checkCompletedNormally(g, v1);
+ checkCompletedExceptionally(f, ex);
+ assertEquals(1, a.get());
+ }}
+
+ /**
+ * handle action completes normally with function value on
+ * cancelled source
+ */
+ public void testHandle_sourceCancelled() {
+ for (ExecutionMode m : ExecutionMode.values())
+ for (boolean mayInterruptIfRunning : new boolean[] { true, false })
+ for (boolean createIncomplete : new boolean[] { true, false })
+ for (Integer v1 : new Integer[] { 1, null })
+ {
+ final CompletableFuture<Integer> f = new CompletableFuture<>();
+ final AtomicInteger a = new AtomicInteger(0);
+ if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
+ final CompletableFuture<Integer> g = m.handle
+ (f,
+ (Integer result, Throwable t) -> {
+ m.checkExecutionMode();
+ threadAssertNull(result);
+ threadAssertTrue(t instanceof CancellationException);
+ a.getAndIncrement();
+ return v1;
+ });
+ if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
+
+ checkCompletedNormally(g, v1);
+ checkCancelled(f);
+ assertEquals(1, a.get());
+ }}
+
+ /**
+ * If a "handle action" throws an exception when triggered by
+ * a normal completion, it completes exceptionally
+ */
+ public void testHandle_sourceCompletedNormallyActionFailed() {
+ for (ExecutionMode m : ExecutionMode.values())
+ for (boolean createIncomplete : new boolean[] { true, false })
+ for (Integer v1 : new Integer[] { 1, null })
+ {
+ final CompletableFuture<Integer> f = new CompletableFuture<>();
+ final AtomicInteger a = new AtomicInteger(0);
+ final CFException ex = new CFException();
+ if (!createIncomplete) assertTrue(f.complete(v1));
+ final CompletableFuture<Integer> g = m.handle
+ (f,
+ (Integer result, Throwable t) -> {
+ m.checkExecutionMode();
+ threadAssertSame(result, v1);
+ threadAssertNull(t);
+ a.getAndIncrement();
+ throw ex;
+ });
+ if (createIncomplete) assertTrue(f.complete(v1));
+
+ checkCompletedWithWrappedException(g, ex);
+ checkCompletedNormally(f, v1);
+ assertEquals(1, a.get());
+ }}
+
+ /**
+ * If a "handle action" throws an exception when triggered by
+ * a source completion that also throws an exception, the action
+ * exception takes precedence (unlike whenComplete)
+ */
+ public void testHandle_sourceFailedActionFailed() {
+ for (boolean createIncomplete : new boolean[] { true, false })
+ for (ExecutionMode m : ExecutionMode.values())
+ {
+ final AtomicInteger a = new AtomicInteger(0);
+ final CFException ex1 = new CFException();
+ final CFException ex2 = new CFException();
+ final CompletableFuture<Integer> f = new CompletableFuture<>();
+
+ if (!createIncomplete) f.completeExceptionally(ex1);
+ final CompletableFuture<Integer> g = m.handle
+ (f,
+ (Integer result, Throwable t) -> {
+ m.checkExecutionMode();
+ threadAssertNull(result);
+ threadAssertSame(ex1, t);
+ a.getAndIncrement();
+ throw ex2;
+ });
+ if (createIncomplete) f.completeExceptionally(ex1);
+
+ checkCompletedWithWrappedException(g, ex2);
+ checkCompletedExceptionally(f, ex1);
+ assertEquals(1, a.get());
+ }}
+
+ /**
+ * runAsync completes after running Runnable
+ */
+ public void testRunAsync_normalCompletion() {
+ ExecutionMode[] executionModes = {
+ ExecutionMode.ASYNC,
+ ExecutionMode.EXECUTOR,
+ };
+ for (ExecutionMode m : executionModes)
+ {
+ final Noop r = new Noop(m);
+ final CompletableFuture<Void> f = m.runAsync(r);
+ assertNull(f.join());
+ checkCompletedNormally(f, null);
+ r.assertInvoked();
+ }}
+
+ /**
+ * failing runAsync completes exceptionally after running Runnable
+ */
+ public void testRunAsync_exceptionalCompletion() {
+ ExecutionMode[] executionModes = {
+ ExecutionMode.ASYNC,
+ ExecutionMode.EXECUTOR,
+ };
+ for (ExecutionMode m : executionModes)
+ {
+ final FailingRunnable r = new FailingRunnable(m);
+ final CompletableFuture<Void> f = m.runAsync(r);
+ checkCompletedWithWrappedCFException(f);
+ r.assertInvoked();
+ }}
+
+ /**
+ * supplyAsync completes with result of supplier
+ */
+ public void testSupplyAsync_normalCompletion() {
+ ExecutionMode[] executionModes = {
+ ExecutionMode.ASYNC,
+ ExecutionMode.EXECUTOR,
+ };
+ for (ExecutionMode m : executionModes)
+ for (Integer v1 : new Integer[] { 1, null })
+ {
+ final IntegerSupplier r = new IntegerSupplier(m, v1);
+ final CompletableFuture<Integer> f = m.supplyAsync(r);
+ assertSame(v1, f.join());
+ checkCompletedNormally(f, v1);
+ r.assertInvoked();
+ }}
+
+ /**
+ * Failing supplyAsync completes exceptionally
+ */
+ public void testSupplyAsync_exceptionalCompletion() {
+ ExecutionMode[] executionModes = {
+ ExecutionMode.ASYNC,
+ ExecutionMode.EXECUTOR,
+ };
+ for (ExecutionMode m : executionModes)
+ {
+ FailingSupplier r = new FailingSupplier(m);
+ CompletableFuture<Integer> f = m.supplyAsync(r);
+ checkCompletedWithWrappedCFException(f);
+ r.assertInvoked();
+ }}
+
+ // seq completion methods
+
+ /**
+ * thenRun result completes normally after normal completion of source
+ */
+ public void testThenRun_normalCompletion() {
+ for (ExecutionMode m : ExecutionMode.values())
+ for (Integer v1 : new Integer[] { 1, null })
+ {
+ final CompletableFuture<Integer> f = new CompletableFuture<>();
+ final Noop[] rs = new Noop[6];
+ for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
+
+ final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
+ final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
+ final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
+ checkIncomplete(h0);
+ checkIncomplete(h1);
+ checkIncomplete(h2);
+ assertTrue(f.complete(v1));
+ final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
+ final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
+ final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
+
+ checkCompletedNormally(h0, null);
+ checkCompletedNormally(h1, null);
+ checkCompletedNormally(h2, null);
+ checkCompletedNormally(h3, null);
+ checkCompletedNormally(h4, null);
+ checkCompletedNormally(h5, null);
+ checkCompletedNormally(f, v1);
+ for (Noop r : rs) r.assertInvoked();
+ }}
+
+ /**
+ * thenRun result completes exceptionally after exceptional
+ * completion of source
+ */
+ public void testThenRun_exceptionalCompletion() {
+ for (ExecutionMode m : ExecutionMode.values())
+ {
+ final CFException ex = new CFException();
+ final CompletableFuture<Integer> f = new CompletableFuture<>();
+ final Noop[] rs = new Noop[6];
+ for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
+
+ final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
+ final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
+ final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
+ checkIncomplete(h0);
+ checkIncomplete(h1);
+ checkIncomplete(h2);
+ assertTrue(f.completeExceptionally(ex));
+ final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
+ final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
+ final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
+
+ checkCompletedWithWrappedException(h0, ex);
+ checkCompletedWithWrappedException(h1, ex);
+ checkCompletedWithWrappedException(h2, ex);
+ checkCompletedWithWrappedException(h3, ex);
+ checkCompletedWithWrappedException(h4, ex);
+ checkCompletedWithWrappedException(h5, ex);
+ checkCompletedExceptionally(f, ex);
+ for (Noop r : rs) r.assertNotInvoked();
+ }}
+
+ /**
+ * thenRun result completes exceptionally if source cancelled
+ */
+ public void testThenRun_sourceCancelled() {
+ for (ExecutionMode m : ExecutionMode.values())
+ for (boolean mayInterruptIfRunning : new boolean[] { true, false })
+ {
+ final CompletableFuture<Integer> f = new CompletableFuture<>();
+ final Noop[] rs = new Noop[6];
+ for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
+
+ final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
+ final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
+ final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
+ checkIncomplete(h0);
+ checkIncomplete(h1);
+ checkIncomplete(h2);
+ assertTrue(f.cancel(mayInterruptIfRunning));
+ final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
+ final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
+ final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
+
+ checkCompletedWithWrappedCancellationException(h0);
+ checkCompletedWithWrappedCancellationException(h1);
+ checkCompletedWithWrappedCancellationException(h2);
+ checkCompletedWithWrappedCancellationException(h3);
+ checkCompletedWithWrappedCancellationException(h4);
+ checkCompletedWithWrappedCancellationException(h5);
+ checkCancelled(f);
+ for (Noop r : rs) r.assertNotInvoked();
+ }}
+
+ /**
+ * thenRun result completes exceptionally if action does
+ */
+ public void testThenRun_actionFailed() {
+ for (ExecutionMode m : ExecutionMode.values())
+ for (Integer v1 : new Integer[] { 1, null })
+ {
+ final CompletableFuture<Integer> f = new CompletableFuture<>();
+ final FailingRunnable[] rs = new FailingRunnable[6];
+ for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m);
+
+ final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
+ final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
+ final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
+ assertTrue(f.complete(v1));
+ final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
+ final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
+ final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
+
+ checkCompletedWithWrappedCFException(h0);
+ checkCompletedWithWrappedCFException(h1);
+ checkCompletedWithWrappedCFException(h2);
+ checkCompletedWithWrappedCFException(h3);
+ checkCompletedWithWrappedCFException(h4);
+ checkCompletedWithWrappedCFException(h5);
+ checkCompletedNormally(f, v1);
+ }}
+
+ /**
+ * thenApply result completes normally after normal completion of source
+ */
+ public void testThenApply_normalCompletion() {
+ for (ExecutionMode m : ExecutionMode.values())
+ for (Integer v1 : new Integer[] { 1, null })
+ {
+ final CompletableFuture<Integer> f = new CompletableFuture<>();
+ final IncFunction[] rs = new IncFunction[4];
+ for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
+
+ final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
+ final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
+ checkIncomplete(h0);
+ checkIncomplete(h1);
+ assertTrue(f.complete(v1));
+ final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
+ final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
+
+ checkCompletedNormally(h0, inc(v1));
+ checkCompletedNormally(h1, inc(v1));
+ checkCompletedNormally(h2, inc(v1));
+ checkCompletedNormally(h3, inc(v1));
+ checkCompletedNormally(f, v1);
+ for (IncFunction r : rs) r.assertValue(inc(v1));
+ }}
+
+ /**
+ * thenApply result completes exceptionally after exceptional
+ * completion of source
+ */
+ public void testThenApply_exceptionalCompletion() {
+ for (ExecutionMode m : ExecutionMode.values())
+ {
+ final CFException ex = new CFException();
+ final CompletableFuture<Integer> f = new CompletableFuture<>();
+ final IncFunction[] rs = new IncFunction[4];
+ for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
+
+ final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
+ final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
+ assertTrue(f.completeExceptionally(ex));
+ final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
+ final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
+
+ checkCompletedWithWrappedException(h0, ex);
+ checkCompletedWithWrappedException(h1, ex);
+ checkCompletedWithWrappedException(h2, ex);
+ checkCompletedWithWrappedException(h3, ex);
+ checkCompletedExceptionally(f, ex);
+ for (IncFunction r : rs) r.assertNotInvoked();
+ }}
+
+ /**
+ * thenApply result completes exceptionally if source cancelled
+ */
+ public void testThenApply_sourceCancelled() {
+ for (ExecutionMode m : ExecutionMode.values())
+ for (boolean mayInterruptIfRunning : new boolean[] { true, false })
+ {
+ final CompletableFuture<Integer> f = new CompletableFuture<>();
+ final IncFunction[] rs = new IncFunction[4];
+ for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
+
+ final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
+ final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
+ assertTrue(f.cancel(mayInterruptIfRunning));
+ final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
+ final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
+
+ checkCompletedWithWrappedCancellationException(h0);
+ checkCompletedWithWrappedCancellationException(h1);
+ checkCompletedWithWrappedCancellationException(h2);
+ checkCompletedWithWrappedCancellationException(h3);
+ checkCancelled(f);
+ for (IncFunction r : rs) r.assertNotInvoked();
+ }}
+
+ /**
+ * thenApply result completes exceptionally if action does
+ */
+ public void testThenApply_actionFailed() {
+ for (ExecutionMode m : ExecutionMode.values())
+ for (Integer v1 : new Integer[] { 1, null })
+ {
+ final CompletableFuture<Integer> f = new CompletableFuture<>();
+ final FailingFunction[] rs = new FailingFunction[4];
+ for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m);
+
+ final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
+ final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
+ assertTrue(f.complete(v1));
+ final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
+ final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
+
+ checkCompletedWithWrappedCFException(h0);
+ checkCompletedWithWrappedCFException(h1);
+ checkCompletedWithWrappedCFException(h2);
+ checkCompletedWithWrappedCFException(h3);
+ checkCompletedNormally(f, v1);
+ }}
+
+ /**
+ * thenAccept result completes normally after normal completion of source
+ */
+ public void testThenAccept_normalCompletion() {
+ for (ExecutionMode m : ExecutionMode.values())
+ for (Integer v1 : new Integer[] { 1, null })
+ {
+ final CompletableFuture<Integer> f = new CompletableFuture<>();
+ final NoopConsumer[] rs = new NoopConsumer[4];
+ for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
+
+ final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
+ final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
+ checkIncomplete(h0);
+ checkIncomplete(h1);
+ assertTrue(f.complete(v1));
+ final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
+ final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
+
+ checkCompletedNormally(h0, null);
+ checkCompletedNormally(h1, null);
+ checkCompletedNormally(h2, null);
+ checkCompletedNormally(h3, null);
+ checkCompletedNormally(f, v1);
+ for (NoopConsumer r : rs) r.assertValue(v1);
+ }}
+
+ /**
+ * thenAccept result completes exceptionally after exceptional
+ * completion of source
+ */
+ public void testThenAccept_exceptionalCompletion() {
+ for (ExecutionMode m : ExecutionMode.values())
+ {
+ final CFException ex = new CFException();
+ final CompletableFuture<Integer> f = new CompletableFuture<>();
+ final NoopConsumer[] rs = new NoopConsumer[4];
+ for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
+
+ final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
+ final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
+ assertTrue(f.completeExceptionally(ex));
+ final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
+ final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
+
+ checkCompletedWithWrappedException(h0, ex);
+ checkCompletedWithWrappedException(h1, ex);
+ checkCompletedWithWrappedException(h2, ex);
+ checkCompletedWithWrappedException(h3, ex);
+ checkCompletedExceptionally(f, ex);
+ for (NoopConsumer r : rs) r.assertNotInvoked();
+ }}
+
+ /**
+ * thenAccept result completes exceptionally if source cancelled
+ */
+ public void testThenAccept_sourceCancelled() {
+ for (ExecutionMode m : ExecutionMode.values())
+ for (boolean mayInterruptIfRunning : new boolean[] { true, false })
+ {
+ final CompletableFuture<Integer> f = new CompletableFuture<>();
+ final NoopConsumer[] rs = new NoopConsumer[4];
+ for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
+
+ final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
+ final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
+ assertTrue(f.cancel(mayInterruptIfRunning));
+ final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
+ final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
+
+ checkCompletedWithWrappedCancellationException(h0);
+ checkCompletedWithWrappedCancellationException(h1);
+ checkCompletedWithWrappedCancellationException(h2);
+ checkCompletedWithWrappedCancellationException(h3);
+ checkCancelled(f);
+ for (NoopConsumer r : rs) r.assertNotInvoked();
+ }}
+
+ /**
+ * thenAccept result completes exceptionally if action does
+ */
+ public void testThenAccept_actionFailed() {
+ for (ExecutionMode m : ExecutionMode.values())
+ for (Integer v1 : new Integer[] { 1, null })
+ {
+ final CompletableFuture<Integer> f = new CompletableFuture<>();
+ final FailingConsumer[] rs = new FailingConsumer[4];
+ for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m);
+
+ final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
+ final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
+ assertTrue(f.complete(v1));
+ final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
+ final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
+
+ checkCompletedWithWrappedCFException(h0);
+ checkCompletedWithWrappedCFException(h1);
+ checkCompletedWithWrappedCFException(h2);
+ checkCompletedWithWrappedCFException(h3);
+ checkCompletedNormally(f, v1);
+ }}
+
+ /**
+ * thenCombine result completes normally after normal completion
+ * of sources
+ */
+ public void testThenCombine_normalCompletion() {
+ for (ExecutionMode m : ExecutionMode.values())
+ for (boolean fFirst : new boolean[] { true, false })
+ for (Integer v1 : new Integer[] { 1, null })
+ for (Integer v2 : new Integer[] { 2, null })
+ {
+ final CompletableFuture<Integer> f = new CompletableFuture<>();
+ final CompletableFuture<Integer> g = new CompletableFuture<>();
+ final SubtractFunction[] rs = new SubtractFunction[6];
+ for (int i = 0; i < rs.length; i++) rs[i] = new SubtractFunction(m);
+
+ final CompletableFuture<Integer> fst = fFirst ? f : g;
+ final CompletableFuture<Integer> snd = !fFirst ? f : g;
+ final Integer w1 = fFirst ? v1 : v2;
+ final Integer w2 = !fFirst ? v1 : v2;
+
+ final CompletableFuture<Integer> h0 = m.thenCombine(f, g, rs[0]);
+ final CompletableFuture<Integer> h1 = m.thenCombine(fst, fst, rs[1]);
+ assertTrue(fst.complete(w1));
+ final CompletableFuture<Integer> h2 = m.thenCombine(f, g, rs[2]);
+ final CompletableFuture<Integer> h3 = m.thenCombine(fst, fst, rs[3]);
+ checkIncomplete(h0); rs[0].assertNotInvoked();
+ checkIncomplete(h2); rs[2].assertNotInvoked();
+ checkCompletedNormally(h1, subtract(w1, w1));
+ checkCompletedNormally(h3, subtract(w1, w1));
+ rs[1].assertValue(subtract(w1, w1));
+ rs[3].assertValue(subtract(w1, w1));
+ assertTrue(snd.complete(w2));
+ final CompletableFuture<Integer> h4 = m.thenCombine(f, g, rs[4]);
+
+ checkCompletedNormally(h0, subtract(v1, v2));
+ checkCompletedNormally(h2, subtract(v1, v2));
+ checkCompletedNormally(h4, subtract(v1, v2));
+ rs[0].assertValue(subtract(v1, v2));
+ rs[2].assertValue(subtract(v1, v2));
+ rs[4].assertValue(subtract(v1, v2));
+
+ checkCompletedNormally(f, v1);
+ checkCompletedNormally(g, v2);
+ }}
+
+ /**
+ * thenCombine result completes exceptionally after exceptional
+ * completion of either source
+ */
+ public void testThenCombine_exceptionalCompletion() throws Throwable {
+ for (ExecutionMode m : ExecutionMode.values())
+ for (boolean fFirst : new boolean[] { true, false })
+ for (boolean failFirst : new boolean[] { true, false })
+ for (Integer v1 : new Integer[] { 1, null })
+ {
+ final CompletableFuture<Integer> f = new CompletableFuture<>();
+ final CompletableFuture<Integer> g = new CompletableFuture<>();
+ final CFException ex = new CFException();
+ final SubtractFunction r1 = new SubtractFunction(m);
+ final SubtractFunction r2 = new SubtractFunction(m);
+ final SubtractFunction r3 = new SubtractFunction(m);
+
+ final CompletableFuture<Integer> fst = fFirst ? f : g;
+ final CompletableFuture<Integer> snd = !fFirst ? f : g;
+ final Callable<Boolean> complete1 = failFirst ?
+ () -> fst.completeExceptionally(ex) :
+ () -> fst.complete(v1);
+ final Callable<Boolean> complete2 = failFirst ?
+ () -> snd.complete(v1) :
+ () -> snd.completeExceptionally(ex);
+
+ final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1);
+ assertTrue(complete1.call());
+ final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2);
+ checkIncomplete(h1);
+ checkIncomplete(h2);
+ assertTrue(complete2.call());
+ final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
+
+ checkCompletedWithWrappedException(h1, ex);
+ checkCompletedWithWrappedException(h2, ex);
+ checkCompletedWithWrappedException(h3, ex);
+ r1.assertNotInvoked();
+ r2.assertNotInvoked();
+ r3.assertNotInvoked();
+ checkCompletedNormally(failFirst ? snd : fst, v1);
+ checkCompletedExceptionally(failFirst ? fst : snd, ex);
+ }}
+
+ /**
+ * thenCombine result completes exceptionally if either source cancelled
+ */
+ public void testThenCombine_sourceCancelled() throws Throwable {
+ for (ExecutionMode m : ExecutionMode.values())
+ for (boolean mayInterruptIfRunning : new boolean[] { true, false })
+ for (boolean fFirst : new boolean[] { true, false })
+ for (boolean failFirst : new boolean[] { true, false })
+ for (Integer v1 : new Integer[] { 1, null })
+ {
+ final CompletableFuture<Integer> f = new CompletableFuture<>();
+ final CompletableFuture<Integer> g = new CompletableFuture<>();
+ final SubtractFunction r1 = new SubtractFunction(m);
+ final SubtractFunction r2 = new SubtractFunction(m);
+ final SubtractFunction r3 = new SubtractFunction(m);
+
+ final CompletableFuture<Integer> fst = fFirst ? f : g;
+ final CompletableFuture<Integer> snd = !fFirst ? f : g;
+ final Callable<Boolean> complete1 = failFirst ?
+ () -> fst.cancel(mayInterruptIfRunning) :
+ () -> fst.complete(v1);
+ final Callable<Boolean> complete2 = failFirst ?
+ () -> snd.complete(v1) :
+ () -> snd.cancel(mayInterruptIfRunning);
+
+ final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1);
+ assertTrue(complete1.call());
+ final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2);
+ checkIncomplete(h1);
+ checkIncomplete(h2);
+ assertTrue(complete2.call());
+ final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
+
+ checkCompletedWithWrappedCancellationException(h1);
+ checkCompletedWithWrappedCancellationException(h2);
+ checkCompletedWithWrappedCancellationException(h3);
+ r1.assertNotInvoked();
+ r2.assertNotInvoked();
+ r3.assertNotInvoked();
+ checkCompletedNormally(failFirst ? snd : fst, v1);
+ checkCancelled(failFirst ? fst : snd);
+ }}
+
+ /**
+ * thenCombine result completes exceptionally if action does
+ */
+ public void testThenCombine_actionFailed() {
+ for (ExecutionMode m : ExecutionMode.values())
+ for (boolean fFirst : new boolean[] { true, false })
+ for (Integer v1 : new Integer[] { 1, null })
+ for (Integer v2 : new Integer[] { 2, null })
+ {
+ final CompletableFuture<Integer> f = new CompletableFuture<>();
+ final CompletableFuture<Integer> g = new CompletableFuture<>();
+ final FailingBiFunction r1 = new FailingBiFunction(m);
+ final FailingBiFunction r2 = new FailingBiFunction(m);
+ final FailingBiFunction r3 = new FailingBiFunction(m);
+
+ final CompletableFuture<Integer> fst = fFirst ? f : g;
+ final CompletableFuture<Integer> snd = !fFirst ? f : g;
+ final Integer w1 = fFirst ? v1 : v2;
+ final Integer w2 = !fFirst ? v1 : v2;
+
+ final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1);
+ assertTrue(fst.complete(w1));
+ final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2);
+ assertTrue(snd.complete(w2));
+ final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
+
+ checkCompletedWithWrappedCFException(h1);
+ checkCompletedWithWrappedCFException(h2);
+ checkCompletedWithWrappedCFException(h3);
+ r1.assertInvoked();
+ r2.assertInvoked();
+ r3.assertInvoked();
+ checkCompletedNormally(f, v1);
+ checkCompletedNormally(g, v2);
+ }}
+
+ /**
+ * thenAcceptBoth result completes normally after normal
+ * completion of sources
+ */
+ public void testThenAcceptBoth_normalCompletion() {
+ for (ExecutionMode m : ExecutionMode.values())
+ for (boolean fFirst : new boolean[] { true, false })
+ for (Integer v1 : new Integer[] { 1, null })
+ for (Integer v2 : new Integer[] { 2, null })
+ {
+ final CompletableFuture<Integer> f = new CompletableFuture<>();
+ final CompletableFuture<Integer> g = new CompletableFuture<>();
+ final SubtractAction r1 = new SubtractAction(m);
+ final SubtractAction r2 = new SubtractAction(m);
+ final SubtractAction r3 = new SubtractAction(m);
+
+ final CompletableFuture<Integer> fst = fFirst ? f : g;
+ final CompletableFuture<Integer> snd = !fFirst ? f : g;
+ final Integer w1 = fFirst ? v1 : v2;
+ final Integer w2 = !fFirst ? v1 : v2;
+
+ final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
+ assertTrue(fst.complete(w1));
+ final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
+ checkIncomplete(h1);
+ checkIncomplete(h2);
+ r1.assertNotInvoked();
+ r2.assertNotInvoked();
+ assertTrue(snd.complete(w2));
+ final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
+
+ checkCompletedNormally(h1, null);
+ checkCompletedNormally(h2, null);
+ checkCompletedNormally(h3, null);
+ r1.assertValue(subtract(v1, v2));
+ r2.assertValue(subtract(v1, v2));
+ r3.assertValue(subtract(v1, v2));
+ checkCompletedNormally(f, v1);
+ checkCompletedNormally(g, v2);
+ }}
+
+ /**
+ * thenAcceptBoth result completes exceptionally after exceptional
+ * completion of either source
+ */
+ public void testThenAcceptBoth_exceptionalCompletion() throws Throwable {
+ for (ExecutionMode m : ExecutionMode.values())
+ for (boolean fFirst : new boolean[] { true, false })
+ for (boolean failFirst : new boolean[] { true, false })
+ for (Integer v1 : new Integer[] { 1, null })
+ {
+ final CompletableFuture<Integer> f = new CompletableFuture<>();
+ final CompletableFuture<Integer> g = new CompletableFuture<>();
+ final CFException ex = new CFException();
+ final SubtractAction r1 = new SubtractAction(m);
+ final SubtractAction r2 = new SubtractAction(m);
+ final SubtractAction r3 = new SubtractAction(m);
+
+ final CompletableFuture<Integer> fst = fFirst ? f : g;
+ final CompletableFuture<Integer> snd = !fFirst ? f : g;
+ final Callable<Boolean> complete1 = failFirst ?
+ () -> fst.completeExceptionally(ex) :
+ () -> fst.complete(v1);
+ final Callable<Boolean> complete2 = failFirst ?
+ () -> snd.complete(v1) :
+ () -> snd.completeExceptionally(ex);
+
+ final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
+ assertTrue(complete1.call());
+ final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
+ checkIncomplete(h1);
+ checkIncomplete(h2);
+ assertTrue(complete2.call());
+ final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
+
+ checkCompletedWithWrappedException(h1, ex);
+ checkCompletedWithWrappedException(h2, ex);
+ checkCompletedWithWrappedException(h3, ex);
+ r1.assertNotInvoked();
+ r2.assertNotInvoked();
+ r3.assertNotInvoked();
+ checkCompletedNormally(failFirst ? snd : fst, v1);
+ checkCompletedExceptionally(failFirst ? fst : snd, ex);
+ }}
+
+ /**
+ * thenAcceptBoth result completes exceptionally if either source cancelled
+ */
+ public void testThenAcceptBoth_sourceCancelled() throws Throwable {
+ for (ExecutionMode m : ExecutionMode.values())
+ for (boolean mayInterruptIfRunning : new boolean[] { true, false })
+ for (boolean fFirst : new boolean[] { true, false })
+ for (boolean failFirst : new boolean[] { true, false })
+ for (Integer v1 : new Integer[] { 1, null })
+ {
+ final CompletableFuture<Integer> f = new CompletableFuture<>();
+ final CompletableFuture<Integer> g = new CompletableFuture<>();
+ final SubtractAction r1 = new SubtractAction(m);
+ final SubtractAction r2 = new SubtractAction(m);
+ final SubtractAction r3 = new SubtractAction(m);
+
+ final CompletableFuture<Integer> fst = fFirst ? f : g;
+ final CompletableFuture<Integer> snd = !fFirst ? f : g;
+ final Callable<Boolean> complete1 = failFirst ?
+ () -> fst.cancel(mayInterruptIfRunning) :
+ () -> fst.complete(v1);
+ final Callable<Boolean> complete2 = failFirst ?
+ () -> snd.complete(v1) :
+ () -> snd.cancel(mayInterruptIfRunning);
+
+ final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
+ assertTrue(complete1.call());
+ final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
+ checkIncomplete(h1);
+ checkIncomplete(h2);
+ assertTrue(complete2.call());
+ final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
+
+ checkCompletedWithWrappedCancellationException(h1);
+ checkCompletedWithWrappedCancellationException(h2);
+ checkCompletedWithWrappedCancellationException(h3);
+ r1.assertNotInvoked();
+ r2.assertNotInvoked();
+ r3.assertNotInvoked();
+ checkCompletedNormally(failFirst ? snd : fst, v1);
+ checkCancelled(failFirst ? fst : snd);
+ }}
+
+ /**
+ * thenAcceptBoth result completes exceptionally if action does
+ */
+ public void testThenAcceptBoth_actionFailed() {
+ for (ExecutionMode m : ExecutionMode.values())
+ for (boolean fFirst : new boolean[] { true, false })
+ for (Integer v1 : new Integer[] { 1, null })
+ for (Integer v2 : new Integer[] { 2, null })
+ {
+ final CompletableFuture<Integer> f = new CompletableFuture<>();
+ final CompletableFuture<Integer> g = new CompletableFuture<>();
+ final FailingBiConsumer r1 = new FailingBiConsumer(m);
+ final FailingBiConsumer r2 = new FailingBiConsumer(m);
+ final FailingBiConsumer r3 = new FailingBiConsumer(m);
+
+ final CompletableFuture<Integer> fst = fFirst ? f : g;
+ final CompletableFuture<Integer> snd = !fFirst ? f : g;
+ final Integer w1 = fFirst ? v1 : v2;
+ final Integer w2 = !fFirst ? v1 : v2;
+
+ final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
+ assertTrue(fst.complete(w1));
+ final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
+ assertTrue(snd.complete(w2));
+ final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
+
+ checkCompletedWithWrappedCFException(h1);
+ checkCompletedWithWrappedCFException(h2);
+ checkCompletedWithWrappedCFException(h3);
+ r1.assertInvoked();
+ r2.assertInvoked();
+ r3.assertInvoked();
+ checkCompletedNormally(f, v1);
+ checkCompletedNormally(g, v2);
+ }}
+
+ /**
+ * runAfterBoth result completes normally after normal
+ * completion of sources
+ */
+ public void testRunAfterBoth_normalCompletion() {
+ for (ExecutionMode m : ExecutionMode.values())
+ for (boolean fFirst : new boolean[] { true, false })
+ for (Integer v1 : new Integer[] { 1, null })
+ for (Integer v2 : new Integer[] { 2, null })
+ {
+ final CompletableFuture<Integer> f = new CompletableFuture<>();
+ final CompletableFuture<Integer> g = new CompletableFuture<>();
+ final Noop r1 = new Noop(m);
+ final Noop r2 = new Noop(m);
+ final Noop r3 = new Noop(m);
+
+ final CompletableFuture<Integer> fst = fFirst ? f : g;
+ final CompletableFuture<Integer> snd = !fFirst ? f : g;
+ final Integer w1 = fFirst ? v1 : v2;
+ final Integer w2 = !fFirst ? v1 : v2;
+
+ final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
+ assertTrue(fst.complete(w1));
+ final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
+ checkIncomplete(h1);
+ checkIncomplete(h2);
+ r1.assertNotInvoked();
+ r2.assertNotInvoked();
+ assertTrue(snd.complete(w2));
+ final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
+
+ checkCompletedNormally(h1, null);
+ checkCompletedNormally(h2, null);
+ checkCompletedNormally(h3, null);
+ r1.assertInvoked();
+ r2.assertInvoked();
+ r3.assertInvoked();
+ checkCompletedNormally(f, v1);
+ checkCompletedNormally(g, v2);
+ }}
+
+ /**
+ * runAfterBoth result completes exceptionally after exceptional
+ * completion of either source
+ */
+ public void testRunAfterBoth_exceptionalCompletion() throws Throwable {
+ for (ExecutionMode m : ExecutionMode.values())
+ for (boolean fFirst : new boolean[] { true, false })
+ for (boolean failFirst : new boolean[] { true, false })
+ for (Integer v1 : new Integer[] { 1, null })
+ {
+ final CompletableFuture<Integer> f = new CompletableFuture<>();
+ final CompletableFuture<Integer> g = new CompletableFuture<>();
+ final CFException ex = new CFException();
+ final Noop r1 = new Noop(m);
+ final Noop r2 = new Noop(m);
+ final Noop r3 = new Noop(m);
+
+ final CompletableFuture<Integer> fst = fFirst ? f : g;
+ final CompletableFuture<Integer> snd = !fFirst ? f : g;
+ final Callable<Boolean> complete1 = failFirst ?
+ () -> fst.completeExceptionally(ex) :
+ () -> fst.complete(v1);
+ final Callable<Boolean> complete2 = failFirst ?
+ () -> snd.complete(v1) :
+ () -> snd.completeExceptionally(ex);
+
+ final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
+ assertTrue(complete1.call());
+ final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
+ checkIncomplete(h1);
+ checkIncomplete(h2);
+ assertTrue(complete2.call());
+ final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
+
+ checkCompletedWithWrappedException(h1, ex);
+ checkCompletedWithWrappedException(h2, ex);
+ checkCompletedWithWrappedException(h3, ex);
+ r1.assertNotInvoked();
+ r2.assertNotInvoked();
+ r3.assertNotInvoked();
+ checkCompletedNormally(failFirst ? snd : fst, v1);
+ checkCompletedExceptionally(failFirst ? fst : snd, ex);
+ }}
+
+ /**
+ * runAfterBoth result completes exceptionally if either source cancelled
+ */
+ public void testRunAfterBoth_sourceCancelled() throws Throwable {
+ for (ExecutionMode m : ExecutionMode.values())
+ for (boolean mayInterruptIfRunning : new boolean[] { true, false })
+ for (boolean fFirst : new boolean[] { true, false })
+ for (boolean failFirst : new boolean[] { true, false })
+ for (Integer v1 : new Integer[] { 1, null })
+ {
+ final CompletableFuture<Integer> f = new CompletableFuture<>();
+ final CompletableFuture<Integer> g = new CompletableFuture<>();
+ final Noop r1 = new Noop(m);
+ final Noop r2 = new Noop(m);
+ final Noop r3 = new Noop(m);
+
+ final CompletableFuture<Integer> fst = fFirst ? f : g;
+ final CompletableFuture<Integer> snd = !fFirst ? f : g;
+ final Callable<Boolean> complete1 = failFirst ?
+ () -> fst.cancel(mayInterruptIfRunning) :
+ () -> fst.complete(v1);
+ final Callable<Boolean> complete2 = failFirst ?
+ () -> snd.complete(v1) :
+ () -> snd.cancel(mayInterruptIfRunning);
+
+ final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
+ assertTrue(complete1.call());
+ final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
+ checkIncomplete(h1);
+ checkIncomplete(h2);
+ assertTrue(complete2.call());
+ final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
+
+ checkCompletedWithWrappedCancellationException(h1);
+ checkCompletedWithWrappedCancellationException(h2);
+ checkCompletedWithWrappedCancellationException(h3);
+ r1.assertNotInvoked();
+ r2.assertNotInvoked();
+ r3.assertNotInvoked();
+ checkCompletedNormally(failFirst ? snd : fst, v1);
+ checkCancelled(failFirst ? fst : snd);
+ }}
+
+ /**
+ * runAfterBoth result completes exceptionally if action does
+ */
+ public void testRunAfterBoth_actionFailed() {
+ for (ExecutionMode m : ExecutionMode.values())
+ for (boolean fFirst : new boolean[] { true, false })
+ for (Integer v1 : new Integer[] { 1, null })
+ for (Integer v2 : new Integer[] { 2, null })
+ {
+ final CompletableFuture<Integer> f = new CompletableFuture<>();
+ final CompletableFuture<Integer> g = new CompletableFuture<>();
+ final FailingRunnable r1 = new FailingRunnable(m);
+ final FailingRunnable r2 = new FailingRunnable(m);
+ final FailingRunnable r3 = new FailingRunnable(m);
+
+ final CompletableFuture<Integer> fst = fFirst ? f : g;
+ final CompletableFuture<Integer> snd = !fFirst ? f : g;
+ final Integer w1 = fFirst ? v1 : v2;
+ final Integer w2 = !fFirst ? v1 : v2;
+
+ final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
+ assertTrue(fst.complete(w1));
+ final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
+ assertTrue(snd.complete(w2));
+ final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
+
+ checkCompletedWithWrappedCFException(h1);
+ checkCompletedWithWrappedCFException(h2);
+ checkCompletedWithWrappedCFException(h3);
+ r1.assertInvoked();
+ r2.assertInvoked();
+ r3.assertInvoked();
+ checkCompletedNormally(f, v1);
+ checkCompletedNormally(g, v2);
+ }}
+
+ /**
+ * applyToEither result completes normally after normal completion
+ * of either source
+ */
+ public void testApplyToEither_normalCompletion() {
+ for (ExecutionMode m : ExecutionMode.values())
+ for (Integer v1 : new Integer[] { 1, null })
+ for (Integer v2 : new Integer[] { 2, null })
+ {
+ final CompletableFuture<Integer> f = new CompletableFuture<>();
+ final CompletableFuture<Integer> g = new CompletableFuture<>();
+ final IncFunction[] rs = new IncFunction[6];
+ for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
+
+ final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
+ final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
+ checkIncomplete(h0);
+ checkIncomplete(h1);
+ rs[0].assertNotInvoked();
+ rs[1].assertNotInvoked();
+ f.complete(v1);
+ checkCompletedNormally(h0, inc(v1));
+ checkCompletedNormally(h1, inc(v1));
+ final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
+ final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
+ checkCompletedNormally(h2, inc(v1));
+ checkCompletedNormally(h3, inc(v1));
+ g.complete(v2);
+
+ // unspecified behavior - both source completions available
+ final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
+ final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
+ rs[4].assertValue(h4.join());
+ rs[5].assertValue(h5.join());
+ assertTrue(Objects.equals(inc(v1), h4.join()) ||
+ Objects.equals(inc(v2), h4.join()));
+ assertTrue(Objects.equals(inc(v1), h5.join()) ||
+ Objects.equals(inc(v2), h5.join()));
+
+ checkCompletedNormally(f, v1);
+ checkCompletedNormally(g, v2);
+ checkCompletedNormally(h0, inc(v1));
+ checkCompletedNormally(h1, inc(v1));
+ checkCompletedNormally(h2, inc(v1));
+ checkCompletedNormally(h3, inc(v1));
+ for (int i = 0; i < 4; i++) rs[i].assertValue(inc(v1));
+ }}
+
+ /**
+ * applyToEither result completes exceptionally after exceptional
+ * completion of either source
+ */
+ public void testApplyToEither_exceptionalCompletion() {
+ for (ExecutionMode m : ExecutionMode.values())
+ for (Integer v1 : new Integer[] { 1, null })
+ {
+ final CompletableFuture<Integer> f = new CompletableFuture<>();
+ final CompletableFuture<Integer> g = new CompletableFuture<>();
+ final CFException ex = new CFException();
+ final IncFunction[] rs = new IncFunction[6];
+ for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
+
+ final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
+ final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
+ checkIncomplete(h0);
+ checkIncomplete(h1);
+ rs[0].assertNotInvoked();
+ rs[1].assertNotInvoked();
+ f.completeExceptionally(ex);
+ checkCompletedWithWrappedException(h0, ex);
+ checkCompletedWithWrappedException(h1, ex);
+ final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
+ final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
+ checkCompletedWithWrappedException(h2, ex);
+ checkCompletedWithWrappedException(h3, ex);
+ g.complete(v1);
+
+ // unspecified behavior - both source completions available
+ final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
+ final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
+ try {
+ assertEquals(inc(v1), h4.join());
+ rs[4].assertValue(inc(v1));
+ } catch (CompletionException ok) {
+ checkCompletedWithWrappedException(h4, ex);
+ rs[4].assertNotInvoked();
+ }
+ try {
+ assertEquals(inc(v1), h5.join());
+ rs[5].assertValue(inc(v1));
+ } catch (CompletionException ok) {
+ checkCompletedWithWrappedException(h5, ex);
+ rs[5].assertNotInvoked();
+ }
+
+ checkCompletedExceptionally(f, ex);
+ checkCompletedNormally(g, v1);
+ checkCompletedWithWrappedException(h0, ex);
+ checkCompletedWithWrappedException(h1, ex);
+ checkCompletedWithWrappedException(h2, ex);
+ checkCompletedWithWrappedException(h3, ex);
+ checkCompletedWithWrappedException(h4, ex);
+ for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
+ }}
+
+ public void testApplyToEither_exceptionalCompletion2() {
+ for (ExecutionMode m : ExecutionMode.values())
+ for (boolean fFirst : new boolean[] { true, false })
+ for (Integer v1 : new Integer[] { 1, null })
+ {
+ final CompletableFuture<Integer> f = new CompletableFuture<>();
+ final CompletableFuture<Integer> g = new CompletableFuture<>();
+ final CFException ex = new CFException();
+ final IncFunction[] rs = new IncFunction[6];
+ for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
+
+ final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
+ final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
+ assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex));
+ assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex));
+ final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
+ final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
+
+ // unspecified behavior - both source completions available
+ try {
+ assertEquals(inc(v1), h0.join());
+ rs[0].assertValue(inc(v1));
+ } catch (CompletionException ok) {
+ checkCompletedWithWrappedException(h0, ex);
+ rs[0].assertNotInvoked();
+ }
+ try {
+ assertEquals(inc(v1), h1.join());
+ rs[1].assertValue(inc(v1));
+ } catch (CompletionException ok) {
+ checkCompletedWithWrappedException(h1, ex);
+ rs[1].assertNotInvoked();
+ }
+ try {
+ assertEquals(inc(v1), h2.join());
+ rs[2].assertValue(inc(v1));
+ } catch (CompletionException ok) {
+ checkCompletedWithWrappedException(h2, ex);
+ rs[2].assertNotInvoked();
+ }
+ try {
+ assertEquals(inc(v1), h3.join());
+ rs[3].assertValue(inc(v1));
+ } catch (CompletionException ok) {
+ checkCompletedWithWrappedException(h3, ex);
+ rs[3].assertNotInvoked();
+ }
+
+ checkCompletedNormally(f, v1);
+ checkCompletedExceptionally(g, ex);
+ }}
+
+ /**
+ * applyToEither result completes exceptionally if either source cancelled
+ */
+ public void testApplyToEither_sourceCancelled() {
+ for (ExecutionMode m : ExecutionMode.values())
+ for (boolean mayInterruptIfRunning : new boolean[] { true, false })
+ for (Integer v1 : new Integer[] { 1, null })
+ {
+ final CompletableFuture<Integer> f = new CompletableFuture<>();
+ final CompletableFuture<Integer> g = new CompletableFuture<>();
+ final IncFunction[] rs = new IncFunction[6];
+ for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
+
+ final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
+ final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
+ checkIncomplete(h0);
+ checkIncomplete(h1);
+ rs[0].assertNotInvoked();
+ rs[1].assertNotInvoked();
+ f.cancel(mayInterruptIfRunning);
+ checkCompletedWithWrappedCancellationException(h0);
+ checkCompletedWithWrappedCancellationException(h1);
+ final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
+ final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
+ checkCompletedWithWrappedCancellationException(h2);
+ checkCompletedWithWrappedCancellationException(h3);
+ g.complete(v1);
+
+ // unspecified behavior - both source completions available
+ final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
+ final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
+ try {
+ assertEquals(inc(v1), h4.join());
+ rs[4].assertValue(inc(v1));
+ } catch (CompletionException ok) {
+ checkCompletedWithWrappedCancellationException(h4);
+ rs[4].assertNotInvoked();
+ }
+ try {
+ assertEquals(inc(v1), h5.join());
+ rs[5].assertValue(inc(v1));
+ } catch (CompletionException ok) {
+ checkCompletedWithWrappedCancellationException(h5);
+ rs[5].assertNotInvoked();
+ }
+
+ checkCancelled(f);
+ checkCompletedNormally(g, v1);
+ checkCompletedWithWrappedCancellationException(h0);
+ checkCompletedWithWrappedCancellationException(h1);
+ checkCompletedWithWrappedCancellationException(h2);
+ checkCompletedWithWrappedCancellationException(h3);
+ for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
+ }}
+
+ public void testApplyToEither_sourceCancelled2() {
+ for (ExecutionMode m : ExecutionMode.values())
+ for (boolean mayInterruptIfRunning : new boolean[] { true, false })
+ for (boolean fFirst : new boolean[] { true, false })
+ for (Integer v1 : new Integer[] { 1, null })
+ {
+ final CompletableFuture<Integer> f = new CompletableFuture<>();
+ final CompletableFuture<Integer> g = new CompletableFuture<>();
+ final IncFunction[] rs = new IncFunction[6];
+ for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
+
+ final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
+ final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
+ assertTrue(fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning));
+ assertTrue(!fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning));
+ final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
+ final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
+
+ // unspecified behavior - both source completions available
+ try {
+ assertEquals(inc(v1), h0.join());
+ rs[0].assertValue(inc(v1));
+ } catch (CompletionException ok) {
+ checkCompletedWithWrappedCancellationException(h0);
+ rs[0].assertNotInvoked();
+ }
+ try {
+ assertEquals(inc(v1), h1.join());
+ rs[1].assertValue(inc(v1));
+ } catch (CompletionException ok) {
+ checkCompletedWithWrappedCancellationException(h1);
+ rs[1].assertNotInvoked();
+ }
+ try {
+ assertEquals(inc(v1), h2.join());
+ rs[2].assertValue(inc(v1));
+ } catch (CompletionException ok) {
+ checkCompletedWithWrappedCancellationException(h2);
+ rs[2].assertNotInvoked();
+ }
+ try {
+ assertEquals(inc(v1), h3.join());
+ rs[3].assertValue(inc(v1));
+ } catch (CompletionException ok) {
+ checkCompletedWithWrappedCancellationException(h3);
+ rs[3].assertNotInvoked();
+ }
+
+ checkCompletedNormally(f, v1);
+ checkCancelled(g);
+ }}
+
+ /**
+ * applyToEither result completes exceptionally if action does
+ */
+ public void testApplyToEither_actionFailed() {
+ for (ExecutionMode m : ExecutionMode.values())
+ for (Integer v1 : new Integer[] { 1, null })
+ for (Integer v2 : new Integer[] { 2, null })
+ {
+ final CompletableFuture<Integer> f = new CompletableFuture<>();
+ final CompletableFuture<Integer> g = new CompletableFuture<>();
+ final FailingFunction[] rs = new FailingFunction[6];
+ for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m);
+
+ final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
+ final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
+ f.complete(v1);
+ final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
+ final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
+ checkCompletedWithWrappedCFException(h0);
+ checkCompletedWithWrappedCFException(h1);
+ checkCompletedWithWrappedCFException(h2);
+ checkCompletedWithWrappedCFException(h3);
+ for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
+
+ g.complete(v2);
+
+ // unspecified behavior - both source completions available
+ final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
+ final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
+
+ checkCompletedWithWrappedCFException(h4);
+ assertTrue(Objects.equals(v1, rs[4].value) ||
+ Objects.equals(v2, rs[4].value));
+ checkCompletedWithWrappedCFException(h5);
+ assertTrue(Objects.equals(v1, rs[5].value) ||
+ Objects.equals(v2, rs[5].value));
+
+ checkCompletedNormally(f, v1);
+ checkCompletedNormally(g, v2);
+ }}
+
+ /**
+ * acceptEither result completes normally after normal completion
+ * of either source
+ */
+ public void testAcceptEither_normalCompletion() {
+ for (ExecutionMode m : ExecutionMode.values())
+ for (Integer v1 : new Integer[] { 1, null })
+ for (Integer v2 : new Integer[] { 2, null })
+ {
+ final CompletableFuture<Integer> f = new CompletableFuture<>();
+ final CompletableFuture<Integer> g = new CompletableFuture<>();
+ final NoopConsumer[] rs = new NoopConsumer[6];
+ for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
+
+ final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
+ final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
+ checkIncomplete(h0);
+ checkIncomplete(h1);
+ rs[0].assertNotInvoked();
+ rs[1].assertNotInvoked();
+ f.complete(v1);
+ checkCompletedNormally(h0, null);
+ checkCompletedNormally(h1, null);
+ rs[0].assertValue(v1);
+ rs[1].assertValue(v1);
+ final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
+ final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
+ checkCompletedNormally(h2, null);
+ checkCompletedNormally(h3, null);
+ rs[2].assertValue(v1);
+ rs[3].assertValue(v1);
+ g.complete(v2);
+
+ // unspecified behavior - both source completions available
+ final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
+ final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
+ checkCompletedNormally(h4, null);
+ checkCompletedNormally(h5, null);
+ assertTrue(Objects.equals(v1, rs[4].value) ||
+ Objects.equals(v2, rs[4].value));
+ assertTrue(Objects.equals(v1, rs[5].value) ||
+ Objects.equals(v2, rs[5].value));
+
+ checkCompletedNormally(f, v1);
+ checkCompletedNormally(g, v2);
+ checkCompletedNormally(h0, null);
+ checkCompletedNormally(h1, null);
+ checkCompletedNormally(h2, null);
+ checkCompletedNormally(h3, null);
+ for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
+ }}
+
+ /**
+ * acceptEither result completes exceptionally after exceptional
+ * completion of either source
+ */
+ public void testAcceptEither_exceptionalCompletion() {
+ for (ExecutionMode m : ExecutionMode.values())
+ for (Integer v1 : new Integer[] { 1, null })
+ {
+ final CompletableFuture<Integer> f = new CompletableFuture<>();
+ final CompletableFuture<Integer> g = new CompletableFuture<>();
+ final CFException ex = new CFException();
+ final NoopConsumer[] rs = new NoopConsumer[6];
+ for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
+
+ final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
+ final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
+ checkIncomplete(h0);
+ checkIncomplete(h1);
+ rs[0].assertNotInvoked();
+ rs[1].assertNotInvoked();
+ f.completeExceptionally(ex);
+ checkCompletedWithWrappedException(h0, ex);
+ checkCompletedWithWrappedException(h1, ex);
+ final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
+ final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
+ checkCompletedWithWrappedException(h2, ex);
+ checkCompletedWithWrappedException(h3, ex);
+
+ g.complete(v1);
+
+ // unspecified behavior - both source completions available
+ final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
+ final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
+ try {
+ assertNull(h4.join());
+ rs[4].assertValue(v1);
+ } catch (CompletionException ok) {
+ checkCompletedWithWrappedException(h4, ex);
+ rs[4].assertNotInvoked();
+ }
+ try {
+ assertNull(h5.join());
+ rs[5].assertValue(v1);
+ } catch (CompletionException ok) {
+ checkCompletedWithWrappedException(h5, ex);
+ rs[5].assertNotInvoked();
+ }
+
+ checkCompletedExceptionally(f, ex);
+ checkCompletedNormally(g, v1);
+ checkCompletedWithWrappedException(h0, ex);
+ checkCompletedWithWrappedException(h1, ex);
+ checkCompletedWithWrappedException(h2, ex);
+ checkCompletedWithWrappedException(h3, ex);
+ checkCompletedWithWrappedException(h4, ex);
+ for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
+ }}
+
+ public void testAcceptEither_exceptionalCompletion2() {
+ for (ExecutionMode m : ExecutionMode.values())
+ for (boolean fFirst : new boolean[] { true, false })
+ for (Integer v1 : new Integer[] { 1, null })
+ {
+ final CompletableFuture<Integer> f = new CompletableFuture<>();
+ final CompletableFuture<Integer> g = new CompletableFuture<>();
+ final CFException ex = new CFException();
+ final NoopConsumer[] rs = new NoopConsumer[6];
+ for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
+
+ final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
+ final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
+ assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex));
+ assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex));
+ final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
+ final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
+
+ // unspecified behavior - both source completions available
+ try {
+ assertEquals(null, h0.join());
+ rs[0].assertValue(v1);
+ } catch (CompletionException ok) {
+ checkCompletedWithWrappedException(h0, ex);
+ rs[0].assertNotInvoked();
+ }
+ try {
+ assertEquals(null, h1.join());
+ rs[1].assertValue(v1);
+ } catch (CompletionException ok) {
+ checkCompletedWithWrappedException(h1, ex);
+ rs[1].assertNotInvoked();
+ }
+ try {
+ assertEquals(null, h2.join());
+ rs[2].assertValue(v1);
+ } catch (CompletionException ok) {
+ checkCompletedWithWrappedException(h2, ex);
+ rs[2].assertNotInvoked();
+ }
+ try {
+ assertEquals(null, h3.join());
+ rs[3].assertValue(v1);
+ } catch (CompletionException ok) {
+ checkCompletedWithWrappedException(h3, ex);
+ rs[3].assertNotInvoked();
+ }
+
+ checkCompletedNormally(f, v1);
+ checkCompletedExceptionally(g, ex);
+ }}
+
+ /**
+ * acceptEither result completes exceptionally if either source cancelled
+ */
+ public void testAcceptEither_sourceCancelled() {
+ for (ExecutionMode m : ExecutionMode.values())
+ for (boolean mayInterruptIfRunning : new boolean[] { true, false })
+ for (Integer v1 : new Integer[] { 1, null })
+ {
+ final CompletableFuture<Integer> f = new CompletableFuture<>();
+ final CompletableFuture<Integer> g = new CompletableFuture<>();
+ final NoopConsumer[] rs = new NoopConsumer[6];
+ for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
+
+ final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
+ final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
+ checkIncomplete(h0);
+ checkIncomplete(h1);
+ rs[0].assertNotInvoked();
+ rs[1].assertNotInvoked();
+ f.cancel(mayInterruptIfRunning);
+ checkCompletedWithWrappedCancellationException(h0);
+ checkCompletedWithWrappedCancellationException(h1);
+ final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
+ final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
+ checkCompletedWithWrappedCancellationException(h2);
+ checkCompletedWithWrappedCancellationException(h3);
+
+ g.complete(v1);
+
+ // unspecified behavior - both source completions available
+ final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
+ final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
+ try {
+ assertNull(h4.join());
+ rs[4].assertValue(v1);
+ } catch (CompletionException ok) {
+ checkCompletedWithWrappedCancellationException(h4);
+ rs[4].assertNotInvoked();
+ }
+ try {
+ assertNull(h5.join());
+ rs[5].assertValue(v1);
+ } catch (CompletionException ok) {
+ checkCompletedWithWrappedCancellationException(h5);
+ rs[5].assertNotInvoked();
+ }
+
+ checkCancelled(f);
+ checkCompletedNormally(g, v1);
+ checkCompletedWithWrappedCancellationException(h0);
+ checkCompletedWithWrappedCancellationException(h1);
+ checkCompletedWithWrappedCancellationException(h2);
+ checkCompletedWithWrappedCancellationException(h3);
+ for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
+ }}
+
+ /**
+ * acceptEither result completes exceptionally if action does
+ */
+ public void testAcceptEither_actionFailed() {
+ for (ExecutionMode m : ExecutionMode.values())
+ for (Integer v1 : new Integer[] { 1, null })
+ for (Integer v2 : new Integer[] { 2, null })
+ {
+ final CompletableFuture<Integer> f = new CompletableFuture<>();
+ final CompletableFuture<Integer> g = new CompletableFuture<>();
+ final FailingConsumer[] rs = new FailingConsumer[6];
+ for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m);
+
+ final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
+ final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
+ f.complete(v1);
+ final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
+ final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
+ checkCompletedWithWrappedCFException(h0);
+ checkCompletedWithWrappedCFException(h1);
+ checkCompletedWithWrappedCFException(h2);
+ checkCompletedWithWrappedCFException(h3);
+ for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
+
+ g.complete(v2);
+
+ // unspecified behavior - both source completions available
+ final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
+ final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
+
+ checkCompletedWithWrappedCFException(h4);
+ assertTrue(Objects.equals(v1, rs[4].value) ||
+ Objects.equals(v2, rs[4].value));
+ checkCompletedWithWrappedCFException(h5);
+ assertTrue(Objects.equals(v1, rs[5].value) ||
+ Objects.equals(v2, rs[5].value));
+
+ checkCompletedNormally(f, v1);
+ checkCompletedNormally(g, v2);
+ }}
+
+ /**
+ * runAfterEither result completes normally after normal completion
+ * of either source
+ */
+ public void testRunAfterEither_normalCompletion() {
+ for (ExecutionMode m : ExecutionMode.values())
+ for (Integer v1 : new Integer[] { 1, null })
+ for (Integer v2 : new Integer[] { 2, null })
+ {
+ final CompletableFuture<Integer> f = new CompletableFuture<>();
+ final CompletableFuture<Integer> g = new CompletableFuture<>();
+ final Noop[] rs = new Noop[6];
+ for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
+
+ final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
+ final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
+ checkIncomplete(h0);
+ checkIncomplete(h1);
+ rs[0].assertNotInvoked();
+ rs[1].assertNotInvoked();
+ f.complete(v1);
+ checkCompletedNormally(h0, null);
+ checkCompletedNormally(h1, null);
+ rs[0].assertInvoked();
+ rs[1].assertInvoked();
+ final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
+ final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
+ checkCompletedNormally(h2, null);
+ checkCompletedNormally(h3, null);
+ rs[2].assertInvoked();
+ rs[3].assertInvoked();
+
+ g.complete(v2);
+
+ final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
+ final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
+
+ checkCompletedNormally(f, v1);
+ checkCompletedNormally(g, v2);
+ checkCompletedNormally(h0, null);
+ checkCompletedNormally(h1, null);
+ checkCompletedNormally(h2, null);
+ checkCompletedNormally(h3, null);
+ checkCompletedNormally(h4, null);
+ checkCompletedNormally(h5, null);
+ for (int i = 0; i < 6; i++) rs[i].assertInvoked();
+ }}
+
+ /**
+ * runAfterEither result completes exceptionally after exceptional
+ * completion of either source
+ */
+ public void testRunAfterEither_exceptionalCompletion() {
+ for (ExecutionMode m : ExecutionMode.values())
+ for (Integer v1 : new Integer[] { 1, null })
+ {
+ final CompletableFuture<Integer> f = new CompletableFuture<>();
+ final CompletableFuture<Integer> g = new CompletableFuture<>();
+ final CFException ex = new CFException();
+ final Noop[] rs = new Noop[6];
+ for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
+
+ final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
+ final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
+ checkIncomplete(h0);
+ checkIncomplete(h1);
+ rs[0].assertNotInvoked();
+ rs[1].assertNotInvoked();
+ assertTrue(f.completeExceptionally(ex));
+ checkCompletedWithWrappedException(h0, ex);
+ checkCompletedWithWrappedException(h1, ex);
+ final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
+ final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
+ checkCompletedWithWrappedException(h2, ex);
+ checkCompletedWithWrappedException(h3, ex);
+
+ assertTrue(g.complete(v1));
+
+ // unspecified behavior - both source completions available
+ final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
+ final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
+ try {
+ assertNull(h4.join());
+ rs[4].assertInvoked();
+ } catch (CompletionException ok) {
+ checkCompletedWithWrappedException(h4, ex);
+ rs[4].assertNotInvoked();
+ }
+ try {
+ assertNull(h5.join());
+ rs[5].assertInvoked();
+ } catch (CompletionException ok) {
+ checkCompletedWithWrappedException(h5, ex);
+ rs[5].assertNotInvoked();
+ }
+
+ checkCompletedExceptionally(f, ex);
+ checkCompletedNormally(g, v1);
+ checkCompletedWithWrappedException(h0, ex);
+ checkCompletedWithWrappedException(h1, ex);
+ checkCompletedWithWrappedException(h2, ex);
+ checkCompletedWithWrappedException(h3, ex);
+ checkCompletedWithWrappedException(h4, ex);
+ for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
+ }}
+
+ public void testRunAfterEither_exceptionalCompletion2() {
+ for (ExecutionMode m : ExecutionMode.values())
+ for (boolean fFirst : new boolean[] { true, false })
+ for (Integer v1 : new Integer[] { 1, null })
+ {
+ final CompletableFuture<Integer> f = new CompletableFuture<>();
+ final CompletableFuture<Integer> g = new CompletableFuture<>();
+ final CFException ex = new CFException();
+ final Noop[] rs = new Noop[6];
+ for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
+
+ final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
+ final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
+ assertTrue( fFirst ? f.complete(v1) : g.completeExceptionally(ex));
+ assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex));
+ final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
+ final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
+
+ // unspecified behavior - both source completions available
+ try {
+ assertEquals(null, h0.join());
+ rs[0].assertInvoked();
+ } catch (CompletionException ok) {
+ checkCompletedWithWrappedException(h0, ex);
+ rs[0].assertNotInvoked();
+ }
+ try {
+ assertEquals(null, h1.join());
+ rs[1].assertInvoked();
+ } catch (CompletionException ok) {
+ checkCompletedWithWrappedException(h1, ex);
+ rs[1].assertNotInvoked();
+ }
+ try {
+ assertEquals(null, h2.join());
+ rs[2].assertInvoked();
+ } catch (CompletionException ok) {
+ checkCompletedWithWrappedException(h2, ex);
+ rs[2].assertNotInvoked();
+ }
+ try {
+ assertEquals(null, h3.join());
+ rs[3].assertInvoked();
+ } catch (CompletionException ok) {
+ checkCompletedWithWrappedException(h3, ex);
+ rs[3].assertNotInvoked();
+ }
+
+ checkCompletedNormally(f, v1);
+ checkCompletedExceptionally(g, ex);
+ }}
+
+ /**
+ * runAfterEither result completes exceptionally if either source cancelled
+ */
+ public void testRunAfterEither_sourceCancelled() {
+ for (ExecutionMode m : ExecutionMode.values())
+ for (boolean mayInterruptIfRunning : new boolean[] { true, false })
+ for (Integer v1 : new Integer[] { 1, null })
+ {
+ final CompletableFuture<Integer> f = new CompletableFuture<>();
+ final CompletableFuture<Integer> g = new CompletableFuture<>();
+ final Noop[] rs = new Noop[6];
+ for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
+
+ final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
+ final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
+ checkIncomplete(h0);
+ checkIncomplete(h1);
+ rs[0].assertNotInvoked();
+ rs[1].assertNotInvoked();
+ f.cancel(mayInterruptIfRunning);
+ checkCompletedWithWrappedCancellationException(h0);
+ checkCompletedWithWrappedCancellationException(h1);
+ final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
+ final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
+ checkCompletedWithWrappedCancellationException(h2);
+ checkCompletedWithWrappedCancellationException(h3);
+
+ assertTrue(g.complete(v1));
+
+ // unspecified behavior - both source completions available
+ final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
+ final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
+ try {
+ assertNull(h4.join());
+ rs[4].assertInvoked();
+ } catch (CompletionException ok) {
+ checkCompletedWithWrappedCancellationException(h4);
+ rs[4].assertNotInvoked();
+ }
+ try {
+ assertNull(h5.join());
+ rs[5].assertInvoked();
+ } catch (CompletionException ok) {
+ checkCompletedWithWrappedCancellationException(h5);
+ rs[5].assertNotInvoked();
+ }
+
+ checkCancelled(f);
+ checkCompletedNormally(g, v1);
+ checkCompletedWithWrappedCancellationException(h0);
+ checkCompletedWithWrappedCancellationException(h1);
+ checkCompletedWithWrappedCancellationException(h2);
+ checkCompletedWithWrappedCancellationException(h3);
+ for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
+ }}
+
+ /**
+ * runAfterEither result completes exceptionally if action does
+ */
+ public void testRunAfterEither_actionFailed() {
+ for (ExecutionMode m : ExecutionMode.values())
+ for (Integer v1 : new Integer[] { 1, null })
+ for (Integer v2 : new Integer[] { 2, null })
+ {
+ final CompletableFuture<Integer> f = new CompletableFuture<>();
+ final CompletableFuture<Integer> g = new CompletableFuture<>();
+ final FailingRunnable[] rs = new FailingRunnable[6];
+ for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m);
+
+ final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
+ final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
+ assertTrue(f.complete(v1));
+ final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
+ final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
+ checkCompletedWithWrappedCFException(h0);
+ checkCompletedWithWrappedCFException(h1);
+ checkCompletedWithWrappedCFException(h2);
+ checkCompletedWithWrappedCFException(h3);
+ for (int i = 0; i < 4; i++) rs[i].assertInvoked();
+ assertTrue(g.complete(v2));
+ final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
+ final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
+ checkCompletedWithWrappedCFException(h4);
+ checkCompletedWithWrappedCFException(h5);
+
+ checkCompletedNormally(f, v1);
+ checkCompletedNormally(g, v2);
+ for (int i = 0; i < 6; i++) rs[i].assertInvoked();
+ }}
+
+ /**
+ * thenCompose result completes normally after normal completion of source
+ */
+ public void testThenCompose_normalCompletion() {
+ for (ExecutionMode m : ExecutionMode.values())
+ for (boolean createIncomplete : new boolean[] { true, false })
+ for (Integer v1 : new Integer[] { 1, null })
+ {
+ final CompletableFuture<Integer> f = new CompletableFuture<>();
+ final CompletableFutureInc r = new CompletableFutureInc(m);
+ if (!createIncomplete) assertTrue(f.complete(v1));
+ final CompletableFuture<Integer> g = m.thenCompose(f, r);
+ if (createIncomplete) assertTrue(f.complete(v1));
+
+ checkCompletedNormally(g, inc(v1));
+ checkCompletedNormally(f, v1);
+ r.assertValue(v1);
+ }}
+
+ /**
+ * thenCompose result completes exceptionally after exceptional
+ * completion of source
+ */
+ public void testThenCompose_exceptionalCompletion() {
+ for (ExecutionMode m : ExecutionMode.values())
+ for (boolean createIncomplete : new boolean[] { true, false })
+ {
+ final CFException ex = new CFException();
+ final CompletableFutureInc r = new CompletableFutureInc(m);
+ final CompletableFuture<Integer> f = new CompletableFuture<>();
+ if (!createIncomplete) f.completeExceptionally(ex);
+ final CompletableFuture<Integer> g = m.thenCompose(f, r);
+ if (createIncomplete) f.completeExceptionally(ex);
+
+ checkCompletedWithWrappedException(g, ex);
+ checkCompletedExceptionally(f, ex);
+ r.assertNotInvoked();
+ }}
+
+ /**
+ * thenCompose result completes exceptionally if action does
+ */
+ public void testThenCompose_actionFailed() {
+ for (ExecutionMode m : ExecutionMode.values())
+ for (boolean createIncomplete : new boolean[] { true, false })
+ for (Integer v1 : new Integer[] { 1, null })
+ {
+ final CompletableFuture<Integer> f = new CompletableFuture<>();
+ final FailingCompletableFutureFunction r
+ = new FailingCompletableFutureFunction(m);
+ if (!createIncomplete) assertTrue(f.complete(v1));
+ final CompletableFuture<Integer> g = m.thenCompose(f, r);
+ if (createIncomplete) assertTrue(f.complete(v1));
+
+ checkCompletedWithWrappedCFException(g);
+ checkCompletedNormally(f, v1);
+ }}
+
+ /**
+ * thenCompose result completes exceptionally if source cancelled
+ */
+ public void testThenCompose_sourceCancelled() {
+ for (ExecutionMode m : ExecutionMode.values())
+ for (boolean createIncomplete : new boolean[] { true, false })
+ for (boolean mayInterruptIfRunning : new boolean[] { true, false })
+ {
+ final CompletableFuture<Integer> f = new CompletableFuture<>();
+ final CompletableFutureInc r = new CompletableFutureInc(m);
+ if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
+ final CompletableFuture<Integer> g = m.thenCompose(f, r);
+ if (createIncomplete) {
+ checkIncomplete(g);
+ assertTrue(f.cancel(mayInterruptIfRunning));
+ }
+
+ checkCompletedWithWrappedCancellationException(g);
+ checkCancelled(f);
+ }}
+
+ /**
+ * thenCompose result completes exceptionally if the result of the action does
+ */
+ public void testThenCompose_actionReturnsFailingFuture() {
+ for (ExecutionMode m : ExecutionMode.values())
+ for (int order = 0; order < 6; order++)
+ for (Integer v1 : new Integer[] { 1, null })
+ {
+ final CFException ex = new CFException();
+ final CompletableFuture<Integer> f = new CompletableFuture<>();
+ final CompletableFuture<Integer> g = new CompletableFuture<>();
+ final CompletableFuture<Integer> h;
+ // Test all permutations of orders
+ switch (order) {
+ case 0:
+ assertTrue(f.complete(v1));
+ assertTrue(g.completeExceptionally(ex));
+ h = m.thenCompose(f, (x -> g));
+ break;
+ case 1:
+ assertTrue(f.complete(v1));
+ h = m.thenCompose(f, (x -> g));
+ assertTrue(g.completeExceptionally(ex));
+ break;
+ case 2:
+ assertTrue(g.completeExceptionally(ex));
+ assertTrue(f.complete(v1));
+ h = m.thenCompose(f, (x -> g));
+ break;
+ case 3:
+ assertTrue(g.completeExceptionally(ex));
+ h = m.thenCompose(f, (x -> g));
+ assertTrue(f.complete(v1));
+ break;
+ case 4:
+ h = m.thenCompose(f, (x -> g));
+ assertTrue(f.complete(v1));
+ assertTrue(g.completeExceptionally(ex));
+ break;
+ case 5:
+ h = m.thenCompose(f, (x -> g));
+ assertTrue(f.complete(v1));
+ assertTrue(g.completeExceptionally(ex));
+ break;
+ default: throw new AssertionError();
+ }
+
+ checkCompletedExceptionally(g, ex);
+ checkCompletedWithWrappedException(h, ex);
+ checkCompletedNormally(f, v1);
+ }}
+
+ // other static methods
+
+ /**
+ * allOf(no component futures) returns a future completed normally
+ * with the value null
+ */
+ public void testAllOf_empty() throws Exception {
+ CompletableFuture<Void> f = CompletableFuture.allOf();
+ checkCompletedNormally(f, null);
+ }
+
+ /**
+ * allOf returns a future completed normally with the value null
+ * when all components complete normally
+ */
+ public void testAllOf_normal() throws Exception {
+ for (int k = 1; k < 10; k++) {
+ CompletableFuture<Integer>[] fs
+ = (CompletableFuture<Integer>[]) new CompletableFuture[k];
+ for (int i = 0; i < k; i++)
+ fs[i] = new CompletableFuture<>();
+ CompletableFuture<Void> f = CompletableFuture.allOf(fs);
+ for (int i = 0; i < k; i++) {
+ checkIncomplete(f);
+ checkIncomplete(CompletableFuture.allOf(fs));
+ fs[i].complete(one);
+ }
+ checkCompletedNormally(f, null);
+ checkCompletedNormally(CompletableFuture.allOf(fs), null);
+ }
+ }
+
+ public void testAllOf_backwards() throws Exception {
+ for (int k = 1; k < 10; k++) {
+ CompletableFuture<Integer>[] fs
+ = (CompletableFuture<Integer>[]) new CompletableFuture[k];
+ for (int i = 0; i < k; i++)
+ fs[i] = new CompletableFuture<>();
+ CompletableFuture<Void> f = CompletableFuture.allOf(fs);
+ for (int i = k - 1; i >= 0; i--) {
+ checkIncomplete(f);
+ checkIncomplete(CompletableFuture.allOf(fs));
+ fs[i].complete(one);
+ }
+ checkCompletedNormally(f, null);
+ checkCompletedNormally(CompletableFuture.allOf(fs), null);
+ }
+ }
+
+ public void testAllOf_exceptional() throws Exception {
+ for (int k = 1; k < 10; k++) {
+ CompletableFuture<Integer>[] fs
+ = (CompletableFuture<Integer>[]) new CompletableFuture[k];
+ CFException ex = new CFException();
+ for (int i = 0; i < k; i++)
+ fs[i] = new CompletableFuture<>();
+ CompletableFuture<Void> f = CompletableFuture.allOf(fs);
+ for (int i = 0; i < k; i++) {
+ checkIncomplete(f);
+ checkIncomplete(CompletableFuture.allOf(fs));
+ if (i != k / 2) {
+ fs[i].complete(i);
+ checkCompletedNormally(fs[i], i);
+ } else {
+ fs[i].completeExceptionally(ex);
+ checkCompletedExceptionally(fs[i], ex);
+ }
+ }
+ checkCompletedWithWrappedException(f, ex);
+ checkCompletedWithWrappedException(CompletableFuture.allOf(fs), ex);
+ }
+ }
+
+ /**
+ * anyOf(no component futures) returns an incomplete future
+ */
+ public void testAnyOf_empty() throws Exception {
+ for (Integer v1 : new Integer[] { 1, null })
+ {
+ CompletableFuture<Object> f = CompletableFuture.anyOf();
+ checkIncomplete(f);
+
+ f.complete(v1);
+ checkCompletedNormally(f, v1);
+ }}
+
+ /**
+ * anyOf returns a future completed normally with a value when
+ * a component future does
+ */
+ public void testAnyOf_normal() throws Exception {
+ for (int k = 0; k < 10; k++) {
+ CompletableFuture[] fs = new CompletableFuture[k];
+ for (int i = 0; i < k; i++)
+ fs[i] = new CompletableFuture<>();
+ CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
+ checkIncomplete(f);
+ for (int i = 0; i < k; i++) {
+ fs[i].complete(i);
+ checkCompletedNormally(f, 0);
+ int x = (int) CompletableFuture.anyOf(fs).join();
+ assertTrue(0 <= x && x <= i);
+ }
+ }
+ }
+ public void testAnyOf_normal_backwards() throws Exception {
+ for (int k = 0; k < 10; k++) {
+ CompletableFuture[] fs = new CompletableFuture[k];
+ for (int i = 0; i < k; i++)
+ fs[i] = new CompletableFuture<>();
+ CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
+ checkIncomplete(f);
+ for (int i = k - 1; i >= 0; i--) {
+ fs[i].complete(i);
+ checkCompletedNormally(f, k - 1);
+ int x = (int) CompletableFuture.anyOf(fs).join();
+ assertTrue(i <= x && x <= k - 1);
+ }
+ }
+ }
+
+ /**
+ * anyOf result completes exceptionally when any component does.
+ */
+ public void testAnyOf_exceptional() throws Exception {
+ for (int k = 0; k < 10; k++) {
+ CompletableFuture[] fs = new CompletableFuture[k];
+ CFException[] exs = new CFException[k];
+ for (int i = 0; i < k; i++) {
+ fs[i] = new CompletableFuture<>();
+ exs[i] = new CFException();
+ }
+ CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
+ checkIncomplete(f);
+ for (int i = 0; i < k; i++) {
+ fs[i].completeExceptionally(exs[i]);
+ checkCompletedWithWrappedException(f, exs[0]);
+ checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs));
+ }
+ }
+ }
+
+ public void testAnyOf_exceptional_backwards() throws Exception {
+ for (int k = 0; k < 10; k++) {
+ CompletableFuture[] fs = new CompletableFuture[k];
+ CFException[] exs = new CFException[k];
+ for (int i = 0; i < k; i++) {
+ fs[i] = new CompletableFuture<>();
+ exs[i] = new CFException();
+ }
+ CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
+ checkIncomplete(f);
+ for (int i = k - 1; i >= 0; i--) {
+ fs[i].completeExceptionally(exs[i]);
+ checkCompletedWithWrappedException(f, exs[k - 1]);
+ checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs));
+ }
+ }
+ }
+
+ /**
+ * Completion methods throw NullPointerException with null arguments
+ */
+ public void testNPE() {
+ CompletableFuture<Integer> f = new CompletableFuture<>();
+ CompletableFuture<Integer> g = new CompletableFuture<>();
+ CompletableFuture<Integer> nullFuture = (CompletableFuture<Integer>)null;
+ ThreadExecutor exec = new ThreadExecutor();
+
+ Runnable[] throwingActions = {
+ () -> CompletableFuture.supplyAsync(null),
+ () -> CompletableFuture.supplyAsync(null, exec),
+ () -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.SYNC, 42), null),
+
+ () -> CompletableFuture.runAsync(null),
+ () -> CompletableFuture.runAsync(null, exec),
+ () -> CompletableFuture.runAsync(() -> {}, null),
+
+ () -> f.completeExceptionally(null),
+
+ () -> f.thenApply(null),
+ () -> f.thenApplyAsync(null),
+ () -> f.thenApplyAsync((x) -> x, null),
+ () -> f.thenApplyAsync(null, exec),
+
+ () -> f.thenAccept(null),
+ () -> f.thenAcceptAsync(null),
+ () -> f.thenAcceptAsync((x) -> {} , null),
+ () -> f.thenAcceptAsync(null, exec),
+
+ () -> f.thenRun(null),
+ () -> f.thenRunAsync(null),
+ () -> f.thenRunAsync(() -> {} , null),
+ () -> f.thenRunAsync(null, exec),
+
+ () -> f.thenCombine(g, null),
+ () -> f.thenCombineAsync(g, null),
+ () -> f.thenCombineAsync(g, null, exec),
+ () -> f.thenCombine(nullFuture, (x, y) -> x),
+ () -> f.thenCombineAsync(nullFuture, (x, y) -> x),
+ () -> f.thenCombineAsync(nullFuture, (x, y) -> x, exec),
+ () -> f.thenCombineAsync(g, (x, y) -> x, null),
+
+ () -> f.thenAcceptBoth(g, null),
+ () -> f.thenAcceptBothAsync(g, null),
+ () -> f.thenAcceptBothAsync(g, null, exec),
+ () -> f.thenAcceptBoth(nullFuture, (x, y) -> {}),
+ () -> f.thenAcceptBothAsync(nullFuture, (x, y) -> {}),
+ () -> f.thenAcceptBothAsync(nullFuture, (x, y) -> {}, exec),
+ () -> f.thenAcceptBothAsync(g, (x, y) -> {}, null),
+
+ () -> f.runAfterBoth(g, null),
+ () -> f.runAfterBothAsync(g, null),
+ () -> f.runAfterBothAsync(g, null, exec),
+ () -> f.runAfterBoth(nullFuture, () -> {}),
+ () -> f.runAfterBothAsync(nullFuture, () -> {}),
+ () -> f.runAfterBothAsync(nullFuture, () -> {}, exec),
+ () -> f.runAfterBothAsync(g, () -> {}, null),
+
+ () -> f.applyToEither(g, null),
+ () -> f.applyToEitherAsync(g, null),
+ () -> f.applyToEitherAsync(g, null, exec),
+ () -> f.applyToEither(nullFuture, (x) -> x),
+ () -> f.applyToEitherAsync(nullFuture, (x) -> x),
+ () -> f.applyToEitherAsync(nullFuture, (x) -> x, exec),
+ () -> f.applyToEitherAsync(g, (x) -> x, null),
+
+ () -> f.acceptEither(g, null),
+ () -> f.acceptEitherAsync(g, null),
+ () -> f.acceptEitherAsync(g, null, exec),
+ () -> f.acceptEither(nullFuture, (x) -> {}),
+ () -> f.acceptEitherAsync(nullFuture, (x) -> {}),
+ () -> f.acceptEitherAsync(nullFuture, (x) -> {}, exec),
+ () -> f.acceptEitherAsync(g, (x) -> {}, null),
+
+ () -> f.runAfterEither(g, null),
+ () -> f.runAfterEitherAsync(g, null),
+ () -> f.runAfterEitherAsync(g, null, exec),
+ () -> f.runAfterEither(nullFuture, () -> {}),
+ () -> f.runAfterEitherAsync(nullFuture, () -> {}),
+ () -> f.runAfterEitherAsync(nullFuture, () -> {}, exec),
+ () -> f.runAfterEitherAsync(g, () -> {}, null),
+
+ () -> f.thenCompose(null),
+ () -> f.thenComposeAsync(null),
+ () -> f.thenComposeAsync(new CompletableFutureInc(ExecutionMode.EXECUTOR), null),
+ () -> f.thenComposeAsync(null, exec),
+
+ () -> f.exceptionally(null),
+
+ () -> f.handle(null),
+
+ () -> CompletableFuture.allOf((CompletableFuture<?>)null),
+ () -> CompletableFuture.allOf((CompletableFuture<?>[])null),
+ () -> CompletableFuture.allOf(f, null),
+ () -> CompletableFuture.allOf(null, f),
+
+ () -> CompletableFuture.anyOf((CompletableFuture<?>)null),
+ () -> CompletableFuture.anyOf((CompletableFuture<?>[])null),
+ () -> CompletableFuture.anyOf(f, null),
+ () -> CompletableFuture.anyOf(null, f),
+
+ () -> f.obtrudeException(null),
+
+ () -> CompletableFuture.delayedExecutor(1L, SECONDS, null),
+ () -> CompletableFuture.delayedExecutor(1L, null, new ThreadExecutor()),
+ () -> CompletableFuture.delayedExecutor(1L, null),
+
+ () -> f.orTimeout(1L, null),
+ () -> f.completeOnTimeout(42, 1L, null),
+
+ () -> CompletableFuture.failedFuture(null),
+ () -> CompletableFuture.failedStage(null),
+ };
+
+ assertThrows(NullPointerException.class, throwingActions);
+ assertEquals(0, exec.count.get());
+ }
+
+ /**
+ * toCompletableFuture returns this CompletableFuture.
+ */
+ public void testToCompletableFuture() {
+ CompletableFuture<Integer> f = new CompletableFuture<>();
+ assertSame(f, f.toCompletableFuture());
+ }
+
+ // jdk9
+
+ /**
+ * newIncompleteFuture returns an incomplete CompletableFuture
+ */
+ public void testNewIncompleteFuture() {
+ for (Integer v1 : new Integer[] { 1, null })
+ {
+ CompletableFuture<Integer> f = new CompletableFuture<>();
+ CompletableFuture<Integer> g = f.newIncompleteFuture();
+ checkIncomplete(f);
+ checkIncomplete(g);
+ f.complete(v1);
+ checkCompletedNormally(f, v1);
+ checkIncomplete(g);
+ g.complete(v1);
+ checkCompletedNormally(g, v1);
+ assertSame(g.getClass(), CompletableFuture.class);
+ }}
+
+ /**
+ * completedStage returns a completed CompletionStage
+ */
+ public void testCompletedStage() {
+ AtomicInteger x = new AtomicInteger(0);
+ AtomicReference<Throwable> r = new AtomicReference<Throwable>();
+ CompletionStage<Integer> f = CompletableFuture.completedStage(1);
+ f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
+ assertEquals(x.get(), 1);
+ assertNull(r.get());
+ }
+
+ /**
+ * defaultExecutor by default returns the commonPool if
+ * it supports more than one thread.
+ */
+ public void testDefaultExecutor() {
+ CompletableFuture<Integer> f = new CompletableFuture<>();
+ Executor e = f.defaultExecutor();
+ Executor c = ForkJoinPool.commonPool();
+ if (ForkJoinPool.getCommonPoolParallelism() > 1)
+ assertSame(e, c);
+ else
+ assertNotSame(e, c);
+ }
+
+ /**
+ * failedFuture returns a CompletableFuture completed
+ * exceptionally with the given Exception
+ */
+ public void testFailedFuture() {
+ CFException ex = new CFException();
+ CompletableFuture<Integer> f = CompletableFuture.failedFuture(ex);
+ checkCompletedExceptionally(f, ex);
+ }
+
+ /**
+ * failedFuture(null) throws NPE
+ */
+ public void testFailedFuture_null() {
+ try {
+ CompletableFuture<Integer> f = CompletableFuture.failedFuture(null);
+ shouldThrow();
+ } catch (NullPointerException success) {}
+ }
+
+ /**
+ * copy returns a CompletableFuture that is completed normally,
+ * with the same value, when source is.
+ */
+ public void testCopy() {
+ CompletableFuture<Integer> f = new CompletableFuture<>();
+ CompletableFuture<Integer> g = f.copy();
+ checkIncomplete(f);
+ checkIncomplete(g);
+ f.complete(1);
+ checkCompletedNormally(f, 1);
+ checkCompletedNormally(g, 1);
+ }
+
+ /**
+ * copy returns a CompletableFuture that is completed exceptionally
+ * when source is.
+ */
+ public void testCopy2() {
+ CompletableFuture<Integer> f = new CompletableFuture<>();
+ CompletableFuture<Integer> g = f.copy();
+ checkIncomplete(f);
+ checkIncomplete(g);
+ CFException ex = new CFException();
+ f.completeExceptionally(ex);
+ checkCompletedExceptionally(f, ex);
+ checkCompletedWithWrappedException(g, ex);
+ }
+
+ /**
+ * minimalCompletionStage returns a CompletableFuture that is
+ * completed normally, with the same value, when source is.
+ */
+ public void testMinimalCompletionStage() {
+ CompletableFuture<Integer> f = new CompletableFuture<>();
+ CompletionStage<Integer> g = f.minimalCompletionStage();
+ AtomicInteger x = new AtomicInteger(0);
+ AtomicReference<Throwable> r = new AtomicReference<Throwable>();
+ checkIncomplete(f);
+ g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
+ f.complete(1);
+ checkCompletedNormally(f, 1);
+ assertEquals(x.get(), 1);
+ assertNull(r.get());
+ }
+
+ /**
+ * minimalCompletionStage returns a CompletableFuture that is
+ * completed exceptionally when source is.
+ */
+ public void testMinimalCompletionStage2() {
+ CompletableFuture<Integer> f = new CompletableFuture<>();
+ CompletionStage<Integer> g = f.minimalCompletionStage();
+ AtomicInteger x = new AtomicInteger(0);
+ AtomicReference<Throwable> r = new AtomicReference<Throwable>();
+ g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
+ checkIncomplete(f);
+ CFException ex = new CFException();
+ f.completeExceptionally(ex);
+ checkCompletedExceptionally(f, ex);
+ assertEquals(x.get(), 0);
+ assertEquals(r.get().getCause(), ex);
+ }
+
+ /**
+ * failedStage returns a CompletionStage completed
+ * exceptionally with the given Exception
+ */
+ public void testFailedStage() {
+ CFException ex = new CFException();
+ CompletionStage<Integer> f = CompletableFuture.failedStage(ex);
+ AtomicInteger x = new AtomicInteger(0);
+ AtomicReference<Throwable> r = new AtomicReference<Throwable>();
+ f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
+ assertEquals(x.get(), 0);
+ assertEquals(r.get(), ex);
+ }
+
+ /**
+ * completeAsync completes with value of given supplier
+ */
+ public void testCompleteAsync() {
+ for (Integer v1 : new Integer[] { 1, null })
+ {
+ CompletableFuture<Integer> f = new CompletableFuture<>();
+ f.completeAsync(() -> v1);
+ f.join();
+ checkCompletedNormally(f, v1);
+ }}
+
+ /**
+ * completeAsync completes exceptionally if given supplier throws
+ */
+ public void testCompleteAsync2() {
+ CompletableFuture<Integer> f = new CompletableFuture<>();
+ CFException ex = new CFException();
+ f.completeAsync(() -> {if (true) throw ex; return 1;});
+ try {
+ f.join();
+ shouldThrow();
+ } catch (CompletionException success) {}
+ checkCompletedWithWrappedException(f, ex);
+ }
+
+ /**
+ * completeAsync with given executor completes with value of given supplier
+ */
+ public void testCompleteAsync3() {
+ for (Integer v1 : new Integer[] { 1, null })
+ {
+ CompletableFuture<Integer> f = new CompletableFuture<>();
+ ThreadExecutor executor = new ThreadExecutor();
+ f.completeAsync(() -> v1, executor);
+ assertSame(v1, f.join());
+ checkCompletedNormally(f, v1);
+ assertEquals(1, executor.count.get());
+ }}
+
+ /**
+ * completeAsync with given executor completes exceptionally if
+ * given supplier throws
+ */
+ public void testCompleteAsync4() {
+ CompletableFuture<Integer> f = new CompletableFuture<>();
+ CFException ex = new CFException();
+ ThreadExecutor executor = new ThreadExecutor();
+ f.completeAsync(() -> {if (true) throw ex; return 1;}, executor);
+ try {
+ f.join();
+ shouldThrow();
+ } catch (CompletionException success) {}
+ checkCompletedWithWrappedException(f, ex);
+ assertEquals(1, executor.count.get());
+ }
+
+ /**
+ * orTimeout completes with TimeoutException if not complete
+ */
+ public void testOrTimeout_timesOut() {
+ long timeoutMillis = timeoutMillis();
+ CompletableFuture<Integer> f = new CompletableFuture<>();
+ long startTime = System.nanoTime();
+ f.orTimeout(timeoutMillis, MILLISECONDS);
+ checkCompletedWithTimeoutException(f);
+ assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
+ }
+
+ /**
+ * orTimeout completes normally if completed before timeout
+ */
+ public void testOrTimeout_completed() {
+ for (Integer v1 : new Integer[] { 1, null })
+ {
+ CompletableFuture<Integer> f = new CompletableFuture<>();
+ CompletableFuture<Integer> g = new CompletableFuture<>();
+ long startTime = System.nanoTime();
+ f.complete(v1);
+ f.orTimeout(LONG_DELAY_MS, MILLISECONDS);
+ g.orTimeout(LONG_DELAY_MS, MILLISECONDS);
+ g.complete(v1);
+ checkCompletedNormally(f, v1);
+ checkCompletedNormally(g, v1);
+ assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
+ }}
+
+ /**
+ * completeOnTimeout completes with given value if not complete
+ */
+ public void testCompleteOnTimeout_timesOut() {
+ testInParallel(() -> testCompleteOnTimeout_timesOut(42),
+ () -> testCompleteOnTimeout_timesOut(null));
+ }
+
+ public void testCompleteOnTimeout_timesOut(Integer v) {
+ long timeoutMillis = timeoutMillis();
+ CompletableFuture<Integer> f = new CompletableFuture<>();
+ long startTime = System.nanoTime();
+ f.completeOnTimeout(v, timeoutMillis, MILLISECONDS);
+ assertSame(v, f.join());
+ assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
+ f.complete(99); // should have no effect
+ checkCompletedNormally(f, v);
+ }
+
+ /**
+ * completeOnTimeout has no effect if completed within timeout
+ */
+ public void testCompleteOnTimeout_completed() {
+ for (Integer v1 : new Integer[] { 1, null })
+ {
+ CompletableFuture<Integer> f = new CompletableFuture<>();
+ CompletableFuture<Integer> g = new CompletableFuture<>();
+ long startTime = System.nanoTime();
+ f.complete(v1);
+ f.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS);
+ g.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS);
+ g.complete(v1);
+ checkCompletedNormally(f, v1);
+ checkCompletedNormally(g, v1);
+ assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
+ }}
+
+ /**
+ * delayedExecutor returns an executor that delays submission
+ */
+ public void testDelayedExecutor() {
+ testInParallel(() -> testDelayedExecutor(null, null),
+ () -> testDelayedExecutor(null, 1),
+ () -> testDelayedExecutor(new ThreadExecutor(), 1),
+ () -> testDelayedExecutor(new ThreadExecutor(), 1));
+ }
+
+ public void testDelayedExecutor(Executor executor, Integer v) throws Exception {
+ long timeoutMillis = timeoutMillis();
+ // Use an "unreasonably long" long timeout to catch lingering threads
+ long longTimeoutMillis = 1000 * 60 * 60 * 24;
+ final Executor delayer, longDelayer;
+ if (executor == null) {
+ delayer = CompletableFuture.delayedExecutor(timeoutMillis, MILLISECONDS);
+ longDelayer = CompletableFuture.delayedExecutor(longTimeoutMillis, MILLISECONDS);
+ } else {
+ delayer = CompletableFuture.delayedExecutor(timeoutMillis, MILLISECONDS, executor);
+ longDelayer = CompletableFuture.delayedExecutor(longTimeoutMillis, MILLISECONDS, executor);
+ }
+ long startTime = System.nanoTime();
+ CompletableFuture<Integer> f =
+ CompletableFuture.supplyAsync(() -> v, delayer);
+ CompletableFuture<Integer> g =
+ CompletableFuture.supplyAsync(() -> v, longDelayer);
+
+ assertNull(g.getNow(null));
+
+ assertSame(v, f.get(LONG_DELAY_MS, MILLISECONDS));
+ long millisElapsed = millisElapsedSince(startTime);
+ assertTrue(millisElapsed >= timeoutMillis);
+ assertTrue(millisElapsed < LONG_DELAY_MS / 2);
+
+ checkCompletedNormally(f, v);
+
+ checkIncomplete(g);
+ assertTrue(g.cancel(true));
+ }
+
+ //--- tests of implementation details; not part of official tck ---
+
+ Object resultOf(CompletableFuture<?> f) {
+ try {
+ java.lang.reflect.Field resultField
+ = CompletableFuture.class.getDeclaredField("result");
+ resultField.setAccessible(true);
+ return resultField.get(f);
+ } catch (Throwable t) { throw new AssertionError(t); }
+ }
+
+ public void testExceptionPropagationReusesResultObject() {
+ if (!testImplementationDetails) return;
+ for (ExecutionMode m : ExecutionMode.values())
+ {
+ final CFException ex = new CFException();
+ final CompletableFuture<Integer> v42 = CompletableFuture.completedFuture(42);
+ final CompletableFuture<Integer> incomplete = new CompletableFuture<>();
+
+ List<Function<CompletableFuture<Integer>, CompletableFuture<?>>> funs
+ = new ArrayList<>();
+
+ funs.add((y) -> m.thenRun(y, new Noop(m)));
+ funs.add((y) -> m.thenAccept(y, new NoopConsumer(m)));
+ funs.add((y) -> m.thenApply(y, new IncFunction(m)));
+
+ funs.add((y) -> m.runAfterEither(y, incomplete, new Noop(m)));
+ funs.add((y) -> m.acceptEither(y, incomplete, new NoopConsumer(m)));
+ funs.add((y) -> m.applyToEither(y, incomplete, new IncFunction(m)));
+
+ funs.add((y) -> m.runAfterBoth(y, v42, new Noop(m)));
+ funs.add((y) -> m.thenAcceptBoth(y, v42, new SubtractAction(m)));
+ funs.add((y) -> m.thenCombine(y, v42, new SubtractFunction(m)));
+
+ funs.add((y) -> m.whenComplete(y, (Integer r, Throwable t) -> {}));
+
+ funs.add((y) -> m.thenCompose(y, new CompletableFutureInc(m)));
+
+ funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {y, v42}));
+ funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {y, incomplete}));
+
+ for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
+ fun : funs) {
+ CompletableFuture<Integer> f = new CompletableFuture<>();
+ f.completeExceptionally(ex);
+ CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
+ checkCompletedWithWrappedException(src, ex);
+ CompletableFuture<?> dep = fun.apply(src);
+ checkCompletedWithWrappedException(dep, ex);
+ assertSame(resultOf(src), resultOf(dep));
+ }
+
+ for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
+ fun : funs) {
+ CompletableFuture<Integer> f = new CompletableFuture<>();
+ CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
+ CompletableFuture<?> dep = fun.apply(src);
+ f.completeExceptionally(ex);
+ checkCompletedWithWrappedException(src, ex);
+ checkCompletedWithWrappedException(dep, ex);
+ assertSame(resultOf(src), resultOf(dep));
+ }
+
+ for (boolean mayInterruptIfRunning : new boolean[] { true, false })
+ for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
+ fun : funs) {
+ CompletableFuture<Integer> f = new CompletableFuture<>();
+ f.cancel(mayInterruptIfRunning);
+ checkCancelled(f);
+ CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
+ checkCompletedWithWrappedCancellationException(src);
+ CompletableFuture<?> dep = fun.apply(src);
+ checkCompletedWithWrappedCancellationException(dep);
+ assertSame(resultOf(src), resultOf(dep));
+ }
+
+ for (boolean mayInterruptIfRunning : new boolean[] { true, false })
+ for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
+ fun : funs) {
+ CompletableFuture<Integer> f = new CompletableFuture<>();
+ CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
+ CompletableFuture<?> dep = fun.apply(src);
+ f.cancel(mayInterruptIfRunning);
+ checkCancelled(f);
+ checkCompletedWithWrappedCancellationException(src);
+ checkCompletedWithWrappedCancellationException(dep);
+ assertSame(resultOf(src), resultOf(dep));
+ }
+ }}
+
+ /**
+ * Minimal completion stages throw UOE for all non-CompletionStage methods
+ */
+ // TODO(streams):
+ // public void testMinimalCompletionStage_minimality() {
+ // if (!testImplementationDetails) return;
+ // Function<Method, String> toSignature =
+ // (method) -> method.getName() + Arrays.toString(method.getParameterTypes());
+ // Predicate<Method> isNotStatic =
+ // (method) -> (method.getModifiers() & Modifier.STATIC) == 0;
+ // List<Method> minimalMethods =
+ // Stream.of(Object.class, CompletionStage.class)
+ // .flatMap((klazz) -> Stream.of(klazz.getMethods()))
+ // .filter(isNotStatic)
+ // .collect(Collectors.toList());
+ // // Methods from CompletableFuture permitted NOT to throw UOE
+ // String[] signatureWhitelist = {
+ // "newIncompleteFuture[]",
+ // "defaultExecutor[]",
+ // "minimalCompletionStage[]",
+ // "copy[]",
+ // };
+ // Set<String> permittedMethodSignatures =
+ // Stream.concat(minimalMethods.stream().map(toSignature),
+ // Stream.of(signatureWhitelist))
+ // .collect(Collectors.toSet());
+ // List<Method> allMethods = Stream.of(CompletableFuture.class.getMethods())
+ // .filter(isNotStatic)
+ // .filter((method) -> !permittedMethodSignatures.contains(toSignature.apply(method)))
+ // .collect(Collectors.toList());
+
+ // CompletionStage<Integer> minimalStage =
+ // new CompletableFuture<Integer>().minimalCompletionStage();
+
+ // List<Method> bugs = new ArrayList<>();
+ // for (Method method : allMethods) {
+ // Class<?>[] parameterTypes = method.getParameterTypes();
+ // Object[] args = new Object[parameterTypes.length];
+ // // Manufacture boxed primitives for primitive params
+ // for (int i = 0; i < args.length; i++) {
+ // Class<?> type = parameterTypes[i];
+ // if (parameterTypes[i] == boolean.class)
+ // args[i] = false;
+ // else if (parameterTypes[i] == int.class)
+ // args[i] = 0;
+ // else if (parameterTypes[i] == long.class)
+ // args[i] = 0L;
+ // }
+ // try {
+ // method.invoke(minimalStage, args);
+ // bugs.add(method);
+ // }
+ // catch (java.lang.reflect.InvocationTargetException expected) {
+ // if (! (expected.getCause() instanceof UnsupportedOperationException)) {
+ // bugs.add(method);
+ // // expected.getCause().printStackTrace();
+ // }
+ // }
+ // catch (ReflectiveOperationException bad) { throw new Error(bad); }
+ // }
+ // if (!bugs.isEmpty())
+ // throw new Error("Methods did not throw UOE: " + bugs.toString());
+ // }
+
+ static class Monad {
+ static class ZeroException extends RuntimeException {
+ public ZeroException() { super("monadic zero"); }
+ }
+ // "return", "unit"
+ static <T> CompletableFuture<T> unit(T value) {
+ return completedFuture(value);
+ }
+ // monadic zero ?
+ static <T> CompletableFuture<T> zero() {
+ return failedFuture(new ZeroException());
+ }
+ // >=>
+ static <T,U,V> Function<T, CompletableFuture<V>> compose
+ (Function<T, CompletableFuture<U>> f,
+ Function<U, CompletableFuture<V>> g) {
+ return (x) -> f.apply(x).thenCompose(g);
+ }
+
+ static void assertZero(CompletableFuture<?> f) {
+ try {
+ f.getNow(null);
+ throw new AssertionFailedError("should throw");
+ } catch (CompletionException success) {
+ assertTrue(success.getCause() instanceof ZeroException);
+ }
+ }
+
+ static <T> void assertFutureEquals(CompletableFuture<T> f,
+ CompletableFuture<T> g) {
+ T fval = null, gval = null;
+ Throwable fex = null, gex = null;
+
+ try { fval = f.get(); }
+ catch (ExecutionException ex) { fex = ex.getCause(); }
+ catch (Throwable ex) { fex = ex; }
+
+ try { gval = g.get(); }
+ catch (ExecutionException ex) { gex = ex.getCause(); }
+ catch (Throwable ex) { gex = ex; }
+
+ if (fex != null || gex != null)
+ assertSame(fex.getClass(), gex.getClass());
+ else
+ assertEquals(fval, gval);
+ }
+
+ static class PlusFuture<T> extends CompletableFuture<T> {
+ AtomicReference<Throwable> firstFailure = new AtomicReference<>(null);
+ }
+
+ /** Implements "monadic plus". */
+ static <T> CompletableFuture<T> plus(CompletableFuture<? extends T> f,
+ CompletableFuture<? extends T> g) {
+ PlusFuture<T> plus = new PlusFuture<T>();
+ BiConsumer<T, Throwable> action = (T result, Throwable ex) -> {
+ try {
+ if (ex == null) {
+ if (plus.complete(result))
+ if (plus.firstFailure.get() != null)
+ plus.firstFailure.set(null);
+ }
+ else if (plus.firstFailure.compareAndSet(null, ex)) {
+ if (plus.isDone())
+ plus.firstFailure.set(null);
+ }
+ else {
+ // first failure has precedence
+ Throwable first = plus.firstFailure.getAndSet(null);
+
+ // may fail with "Self-suppression not permitted"
+ try { first.addSuppressed(ex); }
+ catch (Exception ignored) {}
+
+ plus.completeExceptionally(first);
+ }
+ } catch (Throwable unexpected) {
+ plus.completeExceptionally(unexpected);
+ }
+ };
+ f.whenComplete(action);
+ g.whenComplete(action);
+ return plus;
+ }
+ }
+
+ /**
+ * CompletableFuture is an additive monad - sort of.
+ * https://en.wikipedia.org/wiki/Monad_(functional_programming)#Additive_monads
+ */
+ public void testAdditiveMonad() throws Throwable {
+ Function<Long, CompletableFuture<Long>> unit = Monad::unit;
+ CompletableFuture<Long> zero = Monad.zero();
+
+ // Some mutually non-commutative functions
+ Function<Long, CompletableFuture<Long>> triple
+ = (x) -> Monad.unit(3 * x);
+ Function<Long, CompletableFuture<Long>> inc
+ = (x) -> Monad.unit(x + 1);
+
+ // unit is a right identity: m >>= unit === m
+ Monad.assertFutureEquals(inc.apply(5L).thenCompose(unit),
+ inc.apply(5L));
+ // unit is a left identity: (unit x) >>= f === f x
+ Monad.assertFutureEquals(unit.apply(5L).thenCompose(inc),
+ inc.apply(5L));
+
+ // associativity: (m >>= f) >>= g === m >>= ( \x -> (f x >>= g) )
+ Monad.assertFutureEquals(
+ unit.apply(5L).thenCompose(inc).thenCompose(triple),
+ unit.apply(5L).thenCompose((x) -> inc.apply(x).thenCompose(triple)));
+
+ // The case for CompletableFuture as an additive monad is weaker...
+
+ // zero is a monadic zero
+ Monad.assertZero(zero);
+
+ // left zero: zero >>= f === zero
+ Monad.assertZero(zero.thenCompose(inc));
+ // right zero: f >>= (\x -> zero) === zero
+ Monad.assertZero(inc.apply(5L).thenCompose((x) -> zero));
+
+ // f plus zero === f
+ Monad.assertFutureEquals(Monad.unit(5L),
+ Monad.plus(Monad.unit(5L), zero));
+ // zero plus f === f
+ Monad.assertFutureEquals(Monad.unit(5L),
+ Monad.plus(zero, Monad.unit(5L)));
+ // zero plus zero === zero
+ Monad.assertZero(Monad.plus(zero, zero));
+ {
+ CompletableFuture<Long> f = Monad.plus(Monad.unit(5L),
+ Monad.unit(8L));
+ // non-determinism
+ assertTrue(f.get() == 5L || f.get() == 8L);
+ }
+
+ CompletableFuture<Long> godot = new CompletableFuture<>();
+ // f plus godot === f (doesn't wait for godot)
+ Monad.assertFutureEquals(Monad.unit(5L),
+ Monad.plus(Monad.unit(5L), godot));
+ // godot plus f === f (doesn't wait for godot)
+ Monad.assertFutureEquals(Monad.unit(5L),
+ Monad.plus(godot, Monad.unit(5L)));
+ }
+
+// static <U> U join(CompletionStage<U> stage) {
+// CompletableFuture<U> f = new CompletableFuture<>();
+// stage.whenComplete((v, ex) -> {
+// if (ex != null) f.completeExceptionally(ex); else f.complete(v);
+// });
+// return f.join();
+// }
+
+// static <U> boolean isDone(CompletionStage<U> stage) {
+// CompletableFuture<U> f = new CompletableFuture<>();
+// stage.whenComplete((v, ex) -> {
+// if (ex != null) f.completeExceptionally(ex); else f.complete(v);
+// });
+// return f.isDone();
+// }
+
+// static <U> U join2(CompletionStage<U> stage) {
+// return stage.toCompletableFuture().copy().join();
+// }
+
+// static <U> boolean isDone2(CompletionStage<U> stage) {
+// return stage.toCompletableFuture().copy().isDone();
+// }
+
+}
diff --git a/jsr166-tests/src/test/java/jsr166/ConcurrentHashMap8Test.java b/jsr166-tests/src/test/java/jsr166/ConcurrentHashMap8Test.java
new file mode 100644
index 0000000..60de8b9
--- /dev/null
+++ b/jsr166-tests/src/test/java/jsr166/ConcurrentHashMap8Test.java
@@ -0,0 +1,1096 @@
+/*
+ * Written by Doug Lea with assistance from members of JCP JSR-166
+ * Expert Group and released to the public domain, as explained at
+ * http://creativecommons.org/publicdomain/zero/1.0/
+ */
+
+package jsr166;
+
+import static java.util.Spliterator.CONCURRENT;
+import static java.util.Spliterator.DISTINCT;
+import static java.util.Spliterator.NONNULL;
+
+import java.util.AbstractMap;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.NoSuchElementException;
+import java.util.Set;
+import java.util.Spliterator;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.LongAdder;
+import java.util.function.BiFunction;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+public class ConcurrentHashMap8Test 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(ConcurrentHashMap8Test.class);
+ // }
+
+ /**
+ * Returns a new map from Integers 1-5 to Strings "A"-"E".
+ */
+ private static ConcurrentHashMap map5() {
+ ConcurrentHashMap map = new ConcurrentHashMap(5);
+ assertTrue(map.isEmpty());
+ map.put(one, "A");
+ map.put(two, "B");
+ map.put(three, "C");
+ map.put(four, "D");
+ map.put(five, "E");
+ assertFalse(map.isEmpty());
+ assertEquals(5, map.size());
+ return map;
+ }
+
+ /**
+ * getOrDefault returns value if present, else default
+ */
+ public void testGetOrDefault() {
+ ConcurrentHashMap map = map5();
+ assertEquals(map.getOrDefault(one, "Z"), "A");
+ assertEquals(map.getOrDefault(six, "Z"), "Z");
+ }
+
+ /**
+ * computeIfAbsent adds when the given key is not present
+ */
+ public void testComputeIfAbsent() {
+ ConcurrentHashMap map = map5();
+ map.computeIfAbsent(six, (x) -> "Z");
+ assertTrue(map.containsKey(six));
+ }
+
+ /**
+ * computeIfAbsent does not replace if the key is already present
+ */
+ public void testComputeIfAbsent2() {
+ ConcurrentHashMap map = map5();
+ assertEquals("A", map.computeIfAbsent(one, (x) -> "Z"));
+ }
+
+ /**
+ * computeIfAbsent does not add if function returns null
+ */
+ public void testComputeIfAbsent3() {
+ ConcurrentHashMap map = map5();
+ map.computeIfAbsent(six, (x) -> null);
+ assertFalse(map.containsKey(six));
+ }
+
+ /**
+ * computeIfPresent does not replace if the key is already present
+ */
+ public void testComputeIfPresent() {
+ ConcurrentHashMap map = map5();
+ map.computeIfPresent(six, (x, y) -> "Z");
+ assertFalse(map.containsKey(six));
+ }
+
+ /**
+ * computeIfPresent adds when the given key is not present
+ */
+ public void testComputeIfPresent2() {
+ ConcurrentHashMap map = map5();
+ assertEquals("Z", map.computeIfPresent(one, (x, y) -> "Z"));
+ }
+
+ /**
+ * compute does not replace if the function returns null
+ */
+ public void testCompute() {
+ ConcurrentHashMap map = map5();
+ map.compute(six, (x, y) -> null);
+ assertFalse(map.containsKey(six));
+ }
+
+ /**
+ * compute adds when the given key is not present
+ */
+ public void testCompute2() {
+ ConcurrentHashMap map = map5();
+ assertEquals("Z", map.compute(six, (x, y) -> "Z"));
+ }
+
+ /**
+ * compute replaces when the given key is present
+ */
+ public void testCompute3() {
+ ConcurrentHashMap map = map5();
+ assertEquals("Z", map.compute(one, (x, y) -> "Z"));
+ }
+
+ /**
+ * compute removes when the given key is present and function returns null
+ */
+ public void testCompute4() {
+ ConcurrentHashMap map = map5();
+ map.compute(one, (x, y) -> null);
+ assertFalse(map.containsKey(one));
+ }
+
+ /**
+ * merge adds when the given key is not present
+ */
+ public void testMerge1() {
+ ConcurrentHashMap map = map5();
+ assertEquals("Y", map.merge(six, "Y", (x, y) -> "Z"));
+ }
+
+ /**
+ * merge replaces when the given key is present
+ */
+ public void testMerge2() {
+ ConcurrentHashMap map = map5();
+ assertEquals("Z", map.merge(one, "Y", (x, y) -> "Z"));
+ }
+
+ /**
+ * merge removes when the given key is present and function returns null
+ */
+ public void testMerge3() {
+ ConcurrentHashMap map = map5();
+ map.merge(one, "Y", (x, y) -> null);
+ assertFalse(map.containsKey(one));
+ }
+
+ static Set<Integer> populatedSet(int n) {
+ Set<Integer> a = ConcurrentHashMap.<Integer>newKeySet();
+ assertTrue(a.isEmpty());
+ for (int i = 0; i < n; i++)
+ assertTrue(a.add(i));
+ assertEquals(n == 0, a.isEmpty());
+ assertEquals(n, a.size());
+ return a;
+ }
+
+ static Set populatedSet(Integer[] elements) {
+ Set<Integer> a = ConcurrentHashMap.<Integer>newKeySet();
+ assertTrue(a.isEmpty());
+ for (int i = 0; i < elements.length; i++)
+ assertTrue(a.add(elements[i]));
+ assertFalse(a.isEmpty());
+ assertEquals(elements.length, a.size());
+ return a;
+ }
+
+ /**
+ * replaceAll replaces all matching values.
+ */
+ public void testReplaceAll() {
+ ConcurrentHashMap<Integer, String> map = map5();
+ map.replaceAll((x, y) -> { return x > 3 ? "Z" : y; });
+ assertEquals("A", map.get(one));
+ assertEquals("B", map.get(two));
+ assertEquals("C", map.get(three));
+ assertEquals("Z", map.get(four));
+ assertEquals("Z", map.get(five));
+ }
+
+ /**
+ * Default-constructed set is empty
+ */
+ public void testNewKeySet() {
+ Set a = ConcurrentHashMap.newKeySet();
+ assertTrue(a.isEmpty());
+ }
+
+ /**
+ * keySet.add adds the key with the established value to the map;
+ * remove removes it.
+ */
+ public void testKeySetAddRemove() {
+ ConcurrentHashMap map = map5();
+ Set set1 = map.keySet();
+ Set set2 = map.keySet(true);
+ set2.add(six);
+ assertTrue(((ConcurrentHashMap.KeySetView)set2).getMap() == map);
+ assertTrue(((ConcurrentHashMap.KeySetView)set1).getMap() == map);
+ assertEquals(set2.size(), map.size());
+ assertEquals(set1.size(), map.size());
+ assertTrue((Boolean)map.get(six));
+ assertTrue(set1.contains(six));
+ assertTrue(set2.contains(six));
+ set2.remove(six);
+ assertNull(map.get(six));
+ assertFalse(set1.contains(six));
+ assertFalse(set2.contains(six));
+ }
+
+ /**
+ * keySet.addAll adds each element from the given collection
+ */
+ public void testAddAll() {
+ Set full = populatedSet(3);
+ assertTrue(full.addAll(Arrays.asList(three, four, five)));
+ assertEquals(6, full.size());
+ assertFalse(full.addAll(Arrays.asList(three, four, five)));
+ assertEquals(6, full.size());
+ }
+
+ /**
+ * keySet.addAll adds each element from the given collection that did not
+ * already exist in the set
+ */
+ public void testAddAll2() {
+ Set full = populatedSet(3);
+ // "one" is duplicate and will not be added
+ assertTrue(full.addAll(Arrays.asList(three, four, one)));
+ assertEquals(5, full.size());
+ assertFalse(full.addAll(Arrays.asList(three, four, one)));
+ assertEquals(5, full.size());
+ }
+
+ /**
+ * keySet.add will not add the element if it already exists in the set
+ */
+ public void testAdd2() {
+ Set full = populatedSet(3);
+ assertFalse(full.add(one));
+ assertEquals(3, full.size());
+ }
+
+ /**
+ * keySet.add adds the element when it does not exist in the set
+ */
+ public void testAdd3() {
+ Set full = populatedSet(3);
+ assertTrue(full.add(three));
+ assertTrue(full.contains(three));
+ assertFalse(full.add(three));
+ assertTrue(full.contains(three));
+ }
+
+ /**
+ * keySet.add throws UnsupportedOperationException if no default
+ * mapped value
+ */
+ public void testAdd4() {
+ Set full = map5().keySet();
+ try {
+ full.add(three);
+ shouldThrow();
+ } catch (UnsupportedOperationException success) {}
+ }
+
+ /**
+ * keySet.add throws NullPointerException if the specified key is
+ * null
+ */
+ public void testAdd5() {
+ Set full = populatedSet(3);
+ try {
+ full.add(null);
+ shouldThrow();
+ } catch (NullPointerException success) {}
+ }
+
+ /**
+ * KeySetView.getMappedValue returns the map's mapped value
+ */
+ public void testGetMappedValue() {
+ ConcurrentHashMap map = map5();
+ assertNull(map.keySet().getMappedValue());
+ try {
+ map.keySet(null);
+ shouldThrow();
+ } catch (NullPointerException success) {}
+ ConcurrentHashMap.KeySetView set = map.keySet(one);
+ assertFalse(set.add(one));
+ assertTrue(set.add(six));
+ assertTrue(set.add(seven));
+ assertTrue(set.getMappedValue() == one);
+ assertTrue(map.get(one) != one);
+ assertTrue(map.get(six) == one);
+ assertTrue(map.get(seven) == one);
+ }
+
+ void checkSpliteratorCharacteristics(Spliterator<?> sp,
+ int requiredCharacteristics) {
+ assertEquals(requiredCharacteristics,
+ requiredCharacteristics & sp.characteristics());
+ }
+
+ /**
+ * KeySetView.spliterator returns spliterator over the elements in this set
+ */
+ public void testKeySetSpliterator() {
+ LongAdder adder = new LongAdder();
+ ConcurrentHashMap map = map5();
+ Set set = map.keySet();
+ Spliterator<Integer> sp = set.spliterator();
+ checkSpliteratorCharacteristics(sp, CONCURRENT | DISTINCT | NONNULL);
+ assertEquals(sp.estimateSize(), map.size());
+ Spliterator<Integer> sp2 = sp.trySplit();
+ sp.forEachRemaining((Integer x) -> adder.add(x.longValue()));
+ long v = adder.sumThenReset();
+ sp2.forEachRemaining((Integer x) -> adder.add(x.longValue()));
+ long v2 = adder.sum();
+ assertEquals(v + v2, 15);
+ }
+
+ /**
+ * keyset.clear removes all elements from the set
+ */
+ public void testClear() {
+ Set full = populatedSet(3);
+ full.clear();
+ assertEquals(0, full.size());
+ }
+
+ /**
+ * keyset.contains returns true for added elements
+ */
+ public void testContains() {
+ Set full = populatedSet(3);
+ assertTrue(full.contains(one));
+ assertFalse(full.contains(five));
+ }
+
+ /**
+ * KeySets with equal elements are equal
+ */
+ public void testEquals() {
+ Set a = populatedSet(3);
+ Set b = populatedSet(3);
+ assertTrue(a.equals(b));
+ assertTrue(b.equals(a));
+ assertEquals(a.hashCode(), b.hashCode());
+ a.add(m1);
+ assertFalse(a.equals(b));
+ assertFalse(b.equals(a));
+ b.add(m1);
+ assertTrue(a.equals(b));
+ assertTrue(b.equals(a));
+ assertEquals(a.hashCode(), b.hashCode());
+ }
+
+ /**
+ * KeySet.containsAll returns true for collections with subset of elements
+ */
+ public void testContainsAll() {
+ Collection full = populatedSet(3);
+ assertTrue(full.containsAll(Arrays.asList()));
+ assertTrue(full.containsAll(Arrays.asList(one)));
+ assertTrue(full.containsAll(Arrays.asList(one, two)));
+ assertFalse(full.containsAll(Arrays.asList(one, two, six)));
+ assertFalse(full.containsAll(Arrays.asList(six)));
+ }
+
+ /**
+ * KeySet.isEmpty is true when empty, else false
+ */
+ public void testIsEmpty() {
+ assertTrue(populatedSet(0).isEmpty());
+ assertFalse(populatedSet(3).isEmpty());
+ }
+
+ /**
+ * KeySet.iterator() returns an iterator containing the elements of the
+ * set
+ */
+ public void testIterator() {
+ Collection empty = ConcurrentHashMap.newKeySet();
+ int size = 20;
+ assertFalse(empty.iterator().hasNext());
+ try {
+ empty.iterator().next();
+ shouldThrow();
+ } catch (NoSuchElementException success) {}
+
+ Integer[] elements = new Integer[size];
+ for (int i = 0; i < size; i++)
+ elements[i] = i;
+ Collections.shuffle(Arrays.asList(elements));
+ Collection<Integer> full = populatedSet(elements);
+
+ Iterator it = full.iterator();
+ for (int j = 0; j < size; j++) {
+ assertTrue(it.hasNext());
+ it.next();
+ }
+ assertIteratorExhausted(it);
+ }
+
+ /**
+ * iterator of empty collections has no elements
+ */
+ public void testEmptyIterator() {
+ assertIteratorExhausted(ConcurrentHashMap.newKeySet().iterator());
+ assertIteratorExhausted(new ConcurrentHashMap().entrySet().iterator());
+ assertIteratorExhausted(new ConcurrentHashMap().values().iterator());
+ assertIteratorExhausted(new ConcurrentHashMap().keySet().iterator());
+ }
+
+ /**
+ * KeySet.iterator.remove removes current element
+ */
+ public void testIteratorRemove() {
+ Set q = populatedSet(3);
+ Iterator it = q.iterator();
+ Object removed = it.next();
+ it.remove();
+
+ it = q.iterator();
+ assertFalse(it.next().equals(removed));
+ assertFalse(it.next().equals(removed));
+ assertFalse(it.hasNext());
+ }
+
+ /**
+ * KeySet.toString holds toString of elements
+ */
+ public void testToString() {
+ assertEquals("[]", ConcurrentHashMap.newKeySet().toString());
+ Set full = populatedSet(3);
+ String s = full.toString();
+ for (int i = 0; i < 3; ++i)
+ assertTrue(s.contains(String.valueOf(i)));
+ }
+
+ /**
+ * KeySet.removeAll removes all elements from the given collection
+ */
+ public void testRemoveAll() {
+ Set full = populatedSet(3);
+ assertTrue(full.removeAll(Arrays.asList(one, two)));
+ assertEquals(1, full.size());
+ assertFalse(full.removeAll(Arrays.asList(one, two)));
+ assertEquals(1, full.size());
+ }
+
+ /**
+ * KeySet.remove removes an element
+ */
+ public void testRemove() {
+ Set full = populatedSet(3);
+ full.remove(one);
+ assertFalse(full.contains(one));
+ assertEquals(2, full.size());
+ }
+
+ /**
+ * keySet.size returns the number of elements
+ */
+ public void testSize() {
+ Set empty = ConcurrentHashMap.newKeySet();
+ Set full = populatedSet(3);
+ assertEquals(3, full.size());
+ assertEquals(0, empty.size());
+ }
+
+ /**
+ * KeySet.toArray() returns an Object array containing all elements from
+ * the set
+ */
+ public void testToArray() {
+ Object[] a = ConcurrentHashMap.newKeySet().toArray();
+ assertTrue(Arrays.equals(new Object[0], a));
+ assertSame(Object[].class, a.getClass());
+ int size = 20;
+ Integer[] elements = new Integer[size];
+ for (int i = 0; i < size; i++)
+ elements[i] = i;
+ Collections.shuffle(Arrays.asList(elements));
+ Collection<Integer> full = populatedSet(elements);
+
+ assertTrue(Arrays.asList(elements).containsAll(Arrays.asList(full.toArray())));
+ assertTrue(full.containsAll(Arrays.asList(full.toArray())));
+ assertSame(Object[].class, full.toArray().getClass());
+ }
+
+ /**
+ * toArray(Integer array) returns an Integer array containing all
+ * elements from the set
+ */
+ public void testToArray2() {
+ Collection empty = ConcurrentHashMap.newKeySet();
+ Integer[] a;
+ int size = 20;
+
+ a = new Integer[0];
+ assertSame(a, empty.toArray(a));
+
+ a = new Integer[size / 2];
+ Arrays.fill(a, 42);
+ assertSame(a, empty.toArray(a));
+ assertNull(a[0]);
+ for (int i = 1; i < a.length; i++)
+ assertEquals(42, (int) a[i]);
+
+ Integer[] elements = new Integer[size];
+ for (int i = 0; i < size; i++)
+ elements[i] = i;
+ Collections.shuffle(Arrays.asList(elements));
+ Collection<Integer> full = populatedSet(elements);
+
+ Arrays.fill(a, 42);
+ assertTrue(Arrays.asList(elements).containsAll(Arrays.asList(full.toArray(a))));
+ for (int i = 0; i < a.length; i++)
+ assertEquals(42, (int) a[i]);
+ assertSame(Integer[].class, full.toArray(a).getClass());
+
+ a = new Integer[size];
+ Arrays.fill(a, 42);
+ assertSame(a, full.toArray(a));
+ assertTrue(Arrays.asList(elements).containsAll(Arrays.asList(full.toArray(a))));
+ }
+
+ /**
+ * A deserialized serialized set is equal
+ */
+ public void testSerialization() throws Exception {
+ int size = 20;
+ Set x = populatedSet(size);
+ Set y = serialClone(x);
+
+ assertNotSame(x, y);
+ assertEquals(x.size(), y.size());
+ assertEquals(x, y);
+ assertEquals(y, x);
+ }
+
+ static final int SIZE = 10000;
+ static ConcurrentHashMap<Long, Long> longMap;
+
+ static ConcurrentHashMap<Long, Long> longMap() {
+ if (longMap == null) {
+ longMap = new ConcurrentHashMap<Long, Long>(SIZE);
+ for (int i = 0; i < SIZE; ++i)
+ longMap.put(Long.valueOf(i), Long.valueOf(2 *i));
+ }
+ return longMap;
+ }
+
+ // explicit function class to avoid type inference problems
+ static class AddKeys implements BiFunction<Map.Entry<Long,Long>, Map.Entry<Long,Long>, Map.Entry<Long,Long>> {
+ public Map.Entry<Long,Long> apply(Map.Entry<Long,Long> x, Map.Entry<Long,Long> y) {
+ return new AbstractMap.SimpleEntry<Long,Long>
+ (Long.valueOf(x.getKey().longValue() + y.getKey().longValue()),
+ Long.valueOf(1L));
+ }
+ }
+
+ /**
+ * forEachKeySequentially traverses all keys
+ */
+ public void testForEachKeySequentially() {
+ LongAdder adder = new LongAdder();
+ ConcurrentHashMap<Long, Long> m = longMap();
+ m.forEachKey(Long.MAX_VALUE, (Long x) -> adder.add(x.longValue()));
+ assertEquals(adder.sum(), SIZE * (SIZE - 1) / 2);
+ }
+
+ /**
+ * forEachValueSequentially traverses all values
+ */
+ public void testForEachValueSequentially() {
+ LongAdder adder = new LongAdder();
+ ConcurrentHashMap<Long, Long> m = longMap();
+ m.forEachValue(Long.MAX_VALUE, (Long x) -> adder.add(x.longValue()));
+ assertEquals(adder.sum(), SIZE * (SIZE - 1));
+ }
+
+ /**
+ * forEachSequentially traverses all mappings
+ */
+ public void testForEachSequentially() {
+ LongAdder adder = new LongAdder();
+ ConcurrentHashMap<Long, Long> m = longMap();
+ m.forEach(Long.MAX_VALUE, (Long x, Long y) -> adder.add(x.longValue() + y.longValue()));
+ assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2);
+ }
+
+ /**
+ * forEachEntrySequentially traverses all entries
+ */
+ public void testForEachEntrySequentially() {
+ LongAdder adder = new LongAdder();
+ ConcurrentHashMap<Long, Long> m = longMap();
+ m.forEachEntry(Long.MAX_VALUE, (Map.Entry<Long,Long> e) -> adder.add(e.getKey().longValue() + e.getValue().longValue()));
+ assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2);
+ }
+
+ /**
+ * forEachKeyInParallel traverses all keys
+ */
+ public void testForEachKeyInParallel() {
+ LongAdder adder = new LongAdder();
+ ConcurrentHashMap<Long, Long> m = longMap();
+ m.forEachKey(1L, (Long x) -> adder.add(x.longValue()));
+ assertEquals(adder.sum(), SIZE * (SIZE - 1) / 2);
+ }
+
+ /**
+ * forEachValueInParallel traverses all values
+ */
+ public void testForEachValueInParallel() {
+ LongAdder adder = new LongAdder();
+ ConcurrentHashMap<Long, Long> m = longMap();
+ m.forEachValue(1L, (Long x) -> adder.add(x.longValue()));
+ assertEquals(adder.sum(), SIZE * (SIZE - 1));
+ }
+
+ /**
+ * forEachInParallel traverses all mappings
+ */
+ public void testForEachInParallel() {
+ LongAdder adder = new LongAdder();
+ ConcurrentHashMap<Long, Long> m = longMap();
+ m.forEach(1L, (Long x, Long y) -> adder.add(x.longValue() + y.longValue()));
+ assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2);
+ }
+
+ /**
+ * forEachEntryInParallel traverses all entries
+ */
+ public void testForEachEntryInParallel() {
+ LongAdder adder = new LongAdder();
+ ConcurrentHashMap<Long, Long> m = longMap();
+ m.forEachEntry(1L, (Map.Entry<Long,Long> e) -> adder.add(e.getKey().longValue() + e.getValue().longValue()));
+ assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2);
+ }
+
+ /**
+ * Mapped forEachKeySequentially traverses the given
+ * transformations of all keys
+ */
+ public void testMappedForEachKeySequentially() {
+ LongAdder adder = new LongAdder();
+ ConcurrentHashMap<Long, Long> m = longMap();
+ m.forEachKey(Long.MAX_VALUE, (Long x) -> Long.valueOf(4 * x.longValue()),
+ (Long x) -> adder.add(x.longValue()));
+ assertEquals(adder.sum(), 4 * SIZE * (SIZE - 1) / 2);
+ }
+
+ /**
+ * Mapped forEachValueSequentially traverses the given
+ * transformations of all values
+ */
+ public void testMappedForEachValueSequentially() {
+ LongAdder adder = new LongAdder();
+ ConcurrentHashMap<Long, Long> m = longMap();
+ m.forEachValue(Long.MAX_VALUE, (Long x) -> Long.valueOf(4 * x.longValue()),
+ (Long x) -> adder.add(x.longValue()));
+ assertEquals(adder.sum(), 4 * SIZE * (SIZE - 1));
+ }
+
+ /**
+ * Mapped forEachSequentially traverses the given
+ * transformations of all mappings
+ */
+ public void testMappedForEachSequentially() {
+ LongAdder adder = new LongAdder();
+ ConcurrentHashMap<Long, Long> m = longMap();
+ m.forEach(Long.MAX_VALUE, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()),
+ (Long x) -> adder.add(x.longValue()));
+ assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2);
+ }
+
+ /**
+ * Mapped forEachEntrySequentially traverses the given
+ * transformations of all entries
+ */
+ public void testMappedForEachEntrySequentially() {
+ LongAdder adder = new LongAdder();
+ ConcurrentHashMap<Long, Long> m = longMap();
+ m.forEachEntry(Long.MAX_VALUE, (Map.Entry<Long,Long> e) -> Long.valueOf(e.getKey().longValue() + e.getValue().longValue()),
+ (Long x) -> adder.add(x.longValue()));
+ assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2);
+ }
+
+ /**
+ * Mapped forEachKeyInParallel traverses the given
+ * transformations of all keys
+ */
+ public void testMappedForEachKeyInParallel() {
+ LongAdder adder = new LongAdder();
+ ConcurrentHashMap<Long, Long> m = longMap();
+ m.forEachKey(1L, (Long x) -> Long.valueOf(4 * x.longValue()),
+ (Long x) -> adder.add(x.longValue()));
+ assertEquals(adder.sum(), 4 * SIZE * (SIZE - 1) / 2);
+ }
+
+ /**
+ * Mapped forEachValueInParallel traverses the given
+ * transformations of all values
+ */
+ public void testMappedForEachValueInParallel() {
+ LongAdder adder = new LongAdder();
+ ConcurrentHashMap<Long, Long> m = longMap();
+ m.forEachValue(1L, (Long x) -> Long.valueOf(4 * x.longValue()),
+ (Long x) -> adder.add(x.longValue()));
+ assertEquals(adder.sum(), 4 * SIZE * (SIZE - 1));
+ }
+
+ /**
+ * Mapped forEachInParallel traverses the given
+ * transformations of all mappings
+ */
+ public void testMappedForEachInParallel() {
+ LongAdder adder = new LongAdder();
+ ConcurrentHashMap<Long, Long> m = longMap();
+ m.forEach(1L, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()),
+ (Long x) -> adder.add(x.longValue()));
+ assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2);
+ }
+
+ /**
+ * Mapped forEachEntryInParallel traverses the given
+ * transformations of all entries
+ */
+ public void testMappedForEachEntryInParallel() {
+ LongAdder adder = new LongAdder();
+ ConcurrentHashMap<Long, Long> m = longMap();
+ m.forEachEntry(1L, (Map.Entry<Long,Long> e) -> Long.valueOf(e.getKey().longValue() + e.getValue().longValue()),
+ (Long x) -> adder.add(x.longValue()));
+ assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2);
+ }
+
+ /**
+ * reduceKeysSequentially accumulates across all keys,
+ */
+ public void testReduceKeysSequentially() {
+ ConcurrentHashMap<Long, Long> m = longMap();
+ Long r;
+ r = m.reduceKeys(Long.MAX_VALUE, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()));
+ assertEquals((long)r, (long)SIZE * (SIZE - 1) / 2);
+ }
+
+ /**
+ * reduceValuesSequentially accumulates across all values
+ */
+ public void testReduceValuesSequentially() {
+ ConcurrentHashMap<Long, Long> m = longMap();
+ Long r;
+ r = m.reduceKeys(Long.MAX_VALUE, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()));
+ assertEquals((long)r, (long)SIZE * (SIZE - 1) / 2);
+ }
+
+ /**
+ * reduceEntriesSequentially accumulates across all entries
+ */
+ public void testReduceEntriesSequentially() {
+ ConcurrentHashMap<Long, Long> m = longMap();
+ Map.Entry<Long,Long> r;
+ r = m.reduceEntries(Long.MAX_VALUE, new AddKeys());
+ assertEquals(r.getKey().longValue(), (long)SIZE * (SIZE - 1) / 2);
+ }
+
+ /**
+ * reduceKeysInParallel accumulates across all keys
+ */
+ public void testReduceKeysInParallel() {
+ ConcurrentHashMap<Long, Long> m = longMap();
+ Long r;
+ r = m.reduceKeys(1L, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()));
+ assertEquals((long)r, (long)SIZE * (SIZE - 1) / 2);
+ }
+
+ /**
+ * reduceValuesInParallel accumulates across all values
+ */
+ public void testReduceValuesInParallel() {
+ ConcurrentHashMap<Long, Long> m = longMap();
+ Long r;
+ r = m.reduceValues(1L, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()));
+ assertEquals((long)r, (long)SIZE * (SIZE - 1));
+ }
+
+ /**
+ * reduceEntriesInParallel accumulate across all entries
+ */
+ public void testReduceEntriesInParallel() {
+ ConcurrentHashMap<Long, Long> m = longMap();
+ Map.Entry<Long,Long> r;
+ r = m.reduceEntries(1L, new AddKeys());
+ assertEquals(r.getKey().longValue(), (long)SIZE * (SIZE - 1) / 2);
+ }
+
+ /**
+ * Mapped reduceKeysSequentially accumulates mapped keys
+ */
+ public void testMapReduceKeysSequentially() {
+ ConcurrentHashMap<Long, Long> m = longMap();
+ Long r = m.reduceKeys(Long.MAX_VALUE, (Long x) -> Long.valueOf(4 * x.longValue()),
+ (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()));
+ assertEquals((long)r, (long)4 * SIZE * (SIZE - 1) / 2);
+ }
+
+ /**
+ * Mapped reduceValuesSequentially accumulates mapped values
+ */
+ public void testMapReduceValuesSequentially() {
+ ConcurrentHashMap<Long, Long> m = longMap();
+ Long r = m.reduceValues(Long.MAX_VALUE, (Long x) -> Long.valueOf(4 * x.longValue()),
+ (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()));
+ assertEquals((long)r, (long)4 * SIZE * (SIZE - 1));
+ }
+
+ /**
+ * reduceSequentially accumulates across all transformed mappings
+ */
+ public void testMappedReduceSequentially() {
+ ConcurrentHashMap<Long, Long> m = longMap();
+ Long r = m.reduce(Long.MAX_VALUE, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()),
+ (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()));
+
+ assertEquals((long)r, (long)3 * SIZE * (SIZE - 1) / 2);
+ }
+
+ /**
+ * Mapped reduceKeysInParallel, accumulates mapped keys
+ */
+ public void testMapReduceKeysInParallel() {
+ ConcurrentHashMap<Long, Long> m = longMap();
+ Long r = m.reduceKeys(1L, (Long x) -> Long.valueOf(4 * x.longValue()),
+ (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()));
+ assertEquals((long)r, (long)4 * SIZE * (SIZE - 1) / 2);
+ }
+
+ /**
+ * Mapped reduceValuesInParallel accumulates mapped values
+ */
+ public void testMapReduceValuesInParallel() {
+ ConcurrentHashMap<Long, Long> m = longMap();
+ Long r = m.reduceValues(1L, (Long x) -> Long.valueOf(4 * x.longValue()),
+ (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()));
+ assertEquals((long)r, (long)4 * SIZE * (SIZE - 1));
+ }
+
+ /**
+ * reduceInParallel accumulate across all transformed mappings
+ */
+ public void testMappedReduceInParallel() {
+ ConcurrentHashMap<Long, Long> m = longMap();
+ Long r;
+ r = m.reduce(1L, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()),
+ (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()));
+ assertEquals((long)r, (long)3 * SIZE * (SIZE - 1) / 2);
+ }
+
+ /**
+ * reduceKeysToLongSequentially accumulates mapped keys
+ */
+ public void testReduceKeysToLongSequentially() {
+ ConcurrentHashMap<Long, Long> m = longMap();
+ long lr = m.reduceKeysToLong(Long.MAX_VALUE, (Long x) -> x.longValue(), 0L, Long::sum);
+ assertEquals(lr, (long)SIZE * (SIZE - 1) / 2);
+ }
+
+ /**
+ * reduceKeysToIntSequentially accumulates mapped keys
+ */
+ public void testReduceKeysToIntSequentially() {
+ ConcurrentHashMap<Long, Long> m = longMap();
+ int ir = m.reduceKeysToInt(Long.MAX_VALUE, (Long x) -> x.intValue(), 0, Integer::sum);
+ assertEquals(ir, SIZE * (SIZE - 1) / 2);
+ }
+
+ /**
+ * reduceKeysToDoubleSequentially accumulates mapped keys
+ */
+ public void testReduceKeysToDoubleSequentially() {
+ ConcurrentHashMap<Long, Long> m = longMap();
+ double dr = m.reduceKeysToDouble(Long.MAX_VALUE, (Long x) -> x.doubleValue(), 0.0, Double::sum);
+ assertEquals(dr, (double)SIZE * (SIZE - 1) / 2);
+ }
+
+ /**
+ * reduceValuesToLongSequentially accumulates mapped values
+ */
+ public void testReduceValuesToLongSequentially() {
+ ConcurrentHashMap<Long, Long> m = longMap();
+ long lr = m.reduceValuesToLong(Long.MAX_VALUE, (Long x) -> x.longValue(), 0L, Long::sum);
+ assertEquals(lr, (long)SIZE * (SIZE - 1));
+ }
+
+ /**
+ * reduceValuesToIntSequentially accumulates mapped values
+ */
+ public void testReduceValuesToIntSequentially() {
+ ConcurrentHashMap<Long, Long> m = longMap();
+ int ir = m.reduceValuesToInt(Long.MAX_VALUE, (Long x) -> x.intValue(), 0, Integer::sum);
+ assertEquals(ir, SIZE * (SIZE - 1));
+ }
+
+ /**
+ * reduceValuesToDoubleSequentially accumulates mapped values
+ */
+ public void testReduceValuesToDoubleSequentially() {
+ ConcurrentHashMap<Long, Long> m = longMap();
+ double dr = m.reduceValuesToDouble(Long.MAX_VALUE, (Long x) -> x.doubleValue(), 0.0, Double::sum);
+ assertEquals(dr, (double)SIZE * (SIZE - 1));
+ }
+
+ /**
+ * reduceKeysToLongInParallel accumulates mapped keys
+ */
+ public void testReduceKeysToLongInParallel() {
+ ConcurrentHashMap<Long, Long> m = longMap();
+ long lr = m.reduceKeysToLong(1L, (Long x) -> x.longValue(), 0L, Long::sum);
+ assertEquals(lr, (long)SIZE * (SIZE - 1) / 2);
+ }
+
+ /**
+ * reduceKeysToIntInParallel accumulates mapped keys
+ */
+ public void testReduceKeysToIntInParallel() {
+ ConcurrentHashMap<Long, Long> m = longMap();
+ int ir = m.reduceKeysToInt(1L, (Long x) -> x.intValue(), 0, Integer::sum);
+ assertEquals(ir, SIZE * (SIZE - 1) / 2);
+ }
+
+ /**
+ * reduceKeysToDoubleInParallel accumulates mapped values
+ */
+ public void testReduceKeysToDoubleInParallel() {
+ ConcurrentHashMap<Long, Long> m = longMap();
+ double dr = m.reduceKeysToDouble(1L, (Long x) -> x.doubleValue(), 0.0, Double::sum);
+ assertEquals(dr, (double)SIZE * (SIZE - 1) / 2);
+ }
+
+ /**
+ * reduceValuesToLongInParallel accumulates mapped values
+ */
+ public void testReduceValuesToLongInParallel() {
+ ConcurrentHashMap<Long, Long> m = longMap();
+ long lr = m.reduceValuesToLong(1L, (Long x) -> x.longValue(), 0L, Long::sum);
+ assertEquals(lr, (long)SIZE * (SIZE - 1));
+ }
+
+ /**
+ * reduceValuesToIntInParallel accumulates mapped values
+ */
+ public void testReduceValuesToIntInParallel() {
+ ConcurrentHashMap<Long, Long> m = longMap();
+ int ir = m.reduceValuesToInt(1L, (Long x) -> x.intValue(), 0, Integer::sum);
+ assertEquals(ir, SIZE * (SIZE - 1));
+ }
+
+ /**
+ * reduceValuesToDoubleInParallel accumulates mapped values
+ */
+ public void testReduceValuesToDoubleInParallel() {
+ ConcurrentHashMap<Long, Long> m = longMap();
+ double dr = m.reduceValuesToDouble(1L, (Long x) -> x.doubleValue(), 0.0, Double::sum);
+ assertEquals(dr, (double)SIZE * (SIZE - 1));
+ }
+
+ /**
+ * searchKeysSequentially returns a non-null result of search
+ * function, or null if none
+ */
+ public void testSearchKeysSequentially() {
+ ConcurrentHashMap<Long, Long> m = longMap();
+ Long r;
+ r = m.searchKeys(Long.MAX_VALUE, (Long x) -> x.longValue() == (long)(SIZE/2) ? x : null);
+ assertEquals((long)r, (long)(SIZE/2));
+ r = m.searchKeys(Long.MAX_VALUE, (Long x) -> x.longValue() < 0L ? x : null);
+ assertNull(r);
+ }
+
+ /**
+ * searchValuesSequentially returns a non-null result of search
+ * function, or null if none
+ */
+ public void testSearchValuesSequentially() {
+ ConcurrentHashMap<Long, Long> m = longMap();
+ Long r;
+ r = m.searchValues(Long.MAX_VALUE,
+ (Long x) -> (x.longValue() == (long)(SIZE/2)) ? x : null);
+ assertEquals((long)r, (long)(SIZE/2));
+ r = m.searchValues(Long.MAX_VALUE,
+ (Long x) -> (x.longValue() < 0L) ? x : null);
+ assertNull(r);
+ }
+
+ /**
+ * searchSequentially returns a non-null result of search
+ * function, or null if none
+ */
+ public void testSearchSequentially() {
+ ConcurrentHashMap<Long, Long> m = longMap();
+ Long r;
+ r = m.search(Long.MAX_VALUE, (Long x, Long y) -> x.longValue() == (long)(SIZE/2) ? x : null);
+ assertEquals((long)r, (long)(SIZE/2));
+ r = m.search(Long.MAX_VALUE, (Long x, Long y) -> x.longValue() < 0L ? x : null);
+ assertNull(r);
+ }
+
+ /**
+ * searchEntriesSequentially returns a non-null result of search
+ * function, or null if none
+ */
+ public void testSearchEntriesSequentially() {
+ ConcurrentHashMap<Long, Long> m = longMap();
+ Long r;
+ r = m.searchEntries(Long.MAX_VALUE, (Map.Entry<Long,Long> e) -> e.getKey().longValue() == (long)(SIZE/2) ? e.getKey() : null);
+ assertEquals((long)r, (long)(SIZE/2));
+ r = m.searchEntries(Long.MAX_VALUE, (Map.Entry<Long,Long> e) -> e.getKey().longValue() < 0L ? e.getKey() : null);
+ assertNull(r);
+ }
+
+ /**
+ * searchKeysInParallel returns a non-null result of search
+ * function, or null if none
+ */
+ public void testSearchKeysInParallel() {
+ ConcurrentHashMap<Long, Long> m = longMap();
+ Long r;
+ r = m.searchKeys(1L, (Long x) -> x.longValue() == (long)(SIZE/2) ? x : null);
+ assertEquals((long)r, (long)(SIZE/2));
+ r = m.searchKeys(1L, (Long x) -> x.longValue() < 0L ? x : null);
+ assertNull(r);
+ }
+
+ /**
+ * searchValuesInParallel returns a non-null result of search
+ * function, or null if none
+ */
+ public void testSearchValuesInParallel() {
+ ConcurrentHashMap<Long, Long> m = longMap();
+ Long r;
+ r = m.searchValues(1L, (Long x) -> x.longValue() == (long)(SIZE/2) ? x : null);
+ assertEquals((long)r, (long)(SIZE/2));
+ r = m.searchValues(1L, (Long x) -> x.longValue() < 0L ? x : null);
+ assertNull(r);
+ }
+
+ /**
+ * searchInParallel returns a non-null result of search function,
+ * or null if none
+ */
+ public void testSearchInParallel() {
+ ConcurrentHashMap<Long, Long> m = longMap();
+ Long r;
+ r = m.search(1L, (Long x, Long y) -> x.longValue() == (long)(SIZE/2) ? x : null);
+ assertEquals((long)r, (long)(SIZE/2));
+ r = m.search(1L, (Long x, Long y) -> x.longValue() < 0L ? x : null);
+ assertNull(r);
+ }
+
+ /**
+ * searchEntriesInParallel returns a non-null result of search
+ * function, or null if none
+ */
+ public void testSearchEntriesInParallel() {
+ ConcurrentHashMap<Long, Long> m = longMap();
+ Long r;
+ r = m.searchEntries(1L, (Map.Entry<Long,Long> e) -> e.getKey().longValue() == (long)(SIZE/2) ? e.getKey() : null);
+ assertEquals((long)r, (long)(SIZE/2));
+ r = m.searchEntries(1L, (Map.Entry<Long,Long> e) -> e.getKey().longValue() < 0L ? e.getKey() : null);
+ assertNull(r);
+ }
+
+}
diff --git a/jsr166-tests/src/test/java/jsr166/ConcurrentHashMapTest.java b/jsr166-tests/src/test/java/jsr166/ConcurrentHashMapTest.java
index 4650f41..f427127 100644
--- a/jsr166-tests/src/test/java/jsr166/ConcurrentHashMapTest.java
+++ b/jsr166-tests/src/test/java/jsr166/ConcurrentHashMapTest.java
@@ -30,7 +30,7 @@
// main(suite(), args);
// }
// public static Test suite() {
- // return new TestSuite(...);
+ // return new TestSuite(ConcurrentHashMapTest.class);
// }
/**
@@ -50,7 +50,9 @@
}
/** Re-implement Integer.compare for old java versions */
- static int compare(int x, int y) { return x < y ? -1 : x > y ? 1 : 0; }
+ static int compare(int x, int y) {
+ return (x < y) ? -1 : (x > y) ? 1 : 0;
+ }
// classes for testing Comparable fallbacks
static class BI implements Comparable<BI> {
@@ -538,7 +540,7 @@
/**
* Constructor (initialCapacity, loadFactor) throws
* IllegalArgumentException if either argument is negative
- */
+ */
public void testConstructor2() {
try {
new ConcurrentHashMap(-1, .75f);
diff --git a/jsr166-tests/src/test/java/jsr166/ConcurrentLinkedDequeTest.java b/jsr166-tests/src/test/java/jsr166/ConcurrentLinkedDequeTest.java
index c445957..6625e7e 100644
--- a/jsr166-tests/src/test/java/jsr166/ConcurrentLinkedDequeTest.java
+++ b/jsr166-tests/src/test/java/jsr166/ConcurrentLinkedDequeTest.java
@@ -29,7 +29,7 @@
// main(suite(), args);
// }
// public static Test suite() {
- // return new TestSuite(...);
+ // return new TestSuite(ConcurrentLinkedDequeTest.class);
// }
/**
@@ -69,8 +69,7 @@
*/
public void testConstructor4() {
try {
- Integer[] ints = new Integer[SIZE];
- new ConcurrentLinkedDeque(Arrays.asList(ints));
+ new ConcurrentLinkedDeque(Arrays.asList(new Integer[SIZE]));
shouldThrow();
} catch (NullPointerException success) {}
}
@@ -79,10 +78,10 @@
* Initializing from Collection with some null elements throws NPE
*/
public void testConstructor5() {
+ Integer[] ints = new Integer[SIZE];
+ for (int i = 0; i < SIZE - 1; ++i)
+ ints[i] = new Integer(i);
try {
- Integer[] ints = new Integer[SIZE];
- for (int i = 0; i < SIZE-1; ++i)
- ints[i] = new Integer(i);
new ConcurrentLinkedDeque(Arrays.asList(ints));
shouldThrow();
} catch (NullPointerException success) {}
@@ -120,7 +119,7 @@
public void testSize() {
ConcurrentLinkedDeque q = populatedDeque(SIZE);
for (int i = 0; i < SIZE; ++i) {
- assertEquals(SIZE-i, q.size());
+ assertEquals(SIZE - i, q.size());
q.remove();
}
for (int i = 0; i < SIZE; ++i) {
@@ -133,8 +132,8 @@
* push(null) throws NPE
*/
public void testPushNull() {
+ ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
try {
- ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
q.push(null);
shouldThrow();
} catch (NullPointerException success) {}
@@ -168,8 +167,8 @@
* offer(null) throws NPE
*/
public void testOfferNull() {
+ ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
try {
- ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
q.offer(null);
shouldThrow();
} catch (NullPointerException success) {}
@@ -179,8 +178,8 @@
* offerFirst(null) throws NPE
*/
public void testOfferFirstNull() {
+ ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
try {
- ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
q.offerFirst(null);
shouldThrow();
} catch (NullPointerException success) {}
@@ -190,8 +189,8 @@
* offerLast(null) throws NPE
*/
public void testOfferLastNull() {
+ ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
try {
- ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
q.offerLast(null);
shouldThrow();
} catch (NullPointerException success) {}
@@ -234,8 +233,8 @@
* add(null) throws NPE
*/
public void testAddNull() {
+ ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
try {
- ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
q.add(null);
shouldThrow();
} catch (NullPointerException success) {}
@@ -245,8 +244,8 @@
* addFirst(null) throws NPE
*/
public void testAddFirstNull() {
+ ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
try {
- ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
q.addFirst(null);
shouldThrow();
} catch (NullPointerException success) {}
@@ -256,8 +255,8 @@
* addLast(null) throws NPE
*/
public void testAddLastNull() {
+ ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
try {
- ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
q.addLast(null);
shouldThrow();
} catch (NullPointerException success) {}
@@ -300,8 +299,8 @@
* addAll(null) throws NPE
*/
public void testAddAll1() {
+ ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
try {
- ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
q.addAll(null);
shouldThrow();
} catch (NullPointerException success) {}
@@ -311,8 +310,8 @@
* addAll(this) throws IAE
*/
public void testAddAllSelf() {
+ ConcurrentLinkedDeque q = populatedDeque(SIZE);
try {
- ConcurrentLinkedDeque q = populatedDeque(SIZE);
q.addAll(q);
shouldThrow();
} catch (IllegalArgumentException success) {}
@@ -322,10 +321,9 @@
* addAll of a collection with null elements throws NPE
*/
public void testAddAll2() {
+ ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
try {
- ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
- Integer[] ints = new Integer[SIZE];
- q.addAll(Arrays.asList(ints));
+ q.addAll(Arrays.asList(new Integer[SIZE]));
shouldThrow();
} catch (NullPointerException success) {}
}
@@ -335,11 +333,11 @@
* possibly adding some elements
*/
public void testAddAll3() {
+ ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
+ Integer[] ints = new Integer[SIZE];
+ for (int i = 0; i < SIZE - 1; ++i)
+ ints[i] = new Integer(i);
try {
- ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
- Integer[] ints = new Integer[SIZE];
- for (int i = 0; i < SIZE-1; ++i)
- ints[i] = new Integer(i);
q.addAll(Arrays.asList(ints));
shouldThrow();
} catch (NullPointerException success) {}
@@ -376,7 +374,7 @@
*/
public void testPollLast() {
ConcurrentLinkedDeque q = populatedDeque(SIZE);
- for (int i = SIZE-1; i >= 0; --i) {
+ for (int i = SIZE - 1; i >= 0; --i) {
assertEquals(i, q.pollLast());
}
assertNull(q.pollLast());
@@ -445,14 +443,14 @@
assertTrue(q.contains(i));
assertTrue(q.remove(i));
assertFalse(q.contains(i));
- assertTrue(q.contains(i-1));
+ assertTrue(q.contains(i - 1));
}
for (int i = 0; i < SIZE; i += 2) {
assertTrue(q.contains(i));
assertTrue(q.remove(i));
assertFalse(q.contains(i));
- assertFalse(q.remove(i+1));
- assertFalse(q.contains(i+1));
+ assertFalse(q.remove(i + 1));
+ assertFalse(q.contains(i + 1));
}
assertTrue(q.isEmpty());
}
@@ -476,7 +474,7 @@
*/
public void testPeekLast() {
ConcurrentLinkedDeque q = populatedDeque(SIZE);
- for (int i = SIZE-1; i >= 0; --i) {
+ for (int i = SIZE - 1; i >= 0; --i) {
assertEquals(i, q.peekLast());
assertEquals(i, q.pollLast());
assertTrue(q.peekLast() == null ||
@@ -505,7 +503,7 @@
*/
public void testLastElement() {
ConcurrentLinkedDeque q = populatedDeque(SIZE);
- for (int i = SIZE-1; i >= 0; --i) {
+ for (int i = SIZE - 1; i >= 0; --i) {
assertEquals(i, q.getLast());
assertEquals(i, q.pollLast());
}
@@ -556,7 +554,7 @@
}
for (int i = 0; i < SIZE; i += 2) {
assertTrue(q.removeFirstOccurrence(new Integer(i)));
- assertFalse(q.removeFirstOccurrence(new Integer(i+1)));
+ assertFalse(q.removeFirstOccurrence(new Integer(i + 1)));
}
assertTrue(q.isEmpty());
}
@@ -571,7 +569,7 @@
}
for (int i = 0; i < SIZE; i += 2) {
assertTrue(q.removeLastOccurrence(new Integer(i)));
- assertFalse(q.removeLastOccurrence(new Integer(i+1)));
+ assertFalse(q.removeLastOccurrence(new Integer(i + 1)));
}
assertTrue(q.isEmpty());
}
@@ -630,7 +628,7 @@
assertTrue(changed);
assertTrue(q.containsAll(p));
- assertEquals(SIZE-i, q.size());
+ assertEquals(SIZE - i, q.size());
p.remove();
}
}
@@ -643,7 +641,7 @@
ConcurrentLinkedDeque q = populatedDeque(SIZE);
ConcurrentLinkedDeque p = populatedDeque(i);
assertTrue(q.removeAll(p));
- assertEquals(SIZE-i, q.size());
+ assertEquals(SIZE - i, q.size());
for (int j = 0; j < i; ++j) {
Integer x = (Integer)(p.remove());
assertFalse(q.contains(x));
@@ -759,18 +757,18 @@
final Random rng = new Random();
for (int iters = 0; iters < 100; ++iters) {
int max = rng.nextInt(5) + 2;
- int split = rng.nextInt(max-1) + 1;
+ int split = rng.nextInt(max - 1) + 1;
for (int j = 1; j <= max; ++j)
q.add(new Integer(j));
Iterator it = q.iterator();
for (int j = 1; j <= split; ++j)
assertEquals(it.next(), new Integer(j));
it.remove();
- assertEquals(it.next(), new Integer(split+1));
+ assertEquals(it.next(), new Integer(split + 1));
for (int j = 1; j <= split; ++j)
q.remove(new Integer(j));
it = q.iterator();
- for (int j = split+1; j <= max; ++j) {
+ for (int j = split + 1; j <= max; ++j) {
assertEquals(it.next(), new Integer(j));
it.remove();
}
@@ -827,18 +825,18 @@
final Random rng = new Random();
for (int iters = 0; iters < 100; ++iters) {
int max = rng.nextInt(5) + 2;
- int split = rng.nextInt(max-1) + 1;
+ int split = rng.nextInt(max - 1) + 1;
for (int j = max; j >= 1; --j)
q.add(new Integer(j));
Iterator it = q.descendingIterator();
for (int j = 1; j <= split; ++j)
assertEquals(it.next(), new Integer(j));
it.remove();
- assertEquals(it.next(), new Integer(split+1));
+ assertEquals(it.next(), new Integer(split + 1));
for (int j = 1; j <= split; ++j)
q.remove(new Integer(j));
it = q.descendingIterator();
- for (int j = split+1; j <= max; ++j) {
+ for (int j = split + 1; j <= max; ++j) {
assertEquals(it.next(), new Integer(j));
it.remove();
}
diff --git a/jsr166-tests/src/test/java/jsr166/ConcurrentLinkedQueueTest.java b/jsr166-tests/src/test/java/jsr166/ConcurrentLinkedQueueTest.java
index d3f5b1f..70519a4 100644
--- a/jsr166-tests/src/test/java/jsr166/ConcurrentLinkedQueueTest.java
+++ b/jsr166-tests/src/test/java/jsr166/ConcurrentLinkedQueueTest.java
@@ -27,7 +27,7 @@
// main(suite(), args);
// }
// public static Test suite() {
- // return new TestSuite(...);
+ // return new TestSuite(ConcurrentLinkedQueueTest.class);
// }
/**
@@ -66,8 +66,7 @@
*/
public void testConstructor4() {
try {
- Integer[] ints = new Integer[SIZE];
- new ConcurrentLinkedQueue(Arrays.asList(ints));
+ new ConcurrentLinkedQueue(Arrays.asList(new Integer[SIZE]));
shouldThrow();
} catch (NullPointerException success) {}
}
@@ -76,10 +75,10 @@
* Initializing from Collection with some null elements throws NPE
*/
public void testConstructor5() {
+ Integer[] ints = new Integer[SIZE];
+ for (int i = 0; i < SIZE - 1; ++i)
+ ints[i] = new Integer(i);
try {
- Integer[] ints = new Integer[SIZE];
- for (int i = 0; i < SIZE-1; ++i)
- ints[i] = new Integer(i);
new ConcurrentLinkedQueue(Arrays.asList(ints));
shouldThrow();
} catch (NullPointerException success) {}
@@ -117,7 +116,7 @@
public void testSize() {
ConcurrentLinkedQueue q = populatedQueue(SIZE);
for (int i = 0; i < SIZE; ++i) {
- assertEquals(SIZE-i, q.size());
+ assertEquals(SIZE - i, q.size());
q.remove();
}
for (int i = 0; i < SIZE; ++i) {
@@ -130,8 +129,8 @@
* offer(null) throws NPE
*/
public void testOfferNull() {
+ ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
try {
- ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
q.offer(null);
shouldThrow();
} catch (NullPointerException success) {}
@@ -141,8 +140,8 @@
* add(null) throws NPE
*/
public void testAddNull() {
+ ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
try {
- ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
q.add(null);
shouldThrow();
} catch (NullPointerException success) {}
@@ -172,8 +171,8 @@
* addAll(null) throws NPE
*/
public void testAddAll1() {
+ ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
try {
- ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
q.addAll(null);
shouldThrow();
} catch (NullPointerException success) {}
@@ -183,8 +182,8 @@
* addAll(this) throws IAE
*/
public void testAddAllSelf() {
+ ConcurrentLinkedQueue q = populatedQueue(SIZE);
try {
- ConcurrentLinkedQueue q = populatedQueue(SIZE);
q.addAll(q);
shouldThrow();
} catch (IllegalArgumentException success) {}
@@ -194,10 +193,9 @@
* addAll of a collection with null elements throws NPE
*/
public void testAddAll2() {
+ ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
try {
- ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
- Integer[] ints = new Integer[SIZE];
- q.addAll(Arrays.asList(ints));
+ q.addAll(Arrays.asList(new Integer[SIZE]));
shouldThrow();
} catch (NullPointerException success) {}
}
@@ -207,11 +205,11 @@
* possibly adding some elements
*/
public void testAddAll3() {
+ ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
+ Integer[] ints = new Integer[SIZE];
+ for (int i = 0; i < SIZE - 1; ++i)
+ ints[i] = new Integer(i);
try {
- ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
- Integer[] ints = new Integer[SIZE];
- for (int i = 0; i < SIZE-1; ++i)
- ints[i] = new Integer(i);
q.addAll(Arrays.asList(ints));
shouldThrow();
} catch (NullPointerException success) {}
@@ -295,14 +293,14 @@
assertTrue(q.contains(i));
assertTrue(q.remove(i));
assertFalse(q.contains(i));
- assertTrue(q.contains(i-1));
+ assertTrue(q.contains(i - 1));
}
for (int i = 0; i < SIZE; i += 2) {
assertTrue(q.contains(i));
assertTrue(q.remove(i));
assertFalse(q.contains(i));
- assertFalse(q.remove(i+1));
- assertFalse(q.contains(i+1));
+ assertFalse(q.remove(i + 1));
+ assertFalse(q.contains(i + 1));
}
assertTrue(q.isEmpty());
}
@@ -361,7 +359,7 @@
assertTrue(changed);
assertTrue(q.containsAll(p));
- assertEquals(SIZE-i, q.size());
+ assertEquals(SIZE - i, q.size());
p.remove();
}
}
@@ -374,7 +372,7 @@
ConcurrentLinkedQueue q = populatedQueue(SIZE);
ConcurrentLinkedQueue p = populatedQueue(i);
assertTrue(q.removeAll(p));
- assertEquals(SIZE-i, q.size());
+ assertEquals(SIZE - i, q.size());
for (int j = 0; j < i; ++j) {
Integer x = (Integer)(p.remove());
assertFalse(q.contains(x));
diff --git a/jsr166-tests/src/test/java/jsr166/ConcurrentSkipListMapTest.java b/jsr166-tests/src/test/java/jsr166/ConcurrentSkipListMapTest.java
index 0aadd23..f53a446 100644
--- a/jsr166-tests/src/test/java/jsr166/ConcurrentSkipListMapTest.java
+++ b/jsr166-tests/src/test/java/jsr166/ConcurrentSkipListMapTest.java
@@ -30,7 +30,7 @@
// main(suite(), args);
// }
// public static Test suite() {
- // return new TestSuite(...);
+ // return new TestSuite(ConcurrentSkipListMapTest.class);
// }
/**
@@ -1278,7 +1278,7 @@
}
static boolean eq(Integer i, int j) {
- return i == null ? j == -1 : i == j;
+ return (i == null) ? j == -1 : i == j;
}
}
diff --git a/jsr166-tests/src/test/java/jsr166/ConcurrentSkipListSetTest.java b/jsr166-tests/src/test/java/jsr166/ConcurrentSkipListSetTest.java
index 41f8835..959d703 100644
--- a/jsr166-tests/src/test/java/jsr166/ConcurrentSkipListSetTest.java
+++ b/jsr166-tests/src/test/java/jsr166/ConcurrentSkipListSetTest.java
@@ -29,7 +29,7 @@
// main(suite(), args);
// }
// public static Test suite() {
- // return new TestSuite(...);
+ // return new TestSuite(ConcurrentSkipListSetTest.class);
// }
static class MyReverseComparator implements Comparator {
@@ -46,7 +46,7 @@
ConcurrentSkipListSet<Integer> q =
new ConcurrentSkipListSet<Integer>();
assertTrue(q.isEmpty());
- for (int i = n-1; i >= 0; i -= 2)
+ for (int i = n - 1; i >= 0; i -= 2)
assertTrue(q.add(new Integer(i)));
for (int i = (n & 1); i < n; i += 2)
assertTrue(q.add(new Integer(i)));
@@ -92,8 +92,7 @@
*/
public void testConstructor4() {
try {
- Integer[] ints = new Integer[SIZE];
- new ConcurrentSkipListSet(Arrays.asList(ints));
+ new ConcurrentSkipListSet(Arrays.asList(new Integer[SIZE]));
shouldThrow();
} catch (NullPointerException success) {}
}
@@ -102,10 +101,10 @@
* Initializing from Collection with some null elements throws NPE
*/
public void testConstructor5() {
+ Integer[] ints = new Integer[SIZE];
+ for (int i = 0; i < SIZE - 1; ++i)
+ ints[i] = new Integer(i);
try {
- Integer[] ints = new Integer[SIZE];
- for (int i = 0; i < SIZE-1; ++i)
- ints[i] = new Integer(i);
new ConcurrentSkipListSet(Arrays.asList(ints));
shouldThrow();
} catch (NullPointerException success) {}
@@ -134,7 +133,7 @@
for (int i = 0; i < SIZE; ++i)
ints[i] = new Integer(i);
q.addAll(Arrays.asList(ints));
- for (int i = SIZE-1; i >= 0; --i)
+ for (int i = SIZE - 1; i >= 0; --i)
assertEquals(ints[i], q.pollFirst());
}
@@ -158,7 +157,7 @@
public void testSize() {
ConcurrentSkipListSet q = populatedSet(SIZE);
for (int i = 0; i < SIZE; ++i) {
- assertEquals(SIZE-i, q.size());
+ assertEquals(SIZE - i, q.size());
q.pollFirst();
}
for (int i = 0; i < SIZE; ++i) {
@@ -238,7 +237,7 @@
public void testAddAll3() {
ConcurrentSkipListSet q = new ConcurrentSkipListSet();
Integer[] ints = new Integer[SIZE];
- for (int i = 0; i < SIZE-1; ++i)
+ for (int i = 0; i < SIZE - 1; ++i)
ints[i] = new Integer(i);
try {
q.addAll(Arrays.asList(ints));
@@ -253,7 +252,7 @@
Integer[] empty = new Integer[0];
Integer[] ints = new Integer[SIZE];
for (int i = 0; i < SIZE; ++i)
- ints[i] = new Integer(SIZE-1-i);
+ ints[i] = new Integer(SIZE - 1 - i);
ConcurrentSkipListSet q = new ConcurrentSkipListSet();
assertFalse(q.addAll(Arrays.asList(empty)));
assertTrue(q.addAll(Arrays.asList(ints)));
@@ -277,7 +276,7 @@
*/
public void testPollLast() {
ConcurrentSkipListSet q = populatedSet(SIZE);
- for (int i = SIZE-1; i >= 0; --i) {
+ for (int i = SIZE - 1; i >= 0; --i) {
assertEquals(i, q.pollLast());
}
assertNull(q.pollFirst());
@@ -292,14 +291,14 @@
assertTrue(q.contains(i));
assertTrue(q.remove(i));
assertFalse(q.contains(i));
- assertTrue(q.contains(i-1));
+ assertTrue(q.contains(i - 1));
}
for (int i = 0; i < SIZE; i += 2) {
assertTrue(q.contains(i));
assertTrue(q.remove(i));
assertFalse(q.contains(i));
- assertFalse(q.remove(i+1));
- assertFalse(q.contains(i+1));
+ assertFalse(q.remove(i + 1));
+ assertFalse(q.contains(i + 1));
}
assertTrue(q.isEmpty());
}
@@ -358,7 +357,7 @@
assertTrue(changed);
assertTrue(q.containsAll(p));
- assertEquals(SIZE-i, q.size());
+ assertEquals(SIZE - i, q.size());
p.pollFirst();
}
}
@@ -371,7 +370,7 @@
ConcurrentSkipListSet q = populatedSet(SIZE);
ConcurrentSkipListSet p = populatedSet(i);
assertTrue(q.removeAll(p));
- assertEquals(SIZE-i, q.size());
+ assertEquals(SIZE - i, q.size());
for (int j = 0; j < i; ++j) {
Integer x = (Integer)(p.pollFirst());
assertFalse(q.contains(x));
@@ -980,7 +979,7 @@
}
static boolean eq(Integer i, int j) {
- return i == null ? j == -1 : i == j;
+ return (i == null) ? j == -1 : i == j;
}
}
diff --git a/jsr166-tests/src/test/java/jsr166/ConcurrentSkipListSubMapTest.java b/jsr166-tests/src/test/java/jsr166/ConcurrentSkipListSubMapTest.java
index 5315bcb..a6510f5 100644
--- a/jsr166-tests/src/test/java/jsr166/ConcurrentSkipListSubMapTest.java
+++ b/jsr166-tests/src/test/java/jsr166/ConcurrentSkipListSubMapTest.java
@@ -28,7 +28,7 @@
// main(suite(), args);
// }
// public static Test suite() {
- // return new TestSuite(...);
+ // return new TestSuite(ConcurrentSkipListSubMapTest.class);
// }
/**
diff --git a/jsr166-tests/src/test/java/jsr166/ConcurrentSkipListSubSetTest.java b/jsr166-tests/src/test/java/jsr166/ConcurrentSkipListSubSetTest.java
index f1c4aae..220a092 100644
--- a/jsr166-tests/src/test/java/jsr166/ConcurrentSkipListSubSetTest.java
+++ b/jsr166-tests/src/test/java/jsr166/ConcurrentSkipListSubSetTest.java
@@ -24,7 +24,7 @@
// main(suite(), args);
// }
// public static Test suite() {
- // return new TestSuite(...);
+ // return new TestSuite(ConcurrentSkipListSubSetTest.class);
// }
static class MyReverseComparator implements Comparator {
@@ -42,7 +42,7 @@
new ConcurrentSkipListSet<Integer>();
assertTrue(q.isEmpty());
- for (int i = n-1; i >= 0; i -= 2)
+ for (int i = n - 1; i >= 0; i -= 2)
assertTrue(q.add(new Integer(i)));
for (int i = (n & 1); i < n; i += 2)
assertTrue(q.add(new Integer(i)));
@@ -127,7 +127,7 @@
public void testSize() {
NavigableSet q = populatedSet(SIZE);
for (int i = 0; i < SIZE; ++i) {
- assertEquals(SIZE-i, q.size());
+ assertEquals(SIZE - i, q.size());
q.pollFirst();
}
for (int i = 0; i < SIZE; ++i) {
@@ -206,8 +206,8 @@
public void testAddAll3() {
NavigableSet q = set0();
Integer[] ints = new Integer[SIZE];
- for (int i = 0; i < SIZE-1; ++i)
- ints[i] = new Integer(i+SIZE);
+ for (int i = 0; i < SIZE - 1; ++i)
+ ints[i] = new Integer(i + SIZE);
try {
q.addAll(Arrays.asList(ints));
shouldThrow();
@@ -221,7 +221,7 @@
Integer[] empty = new Integer[0];
Integer[] ints = new Integer[SIZE];
for (int i = 0; i < SIZE; ++i)
- ints[i] = new Integer(SIZE-1- i);
+ ints[i] = new Integer(SIZE - 1 - i);
NavigableSet q = set0();
assertFalse(q.addAll(Arrays.asList(empty)));
assertTrue(q.addAll(Arrays.asList(ints)));
@@ -249,14 +249,14 @@
assertTrue(q.contains(i));
assertTrue(q.remove(i));
assertFalse(q.contains(i));
- assertTrue(q.contains(i-1));
+ assertTrue(q.contains(i - 1));
}
for (int i = 0; i < SIZE; i += 2) {
assertTrue(q.contains(i));
assertTrue(q.remove(i));
assertFalse(q.contains(i));
- assertFalse(q.remove(i+1));
- assertFalse(q.contains(i+1));
+ assertFalse(q.remove(i + 1));
+ assertFalse(q.contains(i + 1));
}
assertTrue(q.isEmpty());
}
@@ -315,7 +315,7 @@
assertTrue(changed);
assertTrue(q.containsAll(p));
- assertEquals(SIZE-i, q.size());
+ assertEquals(SIZE - i, q.size());
p.pollFirst();
}
}
@@ -328,7 +328,7 @@
NavigableSet q = populatedSet(SIZE);
NavigableSet p = populatedSet(i);
assertTrue(q.removeAll(p));
- assertEquals(SIZE-i, q.size());
+ assertEquals(SIZE - i, q.size());
for (int j = 0; j < i; ++j) {
Integer x = (Integer)(p.pollFirst());
assertFalse(q.contains(x));
@@ -623,7 +623,7 @@
public void testDescendingSize() {
NavigableSet q = populatedSet(SIZE);
for (int i = 0; i < SIZE; ++i) {
- assertEquals(SIZE-i, q.size());
+ assertEquals(SIZE - i, q.size());
q.pollFirst();
}
for (int i = 0; i < SIZE; ++i) {
@@ -702,8 +702,8 @@
public void testDescendingAddAll3() {
NavigableSet q = dset0();
Integer[] ints = new Integer[SIZE];
- for (int i = 0; i < SIZE-1; ++i)
- ints[i] = new Integer(i+SIZE);
+ for (int i = 0; i < SIZE - 1; ++i)
+ ints[i] = new Integer(i + SIZE);
try {
q.addAll(Arrays.asList(ints));
shouldThrow();
@@ -717,7 +717,7 @@
Integer[] empty = new Integer[0];
Integer[] ints = new Integer[SIZE];
for (int i = 0; i < SIZE; ++i)
- ints[i] = new Integer(SIZE-1- i);
+ ints[i] = new Integer(SIZE - 1 - i);
NavigableSet q = dset0();
assertFalse(q.addAll(Arrays.asList(empty)));
assertTrue(q.addAll(Arrays.asList(ints)));
@@ -746,7 +746,7 @@
}
for (int i = 0; i < SIZE; i += 2 ) {
assertTrue(q.remove(new Integer(i)));
- assertFalse(q.remove(new Integer(i+1)));
+ assertFalse(q.remove(new Integer(i + 1)));
}
assertTrue(q.isEmpty());
}
@@ -805,7 +805,7 @@
assertTrue(changed);
assertTrue(q.containsAll(p));
- assertEquals(SIZE-i, q.size());
+ assertEquals(SIZE - i, q.size());
p.pollFirst();
}
}
@@ -818,7 +818,7 @@
NavigableSet q = populatedSet(SIZE);
NavigableSet p = populatedSet(i);
assertTrue(q.removeAll(p));
- assertEquals(SIZE-i, q.size());
+ assertEquals(SIZE - i, q.size());
for (int j = 0; j < i; ++j) {
Integer x = (Integer)(p.pollFirst());
assertFalse(q.contains(x));
diff --git a/jsr166-tests/src/test/java/jsr166/CopyOnWriteArrayListTest.java b/jsr166-tests/src/test/java/jsr166/CopyOnWriteArrayListTest.java
index 658268a..8a37d03 100644
--- a/jsr166-tests/src/test/java/jsr166/CopyOnWriteArrayListTest.java
+++ b/jsr166-tests/src/test/java/jsr166/CopyOnWriteArrayListTest.java
@@ -31,7 +31,7 @@
// main(suite(), args);
// }
// public static Test suite() {
- // return new TestSuite(...);
+ // return new TestSuite(CopyOnWriteArrayListTest.class);
// }
static CopyOnWriteArrayList<Integer> populatedArray(int n) {
@@ -67,7 +67,7 @@
*/
public void testConstructor2() {
Integer[] ints = new Integer[SIZE];
- for (int i = 0; i < SIZE-1; ++i)
+ for (int i = 0; i < SIZE - 1; ++i)
ints[i] = new Integer(i);
CopyOnWriteArrayList a = new CopyOnWriteArrayList(ints);
for (int i = 0; i < SIZE; ++i)
@@ -79,7 +79,7 @@
*/
public void testConstructor3() {
Integer[] ints = new Integer[SIZE];
- for (int i = 0; i < SIZE-1; ++i)
+ for (int i = 0; i < SIZE - 1; ++i)
ints[i] = new Integer(i);
CopyOnWriteArrayList a = new CopyOnWriteArrayList(Arrays.asList(ints));
for (int i = 0; i < SIZE; ++i)
@@ -181,18 +181,26 @@
CopyOnWriteArrayList b = populatedArray(3);
assertTrue(a.equals(b));
assertTrue(b.equals(a));
+ assertTrue(a.containsAll(b));
+ assertTrue(b.containsAll(a));
assertEquals(a.hashCode(), b.hashCode());
a.add(m1);
assertFalse(a.equals(b));
assertFalse(b.equals(a));
+ assertTrue(a.containsAll(b));
+ assertFalse(b.containsAll(a));
b.add(m1);
assertTrue(a.equals(b));
assertTrue(b.equals(a));
+ assertTrue(a.containsAll(b));
+ assertTrue(b.containsAll(a));
assertEquals(a.hashCode(), b.hashCode());
+
+ assertFalse(a.equals(null));
}
/**
- * containsAll returns true for collection with subset of elements
+ * containsAll returns true for collections with subset of elements
*/
public void testContainsAll() {
CopyOnWriteArrayList full = populatedArray(3);
@@ -201,6 +209,11 @@
assertTrue(full.containsAll(Arrays.asList(one, two)));
assertFalse(full.containsAll(Arrays.asList(one, two, six)));
assertFalse(full.containsAll(Arrays.asList(six)));
+
+ try {
+ full.containsAll(null);
+ shouldThrow();
+ } catch (NullPointerException success) {}
}
/**
@@ -342,7 +355,7 @@
ListIterator i = full.listIterator(1);
int j;
for (j = 0; i.hasNext(); j++)
- assertEquals(j+1, i.next());
+ assertEquals(j + 1, i.next());
assertEquals(2, j);
}
@@ -441,7 +454,7 @@
a = new Integer[0];
assertSame(a, empty.toArray(a));
- a = new Integer[SIZE/2];
+ a = new Integer[SIZE / 2];
Arrays.fill(a, 42);
assertSame(a, empty.toArray(a));
assertNull(a[0]);
@@ -465,7 +478,7 @@
assertSame(a, full.toArray(a));
assertTrue(Arrays.equals(elements, a));
- a = new Integer[2*SIZE];
+ a = new Integer[2 * SIZE];
Arrays.fill(a, 42);
assertSame(a, full.toArray(a));
assertTrue(Arrays.equals(elements, Arrays.copyOf(a, SIZE)));
diff --git a/jsr166-tests/src/test/java/jsr166/CopyOnWriteArraySetTest.java b/jsr166-tests/src/test/java/jsr166/CopyOnWriteArraySetTest.java
index 2810802..a486c6a 100644
--- a/jsr166-tests/src/test/java/jsr166/CopyOnWriteArraySetTest.java
+++ b/jsr166-tests/src/test/java/jsr166/CopyOnWriteArraySetTest.java
@@ -28,7 +28,7 @@
// main(suite(), args);
// }
// public static Test suite() {
- // return new TestSuite(...);
+ // return new TestSuite(CopyOnWriteArraySetTest.class);
// }
static CopyOnWriteArraySet<Integer> populatedSet(int n) {
@@ -64,7 +64,7 @@
*/
public void testConstructor3() {
Integer[] ints = new Integer[SIZE];
- for (int i = 0; i < SIZE-1; ++i)
+ for (int i = 0; i < SIZE - 1; ++i)
ints[i] = new Integer(i);
CopyOnWriteArraySet a = new CopyOnWriteArraySet(Arrays.asList(ints));
for (int i = 0; i < SIZE; ++i)
@@ -139,14 +139,46 @@
CopyOnWriteArraySet b = populatedSet(3);
assertTrue(a.equals(b));
assertTrue(b.equals(a));
+ assertTrue(a.containsAll(b));
+ assertTrue(b.containsAll(a));
assertEquals(a.hashCode(), b.hashCode());
+ assertEquals(a.size(), b.size());
+
a.add(m1);
assertFalse(a.equals(b));
assertFalse(b.equals(a));
+ assertTrue(a.containsAll(b));
+ assertFalse(b.containsAll(a));
b.add(m1);
assertTrue(a.equals(b));
assertTrue(b.equals(a));
+ assertTrue(a.containsAll(b));
+ assertTrue(b.containsAll(a));
assertEquals(a.hashCode(), b.hashCode());
+
+ Object x = a.iterator().next();
+ a.remove(x);
+ assertFalse(a.equals(b));
+ assertFalse(b.equals(a));
+ assertFalse(a.containsAll(b));
+ assertTrue(b.containsAll(a));
+ a.add(x);
+ assertTrue(a.equals(b));
+ assertTrue(b.equals(a));
+ assertTrue(a.containsAll(b));
+ assertTrue(b.containsAll(a));
+ assertEquals(a.hashCode(), b.hashCode());
+ assertEquals(a.size(), b.size());
+
+ CopyOnWriteArraySet empty1 = new CopyOnWriteArraySet(Arrays.asList());
+ CopyOnWriteArraySet empty2 = new CopyOnWriteArraySet(Arrays.asList());
+ assertTrue(empty1.equals(empty1));
+ assertTrue(empty1.equals(empty2));
+
+ assertFalse(empty1.equals(a));
+ assertFalse(a.equals(empty1));
+
+ assertFalse(a.equals(null));
}
/**
@@ -154,11 +186,24 @@
*/
public void testContainsAll() {
Collection full = populatedSet(3);
+ assertTrue(full.containsAll(full));
assertTrue(full.containsAll(Arrays.asList()));
assertTrue(full.containsAll(Arrays.asList(one)));
assertTrue(full.containsAll(Arrays.asList(one, two)));
assertFalse(full.containsAll(Arrays.asList(one, two, six)));
assertFalse(full.containsAll(Arrays.asList(six)));
+
+ CopyOnWriteArraySet empty1 = new CopyOnWriteArraySet(Arrays.asList());
+ CopyOnWriteArraySet empty2 = new CopyOnWriteArraySet(Arrays.asList());
+ assertTrue(empty1.containsAll(empty2));
+ assertTrue(empty1.containsAll(empty1));
+ assertFalse(empty1.containsAll(full));
+ assertTrue(full.containsAll(empty1));
+
+ try {
+ full.containsAll(null);
+ shouldThrow();
+ } catch (NullPointerException success) {}
}
/**
@@ -289,7 +334,7 @@
a = new Integer[0];
assertSame(a, empty.toArray(a));
- a = new Integer[SIZE/2];
+ a = new Integer[SIZE / 2];
Arrays.fill(a, 42);
assertSame(a, empty.toArray(a));
assertNull(a[0]);
@@ -313,7 +358,7 @@
assertSame(a, full.toArray(a));
assertTrue(Arrays.equals(elements, a));
- a = new Integer[2*SIZE];
+ a = new Integer[2 * SIZE];
Arrays.fill(a, 42);
assertSame(a, full.toArray(a));
assertTrue(Arrays.equals(elements, Arrays.copyOf(a, SIZE)));
@@ -327,10 +372,10 @@
* not store the objects inside the set
*/
public void testToArray_ArrayStoreException() {
+ CopyOnWriteArraySet c = new CopyOnWriteArraySet();
+ c.add("zfasdfsdf");
+ c.add("asdadasd");
try {
- CopyOnWriteArraySet c = new CopyOnWriteArraySet();
- c.add("zfasdfsdf");
- c.add("asdadasd");
c.toArray(new Long[5]);
shouldThrow();
} catch (ArrayStoreException success) {}
diff --git a/jsr166-tests/src/test/java/jsr166/CountDownLatchTest.java b/jsr166-tests/src/test/java/jsr166/CountDownLatchTest.java
index da1ebb4..e764c9e 100644
--- a/jsr166-tests/src/test/java/jsr166/CountDownLatchTest.java
+++ b/jsr166-tests/src/test/java/jsr166/CountDownLatchTest.java
@@ -23,7 +23,7 @@
// main(suite(), args);
// }
// public static Test suite() {
- // return new TestSuite(...);
+ // return new TestSuite(CountDownLatchTest.class);
// }
/**
diff --git a/jsr166-tests/src/test/java/jsr166/CountedCompleterTest.java b/jsr166-tests/src/test/java/jsr166/CountedCompleterTest.java
index 80d7b3b..8a38eff 100644
--- a/jsr166-tests/src/test/java/jsr166/CountedCompleterTest.java
+++ b/jsr166-tests/src/test/java/jsr166/CountedCompleterTest.java
@@ -31,7 +31,7 @@
// main(suite(), args);
// }
// public static Test suite() {
- // return new TestSuite(...);
+ // return new TestSuite(CountedCompleterTest.class);
// }
// Runs with "mainPool" use > 1 thread. singletonPool tests use 1
@@ -53,7 +53,7 @@
}
private void testInvokeOnPool(ForkJoinPool pool, ForkJoinTask a) {
- try {
+ try (PoolCleaner cleaner = cleaner(pool)) {
assertFalse(a.isDone());
assertFalse(a.isCompletedNormally());
assertFalse(a.isCompletedAbnormally());
@@ -69,8 +69,6 @@
assertFalse(a.isCancelled());
assertNull(a.getException());
assertNull(a.getRawResult());
- } finally {
- joinPool(pool);
}
}
@@ -99,17 +97,17 @@
{
Thread.currentThread().interrupt();
- long t0 = System.nanoTime();
+ long startTime = System.nanoTime();
assertNull(a.join());
- assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
+ assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
Thread.interrupted();
}
{
Thread.currentThread().interrupt();
- long t0 = System.nanoTime();
+ long startTime = System.nanoTime();
a.quietlyJoin(); // should be no-op
- assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
+ assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
Thread.interrupted();
}
@@ -142,9 +140,9 @@
Thread.interrupted();
{
- long t0 = System.nanoTime();
+ long startTime = System.nanoTime();
a.quietlyJoin(); // should be no-op
- assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
+ assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
}
try {
@@ -180,9 +178,9 @@
Thread.interrupted();
{
- long t0 = System.nanoTime();
+ long startTime = System.nanoTime();
a.quietlyJoin(); // should be no-op
- assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
+ assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
}
try {
@@ -284,6 +282,9 @@
final class NoopCC extends CheckedCC {
NoopCC() { super(); }
NoopCC(CountedCompleter p) { super(p); }
+ NoopCC(CountedCompleter p, int initialPendingCount) {
+ super(p, initialPendingCount);
+ }
protected void realCompute() {}
}
@@ -302,6 +303,7 @@
void testComplete(NoopCC cc, Object x, int pendingCount) {
cc.setPendingCount(pendingCount);
cc.checkCompletes(x);
+ assertEquals(pendingCount, cc.getPendingCount());
}
/**
@@ -315,14 +317,20 @@
}
/**
- * completeExceptionally(null) throws NullPointerException
+ * completeExceptionally(null) surprisingly has the same effect as
+ * completeExceptionally(new RuntimeException())
*/
public void testCompleteExceptionally_null() {
+ NoopCC a = new NoopCC();
+ a.completeExceptionally(null);
try {
- new NoopCC()
- .checkCompletesExceptionally(null);
+ a.invoke();
shouldThrow();
- } catch (NullPointerException success) {}
+ } catch (RuntimeException success) {
+ assertSame(success.getClass(), RuntimeException.class);
+ assertNull(success.getCause());
+ a.checkCompletedExceptionally(success);
+ }
}
/**
@@ -331,10 +339,15 @@
public void testSetPendingCount() {
NoopCC a = new NoopCC();
assertEquals(0, a.getPendingCount());
- a.setPendingCount(1);
- assertEquals(1, a.getPendingCount());
- a.setPendingCount(27);
- assertEquals(27, a.getPendingCount());
+ int[] vals = {
+ -1, 0, 1,
+ Integer.MIN_VALUE,
+ Integer.MAX_VALUE,
+ };
+ for (int val : vals) {
+ a.setPendingCount(val);
+ assertEquals(val, a.getPendingCount());
+ }
}
/**
@@ -347,21 +360,26 @@
assertEquals(1, a.getPendingCount());
a.addToPendingCount(27);
assertEquals(28, a.getPendingCount());
+ a.addToPendingCount(-28);
+ assertEquals(0, a.getPendingCount());
}
/**
* decrementPendingCountUnlessZero decrements reported pending
* count unless zero
*/
- public void testDecrementPendingCount() {
- NoopCC a = new NoopCC();
- assertEquals(0, a.getPendingCount());
- a.addToPendingCount(1);
+ public void testDecrementPendingCountUnlessZero() {
+ NoopCC a = new NoopCC(null, 2);
+ assertEquals(2, a.getPendingCount());
+ assertEquals(2, a.decrementPendingCountUnlessZero());
assertEquals(1, a.getPendingCount());
- a.decrementPendingCountUnlessZero();
+ assertEquals(1, a.decrementPendingCountUnlessZero());
assertEquals(0, a.getPendingCount());
- a.decrementPendingCountUnlessZero();
+ assertEquals(0, a.decrementPendingCountUnlessZero());
assertEquals(0, a.getPendingCount());
+ a.setPendingCount(-1);
+ assertEquals(-1, a.decrementPendingCountUnlessZero());
+ assertEquals(-2, a.getPendingCount());
}
/**
@@ -485,7 +503,7 @@
}
/**
- * quietlyCompleteRoot completes root task
+ * quietlyCompleteRoot completes root task and only root task
*/
public void testQuietlyCompleteRoot() {
NoopCC a = new NoopCC();
diff --git a/jsr166-tests/src/test/java/jsr166/CyclicBarrierTest.java b/jsr166-tests/src/test/java/jsr166/CyclicBarrierTest.java
index a9d8c54..37adcb1 100644
--- a/jsr166-tests/src/test/java/jsr166/CyclicBarrierTest.java
+++ b/jsr166-tests/src/test/java/jsr166/CyclicBarrierTest.java
@@ -27,7 +27,7 @@
// main(suite(), args);
// }
// public static Test suite() {
- // return new TestSuite(...);
+ // return new TestSuite(CyclicBarrierTest.class);
// }
private volatile int countAction;
diff --git a/jsr166-tests/src/test/java/jsr166/DelayQueueTest.java b/jsr166-tests/src/test/java/jsr166/DelayQueueTest.java
index 7619b48..e42ac2d 100644
--- a/jsr166-tests/src/test/java/jsr166/DelayQueueTest.java
+++ b/jsr166-tests/src/test/java/jsr166/DelayQueueTest.java
@@ -25,25 +25,26 @@
import junit.framework.Test;
-// android-changed: Extend BlockingQueueTest directly.
-public class DelayQueueTest extends BlockingQueueTest {
+public class DelayQueueTest extends JSR166TestCase {
// android-changed: Extend BlockingQueueTest directly instead of creating
// an inner class and its associated suite.
//
// public static class Generic extends BlockingQueueTest {
- // protected BlockingQueue emptyCollection() {
+ // protected BlockingQueue emptyCollection() {
// return new DelayQueue();
// }
// protected PDelay makeElement(int i) {
// return new PDelay(i);
// }
// }
+
+ // 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 newTestSuite(DelayQueueTest.class,
// new Generic().testSuite());
@@ -138,7 +139,7 @@
private DelayQueue<PDelay> populatedQueue(int n) {
DelayQueue<PDelay> q = new DelayQueue<PDelay>();
assertTrue(q.isEmpty());
- for (int i = n-1; i >= 0; i -= 2)
+ for (int i = n - 1; i >= 0; i -= 2)
assertTrue(q.offer(new PDelay(i)));
for (int i = (n & 1); i < n; i += 2)
assertTrue(q.offer(new PDelay(i)));
@@ -170,8 +171,7 @@
*/
public void testConstructor4() {
try {
- PDelay[] ints = new PDelay[SIZE];
- new DelayQueue(Arrays.asList(ints));
+ new DelayQueue(Arrays.asList(new PDelay[SIZE]));
shouldThrow();
} catch (NullPointerException success) {}
}
@@ -180,11 +180,11 @@
* Initializing from Collection with some null elements throws NPE
*/
public void testConstructor5() {
+ PDelay[] a = new PDelay[SIZE];
+ for (int i = 0; i < SIZE - 1; ++i)
+ a[i] = new PDelay(i);
try {
- PDelay[] ints = new PDelay[SIZE];
- for (int i = 0; i < SIZE-1; ++i)
- ints[i] = new PDelay(i);
- new DelayQueue(Arrays.asList(ints));
+ new DelayQueue(Arrays.asList(a));
shouldThrow();
} catch (NullPointerException success) {}
}
@@ -223,7 +223,7 @@
BlockingQueue q = populatedQueue(SIZE);
for (int i = 0; i < SIZE; ++i) {
assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
- assertEquals(SIZE-i, q.size());
+ assertEquals(SIZE - i, q.size());
assertTrue(q.remove() instanceof PDelay);
}
for (int i = 0; i < SIZE; ++i) {
@@ -257,8 +257,8 @@
* addAll(this) throws IAE
*/
public void testAddAllSelf() {
+ DelayQueue q = populatedQueue(SIZE);
try {
- DelayQueue q = populatedQueue(SIZE);
q.addAll(q);
shouldThrow();
} catch (IllegalArgumentException success) {}
@@ -269,12 +269,12 @@
* possibly adding some elements
*/
public void testAddAll3() {
+ DelayQueue q = new DelayQueue();
+ PDelay[] a = new PDelay[SIZE];
+ for (int i = 0; i < SIZE - 1; ++i)
+ a[i] = new PDelay(i);
try {
- DelayQueue q = new DelayQueue();
- PDelay[] ints = new PDelay[SIZE];
- for (int i = 0; i < SIZE-1; ++i)
- ints[i] = new PDelay(i);
- q.addAll(Arrays.asList(ints));
+ q.addAll(Arrays.asList(a));
shouldThrow();
} catch (NullPointerException success) {}
}
@@ -285,7 +285,7 @@
public void testAddAll5() {
PDelay[] empty = new PDelay[0];
PDelay[] ints = new PDelay[SIZE];
- for (int i = SIZE-1; i >= 0; --i)
+ for (int i = SIZE - 1; i >= 0; --i)
ints[i] = new PDelay(i);
DelayQueue q = new DelayQueue();
assertFalse(q.addAll(Arrays.asList(empty)));
@@ -427,11 +427,13 @@
*/
public void testInterruptedTimedPoll() throws InterruptedException {
final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
+ final DelayQueue q = populatedQueue(SIZE);
Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
- DelayQueue q = populatedQueue(SIZE);
+ long startTime = System.nanoTime();
for (int i = 0; i < SIZE; ++i) {
- assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, MILLISECONDS)));
+ assertEquals(new PDelay(i),
+ ((PDelay)q.poll(LONG_DELAY_MS, MILLISECONDS)));
}
Thread.currentThread().interrupt();
@@ -447,12 +449,14 @@
shouldThrow();
} catch (InterruptedException success) {}
assertFalse(Thread.interrupted());
+ assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
}});
await(pleaseInterrupt);
assertThreadStaysAlive(t);
t.interrupt();
awaitTermination(t);
+ checkEmpty(q);
}
/**
@@ -557,7 +561,7 @@
assertTrue(changed);
assertTrue(q.containsAll(p));
- assertEquals(SIZE-i, q.size());
+ assertEquals(SIZE - i, q.size());
p.remove();
}
}
@@ -570,7 +574,7 @@
DelayQueue q = populatedQueue(SIZE);
DelayQueue p = populatedQueue(i);
assertTrue(q.removeAll(p));
- assertEquals(SIZE-i, q.size());
+ assertEquals(SIZE - i, q.size());
for (int j = 0; j < i; ++j) {
PDelay x = (PDelay)(p.remove());
assertFalse(q.contains(x));
@@ -668,22 +672,22 @@
public void testPollInExecutor() {
final DelayQueue q = new DelayQueue();
final CheckedBarrier threadsStarted = new CheckedBarrier(2);
- ExecutorService executor = Executors.newFixedThreadPool(2);
- executor.execute(new CheckedRunnable() {
- public void realRun() throws InterruptedException {
- assertNull(q.poll());
- threadsStarted.await();
- assertNotNull(q.poll(LONG_DELAY_MS, MILLISECONDS));
- checkEmpty(q);
- }});
+ final ExecutorService executor = Executors.newFixedThreadPool(2);
+ try (PoolCleaner cleaner = cleaner(executor)) {
+ executor.execute(new CheckedRunnable() {
+ public void realRun() throws InterruptedException {
+ assertNull(q.poll());
+ threadsStarted.await();
+ assertNotNull(q.poll(LONG_DELAY_MS, MILLISECONDS));
+ checkEmpty(q);
+ }});
- executor.execute(new CheckedRunnable() {
- public void realRun() throws InterruptedException {
- threadsStarted.await();
- q.put(new PDelay(1));
- }});
-
- joinPool(executor);
+ executor.execute(new CheckedRunnable() {
+ public void realRun() throws InterruptedException {
+ threadsStarted.await();
+ q.put(new PDelay(1));
+ }});
+ }
}
/**
@@ -768,7 +772,7 @@
final DelayQueue q = populatedQueue(SIZE);
Thread t = new Thread(new CheckedRunnable() {
public void realRun() {
- q.put(new PDelay(SIZE+1));
+ q.put(new PDelay(SIZE + 1));
}});
t.start();
@@ -788,7 +792,7 @@
ArrayList l = new ArrayList();
q.drainTo(l, i);
int k = (i < SIZE) ? i : SIZE;
- assertEquals(SIZE-k, q.size());
+ assertEquals(SIZE - k, q.size());
assertEquals(k, l.size());
}
}
diff --git a/jsr166-tests/src/test/java/jsr166/DoubleAccumulatorTest.java b/jsr166-tests/src/test/java/jsr166/DoubleAccumulatorTest.java
new file mode 100644
index 0000000..e061f9a
--- /dev/null
+++ b/jsr166-tests/src/test/java/jsr166/DoubleAccumulatorTest.java
@@ -0,0 +1,161 @@
+/*
+ * Written by Doug Lea with assistance from members of JCP JSR-166
+ * Expert Group and released to the public domain, as explained at
+ * http://creativecommons.org/publicdomain/zero/1.0/
+ */
+
+package jsr166;
+
+import java.util.concurrent.Executors;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Phaser;
+import java.util.concurrent.atomic.DoubleAccumulator;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+public class DoubleAccumulatorTest 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(DoubleAccumulatorTest.class);
+ // }
+
+ /**
+ * default constructed initializes to zero
+ */
+ public void testConstructor() {
+ DoubleAccumulator ai = new DoubleAccumulator(Double::max, 0.0);
+ assertEquals(0.0, ai.get());
+ }
+
+ /**
+ * accumulate accumulates given value to current, and get returns current value
+ */
+ public void testAccumulateAndGet() {
+ DoubleAccumulator ai = new DoubleAccumulator(Double::max, 0.0);
+ ai.accumulate(2.0);
+ assertEquals(2.0, ai.get());
+ ai.accumulate(-4.0);
+ assertEquals(2.0, ai.get());
+ ai.accumulate(4.0);
+ assertEquals(4.0, ai.get());
+ }
+
+ /**
+ * reset() causes subsequent get() to return zero
+ */
+ public void testReset() {
+ DoubleAccumulator ai = new DoubleAccumulator(Double::max, 0.0);
+ ai.accumulate(2.0);
+ assertEquals(2.0, ai.get());
+ ai.reset();
+ assertEquals(0.0, ai.get());
+ }
+
+ /**
+ * getThenReset() returns current value; subsequent get() returns zero
+ */
+ public void testGetThenReset() {
+ DoubleAccumulator ai = new DoubleAccumulator(Double::max, 0.0);
+ ai.accumulate(2.0);
+ assertEquals(2.0, ai.get());
+ assertEquals(2.0, ai.getThenReset());
+ assertEquals(0.0, ai.get());
+ }
+
+ /**
+ * toString returns current value.
+ */
+ public void testToString() {
+ DoubleAccumulator ai = new DoubleAccumulator(Double::max, 0.0);
+ assertEquals("0.0", ai.toString());
+ ai.accumulate(1.0);
+ assertEquals(Double.toString(1.0), ai.toString());
+ }
+
+ /**
+ * intValue returns current value.
+ */
+ public void testIntValue() {
+ DoubleAccumulator ai = new DoubleAccumulator(Double::max, 0.0);
+ assertEquals(0, ai.intValue());
+ ai.accumulate(1.0);
+ assertEquals(1, ai.intValue());
+ }
+
+ /**
+ * longValue returns current value.
+ */
+ public void testLongValue() {
+ DoubleAccumulator ai = new DoubleAccumulator(Double::max, 0.0);
+ assertEquals(0, ai.longValue());
+ ai.accumulate(1.0);
+ assertEquals(1, ai.longValue());
+ }
+
+ /**
+ * floatValue returns current value.
+ */
+ public void testFloatValue() {
+ DoubleAccumulator ai = new DoubleAccumulator(Double::max, 0.0);
+ assertEquals(0.0f, ai.floatValue());
+ ai.accumulate(1.0);
+ assertEquals(1.0f, ai.floatValue());
+ }
+
+ /**
+ * doubleValue returns current value.
+ */
+ public void testDoubleValue() {
+ DoubleAccumulator ai = new DoubleAccumulator(Double::max, 0.0);
+ assertEquals(0.0, ai.doubleValue());
+ ai.accumulate(1.0);
+ assertEquals(1.0, ai.doubleValue());
+ }
+
+ /**
+ * accumulates by multiple threads produce correct result
+ */
+ public void testAccumulateAndGetMT() {
+ final int incs = 1000000;
+ final int nthreads = 4;
+ final ExecutorService pool = Executors.newCachedThreadPool();
+ DoubleAccumulator a = new DoubleAccumulator(Double::max, 0.0);
+ Phaser phaser = new Phaser(nthreads + 1);
+ for (int i = 0; i < nthreads; ++i)
+ pool.execute(new AccTask(a, phaser, incs));
+ phaser.arriveAndAwaitAdvance();
+ phaser.arriveAndAwaitAdvance();
+ double expected = incs - 1;
+ double result = a.get();
+ assertEquals(expected, result);
+ pool.shutdown();
+ }
+
+ static final class AccTask implements Runnable {
+ final DoubleAccumulator acc;
+ final Phaser phaser;
+ final int incs;
+ volatile double result;
+ AccTask(DoubleAccumulator acc, Phaser phaser, int incs) {
+ this.acc = acc;
+ this.phaser = phaser;
+ this.incs = incs;
+ }
+
+ public void run() {
+ phaser.arriveAndAwaitAdvance();
+ DoubleAccumulator a = acc;
+ for (int i = 0; i < incs; ++i)
+ a.accumulate(i);
+ result = a.get();
+ phaser.arrive();
+ }
+ }
+
+}
diff --git a/jsr166-tests/src/test/java/jsr166/DoubleAdderTest.java b/jsr166-tests/src/test/java/jsr166/DoubleAdderTest.java
new file mode 100644
index 0000000..d02e2a1
--- /dev/null
+++ b/jsr166-tests/src/test/java/jsr166/DoubleAdderTest.java
@@ -0,0 +1,175 @@
+/*
+ * Written by Doug Lea with assistance from members of JCP JSR-166
+ * Expert Group and released to the public domain, as explained at
+ * http://creativecommons.org/publicdomain/zero/1.0/
+ */
+
+package jsr166;
+
+import java.util.concurrent.CyclicBarrier;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.atomic.DoubleAdder;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+public class DoubleAdderTest 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(DoubleAdderTest.class);
+ // }
+
+ /**
+ * default constructed initializes to zero
+ */
+ public void testConstructor() {
+ DoubleAdder ai = new DoubleAdder();
+ assertEquals(0.0, ai.sum());
+ }
+
+ /**
+ * add adds given value to current, and sum returns current value
+ */
+ public void testAddAndSum() {
+ DoubleAdder ai = new DoubleAdder();
+ ai.add(2.0);
+ assertEquals(2.0, ai.sum());
+ ai.add(-4.0);
+ assertEquals(-2.0, ai.sum());
+ }
+
+ /**
+ * reset() causes subsequent sum() to return zero
+ */
+ public void testReset() {
+ DoubleAdder ai = new DoubleAdder();
+ ai.add(2.0);
+ assertEquals(2.0, ai.sum());
+ ai.reset();
+ assertEquals(0.0, ai.sum());
+ }
+
+ /**
+ * sumThenReset() returns sum; subsequent sum() returns zero
+ */
+ public void testSumThenReset() {
+ DoubleAdder ai = new DoubleAdder();
+ ai.add(2.0);
+ assertEquals(2.0, ai.sum());
+ assertEquals(2.0, ai.sumThenReset());
+ assertEquals(0.0, ai.sum());
+ }
+
+ /**
+ * a deserialized serialized adder holds same value
+ */
+ public void testSerialization() throws Exception {
+ DoubleAdder x = new DoubleAdder();
+ DoubleAdder y = serialClone(x);
+ assertNotSame(x, y);
+ x.add(-22.0);
+ DoubleAdder z = serialClone(x);
+ assertEquals(-22.0, x.sum());
+ assertEquals(0.0, y.sum());
+ assertEquals(-22.0, z.sum());
+ }
+
+ /**
+ * toString returns current value.
+ */
+ public void testToString() {
+ DoubleAdder ai = new DoubleAdder();
+ assertEquals(Double.toString(0.0), ai.toString());
+ ai.add(1.0);
+ assertEquals(Double.toString(1.0), ai.toString());
+ }
+
+ /**
+ * intValue returns current value.
+ */
+ public void testIntValue() {
+ DoubleAdder ai = new DoubleAdder();
+ assertEquals(0, ai.intValue());
+ ai.add(1.0);
+ assertEquals(1, ai.intValue());
+ }
+
+ /**
+ * longValue returns current value.
+ */
+ public void testLongValue() {
+ DoubleAdder ai = new DoubleAdder();
+ assertEquals(0, ai.longValue());
+ ai.add(1.0);
+ assertEquals(1, ai.longValue());
+ }
+
+ /**
+ * floatValue returns current value.
+ */
+ public void testFloatValue() {
+ DoubleAdder ai = new DoubleAdder();
+ assertEquals(0.0f, ai.floatValue());
+ ai.add(1.0);
+ assertEquals(1.0f, ai.floatValue());
+ }
+
+ /**
+ * doubleValue returns current value.
+ */
+ public void testDoubleValue() {
+ DoubleAdder ai = new DoubleAdder();
+ assertEquals(0.0, ai.doubleValue());
+ ai.add(1.0);
+ assertEquals(1.0, ai.doubleValue());
+ }
+
+ /**
+ * adds by multiple threads produce correct sum
+ */
+ public void testAddAndSumMT() throws Throwable {
+ final int incs = 1000000;
+ final int nthreads = 4;
+ final ExecutorService pool = Executors.newCachedThreadPool();
+ DoubleAdder a = new DoubleAdder();
+ CyclicBarrier barrier = new CyclicBarrier(nthreads + 1);
+ for (int i = 0; i < nthreads; ++i)
+ pool.execute(new AdderTask(a, barrier, incs));
+ barrier.await();
+ barrier.await();
+ double total = (long)nthreads * incs;
+ double sum = a.sum();
+ assertEquals(sum, total);
+ pool.shutdown();
+ }
+
+ static final class AdderTask implements Runnable {
+ final DoubleAdder adder;
+ final CyclicBarrier barrier;
+ final int incs;
+ volatile double result;
+ AdderTask(DoubleAdder adder, CyclicBarrier barrier, int incs) {
+ this.adder = adder;
+ this.barrier = barrier;
+ this.incs = incs;
+ }
+
+ public void run() {
+ try {
+ barrier.await();
+ DoubleAdder a = adder;
+ for (int i = 0; i < incs; ++i)
+ a.add(1.0);
+ result = a.sum();
+ barrier.await();
+ } catch (Throwable t) { throw new Error(t); }
+ }
+ }
+
+}
diff --git a/jsr166-tests/src/test/java/jsr166/EntryTest.java b/jsr166-tests/src/test/java/jsr166/EntryTest.java
index d141a84..72740e3 100644
--- a/jsr166-tests/src/test/java/jsr166/EntryTest.java
+++ b/jsr166-tests/src/test/java/jsr166/EntryTest.java
@@ -20,7 +20,7 @@
// main(suite(), args);
// }
// public static Test suite() {
- // return new TestSuite(...);
+ // return new TestSuite(EntryTest.class);
// }
static final String k1 = "1";
diff --git a/jsr166-tests/src/test/java/jsr166/ExchangerTest.java b/jsr166-tests/src/test/java/jsr166/ExchangerTest.java
index 172fccd..b111980 100644
--- a/jsr166-tests/src/test/java/jsr166/ExchangerTest.java
+++ b/jsr166-tests/src/test/java/jsr166/ExchangerTest.java
@@ -26,7 +26,7 @@
// main(suite(), args);
// }
// public static Test suite() {
- // return new TestSuite(...);
+ // return new TestSuite(ExchangerTest.class);
// }
/**
diff --git a/jsr166-tests/src/test/java/jsr166/ExecutorCompletionServiceTest.java b/jsr166-tests/src/test/java/jsr166/ExecutorCompletionServiceTest.java
index e988cc6..0f58e78 100644
--- a/jsr166-tests/src/test/java/jsr166/ExecutorCompletionServiceTest.java
+++ b/jsr166-tests/src/test/java/jsr166/ExecutorCompletionServiceTest.java
@@ -33,7 +33,7 @@
// main(suite(), args);
// }
// public static Test suite() {
- // return new TestSuite(...);
+ // return new TestSuite(ExecutorCompletionServiceTest.class);
// }
/**
@@ -61,15 +61,14 @@
* Submitting a null callable throws NPE
*/
public void testSubmitNPE() {
- ExecutorService e = Executors.newCachedThreadPool();
- ExecutorCompletionService ecs = new ExecutorCompletionService(e);
- try {
+ final ExecutorService e = Executors.newCachedThreadPool();
+ final ExecutorCompletionService ecs = new ExecutorCompletionService(e);
+ try (PoolCleaner cleaner = cleaner(e)) {
Callable c = null;
- ecs.submit(c);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
- joinPool(e);
+ try {
+ ecs.submit(c);
+ shouldThrow();
+ } catch (NullPointerException success) {}
}
}
@@ -77,15 +76,14 @@
* Submitting a null runnable throws NPE
*/
public void testSubmitNPE2() {
- ExecutorService e = Executors.newCachedThreadPool();
- ExecutorCompletionService ecs = new ExecutorCompletionService(e);
- try {
+ final ExecutorService e = Executors.newCachedThreadPool();
+ final ExecutorCompletionService ecs = new ExecutorCompletionService(e);
+ try (PoolCleaner cleaner = cleaner(e)) {
Runnable r = null;
- ecs.submit(r, Boolean.TRUE);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
- joinPool(e);
+ try {
+ ecs.submit(r, Boolean.TRUE);
+ shouldThrow();
+ } catch (NullPointerException success) {}
}
}
@@ -93,15 +91,13 @@
* A taken submitted task is completed
*/
public void testTake() throws InterruptedException {
- ExecutorService e = Executors.newCachedThreadPool();
- ExecutorCompletionService ecs = new ExecutorCompletionService(e);
- try {
+ final ExecutorService e = Executors.newCachedThreadPool();
+ final ExecutorCompletionService ecs = new ExecutorCompletionService(e);
+ try (PoolCleaner cleaner = cleaner(e)) {
Callable c = new StringTask();
ecs.submit(c);
Future f = ecs.take();
assertTrue(f.isDone());
- } finally {
- joinPool(e);
}
}
@@ -109,15 +105,13 @@
* Take returns the same future object returned by submit
*/
public void testTake2() throws InterruptedException {
- ExecutorService e = Executors.newCachedThreadPool();
- ExecutorCompletionService ecs = new ExecutorCompletionService(e);
- try {
+ final ExecutorService e = Executors.newCachedThreadPool();
+ final ExecutorCompletionService ecs = new ExecutorCompletionService(e);
+ try (PoolCleaner cleaner = cleaner(e)) {
Callable c = new StringTask();
Future f1 = ecs.submit(c);
Future f2 = ecs.take();
assertSame(f1, f2);
- } finally {
- joinPool(e);
}
}
@@ -125,9 +119,9 @@
* If poll returns non-null, the returned task is completed
*/
public void testPoll1() throws Exception {
- ExecutorService e = Executors.newCachedThreadPool();
- ExecutorCompletionService ecs = new ExecutorCompletionService(e);
- try {
+ final ExecutorService e = Executors.newCachedThreadPool();
+ final ExecutorCompletionService ecs = new ExecutorCompletionService(e);
+ try (PoolCleaner cleaner = cleaner(e)) {
assertNull(ecs.poll());
Callable c = new StringTask();
ecs.submit(c);
@@ -141,8 +135,6 @@
}
assertTrue(f.isDone());
assertSame(TEST_STRING, f.get());
- } finally {
- joinPool(e);
}
}
@@ -150,17 +142,15 @@
* If timed poll returns non-null, the returned task is completed
*/
public void testPoll2() throws InterruptedException {
- ExecutorService e = Executors.newCachedThreadPool();
- ExecutorCompletionService ecs = new ExecutorCompletionService(e);
- try {
+ final ExecutorService e = Executors.newCachedThreadPool();
+ final ExecutorCompletionService ecs = new ExecutorCompletionService(e);
+ try (PoolCleaner cleaner = cleaner(e)) {
assertNull(ecs.poll());
Callable c = new StringTask();
ecs.submit(c);
Future f = ecs.poll(SHORT_DELAY_MS, MILLISECONDS);
if (f != null)
assertTrue(f.isDone());
- } finally {
- joinPool(e);
}
}
@@ -174,15 +164,16 @@
MyCallableFuture(Callable<V> c) { super(c); }
protected void done() { done.set(true); }
}
- ExecutorService e = new ThreadPoolExecutor(
- 1, 1, 30L, TimeUnit.SECONDS,
- new ArrayBlockingQueue<Runnable>(1)) {
- protected <T> RunnableFuture<T> newTaskFor(Callable<T> c) {
- return new MyCallableFuture<T>(c);
- }};
+ final ExecutorService e =
+ new ThreadPoolExecutor(1, 1,
+ 30L, TimeUnit.SECONDS,
+ new ArrayBlockingQueue<Runnable>(1)) {
+ protected <T> RunnableFuture<T> newTaskFor(Callable<T> c) {
+ return new MyCallableFuture<T>(c);
+ }};
ExecutorCompletionService<String> ecs =
new ExecutorCompletionService<String>(e);
- try {
+ try (PoolCleaner cleaner = cleaner(e)) {
assertNull(ecs.poll());
Callable<String> c = new StringTask();
Future f1 = ecs.submit(c);
@@ -191,8 +182,6 @@
Future f2 = ecs.take();
assertSame("submit and take must return same objects", f1, f2);
assertTrue("completed task must have set done", done.get());
- } finally {
- joinPool(e);
}
}
@@ -206,15 +195,16 @@
MyRunnableFuture(Runnable t, V r) { super(t, r); }
protected void done() { done.set(true); }
}
- ExecutorService e = new ThreadPoolExecutor(
- 1, 1, 30L, TimeUnit.SECONDS,
- new ArrayBlockingQueue<Runnable>(1)) {
- protected <T> RunnableFuture<T> newTaskFor(Runnable t, T r) {
- return new MyRunnableFuture<T>(t, r);
- }};
- ExecutorCompletionService<String> ecs =
+ final ExecutorService e =
+ new ThreadPoolExecutor(1, 1,
+ 30L, TimeUnit.SECONDS,
+ new ArrayBlockingQueue<Runnable>(1)) {
+ protected <T> RunnableFuture<T> newTaskFor(Runnable t, T r) {
+ return new MyRunnableFuture<T>(t, r);
+ }};
+ final ExecutorCompletionService<String> ecs =
new ExecutorCompletionService<String>(e);
- try {
+ try (PoolCleaner cleaner = cleaner(e)) {
assertNull(ecs.poll());
Runnable r = new NoOpRunnable();
Future f1 = ecs.submit(r, null);
@@ -223,8 +213,6 @@
Future f2 = ecs.take();
assertSame("submit and take must return same objects", f1, f2);
assertTrue("completed task must have set done", done.get());
- } finally {
- joinPool(e);
}
}
diff --git a/jsr166-tests/src/test/java/jsr166/ExecutorsTest.java b/jsr166-tests/src/test/java/jsr166/ExecutorsTest.java
index ae475f1..d70ae6e 100644
--- a/jsr166-tests/src/test/java/jsr166/ExecutorsTest.java
+++ b/jsr166-tests/src/test/java/jsr166/ExecutorsTest.java
@@ -36,29 +36,31 @@
// main(suite(), args);
// }
// public static Test suite() {
- // return new TestSuite(...);
+ // return new TestSuite(ExecutorsTest.class);
// }
/**
* A newCachedThreadPool can execute runnables
*/
public void testNewCachedThreadPool1() {
- ExecutorService e = Executors.newCachedThreadPool();
- e.execute(new NoOpRunnable());
- e.execute(new NoOpRunnable());
- e.execute(new NoOpRunnable());
- joinPool(e);
+ final ExecutorService e = Executors.newCachedThreadPool();
+ try (PoolCleaner cleaner = cleaner(e)) {
+ e.execute(new NoOpRunnable());
+ e.execute(new NoOpRunnable());
+ e.execute(new NoOpRunnable());
+ }
}
/**
* A newCachedThreadPool with given ThreadFactory can execute runnables
*/
public void testNewCachedThreadPool2() {
- ExecutorService e = Executors.newCachedThreadPool(new SimpleThreadFactory());
- e.execute(new NoOpRunnable());
- e.execute(new NoOpRunnable());
- e.execute(new NoOpRunnable());
- joinPool(e);
+ final ExecutorService e = Executors.newCachedThreadPool(new SimpleThreadFactory());
+ try (PoolCleaner cleaner = cleaner(e)) {
+ e.execute(new NoOpRunnable());
+ e.execute(new NoOpRunnable());
+ e.execute(new NoOpRunnable());
+ }
}
/**
@@ -75,22 +77,24 @@
* A new SingleThreadExecutor can execute runnables
*/
public void testNewSingleThreadExecutor1() {
- ExecutorService e = Executors.newSingleThreadExecutor();
- e.execute(new NoOpRunnable());
- e.execute(new NoOpRunnable());
- e.execute(new NoOpRunnable());
- joinPool(e);
+ final ExecutorService e = Executors.newSingleThreadExecutor();
+ try (PoolCleaner cleaner = cleaner(e)) {
+ e.execute(new NoOpRunnable());
+ e.execute(new NoOpRunnable());
+ e.execute(new NoOpRunnable());
+ }
}
/**
* A new SingleThreadExecutor with given ThreadFactory can execute runnables
*/
public void testNewSingleThreadExecutor2() {
- ExecutorService e = Executors.newSingleThreadExecutor(new SimpleThreadFactory());
- e.execute(new NoOpRunnable());
- e.execute(new NoOpRunnable());
- e.execute(new NoOpRunnable());
- joinPool(e);
+ final ExecutorService e = Executors.newSingleThreadExecutor(new SimpleThreadFactory());
+ try (PoolCleaner cleaner = cleaner(e)) {
+ e.execute(new NoOpRunnable());
+ e.execute(new NoOpRunnable());
+ e.execute(new NoOpRunnable());
+ }
}
/**
@@ -107,13 +111,12 @@
* A new SingleThreadExecutor cannot be casted to concrete implementation
*/
public void testCastNewSingleThreadExecutor() {
- ExecutorService e = Executors.newSingleThreadExecutor();
- try {
- ThreadPoolExecutor tpe = (ThreadPoolExecutor)e;
- shouldThrow();
- } catch (ClassCastException success) {
- } finally {
- joinPool(e);
+ final ExecutorService e = Executors.newSingleThreadExecutor();
+ try (PoolCleaner cleaner = cleaner(e)) {
+ try {
+ ThreadPoolExecutor tpe = (ThreadPoolExecutor)e;
+ shouldThrow();
+ } catch (ClassCastException success) {}
}
}
@@ -121,22 +124,24 @@
* A new newFixedThreadPool can execute runnables
*/
public void testNewFixedThreadPool1() {
- ExecutorService e = Executors.newFixedThreadPool(2);
- e.execute(new NoOpRunnable());
- e.execute(new NoOpRunnable());
- e.execute(new NoOpRunnable());
- joinPool(e);
+ final ExecutorService e = Executors.newFixedThreadPool(2);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ e.execute(new NoOpRunnable());
+ e.execute(new NoOpRunnable());
+ e.execute(new NoOpRunnable());
+ }
}
/**
* A new newFixedThreadPool with given ThreadFactory can execute runnables
*/
public void testNewFixedThreadPool2() {
- ExecutorService e = Executors.newFixedThreadPool(2, new SimpleThreadFactory());
- e.execute(new NoOpRunnable());
- e.execute(new NoOpRunnable());
- e.execute(new NoOpRunnable());
- joinPool(e);
+ final ExecutorService e = Executors.newFixedThreadPool(2, new SimpleThreadFactory());
+ try (PoolCleaner cleaner = cleaner(e)) {
+ e.execute(new NoOpRunnable());
+ e.execute(new NoOpRunnable());
+ e.execute(new NoOpRunnable());
+ }
}
/**
@@ -163,11 +168,12 @@
* An unconfigurable newFixedThreadPool can execute runnables
*/
public void testUnconfigurableExecutorService() {
- ExecutorService e = Executors.unconfigurableExecutorService(Executors.newFixedThreadPool(2));
- e.execute(new NoOpRunnable());
- e.execute(new NoOpRunnable());
- e.execute(new NoOpRunnable());
- joinPool(e);
+ final ExecutorService e = Executors.unconfigurableExecutorService(Executors.newFixedThreadPool(2));
+ try (PoolCleaner cleaner = cleaner(e)) {
+ e.execute(new NoOpRunnable());
+ e.execute(new NoOpRunnable());
+ e.execute(new NoOpRunnable());
+ }
}
/**
@@ -194,8 +200,8 @@
* a newSingleThreadScheduledExecutor successfully runs delayed task
*/
public void testNewSingleThreadScheduledExecutor() throws Exception {
- ScheduledExecutorService p = Executors.newSingleThreadScheduledExecutor();
- try {
+ final ScheduledExecutorService p = Executors.newSingleThreadScheduledExecutor();
+ try (PoolCleaner cleaner = cleaner(p)) {
final CountDownLatch proceed = new CountDownLatch(1);
final Runnable task = new CheckedRunnable() {
public void realRun() {
@@ -211,8 +217,6 @@
assertTrue(f.isDone());
assertFalse(f.isCancelled());
assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
- } finally {
- joinPool(p);
}
}
@@ -220,8 +224,8 @@
* a newScheduledThreadPool successfully runs delayed task
*/
public void testNewScheduledThreadPool() throws Exception {
- ScheduledExecutorService p = Executors.newScheduledThreadPool(2);
- try {
+ final ScheduledExecutorService p = Executors.newScheduledThreadPool(2);
+ try (PoolCleaner cleaner = cleaner(p)) {
final CountDownLatch proceed = new CountDownLatch(1);
final Runnable task = new CheckedRunnable() {
public void realRun() {
@@ -237,8 +241,6 @@
assertTrue(f.isDone());
assertFalse(f.isCancelled());
assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
- } finally {
- joinPool(p);
}
}
@@ -246,10 +248,10 @@
* an unconfigurable newScheduledThreadPool successfully runs delayed task
*/
public void testUnconfigurableScheduledExecutorService() throws Exception {
- ScheduledExecutorService p =
+ final ScheduledExecutorService p =
Executors.unconfigurableScheduledExecutorService
(Executors.newScheduledThreadPool(2));
- try {
+ try (PoolCleaner cleaner = cleaner(p)) {
final CountDownLatch proceed = new CountDownLatch(1);
final Runnable task = new CheckedRunnable() {
public void realRun() {
@@ -265,8 +267,6 @@
assertTrue(f.isDone());
assertFalse(f.isCancelled());
assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
- } finally {
- joinPool(p);
}
}
@@ -327,16 +327,10 @@
done.countDown();
}};
ExecutorService e = Executors.newSingleThreadExecutor(Executors.defaultThreadFactory());
-
- e.execute(r);
- await(done);
-
- try {
- e.shutdown();
- } catch (SecurityException ok) {
+ try (PoolCleaner cleaner = cleaner(e)) {
+ e.execute(r);
+ await(done);
}
-
- joinPool(e);
}
/**
@@ -366,14 +360,14 @@
String name = current.getName();
assertTrue(name.endsWith("thread-1"));
assertSame(thisccl, current.getContextClassLoader());
- // assertEquals(thisacc, AccessController.getContext());
+ //assertEquals(thisacc, AccessController.getContext());
done.countDown();
}};
ExecutorService e = Executors.newSingleThreadExecutor(Executors.privilegedThreadFactory());
- e.execute(r);
- await(done);
- e.shutdown();
- joinPool(e);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ e.execute(r);
+ await(done);
+ }
}};
runWithPermissions(r,
diff --git a/jsr166-tests/src/test/java/jsr166/ForkJoinPool8Test.java b/jsr166-tests/src/test/java/jsr166/ForkJoinPool8Test.java
new file mode 100644
index 0000000..f9f9239
--- /dev/null
+++ b/jsr166-tests/src/test/java/jsr166/ForkJoinPool8Test.java
@@ -0,0 +1,1592 @@
+/*
+ * Written by Doug Lea with assistance from members of JCP JSR-166
+ * Expert Group and released to the public domain, as explained at
+ * http://creativecommons.org/publicdomain/zero/1.0/
+ */
+
+package jsr166;
+
+import static java.util.concurrent.TimeUnit.MILLISECONDS;
+import static java.util.concurrent.TimeUnit.SECONDS;
+
+import java.util.HashSet;
+import java.util.concurrent.CancellationException;
+import java.util.concurrent.CountedCompleter;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ForkJoinPool;
+import java.util.concurrent.ForkJoinTask;
+import java.util.concurrent.RecursiveAction;
+import java.util.concurrent.TimeoutException;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+public class ForkJoinPool8Test 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(ForkJoinPool8Test.class);
+ // }
+
+ /**
+ * Common pool exists and has expected parallelism.
+ */
+ public void testCommonPoolParallelism() {
+ assertEquals(ForkJoinPool.getCommonPoolParallelism(),
+ ForkJoinPool.commonPool().getParallelism());
+ }
+
+ /**
+ * Common pool cannot be shut down
+ */
+ public void testCommonPoolShutDown() {
+ assertFalse(ForkJoinPool.commonPool().isShutdown());
+ assertFalse(ForkJoinPool.commonPool().isTerminating());
+ assertFalse(ForkJoinPool.commonPool().isTerminated());
+ ForkJoinPool.commonPool().shutdown();
+ assertFalse(ForkJoinPool.commonPool().isShutdown());
+ assertFalse(ForkJoinPool.commonPool().isTerminating());
+ assertFalse(ForkJoinPool.commonPool().isTerminated());
+ ForkJoinPool.commonPool().shutdownNow();
+ assertFalse(ForkJoinPool.commonPool().isShutdown());
+ assertFalse(ForkJoinPool.commonPool().isTerminating());
+ assertFalse(ForkJoinPool.commonPool().isTerminated());
+ }
+
+ /*
+ * All of the following test methods are adaptations of those for
+ * RecursiveAction and CountedCompleter, but with all actions
+ * executed in the common pool, generally implicitly via
+ * checkInvoke.
+ */
+
+ private void checkInvoke(ForkJoinTask a) {
+ checkNotDone(a);
+ assertNull(a.invoke());
+ checkCompletedNormally(a);
+ }
+
+ void checkNotDone(ForkJoinTask a) {
+ assertFalse(a.isDone());
+ assertFalse(a.isCompletedNormally());
+ assertFalse(a.isCompletedAbnormally());
+ assertFalse(a.isCancelled());
+ assertNull(a.getException());
+ assertNull(a.getRawResult());
+
+ if (! ForkJoinTask.inForkJoinPool()) {
+ Thread.currentThread().interrupt();
+ try {
+ a.get();
+ shouldThrow();
+ } catch (InterruptedException success) {
+ } catch (Throwable fail) { threadUnexpectedException(fail); }
+
+ Thread.currentThread().interrupt();
+ try {
+ a.get(5L, SECONDS);
+ shouldThrow();
+ } catch (InterruptedException success) {
+ } catch (Throwable fail) { threadUnexpectedException(fail); }
+ }
+
+ try {
+ a.get(0L, SECONDS);
+ shouldThrow();
+ } catch (TimeoutException success) {
+ } catch (Throwable fail) { threadUnexpectedException(fail); }
+ }
+
+ void checkCompletedNormally(ForkJoinTask a) {
+ assertTrue(a.isDone());
+ assertFalse(a.isCancelled());
+ assertTrue(a.isCompletedNormally());
+ assertFalse(a.isCompletedAbnormally());
+ assertNull(a.getException());
+ assertNull(a.getRawResult());
+ assertNull(a.join());
+ assertFalse(a.cancel(false));
+ assertFalse(a.cancel(true));
+ try {
+ assertNull(a.get());
+ } catch (Throwable fail) { threadUnexpectedException(fail); }
+ try {
+ assertNull(a.get(5L, SECONDS));
+ } catch (Throwable fail) { threadUnexpectedException(fail); }
+ }
+
+ void checkCancelled(ForkJoinTask a) {
+ assertTrue(a.isDone());
+ assertTrue(a.isCancelled());
+ assertFalse(a.isCompletedNormally());
+ assertTrue(a.isCompletedAbnormally());
+ assertTrue(a.getException() instanceof CancellationException);
+ assertNull(a.getRawResult());
+
+ try {
+ a.join();
+ shouldThrow();
+ } catch (CancellationException success) {
+ } catch (Throwable fail) { threadUnexpectedException(fail); }
+
+ try {
+ a.get();
+ shouldThrow();
+ } catch (CancellationException success) {
+ } catch (Throwable fail) { threadUnexpectedException(fail); }
+
+ try {
+ a.get(5L, SECONDS);
+ shouldThrow();
+ } catch (CancellationException success) {
+ } catch (Throwable fail) { threadUnexpectedException(fail); }
+ }
+
+ void checkCompletedAbnormally(ForkJoinTask a, Throwable t) {
+ assertTrue(a.isDone());
+ assertFalse(a.isCancelled());
+ assertFalse(a.isCompletedNormally());
+ assertTrue(a.isCompletedAbnormally());
+ assertSame(t.getClass(), a.getException().getClass());
+ assertNull(a.getRawResult());
+ assertFalse(a.cancel(false));
+ assertFalse(a.cancel(true));
+
+ try {
+ a.join();
+ shouldThrow();
+ } catch (Throwable expected) {
+ assertSame(expected.getClass(), t.getClass());
+ }
+
+ try {
+ a.get();
+ shouldThrow();
+ } catch (ExecutionException success) {
+ assertSame(t.getClass(), success.getCause().getClass());
+ } catch (Throwable fail) { threadUnexpectedException(fail); }
+
+ try {
+ a.get(5L, SECONDS);
+ shouldThrow();
+ } catch (ExecutionException success) {
+ assertSame(t.getClass(), success.getCause().getClass());
+ } catch (Throwable fail) { threadUnexpectedException(fail); }
+ }
+
+ public static final class FJException extends RuntimeException {
+ public FJException() { super(); }
+ public FJException(Throwable cause) { super(cause); }
+ }
+
+ // A simple recursive action for testing
+ final class FibAction extends CheckedRecursiveAction {
+ final int number;
+ int result;
+ FibAction(int n) { number = n; }
+ protected void realCompute() {
+ int n = number;
+ if (n <= 1)
+ result = n;
+ else {
+ FibAction f1 = new FibAction(n - 1);
+ FibAction f2 = new FibAction(n - 2);
+ invokeAll(f1, f2);
+ result = f1.result + f2.result;
+ }
+ }
+ }
+
+ // A recursive action failing in base case
+ static final class FailingFibAction extends RecursiveAction {
+ final int number;
+ int result;
+ FailingFibAction(int n) { number = n; }
+ public void compute() {
+ int n = number;
+ if (n <= 1)
+ throw new FJException();
+ else {
+ FailingFibAction f1 = new FailingFibAction(n - 1);
+ FailingFibAction f2 = new FailingFibAction(n - 2);
+ invokeAll(f1, f2);
+ result = f1.result + f2.result;
+ }
+ }
+ }
+
+ /**
+ * invoke returns when task completes normally.
+ * isCompletedAbnormally and isCancelled return false for normally
+ * completed tasks. getRawResult of a RecursiveAction returns null;
+ */
+ public void testInvoke() {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ FibAction f = new FibAction(8);
+ assertNull(f.invoke());
+ assertEquals(21, f.result);
+ checkCompletedNormally(f);
+ }};
+ checkInvoke(a);
+ }
+
+ /**
+ * quietlyInvoke task returns when task completes normally.
+ * isCompletedAbnormally and isCancelled return false for normally
+ * completed tasks
+ */
+ public void testQuietlyInvoke() {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ FibAction f = new FibAction(8);
+ f.quietlyInvoke();
+ assertEquals(21, f.result);
+ checkCompletedNormally(f);
+ }};
+ checkInvoke(a);
+ }
+
+ /**
+ * join of a forked task returns when task completes
+ */
+ public void testForkJoin() {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ FibAction f = new FibAction(8);
+ assertSame(f, f.fork());
+ assertNull(f.join());
+ assertEquals(21, f.result);
+ checkCompletedNormally(f);
+ }};
+ checkInvoke(a);
+ }
+
+ /**
+ * join/quietlyJoin of a forked task succeeds in the presence of interrupts
+ */
+ public void testJoinIgnoresInterrupts() {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ FibAction f = new FibAction(8);
+ final Thread myself = Thread.currentThread();
+
+ // test join()
+ assertSame(f, f.fork());
+ myself.interrupt();
+ assertTrue(myself.isInterrupted());
+ assertNull(f.join());
+ Thread.interrupted();
+ assertEquals(21, f.result);
+ checkCompletedNormally(f);
+
+ f = new FibAction(8);
+ f.cancel(true);
+ assertSame(f, f.fork());
+ myself.interrupt();
+ assertTrue(myself.isInterrupted());
+ try {
+ f.join();
+ shouldThrow();
+ } catch (CancellationException success) {
+ Thread.interrupted();
+ checkCancelled(f);
+ }
+
+ f = new FibAction(8);
+ f.completeExceptionally(new FJException());
+ assertSame(f, f.fork());
+ myself.interrupt();
+ assertTrue(myself.isInterrupted());
+ try {
+ f.join();
+ shouldThrow();
+ } catch (FJException success) {
+ Thread.interrupted();
+ checkCompletedAbnormally(f, success);
+ }
+
+ // test quietlyJoin()
+ f = new FibAction(8);
+ assertSame(f, f.fork());
+ myself.interrupt();
+ assertTrue(myself.isInterrupted());
+ f.quietlyJoin();
+ Thread.interrupted();
+ assertEquals(21, f.result);
+ checkCompletedNormally(f);
+
+ f = new FibAction(8);
+ f.cancel(true);
+ assertSame(f, f.fork());
+ myself.interrupt();
+ assertTrue(myself.isInterrupted());
+ f.quietlyJoin();
+ Thread.interrupted();
+ checkCancelled(f);
+
+ f = new FibAction(8);
+ f.completeExceptionally(new FJException());
+ assertSame(f, f.fork());
+ myself.interrupt();
+ assertTrue(myself.isInterrupted());
+ f.quietlyJoin();
+ Thread.interrupted();
+ checkCompletedAbnormally(f, f.getException());
+ }};
+ checkInvoke(a);
+ a.reinitialize();
+ checkInvoke(a);
+ }
+
+ /**
+ * get of a forked task returns when task completes
+ */
+ public void testForkGet() {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() throws Exception {
+ FibAction f = new FibAction(8);
+ assertSame(f, f.fork());
+ assertNull(f.get());
+ assertEquals(21, f.result);
+ checkCompletedNormally(f);
+ }};
+ checkInvoke(a);
+ }
+
+ /**
+ * timed get of a forked task returns when task completes
+ */
+ public void testForkTimedGet() {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() throws Exception {
+ FibAction f = new FibAction(8);
+ assertSame(f, f.fork());
+ assertNull(f.get(5L, SECONDS));
+ assertEquals(21, f.result);
+ checkCompletedNormally(f);
+ }};
+ checkInvoke(a);
+ }
+
+ /**
+ * timed get with null time unit throws NPE
+ */
+ public void testForkTimedGetNPE() {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() throws Exception {
+ FibAction f = new FibAction(8);
+ assertSame(f, f.fork());
+ try {
+ f.get(5L, null);
+ shouldThrow();
+ } catch (NullPointerException success) {}
+ }};
+ checkInvoke(a);
+ }
+
+ /**
+ * quietlyJoin of a forked task returns when task completes
+ */
+ public void testForkQuietlyJoin() {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ FibAction f = new FibAction(8);
+ assertSame(f, f.fork());
+ f.quietlyJoin();
+ assertEquals(21, f.result);
+ checkCompletedNormally(f);
+ }};
+ checkInvoke(a);
+ }
+
+ /**
+ * invoke task throws exception when task completes abnormally
+ */
+ public void testAbnormalInvoke() {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ FailingFibAction f = new FailingFibAction(8);
+ try {
+ f.invoke();
+ shouldThrow();
+ } catch (FJException success) {
+ checkCompletedAbnormally(f, success);
+ }
+ }};
+ checkInvoke(a);
+ }
+
+ /**
+ * quietlyInvoke task returns when task completes abnormally
+ */
+ public void testAbnormalQuietlyInvoke() {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ FailingFibAction f = new FailingFibAction(8);
+ f.quietlyInvoke();
+ assertTrue(f.getException() instanceof FJException);
+ checkCompletedAbnormally(f, f.getException());
+ }};
+ checkInvoke(a);
+ }
+
+ /**
+ * join of a forked task throws exception when task completes abnormally
+ */
+ public void testAbnormalForkJoin() {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ FailingFibAction f = new FailingFibAction(8);
+ assertSame(f, f.fork());
+ try {
+ f.join();
+ shouldThrow();
+ } catch (FJException success) {
+ checkCompletedAbnormally(f, success);
+ }
+ }};
+ checkInvoke(a);
+ }
+
+ /**
+ * get of a forked task throws exception when task completes abnormally
+ */
+ public void testAbnormalForkGet() {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() throws Exception {
+ FailingFibAction f = new FailingFibAction(8);
+ assertSame(f, f.fork());
+ try {
+ f.get();
+ shouldThrow();
+ } catch (ExecutionException success) {
+ Throwable cause = success.getCause();
+ assertTrue(cause instanceof FJException);
+ checkCompletedAbnormally(f, cause);
+ }
+ }};
+ checkInvoke(a);
+ }
+
+ /**
+ * timed get of a forked task throws exception when task completes abnormally
+ */
+ public void testAbnormalForkTimedGet() {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() throws Exception {
+ FailingFibAction f = new FailingFibAction(8);
+ assertSame(f, f.fork());
+ try {
+ f.get(5L, SECONDS);
+ shouldThrow();
+ } catch (ExecutionException success) {
+ Throwable cause = success.getCause();
+ assertTrue(cause instanceof FJException);
+ checkCompletedAbnormally(f, cause);
+ }
+ }};
+ checkInvoke(a);
+ }
+
+ /**
+ * quietlyJoin of a forked task returns when task completes abnormally
+ */
+ public void testAbnormalForkQuietlyJoin() {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ FailingFibAction f = new FailingFibAction(8);
+ assertSame(f, f.fork());
+ f.quietlyJoin();
+ assertTrue(f.getException() instanceof FJException);
+ checkCompletedAbnormally(f, f.getException());
+ }};
+ checkInvoke(a);
+ }
+
+ /**
+ * invoke task throws exception when task cancelled
+ */
+ public void testCancelledInvoke() {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ FibAction f = new FibAction(8);
+ assertTrue(f.cancel(true));
+ try {
+ f.invoke();
+ shouldThrow();
+ } catch (CancellationException success) {
+ checkCancelled(f);
+ }
+ }};
+ checkInvoke(a);
+ }
+
+ /**
+ * join of a forked task throws exception when task cancelled
+ */
+ public void testCancelledForkJoin() {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ FibAction f = new FibAction(8);
+ assertTrue(f.cancel(true));
+ assertSame(f, f.fork());
+ try {
+ f.join();
+ shouldThrow();
+ } catch (CancellationException success) {
+ checkCancelled(f);
+ }
+ }};
+ checkInvoke(a);
+ }
+
+ /**
+ * get of a forked task throws exception when task cancelled
+ */
+ public void testCancelledForkGet() {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() throws Exception {
+ FibAction f = new FibAction(8);
+ assertTrue(f.cancel(true));
+ assertSame(f, f.fork());
+ try {
+ f.get();
+ shouldThrow();
+ } catch (CancellationException success) {
+ checkCancelled(f);
+ }
+ }};
+ checkInvoke(a);
+ }
+
+ /**
+ * timed get of a forked task throws exception when task cancelled
+ */
+ public void testCancelledForkTimedGet() {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() throws Exception {
+ FibAction f = new FibAction(8);
+ assertTrue(f.cancel(true));
+ assertSame(f, f.fork());
+ try {
+ f.get(5L, SECONDS);
+ shouldThrow();
+ } catch (CancellationException success) {
+ checkCancelled(f);
+ }
+ }};
+ checkInvoke(a);
+ }
+
+ /**
+ * quietlyJoin of a forked task returns when task cancelled
+ */
+ public void testCancelledForkQuietlyJoin() {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ FibAction f = new FibAction(8);
+ assertTrue(f.cancel(true));
+ assertSame(f, f.fork());
+ f.quietlyJoin();
+ checkCancelled(f);
+ }};
+ checkInvoke(a);
+ }
+
+ /**
+ * inForkJoinPool of non-FJ task returns false
+ */
+ public void testInForkJoinPool2() {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ assertFalse(inForkJoinPool());
+ }};
+ assertNull(a.invoke());
+ }
+
+ /**
+ * A reinitialized normally completed task may be re-invoked
+ */
+ public void testReinitialize() {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ FibAction f = new FibAction(8);
+ checkNotDone(f);
+
+ for (int i = 0; i < 3; i++) {
+ assertNull(f.invoke());
+ assertEquals(21, f.result);
+ checkCompletedNormally(f);
+ f.reinitialize();
+ checkNotDone(f);
+ }
+ }};
+ checkInvoke(a);
+ }
+
+ /**
+ * A reinitialized abnormally completed task may be re-invoked
+ */
+ public void testReinitializeAbnormal() {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ FailingFibAction f = new FailingFibAction(8);
+ checkNotDone(f);
+
+ for (int i = 0; i < 3; i++) {
+ try {
+ f.invoke();
+ shouldThrow();
+ } catch (FJException success) {
+ checkCompletedAbnormally(f, success);
+ }
+ f.reinitialize();
+ checkNotDone(f);
+ }
+ }};
+ checkInvoke(a);
+ }
+
+ /**
+ * invoke task throws exception after invoking completeExceptionally
+ */
+ public void testCompleteExceptionally() {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ FibAction f = new FibAction(8);
+ f.completeExceptionally(new FJException());
+ try {
+ f.invoke();
+ shouldThrow();
+ } catch (FJException success) {
+ checkCompletedAbnormally(f, success);
+ }
+ }};
+ checkInvoke(a);
+ }
+
+ /**
+ * invoke task suppresses execution invoking complete
+ */
+ public void testComplete() {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ FibAction f = new FibAction(8);
+ f.complete(null);
+ assertNull(f.invoke());
+ assertEquals(0, f.result);
+ checkCompletedNormally(f);
+ }};
+ checkInvoke(a);
+ }
+
+ /**
+ * invokeAll(t1, t2) invokes all task arguments
+ */
+ public void testInvokeAll2() {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ FibAction f = new FibAction(8);
+ FibAction g = new FibAction(9);
+ invokeAll(f, g);
+ checkCompletedNormally(f);
+ assertEquals(21, f.result);
+ checkCompletedNormally(g);
+ assertEquals(34, g.result);
+ }};
+ checkInvoke(a);
+ }
+
+ /**
+ * invokeAll(tasks) with 1 argument invokes task
+ */
+ public void testInvokeAll1() {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ FibAction f = new FibAction(8);
+ invokeAll(f);
+ checkCompletedNormally(f);
+ assertEquals(21, f.result);
+ }};
+ checkInvoke(a);
+ }
+
+ /**
+ * invokeAll(tasks) with > 2 argument invokes tasks
+ */
+ public void testInvokeAll3() {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ FibAction f = new FibAction(8);
+ FibAction g = new FibAction(9);
+ FibAction h = new FibAction(7);
+ invokeAll(f, g, h);
+ assertTrue(f.isDone());
+ assertTrue(g.isDone());
+ assertTrue(h.isDone());
+ checkCompletedNormally(f);
+ assertEquals(21, f.result);
+ checkCompletedNormally(g);
+ assertEquals(34, g.result);
+ checkCompletedNormally(g);
+ assertEquals(13, h.result);
+ }};
+ checkInvoke(a);
+ }
+
+ /**
+ * invokeAll(collection) invokes all tasks in the collection
+ */
+ public void testInvokeAllCollection() {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ FibAction f = new FibAction(8);
+ FibAction g = new FibAction(9);
+ FibAction h = new FibAction(7);
+ HashSet set = new HashSet();
+ set.add(f);
+ set.add(g);
+ set.add(h);
+ invokeAll(set);
+ assertTrue(f.isDone());
+ assertTrue(g.isDone());
+ assertTrue(h.isDone());
+ checkCompletedNormally(f);
+ assertEquals(21, f.result);
+ checkCompletedNormally(g);
+ assertEquals(34, g.result);
+ checkCompletedNormally(g);
+ assertEquals(13, h.result);
+ }};
+ checkInvoke(a);
+ }
+
+ /**
+ * invokeAll(tasks) with any null task throws NPE
+ */
+ public void testInvokeAllNPE() {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ FibAction f = new FibAction(8);
+ FibAction g = new FibAction(9);
+ FibAction h = null;
+ try {
+ invokeAll(f, g, h);
+ shouldThrow();
+ } catch (NullPointerException success) {}
+ }};
+ checkInvoke(a);
+ }
+
+ /**
+ * invokeAll(t1, t2) throw exception if any task does
+ */
+ public void testAbnormalInvokeAll2() {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ FibAction f = new FibAction(8);
+ FailingFibAction g = new FailingFibAction(9);
+ try {
+ invokeAll(f, g);
+ shouldThrow();
+ } catch (FJException success) {
+ checkCompletedAbnormally(g, success);
+ }
+ }};
+ checkInvoke(a);
+ }
+
+ /**
+ * invokeAll(tasks) with 1 argument throws exception if task does
+ */
+ public void testAbnormalInvokeAll1() {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ FailingFibAction g = new FailingFibAction(9);
+ try {
+ invokeAll(g);
+ shouldThrow();
+ } catch (FJException success) {
+ checkCompletedAbnormally(g, success);
+ }
+ }};
+ checkInvoke(a);
+ }
+
+ /**
+ * invokeAll(tasks) with > 2 argument throws exception if any task does
+ */
+ public void testAbnormalInvokeAll3() {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ FibAction f = new FibAction(8);
+ FailingFibAction g = new FailingFibAction(9);
+ FibAction h = new FibAction(7);
+ try {
+ invokeAll(f, g, h);
+ shouldThrow();
+ } catch (FJException success) {
+ checkCompletedAbnormally(g, success);
+ }
+ }};
+ checkInvoke(a);
+ }
+
+ /**
+ * invokeAll(collection) throws exception if any task does
+ */
+ public void testAbnormalInvokeAllCollection() {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ FailingFibAction f = new FailingFibAction(8);
+ FibAction g = new FibAction(9);
+ FibAction h = new FibAction(7);
+ HashSet set = new HashSet();
+ set.add(f);
+ set.add(g);
+ set.add(h);
+ try {
+ invokeAll(set);
+ shouldThrow();
+ } catch (FJException success) {
+ checkCompletedAbnormally(f, success);
+ }
+ }};
+ checkInvoke(a);
+ }
+
+ // CountedCompleter versions
+
+ abstract static class CCF extends CountedCompleter {
+ int number;
+ int rnumber;
+
+ public CCF(CountedCompleter parent, int n) {
+ super(parent, 1);
+ this.number = n;
+ }
+
+ public final void compute() {
+ CountedCompleter p;
+ CCF f = this;
+ int n = number;
+ while (n >= 2) {
+ new RCCF(f, n - 2).fork();
+ f = new LCCF(f, --n);
+ }
+ f.number = n;
+ f.onCompletion(f);
+ if ((p = f.getCompleter()) != null)
+ p.tryComplete();
+ else
+ f.quietlyComplete();
+ }
+ }
+
+ static final class LCCF extends CCF {
+ public LCCF(CountedCompleter parent, int n) {
+ super(parent, n);
+ }
+ public final void onCompletion(CountedCompleter caller) {
+ CCF p = (CCF)getCompleter();
+ int n = number + rnumber;
+ if (p != null)
+ p.number = n;
+ else
+ number = n;
+ }
+ }
+ static final class RCCF extends CCF {
+ public RCCF(CountedCompleter parent, int n) {
+ super(parent, n);
+ }
+ public final void onCompletion(CountedCompleter caller) {
+ CCF p = (CCF)getCompleter();
+ int n = number + rnumber;
+ if (p != null)
+ p.rnumber = n;
+ else
+ number = n;
+ }
+ }
+
+ // Version of CCF with forced failure in left completions
+ abstract static class FailingCCF extends CountedCompleter {
+ int number;
+ int rnumber;
+
+ public FailingCCF(CountedCompleter parent, int n) {
+ super(parent, 1);
+ this.number = n;
+ }
+
+ public final void compute() {
+ CountedCompleter p;
+ FailingCCF f = this;
+ int n = number;
+ while (n >= 2) {
+ new RFCCF(f, n - 2).fork();
+ f = new LFCCF(f, --n);
+ }
+ f.number = n;
+ f.onCompletion(f);
+ if ((p = f.getCompleter()) != null)
+ p.tryComplete();
+ else
+ f.quietlyComplete();
+ }
+ }
+
+ static final class LFCCF extends FailingCCF {
+ public LFCCF(CountedCompleter parent, int n) {
+ super(parent, n);
+ }
+ public final void onCompletion(CountedCompleter caller) {
+ FailingCCF p = (FailingCCF)getCompleter();
+ int n = number + rnumber;
+ if (p != null)
+ p.number = n;
+ else
+ number = n;
+ }
+ }
+ static final class RFCCF extends FailingCCF {
+ public RFCCF(CountedCompleter parent, int n) {
+ super(parent, n);
+ }
+ public final void onCompletion(CountedCompleter caller) {
+ completeExceptionally(new FJException());
+ }
+ }
+
+ /**
+ * invoke returns when task completes normally.
+ * isCompletedAbnormally and isCancelled return false for normally
+ * completed tasks; getRawResult returns null.
+ */
+ public void testInvokeCC() {
+ ForkJoinTask a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ CCF f = new LCCF(null, 8);
+ assertNull(f.invoke());
+ assertEquals(21, f.number);
+ checkCompletedNormally(f);
+ }};
+ checkInvoke(a);
+ }
+
+ /**
+ * quietlyInvoke task returns when task completes normally.
+ * isCompletedAbnormally and isCancelled return false for normally
+ * completed tasks
+ */
+ public void testQuietlyInvokeCC() {
+ ForkJoinTask a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ CCF f = new LCCF(null, 8);
+ f.quietlyInvoke();
+ assertEquals(21, f.number);
+ checkCompletedNormally(f);
+ }};
+ checkInvoke(a);
+ }
+
+ /**
+ * join of a forked task returns when task completes
+ */
+ public void testForkJoinCC() {
+ ForkJoinTask a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ CCF f = new LCCF(null, 8);
+ assertSame(f, f.fork());
+ assertNull(f.join());
+ assertEquals(21, f.number);
+ checkCompletedNormally(f);
+ }};
+ checkInvoke(a);
+ }
+
+ /**
+ * get of a forked task returns when task completes
+ */
+ public void testForkGetCC() {
+ ForkJoinTask a = new CheckedRecursiveAction() {
+ protected void realCompute() throws Exception {
+ CCF f = new LCCF(null, 8);
+ assertSame(f, f.fork());
+ assertNull(f.get());
+ assertEquals(21, f.number);
+ checkCompletedNormally(f);
+ }};
+ checkInvoke(a);
+ }
+
+ /**
+ * timed get of a forked task returns when task completes
+ */
+ public void testForkTimedGetCC() {
+ ForkJoinTask a = new CheckedRecursiveAction() {
+ protected void realCompute() throws Exception {
+ CCF f = new LCCF(null, 8);
+ assertSame(f, f.fork());
+ assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
+ assertEquals(21, f.number);
+ checkCompletedNormally(f);
+ }};
+ checkInvoke(a);
+ }
+
+ /**
+ * timed get with null time unit throws NPE
+ */
+ public void testForkTimedGetNPECC() {
+ ForkJoinTask a = new CheckedRecursiveAction() {
+ protected void realCompute() throws Exception {
+ CCF f = new LCCF(null, 8);
+ assertSame(f, f.fork());
+ try {
+ f.get(5L, null);
+ shouldThrow();
+ } catch (NullPointerException success) {}
+ }};
+ checkInvoke(a);
+ }
+
+ /**
+ * quietlyJoin of a forked task returns when task completes
+ */
+ public void testForkQuietlyJoinCC() {
+ ForkJoinTask a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ CCF f = new LCCF(null, 8);
+ assertSame(f, f.fork());
+ f.quietlyJoin();
+ assertEquals(21, f.number);
+ checkCompletedNormally(f);
+ }};
+ checkInvoke(a);
+ }
+
+ /**
+ * invoke task throws exception when task completes abnormally
+ */
+ public void testAbnormalInvokeCC() {
+ ForkJoinTask a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ FailingCCF f = new LFCCF(null, 8);
+ try {
+ f.invoke();
+ shouldThrow();
+ } catch (FJException success) {
+ checkCompletedAbnormally(f, success);
+ }
+ }};
+ checkInvoke(a);
+ }
+
+ /**
+ * quietlyInvoke task returns when task completes abnormally
+ */
+ public void testAbnormalQuietlyInvokeCC() {
+ ForkJoinTask a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ FailingCCF f = new LFCCF(null, 8);
+ f.quietlyInvoke();
+ assertTrue(f.getException() instanceof FJException);
+ checkCompletedAbnormally(f, f.getException());
+ }};
+ checkInvoke(a);
+ }
+
+ /**
+ * join of a forked task throws exception when task completes abnormally
+ */
+ public void testAbnormalForkJoinCC() {
+ ForkJoinTask a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ FailingCCF f = new LFCCF(null, 8);
+ assertSame(f, f.fork());
+ try {
+ f.join();
+ shouldThrow();
+ } catch (FJException success) {
+ checkCompletedAbnormally(f, success);
+ }
+ }};
+ checkInvoke(a);
+ }
+
+ /**
+ * get of a forked task throws exception when task completes abnormally
+ */
+ public void testAbnormalForkGetCC() {
+ ForkJoinTask a = new CheckedRecursiveAction() {
+ protected void realCompute() throws Exception {
+ FailingCCF f = new LFCCF(null, 8);
+ assertSame(f, f.fork());
+ try {
+ f.get();
+ shouldThrow();
+ } catch (ExecutionException success) {
+ Throwable cause = success.getCause();
+ assertTrue(cause instanceof FJException);
+ checkCompletedAbnormally(f, cause);
+ }
+ }};
+ checkInvoke(a);
+ }
+
+ /**
+ * timed get of a forked task throws exception when task completes abnormally
+ */
+ public void testAbnormalForkTimedGetCC() {
+ ForkJoinTask a = new CheckedRecursiveAction() {
+ protected void realCompute() throws Exception {
+ FailingCCF f = new LFCCF(null, 8);
+ assertSame(f, f.fork());
+ try {
+ f.get(LONG_DELAY_MS, MILLISECONDS);
+ shouldThrow();
+ } catch (ExecutionException success) {
+ Throwable cause = success.getCause();
+ assertTrue(cause instanceof FJException);
+ checkCompletedAbnormally(f, cause);
+ }
+ }};
+ checkInvoke(a);
+ }
+
+ /**
+ * quietlyJoin of a forked task returns when task completes abnormally
+ */
+ public void testAbnormalForkQuietlyJoinCC() {
+ ForkJoinTask a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ FailingCCF f = new LFCCF(null, 8);
+ assertSame(f, f.fork());
+ f.quietlyJoin();
+ assertTrue(f.getException() instanceof FJException);
+ checkCompletedAbnormally(f, f.getException());
+ }};
+ checkInvoke(a);
+ }
+
+ /**
+ * invoke task throws exception when task cancelled
+ */
+ public void testCancelledInvokeCC() {
+ ForkJoinTask a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ CCF f = new LCCF(null, 8);
+ assertTrue(f.cancel(true));
+ try {
+ f.invoke();
+ shouldThrow();
+ } catch (CancellationException success) {
+ checkCancelled(f);
+ }
+ }};
+ checkInvoke(a);
+ }
+
+ /**
+ * join of a forked task throws exception when task cancelled
+ */
+ public void testCancelledForkJoinCC() {
+ ForkJoinTask a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ CCF f = new LCCF(null, 8);
+ assertTrue(f.cancel(true));
+ assertSame(f, f.fork());
+ try {
+ f.join();
+ shouldThrow();
+ } catch (CancellationException success) {
+ checkCancelled(f);
+ }
+ }};
+ checkInvoke(a);
+ }
+
+ /**
+ * get of a forked task throws exception when task cancelled
+ */
+ public void testCancelledForkGetCC() {
+ ForkJoinTask a = new CheckedRecursiveAction() {
+ protected void realCompute() throws Exception {
+ CCF f = new LCCF(null, 8);
+ assertTrue(f.cancel(true));
+ assertSame(f, f.fork());
+ try {
+ f.get();
+ shouldThrow();
+ } catch (CancellationException success) {
+ checkCancelled(f);
+ }
+ }};
+ checkInvoke(a);
+ }
+
+ /**
+ * timed get of a forked task throws exception when task cancelled
+ */
+ public void testCancelledForkTimedGetCC() throws Exception {
+ ForkJoinTask a = new CheckedRecursiveAction() {
+ protected void realCompute() throws Exception {
+ CCF f = new LCCF(null, 8);
+ assertTrue(f.cancel(true));
+ assertSame(f, f.fork());
+ try {
+ f.get(LONG_DELAY_MS, MILLISECONDS);
+ shouldThrow();
+ } catch (CancellationException success) {
+ checkCancelled(f);
+ }
+ }};
+ checkInvoke(a);
+ }
+
+ /**
+ * quietlyJoin of a forked task returns when task cancelled
+ */
+ public void testCancelledForkQuietlyJoinCC() {
+ ForkJoinTask a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ CCF f = new LCCF(null, 8);
+ assertTrue(f.cancel(true));
+ assertSame(f, f.fork());
+ f.quietlyJoin();
+ checkCancelled(f);
+ }};
+ checkInvoke(a);
+ }
+
+ /**
+ * getPool of non-FJ task returns null
+ */
+ public void testGetPool2CC() {
+ ForkJoinTask a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ assertNull(getPool());
+ }};
+ assertNull(a.invoke());
+ }
+
+ /**
+ * inForkJoinPool of non-FJ task returns false
+ */
+ public void testInForkJoinPool2CC() {
+ ForkJoinTask a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ assertFalse(inForkJoinPool());
+ }};
+ assertNull(a.invoke());
+ }
+
+ /**
+ * setRawResult(null) succeeds
+ */
+ public void testSetRawResultCC() {
+ ForkJoinTask a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ setRawResult(null);
+ assertNull(getRawResult());
+ }};
+ assertNull(a.invoke());
+ }
+
+ /**
+ * invoke task throws exception after invoking completeExceptionally
+ */
+ public void testCompleteExceptionally2CC() {
+ ForkJoinTask a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ CCF f = new LCCF(null, 8);
+ f.completeExceptionally(new FJException());
+ try {
+ f.invoke();
+ shouldThrow();
+ } catch (FJException success) {
+ checkCompletedAbnormally(f, success);
+ }
+ }};
+ checkInvoke(a);
+ }
+
+ /**
+ * invokeAll(t1, t2) invokes all task arguments
+ */
+ public void testInvokeAll2CC() {
+ ForkJoinTask a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ CCF f = new LCCF(null, 8);
+ CCF g = new LCCF(null, 9);
+ invokeAll(f, g);
+ assertEquals(21, f.number);
+ assertEquals(34, g.number);
+ checkCompletedNormally(f);
+ checkCompletedNormally(g);
+ }};
+ checkInvoke(a);
+ }
+
+ /**
+ * invokeAll(tasks) with 1 argument invokes task
+ */
+ public void testInvokeAll1CC() {
+ ForkJoinTask a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ CCF f = new LCCF(null, 8);
+ invokeAll(f);
+ checkCompletedNormally(f);
+ assertEquals(21, f.number);
+ }};
+ checkInvoke(a);
+ }
+
+ /**
+ * invokeAll(tasks) with > 2 argument invokes tasks
+ */
+ public void testInvokeAll3CC() {
+ ForkJoinTask a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ CCF f = new LCCF(null, 8);
+ CCF g = new LCCF(null, 9);
+ CCF h = new LCCF(null, 7);
+ invokeAll(f, g, h);
+ assertEquals(21, f.number);
+ assertEquals(34, g.number);
+ assertEquals(13, h.number);
+ checkCompletedNormally(f);
+ checkCompletedNormally(g);
+ checkCompletedNormally(h);
+ }};
+ checkInvoke(a);
+ }
+
+ /**
+ * invokeAll(collection) invokes all tasks in the collection
+ */
+ public void testInvokeAllCollectionCC() {
+ ForkJoinTask a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ CCF f = new LCCF(null, 8);
+ CCF g = new LCCF(null, 9);
+ CCF h = new LCCF(null, 7);
+ HashSet set = new HashSet();
+ set.add(f);
+ set.add(g);
+ set.add(h);
+ invokeAll(set);
+ assertEquals(21, f.number);
+ assertEquals(34, g.number);
+ assertEquals(13, h.number);
+ checkCompletedNormally(f);
+ checkCompletedNormally(g);
+ checkCompletedNormally(h);
+ }};
+ checkInvoke(a);
+ }
+
+ /**
+ * invokeAll(tasks) with any null task throws NPE
+ */
+ public void testInvokeAllNPECC() {
+ ForkJoinTask a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ CCF f = new LCCF(null, 8);
+ CCF g = new LCCF(null, 9);
+ CCF h = null;
+ try {
+ invokeAll(f, g, h);
+ shouldThrow();
+ } catch (NullPointerException success) {}
+ }};
+ checkInvoke(a);
+ }
+
+ /**
+ * invokeAll(t1, t2) throw exception if any task does
+ */
+ public void testAbnormalInvokeAll2CC() {
+ ForkJoinTask a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ CCF f = new LCCF(null, 8);
+ FailingCCF g = new LFCCF(null, 9);
+ try {
+ invokeAll(f, g);
+ shouldThrow();
+ } catch (FJException success) {
+ checkCompletedAbnormally(g, success);
+ }
+ }};
+ checkInvoke(a);
+ }
+
+ /**
+ * invokeAll(tasks) with 1 argument throws exception if task does
+ */
+ public void testAbnormalInvokeAll1CC() {
+ ForkJoinTask a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ FailingCCF g = new LFCCF(null, 9);
+ try {
+ invokeAll(g);
+ shouldThrow();
+ } catch (FJException success) {
+ checkCompletedAbnormally(g, success);
+ }
+ }};
+ checkInvoke(a);
+ }
+
+ /**
+ * invokeAll(tasks) with > 2 argument throws exception if any task does
+ */
+ public void testAbnormalInvokeAll3CC() {
+ ForkJoinTask a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ CCF f = new LCCF(null, 8);
+ FailingCCF g = new LFCCF(null, 9);
+ CCF h = new LCCF(null, 7);
+ try {
+ invokeAll(f, g, h);
+ shouldThrow();
+ } catch (FJException success) {
+ checkCompletedAbnormally(g, success);
+ }
+ }};
+ checkInvoke(a);
+ }
+
+ /**
+ * invokeAll(collection) throws exception if any task does
+ */
+ public void testAbnormalInvokeAllCollectionCC() {
+ ForkJoinTask a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ FailingCCF f = new LFCCF(null, 8);
+ CCF g = new LCCF(null, 9);
+ CCF h = new LCCF(null, 7);
+ HashSet set = new HashSet();
+ set.add(f);
+ set.add(g);
+ set.add(h);
+ try {
+ invokeAll(set);
+ shouldThrow();
+ } catch (FJException success) {
+ checkCompletedAbnormally(f, success);
+ }
+ }};
+ checkInvoke(a);
+ }
+
+ /**
+ * awaitQuiescence by a worker is equivalent in effect to
+ * ForkJoinTask.helpQuiesce()
+ */
+ public void testAwaitQuiescence1() throws Exception {
+ final ForkJoinPool p = new ForkJoinPool();
+ try (PoolCleaner cleaner = cleaner(p)) {
+ final long startTime = System.nanoTime();
+ assertTrue(p.isQuiescent());
+ ForkJoinTask a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ FibAction f = new FibAction(8);
+ assertSame(f, f.fork());
+ assertSame(p, ForkJoinTask.getPool());
+ boolean quiescent = p.awaitQuiescence(LONG_DELAY_MS, MILLISECONDS);
+ assertTrue(quiescent);
+ assertFalse(p.isQuiescent());
+ while (!f.isDone()) {
+ assertFalse(p.getAsyncMode());
+ assertFalse(p.isShutdown());
+ assertFalse(p.isTerminating());
+ assertFalse(p.isTerminated());
+ Thread.yield();
+ }
+ assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
+ assertFalse(p.isQuiescent());
+ assertEquals(0, ForkJoinTask.getQueuedTaskCount());
+ assertEquals(21, f.result);
+ }};
+ p.execute(a);
+ while (!a.isDone() || !p.isQuiescent()) {
+ assertFalse(p.getAsyncMode());
+ assertFalse(p.isShutdown());
+ assertFalse(p.isTerminating());
+ assertFalse(p.isTerminated());
+ Thread.yield();
+ }
+ assertEquals(0, p.getQueuedTaskCount());
+ assertFalse(p.getAsyncMode());
+ assertEquals(0, p.getQueuedSubmissionCount());
+ assertFalse(p.hasQueuedSubmissions());
+ while (p.getActiveThreadCount() != 0
+ && millisElapsedSince(startTime) < LONG_DELAY_MS)
+ Thread.yield();
+ assertFalse(p.isShutdown());
+ assertFalse(p.isTerminating());
+ assertFalse(p.isTerminated());
+ assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
+ }
+ }
+
+ /**
+ * awaitQuiescence returns when pool isQuiescent() or the indicated
+ * timeout elapsed
+ */
+ public void testAwaitQuiescence2() throws Exception {
+ /**
+ * """It is possible to disable or limit the use of threads in the
+ * common pool by setting the parallelism property to zero. However
+ * doing so may cause unjoined tasks to never be executed."""
+ */
+ if ("0".equals(System.getProperty(
+ "java.util.concurrent.ForkJoinPool.common.parallelism")))
+ return;
+ final ForkJoinPool p = new ForkJoinPool();
+ try (PoolCleaner cleaner = cleaner(p)) {
+ assertTrue(p.isQuiescent());
+ final long startTime = System.nanoTime();
+ ForkJoinTask a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ FibAction f = new FibAction(8);
+ assertSame(f, f.fork());
+ while (!f.isDone()
+ && millisElapsedSince(startTime) < LONG_DELAY_MS) {
+ assertFalse(p.getAsyncMode());
+ assertFalse(p.isShutdown());
+ assertFalse(p.isTerminating());
+ assertFalse(p.isTerminated());
+ Thread.yield();
+ }
+ assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
+ assertEquals(0, ForkJoinTask.getQueuedTaskCount());
+ assertEquals(21, f.result);
+ }};
+ p.execute(a);
+ assertTrue(p.awaitQuiescence(LONG_DELAY_MS, MILLISECONDS));
+ assertTrue(p.isQuiescent());
+ assertTrue(a.isDone());
+ assertEquals(0, p.getQueuedTaskCount());
+ assertFalse(p.getAsyncMode());
+ assertEquals(0, p.getQueuedSubmissionCount());
+ assertFalse(p.hasQueuedSubmissions());
+ while (p.getActiveThreadCount() != 0
+ && millisElapsedSince(startTime) < LONG_DELAY_MS)
+ Thread.yield();
+ assertFalse(p.isShutdown());
+ assertFalse(p.isTerminating());
+ assertFalse(p.isTerminated());
+ assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
+ }
+ }
+
+}
diff --git a/jsr166-tests/src/test/java/jsr166/ForkJoinPoolTest.java b/jsr166-tests/src/test/java/jsr166/ForkJoinPoolTest.java
index 09a3511..e3bb428 100644
--- a/jsr166-tests/src/test/java/jsr166/ForkJoinPoolTest.java
+++ b/jsr166-tests/src/test/java/jsr166/ForkJoinPoolTest.java
@@ -40,7 +40,7 @@
// main(suite(), args);
// }
// public static Test suite() {
- // return new TestSuite(...);
+ // return new TestSuite(ForkJoinPoolTest.class);
// }
/*
@@ -68,10 +68,12 @@
}
}
+ static class MyError extends Error {}
+
// to test handlers
static class FailingFJWSubclass extends ForkJoinWorkerThread {
public FailingFJWSubclass(ForkJoinPool p) { super(p) ; }
- protected void onStart() { super.onStart(); throw new Error(); }
+ protected void onStart() { super.onStart(); throw new MyError(); }
}
static class FailingThreadFactory
@@ -166,7 +168,7 @@
*/
public void testDefaultInitialState() {
ForkJoinPool p = new ForkJoinPool(1);
- try {
+ try (PoolCleaner cleaner = cleaner(p)) {
assertSame(ForkJoinPool.defaultForkJoinWorkerThreadFactory,
p.getFactory());
assertFalse(p.getAsyncMode());
@@ -178,8 +180,6 @@
assertFalse(p.isShutdown());
assertFalse(p.isTerminating());
assertFalse(p.isTerminated());
- } finally {
- joinPool(p);
}
}
@@ -208,10 +208,8 @@
*/
public void testGetParallelism() {
ForkJoinPool p = new ForkJoinPool(1);
- try {
+ try (PoolCleaner cleaner = cleaner(p)) {
assertEquals(1, p.getParallelism());
- } finally {
- joinPool(p);
}
}
@@ -219,14 +217,26 @@
* getPoolSize returns number of started workers.
*/
public void testGetPoolSize() {
- ForkJoinPool p = new ForkJoinPool(1);
- try {
+ final CountDownLatch taskStarted = new CountDownLatch(1);
+ final CountDownLatch done = new CountDownLatch(1);
+ final ForkJoinPool p = new ForkJoinPool(1);
+ try (PoolCleaner cleaner = cleaner(p)) {
assertEquals(0, p.getActiveThreadCount());
- Future<String> future = p.submit(new StringTask());
+ final Runnable task = new CheckedRunnable() {
+ public void realRun() throws InterruptedException {
+ taskStarted.countDown();
+ assertEquals(1, p.getPoolSize());
+ assertEquals(1, p.getActiveThreadCount());
+ done.await();
+ }};
+ Future<?> future = p.submit(task);
+ await(taskStarted);
assertEquals(1, p.getPoolSize());
- } finally {
- joinPool(p);
+ assertEquals(1, p.getActiveThreadCount());
+ done.countDown();
}
+ assertEquals(0, p.getPoolSize());
+ assertEquals(0, p.getActiveThreadCount());
}
/**
@@ -234,26 +244,28 @@
*/
public void testAwaitTermination_timesOut() throws InterruptedException {
ForkJoinPool p = new ForkJoinPool(1);
- assertFalse(p.isTerminated());
- assertFalse(p.awaitTermination(Long.MIN_VALUE, NANOSECONDS));
- assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS));
- assertFalse(p.awaitTermination(-1L, NANOSECONDS));
- assertFalse(p.awaitTermination(-1L, MILLISECONDS));
- assertFalse(p.awaitTermination(0L, NANOSECONDS));
- assertFalse(p.awaitTermination(0L, MILLISECONDS));
- long timeoutNanos = 999999L;
- long startTime = System.nanoTime();
- assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS));
- assertTrue(System.nanoTime() - startTime >= timeoutNanos);
- assertFalse(p.isTerminated());
- startTime = System.nanoTime();
- long timeoutMillis = timeoutMillis();
- assertFalse(p.awaitTermination(timeoutMillis, MILLISECONDS));
- assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
- assertFalse(p.isTerminated());
- p.shutdown();
- assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
- assertTrue(p.isTerminated());
+ try (PoolCleaner cleaner = cleaner(p)) {
+ assertFalse(p.isTerminated());
+ assertFalse(p.awaitTermination(Long.MIN_VALUE, NANOSECONDS));
+ assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS));
+ assertFalse(p.awaitTermination(-1L, NANOSECONDS));
+ assertFalse(p.awaitTermination(-1L, MILLISECONDS));
+ assertFalse(p.awaitTermination(0L, NANOSECONDS));
+ assertFalse(p.awaitTermination(0L, MILLISECONDS));
+ long timeoutNanos = 999999L;
+ long startTime = System.nanoTime();
+ assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS));
+ assertTrue(System.nanoTime() - startTime >= timeoutNanos);
+ assertFalse(p.isTerminated());
+ startTime = System.nanoTime();
+ long timeoutMillis = timeoutMillis();
+ assertFalse(p.awaitTermination(timeoutMillis, MILLISECONDS));
+ assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
+ assertFalse(p.isTerminated());
+ p.shutdown();
+ assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
+ assertTrue(p.isTerminated());
+ }
}
/**
@@ -264,23 +276,23 @@
*/
public void testSetUncaughtExceptionHandler() throws InterruptedException {
final CountDownLatch uehInvoked = new CountDownLatch(1);
- final Thread.UncaughtExceptionHandler eh =
+ final Thread.UncaughtExceptionHandler ueh =
new Thread.UncaughtExceptionHandler() {
public void uncaughtException(Thread t, Throwable e) {
+ threadAssertTrue(e instanceof MyError);
+ threadAssertTrue(t instanceof FailingFJWSubclass);
uehInvoked.countDown();
}};
ForkJoinPool p = new ForkJoinPool(1, new FailingThreadFactory(),
- eh, false);
- try {
- assertSame(eh, p.getUncaughtExceptionHandler());
+ ueh, false);
+ try (PoolCleaner cleaner = cleaner(p)) {
+ assertSame(ueh, p.getUncaughtExceptionHandler());
try {
p.execute(new FibTask(8));
- assertTrue(uehInvoked.await(MEDIUM_DELAY_MS, MILLISECONDS));
- } catch (RejectedExecutionException ok) {
+ await(uehInvoked);
+ } finally {
+ p.shutdownNow(); // failure might have prevented processing task
}
- } finally {
- p.shutdownNow(); // failure might have prevented processing task
- joinPool(p);
}
}
@@ -292,7 +304,7 @@
*/
public void testIsQuiescent() throws Exception {
ForkJoinPool p = new ForkJoinPool(2);
- try {
+ try (PoolCleaner cleaner = cleaner(p)) {
assertTrue(p.isQuiescent());
long startTime = System.nanoTime();
FibTask f = new FibTask(20);
@@ -311,17 +323,18 @@
assertTrue(p.isQuiescent());
assertFalse(p.getAsyncMode());
- assertEquals(0, p.getActiveThreadCount());
assertEquals(0, p.getQueuedTaskCount());
assertEquals(0, p.getQueuedSubmissionCount());
assertFalse(p.hasQueuedSubmissions());
+ while (p.getActiveThreadCount() != 0
+ && millisElapsedSince(startTime) < LONG_DELAY_MS)
+ Thread.yield();
assertFalse(p.isShutdown());
assertFalse(p.isTerminating());
assertFalse(p.isTerminated());
assertTrue(f.isDone());
assertEquals(6765, (int) f.get());
- } finally {
- joinPool(p);
+ assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
}
}
@@ -330,11 +343,9 @@
*/
public void testSubmitForkJoinTask() throws Throwable {
ForkJoinPool p = new ForkJoinPool(1);
- try {
+ try (PoolCleaner cleaner = cleaner(p)) {
ForkJoinTask<Integer> f = p.submit(new FibTask(8));
assertEquals(21, (int) f.get());
- } finally {
- joinPool(p);
}
}
@@ -343,15 +354,13 @@
*/
public void testSubmitAfterShutdown() {
ForkJoinPool p = new ForkJoinPool(1);
- try {
+ try (PoolCleaner cleaner = cleaner(p)) {
p.shutdown();
assertTrue(p.isShutdown());
try {
ForkJoinTask<Integer> f = p.submit(new FibTask(8));
shouldThrow();
} catch (RejectedExecutionException success) {}
- } finally {
- joinPool(p);
}
}
@@ -377,16 +386,14 @@
public void testPollSubmission() {
final CountDownLatch done = new CountDownLatch(1);
SubFJP p = new SubFJP();
- try {
+ try (PoolCleaner cleaner = cleaner(p)) {
ForkJoinTask a = p.submit(awaiter(done));
ForkJoinTask b = p.submit(awaiter(done));
ForkJoinTask c = p.submit(awaiter(done));
ForkJoinTask r = p.pollSubmission();
assertTrue(r == a || r == b || r == c);
assertFalse(r.isDone());
- } finally {
done.countDown();
- joinPool(p);
}
}
@@ -396,7 +403,7 @@
public void testDrainTasksTo() {
final CountDownLatch done = new CountDownLatch(1);
SubFJP p = new SubFJP();
- try {
+ try (PoolCleaner cleaner = cleaner(p)) {
ForkJoinTask a = p.submit(awaiter(done));
ForkJoinTask b = p.submit(awaiter(done));
ForkJoinTask c = p.submit(awaiter(done));
@@ -407,9 +414,7 @@
assertTrue(r == a || r == b || r == c);
assertFalse(r.isDone());
}
- } finally {
done.countDown();
- joinPool(p);
}
}
@@ -420,7 +425,7 @@
*/
public void testExecuteRunnable() throws Throwable {
ExecutorService e = new ForkJoinPool(1);
- try {
+ try (PoolCleaner cleaner = cleaner(e)) {
final AtomicBoolean done = new AtomicBoolean(false);
Future<?> future = e.submit(new CheckedRunnable() {
public void realRun() {
@@ -431,8 +436,6 @@
assertTrue(done.get());
assertTrue(future.isDone());
assertFalse(future.isCancelled());
- } finally {
- joinPool(e);
}
}
@@ -441,13 +444,11 @@
*/
public void testSubmitCallable() throws Throwable {
ExecutorService e = new ForkJoinPool(1);
- try {
+ try (PoolCleaner cleaner = cleaner(e)) {
Future<String> future = e.submit(new StringTask());
assertSame(TEST_STRING, future.get());
assertTrue(future.isDone());
assertFalse(future.isCancelled());
- } finally {
- joinPool(e);
}
}
@@ -456,13 +457,11 @@
*/
public void testSubmitRunnable() throws Throwable {
ExecutorService e = new ForkJoinPool(1);
- try {
+ try (PoolCleaner cleaner = cleaner(e)) {
Future<?> future = e.submit(new NoOpRunnable());
assertNull(future.get());
assertTrue(future.isDone());
assertFalse(future.isCancelled());
- } finally {
- joinPool(e);
}
}
@@ -471,13 +470,11 @@
*/
public void testSubmitRunnable2() throws Throwable {
ExecutorService e = new ForkJoinPool(1);
- try {
+ try (PoolCleaner cleaner = cleaner(e)) {
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
assertSame(TEST_STRING, future.get());
assertTrue(future.isDone());
assertFalse(future.isCancelled());
- } finally {
- joinPool(e);
}
}
@@ -490,11 +487,9 @@
Runnable r = new CheckedRunnable() {
public void realRun() throws Exception {
ExecutorService e = new ForkJoinPool(1);
- try {
+ try (PoolCleaner cleaner = cleaner(e)) {
Future future = e.submit(callable);
assertSame(TEST_STRING, future.get());
- } finally {
- joinPool(e);
}
}};
@@ -511,11 +506,9 @@
Runnable r = new CheckedRunnable() {
public void realRun() throws Exception {
ExecutorService e = new ForkJoinPool(1);
- try {
+ try (PoolCleaner cleaner = cleaner(e)) {
Future future = e.submit(callable);
assertSame(TEST_STRING, future.get());
- } finally {
- joinPool(e);
}
}};
@@ -532,7 +525,7 @@
Runnable r = new CheckedRunnable() {
public void realRun() throws Exception {
ExecutorService e = new ForkJoinPool(1);
- try {
+ try (PoolCleaner cleaner = cleaner(e)) {
Future future = e.submit(callable);
try {
future.get();
@@ -540,8 +533,6 @@
} catch (ExecutionException success) {
assertTrue(success.getCause() instanceof IndexOutOfBoundsException);
}
- } finally {
- joinPool(e);
}
}};
@@ -553,12 +544,11 @@
*/
public void testExecuteNullRunnable() {
ExecutorService e = new ForkJoinPool(1);
- try {
- Future<?> future = e.submit((Runnable) null);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
- joinPool(e);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ try {
+ Future<?> future = e.submit((Runnable) null);
+ shouldThrow();
+ } catch (NullPointerException success) {}
}
}
@@ -567,12 +557,11 @@
*/
public void testSubmitNullCallable() {
ExecutorService e = new ForkJoinPool(1);
- try {
- Future<String> future = e.submit((Callable) null);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
- joinPool(e);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ try {
+ Future<String> future = e.submit((Callable) null);
+ shouldThrow();
+ } catch (NullPointerException success) {}
}
}
@@ -582,13 +571,13 @@
public void testInterruptedSubmit() throws InterruptedException {
final CountDownLatch submitted = new CountDownLatch(1);
final CountDownLatch quittingTime = new CountDownLatch(1);
- final ExecutorService p = new ForkJoinPool(1);
final Callable<Void> awaiter = new CheckedCallable<Void>() {
public Void realCall() throws InterruptedException {
- assertTrue(quittingTime.await(MEDIUM_DELAY_MS, MILLISECONDS));
+ assertTrue(quittingTime.await(2*LONG_DELAY_MS, MILLISECONDS));
return null;
}};
- try {
+ final ExecutorService p = new ForkJoinPool(1);
+ try (PoolCleaner cleaner = cleaner(p, quittingTime)) {
Thread t = new Thread(new CheckedInterruptedRunnable() {
public void realRun() throws Exception {
Future<Void> future = p.submit(awaiter);
@@ -596,12 +585,9 @@
future.get();
}});
t.start();
- assertTrue(submitted.await(MEDIUM_DELAY_MS, MILLISECONDS));
+ await(submitted);
t.interrupt();
- t.join();
- } finally {
- quittingTime.countDown();
- joinPool(p);
+ awaitTermination(t);
}
}
@@ -611,15 +597,15 @@
*/
public void testSubmitEE() throws Throwable {
ForkJoinPool p = new ForkJoinPool(1);
- try {
- p.submit(new Callable() {
- public Object call() { throw new ArithmeticException(); }})
- .get();
- shouldThrow();
- } catch (ExecutionException success) {
- assertTrue(success.getCause() instanceof ArithmeticException);
- } finally {
- joinPool(p);
+ try (PoolCleaner cleaner = cleaner(p)) {
+ try {
+ p.submit(new Callable() {
+ public Object call() { throw new ArithmeticException(); }})
+ .get();
+ shouldThrow();
+ } catch (ExecutionException success) {
+ assertTrue(success.getCause() instanceof ArithmeticException);
+ }
}
}
@@ -628,12 +614,11 @@
*/
public void testInvokeAny1() throws Throwable {
ExecutorService e = new ForkJoinPool(1);
- try {
- e.invokeAny(null);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
- joinPool(e);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ try {
+ e.invokeAny(null);
+ shouldThrow();
+ } catch (NullPointerException success) {}
}
}
@@ -642,12 +627,11 @@
*/
public void testInvokeAny2() throws Throwable {
ExecutorService e = new ForkJoinPool(1);
- try {
- e.invokeAny(new ArrayList<Callable<String>>());
- shouldThrow();
- } catch (IllegalArgumentException success) {
- } finally {
- joinPool(e);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ try {
+ e.invokeAny(new ArrayList<Callable<String>>());
+ shouldThrow();
+ } catch (IllegalArgumentException success) {}
}
}
@@ -656,14 +640,13 @@
*/
public void testInvokeAny3() throws Throwable {
ExecutorService e = new ForkJoinPool(1);
- List<Callable<String>> l = new ArrayList<Callable<String>>();
- l.add(null);
- try {
- e.invokeAny(l);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
- joinPool(e);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ List<Callable<String>> l = new ArrayList<Callable<String>>();
+ l.add(null);
+ try {
+ e.invokeAny(l);
+ shouldThrow();
+ } catch (NullPointerException success) {}
}
}
@@ -673,16 +656,15 @@
public void testInvokeAny4() throws Throwable {
CountDownLatch latch = new CountDownLatch(1);
ExecutorService e = new ForkJoinPool(1);
- List<Callable<String>> l = new ArrayList<Callable<String>>();
- l.add(latchAwaitingStringTask(latch));
- l.add(null);
- try {
- e.invokeAny(l);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
+ try (PoolCleaner cleaner = cleaner(e)) {
+ List<Callable<String>> l = new ArrayList<Callable<String>>();
+ l.add(latchAwaitingStringTask(latch));
+ l.add(null);
+ try {
+ e.invokeAny(l);
+ shouldThrow();
+ } catch (NullPointerException success) {}
latch.countDown();
- joinPool(e);
}
}
@@ -691,15 +673,15 @@
*/
public void testInvokeAny5() throws Throwable {
ExecutorService e = new ForkJoinPool(1);
- List<Callable<String>> l = new ArrayList<Callable<String>>();
- l.add(new NPETask());
- try {
- e.invokeAny(l);
- shouldThrow();
- } catch (ExecutionException success) {
- assertTrue(success.getCause() instanceof NullPointerException);
- } finally {
- joinPool(e);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ List<Callable<String>> l = new ArrayList<Callable<String>>();
+ l.add(new NPETask());
+ try {
+ e.invokeAny(l);
+ shouldThrow();
+ } catch (ExecutionException success) {
+ assertTrue(success.getCause() instanceof NullPointerException);
+ }
}
}
@@ -708,14 +690,12 @@
*/
public void testInvokeAny6() throws Throwable {
ExecutorService e = new ForkJoinPool(1);
- try {
+ try (PoolCleaner cleaner = cleaner(e)) {
List<Callable<String>> l = new ArrayList<Callable<String>>();
l.add(new StringTask());
l.add(new StringTask());
String result = e.invokeAny(l);
assertSame(TEST_STRING, result);
- } finally {
- joinPool(e);
}
}
@@ -724,12 +704,11 @@
*/
public void testInvokeAll1() throws Throwable {
ExecutorService e = new ForkJoinPool(1);
- try {
- e.invokeAll(null);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
- joinPool(e);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ try {
+ e.invokeAll(null);
+ shouldThrow();
+ } catch (NullPointerException success) {}
}
}
@@ -738,12 +717,10 @@
*/
public void testInvokeAll2() throws InterruptedException {
ExecutorService e = new ForkJoinPool(1);
- try {
+ try (PoolCleaner cleaner = cleaner(e)) {
List<Future<String>> r
= e.invokeAll(new ArrayList<Callable<String>>());
assertTrue(r.isEmpty());
- } finally {
- joinPool(e);
}
}
@@ -752,15 +729,14 @@
*/
public void testInvokeAll3() throws InterruptedException {
ExecutorService e = new ForkJoinPool(1);
- List<Callable<String>> l = new ArrayList<Callable<String>>();
- l.add(new StringTask());
- l.add(null);
- try {
- e.invokeAll(l);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
- joinPool(e);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ List<Callable<String>> l = new ArrayList<Callable<String>>();
+ l.add(new StringTask());
+ l.add(null);
+ try {
+ e.invokeAll(l);
+ shouldThrow();
+ } catch (NullPointerException success) {}
}
}
@@ -770,17 +746,17 @@
*/
public void testInvokeAll4() throws Throwable {
ExecutorService e = new ForkJoinPool(1);
- List<Callable<String>> l = new ArrayList<Callable<String>>();
- l.add(new NPETask());
- List<Future<String>> futures = e.invokeAll(l);
- assertEquals(1, futures.size());
- try {
- futures.get(0).get();
- shouldThrow();
- } catch (ExecutionException success) {
- assertTrue(success.getCause() instanceof NullPointerException);
- } finally {
- joinPool(e);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ List<Callable<String>> l = new ArrayList<Callable<String>>();
+ l.add(new NPETask());
+ List<Future<String>> futures = e.invokeAll(l);
+ assertEquals(1, futures.size());
+ try {
+ futures.get(0).get();
+ shouldThrow();
+ } catch (ExecutionException success) {
+ assertTrue(success.getCause() instanceof NullPointerException);
+ }
}
}
@@ -789,7 +765,7 @@
*/
public void testInvokeAll5() throws Throwable {
ExecutorService e = new ForkJoinPool(1);
- try {
+ try (PoolCleaner cleaner = cleaner(e)) {
List<Callable<String>> l = new ArrayList<Callable<String>>();
l.add(new StringTask());
l.add(new StringTask());
@@ -797,8 +773,6 @@
assertEquals(2, futures.size());
for (Future<String> future : futures)
assertSame(TEST_STRING, future.get());
- } finally {
- joinPool(e);
}
}
@@ -807,12 +781,11 @@
*/
public void testTimedInvokeAny1() throws Throwable {
ExecutorService e = new ForkJoinPool(1);
- try {
- e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
- joinPool(e);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ try {
+ e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
+ shouldThrow();
+ } catch (NullPointerException success) {}
}
}
@@ -821,14 +794,13 @@
*/
public void testTimedInvokeAnyNullTimeUnit() throws Throwable {
ExecutorService e = new ForkJoinPool(1);
- List<Callable<String>> l = new ArrayList<Callable<String>>();
- l.add(new StringTask());
- try {
- e.invokeAny(l, MEDIUM_DELAY_MS, null);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
- joinPool(e);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ List<Callable<String>> l = new ArrayList<Callable<String>>();
+ l.add(new StringTask());
+ try {
+ e.invokeAny(l, MEDIUM_DELAY_MS, null);
+ shouldThrow();
+ } catch (NullPointerException success) {}
}
}
@@ -837,13 +809,12 @@
*/
public void testTimedInvokeAny2() throws Throwable {
ExecutorService e = new ForkJoinPool(1);
- try {
- e.invokeAny(new ArrayList<Callable<String>>(),
- MEDIUM_DELAY_MS, MILLISECONDS);
- shouldThrow();
- } catch (IllegalArgumentException success) {
- } finally {
- joinPool(e);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ try {
+ e.invokeAny(new ArrayList<Callable<String>>(),
+ MEDIUM_DELAY_MS, MILLISECONDS);
+ shouldThrow();
+ } catch (IllegalArgumentException success) {}
}
}
@@ -853,16 +824,15 @@
public void testTimedInvokeAny3() throws Throwable {
CountDownLatch latch = new CountDownLatch(1);
ExecutorService e = new ForkJoinPool(1);
- List<Callable<String>> l = new ArrayList<Callable<String>>();
- l.add(latchAwaitingStringTask(latch));
- l.add(null);
- try {
- e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
+ try (PoolCleaner cleaner = cleaner(e)) {
+ List<Callable<String>> l = new ArrayList<Callable<String>>();
+ l.add(latchAwaitingStringTask(latch));
+ l.add(null);
+ try {
+ e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
+ shouldThrow();
+ } catch (NullPointerException success) {}
latch.countDown();
- joinPool(e);
}
}
@@ -871,15 +841,17 @@
*/
public void testTimedInvokeAny4() throws Throwable {
ExecutorService e = new ForkJoinPool(1);
- List<Callable<String>> l = new ArrayList<Callable<String>>();
- l.add(new NPETask());
- try {
- e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
- shouldThrow();
- } catch (ExecutionException success) {
- assertTrue(success.getCause() instanceof NullPointerException);
- } finally {
- joinPool(e);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ long startTime = System.nanoTime();
+ List<Callable<String>> l = new ArrayList<Callable<String>>();
+ l.add(new NPETask());
+ try {
+ e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
+ shouldThrow();
+ } catch (ExecutionException success) {
+ assertTrue(success.getCause() instanceof NullPointerException);
+ }
+ assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
}
}
@@ -888,14 +860,14 @@
*/
public void testTimedInvokeAny5() throws Throwable {
ExecutorService e = new ForkJoinPool(1);
- try {
+ try (PoolCleaner cleaner = cleaner(e)) {
+ long startTime = System.nanoTime();
List<Callable<String>> l = new ArrayList<Callable<String>>();
l.add(new StringTask());
l.add(new StringTask());
- String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
+ String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
assertSame(TEST_STRING, result);
- } finally {
- joinPool(e);
+ assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
}
}
@@ -904,12 +876,11 @@
*/
public void testTimedInvokeAll1() throws Throwable {
ExecutorService e = new ForkJoinPool(1);
- try {
- e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
- joinPool(e);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ try {
+ e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
+ shouldThrow();
+ } catch (NullPointerException success) {}
}
}
@@ -918,14 +889,13 @@
*/
public void testTimedInvokeAllNullTimeUnit() throws Throwable {
ExecutorService e = new ForkJoinPool(1);
- List<Callable<String>> l = new ArrayList<Callable<String>>();
- l.add(new StringTask());
- try {
- e.invokeAll(l, MEDIUM_DELAY_MS, null);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
- joinPool(e);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ List<Callable<String>> l = new ArrayList<Callable<String>>();
+ l.add(new StringTask());
+ try {
+ e.invokeAll(l, MEDIUM_DELAY_MS, null);
+ shouldThrow();
+ } catch (NullPointerException success) {}
}
}
@@ -934,13 +904,11 @@
*/
public void testTimedInvokeAll2() throws InterruptedException {
ExecutorService e = new ForkJoinPool(1);
- try {
+ try (PoolCleaner cleaner = cleaner(e)) {
List<Future<String>> r
= e.invokeAll(new ArrayList<Callable<String>>(),
MEDIUM_DELAY_MS, MILLISECONDS);
assertTrue(r.isEmpty());
- } finally {
- joinPool(e);
}
}
@@ -949,15 +917,14 @@
*/
public void testTimedInvokeAll3() throws InterruptedException {
ExecutorService e = new ForkJoinPool(1);
- List<Callable<String>> l = new ArrayList<Callable<String>>();
- l.add(new StringTask());
- l.add(null);
- try {
- e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
- joinPool(e);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ List<Callable<String>> l = new ArrayList<Callable<String>>();
+ l.add(new StringTask());
+ l.add(null);
+ try {
+ e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
+ shouldThrow();
+ } catch (NullPointerException success) {}
}
}
@@ -966,18 +933,18 @@
*/
public void testTimedInvokeAll4() throws Throwable {
ExecutorService e = new ForkJoinPool(1);
- List<Callable<String>> l = new ArrayList<Callable<String>>();
- l.add(new NPETask());
- List<Future<String>> futures
- = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
- assertEquals(1, futures.size());
- try {
- futures.get(0).get();
- shouldThrow();
- } catch (ExecutionException success) {
- assertTrue(success.getCause() instanceof NullPointerException);
- } finally {
- joinPool(e);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ List<Callable<String>> l = new ArrayList<Callable<String>>();
+ l.add(new NPETask());
+ List<Future<String>> futures
+ = e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
+ assertEquals(1, futures.size());
+ try {
+ futures.get(0).get();
+ shouldThrow();
+ } catch (ExecutionException success) {
+ assertTrue(success.getCause() instanceof NullPointerException);
+ }
}
}
@@ -985,18 +952,16 @@
* timed invokeAll(c) returns results of all completed tasks in c
*/
public void testTimedInvokeAll5() throws Throwable {
- ExecutorService e = new ForkJoinPool(1);
- try {
+ ForkJoinPool e = new ForkJoinPool(1);
+ try (PoolCleaner cleaner = cleaner(e)) {
List<Callable<String>> l = new ArrayList<Callable<String>>();
l.add(new StringTask());
l.add(new StringTask());
List<Future<String>> futures
- = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
+ = e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
assertEquals(2, futures.size());
for (Future<String> future : futures)
assertSame(TEST_STRING, future.get());
- } finally {
- joinPool(e);
}
}
diff --git a/jsr166-tests/src/test/java/jsr166/ForkJoinTask8Test.java b/jsr166-tests/src/test/java/jsr166/ForkJoinTask8Test.java
new file mode 100644
index 0000000..6c03348
--- /dev/null
+++ b/jsr166-tests/src/test/java/jsr166/ForkJoinTask8Test.java
@@ -0,0 +1,1205 @@
+/*
+ * Written by Doug Lea with assistance from members of JCP JSR-166
+ * Expert Group and released to the public domain, as explained at
+ * http://creativecommons.org/publicdomain/zero/1.0/
+ */
+
+package jsr166;
+
+import static java.util.concurrent.TimeUnit.MILLISECONDS;
+import static java.util.concurrent.TimeUnit.SECONDS;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ForkJoinPool;
+import java.util.concurrent.ForkJoinTask;
+import java.util.concurrent.ForkJoinWorkerThread;
+import java.util.concurrent.RecursiveAction;
+import java.util.concurrent.TimeoutException;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+public class ForkJoinTask8Test extends JSR166TestCase {
+
+ /*
+ * Testing notes: This differs from ForkJoinTaskTest mainly by
+ * defining a version of BinaryAsyncAction that uses JDK8 task
+ * tags for control state, thereby testing getForkJoinTaskTag,
+ * setForkJoinTaskTag, and compareAndSetForkJoinTaskTag across
+ * various contexts. Most of the test methods using it are
+ * otherwise identical, but omitting retest of those dealing with
+ * cancellation, which is not represented in this tag scheme.
+ */
+
+ static final short INITIAL_STATE = -1;
+ static final short COMPLETE_STATE = 0;
+ static final short EXCEPTION_STATE = 1;
+
+ // 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(ForkJoinTask8Test.class);
+ // }
+
+ // Runs with "mainPool" use > 1 thread. singletonPool tests use 1
+ static final int mainPoolSize =
+ Math.max(2, Runtime.getRuntime().availableProcessors());
+
+ private static ForkJoinPool mainPool() {
+ return new ForkJoinPool(mainPoolSize);
+ }
+
+ private static ForkJoinPool singletonPool() {
+ return new ForkJoinPool(1);
+ }
+
+ private static ForkJoinPool asyncSingletonPool() {
+ return new ForkJoinPool(1,
+ ForkJoinPool.defaultForkJoinWorkerThreadFactory,
+ null, true);
+ }
+
+ // Compute fib naively and efficiently
+ final int[] fib;
+ {
+ int[] fib = new int[10];
+ fib[0] = 0;
+ fib[1] = 1;
+ for (int i = 2; i < fib.length; i++)
+ fib[i] = fib[i - 1] + fib[i - 2];
+ this.fib = fib;
+ }
+
+ private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) {
+ try (PoolCleaner cleaner = cleaner(pool)) {
+ assertFalse(a.isDone());
+ assertFalse(a.isCompletedNormally());
+ assertFalse(a.isCompletedAbnormally());
+ assertFalse(a.isCancelled());
+ assertNull(a.getException());
+ assertNull(a.getRawResult());
+
+ assertNull(pool.invoke(a));
+
+ assertTrue(a.isDone());
+ assertTrue(a.isCompletedNormally());
+ assertFalse(a.isCompletedAbnormally());
+ assertFalse(a.isCancelled());
+ assertNull(a.getException());
+ assertNull(a.getRawResult());
+ }
+ }
+
+ void checkNotDone(ForkJoinTask a) {
+ assertFalse(a.isDone());
+ assertFalse(a.isCompletedNormally());
+ assertFalse(a.isCompletedAbnormally());
+ assertFalse(a.isCancelled());
+ assertNull(a.getException());
+ assertNull(a.getRawResult());
+ if (a instanceof BinaryAsyncAction)
+ assertTrue(((BinaryAsyncAction)a).getForkJoinTaskTag() == INITIAL_STATE);
+
+ try {
+ a.get(0L, SECONDS);
+ shouldThrow();
+ } catch (TimeoutException success) {
+ } catch (Throwable fail) { threadUnexpectedException(fail); }
+ }
+
+ <T> void checkCompletedNormally(ForkJoinTask<T> a) {
+ checkCompletedNormally(a, null);
+ }
+
+ <T> void checkCompletedNormally(ForkJoinTask<T> a, T expected) {
+ assertTrue(a.isDone());
+ assertFalse(a.isCancelled());
+ assertTrue(a.isCompletedNormally());
+ assertFalse(a.isCompletedAbnormally());
+ assertNull(a.getException());
+ assertSame(expected, a.getRawResult());
+ if (a instanceof BinaryAsyncAction)
+ assertTrue(((BinaryAsyncAction)a).getForkJoinTaskTag() == COMPLETE_STATE);
+
+ {
+ Thread.currentThread().interrupt();
+ long startTime = System.nanoTime();
+ assertSame(expected, a.join());
+ assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
+ Thread.interrupted();
+ }
+
+ {
+ Thread.currentThread().interrupt();
+ long startTime = System.nanoTime();
+ a.quietlyJoin(); // should be no-op
+ assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
+ Thread.interrupted();
+ }
+
+ assertFalse(a.cancel(false));
+ assertFalse(a.cancel(true));
+ try {
+ assertSame(expected, a.get());
+ } catch (Throwable fail) { threadUnexpectedException(fail); }
+ try {
+ assertSame(expected, a.get(5L, SECONDS));
+ } catch (Throwable fail) { threadUnexpectedException(fail); }
+ }
+
+ void checkCompletedAbnormally(ForkJoinTask a, Throwable t) {
+ assertTrue(a.isDone());
+ assertFalse(a.isCancelled());
+ assertFalse(a.isCompletedNormally());
+ assertTrue(a.isCompletedAbnormally());
+ assertSame(t.getClass(), a.getException().getClass());
+ assertNull(a.getRawResult());
+ assertFalse(a.cancel(false));
+ assertFalse(a.cancel(true));
+ if (a instanceof BinaryAsyncAction)
+ assertTrue(((BinaryAsyncAction)a).getForkJoinTaskTag() != INITIAL_STATE);
+
+ try {
+ Thread.currentThread().interrupt();
+ a.join();
+ shouldThrow();
+ } catch (Throwable expected) {
+ assertSame(t.getClass(), expected.getClass());
+ }
+ Thread.interrupted();
+
+ {
+ long startTime = System.nanoTime();
+ a.quietlyJoin(); // should be no-op
+ assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
+ }
+
+ try {
+ a.get();
+ shouldThrow();
+ } catch (ExecutionException success) {
+ assertSame(t.getClass(), success.getCause().getClass());
+ } catch (Throwable fail) { threadUnexpectedException(fail); }
+
+ try {
+ a.get(5L, SECONDS);
+ shouldThrow();
+ } catch (ExecutionException success) {
+ assertSame(t.getClass(), success.getCause().getClass());
+ } catch (Throwable fail) { threadUnexpectedException(fail); }
+ }
+
+ public static final class FJException extends RuntimeException {
+ FJException() { super(); }
+ }
+
+ abstract static class BinaryAsyncAction extends ForkJoinTask<Void> {
+
+ private volatile BinaryAsyncAction parent;
+
+ private volatile BinaryAsyncAction sibling;
+
+ protected BinaryAsyncAction() {
+ setForkJoinTaskTag(INITIAL_STATE);
+ }
+
+ public final Void getRawResult() { return null; }
+ protected final void setRawResult(Void mustBeNull) { }
+
+ public final void linkSubtasks(BinaryAsyncAction x, BinaryAsyncAction y) {
+ x.parent = y.parent = this;
+ x.sibling = y;
+ y.sibling = x;
+ }
+
+ protected void onComplete(BinaryAsyncAction x, BinaryAsyncAction y) {
+ if (this.getForkJoinTaskTag() != COMPLETE_STATE ||
+ x.getForkJoinTaskTag() != COMPLETE_STATE ||
+ y.getForkJoinTaskTag() != COMPLETE_STATE) {
+ completeThisExceptionally(new FJException());
+ }
+ }
+
+ protected boolean onException() {
+ return true;
+ }
+
+ public void linkAndForkSubtasks(BinaryAsyncAction x, BinaryAsyncAction y) {
+ linkSubtasks(x, y);
+ y.fork();
+ x.fork();
+ }
+
+ private void completeThis() {
+ setForkJoinTaskTag(COMPLETE_STATE);
+ super.complete(null);
+ }
+
+ private void completeThisExceptionally(Throwable ex) {
+ setForkJoinTaskTag(EXCEPTION_STATE);
+ super.completeExceptionally(ex);
+ }
+
+ public boolean cancel(boolean mayInterruptIfRunning) {
+ if (super.cancel(mayInterruptIfRunning)) {
+ completeExceptionally(new FJException());
+ return true;
+ }
+ return false;
+ }
+
+ public final void complete() {
+ BinaryAsyncAction a = this;
+ for (;;) {
+ BinaryAsyncAction s = a.sibling;
+ BinaryAsyncAction p = a.parent;
+ a.sibling = null;
+ a.parent = null;
+ a.completeThis();
+ if (p == null ||
+ p.compareAndSetForkJoinTaskTag(INITIAL_STATE, COMPLETE_STATE))
+ break;
+ try {
+ p.onComplete(a, s);
+ } catch (Throwable rex) {
+ p.completeExceptionally(rex);
+ return;
+ }
+ a = p;
+ }
+ }
+
+ public final void completeExceptionally(Throwable ex) {
+ for (BinaryAsyncAction a = this;;) {
+ a.completeThisExceptionally(ex);
+ BinaryAsyncAction s = a.sibling;
+ if (s != null && !s.isDone())
+ s.completeExceptionally(ex);
+ if ((a = a.parent) == null)
+ break;
+ }
+ }
+
+ public final BinaryAsyncAction getParent() {
+ return parent;
+ }
+
+ public BinaryAsyncAction getSibling() {
+ return sibling;
+ }
+
+ public void reinitialize() {
+ parent = sibling = null;
+ super.reinitialize();
+ }
+
+ }
+
+ final class AsyncFib extends BinaryAsyncAction {
+ int number;
+ int expectedResult;
+ public AsyncFib(int number) {
+ this.number = number;
+ this.expectedResult = fib[number];
+ }
+
+ public final boolean exec() {
+ try {
+ AsyncFib f = this;
+ int n = f.number;
+ while (n > 1) {
+ AsyncFib p = f;
+ AsyncFib r = new AsyncFib(n - 2);
+ f = new AsyncFib(--n);
+ p.linkSubtasks(r, f);
+ r.fork();
+ }
+ f.complete();
+ }
+ catch (Throwable ex) {
+ compareAndSetForkJoinTaskTag(INITIAL_STATE, EXCEPTION_STATE);
+ }
+ if (getForkJoinTaskTag() == EXCEPTION_STATE)
+ throw new FJException();
+ return false;
+ }
+
+ protected void onComplete(BinaryAsyncAction x, BinaryAsyncAction y) {
+ number = ((AsyncFib)x).number + ((AsyncFib)y).number;
+ super.onComplete(x, y);
+ }
+
+ public void checkCompletedNormally() {
+ assertEquals(expectedResult, number);
+ ForkJoinTask8Test.this.checkCompletedNormally(this);
+ }
+ }
+
+ static final class FailingAsyncFib extends BinaryAsyncAction {
+ int number;
+ public FailingAsyncFib(int n) {
+ this.number = n;
+ }
+
+ public final boolean exec() {
+ try {
+ FailingAsyncFib f = this;
+ int n = f.number;
+ while (n > 1) {
+ FailingAsyncFib p = f;
+ FailingAsyncFib r = new FailingAsyncFib(n - 2);
+ f = new FailingAsyncFib(--n);
+ p.linkSubtasks(r, f);
+ r.fork();
+ }
+ f.complete();
+ }
+ catch (Throwable ex) {
+ compareAndSetForkJoinTaskTag(INITIAL_STATE, EXCEPTION_STATE);
+ }
+ if (getForkJoinTaskTag() == EXCEPTION_STATE)
+ throw new FJException();
+ return false;
+ }
+
+ protected void onComplete(BinaryAsyncAction x, BinaryAsyncAction y) {
+ completeExceptionally(new FJException());
+ }
+ }
+
+ /**
+ * invoke returns when task completes normally.
+ * isCompletedAbnormally and isCancelled return false for normally
+ * completed tasks; getRawResult returns null.
+ */
+ public void testInvoke() {
+ testInvoke(mainPool());
+ }
+ public void testInvoke_Singleton() {
+ testInvoke(singletonPool());
+ }
+ public void testInvoke(ForkJoinPool pool) {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ AsyncFib f = new AsyncFib(8);
+ assertNull(f.invoke());
+ f.checkCompletedNormally();
+ }};
+ testInvokeOnPool(pool, a);
+ }
+
+ /**
+ * quietlyInvoke task returns when task completes normally.
+ * isCompletedAbnormally and isCancelled return false for normally
+ * completed tasks
+ */
+ public void testQuietlyInvoke() {
+ testQuietlyInvoke(mainPool());
+ }
+ public void testQuietlyInvoke_Singleton() {
+ testQuietlyInvoke(singletonPool());
+ }
+ public void testQuietlyInvoke(ForkJoinPool pool) {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ AsyncFib f = new AsyncFib(8);
+ f.quietlyInvoke();
+ f.checkCompletedNormally();
+ }};
+ testInvokeOnPool(pool, a);
+ }
+
+ /**
+ * join of a forked task returns when task completes
+ */
+ public void testForkJoin() {
+ testForkJoin(mainPool());
+ }
+ public void testForkJoin_Singleton() {
+ testForkJoin(singletonPool());
+ }
+ public void testForkJoin(ForkJoinPool pool) {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ AsyncFib f = new AsyncFib(8);
+ assertSame(f, f.fork());
+ assertNull(f.join());
+ f.checkCompletedNormally();
+ }};
+ testInvokeOnPool(pool, a);
+ }
+
+ /**
+ * get of a forked task returns when task completes
+ */
+ public void testForkGet() {
+ testForkGet(mainPool());
+ }
+ public void testForkGet_Singleton() {
+ testForkGet(singletonPool());
+ }
+ public void testForkGet(ForkJoinPool pool) {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() throws Exception {
+ AsyncFib f = new AsyncFib(8);
+ assertSame(f, f.fork());
+ assertNull(f.get());
+ f.checkCompletedNormally();
+ }};
+ testInvokeOnPool(pool, a);
+ }
+
+ /**
+ * timed get of a forked task returns when task completes
+ */
+ public void testForkTimedGet() {
+ testForkTimedGet(mainPool());
+ }
+ public void testForkTimedGet_Singleton() {
+ testForkTimedGet(singletonPool());
+ }
+ public void testForkTimedGet(ForkJoinPool pool) {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() throws Exception {
+ AsyncFib f = new AsyncFib(8);
+ assertSame(f, f.fork());
+ assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
+ f.checkCompletedNormally();
+ }};
+ testInvokeOnPool(pool, a);
+ }
+
+ /**
+ * timed get with null time unit throws NullPointerException
+ */
+ public void testForkTimedGetNullTimeUnit() {
+ testForkTimedGetNullTimeUnit(mainPool());
+ }
+ public void testForkTimedGetNullTimeUnit_Singleton() {
+ testForkTimedGet(singletonPool());
+ }
+ public void testForkTimedGetNullTimeUnit(ForkJoinPool pool) {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() throws Exception {
+ AsyncFib f = new AsyncFib(8);
+ assertSame(f, f.fork());
+ try {
+ f.get(5L, null);
+ shouldThrow();
+ } catch (NullPointerException success) {}
+ }};
+ testInvokeOnPool(pool, a);
+ }
+
+ /**
+ * quietlyJoin of a forked task returns when task completes
+ */
+ public void testForkQuietlyJoin() {
+ testForkQuietlyJoin(mainPool());
+ }
+ public void testForkQuietlyJoin_Singleton() {
+ testForkQuietlyJoin(singletonPool());
+ }
+ public void testForkQuietlyJoin(ForkJoinPool pool) {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ AsyncFib f = new AsyncFib(8);
+ assertSame(f, f.fork());
+ f.quietlyJoin();
+ f.checkCompletedNormally();
+ }};
+ testInvokeOnPool(pool, a);
+ }
+
+ /**
+ * helpQuiesce returns when tasks are complete.
+ * getQueuedTaskCount returns 0 when quiescent
+ */
+ public void testForkHelpQuiesce() {
+ testForkHelpQuiesce(mainPool());
+ }
+ public void testForkHelpQuiesce_Singleton() {
+ testForkHelpQuiesce(singletonPool());
+ }
+ public void testForkHelpQuiesce(ForkJoinPool pool) {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ AsyncFib f = new AsyncFib(8);
+ assertSame(f, f.fork());
+ helpQuiesce();
+ assertEquals(0, getQueuedTaskCount());
+ f.checkCompletedNormally();
+ }};
+ testInvokeOnPool(pool, a);
+ }
+
+ /**
+ * invoke task throws exception when task completes abnormally
+ */
+ public void testAbnormalInvoke() {
+ testAbnormalInvoke(mainPool());
+ }
+ public void testAbnormalInvoke_Singleton() {
+ testAbnormalInvoke(singletonPool());
+ }
+ public void testAbnormalInvoke(ForkJoinPool pool) {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ FailingAsyncFib f = new FailingAsyncFib(8);
+ try {
+ f.invoke();
+ shouldThrow();
+ } catch (FJException success) {
+ checkCompletedAbnormally(f, success);
+ }
+ }};
+ testInvokeOnPool(pool, a);
+ }
+
+ /**
+ * quietlyInvoke task returns when task completes abnormally
+ */
+ public void testAbnormalQuietlyInvoke() {
+ testAbnormalQuietlyInvoke(mainPool());
+ }
+ public void testAbnormalQuietlyInvoke_Singleton() {
+ testAbnormalQuietlyInvoke(singletonPool());
+ }
+ public void testAbnormalQuietlyInvoke(ForkJoinPool pool) {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ FailingAsyncFib f = new FailingAsyncFib(8);
+ f.quietlyInvoke();
+ assertTrue(f.getException() instanceof FJException);
+ checkCompletedAbnormally(f, f.getException());
+ }};
+ testInvokeOnPool(pool, a);
+ }
+
+ /**
+ * join of a forked task throws exception when task completes abnormally
+ */
+ public void testAbnormalForkJoin() {
+ testAbnormalForkJoin(mainPool());
+ }
+ public void testAbnormalForkJoin_Singleton() {
+ testAbnormalForkJoin(singletonPool());
+ }
+ public void testAbnormalForkJoin(ForkJoinPool pool) {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ FailingAsyncFib f = new FailingAsyncFib(8);
+ assertSame(f, f.fork());
+ try {
+ f.join();
+ shouldThrow();
+ } catch (FJException success) {
+ checkCompletedAbnormally(f, success);
+ }
+ }};
+ testInvokeOnPool(pool, a);
+ }
+
+ /**
+ * get of a forked task throws exception when task completes abnormally
+ */
+ public void testAbnormalForkGet() {
+ testAbnormalForkGet(mainPool());
+ }
+ public void testAbnormalForkGet_Singleton() {
+ testAbnormalForkJoin(singletonPool());
+ }
+ public void testAbnormalForkGet(ForkJoinPool pool) {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() throws Exception {
+ FailingAsyncFib f = new FailingAsyncFib(8);
+ assertSame(f, f.fork());
+ try {
+ f.get();
+ shouldThrow();
+ } catch (ExecutionException success) {
+ Throwable cause = success.getCause();
+ assertTrue(cause instanceof FJException);
+ checkCompletedAbnormally(f, cause);
+ }
+ }};
+ testInvokeOnPool(pool, a);
+ }
+
+ /**
+ * timed get of a forked task throws exception when task completes abnormally
+ */
+ public void testAbnormalForkTimedGet() {
+ testAbnormalForkTimedGet(mainPool());
+ }
+ public void testAbnormalForkTimedGet_Singleton() {
+ testAbnormalForkTimedGet(singletonPool());
+ }
+ public void testAbnormalForkTimedGet(ForkJoinPool pool) {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() throws Exception {
+ FailingAsyncFib f = new FailingAsyncFib(8);
+ assertSame(f, f.fork());
+ try {
+ f.get(LONG_DELAY_MS, MILLISECONDS);
+ shouldThrow();
+ } catch (ExecutionException success) {
+ Throwable cause = success.getCause();
+ assertTrue(cause instanceof FJException);
+ checkCompletedAbnormally(f, cause);
+ }
+ }};
+ testInvokeOnPool(pool, a);
+ }
+
+ /**
+ * quietlyJoin of a forked task returns when task completes abnormally
+ */
+ public void testAbnormalForkQuietlyJoin() {
+ testAbnormalForkQuietlyJoin(mainPool());
+ }
+ public void testAbnormalForkQuietlyJoin_Singleton() {
+ testAbnormalForkQuietlyJoin(singletonPool());
+ }
+ public void testAbnormalForkQuietlyJoin(ForkJoinPool pool) {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ FailingAsyncFib f = new FailingAsyncFib(8);
+ assertSame(f, f.fork());
+ f.quietlyJoin();
+ assertTrue(f.getException() instanceof FJException);
+ checkCompletedAbnormally(f, f.getException());
+ }};
+ testInvokeOnPool(pool, a);
+ }
+
+ /**
+ * getPool of executing task returns its pool
+ */
+ public void testGetPool() {
+ testGetPool(mainPool());
+ }
+ public void testGetPool_Singleton() {
+ testGetPool(singletonPool());
+ }
+ public void testGetPool(ForkJoinPool pool) {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ assertSame(pool, getPool());
+ }};
+ testInvokeOnPool(pool, a);
+ }
+
+ /**
+ * getPool of non-FJ task returns null
+ */
+ public void testGetPool2() {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ assertNull(getPool());
+ }};
+ assertNull(a.invoke());
+ }
+
+ /**
+ * inForkJoinPool of executing task returns true
+ */
+ public void testInForkJoinPool() {
+ testInForkJoinPool(mainPool());
+ }
+ public void testInForkJoinPool_Singleton() {
+ testInForkJoinPool(singletonPool());
+ }
+ public void testInForkJoinPool(ForkJoinPool pool) {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ assertTrue(inForkJoinPool());
+ }};
+ testInvokeOnPool(pool, a);
+ }
+
+ /**
+ * inForkJoinPool of non-FJ task returns false
+ */
+ public void testInForkJoinPool2() {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ assertFalse(inForkJoinPool());
+ }};
+ assertNull(a.invoke());
+ }
+
+ /**
+ * setRawResult(null) succeeds
+ */
+ public void testSetRawResult() {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ setRawResult(null);
+ assertNull(getRawResult());
+ }};
+ assertNull(a.invoke());
+ }
+
+ /**
+ * invoke task throws exception after invoking completeExceptionally
+ */
+ public void testCompleteExceptionally() {
+ testCompleteExceptionally(mainPool());
+ }
+ public void testCompleteExceptionally_Singleton() {
+ testCompleteExceptionally(singletonPool());
+ }
+ public void testCompleteExceptionally(ForkJoinPool pool) {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ AsyncFib f = new AsyncFib(8);
+ f.completeExceptionally(new FJException());
+ try {
+ f.invoke();
+ shouldThrow();
+ } catch (FJException success) {
+ checkCompletedAbnormally(f, success);
+ }
+ }};
+ testInvokeOnPool(pool, a);
+ }
+
+ /**
+ * invokeAll(tasks) with 1 argument invokes task
+ */
+ public void testInvokeAll1() {
+ testInvokeAll1(mainPool());
+ }
+ public void testInvokeAll1_Singleton() {
+ testInvokeAll1(singletonPool());
+ }
+ public void testInvokeAll1(ForkJoinPool pool) {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ AsyncFib f = new AsyncFib(8);
+ invokeAll(f);
+ f.checkCompletedNormally();
+ }};
+ testInvokeOnPool(pool, a);
+ }
+
+ /**
+ * invokeAll(t1, t2) invokes all task arguments
+ */
+ public void testInvokeAll2() {
+ testInvokeAll2(mainPool());
+ }
+ public void testInvokeAll2_Singleton() {
+ testInvokeAll2(singletonPool());
+ }
+ public void testInvokeAll2(ForkJoinPool pool) {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ AsyncFib[] tasks = {
+ new AsyncFib(8),
+ new AsyncFib(9),
+ };
+ invokeAll(tasks[0], tasks[1]);
+ for (AsyncFib task : tasks) assertTrue(task.isDone());
+ for (AsyncFib task : tasks) task.checkCompletedNormally();
+ }};
+ testInvokeOnPool(pool, a);
+ }
+
+ /**
+ * invokeAll(tasks) with > 2 argument invokes tasks
+ */
+ public void testInvokeAll3() {
+ testInvokeAll3(mainPool());
+ }
+ public void testInvokeAll3_Singleton() {
+ testInvokeAll3(singletonPool());
+ }
+ public void testInvokeAll3(ForkJoinPool pool) {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ AsyncFib[] tasks = {
+ new AsyncFib(8),
+ new AsyncFib(9),
+ new AsyncFib(7),
+ };
+ invokeAll(tasks[0], tasks[1], tasks[2]);
+ for (AsyncFib task : tasks) assertTrue(task.isDone());
+ for (AsyncFib task : tasks) task.checkCompletedNormally();
+ }};
+ testInvokeOnPool(pool, a);
+ }
+
+ /**
+ * invokeAll(collection) invokes all tasks in the collection
+ */
+ public void testInvokeAllCollection() {
+ testInvokeAllCollection(mainPool());
+ }
+ public void testInvokeAllCollection_Singleton() {
+ testInvokeAllCollection(singletonPool());
+ }
+ public void testInvokeAllCollection(ForkJoinPool pool) {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ AsyncFib[] tasks = {
+ new AsyncFib(8),
+ new AsyncFib(9),
+ new AsyncFib(7),
+ };
+ invokeAll(Arrays.asList(tasks));
+ for (AsyncFib task : tasks) assertTrue(task.isDone());
+ for (AsyncFib task : tasks) task.checkCompletedNormally();
+ }};
+ testInvokeOnPool(pool, a);
+ }
+
+ /**
+ * invokeAll(tasks) with any null task throws NullPointerException
+ */
+ public void testInvokeAllNullTask() {
+ testInvokeAllNullTask(mainPool());
+ }
+ public void testInvokeAllNullTask_Singleton() {
+ testInvokeAllNullTask(singletonPool());
+ }
+ public void testInvokeAllNullTask(ForkJoinPool pool) {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ AsyncFib nul = null;
+ Runnable[] throwingActions = {
+ () -> invokeAll(nul),
+ () -> invokeAll(nul, nul),
+ () -> invokeAll(new AsyncFib(8), new AsyncFib(9), nul),
+ () -> invokeAll(new AsyncFib(8), nul, new AsyncFib(9)),
+ () -> invokeAll(nul, new AsyncFib(8), new AsyncFib(9)),
+ };
+ assertThrows(NullPointerException.class, throwingActions);
+ }};
+ testInvokeOnPool(pool, a);
+ }
+
+ /**
+ * invokeAll(tasks) with 1 argument throws exception if task does
+ */
+ public void testAbnormalInvokeAll1() {
+ testAbnormalInvokeAll1(mainPool());
+ }
+ public void testAbnormalInvokeAll1_Singleton() {
+ testAbnormalInvokeAll1(singletonPool());
+ }
+ public void testAbnormalInvokeAll1(ForkJoinPool pool) {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ FailingAsyncFib g = new FailingAsyncFib(9);
+ try {
+ invokeAll(g);
+ shouldThrow();
+ } catch (FJException success) {
+ checkCompletedAbnormally(g, success);
+ }
+ }};
+ testInvokeOnPool(pool, a);
+ }
+
+ /**
+ * invokeAll(t1, t2) throw exception if any task does
+ */
+ public void testAbnormalInvokeAll2() {
+ testAbnormalInvokeAll2(mainPool());
+ }
+ public void testAbnormalInvokeAll2_Singleton() {
+ testAbnormalInvokeAll2(singletonPool());
+ }
+ public void testAbnormalInvokeAll2(ForkJoinPool pool) {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ AsyncFib f = new AsyncFib(8);
+ FailingAsyncFib g = new FailingAsyncFib(9);
+ ForkJoinTask[] tasks = { f, g };
+ Collections.shuffle(Arrays.asList(tasks));
+ try {
+ invokeAll(tasks[0], tasks[1]);
+ shouldThrow();
+ } catch (FJException success) {
+ checkCompletedAbnormally(g, success);
+ }
+ }};
+ testInvokeOnPool(pool, a);
+ }
+
+ /**
+ * invokeAll(tasks) with > 2 argument throws exception if any task does
+ */
+ public void testAbnormalInvokeAll3() {
+ testAbnormalInvokeAll3(mainPool());
+ }
+ public void testAbnormalInvokeAll3_Singleton() {
+ testAbnormalInvokeAll3(singletonPool());
+ }
+ public void testAbnormalInvokeAll3(ForkJoinPool pool) {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ AsyncFib f = new AsyncFib(8);
+ FailingAsyncFib g = new FailingAsyncFib(9);
+ AsyncFib h = new AsyncFib(7);
+ ForkJoinTask[] tasks = { f, g, h };
+ Collections.shuffle(Arrays.asList(tasks));
+ try {
+ invokeAll(tasks[0], tasks[1], tasks[2]);
+ shouldThrow();
+ } catch (FJException success) {
+ checkCompletedAbnormally(g, success);
+ }
+ }};
+ testInvokeOnPool(pool, a);
+ }
+
+ /**
+ * invokeAll(collection) throws exception if any task does
+ */
+ public void testAbnormalInvokeAllCollection() {
+ testAbnormalInvokeAllCollection(mainPool());
+ }
+ public void testAbnormalInvokeAllCollection_Singleton() {
+ testAbnormalInvokeAllCollection(singletonPool());
+ }
+ public void testAbnormalInvokeAllCollection(ForkJoinPool pool) {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ FailingAsyncFib f = new FailingAsyncFib(8);
+ AsyncFib g = new AsyncFib(9);
+ AsyncFib h = new AsyncFib(7);
+ ForkJoinTask[] tasks = { f, g, h };
+ Collections.shuffle(Arrays.asList(tasks));
+ try {
+ invokeAll(Arrays.asList(tasks));
+ shouldThrow();
+ } catch (FJException success) {
+ checkCompletedAbnormally(f, success);
+ }
+ }};
+ testInvokeOnPool(pool, a);
+ }
+
+ /**
+ * tryUnfork returns true for most recent unexecuted task,
+ * and suppresses execution
+ */
+ public void testTryUnfork() {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ AsyncFib g = new AsyncFib(9);
+ assertSame(g, g.fork());
+ AsyncFib f = new AsyncFib(8);
+ assertSame(f, f.fork());
+ assertTrue(f.tryUnfork());
+ helpQuiesce();
+ checkNotDone(f);
+ g.checkCompletedNormally();
+ }};
+ testInvokeOnPool(singletonPool(), a);
+ }
+
+ /**
+ * getSurplusQueuedTaskCount returns > 0 when
+ * there are more tasks than threads
+ */
+ public void testGetSurplusQueuedTaskCount() {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ AsyncFib h = new AsyncFib(7);
+ assertSame(h, h.fork());
+ AsyncFib g = new AsyncFib(9);
+ assertSame(g, g.fork());
+ AsyncFib f = new AsyncFib(8);
+ assertSame(f, f.fork());
+ assertTrue(getSurplusQueuedTaskCount() > 0);
+ helpQuiesce();
+ assertEquals(0, getSurplusQueuedTaskCount());
+ f.checkCompletedNormally();
+ g.checkCompletedNormally();
+ h.checkCompletedNormally();
+ }};
+ testInvokeOnPool(singletonPool(), a);
+ }
+
+ /**
+ * peekNextLocalTask returns most recent unexecuted task.
+ */
+ public void testPeekNextLocalTask() {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ AsyncFib g = new AsyncFib(9);
+ assertSame(g, g.fork());
+ AsyncFib f = new AsyncFib(8);
+ assertSame(f, f.fork());
+ assertSame(f, peekNextLocalTask());
+ assertNull(f.join());
+ f.checkCompletedNormally();
+ helpQuiesce();
+ g.checkCompletedNormally();
+ }};
+ testInvokeOnPool(singletonPool(), a);
+ }
+
+ /**
+ * pollNextLocalTask returns most recent unexecuted task without
+ * executing it
+ */
+ public void testPollNextLocalTask() {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ AsyncFib g = new AsyncFib(9);
+ assertSame(g, g.fork());
+ AsyncFib f = new AsyncFib(8);
+ assertSame(f, f.fork());
+ assertSame(f, pollNextLocalTask());
+ helpQuiesce();
+ checkNotDone(f);
+ g.checkCompletedNormally();
+ }};
+ testInvokeOnPool(singletonPool(), a);
+ }
+
+ /**
+ * pollTask returns an unexecuted task without executing it
+ */
+ public void testPollTask() {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ AsyncFib g = new AsyncFib(9);
+ assertSame(g, g.fork());
+ AsyncFib f = new AsyncFib(8);
+ assertSame(f, f.fork());
+ assertSame(f, pollTask());
+ helpQuiesce();
+ checkNotDone(f);
+ g.checkCompletedNormally();
+ }};
+ testInvokeOnPool(singletonPool(), a);
+ }
+
+ /**
+ * peekNextLocalTask returns least recent unexecuted task in async mode
+ */
+ public void testPeekNextLocalTaskAsync() {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ AsyncFib g = new AsyncFib(9);
+ assertSame(g, g.fork());
+ AsyncFib f = new AsyncFib(8);
+ assertSame(f, f.fork());
+ assertSame(g, peekNextLocalTask());
+ assertNull(f.join());
+ helpQuiesce();
+ f.checkCompletedNormally();
+ g.checkCompletedNormally();
+ }};
+ testInvokeOnPool(asyncSingletonPool(), a);
+ }
+
+ /**
+ * pollNextLocalTask returns least recent unexecuted task without
+ * executing it, in async mode
+ */
+ public void testPollNextLocalTaskAsync() {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ AsyncFib g = new AsyncFib(9);
+ assertSame(g, g.fork());
+ AsyncFib f = new AsyncFib(8);
+ assertSame(f, f.fork());
+ assertSame(g, pollNextLocalTask());
+ helpQuiesce();
+ f.checkCompletedNormally();
+ checkNotDone(g);
+ }};
+ testInvokeOnPool(asyncSingletonPool(), a);
+ }
+
+ /**
+ * pollTask returns an unexecuted task without executing it, in
+ * async mode
+ */
+ public void testPollTaskAsync() {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ AsyncFib g = new AsyncFib(9);
+ assertSame(g, g.fork());
+ AsyncFib f = new AsyncFib(8);
+ assertSame(f, f.fork());
+ assertSame(g, pollTask());
+ helpQuiesce();
+ f.checkCompletedNormally();
+ checkNotDone(g);
+ }};
+ testInvokeOnPool(asyncSingletonPool(), a);
+ }
+
+ /**
+ * ForkJoinTask.quietlyComplete returns when task completes
+ * normally without setting a value. The most recent value
+ * established by setRawResult(V) (or null by default) is returned
+ * from invoke.
+ */
+ public void testQuietlyComplete() {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ AsyncFib f = new AsyncFib(8);
+ f.quietlyComplete();
+ assertEquals(8, f.number);
+ assertTrue(f.isDone());
+ assertFalse(f.isCancelled());
+ assertTrue(f.isCompletedNormally());
+ assertFalse(f.isCompletedAbnormally());
+ assertNull(f.getException());
+ }};
+ testInvokeOnPool(mainPool(), a);
+ }
+
+ // jdk9
+
+ /**
+ * pollSubmission returns unexecuted submitted task, if present
+ */
+ public void testPollSubmission() {
+ final CountDownLatch done = new CountDownLatch(1);
+ final ForkJoinTask a = ForkJoinTask.adapt(awaiter(done));
+ final ForkJoinTask b = ForkJoinTask.adapt(awaiter(done));
+ final ForkJoinTask c = ForkJoinTask.adapt(awaiter(done));
+ final ForkJoinPool p = singletonPool();
+ try (PoolCleaner cleaner = cleaner(p, done)) {
+ Thread external = new Thread(new CheckedRunnable() {
+ public void realRun() {
+ p.execute(a);
+ p.execute(b);
+ p.execute(c);
+ }});
+ RecursiveAction s = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ external.start();
+ try {
+ external.join();
+ } catch (Exception ex) {
+ threadUnexpectedException(ex);
+ }
+ assertTrue(p.hasQueuedSubmissions());
+ assertTrue(Thread.currentThread() instanceof ForkJoinWorkerThread);
+ ForkJoinTask r = ForkJoinTask.pollSubmission();
+ assertTrue(r == a || r == b || r == c);
+ assertFalse(r.isDone());
+ }};
+ p.invoke(s);
+ }
+ }
+
+}
diff --git a/jsr166-tests/src/test/java/jsr166/ForkJoinTaskTest.java b/jsr166-tests/src/test/java/jsr166/ForkJoinTaskTest.java
index 3c1fcb7..1616d4f 100644
--- a/jsr166-tests/src/test/java/jsr166/ForkJoinTaskTest.java
+++ b/jsr166-tests/src/test/java/jsr166/ForkJoinTaskTest.java
@@ -9,7 +9,10 @@
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;
+import java.util.Arrays;
+import java.util.Collections;
import java.util.HashSet;
+import java.util.List;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
@@ -30,7 +33,7 @@
// main(suite(), args);
// }
// public static Test suite() {
- // return new TestSuite(...);
+ // return new TestSuite(ForkJoinTaskTest.class);
// }
// Runs with "mainPool" use > 1 thread. singletonPool tests use 1
@@ -52,7 +55,7 @@
}
private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) {
- try {
+ try (PoolCleaner cleaner = cleaner(pool)) {
assertFalse(a.isDone());
assertFalse(a.isCompletedNormally());
assertFalse(a.isCompletedAbnormally());
@@ -68,8 +71,6 @@
assertFalse(a.isCancelled());
assertNull(a.getException());
assertNull(a.getRawResult());
- } finally {
- joinPool(pool);
}
}
@@ -102,17 +103,17 @@
{
Thread.currentThread().interrupt();
- long t0 = System.nanoTime();
+ long startTime = System.nanoTime();
assertSame(expected, a.join());
- assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
+ assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
Thread.interrupted();
}
{
Thread.currentThread().interrupt();
- long t0 = System.nanoTime();
+ long startTime = System.nanoTime();
a.quietlyJoin(); // should be no-op
- assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
+ assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
Thread.interrupted();
}
@@ -145,9 +146,9 @@
Thread.interrupted();
{
- long t0 = System.nanoTime();
+ long startTime = System.nanoTime();
a.quietlyJoin(); // should be no-op
- assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
+ assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
}
try {
@@ -183,9 +184,9 @@
Thread.interrupted();
{
- long t0 = System.nanoTime();
+ long startTime = System.nanoTime();
a.quietlyJoin(); // should be no-op
- assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
+ assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
}
try {
@@ -222,9 +223,9 @@
AtomicIntegerFieldUpdater.newUpdater(BinaryAsyncAction.class,
"controlState");
- private BinaryAsyncAction parent;
+ private volatile BinaryAsyncAction parent;
- private BinaryAsyncAction sibling;
+ private volatile BinaryAsyncAction sibling;
protected BinaryAsyncAction() {
}
@@ -259,6 +260,14 @@
super.completeExceptionally(ex);
}
+ public boolean cancel(boolean mayInterruptIfRunning) {
+ if (super.cancel(mayInterruptIfRunning)) {
+ completeExceptionally(new FJException());
+ return true;
+ }
+ return false;
+ }
+
public final void complete() {
BinaryAsyncAction a = this;
for (;;) {
@@ -280,13 +289,12 @@
}
public final void completeExceptionally(Throwable ex) {
- BinaryAsyncAction a = this;
- while (!a.isCompletedAbnormally()) {
+ for (BinaryAsyncAction a = this;;) {
a.completeThisExceptionally(ex);
BinaryAsyncAction s = a.sibling;
- if (s != null)
- s.cancel(false);
- if (!a.onException() || (a = a.parent) == null)
+ if (s != null && !s.isDone())
+ s.completeExceptionally(ex);
+ if ((a = a.parent) == null)
break;
}
}
@@ -336,15 +344,12 @@
public final boolean exec() {
AsyncFib f = this;
int n = f.number;
- if (n > 1) {
- while (n > 1) {
- AsyncFib p = f;
- AsyncFib r = new AsyncFib(n - 2);
- f = new AsyncFib(--n);
- p.linkSubtasks(r, f);
- r.fork();
- }
- f.number = n;
+ while (n > 1) {
+ AsyncFib p = f;
+ AsyncFib r = new AsyncFib(n - 2);
+ f = new AsyncFib(--n);
+ p.linkSubtasks(r, f);
+ r.fork();
}
f.complete();
return false;
@@ -364,15 +369,12 @@
public final boolean exec() {
FailingAsyncFib f = this;
int n = f.number;
- if (n > 1) {
- while (n > 1) {
- FailingAsyncFib p = f;
- FailingAsyncFib r = new FailingAsyncFib(n - 2);
- f = new FailingAsyncFib(--n);
- p.linkSubtasks(r, f);
- r.fork();
- }
- f.number = n;
+ while (n > 1) {
+ FailingAsyncFib p = f;
+ FailingAsyncFib r = new FailingAsyncFib(n - 2);
+ f = new FailingAsyncFib(--n);
+ p.linkSubtasks(r, f);
+ r.fork();
}
f.complete();
return false;
@@ -778,6 +780,27 @@
}
/**
+ * completeExceptionally(null) surprisingly has the same effect as
+ * completeExceptionally(new RuntimeException())
+ */
+ public void testCompleteExceptionally_null() {
+ RecursiveAction a = new CheckedRecursiveAction() {
+ protected void realCompute() {
+ AsyncFib f = new AsyncFib(8);
+ f.completeExceptionally(null);
+ try {
+ f.invoke();
+ shouldThrow();
+ } catch (RuntimeException success) {
+ assertSame(success.getClass(), RuntimeException.class);
+ assertNull(success.getCause());
+ checkCompletedAbnormally(f, success);
+ }
+ }};
+ testInvokeOnPool(mainPool(), a);
+ }
+
+ /**
* invokeAll(t1, t2) invokes all task arguments
*/
public void testInvokeAll2() {
@@ -877,8 +900,10 @@
protected void realCompute() {
AsyncFib f = new AsyncFib(8);
FailingAsyncFib g = new FailingAsyncFib(9);
+ ForkJoinTask[] tasks = { f, g };
+ Collections.shuffle(Arrays.asList(tasks));
try {
- invokeAll(f, g);
+ invokeAll(tasks);
shouldThrow();
} catch (FJException success) {
checkCompletedAbnormally(g, success);
@@ -913,8 +938,10 @@
AsyncFib f = new AsyncFib(8);
FailingAsyncFib g = new FailingAsyncFib(9);
AsyncFib h = new AsyncFib(7);
+ ForkJoinTask[] tasks = { f, g, h };
+ Collections.shuffle(Arrays.asList(tasks));
try {
- invokeAll(f, g, h);
+ invokeAll(tasks);
shouldThrow();
} catch (FJException success) {
checkCompletedAbnormally(g, success);
@@ -932,12 +959,11 @@
FailingAsyncFib f = new FailingAsyncFib(8);
AsyncFib g = new AsyncFib(9);
AsyncFib h = new AsyncFib(7);
- HashSet set = new HashSet();
- set.add(f);
- set.add(g);
- set.add(h);
+ ForkJoinTask[] tasks = { f, g, h };
+ List taskList = Arrays.asList(tasks);
+ Collections.shuffle(taskList);
try {
- invokeAll(set);
+ invokeAll(taskList);
shouldThrow();
} catch (FJException success) {
checkCompletedAbnormally(f, success);
@@ -1544,8 +1570,10 @@
protected void realCompute() {
AsyncFib f = new AsyncFib(8);
FailingAsyncFib g = new FailingAsyncFib(9);
+ ForkJoinTask[] tasks = { f, g };
+ Collections.shuffle(Arrays.asList(tasks));
try {
- invokeAll(f, g);
+ invokeAll(tasks);
shouldThrow();
} catch (FJException success) {
checkCompletedAbnormally(g, success);
@@ -1580,8 +1608,10 @@
AsyncFib f = new AsyncFib(8);
FailingAsyncFib g = new FailingAsyncFib(9);
AsyncFib h = new AsyncFib(7);
+ ForkJoinTask[] tasks = { f, g, h };
+ Collections.shuffle(Arrays.asList(tasks));
try {
- invokeAll(f, g, h);
+ invokeAll(tasks);
shouldThrow();
} catch (FJException success) {
checkCompletedAbnormally(g, success);
@@ -1599,12 +1629,11 @@
FailingAsyncFib f = new FailingAsyncFib(8);
AsyncFib g = new AsyncFib(9);
AsyncFib h = new AsyncFib(7);
- HashSet set = new HashSet();
- set.add(f);
- set.add(g);
- set.add(h);
+ ForkJoinTask[] tasks = { f, g, h };
+ List taskList = Arrays.asList(tasks);
+ Collections.shuffle(taskList);
try {
- invokeAll(set);
+ invokeAll(taskList);
shouldThrow();
} catch (FJException success) {
checkCompletedAbnormally(f, success);
diff --git a/jsr166-tests/src/test/java/jsr166/FutureTaskTest.java b/jsr166-tests/src/test/java/jsr166/FutureTaskTest.java
index a5d8c46..44d12b3 100644
--- a/jsr166-tests/src/test/java/jsr166/FutureTaskTest.java
+++ b/jsr166-tests/src/test/java/jsr166/FutureTaskTest.java
@@ -38,7 +38,7 @@
// main(suite(), args);
// }
// public static Test suite() {
- // return new TestSuite(...);
+ // return new TestSuite(FutureTaskTest.class);
// }
void checkIsDone(Future<?> f) {
@@ -272,8 +272,8 @@
for (int i = 0; i < 3; i++) {
assertTrue(task.runAndReset());
checkNotDone(task);
- assertEquals(i+1, task.runCount());
- assertEquals(i+1, task.runAndResetCount());
+ assertEquals(i + 1, task.runCount());
+ assertEquals(i + 1, task.runAndResetCount());
assertEquals(0, task.setCount());
assertEquals(0, task.setExceptionCount());
}
@@ -289,7 +289,7 @@
for (int i = 0; i < 3; i++) {
assertFalse(task.runAndReset());
assertEquals(0, task.runCount());
- assertEquals(i+1, task.runAndResetCount());
+ assertEquals(i + 1, task.runAndResetCount());
assertEquals(0, task.setCount());
assertEquals(0, task.setExceptionCount());
}
diff --git a/jsr166-tests/src/test/java/jsr166/JSR166TestCase.java b/jsr166-tests/src/test/java/jsr166/JSR166TestCase.java
index 46be906..fc1632c 100644
--- a/jsr166-tests/src/test/java/jsr166/JSR166TestCase.java
+++ b/jsr166-tests/src/test/java/jsr166/JSR166TestCase.java
@@ -6,17 +6,21 @@
* Pat Fisher, Mike Judd.
*/
+
package jsr166;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
+import static java.util.concurrent.TimeUnit.MINUTES;
import static java.util.concurrent.TimeUnit.NANOSECONDS;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
+import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
-import java.security.CodeSource;
+import java.lang.reflect.Modifier;
+ import java.security.CodeSource;
import java.security.Permission;
import java.security.PermissionCollection;
import java.security.Permissions;
@@ -35,7 +39,10 @@
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
+import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.Future;
import java.util.concurrent.RecursiveAction;
import java.util.concurrent.RecursiveTask;
@@ -44,12 +51,15 @@
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeoutException;
+import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
+import java.util.regex.Matcher;
import java.util.regex.Pattern;
import junit.framework.AssertionFailedError;
import junit.framework.Test;
import junit.framework.TestCase;
+import junit.framework.TestResult;
import junit.framework.TestSuite;
/**
@@ -62,18 +72,18 @@
*
* <ol>
*
- * <li> All assertions in code running in generated threads must use
+ * <li>All assertions in code running in generated threads must use
* the forms {@link #threadFail}, {@link #threadAssertTrue}, {@link
* #threadAssertEquals}, or {@link #threadAssertNull}, (not
* {@code fail}, {@code assertTrue}, etc.) It is OK (but not
* particularly recommended) for other code to use these forms too.
* Only the most typically used JUnit assertion methods are defined
- * this way, but enough to live with.</li>
+ * this way, but enough to live with.
*
- * <li> If you override {@link #setUp} or {@link #tearDown}, make sure
+ * <li>If you override {@link #setUp} or {@link #tearDown}, make sure
* to invoke {@code super.setUp} and {@code super.tearDown} within
* them. These methods are used to clear and check for thread
- * assertion failures.</li>
+ * assertion failures.
*
* <li>All delays and timeouts must use one of the constants {@code
* SHORT_DELAY_MS}, {@code SMALL_DELAY_MS}, {@code MEDIUM_DELAY_MS},
@@ -84,51 +94,480 @@
* is always discriminable as larger than SHORT and smaller than
* MEDIUM. And so on. These constants are set to conservative values,
* but even so, if there is ever any doubt, they can all be increased
- * in one spot to rerun tests on slower platforms.</li>
+ * in one spot to rerun tests on slower platforms.
*
- * <li> All threads generated must be joined inside each test case
+ * <li>All threads generated must be joined inside each test case
* method (or {@code fail} to do so) before returning from the
* method. The {@code joinPool} method can be used to do this when
- * using Executors.</li>
+ * using Executors.
*
* </ol>
*
* <p><b>Other notes</b>
* <ul>
*
- * <li> Usually, there is one testcase method per JSR166 method
+ * <li>Usually, there is one testcase method per JSR166 method
* covering "normal" operation, and then as many exception-testing
* methods as there are exceptions the method can throw. Sometimes
* there are multiple tests per JSR166 method when the different
* "normal" behaviors differ significantly. And sometimes testcases
- * cover multiple methods when they cannot be tested in
- * isolation.</li>
+ * cover multiple methods when they cannot be tested in isolation.
*
- * <li> The documentation style for testcases is to provide as javadoc
+ * <li>The documentation style for testcases is to provide as javadoc
* a simple sentence or two describing the property that the testcase
* method purports to test. The javadocs do not say anything about how
- * the property is tested. To find out, read the code.</li>
+ * the property is tested. To find out, read the code.
*
- * <li> These tests are "conformance tests", and do not attempt to
+ * <li>These tests are "conformance tests", and do not attempt to
* test throughput, latency, scalability or other performance factors
* (see the separate "jtreg" tests for a set intended to check these
* for the most central aspects of functionality.) So, most tests use
* the smallest sensible numbers of threads, collection sizes, etc
- * needed to check basic conformance.</li>
+ * needed to check basic conformance.
*
* <li>The test classes currently do not declare inclusion in
* any particular package to simplify things for people integrating
- * them in TCK test suites.</li>
+ * them in TCK test suites.
*
- * <li> As a convenience, the {@code main} of this class (JSR166TestCase)
- * runs all JSR166 unit tests.</li>
+ * <li>As a convenience, the {@code main} of this class (JSR166TestCase)
+ * runs all JSR166 unit tests.
*
* </ul>
*/
public class JSR166TestCase extends TestCase {
- // Delays for timing-dependent tests, in milliseconds.
+ private static final boolean useSecurityManager =
+ Boolean.getBoolean("jsr166.useSecurityManager");
- protected static final boolean expensiveTests = false;
+ protected static final boolean expensiveTests =
+ Boolean.getBoolean("jsr166.expensiveTests");
+
+ /**
+ * If true, also run tests that are not part of the official tck
+ * because they test unspecified implementation details.
+ */
+ protected static final boolean testImplementationDetails =
+ Boolean.getBoolean("jsr166.testImplementationDetails");
+
+ /**
+ * If true, report on stdout all "slow" tests, that is, ones that
+ * take more than profileThreshold milliseconds to execute.
+ */
+ private static final boolean profileTests =
+ Boolean.getBoolean("jsr166.profileTests");
+
+ /**
+ * The number of milliseconds that tests are permitted for
+ * execution without being reported, when profileTests is set.
+ */
+ private static final long profileThreshold =
+ Long.getLong("jsr166.profileThreshold", 100);
+
+ /**
+ * The number of repetitions per test (for tickling rare bugs).
+ */
+ private static final int runsPerTest =
+ Integer.getInteger("jsr166.runsPerTest", 1);
+
+ /**
+ * The number of repetitions of the test suite (for finding leaks?).
+ */
+ private static final int suiteRuns =
+ Integer.getInteger("jsr166.suiteRuns", 1);
+
+ private static float systemPropertyValue(String name, float defaultValue) {
+ String floatString = System.getProperty(name);
+ if (floatString == null)
+ return defaultValue;
+ try {
+ return Float.parseFloat(floatString);
+ } catch (NumberFormatException ex) {
+ throw new IllegalArgumentException(
+ String.format("Bad float value in system property %s=%s",
+ name, floatString));
+ }
+ }
+
+ /**
+ * The scaling factor to apply to standard delays used in tests.
+ */
+ private static final float delayFactor =
+ systemPropertyValue("jsr166.delay.factor", 1.0f);
+
+ /**
+ * The timeout factor as used in the jtreg test harness.
+ * See: http://openjdk.java.net/jtreg/tag-spec.html
+ */
+ private static final float jtregTestTimeoutFactor
+ = systemPropertyValue("test.timeout.factor", 1.0f);
+
+ public JSR166TestCase() { super(); }
+ public JSR166TestCase(String name) { super(name); }
+
+ /**
+ * A filter for tests to run, matching strings of the form
+ * methodName(className), e.g. "testInvokeAll5(ForkJoinPoolTest)"
+ * Usefully combined with jsr166.runsPerTest.
+ */
+ private static final Pattern methodFilter = methodFilter();
+
+ private static Pattern methodFilter() {
+ String regex = System.getProperty("jsr166.methodFilter");
+ return (regex == null) ? null : Pattern.compile(regex);
+ }
+
+ // Instrumentation to debug very rare, but very annoying hung test runs.
+ static volatile TestCase currentTestCase;
+ // static volatile int currentRun = 0;
+ static {
+ Runnable checkForWedgedTest = new Runnable() { public void run() {
+ // Avoid spurious reports with enormous runsPerTest.
+ // A single test case run should never take more than 1 second.
+ // But let's cap it at the high end too ...
+ final int timeoutMinutes =
+ Math.min(15, Math.max(runsPerTest / 60, 1));
+ for (TestCase lastTestCase = currentTestCase;;) {
+ try { MINUTES.sleep(timeoutMinutes); }
+ catch (InterruptedException unexpected) { break; }
+ if (lastTestCase == currentTestCase) {
+ System.err.printf(
+ "Looks like we're stuck running test: %s%n",
+ lastTestCase);
+// System.err.printf(
+// "Looks like we're stuck running test: %s (%d/%d)%n",
+// lastTestCase, currentRun, runsPerTest);
+// System.err.println("availableProcessors=" +
+// Runtime.getRuntime().availableProcessors());
+// System.err.printf("cpu model = %s%n", cpuModel());
+ dumpTestThreads();
+ // one stack dump is probably enough; more would be spam
+ break;
+ }
+ lastTestCase = currentTestCase;
+ }}};
+ Thread thread = new Thread(checkForWedgedTest, "checkForWedgedTest");
+ thread.setDaemon(true);
+ thread.start();
+ }
+
+// public static String cpuModel() {
+// try {
+// Matcher matcher = Pattern.compile("model name\\s*: (.*)")
+// .matcher(new String(
+// Files.readAllBytes(Paths.get("/proc/cpuinfo")), "UTF-8"));
+// matcher.find();
+// return matcher.group(1);
+// } catch (Exception ex) { return null; }
+// }
+
+ public void runBare() throws Throwable {
+ currentTestCase = this;
+ if (methodFilter == null
+ || methodFilter.matcher(toString()).find())
+ super.runBare();
+ }
+
+ protected void runTest() throws Throwable {
+ for (int i = 0; i < runsPerTest; i++) {
+ // currentRun = i;
+ if (profileTests)
+ runTestProfiled();
+ else
+ super.runTest();
+ }
+ }
+
+ protected void runTestProfiled() throws Throwable {
+ for (int i = 0; i < 2; i++) {
+ long startTime = System.nanoTime();
+ super.runTest();
+ long elapsedMillis = millisElapsedSince(startTime);
+ if (elapsedMillis < profileThreshold)
+ break;
+ // Never report first run of any test; treat it as a
+ // warmup run, notably to trigger all needed classloading,
+ if (i > 0)
+ System.out.printf("%n%s: %d%n", toString(), elapsedMillis);
+ }
+ }
+
+ /**
+ * Runs all JSR166 unit tests using junit.textui.TestRunner.
+ */
+ // android-note: Removed because no junit.textui
+ // public static void main(String[] args) {
+ // main(suite(), args);
+ // }
+
+ // static class PithyResultPrinter extends junit.textui.ResultPrinter {
+ // PithyResultPrinter(java.io.PrintStream writer) { super(writer); }
+ // long runTime;
+ // public void startTest(Test test) {}
+ // protected void printHeader(long runTime) {
+ // this.runTime = runTime; // defer printing for later
+ // }
+ // protected void printFooter(TestResult result) {
+ // if (result.wasSuccessful()) {
+ // getWriter().println("OK (" + result.runCount() + " tests)"
+ // + " Time: " + elapsedTimeAsString(runTime));
+ // } else {
+ // getWriter().println("Time: " + elapsedTimeAsString(runTime));
+ // super.printFooter(result);
+ // }
+ // }
+ // }
+
+ /**
+ * Returns a TestRunner that doesn't bother with unnecessary
+ * fluff, like printing a "." for each test case.
+ */
+ // static junit.textui.TestRunner newPithyTestRunner() {
+ // junit.textui.TestRunner runner = new junit.textui.TestRunner();
+ // runner.setPrinter(new PithyResultPrinter(System.out));
+ // return runner;
+ // }
+
+ /**
+ * Runs all unit tests in the given test suite.
+ * Actual behavior influenced by jsr166.* system properties.
+ */
+ // static void main(Test suite, String[] args) {
+ // if (useSecurityManager) {
+ // System.err.println("Setting a permissive security manager");
+ // Policy.setPolicy(permissivePolicy());
+ // System.setSecurityManager(new SecurityManager());
+ // }
+ // for (int i = 0; i < suiteRuns; i++) {
+ // TestResult result = newPithyTestRunner().doRun(suite);
+ // if (!result.wasSuccessful())
+ // System.exit(1);
+ // System.gc();
+ // System.runFinalization();
+ // }
+ // }
+
+ public static TestSuite newTestSuite(Object... suiteOrClasses) {
+ TestSuite suite = new TestSuite();
+ for (Object suiteOrClass : suiteOrClasses) {
+ if (suiteOrClass instanceof TestSuite)
+ suite.addTest((TestSuite) suiteOrClass);
+ else if (suiteOrClass instanceof Class)
+ suite.addTest(new TestSuite((Class<?>) suiteOrClass));
+ else
+ throw new ClassCastException("not a test suite or class");
+ }
+ return suite;
+ }
+
+ public static void addNamedTestClasses(TestSuite suite,
+ String... testClassNames) {
+ for (String testClassName : testClassNames) {
+ try {
+ Class<?> testClass = Class.forName(testClassName);
+ Method m = testClass.getDeclaredMethod("suite",
+ new Class<?>[0]);
+ suite.addTest(newTestSuite((Test)m.invoke(null)));
+ } catch (Exception e) {
+ throw new Error("Missing test class", e);
+ }
+ }
+ }
+
+ public static final double JAVA_CLASS_VERSION;
+ public static final String JAVA_SPECIFICATION_VERSION;
+ static {
+ try {
+ JAVA_CLASS_VERSION = java.security.AccessController.doPrivileged(
+ new java.security.PrivilegedAction<Double>() {
+ public Double run() {
+ return Double.valueOf(System.getProperty("java.class.version"));}});
+ JAVA_SPECIFICATION_VERSION = java.security.AccessController.doPrivileged(
+ new java.security.PrivilegedAction<String>() {
+ public String run() {
+ return System.getProperty("java.specification.version");}});
+ } catch (Throwable t) {
+ throw new Error(t);
+ }
+ }
+
+ public static boolean atLeastJava6() { return JAVA_CLASS_VERSION >= 50.0; }
+ public static boolean atLeastJava7() { return JAVA_CLASS_VERSION >= 51.0; }
+ public static boolean atLeastJava8() { return JAVA_CLASS_VERSION >= 52.0; }
+ public static boolean atLeastJava9() {
+ return JAVA_CLASS_VERSION >= 53.0
+ // As of 2015-09, java9 still uses 52.0 class file version
+ || JAVA_SPECIFICATION_VERSION.matches("^(1\\.)?(9|[0-9][0-9])$");
+ }
+ public static boolean atLeastJava10() {
+ return JAVA_CLASS_VERSION >= 54.0
+ || JAVA_SPECIFICATION_VERSION.matches("^(1\\.)?[0-9][0-9]$");
+ }
+
+ /**
+ * Collects all JSR166 unit tests as one suite.
+ */
+ // android-note: Removed because the CTS runner does a bad job of
+ // public static Test suite() {
+ // // Java7+ test classes
+ // TestSuite suite = newTestSuite(
+ // ForkJoinPoolTest.suite(),
+ // ForkJoinTaskTest.suite(),
+ // RecursiveActionTest.suite(),
+ // RecursiveTaskTest.suite(),
+ // LinkedTransferQueueTest.suite(),
+ // PhaserTest.suite(),
+ // ThreadLocalRandomTest.suite(),
+ // AbstractExecutorServiceTest.suite(),
+ // AbstractQueueTest.suite(),
+ // AbstractQueuedSynchronizerTest.suite(),
+ // AbstractQueuedLongSynchronizerTest.suite(),
+ // ArrayBlockingQueueTest.suite(),
+ // ArrayDequeTest.suite(),
+ // AtomicBooleanTest.suite(),
+ // AtomicIntegerArrayTest.suite(),
+ // AtomicIntegerFieldUpdaterTest.suite(),
+ // AtomicIntegerTest.suite(),
+ // AtomicLongArrayTest.suite(),
+ // AtomicLongFieldUpdaterTest.suite(),
+ // AtomicLongTest.suite(),
+ // AtomicMarkableReferenceTest.suite(),
+ // AtomicReferenceArrayTest.suite(),
+ // AtomicReferenceFieldUpdaterTest.suite(),
+ // AtomicReferenceTest.suite(),
+ // AtomicStampedReferenceTest.suite(),
+ // ConcurrentHashMapTest.suite(),
+ // ConcurrentLinkedDequeTest.suite(),
+ // ConcurrentLinkedQueueTest.suite(),
+ // ConcurrentSkipListMapTest.suite(),
+ // ConcurrentSkipListSubMapTest.suite(),
+ // ConcurrentSkipListSetTest.suite(),
+ // ConcurrentSkipListSubSetTest.suite(),
+ // CopyOnWriteArrayListTest.suite(),
+ // CopyOnWriteArraySetTest.suite(),
+ // CountDownLatchTest.suite(),
+ // CyclicBarrierTest.suite(),
+ // DelayQueueTest.suite(),
+ // EntryTest.suite(),
+ // ExchangerTest.suite(),
+ // ExecutorsTest.suite(),
+ // ExecutorCompletionServiceTest.suite(),
+ // FutureTaskTest.suite(),
+ // LinkedBlockingDequeTest.suite(),
+ // LinkedBlockingQueueTest.suite(),
+ // LinkedListTest.suite(),
+ // LockSupportTest.suite(),
+ // PriorityBlockingQueueTest.suite(),
+ // PriorityQueueTest.suite(),
+ // ReentrantLockTest.suite(),
+ // ReentrantReadWriteLockTest.suite(),
+ // ScheduledExecutorTest.suite(),
+ // ScheduledExecutorSubclassTest.suite(),
+ // SemaphoreTest.suite(),
+ // SynchronousQueueTest.suite(),
+ // SystemTest.suite(),
+ // ThreadLocalTest.suite(),
+ // ThreadPoolExecutorTest.suite(),
+ // ThreadPoolExecutorSubclassTest.suite(),
+ // ThreadTest.suite(),
+ // TimeUnitTest.suite(),
+ // TreeMapTest.suite(),
+ // TreeSetTest.suite(),
+ // TreeSubMapTest.suite(),
+ // TreeSubSetTest.suite());
+
+ // // Java8+ test classes
+ // if (atLeastJava8()) {
+ // String[] java8TestClassNames = {
+ // "Atomic8Test",
+ // "CompletableFutureTest",
+ // "ConcurrentHashMap8Test",
+ // "CountedCompleterTest",
+ // "DoubleAccumulatorTest",
+ // "DoubleAdderTest",
+ // "ForkJoinPool8Test",
+ // "ForkJoinTask8Test",
+ // "LongAccumulatorTest",
+ // "LongAdderTest",
+ // "SplittableRandomTest",
+ // "StampedLockTest",
+ // "SubmissionPublisherTest",
+ // "ThreadLocalRandom8Test",
+ // };
+ // addNamedTestClasses(suite, java8TestClassNames);
+ // }
+
+ // // Java9+ test classes
+ // if (atLeastJava9()) {
+ // String[] java9TestClassNames = {
+ // // Currently empty, but expecting varhandle tests
+ // };
+ // addNamedTestClasses(suite, java9TestClassNames);
+ // }
+
+ // return suite;
+ // }
+
+ /** Returns list of junit-style test method names in given class. */
+ public static ArrayList<String> testMethodNames(Class<?> testClass) {
+ Method[] methods = testClass.getDeclaredMethods();
+ ArrayList<String> names = new ArrayList<String>(methods.length);
+ for (Method method : methods) {
+ if (method.getName().startsWith("test")
+ && Modifier.isPublic(method.getModifiers())
+ // method.getParameterCount() requires jdk8+
+ && method.getParameterTypes().length == 0) {
+ names.add(method.getName());
+ }
+ }
+ return names;
+ }
+
+ /**
+ * Returns junit-style testSuite for the given test class, but
+ * parameterized by passing extra data to each test.
+ */
+ public static <ExtraData> Test parameterizedTestSuite
+ (Class<? extends JSR166TestCase> testClass,
+ Class<ExtraData> dataClass,
+ ExtraData data) {
+ try {
+ TestSuite suite = new TestSuite();
+ Constructor c =
+ testClass.getDeclaredConstructor(dataClass, String.class);
+ for (String methodName : testMethodNames(testClass))
+ suite.addTest((Test) c.newInstance(data, methodName));
+ return suite;
+ } catch (Exception e) {
+ throw new Error(e);
+ }
+ }
+
+ /**
+ * Returns junit-style testSuite for the jdk8 extension of the
+ * given test class, but parameterized by passing extra data to
+ * each test. Uses reflection to allow compilation in jdk7.
+ */
+ public static <ExtraData> Test jdk8ParameterizedTestSuite
+ (Class<? extends JSR166TestCase> testClass,
+ Class<ExtraData> dataClass,
+ ExtraData data) {
+ if (atLeastJava8()) {
+ String name = testClass.getName();
+ String name8 = name.replaceAll("Test$", "8Test");
+ if (name.equals(name8)) throw new Error(name);
+ try {
+ return (Test)
+ Class.forName(name8)
+ .getMethod("testSuite", new Class[] { dataClass })
+ .invoke(null, data);
+ } catch (Exception e) {
+ throw new Error(e);
+ }
+ } else {
+ return new TestSuite();
+ }
+ }
+
+ // Delays for timing-dependent tests, in milliseconds.
public static long SHORT_DELAY_MS;
public static long SMALL_DELAY_MS;
@@ -136,11 +575,13 @@
public static long LONG_DELAY_MS;
/**
- * Returns the shortest timed delay. This could
- * be reimplemented to use for example a Property.
+ * Returns the shortest timed delay. This can be scaled up for
+ * slow machines using the jsr166.delay.factor system property,
+ * or via jtreg's -timeoutFactor: flag.
+ * http://openjdk.java.net/jtreg/command-help.html
*/
protected long getShortDelay() {
- return 50;
+ return (long) (50 * delayFactor * jtregTestTimeoutFactor);
}
/**
@@ -162,11 +603,12 @@
}
/**
- * Returns a new Date instance representing a time delayMillis
- * milliseconds in the future.
+ * Returns a new Date instance representing a time at least
+ * delayMillis milliseconds in the future.
*/
Date delayedDate(long delayMillis) {
- return new Date(System.currentTimeMillis() + delayMillis);
+ // Add 1 because currentTimeMillis is known to round into the past.
+ return new Date(System.currentTimeMillis() + delayMillis + 1);
}
/**
@@ -182,6 +624,8 @@
* the same test have no effect.
*/
public void threadRecordFailure(Throwable t) {
+ System.err.println(t);
+ dumpTestThreads();
threadFailure.compareAndSet(null, t);
}
@@ -189,6 +633,13 @@
setDelays();
}
+ void tearDownFail(String format, Object... args) {
+ String msg = toString() + ": " + String.format(format, args);
+ System.err.println(msg);
+ dumpTestThreads();
+ throw new AssertionFailedError(msg);
+ }
+
/**
* Extra checks that get done for all test cases.
*
@@ -216,16 +667,16 @@
}
if (Thread.interrupted())
- throw new AssertionFailedError("interrupt status set in main thread");
+ tearDownFail("interrupt status set in main thread");
checkForkJoinPoolThreadLeaks();
}
/**
- * Finds missing try { ... } finally { joinPool(e); }
+ * Finds missing PoolCleaners
*/
void checkForkJoinPoolThreadLeaks() throws InterruptedException {
- Thread[] survivors = new Thread[5];
+ Thread[] survivors = new Thread[7];
int count = Thread.enumerate(survivors);
for (int i = 0; i < count; i++) {
Thread thread = survivors[i];
@@ -233,13 +684,15 @@
if (name.startsWith("ForkJoinPool-")) {
// give thread some time to terminate
thread.join(LONG_DELAY_MS);
- if (!thread.isAlive()) continue;
- thread.stop();
- throw new AssertionFailedError
- (String.format("Found leaked ForkJoinPool thread test=%s thread=%s%n",
- toString(), name));
+ if (thread.isAlive())
+ tearDownFail("Found leaked ForkJoinPool thread thread=%s",
+ thread);
}
}
+
+ if (!ForkJoinPool.commonPool()
+ .awaitQuiescence(LONG_DELAY_MS, MILLISECONDS))
+ tearDownFail("ForkJoin common pool thread stuck");
}
/**
@@ -252,7 +705,7 @@
fail(reason);
} catch (AssertionFailedError t) {
threadRecordFailure(t);
- fail(reason);
+ throw t;
}
}
@@ -379,44 +832,148 @@
/**
* Delays, via Thread.sleep, for the given millisecond delay, but
* if the sleep is shorter than specified, may re-sleep or yield
- * until time elapses.
+ * until time elapses. Ensures that the given time, as measured
+ * by System.nanoTime(), has elapsed.
*/
static void delay(long millis) throws InterruptedException {
- long startTime = System.nanoTime();
- long ns = millis * 1000 * 1000;
- for (;;) {
+ long nanos = millis * (1000 * 1000);
+ final long wakeupTime = System.nanoTime() + nanos;
+ do {
if (millis > 0L)
Thread.sleep(millis);
else // too short to sleep
Thread.yield();
- long d = ns - (System.nanoTime() - startTime);
- if (d > 0L)
- millis = d / (1000 * 1000);
- else
- break;
+ nanos = wakeupTime - System.nanoTime();
+ millis = nanos / (1000 * 1000);
+ } while (nanos >= 0L);
+ }
+
+ /**
+ * Allows use of try-with-resources with per-test thread pools.
+ */
+ class PoolCleaner implements AutoCloseable {
+ private final ExecutorService pool;
+ public PoolCleaner(ExecutorService pool) { this.pool = pool; }
+ public void close() { joinPool(pool); }
+ }
+
+ /**
+ * An extension of PoolCleaner that has an action to release the pool.
+ */
+ class PoolCleanerWithReleaser extends PoolCleaner {
+ private final Runnable releaser;
+ public PoolCleanerWithReleaser(ExecutorService pool, Runnable releaser) {
+ super(pool);
+ this.releaser = releaser;
}
+ public void close() {
+ try {
+ releaser.run();
+ } finally {
+ super.close();
+ }
+ }
+ }
+
+ PoolCleaner cleaner(ExecutorService pool) {
+ return new PoolCleaner(pool);
+ }
+
+ PoolCleaner cleaner(ExecutorService pool, Runnable releaser) {
+ return new PoolCleanerWithReleaser(pool, releaser);
+ }
+
+ PoolCleaner cleaner(ExecutorService pool, CountDownLatch latch) {
+ return new PoolCleanerWithReleaser(pool, releaser(latch));
+ }
+
+ Runnable releaser(final CountDownLatch latch) {
+ return new Runnable() { public void run() {
+ do { latch.countDown(); }
+ while (latch.getCount() > 0);
+ }};
+ }
+
+ PoolCleaner cleaner(ExecutorService pool, AtomicBoolean flag) {
+ return new PoolCleanerWithReleaser(pool, releaser(flag));
+ }
+
+ Runnable releaser(final AtomicBoolean flag) {
+ return new Runnable() { public void run() { flag.set(true); }};
}
/**
* Waits out termination of a thread pool or fails doing so.
*/
- void joinPool(ExecutorService exec) {
+ void joinPool(ExecutorService pool) {
try {
- exec.shutdown();
- if (!exec.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS))
- fail("ExecutorService " + exec +
- " did not terminate in a timely manner");
+ pool.shutdown();
+ if (!pool.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)) {
+ try {
+ threadFail("ExecutorService " + pool +
+ " did not terminate in a timely manner");
+ } finally {
+ // last resort, for the benefit of subsequent tests
+ pool.shutdownNow();
+ pool.awaitTermination(MEDIUM_DELAY_MS, MILLISECONDS);
+ }
+ }
} catch (SecurityException ok) {
// Allowed in case test doesn't have privs
} catch (InterruptedException fail) {
- fail("Unexpected InterruptedException");
+ threadFail("Unexpected InterruptedException");
+ }
+ }
+
+ /** Like Runnable, but with the freedom to throw anything */
+ interface Action { public void run() throws Throwable; }
+
+ /**
+ * Runs all the given actions in parallel, failing if any fail.
+ * Useful for running multiple variants of tests that are
+ * necessarily individually slow because they must block.
+ */
+ void testInParallel(Action ... actions) {
+ ExecutorService pool = Executors.newCachedThreadPool();
+ try (PoolCleaner cleaner = cleaner(pool)) {
+ ArrayList<Future<?>> futures = new ArrayList<>(actions.length);
+ for (final Action action : actions)
+ futures.add(pool.submit(new CheckedRunnable() {
+ public void realRun() throws Throwable { action.run();}}));
+ for (Future<?> future : futures)
+ try {
+ assertNull(future.get(LONG_DELAY_MS, MILLISECONDS));
+ } catch (ExecutionException ex) {
+ threadUnexpectedException(ex.getCause());
+ } catch (Exception ex) {
+ threadUnexpectedException(ex);
+ }
}
}
/**
- * A debugging tool to print all stack traces, as jstack does.
+ * A debugging tool to print stack traces of most threads, as jstack does.
+ * Uninteresting threads are filtered out.
*/
- static void printAllStackTraces() {
+ static void dumpTestThreads() {
+ // Android-change no ThreadMXBean
+ // ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
+ // System.err.println("------ stacktrace dump start ------");
+ // for (ThreadInfo info : threadMXBean.dumpAllThreads(true, true)) {
+ // String name = info.getThreadName();
+ // if ("Signal Dispatcher".equals(name))
+ // continue;
+ // if ("Reference Handler".equals(name)
+ // && info.getLockName().startsWith("java.lang.ref.Reference$Lock"))
+ // continue;
+ // if ("Finalizer".equals(name)
+ // && info.getLockName().startsWith("java.lang.ref.ReferenceQueue$Lock"))
+ // continue;
+ // if ("checkForWedgedTest".equals(name))
+ // continue;
+ // System.err.print(info);
+ // }
+ // System.err.println("------ stacktrace dump end ------");
}
/**
@@ -436,7 +993,7 @@
delay(millis);
assertTrue(thread.isAlive());
} catch (InterruptedException fail) {
- fail("Unexpected InterruptedException");
+ threadFail("Unexpected InterruptedException");
}
}
@@ -458,7 +1015,7 @@
for (Thread thread : threads)
assertTrue(thread.isAlive());
} catch (InterruptedException fail) {
- fail("Unexpected InterruptedException");
+ threadFail("Unexpected InterruptedException");
}
}
@@ -532,6 +1089,12 @@
* getPolicy/setPolicy.
*/
public void runWithPermissions(Runnable r, Permission... permissions) {
+ // Android-changed - no SecurityManager
+ // SecurityManager sm = System.getSecurityManager();
+ // if (sm == null) {
+ // r.run();
+ // }
+ // runWithSecurityManagerWithPermissions(r, permissions);
r.run();
}
@@ -544,6 +1107,30 @@
*/
public void runWithSecurityManagerWithPermissions(Runnable r,
Permission... permissions) {
+ // Android-changed - no SecurityManager
+ // SecurityManager sm = System.getSecurityManager();
+ // if (sm == null) {
+ // Policy savedPolicy = Policy.getPolicy();
+ // try {
+ // Policy.setPolicy(permissivePolicy());
+ // System.setSecurityManager(new SecurityManager());
+ // runWithSecurityManagerWithPermissions(r, permissions);
+ // } finally {
+ // System.setSecurityManager(null);
+ // Policy.setPolicy(savedPolicy);
+ // }
+ // } else {
+ // Policy savedPolicy = Policy.getPolicy();
+ // AdjustablePolicy policy = new AdjustablePolicy(permissions);
+ // Policy.setPolicy(policy);
+
+ // try {
+ // r.run();
+ // } finally {
+ // policy.addPermission(new SecurityPermission("setPolicy"));
+ // Policy.setPolicy(savedPolicy);
+ // }
+ // }
r.run();
}
@@ -648,19 +1235,6 @@
waitForThreadToEnterWaitState(thread, LONG_DELAY_MS);
}
- void waitForThreadToEnterWaitStateNoTimeout(Thread thread) {
- for (;;) {
- Thread.State s = thread.getState();
- if (s == Thread.State.BLOCKED ||
- s == Thread.State.WAITING ||
- s == Thread.State.TIMED_WAITING)
- return;
- else if (s == Thread.State.TERMINATED)
- fail("Unexpected thread termination");
- Thread.yield();
- }
- }
-
/**
* Returns the number of milliseconds since time given by
* startNanoTime, which must have been previously returned from a
@@ -723,7 +1297,7 @@
} finally {
if (t.getState() != Thread.State.TERMINATED) {
t.interrupt();
- fail("Test timed out");
+ threadFail("timed out waiting for thread to terminate");
}
}
}
@@ -848,7 +1422,10 @@
public static final String TEST_STRING = "a test string";
public static class StringTask implements Callable<String> {
- public String call() { return TEST_STRING; }
+ final String value;
+ public StringTask() { this(TEST_STRING); }
+ public StringTask(String value) { this.value = value; }
+ public String call() { return value; }
}
public Callable<String> latchAwaitingStringTask(final CountDownLatch latch) {
@@ -861,24 +1438,50 @@
}};
}
- public Runnable awaiter(final CountDownLatch latch) {
+ public Runnable countDowner(final CountDownLatch latch) {
return new CheckedRunnable() {
public void realRun() throws InterruptedException {
- await(latch);
+ latch.countDown();
}};
}
- public void await(CountDownLatch latch) {
+ class LatchAwaiter extends CheckedRunnable {
+ static final int NEW = 0;
+ static final int RUNNING = 1;
+ static final int DONE = 2;
+ final CountDownLatch latch;
+ int state = NEW;
+ LatchAwaiter(CountDownLatch latch) { this.latch = latch; }
+ public void realRun() throws InterruptedException {
+ state = 1;
+ await(latch);
+ state = 2;
+ }
+ }
+
+ public LatchAwaiter awaiter(CountDownLatch latch) {
+ return new LatchAwaiter(latch);
+ }
+
+ public void await(CountDownLatch latch, long timeoutMillis) {
try {
- assertTrue(latch.await(LONG_DELAY_MS, MILLISECONDS));
+ if (!latch.await(timeoutMillis, MILLISECONDS))
+ fail("timed out waiting for CountDownLatch for "
+ + (timeoutMillis/1000) + " sec");
} catch (Throwable fail) {
threadUnexpectedException(fail);
}
}
+ public void await(CountDownLatch latch) {
+ await(latch, LONG_DELAY_MS);
+ }
+
public void await(Semaphore semaphore) {
try {
- assertTrue(semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS));
+ if (!semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS))
+ fail("timed out waiting for Semaphore for "
+ + (LONG_DELAY_MS/1000) + " sec");
} catch (Throwable fail) {
threadUnexpectedException(fail);
}
diff --git a/jsr166-tests/src/test/java/jsr166/LinkedBlockingDequeTest.java b/jsr166-tests/src/test/java/jsr166/LinkedBlockingDequeTest.java
index 62802bb..789373d 100644
--- a/jsr166-tests/src/test/java/jsr166/LinkedBlockingDequeTest.java
+++ b/jsr166-tests/src/test/java/jsr166/LinkedBlockingDequeTest.java
@@ -26,25 +26,24 @@
public class LinkedBlockingDequeTest extends JSR166TestCase {
- // android-note: These tests have been moved into their own separate
- // classes to work around CTS issues.
- //
- // public static class Unbounded extends BlockingQueueTest {
- // protected BlockingQueue emptyCollection() {
- // return new LinkedBlockingDeque();
- // }
- // }
- //
- // public static class Bounded extends BlockingQueueTest {
- // protected BlockingQueue emptyCollection() {
- // return new LinkedBlockingDeque(SIZE);
- // }
- // }
+ public static class Unbounded extends BlockingQueueTest {
+ protected BlockingQueue emptyCollection() {
+ return new LinkedBlockingDeque();
+ }
+ }
+
+ public static class Bounded extends BlockingQueueTest {
+ protected BlockingQueue emptyCollection() {
+ return new LinkedBlockingDeque(SIZE);
+ }
+ }
+
+ // 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 newTestSuite(LinkedBlockingDequeTest.class,
// new Unbounded().testSuite(),
@@ -87,7 +86,7 @@
public void testSize() {
LinkedBlockingDeque q = populatedDeque(SIZE);
for (int i = 0; i < SIZE; ++i) {
- assertEquals(SIZE-i, q.size());
+ assertEquals(SIZE - i, q.size());
q.removeFirst();
}
for (int i = 0; i < SIZE; ++i) {
@@ -152,7 +151,7 @@
*/
public void testPollLast() {
LinkedBlockingDeque q = populatedDeque(SIZE);
- for (int i = SIZE-1; i >= 0; --i) {
+ for (int i = SIZE - 1; i >= 0; --i) {
assertEquals(i, q.pollLast());
}
assertNull(q.pollLast());
@@ -191,7 +190,7 @@
*/
public void testPeekLast() {
LinkedBlockingDeque q = populatedDeque(SIZE);
- for (int i = SIZE-1; i >= 0; --i) {
+ for (int i = SIZE - 1; i >= 0; --i) {
assertEquals(i, q.peekLast());
assertEquals(i, q.pollLast());
assertTrue(q.peekLast() == null ||
@@ -221,7 +220,7 @@
*/
public void testLastElement() {
LinkedBlockingDeque q = populatedDeque(SIZE);
- for (int i = SIZE-1; i >= 0; --i) {
+ for (int i = SIZE - 1; i >= 0; --i) {
assertEquals(i, q.getLast());
assertEquals(i, q.pollLast());
}
@@ -286,7 +285,7 @@
}
for (int i = 0; i < SIZE; i += 2) {
assertTrue(q.removeFirstOccurrence(new Integer(i)));
- assertFalse(q.removeFirstOccurrence(new Integer(i+1)));
+ assertFalse(q.removeFirstOccurrence(new Integer(i + 1)));
}
assertTrue(q.isEmpty());
}
@@ -301,7 +300,7 @@
}
for (int i = 0; i < SIZE; i += 2) {
assertTrue(q.removeLastOccurrence(new Integer(i)));
- assertFalse(q.removeLastOccurrence(new Integer(i+1)));
+ assertFalse(q.removeLastOccurrence(new Integer(i + 1)));
}
assertTrue(q.isEmpty());
}
@@ -372,7 +371,7 @@
*/
public void testConstructor5() {
Integer[] ints = new Integer[SIZE];
- for (int i = 0; i < SIZE-1; ++i)
+ for (int i = 0; i < SIZE - 1; ++i)
ints[i] = i;
Collection<Integer> elements = Arrays.asList(ints);
try {
@@ -419,7 +418,7 @@
assertEquals(i, q.remove());
}
for (int i = 0; i < SIZE; ++i) {
- assertEquals(SIZE-i, q.remainingCapacity());
+ assertEquals(SIZE - i, q.remainingCapacity());
assertEquals(SIZE, q.size() + q.remainingCapacity());
assertTrue(q.add(i));
}
@@ -429,8 +428,8 @@
* push(null) throws NPE
*/
public void testPushNull() {
+ LinkedBlockingDeque q = new LinkedBlockingDeque(1);
try {
- LinkedBlockingDeque q = new LinkedBlockingDeque(1);
q.push(null);
shouldThrow();
} catch (NullPointerException success) {}
@@ -440,14 +439,14 @@
* push succeeds if not full; throws ISE if full
*/
public void testPush() {
+ LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
+ for (int i = 0; i < SIZE; ++i) {
+ Integer x = new Integer(i);
+ q.push(x);
+ assertEquals(x, q.peek());
+ }
+ assertEquals(0, q.remainingCapacity());
try {
- LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
- for (int i = 0; i < SIZE; ++i) {
- Integer x = new Integer(i);
- q.push(x);
- assertEquals(x, q.peek());
- }
- assertEquals(0, q.remainingCapacity());
q.push(new Integer(SIZE));
shouldThrow();
} catch (IllegalStateException success) {}
@@ -518,7 +517,7 @@
public void testAddAll3() {
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
Integer[] ints = new Integer[SIZE];
- for (int i = 0; i < SIZE-1; ++i)
+ for (int i = 0; i < SIZE - 1; ++i)
ints[i] = new Integer(i);
Collection<Integer> elements = Arrays.asList(ints);
try {
@@ -756,25 +755,23 @@
final CountDownLatch aboutToWait = new CountDownLatch(1);
Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
+ long startTime = System.nanoTime();
for (int i = 0; i < SIZE; ++i) {
- long t0 = System.nanoTime();
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
- assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
}
- long t0 = System.nanoTime();
aboutToWait.countDown();
try {
- q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
+ q.poll(LONG_DELAY_MS, MILLISECONDS);
shouldThrow();
} catch (InterruptedException success) {
- assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
+ assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
}
}});
aboutToWait.await();
- waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
+ waitForThreadToEnterWaitState(t, LONG_DELAY_MS);
t.interrupt();
- awaitTermination(t, MEDIUM_DELAY_MS);
+ awaitTermination(t);
checkEmpty(q);
}
@@ -1055,17 +1052,18 @@
* returning timeout status
*/
public void testInterruptedTimedPollFirst() throws InterruptedException {
+ final LinkedBlockingDeque q = populatedDeque(SIZE);
final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
- LinkedBlockingDeque q = populatedDeque(SIZE);
+ long startTime = System.nanoTime();
for (int i = 0; i < SIZE; ++i) {
assertEquals(i, q.pollFirst(LONG_DELAY_MS, MILLISECONDS));
}
Thread.currentThread().interrupt();
try {
- q.pollFirst(SMALL_DELAY_MS, MILLISECONDS);
+ q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
shouldThrow();
} catch (InterruptedException success) {}
assertFalse(Thread.interrupted());
@@ -1076,6 +1074,7 @@
shouldThrow();
} catch (InterruptedException success) {}
assertFalse(Thread.interrupted());
+ assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
}});
await(pleaseInterrupt);
@@ -1251,7 +1250,7 @@
public void testTakeLast() throws InterruptedException {
LinkedBlockingDeque q = populatedDeque(SIZE);
for (int i = 0; i < SIZE; ++i) {
- assertEquals(SIZE-i-1, q.takeLast());
+ assertEquals(SIZE - i - 1, q.takeLast());
}
}
@@ -1264,7 +1263,7 @@
Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
for (int i = 0; i < SIZE; ++i) {
- assertEquals(SIZE-i-1, q.takeLast());
+ assertEquals(SIZE - i - 1, q.takeLast());
}
Thread.currentThread().interrupt();
@@ -1294,7 +1293,7 @@
public void testTimedPollLast0() throws InterruptedException {
LinkedBlockingDeque q = populatedDeque(SIZE);
for (int i = 0; i < SIZE; ++i) {
- assertEquals(SIZE-i-1, q.pollLast(0, MILLISECONDS));
+ assertEquals(SIZE - i - 1, q.pollLast(0, MILLISECONDS));
}
assertNull(q.pollLast(0, MILLISECONDS));
}
@@ -1306,7 +1305,7 @@
LinkedBlockingDeque q = populatedDeque(SIZE);
for (int i = 0; i < SIZE; ++i) {
long startTime = System.nanoTime();
- assertEquals(SIZE-i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS));
+ assertEquals(SIZE - i - 1, q.pollLast(LONG_DELAY_MS, MILLISECONDS));
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
}
long startTime = System.nanoTime();
@@ -1320,12 +1319,14 @@
* returning timeout status
*/
public void testInterruptedTimedPollLast() throws InterruptedException {
+ final LinkedBlockingDeque q = populatedDeque(SIZE);
final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
- LinkedBlockingDeque q = populatedDeque(SIZE);
+ long startTime = System.nanoTime();
for (int i = 0; i < SIZE; ++i) {
- assertEquals(SIZE-i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS));
+ assertEquals(SIZE - i - 1,
+ q.pollLast(LONG_DELAY_MS, MILLISECONDS));
}
Thread.currentThread().interrupt();
@@ -1341,12 +1342,15 @@
shouldThrow();
} catch (InterruptedException success) {}
assertFalse(Thread.interrupted());
+
+ assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
}});
await(pleaseInterrupt);
assertThreadStaysAlive(t);
t.interrupt();
awaitTermination(t);
+ checkEmpty(q);
}
/**
@@ -1379,6 +1383,8 @@
shouldThrow();
} catch (InterruptedException success) {}
assertFalse(Thread.interrupted());
+
+ assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
}});
barrier.await();
@@ -1463,7 +1469,7 @@
assertTrue(changed);
assertTrue(q.containsAll(p));
- assertEquals(SIZE-i, q.size());
+ assertEquals(SIZE - i, q.size());
p.remove();
}
}
@@ -1476,7 +1482,7 @@
LinkedBlockingDeque q = populatedDeque(SIZE);
LinkedBlockingDeque p = populatedDeque(i);
assertTrue(q.removeAll(p));
- assertEquals(SIZE-i, q.size());
+ assertEquals(SIZE - i, q.size());
for (int j = 0; j < i; ++j) {
Integer x = (Integer)(p.remove());
assertFalse(q.contains(x));
@@ -1675,23 +1681,23 @@
final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
q.add(one);
q.add(two);
- ExecutorService executor = Executors.newFixedThreadPool(2);
final CheckedBarrier threadsStarted = new CheckedBarrier(2);
- executor.execute(new CheckedRunnable() {
- public void realRun() throws InterruptedException {
- assertFalse(q.offer(three));
- threadsStarted.await();
- assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
- assertEquals(0, q.remainingCapacity());
- }});
+ final ExecutorService executor = Executors.newFixedThreadPool(2);
+ try (PoolCleaner cleaner = cleaner(executor)) {
+ executor.execute(new CheckedRunnable() {
+ public void realRun() throws InterruptedException {
+ assertFalse(q.offer(three));
+ threadsStarted.await();
+ assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
+ assertEquals(0, q.remainingCapacity());
+ }});
- executor.execute(new CheckedRunnable() {
- public void realRun() throws InterruptedException {
- threadsStarted.await();
- assertSame(one, q.take());
- }});
-
- joinPool(executor);
+ executor.execute(new CheckedRunnable() {
+ public void realRun() throws InterruptedException {
+ threadsStarted.await();
+ assertSame(one, q.take());
+ }});
+ }
}
/**
@@ -1700,22 +1706,22 @@
public void testPollInExecutor() {
final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
final CheckedBarrier threadsStarted = new CheckedBarrier(2);
- ExecutorService executor = Executors.newFixedThreadPool(2);
- executor.execute(new CheckedRunnable() {
- public void realRun() throws InterruptedException {
- assertNull(q.poll());
- threadsStarted.await();
- assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
- checkEmpty(q);
- }});
+ final ExecutorService executor = Executors.newFixedThreadPool(2);
+ try (PoolCleaner cleaner = cleaner(executor)) {
+ executor.execute(new CheckedRunnable() {
+ public void realRun() throws InterruptedException {
+ assertNull(q.poll());
+ threadsStarted.await();
+ assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
+ checkEmpty(q);
+ }});
- executor.execute(new CheckedRunnable() {
- public void realRun() throws InterruptedException {
- threadsStarted.await();
- q.put(one);
- }});
-
- joinPool(executor);
+ executor.execute(new CheckedRunnable() {
+ public void realRun() throws InterruptedException {
+ threadsStarted.await();
+ q.put(one);
+ }});
+ }
}
/**
@@ -1767,7 +1773,7 @@
final LinkedBlockingDeque q = populatedDeque(SIZE);
Thread t = new Thread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
- q.put(new Integer(SIZE+1));
+ q.put(new Integer(SIZE + 1));
}});
t.start();
@@ -1792,7 +1798,7 @@
q.drainTo(l, i);
int k = (i < SIZE) ? i : SIZE;
assertEquals(k, l.size());
- assertEquals(SIZE-k, q.size());
+ assertEquals(SIZE - k, q.size());
for (int j = 0; j < k; ++j)
assertEquals(l.get(j), new Integer(j));
do {} while (q.poll() != null);
diff --git a/jsr166-tests/src/test/java/jsr166/LinkedBlockingQueueTest.java b/jsr166-tests/src/test/java/jsr166/LinkedBlockingQueueTest.java
index bd37b2a..faf3f18 100644
--- a/jsr166-tests/src/test/java/jsr166/LinkedBlockingQueueTest.java
+++ b/jsr166-tests/src/test/java/jsr166/LinkedBlockingQueueTest.java
@@ -26,25 +26,27 @@
public class LinkedBlockingQueueTest extends JSR166TestCase {
- // android-note: These tests have been moved into their own separate
+ // android-note: These tests have been moved into their own separate
// classes to work around CTS issues.
//
// public static class Unbounded extends BlockingQueueTest {
- // protected BlockingQueue emptyCollection() {
- // return new LinkedBlockingQueue();
+ // protected BlockingQueue emptyCollection() {
+ // return new LinkedBlockingQueue();
// }
// }
- //
+
// public static class Bounded extends BlockingQueueTest {
- // protected BlockingQueue emptyCollection() {
+ // protected BlockingQueue emptyCollection() {
// return new LinkedBlockingQueue(SIZE);
// }
// }
+
+ // 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);
+ // main(suite(), args);
// }
- //
// public static Test suite() {
// return newTestSuite(LinkedBlockingQueueTest.class,
// new Unbounded().testSuite(),
@@ -113,7 +115,7 @@
*/
public void testConstructor5() {
Integer[] ints = new Integer[SIZE];
- for (int i = 0; i < SIZE-1; ++i)
+ for (int i = 0; i < SIZE - 1; ++i)
ints[i] = new Integer(i);
Collection<Integer> elements = Arrays.asList(ints);
try {
@@ -160,7 +162,7 @@
assertEquals(i, q.remove());
}
for (int i = 0; i < SIZE; ++i) {
- assertEquals(SIZE-i, q.remainingCapacity());
+ assertEquals(SIZE - i, q.remainingCapacity());
assertEquals(SIZE, q.size() + q.remainingCapacity());
assertTrue(q.add(i));
}
@@ -207,7 +209,7 @@
public void testAddAll3() {
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
Integer[] ints = new Integer[SIZE];
- for (int i = 0; i < SIZE-1; ++i)
+ for (int i = 0; i < SIZE - 1; ++i)
ints[i] = new Integer(i);
Collection<Integer> elements = Arrays.asList(ints);
try {
@@ -445,25 +447,23 @@
final CountDownLatch aboutToWait = new CountDownLatch(1);
Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
+ long startTime = System.nanoTime();
for (int i = 0; i < SIZE; ++i) {
- long t0 = System.nanoTime();
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
- assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
}
- long t0 = System.nanoTime();
aboutToWait.countDown();
try {
- q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
+ q.poll(LONG_DELAY_MS, MILLISECONDS);
shouldThrow();
} catch (InterruptedException success) {
- assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
+ assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
}
}});
- aboutToWait.await();
- waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
+ await(aboutToWait);
+ waitForThreadToEnterWaitState(t, LONG_DELAY_MS);
t.interrupt();
- awaitTermination(t, MEDIUM_DELAY_MS);
+ awaitTermination(t);
checkEmpty(q);
}
@@ -579,7 +579,7 @@
assertTrue(changed);
assertTrue(q.containsAll(p));
- assertEquals(SIZE-i, q.size());
+ assertEquals(SIZE - i, q.size());
p.remove();
}
}
@@ -592,7 +592,7 @@
LinkedBlockingQueue q = populatedQueue(SIZE);
LinkedBlockingQueue p = populatedQueue(i);
assertTrue(q.removeAll(p));
- assertEquals(SIZE-i, q.size());
+ assertEquals(SIZE - i, q.size());
for (int j = 0; j < i; ++j) {
Integer x = (Integer)(p.remove());
assertFalse(q.contains(x));
@@ -727,23 +727,23 @@
final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
q.add(one);
q.add(two);
- ExecutorService executor = Executors.newFixedThreadPool(2);
final CheckedBarrier threadsStarted = new CheckedBarrier(2);
- executor.execute(new CheckedRunnable() {
- public void realRun() throws InterruptedException {
- assertFalse(q.offer(three));
- threadsStarted.await();
- assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
- assertEquals(0, q.remainingCapacity());
- }});
+ final ExecutorService executor = Executors.newFixedThreadPool(2);
+ try (PoolCleaner cleaner = cleaner(executor)) {
+ executor.execute(new CheckedRunnable() {
+ public void realRun() throws InterruptedException {
+ assertFalse(q.offer(three));
+ threadsStarted.await();
+ assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
+ assertEquals(0, q.remainingCapacity());
+ }});
- executor.execute(new CheckedRunnable() {
- public void realRun() throws InterruptedException {
- threadsStarted.await();
- assertSame(one, q.take());
- }});
-
- joinPool(executor);
+ executor.execute(new CheckedRunnable() {
+ public void realRun() throws InterruptedException {
+ threadsStarted.await();
+ assertSame(one, q.take());
+ }});
+ }
}
/**
@@ -752,22 +752,22 @@
public void testPollInExecutor() {
final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
final CheckedBarrier threadsStarted = new CheckedBarrier(2);
- ExecutorService executor = Executors.newFixedThreadPool(2);
- executor.execute(new CheckedRunnable() {
- public void realRun() throws InterruptedException {
- assertNull(q.poll());
- threadsStarted.await();
- assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
- checkEmpty(q);
- }});
+ final ExecutorService executor = Executors.newFixedThreadPool(2);
+ try (PoolCleaner cleaner = cleaner(executor)) {
+ executor.execute(new CheckedRunnable() {
+ public void realRun() throws InterruptedException {
+ assertNull(q.poll());
+ threadsStarted.await();
+ assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
+ checkEmpty(q);
+ }});
- executor.execute(new CheckedRunnable() {
- public void realRun() throws InterruptedException {
- threadsStarted.await();
- q.put(one);
- }});
-
- joinPool(executor);
+ executor.execute(new CheckedRunnable() {
+ public void realRun() throws InterruptedException {
+ threadsStarted.await();
+ q.put(one);
+ }});
+ }
}
/**
@@ -819,7 +819,7 @@
final LinkedBlockingQueue q = populatedQueue(SIZE);
Thread t = new Thread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
- q.put(new Integer(SIZE+1));
+ q.put(new Integer(SIZE + 1));
}});
t.start();
@@ -844,7 +844,7 @@
q.drainTo(l, i);
int k = (i < SIZE) ? i : SIZE;
assertEquals(k, l.size());
- assertEquals(SIZE-k, q.size());
+ assertEquals(SIZE - k, q.size());
for (int j = 0; j < k; ++j)
assertEquals(l.get(j), new Integer(j));
do {} while (q.poll() != null);
diff --git a/jsr166-tests/src/test/java/jsr166/LinkedListTest.java b/jsr166-tests/src/test/java/jsr166/LinkedListTest.java
index 9d9481d..9c971b4 100644
--- a/jsr166-tests/src/test/java/jsr166/LinkedListTest.java
+++ b/jsr166-tests/src/test/java/jsr166/LinkedListTest.java
@@ -25,7 +25,7 @@
// main(suite(), args);
// }
// public static Test suite() {
- // return new TestSuite(...);
+ // return new TestSuite(LinkedListTest.class);
// }
/**
@@ -91,7 +91,7 @@
public void testSize() {
LinkedList q = populatedQueue(SIZE);
for (int i = 0; i < SIZE; ++i) {
- assertEquals(SIZE-i, q.size());
+ assertEquals(SIZE - i, q.size());
q.remove();
}
for (int i = 0; i < SIZE; ++i) {
@@ -106,6 +106,8 @@
public void testOfferNull() {
LinkedList q = new LinkedList();
q.offer(null);
+ assertNull(q.get(0));
+ assertTrue(q.contains(null));
}
/**
@@ -132,8 +134,8 @@
* addAll(null) throws NPE
*/
public void testAddAll1() {
+ LinkedList q = new LinkedList();
try {
- LinkedList q = new LinkedList();
q.addAll(null);
shouldThrow();
} catch (NullPointerException success) {}
@@ -245,14 +247,14 @@
assertTrue(q.contains(i));
assertTrue(q.remove((Integer)i));
assertFalse(q.contains(i));
- assertTrue(q.contains(i-1));
+ assertTrue(q.contains(i - 1));
}
for (int i = 0; i < SIZE; i += 2) {
assertTrue(q.contains(i));
assertTrue(q.remove((Integer)i));
assertFalse(q.contains(i));
- assertFalse(q.remove((Integer)(i+1)));
- assertFalse(q.contains(i+1));
+ assertFalse(q.remove((Integer)(i + 1)));
+ assertFalse(q.contains(i + 1));
}
assertTrue(q.isEmpty());
}
@@ -311,7 +313,7 @@
assertTrue(changed);
assertTrue(q.containsAll(p));
- assertEquals(SIZE-i, q.size());
+ assertEquals(SIZE - i, q.size());
p.remove();
}
}
@@ -324,7 +326,7 @@
LinkedList q = populatedQueue(SIZE);
LinkedList p = populatedQueue(i);
assertTrue(q.removeAll(p));
- assertEquals(SIZE-i, q.size());
+ assertEquals(SIZE - i, q.size());
for (int j = 0; j < i; ++j) {
Integer x = (Integer)(p.remove());
assertFalse(q.contains(x));
@@ -549,7 +551,7 @@
*/
public void testPollLast() {
LinkedList q = populatedQueue(SIZE);
- for (int i = SIZE-1; i >= 0; --i) {
+ for (int i = SIZE - 1; i >= 0; --i) {
assertEquals(i, q.pollLast());
}
assertNull(q.pollLast());
@@ -574,7 +576,7 @@
*/
public void testPeekLast() {
LinkedList q = populatedQueue(SIZE);
- for (int i = SIZE-1; i >= 0; --i) {
+ for (int i = SIZE - 1; i >= 0; --i) {
assertEquals(i, q.peekLast());
assertEquals(i, q.pollLast());
assertTrue(q.peekLast() == null ||
@@ -600,7 +602,7 @@
*/
public void testLastElement() {
LinkedList q = populatedQueue(SIZE);
- for (int i = SIZE-1; i >= 0; --i) {
+ for (int i = SIZE - 1; i >= 0; --i) {
assertEquals(i, q.getLast());
assertEquals(i, q.pollLast());
}
@@ -621,7 +623,7 @@
}
for (int i = 0; i < SIZE; i += 2) {
assertTrue(q.removeFirstOccurrence(new Integer(i)));
- assertFalse(q.removeFirstOccurrence(new Integer(i+1)));
+ assertFalse(q.removeFirstOccurrence(new Integer(i + 1)));
}
assertTrue(q.isEmpty());
}
@@ -636,7 +638,7 @@
}
for (int i = 0; i < SIZE; i += 2) {
assertTrue(q.removeLastOccurrence(new Integer(i)));
- assertFalse(q.removeLastOccurrence(new Integer(i+1)));
+ assertFalse(q.removeLastOccurrence(new Integer(i + 1)));
}
assertTrue(q.isEmpty());
}
diff --git a/jsr166-tests/src/test/java/jsr166/LinkedTransferQueueTest.java b/jsr166-tests/src/test/java/jsr166/LinkedTransferQueueTest.java
index 8d3f276..c712592 100644
--- a/jsr166-tests/src/test/java/jsr166/LinkedTransferQueueTest.java
+++ b/jsr166-tests/src/test/java/jsr166/LinkedTransferQueueTest.java
@@ -25,30 +25,33 @@
import junit.framework.Test;
@SuppressWarnings({"unchecked", "rawtypes"})
-// android-changed: Extend BlockingQueueTest directly.
-public class LinkedTransferQueueTest extends BlockingQueueTest {
+public class LinkedTransferQueueTest extends JSR166TestCase {
+ static class Implementation implements CollectionImplementation {
+ public Class<?> klazz() { return LinkedTransferQueue.class; }
+ public Collection emptyCollection() { return new LinkedTransferQueue(); }
+ public Object makeElement(int i) { return i; }
+ public boolean isConcurrent() { return true; }
+ public boolean permitsNulls() { return false; }
+ }
- // android-changed: Extend BlockingQueueTest directly.
- //
- // public static class Generic extends BlockingQueueTest {
- // protected BlockingQueue emptyCollection() {
- // return new LinkedTransferQueue();
- // }
- // }
+ public static class Generic extends BlockingQueueTest {
+ protected BlockingQueue emptyCollection() {
+ return new LinkedTransferQueue();
+ }
+ }
+
+ // 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 newTestSuite(LinkedTransferQueueTest.class,
- // new Generic().testSuite());
+ // new Generic().testSuite(),
+ // CollectionTest.testSuite(new Implementation()));
// }
- protected BlockingQueue emptyCollection() {
- return new LinkedTransferQueue();
- }
-
/**
* Constructor builds new queue with size being zero and empty
* being true
@@ -87,7 +90,7 @@
*/
public void testConstructor4() {
Integer[] ints = new Integer[SIZE];
- for (int i = 0; i < SIZE-1; ++i)
+ for (int i = 0; i < SIZE - 1; ++i)
ints[i] = i;
Collection<Integer> elements = Arrays.asList(ints);
try {
@@ -141,8 +144,8 @@
* addAll(this) throws IllegalArgumentException
*/
public void testAddAllSelf() {
+ LinkedTransferQueue q = populatedQueue(SIZE);
try {
- LinkedTransferQueue q = populatedQueue(SIZE);
q.addAll(q);
shouldThrow();
} catch (IllegalArgumentException success) {}
@@ -153,12 +156,11 @@
* NullPointerException after possibly adding some elements
*/
public void testAddAll3() {
+ LinkedTransferQueue q = new LinkedTransferQueue();
+ Integer[] ints = new Integer[SIZE];
+ for (int i = 0; i < SIZE - 1; ++i)
+ ints[i] = i;
try {
- LinkedTransferQueue q = new LinkedTransferQueue();
- Integer[] ints = new Integer[SIZE];
- for (int i = 0; i < SIZE - 1; ++i) {
- ints[i] = i;
- }
q.addAll(Arrays.asList(ints));
shouldThrow();
} catch (NullPointerException success) {}
@@ -265,12 +267,12 @@
*/
public void testTimedPoll() throws InterruptedException {
LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
- for (int i = 0; i < SIZE; ++i) {
- long startTime = System.nanoTime();
- assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
- assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
- }
long startTime = System.nanoTime();
+ for (int i = 0; i < SIZE; ++i)
+ assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
+ assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
+
+ startTime = System.nanoTime();
assertNull(q.poll(timeoutMillis(), MILLISECONDS));
assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
checkEmpty(q);
@@ -285,25 +287,21 @@
final CountDownLatch aboutToWait = new CountDownLatch(1);
Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
- for (int i = 0; i < SIZE; ++i) {
- long t0 = System.nanoTime();
+ long startTime = System.nanoTime();
+ for (int i = 0; i < SIZE; ++i)
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
- assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
- }
- long t0 = System.nanoTime();
aboutToWait.countDown();
try {
- q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
+ q.poll(LONG_DELAY_MS, MILLISECONDS);
shouldThrow();
- } catch (InterruptedException success) {
- assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
- }
+ } catch (InterruptedException success) {}
+ assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
}});
aboutToWait.await();
- waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
+ waitForThreadToEnterWaitState(t);
t.interrupt();
- awaitTermination(t, MEDIUM_DELAY_MS);
+ awaitTermination(t);
checkEmpty(q);
}
@@ -315,19 +313,18 @@
final BlockingQueue<Integer> q = populatedQueue(SIZE);
Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
+ long startTime = System.nanoTime();
Thread.currentThread().interrupt();
- for (int i = 0; i < SIZE; ++i) {
- long t0 = System.nanoTime();
+ for (int i = 0; i < SIZE; ++i)
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
- assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
- }
try {
- q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
+ q.poll(LONG_DELAY_MS, MILLISECONDS);
shouldThrow();
} catch (InterruptedException success) {}
+ assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
}});
- awaitTermination(t, MEDIUM_DELAY_MS);
+ awaitTermination(t);
checkEmpty(q);
}
@@ -598,22 +595,24 @@
public void testOfferInExecutor() {
final LinkedTransferQueue q = new LinkedTransferQueue();
final CheckedBarrier threadsStarted = new CheckedBarrier(2);
- ExecutorService executor = Executors.newFixedThreadPool(2);
+ final ExecutorService executor = Executors.newFixedThreadPool(2);
+ try (PoolCleaner cleaner = cleaner(executor)) {
- executor.execute(new CheckedRunnable() {
- public void realRun() throws InterruptedException {
- threadsStarted.await();
- assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS));
- }});
+ executor.execute(new CheckedRunnable() {
+ public void realRun() throws InterruptedException {
+ threadsStarted.await();
+ long startTime = System.nanoTime();
+ assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS));
+ assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
+ }});
- executor.execute(new CheckedRunnable() {
- public void realRun() throws InterruptedException {
- threadsStarted.await();
- assertSame(one, q.take());
- checkEmpty(q);
- }});
-
- joinPool(executor);
+ executor.execute(new CheckedRunnable() {
+ public void realRun() throws InterruptedException {
+ threadsStarted.await();
+ assertSame(one, q.take());
+ checkEmpty(q);
+ }});
+ }
}
/**
@@ -622,23 +621,25 @@
public void testPollInExecutor() {
final LinkedTransferQueue q = new LinkedTransferQueue();
final CheckedBarrier threadsStarted = new CheckedBarrier(2);
- ExecutorService executor = Executors.newFixedThreadPool(2);
+ final ExecutorService executor = Executors.newFixedThreadPool(2);
+ try (PoolCleaner cleaner = cleaner(executor)) {
- executor.execute(new CheckedRunnable() {
- public void realRun() throws InterruptedException {
- assertNull(q.poll());
- threadsStarted.await();
- assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
- checkEmpty(q);
- }});
+ executor.execute(new CheckedRunnable() {
+ public void realRun() throws InterruptedException {
+ assertNull(q.poll());
+ threadsStarted.await();
+ long startTime = System.nanoTime();
+ assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
+ assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
+ checkEmpty(q);
+ }});
- executor.execute(new CheckedRunnable() {
- public void realRun() throws InterruptedException {
- threadsStarted.await();
- q.put(one);
- }});
-
- joinPool(executor);
+ executor.execute(new CheckedRunnable() {
+ public void realRun() throws InterruptedException {
+ threadsStarted.await();
+ q.put(one);
+ }});
+ }
}
/**
@@ -699,7 +700,7 @@
assertTrue(l.size() >= SIZE);
for (int i = 0; i < SIZE; ++i)
assertEquals(i, l.get(i));
- awaitTermination(t, MEDIUM_DELAY_MS);
+ awaitTermination(t);
assertTrue(q.size() + l.size() >= SIZE);
}
@@ -736,14 +737,15 @@
Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
threadStarted.countDown();
+ long startTime = System.nanoTime();
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
assertEquals(0, q.getWaitingConsumerCount());
assertFalse(q.hasWaitingConsumer());
+ assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
}});
threadStarted.await();
- // waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
- waitForThreadToEnterWaitStateNoTimeout(t);
+ waitForThreadToEnterWaitState(t);
assertEquals(1, q.getWaitingConsumerCount());
assertTrue(q.hasWaitingConsumer());
@@ -751,7 +753,7 @@
assertEquals(0, q.getWaitingConsumerCount());
assertFalse(q.hasWaitingConsumer());
- awaitTermination(t, MEDIUM_DELAY_MS);
+ awaitTermination(t);
}
/**
@@ -782,12 +784,11 @@
}});
threadStarted.await();
- // waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
- waitForThreadToEnterWaitStateNoTimeout(t);
+ waitForThreadToEnterWaitState(t);
assertEquals(1, q.size());
assertSame(five, q.poll());
checkEmpty(q);
- awaitTermination(t, MEDIUM_DELAY_MS);
+ awaitTermination(t);
}
/**
@@ -843,7 +844,7 @@
assertEquals(1, q.size());
assertTrue(q.offer(three));
assertSame(four, q.poll());
- awaitTermination(t, MEDIUM_DELAY_MS);
+ awaitTermination(t);
}
/**
@@ -866,15 +867,15 @@
assertEquals(1, q.size());
assertSame(four, q.take());
checkEmpty(q);
- awaitTermination(t, MEDIUM_DELAY_MS);
+ awaitTermination(t);
}
/**
* tryTransfer(null) throws NullPointerException
*/
public void testTryTransfer1() {
+ final LinkedTransferQueue q = new LinkedTransferQueue();
try {
- final LinkedTransferQueue q = new LinkedTransferQueue();
q.tryTransfer(null);
shouldThrow();
} catch (NullPointerException success) {}
@@ -908,9 +909,11 @@
assertTrue(q.tryTransfer(hotPotato));
}});
- assertSame(hotPotato, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
+ long startTime = System.nanoTime();
+ assertSame(hotPotato, q.poll(LONG_DELAY_MS, MILLISECONDS));
+ assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
checkEmpty(q);
- awaitTermination(t, MEDIUM_DELAY_MS);
+ awaitTermination(t);
}
/**
@@ -932,7 +935,7 @@
assertSame(q.take(), hotPotato);
checkEmpty(q);
- awaitTermination(t, MEDIUM_DELAY_MS);
+ awaitTermination(t);
}
/**
@@ -945,6 +948,7 @@
Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
+ long startTime = System.nanoTime();
Thread.currentThread().interrupt();
try {
q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS);
@@ -958,6 +962,7 @@
shouldThrow();
} catch (InterruptedException success) {}
assertFalse(Thread.interrupted());
+ assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
}});
await(pleaseInterrupt);
@@ -975,10 +980,10 @@
Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
- long t0 = System.nanoTime();
+ long startTime = System.nanoTime();
assertFalse(q.tryTransfer(new Object(),
timeoutMillis(), MILLISECONDS));
- assertTrue(millisElapsedSince(t0) >= timeoutMillis());
+ assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
checkEmpty(q);
}});
@@ -996,7 +1001,9 @@
Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
- assertTrue(q.tryTransfer(five, MEDIUM_DELAY_MS, MILLISECONDS));
+ long startTime = System.nanoTime();
+ assertTrue(q.tryTransfer(five, LONG_DELAY_MS, MILLISECONDS));
+ assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
checkEmpty(q);
}});
@@ -1006,7 +1013,7 @@
assertSame(four, q.poll());
assertSame(five, q.poll());
checkEmpty(q);
- awaitTermination(t, MEDIUM_DELAY_MS);
+ awaitTermination(t);
}
/**
@@ -1017,9 +1024,9 @@
final LinkedTransferQueue q = new LinkedTransferQueue();
assertTrue(q.offer(four));
assertEquals(1, q.size());
- long t0 = System.nanoTime();
+ long startTime = System.nanoTime();
assertFalse(q.tryTransfer(five, timeoutMillis(), MILLISECONDS));
- assertTrue(millisElapsedSince(t0) >= timeoutMillis());
+ assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
assertEquals(1, q.size());
assertSame(four, q.poll());
assertNull(q.poll());
diff --git a/jsr166-tests/src/test/java/jsr166/LockSupportTest.java b/jsr166-tests/src/test/java/jsr166/LockSupportTest.java
index 8347b08..b3a8ed8 100644
--- a/jsr166-tests/src/test/java/jsr166/LockSupportTest.java
+++ b/jsr166-tests/src/test/java/jsr166/LockSupportTest.java
@@ -26,9 +26,15 @@
// main(suite(), args);
// }
// public static Test suite() {
- // return new TestSuite(...);
+ // return new TestSuite(LockSupportTest.class);
// }
+ static {
+ // Reduce the risk of rare disastrous classloading in first call to
+ // LockSupport.park: https://bugs.openjdk.java.net/browse/JDK-8074773
+ Class<?> ensureLoaded = LockSupport.class;
+ }
+
/**
* Returns the blocker object used by tests in this file.
* Any old object will do; we'll return a convenient one.
diff --git a/jsr166-tests/src/test/java/jsr166/LongAccumulatorTest.java b/jsr166-tests/src/test/java/jsr166/LongAccumulatorTest.java
new file mode 100644
index 0000000..5dd52e9
--- /dev/null
+++ b/jsr166-tests/src/test/java/jsr166/LongAccumulatorTest.java
@@ -0,0 +1,161 @@
+/*
+ * Written by Doug Lea with assistance from members of JCP JSR-166
+ * Expert Group and released to the public domain, as explained at
+ * http://creativecommons.org/publicdomain/zero/1.0/
+ */
+
+package jsr166;
+
+import java.util.concurrent.Executors;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Phaser;
+import java.util.concurrent.atomic.LongAccumulator;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+public class LongAccumulatorTest 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(LongAccumulatorTest.class);
+ // }
+
+ /**
+ * default constructed initializes to zero
+ */
+ public void testConstructor() {
+ LongAccumulator ai = new LongAccumulator(Long::max, 0L);
+ assertEquals(0, ai.get());
+ }
+
+ /**
+ * accumulate accumulates given value to current, and get returns current value
+ */
+ public void testAccumulateAndGet() {
+ LongAccumulator ai = new LongAccumulator(Long::max, 0L);
+ ai.accumulate(2);
+ assertEquals(2, ai.get());
+ ai.accumulate(-4);
+ assertEquals(2, ai.get());
+ ai.accumulate(4);
+ assertEquals(4, ai.get());
+ }
+
+ /**
+ * reset() causes subsequent get() to return zero
+ */
+ public void testReset() {
+ LongAccumulator ai = new LongAccumulator(Long::max, 0L);
+ ai.accumulate(2);
+ assertEquals(2, ai.get());
+ ai.reset();
+ assertEquals(0, ai.get());
+ }
+
+ /**
+ * getThenReset() returns current value; subsequent get() returns zero
+ */
+ public void testGetThenReset() {
+ LongAccumulator ai = new LongAccumulator(Long::max, 0L);
+ ai.accumulate(2);
+ assertEquals(2, ai.get());
+ assertEquals(2, ai.getThenReset());
+ assertEquals(0, ai.get());
+ }
+
+ /**
+ * toString returns current value.
+ */
+ public void testToString() {
+ LongAccumulator ai = new LongAccumulator(Long::max, 0L);
+ assertEquals("0", ai.toString());
+ ai.accumulate(1);
+ assertEquals(Long.toString(1), ai.toString());
+ }
+
+ /**
+ * intValue returns current value.
+ */
+ public void testIntValue() {
+ LongAccumulator ai = new LongAccumulator(Long::max, 0L);
+ assertEquals(0, ai.intValue());
+ ai.accumulate(1);
+ assertEquals(1, ai.intValue());
+ }
+
+ /**
+ * longValue returns current value.
+ */
+ public void testLongValue() {
+ LongAccumulator ai = new LongAccumulator(Long::max, 0L);
+ assertEquals(0, ai.longValue());
+ ai.accumulate(1);
+ assertEquals(1, ai.longValue());
+ }
+
+ /**
+ * floatValue returns current value.
+ */
+ public void testFloatValue() {
+ LongAccumulator ai = new LongAccumulator(Long::max, 0L);
+ assertEquals(0.0f, ai.floatValue());
+ ai.accumulate(1);
+ assertEquals(1.0f, ai.floatValue());
+ }
+
+ /**
+ * doubleValue returns current value.
+ */
+ public void testDoubleValue() {
+ LongAccumulator ai = new LongAccumulator(Long::max, 0L);
+ assertEquals(0.0, ai.doubleValue());
+ ai.accumulate(1);
+ assertEquals(1.0, ai.doubleValue());
+ }
+
+ /**
+ * accumulates by multiple threads produce correct result
+ */
+ public void testAccumulateAndGetMT() {
+ final int incs = 1000000;
+ final int nthreads = 4;
+ final ExecutorService pool = Executors.newCachedThreadPool();
+ LongAccumulator a = new LongAccumulator(Long::max, 0L);
+ Phaser phaser = new Phaser(nthreads + 1);
+ for (int i = 0; i < nthreads; ++i)
+ pool.execute(new AccTask(a, phaser, incs));
+ phaser.arriveAndAwaitAdvance();
+ phaser.arriveAndAwaitAdvance();
+ long expected = incs - 1;
+ long result = a.get();
+ assertEquals(expected, result);
+ pool.shutdown();
+ }
+
+ static final class AccTask implements Runnable {
+ final LongAccumulator acc;
+ final Phaser phaser;
+ final int incs;
+ volatile long result;
+ AccTask(LongAccumulator acc, Phaser phaser, int incs) {
+ this.acc = acc;
+ this.phaser = phaser;
+ this.incs = incs;
+ }
+
+ public void run() {
+ phaser.arriveAndAwaitAdvance();
+ LongAccumulator a = acc;
+ for (int i = 0; i < incs; ++i)
+ a.accumulate(i);
+ result = a.get();
+ phaser.arrive();
+ }
+ }
+
+}
diff --git a/jsr166-tests/src/test/java/jsr166/LongAdderTest.java b/jsr166-tests/src/test/java/jsr166/LongAdderTest.java
new file mode 100644
index 0000000..800a9c8
--- /dev/null
+++ b/jsr166-tests/src/test/java/jsr166/LongAdderTest.java
@@ -0,0 +1,198 @@
+/*
+ * Written by Doug Lea with assistance from members of JCP JSR-166
+ * Expert Group and released to the public domain, as explained at
+ * http://creativecommons.org/publicdomain/zero/1.0/
+ */
+
+package jsr166;
+
+import java.util.concurrent.CyclicBarrier;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.atomic.LongAdder;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+public class LongAdderTest 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(LongAdderTest.class);
+ // }
+
+ /**
+ * default constructed initializes to zero
+ */
+ public void testConstructor() {
+ LongAdder ai = new LongAdder();
+ assertEquals(0, ai.sum());
+ }
+
+ /**
+ * add adds given value to current, and sum returns current value
+ */
+ public void testAddAndSum() {
+ LongAdder ai = new LongAdder();
+ ai.add(2);
+ assertEquals(2, ai.sum());
+ ai.add(-4);
+ assertEquals(-2, ai.sum());
+ }
+
+ /**
+ * decrement decrements and sum returns current value
+ */
+ public void testDecrementAndsum() {
+ LongAdder ai = new LongAdder();
+ ai.decrement();
+ assertEquals(-1, ai.sum());
+ ai.decrement();
+ assertEquals(-2, ai.sum());
+ }
+
+ /**
+ * incrementAndGet increments and returns current value
+ */
+ public void testIncrementAndsum() {
+ LongAdder ai = new LongAdder();
+ ai.increment();
+ assertEquals(1, ai.sum());
+ ai.increment();
+ assertEquals(2, ai.sum());
+ }
+
+ /**
+ * reset() causes subsequent sum() to return zero
+ */
+ public void testReset() {
+ LongAdder ai = new LongAdder();
+ ai.add(2);
+ assertEquals(2, ai.sum());
+ ai.reset();
+ assertEquals(0, ai.sum());
+ }
+
+ /**
+ * sumThenReset() returns sum; subsequent sum() returns zero
+ */
+ public void testSumThenReset() {
+ LongAdder ai = new LongAdder();
+ ai.add(2);
+ assertEquals(2, ai.sum());
+ assertEquals(2, ai.sumThenReset());
+ assertEquals(0, ai.sum());
+ }
+
+ /**
+ * a deserialized serialized adder holds same value
+ */
+ public void testSerialization() throws Exception {
+ LongAdder x = new LongAdder();
+ LongAdder y = serialClone(x);
+ assertNotSame(x, y);
+ x.add(-22);
+ LongAdder z = serialClone(x);
+ assertNotSame(y, z);
+ assertEquals(-22, x.sum());
+ assertEquals(0, y.sum());
+ assertEquals(-22, z.sum());
+ }
+
+ /**
+ * toString returns current value.
+ */
+ public void testToString() {
+ LongAdder ai = new LongAdder();
+ assertEquals("0", ai.toString());
+ ai.increment();
+ assertEquals(Long.toString(1), ai.toString());
+ }
+
+ /**
+ * intValue returns current value.
+ */
+ public void testIntValue() {
+ LongAdder ai = new LongAdder();
+ assertEquals(0, ai.intValue());
+ ai.increment();
+ assertEquals(1, ai.intValue());
+ }
+
+ /**
+ * longValue returns current value.
+ */
+ public void testLongValue() {
+ LongAdder ai = new LongAdder();
+ assertEquals(0, ai.longValue());
+ ai.increment();
+ assertEquals(1, ai.longValue());
+ }
+
+ /**
+ * floatValue returns current value.
+ */
+ public void testFloatValue() {
+ LongAdder ai = new LongAdder();
+ assertEquals(0.0f, ai.floatValue());
+ ai.increment();
+ assertEquals(1.0f, ai.floatValue());
+ }
+
+ /**
+ * doubleValue returns current value.
+ */
+ public void testDoubleValue() {
+ LongAdder ai = new LongAdder();
+ assertEquals(0.0, ai.doubleValue());
+ ai.increment();
+ assertEquals(1.0, ai.doubleValue());
+ }
+
+ /**
+ * adds by multiple threads produce correct sum
+ */
+ public void testAddAndSumMT() throws Throwable {
+ final int incs = 1000000;
+ final int nthreads = 4;
+ final ExecutorService pool = Executors.newCachedThreadPool();
+ LongAdder a = new LongAdder();
+ CyclicBarrier barrier = new CyclicBarrier(nthreads + 1);
+ for (int i = 0; i < nthreads; ++i)
+ pool.execute(new AdderTask(a, barrier, incs));
+ barrier.await();
+ barrier.await();
+ long total = (long)nthreads * incs;
+ long sum = a.sum();
+ assertEquals(sum, total);
+ pool.shutdown();
+ }
+
+ static final class AdderTask implements Runnable {
+ final LongAdder adder;
+ final CyclicBarrier barrier;
+ final int incs;
+ volatile long result;
+ AdderTask(LongAdder adder, CyclicBarrier barrier, int incs) {
+ this.adder = adder;
+ this.barrier = barrier;
+ this.incs = incs;
+ }
+
+ public void run() {
+ try {
+ barrier.await();
+ LongAdder a = adder;
+ for (int i = 0; i < incs; ++i)
+ a.add(1L);
+ result = a.sum();
+ barrier.await();
+ } catch (Throwable t) { throw new Error(t); }
+ }
+ }
+
+}
diff --git a/jsr166-tests/src/test/java/jsr166/PhaserTest.java b/jsr166-tests/src/test/java/jsr166/PhaserTest.java
index 42d72f4..673e556 100644
--- a/jsr166-tests/src/test/java/jsr166/PhaserTest.java
+++ b/jsr166-tests/src/test/java/jsr166/PhaserTest.java
@@ -28,7 +28,7 @@
// main(suite(), args);
// }
// public static Test suite() {
- // return new TestSuite(...);
+ // return new TestSuite(PhaserTest.class);
// }
private static final int maxParties = 65535;
@@ -342,8 +342,8 @@
* registered or unarrived parties would become negative
*/
public void testArriveAndDeregister1() {
+ Phaser phaser = new Phaser();
try {
- Phaser phaser = new Phaser();
phaser.arriveAndDeregister();
shouldThrow();
} catch (IllegalStateException success) {}
@@ -629,11 +629,11 @@
threads.add(newStartedThread(new CheckedRunnable() {
public void realRun() {
for (int k = 0; k < 3; k++) {
- assertEquals(2*k+1, phaser.arriveAndAwaitAdvance());
+ assertEquals(2 * k + 1, phaser.arriveAndAwaitAdvance());
count.incrementAndGet();
- assertEquals(2*k+1, phaser.arrive());
- assertEquals(2*k+2, phaser.awaitAdvance(2*k+1));
- assertEquals(4*(k+1), count.get());
+ assertEquals(2 * k + 1, phaser.arrive());
+ assertEquals(2 * k + 2, phaser.awaitAdvance(2 * k + 1));
+ assertEquals(4 * (k + 1), count.get());
}}}));
for (Thread thread : threads)
@@ -689,7 +689,7 @@
for (Phaser phaser : phasers) {
assertEquals(-42, phaser.awaitAdvance(-42));
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42));
- assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, SMALL_DELAY_MS, MILLISECONDS));
+ assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, MEDIUM_DELAY_MS, MILLISECONDS));
}
for (Phaser child : onePartyChildren)
@@ -697,10 +697,10 @@
for (Phaser phaser : phasers) {
assertEquals(-42, phaser.awaitAdvance(-42));
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42));
- assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, SMALL_DELAY_MS, MILLISECONDS));
+ assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, MEDIUM_DELAY_MS, MILLISECONDS));
assertEquals(1, phaser.awaitAdvance(0));
assertEquals(1, phaser.awaitAdvanceInterruptibly(0));
- assertEquals(1, phaser.awaitAdvanceInterruptibly(0, SMALL_DELAY_MS, MILLISECONDS));
+ assertEquals(1, phaser.awaitAdvanceInterruptibly(0, MEDIUM_DELAY_MS, MILLISECONDS));
}
for (Phaser child : onePartyChildren)
@@ -708,13 +708,13 @@
for (Phaser phaser : phasers) {
assertEquals(-42, phaser.awaitAdvance(-42));
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42));
- assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, SMALL_DELAY_MS, MILLISECONDS));
+ assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, MEDIUM_DELAY_MS, MILLISECONDS));
assertEquals(2, phaser.awaitAdvance(0));
assertEquals(2, phaser.awaitAdvanceInterruptibly(0));
- assertEquals(2, phaser.awaitAdvanceInterruptibly(0, SMALL_DELAY_MS, MILLISECONDS));
+ assertEquals(2, phaser.awaitAdvanceInterruptibly(0, MEDIUM_DELAY_MS, MILLISECONDS));
assertEquals(2, phaser.awaitAdvance(1));
assertEquals(2, phaser.awaitAdvanceInterruptibly(1));
- assertEquals(2, phaser.awaitAdvanceInterruptibly(1, SMALL_DELAY_MS, MILLISECONDS));
+ assertEquals(2, phaser.awaitAdvanceInterruptibly(1, MEDIUM_DELAY_MS, MILLISECONDS));
}
}
@@ -752,8 +752,8 @@
* unarrived parties
*/
public void testArriveAndAwaitAdvance1() {
+ Phaser phaser = new Phaser();
try {
- Phaser phaser = new Phaser();
phaser.arriveAndAwaitAdvance();
shouldThrow();
} catch (IllegalStateException success) {}
diff --git a/jsr166-tests/src/test/java/jsr166/PriorityBlockingQueueTest.java b/jsr166-tests/src/test/java/jsr166/PriorityBlockingQueueTest.java
index 64c3b3a..41b06f5 100644
--- a/jsr166-tests/src/test/java/jsr166/PriorityBlockingQueueTest.java
+++ b/jsr166-tests/src/test/java/jsr166/PriorityBlockingQueueTest.java
@@ -27,7 +27,7 @@
public class PriorityBlockingQueueTest extends JSR166TestCase {
- // android-note: These tests have been moved into their own separate
+ // android-note: These tests have been moved into their own separate
// classes to work around CTS issues.
//
// public static class Generic extends BlockingQueueTest {
@@ -35,17 +35,19 @@
// return new PriorityBlockingQueue();
// }
// }
- //
+
// public static class InitialCapacity extends BlockingQueueTest {
// protected BlockingQueue emptyCollection() {
// return new PriorityBlockingQueue(SIZE);
// }
// }
+
+ // 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 newTestSuite(PriorityBlockingQueueTest.class,
// new Generic().testSuite(),
@@ -67,7 +69,7 @@
PriorityBlockingQueue<Integer> q =
new PriorityBlockingQueue<Integer>(n);
assertTrue(q.isEmpty());
- for (int i = n-1; i >= 0; i -= 2)
+ for (int i = n - 1; i >= 0; i -= 2)
assertTrue(q.offer(new Integer(i)));
for (int i = (n & 1); i < n; i += 2)
assertTrue(q.offer(new Integer(i)));
@@ -121,7 +123,7 @@
*/
public void testConstructor5() {
Integer[] ints = new Integer[SIZE];
- for (int i = 0; i < SIZE-1; ++i)
+ for (int i = 0; i < SIZE - 1; ++i)
ints[i] = i;
Collection<Integer> elements = Arrays.asList(ints);
try {
@@ -153,7 +155,7 @@
for (int i = 0; i < SIZE; ++i)
ints[i] = new Integer(i);
q.addAll(Arrays.asList(ints));
- for (int i = SIZE-1; i >= 0; --i)
+ for (int i = SIZE - 1; i >= 0; --i)
assertEquals(ints[i], q.poll());
}
@@ -225,8 +227,8 @@
* addAll(this) throws IAE
*/
public void testAddAllSelf() {
+ PriorityBlockingQueue q = populatedQueue(SIZE);
try {
- PriorityBlockingQueue q = populatedQueue(SIZE);
q.addAll(q);
shouldThrow();
} catch (IllegalArgumentException success) {}
@@ -237,11 +239,11 @@
* possibly adding some elements
*/
public void testAddAll3() {
+ PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
+ Integer[] ints = new Integer[SIZE];
+ for (int i = 0; i < SIZE - 1; ++i)
+ ints[i] = new Integer(i);
try {
- PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
- Integer[] ints = new Integer[SIZE];
- for (int i = 0; i < SIZE-1; ++i)
- ints[i] = new Integer(i);
q.addAll(Arrays.asList(ints));
shouldThrow();
} catch (NullPointerException success) {}
@@ -253,7 +255,7 @@
public void testAddAll5() {
Integer[] empty = new Integer[0];
Integer[] ints = new Integer[SIZE];
- for (int i = SIZE-1; i >= 0; --i)
+ for (int i = SIZE - 1; i >= 0; --i)
ints[i] = new Integer(i);
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
assertFalse(q.addAll(Arrays.asList(empty)));
@@ -398,25 +400,23 @@
final CountDownLatch aboutToWait = new CountDownLatch(1);
Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
+ long startTime = System.nanoTime();
for (int i = 0; i < SIZE; ++i) {
- long t0 = System.nanoTime();
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
- assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
}
- long t0 = System.nanoTime();
aboutToWait.countDown();
try {
q.poll(LONG_DELAY_MS, MILLISECONDS);
shouldThrow();
} catch (InterruptedException success) {
- assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
+ assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
}
}});
aboutToWait.await();
- waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
+ waitForThreadToEnterWaitState(t, LONG_DELAY_MS);
t.interrupt();
- awaitTermination(t, MEDIUM_DELAY_MS);
+ awaitTermination(t);
}
/**
@@ -517,7 +517,7 @@
assertTrue(changed);
assertTrue(q.containsAll(p));
- assertEquals(SIZE-i, q.size());
+ assertEquals(SIZE - i, q.size());
p.remove();
}
}
@@ -530,7 +530,7 @@
PriorityBlockingQueue q = populatedQueue(SIZE);
PriorityBlockingQueue p = populatedQueue(i);
assertTrue(q.removeAll(p));
- assertEquals(SIZE-i, q.size());
+ assertEquals(SIZE - i, q.size());
for (int j = 0; j < i; ++j) {
Integer x = (Integer)(p.remove());
assertFalse(q.contains(x));
@@ -629,22 +629,22 @@
public void testPollInExecutor() {
final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
final CheckedBarrier threadsStarted = new CheckedBarrier(2);
- ExecutorService executor = Executors.newFixedThreadPool(2);
- executor.execute(new CheckedRunnable() {
- public void realRun() throws InterruptedException {
- assertNull(q.poll());
- threadsStarted.await();
- assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
- checkEmpty(q);
- }});
+ final ExecutorService executor = Executors.newFixedThreadPool(2);
+ try (PoolCleaner cleaner = cleaner(executor)) {
+ executor.execute(new CheckedRunnable() {
+ public void realRun() throws InterruptedException {
+ assertNull(q.poll());
+ threadsStarted.await();
+ assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
+ checkEmpty(q);
+ }});
- executor.execute(new CheckedRunnable() {
- public void realRun() throws InterruptedException {
- threadsStarted.await();
- q.put(one);
- }});
-
- joinPool(executor);
+ executor.execute(new CheckedRunnable() {
+ public void realRun() throws InterruptedException {
+ threadsStarted.await();
+ q.put(one);
+ }});
+ }
}
/**
@@ -694,7 +694,7 @@
final PriorityBlockingQueue q = populatedQueue(SIZE);
Thread t = new Thread(new CheckedRunnable() {
public void realRun() {
- q.put(new Integer(SIZE+1));
+ q.put(new Integer(SIZE + 1));
}});
t.start();
@@ -711,7 +711,7 @@
* drainTo(c, n) empties first min(n, size) elements of queue into c
*/
public void testDrainToN() {
- PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE*2);
+ PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE * 2);
for (int i = 0; i < SIZE + 2; ++i) {
for (int j = 0; j < SIZE; j++)
assertTrue(q.offer(new Integer(j)));
@@ -719,7 +719,7 @@
q.drainTo(l, i);
int k = (i < SIZE) ? i : SIZE;
assertEquals(k, l.size());
- assertEquals(SIZE-k, q.size());
+ assertEquals(SIZE - k, q.size());
for (int j = 0; j < k; ++j)
assertEquals(l.get(j), new Integer(j));
do {} while (q.poll() != null);
diff --git a/jsr166-tests/src/test/java/jsr166/PriorityQueueTest.java b/jsr166-tests/src/test/java/jsr166/PriorityQueueTest.java
index 88cdd37..f64ef68 100644
--- a/jsr166-tests/src/test/java/jsr166/PriorityQueueTest.java
+++ b/jsr166-tests/src/test/java/jsr166/PriorityQueueTest.java
@@ -27,7 +27,7 @@
// main(suite(), args);
// }
// public static Test suite() {
- // return new TestSuite(...);
+ // return new TestSuite(PriorityQueueTest.class);
// }
static class MyReverseComparator implements Comparator {
@@ -43,7 +43,7 @@
private PriorityQueue<Integer> populatedQueue(int n) {
PriorityQueue<Integer> q = new PriorityQueue<Integer>(n);
assertTrue(q.isEmpty());
- for (int i = n-1; i >= 0; i -= 2)
+ for (int i = n - 1; i >= 0; i -= 2)
assertTrue(q.offer(new Integer(i)));
for (int i = (n & 1); i < n; i += 2)
assertTrue(q.offer(new Integer(i)));
@@ -84,8 +84,7 @@
*/
public void testConstructor4() {
try {
- Integer[] ints = new Integer[SIZE];
- new PriorityQueue(Arrays.asList(ints));
+ new PriorityQueue(Arrays.asList(new Integer[SIZE]));
shouldThrow();
} catch (NullPointerException success) {}
}
@@ -94,10 +93,10 @@
* Initializing from Collection with some null elements throws NPE
*/
public void testConstructor5() {
+ Integer[] ints = new Integer[SIZE];
+ for (int i = 0; i < SIZE - 1; ++i)
+ ints[i] = new Integer(i);
try {
- Integer[] ints = new Integer[SIZE];
- for (int i = 0; i < SIZE-1; ++i)
- ints[i] = new Integer(i);
new PriorityQueue(Arrays.asList(ints));
shouldThrow();
} catch (NullPointerException success) {}
@@ -126,7 +125,7 @@
for (int i = 0; i < SIZE; ++i)
ints[i] = new Integer(i);
q.addAll(Arrays.asList(ints));
- for (int i = SIZE-1; i >= 0; --i)
+ for (int i = SIZE - 1; i >= 0; --i)
assertEquals(ints[i], q.poll());
}
@@ -150,7 +149,7 @@
public void testSize() {
PriorityQueue q = populatedQueue(SIZE);
for (int i = 0; i < SIZE; ++i) {
- assertEquals(SIZE-i, q.size());
+ assertEquals(SIZE - i, q.size());
q.remove();
}
for (int i = 0; i < SIZE; ++i) {
@@ -163,8 +162,8 @@
* offer(null) throws NPE
*/
public void testOfferNull() {
+ PriorityQueue q = new PriorityQueue(1);
try {
- PriorityQueue q = new PriorityQueue(1);
q.offer(null);
shouldThrow();
} catch (NullPointerException success) {}
@@ -174,8 +173,8 @@
* add(null) throws NPE
*/
public void testAddNull() {
+ PriorityQueue q = new PriorityQueue(1);
try {
- PriorityQueue q = new PriorityQueue(1);
q.add(null);
shouldThrow();
} catch (NullPointerException success) {}
@@ -217,8 +216,8 @@
* addAll(null) throws NPE
*/
public void testAddAll1() {
+ PriorityQueue q = new PriorityQueue(1);
try {
- PriorityQueue q = new PriorityQueue(1);
q.addAll(null);
shouldThrow();
} catch (NullPointerException success) {}
@@ -228,10 +227,9 @@
* addAll of a collection with null elements throws NPE
*/
public void testAddAll2() {
+ PriorityQueue q = new PriorityQueue(SIZE);
try {
- PriorityQueue q = new PriorityQueue(SIZE);
- Integer[] ints = new Integer[SIZE];
- q.addAll(Arrays.asList(ints));
+ q.addAll(Arrays.asList(new Integer[SIZE]));
shouldThrow();
} catch (NullPointerException success) {}
}
@@ -241,11 +239,11 @@
* possibly adding some elements
*/
public void testAddAll3() {
+ PriorityQueue q = new PriorityQueue(SIZE);
+ Integer[] ints = new Integer[SIZE];
+ for (int i = 0; i < SIZE - 1; ++i)
+ ints[i] = new Integer(i);
try {
- PriorityQueue q = new PriorityQueue(SIZE);
- Integer[] ints = new Integer[SIZE];
- for (int i = 0; i < SIZE-1; ++i)
- ints[i] = new Integer(i);
q.addAll(Arrays.asList(ints));
shouldThrow();
} catch (NullPointerException success) {}
@@ -258,7 +256,7 @@
Integer[] empty = new Integer[0];
Integer[] ints = new Integer[SIZE];
for (int i = 0; i < SIZE; ++i)
- ints[i] = new Integer(SIZE-1-i);
+ ints[i] = new Integer(SIZE - 1 - i);
PriorityQueue q = new PriorityQueue(SIZE);
assertFalse(q.addAll(Arrays.asList(empty)));
assertTrue(q.addAll(Arrays.asList(ints)));
@@ -329,14 +327,14 @@
assertTrue(q.contains(i));
assertTrue(q.remove(i));
assertFalse(q.contains(i));
- assertTrue(q.contains(i-1));
+ assertTrue(q.contains(i - 1));
}
for (int i = 0; i < SIZE; i += 2) {
assertTrue(q.contains(i));
assertTrue(q.remove(i));
assertFalse(q.contains(i));
- assertFalse(q.remove(i+1));
- assertFalse(q.contains(i+1));
+ assertFalse(q.remove(i + 1));
+ assertFalse(q.contains(i + 1));
}
assertTrue(q.isEmpty());
}
@@ -395,7 +393,7 @@
assertTrue(changed);
assertTrue(q.containsAll(p));
- assertEquals(SIZE-i, q.size());
+ assertEquals(SIZE - i, q.size());
p.remove();
}
}
@@ -408,7 +406,7 @@
PriorityQueue q = populatedQueue(SIZE);
PriorityQueue p = populatedQueue(i);
assertTrue(q.removeAll(p));
- assertEquals(SIZE-i, q.size());
+ assertEquals(SIZE - i, q.size());
for (int j = 0; j < i; ++j) {
Integer x = (Integer)(p.remove());
assertFalse(q.contains(x));
diff --git a/jsr166-tests/src/test/java/jsr166/RecursiveActionTest.java b/jsr166-tests/src/test/java/jsr166/RecursiveActionTest.java
index 1c3bba8..c8e33be 100644
--- a/jsr166-tests/src/test/java/jsr166/RecursiveActionTest.java
+++ b/jsr166-tests/src/test/java/jsr166/RecursiveActionTest.java
@@ -32,7 +32,7 @@
// main(suite(), args);
// }
// public static Test suite() {
- // return new TestSuite(...);
+ // return new TestSuite(RecursiveActionTest.class);
// }
private static ForkJoinPool mainPool() {
@@ -50,14 +50,12 @@
}
private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) {
- try {
+ try (PoolCleaner cleaner = cleaner(pool)) {
checkNotDone(a);
assertNull(pool.invoke(a));
checkCompletedNormally(a);
- } finally {
- joinPool(pool);
}
}
@@ -429,12 +427,12 @@
t = newStartedThread(r);
testInvokeOnPool(mainPool(), a);
- awaitTermination(t, LONG_DELAY_MS);
+ awaitTermination(t);
a.reinitialize();
t = newStartedThread(r);
testInvokeOnPool(singletonPool(), a);
- awaitTermination(t, LONG_DELAY_MS);
+ awaitTermination(t);
}
/**
diff --git a/jsr166-tests/src/test/java/jsr166/RecursiveTaskTest.java b/jsr166-tests/src/test/java/jsr166/RecursiveTaskTest.java
index 7783370..2c07c2a 100644
--- a/jsr166-tests/src/test/java/jsr166/RecursiveTaskTest.java
+++ b/jsr166-tests/src/test/java/jsr166/RecursiveTaskTest.java
@@ -28,7 +28,7 @@
// main(suite(), args);
// }
// public static Test suite() {
- // return new TestSuite(...);
+ // return new TestSuite(RecursiveTaskTest.class);
// }
private static ForkJoinPool mainPool() {
@@ -46,15 +46,13 @@
}
private <T> T testInvokeOnPool(ForkJoinPool pool, RecursiveTask<T> a) {
- try {
+ try (PoolCleaner cleaner = cleaner(pool)) {
checkNotDone(a);
T result = pool.invoke(a);
checkCompletedNormally(a, result);
return result;
- } finally {
- joinPool(pool);
}
}
@@ -335,6 +333,8 @@
FibTask f = new FibTask(8);
assertSame(f, f.fork());
helpQuiesce();
+ while (!f.isDone()) // wait out race
+ ;
assertEquals(0, getQueuedTaskCount());
checkCompletedNormally(f, 21);
return NoResult;
diff --git a/jsr166-tests/src/test/java/jsr166/ReentrantLockTest.java b/jsr166-tests/src/test/java/jsr166/ReentrantLockTest.java
index 17eaf76..0024ff3 100644
--- a/jsr166-tests/src/test/java/jsr166/ReentrantLockTest.java
+++ b/jsr166-tests/src/test/java/jsr166/ReentrantLockTest.java
@@ -30,8 +30,9 @@
// main(suite(), args);
// }
// public static Test suite() {
- // return new TestSuite(...);
+ // return new TestSuite(ReentrantLockTest.class);
// }
+
/**
* A checked runnable calling lockInterruptibly
*/
@@ -150,7 +151,7 @@
enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil }
/**
- * Awaits condition using the specified AwaitMethod.
+ * Awaits condition "indefinitely" using the specified AwaitMethod.
*/
void await(Condition c, AwaitMethod awaitMethod)
throws InterruptedException {
@@ -163,9 +164,10 @@
assertTrue(c.await(timeoutMillis, MILLISECONDS));
break;
case awaitNanos:
- long nanosTimeout = MILLISECONDS.toNanos(timeoutMillis);
- long nanosRemaining = c.awaitNanos(nanosTimeout);
- assertTrue(nanosRemaining > 0);
+ long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis);
+ long nanosRemaining = c.awaitNanos(timeoutNanos);
+ assertTrue(nanosRemaining > timeoutNanos / 2);
+ assertTrue(nanosRemaining <= timeoutNanos);
break;
case awaitUntil:
assertTrue(c.awaitUntil(delayedDate(timeoutMillis)));
@@ -428,7 +430,7 @@
}
for (int i = SIZE; i > 0; i--) {
lock.unlock();
- assertEquals(i-1, lock.getHoldCount());
+ assertEquals(i - 1, lock.getHoldCount());
}
}
@@ -568,11 +570,11 @@
final ReentrantLock lock = new ReentrantLock(fair);
final Condition c = lock.newCondition();
lock.lock();
- long startTime = System.nanoTime();
- long timeoutMillis = 10;
- java.util.Date d = new java.util.Date();
- assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + timeoutMillis)));
- assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
+ // We shouldn't assume that nanoTime and currentTimeMillis
+ // use the same time source, so don't use nanoTime here.
+ java.util.Date delayedDate = delayedDate(timeoutMillis());
+ assertFalse(c.awaitUntil(delayedDate));
+ assertTrue(new java.util.Date().getTime() >= delayedDate.getTime());
lock.unlock();
} catch (InterruptedException fail) { threadUnexpectedException(fail); }
}
diff --git a/jsr166-tests/src/test/java/jsr166/ReentrantReadWriteLockTest.java b/jsr166-tests/src/test/java/jsr166/ReentrantReadWriteLockTest.java
index 7ef8ea3..918f45d 100644
--- a/jsr166-tests/src/test/java/jsr166/ReentrantReadWriteLockTest.java
+++ b/jsr166-tests/src/test/java/jsr166/ReentrantReadWriteLockTest.java
@@ -31,7 +31,7 @@
// main(suite(), args);
// }
// public static Test suite() {
- // return new TestSuite(...);
+ // return new TestSuite(ReentrantReadWriteLockTest.class);
// }
/**
@@ -160,24 +160,26 @@
enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil }
/**
- * Awaits condition using the specified AwaitMethod.
+ * Awaits condition "indefinitely" using the specified AwaitMethod.
*/
void await(Condition c, AwaitMethod awaitMethod)
throws InterruptedException {
+ long timeoutMillis = 2 * LONG_DELAY_MS;
switch (awaitMethod) {
case await:
c.await();
break;
case awaitTimed:
- assertTrue(c.await(2 * LONG_DELAY_MS, MILLISECONDS));
+ assertTrue(c.await(timeoutMillis, MILLISECONDS));
break;
case awaitNanos:
- long nanosRemaining = c.awaitNanos(MILLISECONDS.toNanos(2 * LONG_DELAY_MS));
- assertTrue(nanosRemaining > 0);
+ long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis);
+ long nanosRemaining = c.awaitNanos(timeoutNanos);
+ assertTrue(nanosRemaining > timeoutNanos / 2);
+ assertTrue(nanosRemaining <= timeoutNanos);
break;
case awaitUntil:
- java.util.Date d = new java.util.Date();
- assertTrue(c.awaitUntil(new java.util.Date(d.getTime() + 2 * LONG_DELAY_MS)));
+ assertTrue(c.awaitUntil(delayedDate(timeoutMillis)));
break;
default:
throw new AssertionError();
@@ -241,7 +243,7 @@
}
for (int i = SIZE; i > 0; i--) {
lock.writeLock().unlock();
- assertEquals(i-1,lock.getWriteHoldCount());
+ assertEquals(i - 1,lock.getWriteHoldCount());
}
}
@@ -258,7 +260,7 @@
}
for (int i = SIZE; i > 0; i--) {
lock.writeLock().unlock();
- assertEquals(i-1,lock.writeLock().getHoldCount());
+ assertEquals(i - 1,lock.writeLock().getHoldCount());
}
}
@@ -275,7 +277,7 @@
}
for (int i = SIZE; i > 0; i--) {
lock.readLock().unlock();
- assertEquals(i-1,lock.getReadHoldCount());
+ assertEquals(i - 1,lock.getReadHoldCount());
}
}
@@ -972,11 +974,11 @@
new ReentrantReadWriteLock(fair);
final Condition c = lock.writeLock().newCondition();
lock.writeLock().lock();
- long startTime = System.nanoTime();
- long timeoutMillis = 10;
- java.util.Date d = new java.util.Date();
- assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + timeoutMillis)));
- assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
+ // We shouldn't assume that nanoTime and currentTimeMillis
+ // use the same time source, so don't use nanoTime here.
+ java.util.Date delayedDate = delayedDate(timeoutMillis());
+ assertFalse(c.awaitUntil(delayedDate));
+ assertTrue(new java.util.Date().getTime() >= delayedDate.getTime());
lock.writeLock().unlock();
} catch (InterruptedException fail) { threadUnexpectedException(fail); }
}
diff --git a/jsr166-tests/src/test/java/jsr166/ScheduledExecutorSubclassTest.java b/jsr166-tests/src/test/java/jsr166/ScheduledExecutorSubclassTest.java
index a93feea..194dd58 100644
--- a/jsr166-tests/src/test/java/jsr166/ScheduledExecutorSubclassTest.java
+++ b/jsr166-tests/src/test/java/jsr166/ScheduledExecutorSubclassTest.java
@@ -7,11 +7,15 @@
package jsr166;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
+import static java.util.concurrent.TimeUnit.NANOSECONDS;
+import static java.util.concurrent.TimeUnit.SECONDS;
import java.util.ArrayList;
+import java.util.HashSet;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable;
+import java.util.concurrent.CancellationException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Delayed;
import java.util.concurrent.ExecutionException;
@@ -27,7 +31,9 @@
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.AtomicLong;
import junit.framework.Test;
import junit.framework.TestSuite;
@@ -40,7 +46,7 @@
// main(suite(), args);
// }
// public static Test suite() {
- // return new TestSuite(...);
+ // return new TestSuite(ScheduledExecutorSubclassTest.class);
// }
static class CustomTask<V> implements RunnableScheduledFuture<V> {
@@ -101,17 +107,13 @@
* execute successfully executes a runnable
*/
public void testExecute() throws InterruptedException {
- CustomExecutor p = new CustomExecutor(1);
- final CountDownLatch done = new CountDownLatch(1);
- final Runnable task = new CheckedRunnable() {
- public void realRun() {
- done.countDown();
- }};
- try {
+ final CustomExecutor p = new CustomExecutor(1);
+ try (PoolCleaner cleaner = cleaner(p)) {
+ final CountDownLatch done = new CountDownLatch(1);
+ final Runnable task = new CheckedRunnable() {
+ public void realRun() { done.countDown(); }};
p.execute(task);
- assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS));
- } finally {
- joinPool(p);
+ await(done);
}
}
@@ -119,10 +121,10 @@
* delayed schedule of callable successfully executes after delay
*/
public void testSchedule1() throws Exception {
- CustomExecutor p = new CustomExecutor(1);
- final long startTime = System.nanoTime();
final CountDownLatch done = new CountDownLatch(1);
- try {
+ final CustomExecutor p = new CustomExecutor(1);
+ try (PoolCleaner cleaner = cleaner(p, done)) {
+ final long startTime = System.nanoTime();
Callable task = new CheckedCallable<Boolean>() {
public Boolean realCall() {
done.countDown();
@@ -132,9 +134,6 @@
Future f = p.schedule(task, timeoutMillis(), MILLISECONDS);
assertSame(Boolean.TRUE, f.get());
assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
- assertTrue(done.await(0L, MILLISECONDS));
- } finally {
- joinPool(p);
}
}
@@ -142,10 +141,10 @@
* delayed schedule of runnable successfully executes after delay
*/
public void testSchedule3() throws Exception {
- CustomExecutor p = new CustomExecutor(1);
- final long startTime = System.nanoTime();
- final CountDownLatch done = new CountDownLatch(1);
- try {
+ final CustomExecutor p = new CustomExecutor(1);
+ try (PoolCleaner cleaner = cleaner(p)) {
+ final long startTime = System.nanoTime();
+ final CountDownLatch done = new CountDownLatch(1);
Runnable task = new CheckedRunnable() {
public void realRun() {
done.countDown();
@@ -155,8 +154,6 @@
await(done);
assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
- } finally {
- joinPool(p);
}
}
@@ -164,10 +161,10 @@
* scheduleAtFixedRate executes runnable after given initial delay
*/
public void testSchedule4() throws InterruptedException {
- CustomExecutor p = new CustomExecutor(1);
- final long startTime = System.nanoTime();
- final CountDownLatch done = new CountDownLatch(1);
- try {
+ final CustomExecutor p = new CustomExecutor(1);
+ try (PoolCleaner cleaner = cleaner(p)) {
+ final long startTime = System.nanoTime();
+ final CountDownLatch done = new CountDownLatch(1);
Runnable task = new CheckedRunnable() {
public void realRun() {
done.countDown();
@@ -179,8 +176,6 @@
await(done);
assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
f.cancel(true);
- } finally {
- joinPool(p);
}
}
@@ -188,10 +183,10 @@
* scheduleWithFixedDelay executes runnable after given initial delay
*/
public void testSchedule5() throws InterruptedException {
- CustomExecutor p = new CustomExecutor(1);
- final long startTime = System.nanoTime();
- final CountDownLatch done = new CountDownLatch(1);
- try {
+ final CustomExecutor p = new CustomExecutor(1);
+ try (PoolCleaner cleaner = cleaner(p)) {
+ final long startTime = System.nanoTime();
+ final CountDownLatch done = new CountDownLatch(1);
Runnable task = new CheckedRunnable() {
public void realRun() {
done.countDown();
@@ -203,8 +198,6 @@
await(done);
assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
f.cancel(true);
- } finally {
- joinPool(p);
}
}
@@ -214,58 +207,77 @@
}
/**
- * scheduleAtFixedRate executes series of tasks at given rate
+ * scheduleAtFixedRate executes series of tasks at given rate.
+ * Eventually, it must hold that:
+ * cycles - 1 <= elapsedMillis/delay < cycles
*/
public void testFixedRateSequence() throws InterruptedException {
- CustomExecutor p = new CustomExecutor(1);
- try {
+ final CustomExecutor p = new CustomExecutor(1);
+ try (PoolCleaner cleaner = cleaner(p)) {
for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) {
- long startTime = System.nanoTime();
- int cycles = 10;
+ final long startTime = System.nanoTime();
+ final int cycles = 8;
final CountDownLatch done = new CountDownLatch(cycles);
- Runnable task = new CheckedRunnable() {
+ final Runnable task = new CheckedRunnable() {
public void realRun() { done.countDown(); }};
- ScheduledFuture h =
+ final ScheduledFuture periodicTask =
p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS);
- done.await();
- h.cancel(true);
- double normalizedTime =
- (double) millisElapsedSince(startTime) / delay;
- if (normalizedTime >= cycles - 1 &&
- normalizedTime <= cycles)
+ final int totalDelayMillis = (cycles - 1) * delay;
+ await(done, totalDelayMillis + LONG_DELAY_MS);
+ periodicTask.cancel(true);
+ final long elapsedMillis = millisElapsedSince(startTime);
+ assertTrue(elapsedMillis >= totalDelayMillis);
+ if (elapsedMillis <= cycles * delay)
return;
+ // else retry with longer delay
}
- throw new AssertionError("unexpected execution rate");
- } finally {
- joinPool(p);
+ fail("unexpected execution rate");
}
}
/**
- * scheduleWithFixedDelay executes series of tasks with given period
+ * scheduleWithFixedDelay executes series of tasks with given period.
+ * Eventually, it must hold that each task starts at least delay and at
+ * most 2 * delay after the termination of the previous task.
*/
public void testFixedDelaySequence() throws InterruptedException {
- CustomExecutor p = new CustomExecutor(1);
- try {
+ final CustomExecutor p = new CustomExecutor(1);
+ try (PoolCleaner cleaner = cleaner(p)) {
for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) {
- long startTime = System.nanoTime();
- int cycles = 10;
+ final long startTime = System.nanoTime();
+ final AtomicLong previous = new AtomicLong(startTime);
+ final AtomicBoolean tryLongerDelay = new AtomicBoolean(false);
+ final int cycles = 8;
final CountDownLatch done = new CountDownLatch(cycles);
- Runnable task = new CheckedRunnable() {
- public void realRun() { done.countDown(); }};
- ScheduledFuture h =
+ final int d = delay;
+ final Runnable task = new CheckedRunnable() {
+ public void realRun() {
+ long now = System.nanoTime();
+ long elapsedMillis
+ = NANOSECONDS.toMillis(now - previous.get());
+ if (done.getCount() == cycles) { // first execution
+ if (elapsedMillis >= d)
+ tryLongerDelay.set(true);
+ } else {
+ assertTrue(elapsedMillis >= d);
+ if (elapsedMillis >= 2 * d)
+ tryLongerDelay.set(true);
+ }
+ previous.set(now);
+ done.countDown();
+ }};
+ final ScheduledFuture periodicTask =
p.scheduleWithFixedDelay(task, 0, delay, MILLISECONDS);
- done.await();
- h.cancel(true);
- double normalizedTime =
- (double) millisElapsedSince(startTime) / delay;
- if (normalizedTime >= cycles - 1 &&
- normalizedTime <= cycles)
+ final int totalDelayMillis = (cycles - 1) * delay;
+ await(done, totalDelayMillis + cycles * LONG_DELAY_MS);
+ periodicTask.cancel(true);
+ final long elapsedMillis = millisElapsedSince(startTime);
+ assertTrue(elapsedMillis >= totalDelayMillis);
+ if (!tryLongerDelay.get())
return;
+ // else retry with longer delay
}
- throw new AssertionError("unexpected execution rate");
- } finally {
- joinPool(p);
+ fail("unexpected execution rate");
}
}
@@ -273,106 +285,107 @@
* execute(null) throws NPE
*/
public void testExecuteNull() throws InterruptedException {
- CustomExecutor se = new CustomExecutor(1);
- try {
- se.execute(null);
- shouldThrow();
- } catch (NullPointerException success) {}
- joinPool(se);
+ final CustomExecutor p = new CustomExecutor(1);
+ try (PoolCleaner cleaner = cleaner(p)) {
+ try {
+ p.execute(null);
+ shouldThrow();
+ } catch (NullPointerException success) {}
+ }
}
/**
* schedule(null) throws NPE
*/
public void testScheduleNull() throws InterruptedException {
- CustomExecutor se = new CustomExecutor(1);
- try {
- TrackedCallable callable = null;
- Future f = se.schedule(callable, SHORT_DELAY_MS, MILLISECONDS);
- shouldThrow();
- } catch (NullPointerException success) {}
- joinPool(se);
+ final CustomExecutor p = new CustomExecutor(1);
+ try (PoolCleaner cleaner = cleaner(p)) {
+ try {
+ TrackedCallable callable = null;
+ Future f = p.schedule(callable, SHORT_DELAY_MS, MILLISECONDS);
+ shouldThrow();
+ } catch (NullPointerException success) {}
+ }
}
/**
* execute throws RejectedExecutionException if shutdown
*/
public void testSchedule1_RejectedExecutionException() {
- CustomExecutor se = new CustomExecutor(1);
- try {
- se.shutdown();
- se.schedule(new NoOpRunnable(),
- MEDIUM_DELAY_MS, MILLISECONDS);
- shouldThrow();
- } catch (RejectedExecutionException success) {
- } catch (SecurityException ok) {
+ final CustomExecutor p = new CustomExecutor(1);
+ try (PoolCleaner cleaner = cleaner(p)) {
+ try {
+ p.shutdown();
+ p.schedule(new NoOpRunnable(),
+ MEDIUM_DELAY_MS, MILLISECONDS);
+ shouldThrow();
+ } catch (RejectedExecutionException success) {
+ } catch (SecurityException ok) {}
}
-
- joinPool(se);
}
/**
* schedule throws RejectedExecutionException if shutdown
*/
public void testSchedule2_RejectedExecutionException() {
- CustomExecutor se = new CustomExecutor(1);
- try {
- se.shutdown();
- se.schedule(new NoOpCallable(),
- MEDIUM_DELAY_MS, MILLISECONDS);
- shouldThrow();
- } catch (RejectedExecutionException success) {
- } catch (SecurityException ok) {
+ final CustomExecutor p = new CustomExecutor(1);
+ try (PoolCleaner cleaner = cleaner(p)) {
+ try {
+ p.shutdown();
+ p.schedule(new NoOpCallable(),
+ MEDIUM_DELAY_MS, MILLISECONDS);
+ shouldThrow();
+ } catch (RejectedExecutionException success) {
+ } catch (SecurityException ok) {}
}
- joinPool(se);
}
/**
* schedule callable throws RejectedExecutionException if shutdown
*/
public void testSchedule3_RejectedExecutionException() {
- CustomExecutor se = new CustomExecutor(1);
- try {
- se.shutdown();
- se.schedule(new NoOpCallable(),
- MEDIUM_DELAY_MS, MILLISECONDS);
- shouldThrow();
- } catch (RejectedExecutionException success) {
- } catch (SecurityException ok) {
+ final CustomExecutor p = new CustomExecutor(1);
+ try (PoolCleaner cleaner = cleaner(p)) {
+ try {
+ p.shutdown();
+ p.schedule(new NoOpCallable(),
+ MEDIUM_DELAY_MS, MILLISECONDS);
+ shouldThrow();
+ } catch (RejectedExecutionException success) {
+ } catch (SecurityException ok) {}
}
- joinPool(se);
}
/**
* scheduleAtFixedRate throws RejectedExecutionException if shutdown
*/
public void testScheduleAtFixedRate1_RejectedExecutionException() {
- CustomExecutor se = new CustomExecutor(1);
- try {
- se.shutdown();
- se.scheduleAtFixedRate(new NoOpRunnable(),
- MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS);
- shouldThrow();
- } catch (RejectedExecutionException success) {
- } catch (SecurityException ok) {
+ final CustomExecutor p = new CustomExecutor(1);
+ try (PoolCleaner cleaner = cleaner(p)) {
+ try {
+ p.shutdown();
+ p.scheduleAtFixedRate(new NoOpRunnable(),
+ MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS);
+ shouldThrow();
+ } catch (RejectedExecutionException success) {
+ } catch (SecurityException ok) {}
}
- joinPool(se);
}
/**
* scheduleWithFixedDelay throws RejectedExecutionException if shutdown
*/
public void testScheduleWithFixedDelay1_RejectedExecutionException() {
- CustomExecutor se = new CustomExecutor(1);
- try {
- se.shutdown();
- se.scheduleWithFixedDelay(new NoOpRunnable(),
- MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS);
- shouldThrow();
- } catch (RejectedExecutionException success) {
- } catch (SecurityException ok) {
+ final CustomExecutor p = new CustomExecutor(1);
+ try (PoolCleaner cleaner = cleaner(p)) {
+ try {
+ p.shutdown();
+ p.scheduleWithFixedDelay(new NoOpRunnable(),
+ MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS);
+ shouldThrow();
+ } catch (RejectedExecutionException success) {
+ } catch (SecurityException ok) {}
}
- joinPool(se);
}
/**
@@ -380,22 +393,19 @@
* thread becomes active
*/
public void testGetActiveCount() throws InterruptedException {
- final ThreadPoolExecutor p = new CustomExecutor(2);
- final CountDownLatch threadStarted = new CountDownLatch(1);
final CountDownLatch done = new CountDownLatch(1);
- try {
+ final ThreadPoolExecutor p = new CustomExecutor(2);
+ try (PoolCleaner cleaner = cleaner(p, done)) {
+ final CountDownLatch threadStarted = new CountDownLatch(1);
assertEquals(0, p.getActiveCount());
p.execute(new CheckedRunnable() {
public void realRun() throws InterruptedException {
threadStarted.countDown();
assertEquals(1, p.getActiveCount());
- done.await();
+ await(done);
}});
- assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
+ await(threadStarted);
assertEquals(1, p.getActiveCount());
- } finally {
- done.countDown();
- joinPool(p);
}
}
@@ -405,10 +415,10 @@
*/
public void testGetCompletedTaskCount() throws InterruptedException {
final ThreadPoolExecutor p = new CustomExecutor(2);
- final CountDownLatch threadStarted = new CountDownLatch(1);
- final CountDownLatch threadProceed = new CountDownLatch(1);
- final CountDownLatch threadDone = new CountDownLatch(1);
- try {
+ try (PoolCleaner cleaner = cleaner(p)) {
+ final CountDownLatch threadStarted = new CountDownLatch(1);
+ final CountDownLatch threadProceed = new CountDownLatch(1);
+ final CountDownLatch threadDone = new CountDownLatch(1);
assertEquals(0, p.getCompletedTaskCount());
p.execute(new CheckedRunnable() {
public void realRun() throws InterruptedException {
@@ -427,8 +437,6 @@
fail("timed out");
Thread.yield();
}
- } finally {
- joinPool(p);
}
}
@@ -436,9 +444,10 @@
* getCorePoolSize returns size given in constructor if not otherwise set
*/
public void testGetCorePoolSize() {
- CustomExecutor p = new CustomExecutor(1);
- assertEquals(1, p.getCorePoolSize());
- joinPool(p);
+ final CustomExecutor p = new CustomExecutor(1);
+ try (PoolCleaner cleaner = cleaner(p)) {
+ assertEquals(1, p.getCorePoolSize());
+ }
}
/**
@@ -447,25 +456,22 @@
*/
public void testGetLargestPoolSize() throws InterruptedException {
final int THREADS = 3;
- final ThreadPoolExecutor p = new CustomExecutor(THREADS);
- final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
final CountDownLatch done = new CountDownLatch(1);
- try {
+ final ThreadPoolExecutor p = new CustomExecutor(THREADS);
+ try (PoolCleaner cleaner = cleaner(p, done)) {
+ final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
assertEquals(0, p.getLargestPoolSize());
for (int i = 0; i < THREADS; i++)
p.execute(new CheckedRunnable() {
public void realRun() throws InterruptedException {
threadsStarted.countDown();
- done.await();
+ await(done);
assertEquals(THREADS, p.getLargestPoolSize());
}});
- assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS));
- assertEquals(THREADS, p.getLargestPoolSize());
- } finally {
- done.countDown();
- joinPool(p);
+ await(threadsStarted);
assertEquals(THREADS, p.getLargestPoolSize());
}
+ assertEquals(THREADS, p.getLargestPoolSize());
}
/**
@@ -473,22 +479,19 @@
* become active
*/
public void testGetPoolSize() throws InterruptedException {
- final ThreadPoolExecutor p = new CustomExecutor(1);
- final CountDownLatch threadStarted = new CountDownLatch(1);
final CountDownLatch done = new CountDownLatch(1);
- try {
+ final ThreadPoolExecutor p = new CustomExecutor(1);
+ try (PoolCleaner cleaner = cleaner(p, done)) {
+ final CountDownLatch threadStarted = new CountDownLatch(1);
assertEquals(0, p.getPoolSize());
p.execute(new CheckedRunnable() {
public void realRun() throws InterruptedException {
threadStarted.countDown();
assertEquals(1, p.getPoolSize());
- done.await();
+ await(done);
}});
- assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
+ await(threadStarted);
assertEquals(1, p.getPoolSize());
- } finally {
- done.countDown();
- joinPool(p);
}
}
@@ -497,58 +500,70 @@
* submitted
*/
public void testGetTaskCount() throws InterruptedException {
- final ThreadPoolExecutor p = new CustomExecutor(1);
- final CountDownLatch threadStarted = new CountDownLatch(1);
+ final int TASKS = 3;
final CountDownLatch done = new CountDownLatch(1);
- final int TASKS = 5;
- try {
+ final ThreadPoolExecutor p = new CustomExecutor(1);
+ try (PoolCleaner cleaner = cleaner(p, done)) {
+ final CountDownLatch threadStarted = new CountDownLatch(1);
assertEquals(0, p.getTaskCount());
- for (int i = 0; i < TASKS; i++)
+ assertEquals(0, p.getCompletedTaskCount());
+ p.execute(new CheckedRunnable() {
+ public void realRun() throws InterruptedException {
+ threadStarted.countDown();
+ await(done);
+ }});
+ await(threadStarted);
+ assertEquals(1, p.getTaskCount());
+ assertEquals(0, p.getCompletedTaskCount());
+ for (int i = 0; i < TASKS; i++) {
+ assertEquals(1 + i, p.getTaskCount());
p.execute(new CheckedRunnable() {
public void realRun() throws InterruptedException {
threadStarted.countDown();
- done.await();
+ assertEquals(1 + TASKS, p.getTaskCount());
+ await(done);
}});
- assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
- assertEquals(TASKS, p.getTaskCount());
- } finally {
- done.countDown();
- joinPool(p);
+ }
+ assertEquals(1 + TASKS, p.getTaskCount());
+ assertEquals(0, p.getCompletedTaskCount());
}
+ assertEquals(1 + TASKS, p.getTaskCount());
+ assertEquals(1 + TASKS, p.getCompletedTaskCount());
}
/**
* getThreadFactory returns factory in constructor if not set
*/
public void testGetThreadFactory() {
- ThreadFactory tf = new SimpleThreadFactory();
- CustomExecutor p = new CustomExecutor(1, tf);
- assertSame(tf, p.getThreadFactory());
- joinPool(p);
+ final ThreadFactory threadFactory = new SimpleThreadFactory();
+ final CustomExecutor p = new CustomExecutor(1, threadFactory);
+ try (PoolCleaner cleaner = cleaner(p)) {
+ assertSame(threadFactory, p.getThreadFactory());
+ }
}
/**
* setThreadFactory sets the thread factory returned by getThreadFactory
*/
public void testSetThreadFactory() {
- ThreadFactory tf = new SimpleThreadFactory();
- CustomExecutor p = new CustomExecutor(1);
- p.setThreadFactory(tf);
- assertSame(tf, p.getThreadFactory());
- joinPool(p);
+ final ThreadFactory threadFactory = new SimpleThreadFactory();
+ final CustomExecutor p = new CustomExecutor(1);
+ try (PoolCleaner cleaner = cleaner(p)) {
+ p.setThreadFactory(threadFactory);
+ assertSame(threadFactory, p.getThreadFactory());
+ }
}
/**
* setThreadFactory(null) throws NPE
*/
public void testSetThreadFactoryNull() {
- CustomExecutor p = new CustomExecutor(1);
- try {
- p.setThreadFactory(null);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
- joinPool(p);
+ final CustomExecutor p = new CustomExecutor(1);
+ try (PoolCleaner cleaner = cleaner(p)) {
+ try {
+ p.setThreadFactory(null);
+ shouldThrow();
+ } catch (NullPointerException success) {}
}
}
@@ -556,91 +571,84 @@
* isShutdown is false before shutdown, true after
*/
public void testIsShutdown() {
- CustomExecutor p = new CustomExecutor(1);
- try {
+ final CustomExecutor p = new CustomExecutor(1);
+ try (PoolCleaner cleaner = cleaner(p)) {
assertFalse(p.isShutdown());
- }
- finally {
try { p.shutdown(); } catch (SecurityException ok) { return; }
+ assertTrue(p.isShutdown());
}
- assertTrue(p.isShutdown());
}
/**
* isTerminated is false before termination, true after
*/
public void testIsTerminated() throws InterruptedException {
- final ThreadPoolExecutor p = new CustomExecutor(1);
- final CountDownLatch threadStarted = new CountDownLatch(1);
final CountDownLatch done = new CountDownLatch(1);
- assertFalse(p.isTerminated());
- try {
+ final ThreadPoolExecutor p = new CustomExecutor(1);
+ try (PoolCleaner cleaner = cleaner(p)) {
+ final CountDownLatch threadStarted = new CountDownLatch(1);
p.execute(new CheckedRunnable() {
public void realRun() throws InterruptedException {
assertFalse(p.isTerminated());
threadStarted.countDown();
- done.await();
+ await(done);
}});
- assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
+ await(threadStarted);
+ assertFalse(p.isTerminated());
assertFalse(p.isTerminating());
done.countDown();
- } finally {
try { p.shutdown(); } catch (SecurityException ok) { return; }
+ assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
+ assertTrue(p.isTerminated());
}
- assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
- assertTrue(p.isTerminated());
}
/**
* isTerminating is not true when running or when terminated
*/
public void testIsTerminating() throws InterruptedException {
- final ThreadPoolExecutor p = new CustomExecutor(1);
- final CountDownLatch threadStarted = new CountDownLatch(1);
final CountDownLatch done = new CountDownLatch(1);
- try {
+ final ThreadPoolExecutor p = new CustomExecutor(1);
+ try (PoolCleaner cleaner = cleaner(p)) {
+ final CountDownLatch threadStarted = new CountDownLatch(1);
assertFalse(p.isTerminating());
p.execute(new CheckedRunnable() {
public void realRun() throws InterruptedException {
assertFalse(p.isTerminating());
threadStarted.countDown();
- done.await();
+ await(done);
}});
- assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
+ await(threadStarted);
assertFalse(p.isTerminating());
done.countDown();
- } finally {
try { p.shutdown(); } catch (SecurityException ok) { return; }
+ assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
+ assertTrue(p.isTerminated());
+ assertFalse(p.isTerminating());
}
- assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
- assertTrue(p.isTerminated());
- assertFalse(p.isTerminating());
}
/**
* getQueue returns the work queue, which contains queued tasks
*/
public void testGetQueue() throws InterruptedException {
- ScheduledThreadPoolExecutor p = new CustomExecutor(1);
- final CountDownLatch threadStarted = new CountDownLatch(1);
final CountDownLatch done = new CountDownLatch(1);
- try {
+ final ScheduledThreadPoolExecutor p = new CustomExecutor(1);
+ try (PoolCleaner cleaner = cleaner(p, done)) {
+ final CountDownLatch threadStarted = new CountDownLatch(1);
ScheduledFuture[] tasks = new ScheduledFuture[5];
for (int i = 0; i < tasks.length; i++) {
Runnable r = new CheckedRunnable() {
public void realRun() throws InterruptedException {
threadStarted.countDown();
- done.await();
+ await(done);
}};
tasks[i] = p.schedule(r, 1, MILLISECONDS);
}
- assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
+ await(threadStarted);
BlockingQueue<Runnable> q = p.getQueue();
assertTrue(q.contains(tasks[tasks.length - 1]));
assertFalse(q.contains(tasks[0]));
- } finally {
- done.countDown();
- joinPool(p);
}
}
@@ -648,20 +656,20 @@
* remove(task) removes queued task, and fails to remove active task
*/
public void testRemove() throws InterruptedException {
- final ScheduledThreadPoolExecutor p = new CustomExecutor(1);
- ScheduledFuture[] tasks = new ScheduledFuture[5];
- final CountDownLatch threadStarted = new CountDownLatch(1);
final CountDownLatch done = new CountDownLatch(1);
- try {
+ final ScheduledThreadPoolExecutor p = new CustomExecutor(1);
+ try (PoolCleaner cleaner = cleaner(p, done)) {
+ ScheduledFuture[] tasks = new ScheduledFuture[5];
+ final CountDownLatch threadStarted = new CountDownLatch(1);
for (int i = 0; i < tasks.length; i++) {
Runnable r = new CheckedRunnable() {
public void realRun() throws InterruptedException {
threadStarted.countDown();
- done.await();
+ await(done);
}};
tasks[i] = p.schedule(r, 1, MILLISECONDS);
}
- assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
+ await(threadStarted);
BlockingQueue<Runnable> q = p.getQueue();
assertFalse(p.remove((Runnable)tasks[0]));
assertTrue(q.contains((Runnable)tasks[4]));
@@ -672,9 +680,6 @@
assertTrue(q.contains((Runnable)tasks[3]));
assertTrue(p.remove((Runnable)tasks[3]));
assertFalse(q.contains((Runnable)tasks[3]));
- } finally {
- done.countDown();
- joinPool(p);
}
}
@@ -682,12 +687,15 @@
* purge removes cancelled tasks from the queue
*/
public void testPurge() throws InterruptedException {
- CustomExecutor p = new CustomExecutor(1);
- ScheduledFuture[] tasks = new ScheduledFuture[5];
- for (int i = 0; i < tasks.length; i++)
- tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(),
- LONG_DELAY_MS, MILLISECONDS);
- try {
+ final ScheduledFuture[] tasks = new ScheduledFuture[5];
+ final Runnable releaser = new Runnable() { public void run() {
+ for (ScheduledFuture task : tasks)
+ if (task != null) task.cancel(true); }};
+ final CustomExecutor p = new CustomExecutor(1);
+ try (PoolCleaner cleaner = cleaner(p, releaser)) {
+ for (int i = 0; i < tasks.length; i++)
+ tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(),
+ LONG_DELAY_MS, MILLISECONDS);
int max = tasks.length;
if (tasks[4].cancel(true)) --max;
if (tasks[3].cancel(true)) --max;
@@ -699,159 +707,185 @@
long count = p.getTaskCount();
if (count == max)
return;
- } while (millisElapsedSince(startTime) < MEDIUM_DELAY_MS);
+ } while (millisElapsedSince(startTime) < LONG_DELAY_MS);
fail("Purge failed to remove cancelled tasks");
- } finally {
- for (ScheduledFuture task : tasks)
- task.cancel(true);
- joinPool(p);
}
}
/**
- * shutdownNow returns a list containing tasks that were not run
+ * shutdownNow returns a list containing tasks that were not run,
+ * and those tasks are drained from the queue
*/
- public void testShutdownNow() {
- CustomExecutor p = new CustomExecutor(1);
- for (int i = 0; i < 5; i++)
- p.schedule(new SmallPossiblyInterruptedRunnable(),
- LONG_DELAY_MS, MILLISECONDS);
+ public void testShutdownNow() throws InterruptedException {
+ final int poolSize = 2;
+ final int count = 5;
+ final AtomicInteger ran = new AtomicInteger(0);
+ final CustomExecutor p = new CustomExecutor(poolSize);
+ final CountDownLatch threadsStarted = new CountDownLatch(poolSize);
+ Runnable waiter = new CheckedRunnable() { public void realRun() {
+ threadsStarted.countDown();
+ try {
+ MILLISECONDS.sleep(2 * LONG_DELAY_MS);
+ } catch (InterruptedException success) {}
+ ran.getAndIncrement();
+ }};
+ for (int i = 0; i < count; i++)
+ p.execute(waiter);
+ await(threadsStarted);
+ assertEquals(poolSize, p.getActiveCount());
+ assertEquals(0, p.getCompletedTaskCount());
+ final List<Runnable> queuedTasks;
try {
- List<Runnable> l = p.shutdownNow();
- assertTrue(p.isShutdown());
- assertEquals(5, l.size());
+ queuedTasks = p.shutdownNow();
} catch (SecurityException ok) {
- // Allowed in case test doesn't have privs
- } finally {
- joinPool(p);
+ return; // Allowed in case test doesn't have privs
}
- }
-
- /**
- * In default setting, shutdown cancels periodic but not delayed
- * tasks at shutdown
- */
- public void testShutdown1() throws InterruptedException {
- CustomExecutor p = new CustomExecutor(1);
- assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
- assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
-
- ScheduledFuture[] tasks = new ScheduledFuture[5];
- for (int i = 0; i < tasks.length; i++)
- tasks[i] = p.schedule(new NoOpRunnable(),
- SHORT_DELAY_MS, MILLISECONDS);
- try { p.shutdown(); } catch (SecurityException ok) { return; }
- BlockingQueue<Runnable> q = p.getQueue();
- for (ScheduledFuture task : tasks) {
- assertFalse(task.isDone());
- assertFalse(task.isCancelled());
- assertTrue(q.contains(task));
- }
- assertTrue(p.isShutdown());
- assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS));
- assertTrue(p.isTerminated());
- for (ScheduledFuture task : tasks) {
- assertTrue(task.isDone());
- assertFalse(task.isCancelled());
- }
- }
-
- /**
- * If setExecuteExistingDelayedTasksAfterShutdownPolicy is false,
- * delayed tasks are cancelled at shutdown
- */
- public void testShutdown2() throws InterruptedException {
- CustomExecutor p = new CustomExecutor(1);
- p.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
- assertFalse(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
- assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
- ScheduledFuture[] tasks = new ScheduledFuture[5];
- for (int i = 0; i < tasks.length; i++)
- tasks[i] = p.schedule(new NoOpRunnable(),
- SHORT_DELAY_MS, MILLISECONDS);
- BlockingQueue q = p.getQueue();
- assertEquals(tasks.length, q.size());
- try { p.shutdown(); } catch (SecurityException ok) { return; }
- assertTrue(p.isShutdown());
- assertTrue(q.isEmpty());
- assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS));
- assertTrue(p.isTerminated());
- for (ScheduledFuture task : tasks) {
- assertTrue(task.isDone());
- assertTrue(task.isCancelled());
- }
- }
-
- /**
- * If setContinueExistingPeriodicTasksAfterShutdownPolicy is set false,
- * periodic tasks are cancelled at shutdown
- */
- public void testShutdown3() throws InterruptedException {
- CustomExecutor p = new CustomExecutor(1);
- assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
- assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
- p.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
- assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
- assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
- long initialDelay = LONG_DELAY_MS;
- ScheduledFuture task =
- p.scheduleAtFixedRate(new NoOpRunnable(), initialDelay,
- 5, MILLISECONDS);
- try { p.shutdown(); } catch (SecurityException ok) { return; }
assertTrue(p.isShutdown());
assertTrue(p.getQueue().isEmpty());
- assertTrue(task.isDone());
- assertTrue(task.isCancelled());
- joinPool(p);
+ assertEquals(count - poolSize, queuedTasks.size());
+ assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
+ assertTrue(p.isTerminated());
+ assertEquals(poolSize, ran.get());
+ assertEquals(poolSize, p.getCompletedTaskCount());
}
/**
- * if setContinueExistingPeriodicTasksAfterShutdownPolicy is true,
- * periodic tasks are not cancelled at shutdown
+ * shutdownNow returns a list containing tasks that were not run,
+ * and those tasks are drained from the queue
*/
- public void testShutdown4() throws InterruptedException {
- CustomExecutor p = new CustomExecutor(1);
- final CountDownLatch counter = new CountDownLatch(2);
+ public void testShutdownNow_delayedTasks() throws InterruptedException {
+ final CustomExecutor p = new CustomExecutor(1);
+ List<ScheduledFuture> tasks = new ArrayList<>();
+ for (int i = 0; i < 3; i++) {
+ Runnable r = new NoOpRunnable();
+ tasks.add(p.schedule(r, 9, SECONDS));
+ tasks.add(p.scheduleAtFixedRate(r, 9, 9, SECONDS));
+ tasks.add(p.scheduleWithFixedDelay(r, 9, 9, SECONDS));
+ }
+ if (testImplementationDetails)
+ assertEquals(new HashSet(tasks), new HashSet(p.getQueue()));
+ final List<Runnable> queuedTasks;
try {
- p.setContinueExistingPeriodicTasksAfterShutdownPolicy(true);
- assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
- assertTrue(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
- final Runnable r = new CheckedRunnable() {
- public void realRun() {
- counter.countDown();
- }};
- ScheduledFuture task =
- p.scheduleAtFixedRate(r, 1, 1, MILLISECONDS);
+ queuedTasks = p.shutdownNow();
+ } catch (SecurityException ok) {
+ return; // Allowed in case test doesn't have privs
+ }
+ assertTrue(p.isShutdown());
+ assertTrue(p.getQueue().isEmpty());
+ if (testImplementationDetails)
+ assertEquals(new HashSet(tasks), new HashSet(queuedTasks));
+ assertEquals(tasks.size(), queuedTasks.size());
+ for (ScheduledFuture task : tasks) {
+ assertFalse(((CustomTask)task).ran);
assertFalse(task.isDone());
assertFalse(task.isCancelled());
- try { p.shutdown(); } catch (SecurityException ok) { return; }
- assertFalse(task.isCancelled());
- assertFalse(p.isTerminated());
- assertTrue(p.isShutdown());
- assertTrue(counter.await(SMALL_DELAY_MS, MILLISECONDS));
- assertFalse(task.isCancelled());
- assertTrue(task.cancel(false));
- assertTrue(task.isDone());
- assertTrue(task.isCancelled());
- assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS));
- assertTrue(p.isTerminated());
}
- finally {
- joinPool(p);
- }
+ assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
+ assertTrue(p.isTerminated());
}
/**
+ * By default, periodic tasks are cancelled at shutdown.
+ * By default, delayed tasks keep running after shutdown.
+ * Check that changing the default values work:
+ * - setExecuteExistingDelayedTasksAfterShutdownPolicy
+ * - setContinueExistingPeriodicTasksAfterShutdownPolicy
+ */
+ public void testShutdown_cancellation() throws Exception {
+ Boolean[] allBooleans = { null, Boolean.FALSE, Boolean.TRUE };
+ for (Boolean policy : allBooleans)
+ {
+ final int poolSize = 2;
+ final CustomExecutor p = new CustomExecutor(poolSize);
+ final boolean effectiveDelayedPolicy = (policy != Boolean.FALSE);
+ final boolean effectivePeriodicPolicy = (policy == Boolean.TRUE);
+ final boolean effectiveRemovePolicy = (policy == Boolean.TRUE);
+ if (policy != null) {
+ p.setExecuteExistingDelayedTasksAfterShutdownPolicy(policy);
+ p.setContinueExistingPeriodicTasksAfterShutdownPolicy(policy);
+ p.setRemoveOnCancelPolicy(policy);
+ }
+ assertEquals(effectiveDelayedPolicy,
+ p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
+ assertEquals(effectivePeriodicPolicy,
+ p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
+ assertEquals(effectiveRemovePolicy,
+ p.getRemoveOnCancelPolicy());
+ // Strategy: Wedge the pool with poolSize "blocker" threads
+ final AtomicInteger ran = new AtomicInteger(0);
+ final CountDownLatch poolBlocked = new CountDownLatch(poolSize);
+ final CountDownLatch unblock = new CountDownLatch(1);
+ final CountDownLatch periodicLatch1 = new CountDownLatch(2);
+ final CountDownLatch periodicLatch2 = new CountDownLatch(2);
+ Runnable task = new CheckedRunnable() { public void realRun()
+ throws InterruptedException {
+ poolBlocked.countDown();
+ assertTrue(unblock.await(LONG_DELAY_MS, MILLISECONDS));
+ ran.getAndIncrement();
+ }};
+ List<Future<?>> blockers = new ArrayList<>();
+ List<Future<?>> periodics = new ArrayList<>();
+ List<Future<?>> delayeds = new ArrayList<>();
+ for (int i = 0; i < poolSize; i++)
+ blockers.add(p.submit(task));
+ assertTrue(poolBlocked.await(LONG_DELAY_MS, MILLISECONDS));
+
+ periodics.add(p.scheduleAtFixedRate(countDowner(periodicLatch1),
+ 1, 1, MILLISECONDS));
+ periodics.add(p.scheduleWithFixedDelay(countDowner(periodicLatch2),
+ 1, 1, MILLISECONDS));
+ delayeds.add(p.schedule(task, 1, MILLISECONDS));
+
+ assertTrue(p.getQueue().containsAll(periodics));
+ assertTrue(p.getQueue().containsAll(delayeds));
+ try { p.shutdown(); } catch (SecurityException ok) { return; }
+ assertTrue(p.isShutdown());
+ assertFalse(p.isTerminated());
+ for (Future<?> periodic : periodics) {
+ assertTrue(effectivePeriodicPolicy ^ periodic.isCancelled());
+ assertTrue(effectivePeriodicPolicy ^ periodic.isDone());
+ }
+ for (Future<?> delayed : delayeds) {
+ assertTrue(effectiveDelayedPolicy ^ delayed.isCancelled());
+ assertTrue(effectiveDelayedPolicy ^ delayed.isDone());
+ }
+ if (testImplementationDetails) {
+ assertEquals(effectivePeriodicPolicy,
+ p.getQueue().containsAll(periodics));
+ assertEquals(effectiveDelayedPolicy,
+ p.getQueue().containsAll(delayeds));
+ }
+ // Release all pool threads
+ unblock.countDown();
+
+ for (Future<?> delayed : delayeds) {
+ if (effectiveDelayedPolicy) {
+ assertNull(delayed.get());
+ }
+ }
+ if (effectivePeriodicPolicy) {
+ assertTrue(periodicLatch1.await(LONG_DELAY_MS, MILLISECONDS));
+ assertTrue(periodicLatch2.await(LONG_DELAY_MS, MILLISECONDS));
+ for (Future<?> periodic : periodics) {
+ assertTrue(periodic.cancel(false));
+ assertTrue(periodic.isCancelled());
+ assertTrue(periodic.isDone());
+ }
+ }
+ assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
+ assertTrue(p.isTerminated());
+ assertEquals(2 + (effectiveDelayedPolicy ? 1 : 0), ran.get());
+ }}
+
+ /**
* completed submit of callable returns result
*/
public void testSubmitCallable() throws Exception {
- ExecutorService e = new CustomExecutor(2);
- try {
+ final ExecutorService e = new CustomExecutor(2);
+ try (PoolCleaner cleaner = cleaner(e)) {
Future<String> future = e.submit(new StringTask());
String result = future.get();
assertSame(TEST_STRING, result);
- } finally {
- joinPool(e);
}
}
@@ -859,13 +893,11 @@
* completed submit of runnable returns successfully
*/
public void testSubmitRunnable() throws Exception {
- ExecutorService e = new CustomExecutor(2);
- try {
+ final ExecutorService e = new CustomExecutor(2);
+ try (PoolCleaner cleaner = cleaner(e)) {
Future<?> future = e.submit(new NoOpRunnable());
future.get();
assertTrue(future.isDone());
- } finally {
- joinPool(e);
}
}
@@ -873,13 +905,11 @@
* completed submit of (runnable, result) returns result
*/
public void testSubmitRunnable2() throws Exception {
- ExecutorService e = new CustomExecutor(2);
- try {
+ final ExecutorService e = new CustomExecutor(2);
+ try (PoolCleaner cleaner = cleaner(e)) {
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
String result = future.get();
assertSame(TEST_STRING, result);
- } finally {
- joinPool(e);
}
}
@@ -887,13 +917,12 @@
* invokeAny(null) throws NPE
*/
public void testInvokeAny1() throws Exception {
- ExecutorService e = new CustomExecutor(2);
- try {
- e.invokeAny(null);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
- joinPool(e);
+ final ExecutorService e = new CustomExecutor(2);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ try {
+ e.invokeAny(null);
+ shouldThrow();
+ } catch (NullPointerException success) {}
}
}
@@ -901,13 +930,12 @@
* invokeAny(empty collection) throws IAE
*/
public void testInvokeAny2() throws Exception {
- ExecutorService e = new CustomExecutor(2);
- try {
- e.invokeAny(new ArrayList<Callable<String>>());
- shouldThrow();
- } catch (IllegalArgumentException success) {
- } finally {
- joinPool(e);
+ final ExecutorService e = new CustomExecutor(2);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ try {
+ e.invokeAny(new ArrayList<Callable<String>>());
+ shouldThrow();
+ } catch (IllegalArgumentException success) {}
}
}
@@ -915,18 +943,17 @@
* invokeAny(c) throws NPE if c has null elements
*/
public void testInvokeAny3() throws Exception {
- CountDownLatch latch = new CountDownLatch(1);
- ExecutorService e = new CustomExecutor(2);
- List<Callable<String>> l = new ArrayList<Callable<String>>();
- l.add(latchAwaitingStringTask(latch));
- l.add(null);
- try {
- e.invokeAny(l);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
+ final CountDownLatch latch = new CountDownLatch(1);
+ final ExecutorService e = new CustomExecutor(2);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ List<Callable<String>> l = new ArrayList<Callable<String>>();
+ l.add(latchAwaitingStringTask(latch));
+ l.add(null);
+ try {
+ e.invokeAny(l);
+ shouldThrow();
+ } catch (NullPointerException success) {}
latch.countDown();
- joinPool(e);
}
}
@@ -934,16 +961,16 @@
* invokeAny(c) throws ExecutionException if no task completes
*/
public void testInvokeAny4() throws Exception {
- ExecutorService e = new CustomExecutor(2);
- List<Callable<String>> l = new ArrayList<Callable<String>>();
- l.add(new NPETask());
- try {
- e.invokeAny(l);
- shouldThrow();
- } catch (ExecutionException success) {
- assertTrue(success.getCause() instanceof NullPointerException);
- } finally {
- joinPool(e);
+ final ExecutorService e = new CustomExecutor(2);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ List<Callable<String>> l = new ArrayList<Callable<String>>();
+ l.add(new NPETask());
+ try {
+ e.invokeAny(l);
+ shouldThrow();
+ } catch (ExecutionException success) {
+ assertTrue(success.getCause() instanceof NullPointerException);
+ }
}
}
@@ -951,15 +978,13 @@
* invokeAny(c) returns result of some task
*/
public void testInvokeAny5() throws Exception {
- ExecutorService e = new CustomExecutor(2);
- try {
+ final ExecutorService e = new CustomExecutor(2);
+ try (PoolCleaner cleaner = cleaner(e)) {
List<Callable<String>> l = new ArrayList<Callable<String>>();
l.add(new StringTask());
l.add(new StringTask());
String result = e.invokeAny(l);
assertSame(TEST_STRING, result);
- } finally {
- joinPool(e);
}
}
@@ -967,13 +992,12 @@
* invokeAll(null) throws NPE
*/
public void testInvokeAll1() throws Exception {
- ExecutorService e = new CustomExecutor(2);
- try {
- e.invokeAll(null);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
- joinPool(e);
+ final ExecutorService e = new CustomExecutor(2);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ try {
+ e.invokeAll(null);
+ shouldThrow();
+ } catch (NullPointerException success) {}
}
}
@@ -981,12 +1005,10 @@
* invokeAll(empty collection) returns empty collection
*/
public void testInvokeAll2() throws Exception {
- ExecutorService e = new CustomExecutor(2);
- try {
+ final ExecutorService e = new CustomExecutor(2);
+ try (PoolCleaner cleaner = cleaner(e)) {
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
assertTrue(r.isEmpty());
- } finally {
- joinPool(e);
}
}
@@ -994,16 +1016,15 @@
* invokeAll(c) throws NPE if c has null elements
*/
public void testInvokeAll3() throws Exception {
- ExecutorService e = new CustomExecutor(2);
- List<Callable<String>> l = new ArrayList<Callable<String>>();
- l.add(new StringTask());
- l.add(null);
- try {
- e.invokeAll(l);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
- joinPool(e);
+ final ExecutorService e = new CustomExecutor(2);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ List<Callable<String>> l = new ArrayList<Callable<String>>();
+ l.add(new StringTask());
+ l.add(null);
+ try {
+ e.invokeAll(l);
+ shouldThrow();
+ } catch (NullPointerException success) {}
}
}
@@ -1011,18 +1032,18 @@
* get of invokeAll(c) throws exception on failed task
*/
public void testInvokeAll4() throws Exception {
- ExecutorService e = new CustomExecutor(2);
- List<Callable<String>> l = new ArrayList<Callable<String>>();
- l.add(new NPETask());
- List<Future<String>> futures = e.invokeAll(l);
- assertEquals(1, futures.size());
- try {
- futures.get(0).get();
- shouldThrow();
- } catch (ExecutionException success) {
- assertTrue(success.getCause() instanceof NullPointerException);
- } finally {
- joinPool(e);
+ final ExecutorService e = new CustomExecutor(2);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ List<Callable<String>> l = new ArrayList<Callable<String>>();
+ l.add(new NPETask());
+ List<Future<String>> futures = e.invokeAll(l);
+ assertEquals(1, futures.size());
+ try {
+ futures.get(0).get();
+ shouldThrow();
+ } catch (ExecutionException success) {
+ assertTrue(success.getCause() instanceof NullPointerException);
+ }
}
}
@@ -1030,8 +1051,8 @@
* invokeAll(c) returns results of all completed tasks
*/
public void testInvokeAll5() throws Exception {
- ExecutorService e = new CustomExecutor(2);
- try {
+ final ExecutorService e = new CustomExecutor(2);
+ try (PoolCleaner cleaner = cleaner(e)) {
List<Callable<String>> l = new ArrayList<Callable<String>>();
l.add(new StringTask());
l.add(new StringTask());
@@ -1039,8 +1060,6 @@
assertEquals(2, futures.size());
for (Future<String> future : futures)
assertSame(TEST_STRING, future.get());
- } finally {
- joinPool(e);
}
}
@@ -1048,13 +1067,12 @@
* timed invokeAny(null) throws NPE
*/
public void testTimedInvokeAny1() throws Exception {
- ExecutorService e = new CustomExecutor(2);
- try {
- e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
- joinPool(e);
+ final ExecutorService e = new CustomExecutor(2);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ try {
+ e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
+ shouldThrow();
+ } catch (NullPointerException success) {}
}
}
@@ -1062,15 +1080,14 @@
* timed invokeAny(,,null) throws NPE
*/
public void testTimedInvokeAnyNullTimeUnit() throws Exception {
- ExecutorService e = new CustomExecutor(2);
- List<Callable<String>> l = new ArrayList<Callable<String>>();
- l.add(new StringTask());
- try {
- e.invokeAny(l, MEDIUM_DELAY_MS, null);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
- joinPool(e);
+ final ExecutorService e = new CustomExecutor(2);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ List<Callable<String>> l = new ArrayList<Callable<String>>();
+ l.add(new StringTask());
+ try {
+ e.invokeAny(l, MEDIUM_DELAY_MS, null);
+ shouldThrow();
+ } catch (NullPointerException success) {}
}
}
@@ -1078,13 +1095,12 @@
* timed invokeAny(empty collection) throws IAE
*/
public void testTimedInvokeAny2() throws Exception {
- ExecutorService e = new CustomExecutor(2);
- try {
- e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
- shouldThrow();
- } catch (IllegalArgumentException success) {
- } finally {
- joinPool(e);
+ final ExecutorService e = new CustomExecutor(2);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ try {
+ e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
+ shouldThrow();
+ } catch (IllegalArgumentException success) {}
}
}
@@ -1093,17 +1109,16 @@
*/
public void testTimedInvokeAny3() throws Exception {
CountDownLatch latch = new CountDownLatch(1);
- ExecutorService e = new CustomExecutor(2);
- List<Callable<String>> l = new ArrayList<Callable<String>>();
- l.add(latchAwaitingStringTask(latch));
- l.add(null);
- try {
- e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
+ final ExecutorService e = new CustomExecutor(2);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ List<Callable<String>> l = new ArrayList<Callable<String>>();
+ l.add(latchAwaitingStringTask(latch));
+ l.add(null);
+ try {
+ e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
+ shouldThrow();
+ } catch (NullPointerException success) {}
latch.countDown();
- joinPool(e);
}
}
@@ -1111,16 +1126,18 @@
* timed invokeAny(c) throws ExecutionException if no task completes
*/
public void testTimedInvokeAny4() throws Exception {
- ExecutorService e = new CustomExecutor(2);
- List<Callable<String>> l = new ArrayList<Callable<String>>();
- l.add(new NPETask());
- try {
- e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
- shouldThrow();
- } catch (ExecutionException success) {
- assertTrue(success.getCause() instanceof NullPointerException);
- } finally {
- joinPool(e);
+ final ExecutorService e = new CustomExecutor(2);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ long startTime = System.nanoTime();
+ List<Callable<String>> l = new ArrayList<Callable<String>>();
+ l.add(new NPETask());
+ try {
+ e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
+ shouldThrow();
+ } catch (ExecutionException success) {
+ assertTrue(success.getCause() instanceof NullPointerException);
+ }
+ assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
}
}
@@ -1128,15 +1145,15 @@
* timed invokeAny(c) returns result of some task
*/
public void testTimedInvokeAny5() throws Exception {
- ExecutorService e = new CustomExecutor(2);
- try {
+ final ExecutorService e = new CustomExecutor(2);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ long startTime = System.nanoTime();
List<Callable<String>> l = new ArrayList<Callable<String>>();
l.add(new StringTask());
l.add(new StringTask());
- String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
+ String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
assertSame(TEST_STRING, result);
- } finally {
- joinPool(e);
+ assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
}
}
@@ -1144,13 +1161,12 @@
* timed invokeAll(null) throws NPE
*/
public void testTimedInvokeAll1() throws Exception {
- ExecutorService e = new CustomExecutor(2);
- try {
- e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
- joinPool(e);
+ final ExecutorService e = new CustomExecutor(2);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ try {
+ e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
+ shouldThrow();
+ } catch (NullPointerException success) {}
}
}
@@ -1158,15 +1174,14 @@
* timed invokeAll(,,null) throws NPE
*/
public void testTimedInvokeAllNullTimeUnit() throws Exception {
- ExecutorService e = new CustomExecutor(2);
- List<Callable<String>> l = new ArrayList<Callable<String>>();
- l.add(new StringTask());
- try {
- e.invokeAll(l, MEDIUM_DELAY_MS, null);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
- joinPool(e);
+ final ExecutorService e = new CustomExecutor(2);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ List<Callable<String>> l = new ArrayList<Callable<String>>();
+ l.add(new StringTask());
+ try {
+ e.invokeAll(l, MEDIUM_DELAY_MS, null);
+ shouldThrow();
+ } catch (NullPointerException success) {}
}
}
@@ -1174,12 +1189,10 @@
* timed invokeAll(empty collection) returns empty collection
*/
public void testTimedInvokeAll2() throws Exception {
- ExecutorService e = new CustomExecutor(2);
- try {
+ final ExecutorService e = new CustomExecutor(2);
+ try (PoolCleaner cleaner = cleaner(e)) {
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
assertTrue(r.isEmpty());
- } finally {
- joinPool(e);
}
}
@@ -1187,16 +1200,15 @@
* timed invokeAll(c) throws NPE if c has null elements
*/
public void testTimedInvokeAll3() throws Exception {
- ExecutorService e = new CustomExecutor(2);
- List<Callable<String>> l = new ArrayList<Callable<String>>();
- l.add(new StringTask());
- l.add(null);
- try {
- e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
- joinPool(e);
+ final ExecutorService e = new CustomExecutor(2);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ List<Callable<String>> l = new ArrayList<Callable<String>>();
+ l.add(new StringTask());
+ l.add(null);
+ try {
+ e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
+ shouldThrow();
+ } catch (NullPointerException success) {}
}
}
@@ -1204,19 +1216,19 @@
* get of element of invokeAll(c) throws exception on failed task
*/
public void testTimedInvokeAll4() throws Exception {
- ExecutorService e = new CustomExecutor(2);
- List<Callable<String>> l = new ArrayList<Callable<String>>();
- l.add(new NPETask());
- List<Future<String>> futures =
- e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
- assertEquals(1, futures.size());
- try {
- futures.get(0).get();
- shouldThrow();
- } catch (ExecutionException success) {
- assertTrue(success.getCause() instanceof NullPointerException);
- } finally {
- joinPool(e);
+ final ExecutorService e = new CustomExecutor(2);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ List<Callable<String>> l = new ArrayList<Callable<String>>();
+ l.add(new NPETask());
+ List<Future<String>> futures =
+ e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
+ assertEquals(1, futures.size());
+ try {
+ futures.get(0).get();
+ shouldThrow();
+ } catch (ExecutionException success) {
+ assertTrue(success.getCause() instanceof NullPointerException);
+ }
}
}
@@ -1224,18 +1236,16 @@
* timed invokeAll(c) returns results of all completed tasks
*/
public void testTimedInvokeAll5() throws Exception {
- ExecutorService e = new CustomExecutor(2);
- try {
+ final ExecutorService e = new CustomExecutor(2);
+ try (PoolCleaner cleaner = cleaner(e)) {
List<Callable<String>> l = new ArrayList<Callable<String>>();
l.add(new StringTask());
l.add(new StringTask());
List<Future<String>> futures =
- e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
+ e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
assertEquals(2, futures.size());
for (Future<String> future : futures)
assertSame(TEST_STRING, future.get());
- } finally {
- joinPool(e);
}
}
@@ -1243,21 +1253,37 @@
* timed invokeAll(c) cancels tasks not completed by timeout
*/
public void testTimedInvokeAll6() throws Exception {
- ExecutorService e = new CustomExecutor(2);
- try {
- List<Callable<String>> l = new ArrayList<Callable<String>>();
- l.add(new StringTask());
- l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
- l.add(new StringTask());
- List<Future<String>> futures =
- e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
- assertEquals(l.size(), futures.size());
- for (Future future : futures)
- assertTrue(future.isDone());
- assertFalse(futures.get(0).isCancelled());
- assertTrue(futures.get(1).isCancelled());
- } finally {
- joinPool(e);
+ for (long timeout = timeoutMillis();;) {
+ final CountDownLatch done = new CountDownLatch(1);
+ final Callable<String> waiter = new CheckedCallable<String>() {
+ public String realCall() {
+ try { done.await(LONG_DELAY_MS, MILLISECONDS); }
+ catch (InterruptedException ok) {}
+ return "1"; }};
+ final ExecutorService p = new CustomExecutor(2);
+ try (PoolCleaner cleaner = cleaner(p, done)) {
+ List<Callable<String>> tasks = new ArrayList<>();
+ tasks.add(new StringTask("0"));
+ tasks.add(waiter);
+ tasks.add(new StringTask("2"));
+ long startTime = System.nanoTime();
+ List<Future<String>> futures =
+ p.invokeAll(tasks, timeout, MILLISECONDS);
+ assertEquals(tasks.size(), futures.size());
+ assertTrue(millisElapsedSince(startTime) >= timeout);
+ for (Future future : futures)
+ assertTrue(future.isDone());
+ assertTrue(futures.get(1).isCancelled());
+ try {
+ assertEquals("0", futures.get(0).get());
+ assertEquals("2", futures.get(2).get());
+ break;
+ } catch (CancellationException retryWithLongerTimeout) {
+ timeout *= 2;
+ if (timeout >= LONG_DELAY_MS / 2)
+ fail("expected exactly one task to be cancelled");
+ }
+ }
}
}
diff --git a/jsr166-tests/src/test/java/jsr166/ScheduledExecutorTest.java b/jsr166-tests/src/test/java/jsr166/ScheduledExecutorTest.java
index a2e83d0..81f7370 100644
--- a/jsr166-tests/src/test/java/jsr166/ScheduledExecutorTest.java
+++ b/jsr166-tests/src/test/java/jsr166/ScheduledExecutorTest.java
@@ -9,11 +9,15 @@
package jsr166;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
+import static java.util.concurrent.TimeUnit.NANOSECONDS;
+import static java.util.concurrent.TimeUnit.SECONDS;
import java.util.ArrayList;
+import java.util.HashSet;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable;
+import java.util.concurrent.CancellationException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
@@ -24,7 +28,9 @@
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.AtomicLong;
import junit.framework.Test;
import junit.framework.TestSuite;
@@ -37,24 +43,20 @@
// main(suite(), args);
// }
// public static Test suite() {
- // return new TestSuite(...);
+ // return new TestSuite(ScheduledExecutorTest.class);
// }
/**
* execute successfully executes a runnable
*/
public void testExecute() throws InterruptedException {
- ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
- final CountDownLatch done = new CountDownLatch(1);
- final Runnable task = new CheckedRunnable() {
- public void realRun() {
- done.countDown();
- }};
- try {
+ final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
+ try (PoolCleaner cleaner = cleaner(p)) {
+ final CountDownLatch done = new CountDownLatch(1);
+ final Runnable task = new CheckedRunnable() {
+ public void realRun() { done.countDown(); }};
p.execute(task);
- assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS));
- } finally {
- joinPool(p);
+ assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
}
}
@@ -62,10 +64,10 @@
* delayed schedule of callable successfully executes after delay
*/
public void testSchedule1() throws Exception {
- ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
- final long startTime = System.nanoTime();
- final CountDownLatch done = new CountDownLatch(1);
- try {
+ final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
+ try (PoolCleaner cleaner = cleaner(p)) {
+ final long startTime = System.nanoTime();
+ final CountDownLatch done = new CountDownLatch(1);
Callable task = new CheckedCallable<Boolean>() {
public Boolean realCall() {
done.countDown();
@@ -76,8 +78,6 @@
assertSame(Boolean.TRUE, f.get());
assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
assertTrue(done.await(0L, MILLISECONDS));
- } finally {
- joinPool(p);
}
}
@@ -85,10 +85,10 @@
* delayed schedule of runnable successfully executes after delay
*/
public void testSchedule3() throws Exception {
- ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
- final long startTime = System.nanoTime();
- final CountDownLatch done = new CountDownLatch(1);
- try {
+ final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
+ try (PoolCleaner cleaner = cleaner(p)) {
+ final long startTime = System.nanoTime();
+ final CountDownLatch done = new CountDownLatch(1);
Runnable task = new CheckedRunnable() {
public void realRun() {
done.countDown();
@@ -98,8 +98,6 @@
await(done);
assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
- } finally {
- joinPool(p);
}
}
@@ -107,10 +105,10 @@
* scheduleAtFixedRate executes runnable after given initial delay
*/
public void testSchedule4() throws Exception {
- ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
- final long startTime = System.nanoTime();
- final CountDownLatch done = new CountDownLatch(1);
- try {
+ final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
+ try (PoolCleaner cleaner = cleaner(p)) {
+ final long startTime = System.nanoTime();
+ final CountDownLatch done = new CountDownLatch(1);
Runnable task = new CheckedRunnable() {
public void realRun() {
done.countDown();
@@ -122,8 +120,6 @@
await(done);
assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
f.cancel(true);
- } finally {
- joinPool(p);
}
}
@@ -131,10 +127,10 @@
* scheduleWithFixedDelay executes runnable after given initial delay
*/
public void testSchedule5() throws Exception {
- ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
- final long startTime = System.nanoTime();
- final CountDownLatch done = new CountDownLatch(1);
- try {
+ final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
+ try (PoolCleaner cleaner = cleaner(p)) {
+ final long startTime = System.nanoTime();
+ final CountDownLatch done = new CountDownLatch(1);
Runnable task = new CheckedRunnable() {
public void realRun() {
done.countDown();
@@ -146,8 +142,6 @@
await(done);
assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
f.cancel(true);
- } finally {
- joinPool(p);
}
}
@@ -157,58 +151,77 @@
}
/**
- * scheduleAtFixedRate executes series of tasks at given rate
+ * scheduleAtFixedRate executes series of tasks at given rate.
+ * Eventually, it must hold that:
+ * cycles - 1 <= elapsedMillis/delay < cycles
*/
public void testFixedRateSequence() throws InterruptedException {
- ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
- try {
+ final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
+ try (PoolCleaner cleaner = cleaner(p)) {
for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) {
- long startTime = System.nanoTime();
- int cycles = 10;
+ final long startTime = System.nanoTime();
+ final int cycles = 8;
final CountDownLatch done = new CountDownLatch(cycles);
- Runnable task = new CheckedRunnable() {
+ final Runnable task = new CheckedRunnable() {
public void realRun() { done.countDown(); }};
- ScheduledFuture h =
+ final ScheduledFuture periodicTask =
p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS);
- done.await();
- h.cancel(true);
- double normalizedTime =
- (double) millisElapsedSince(startTime) / delay;
- if (normalizedTime >= cycles - 1 &&
- normalizedTime <= cycles)
+ final int totalDelayMillis = (cycles - 1) * delay;
+ await(done, totalDelayMillis + LONG_DELAY_MS);
+ periodicTask.cancel(true);
+ final long elapsedMillis = millisElapsedSince(startTime);
+ assertTrue(elapsedMillis >= totalDelayMillis);
+ if (elapsedMillis <= cycles * delay)
return;
+ // else retry with longer delay
}
- throw new AssertionError("unexpected execution rate");
- } finally {
- joinPool(p);
+ fail("unexpected execution rate");
}
}
/**
- * scheduleWithFixedDelay executes series of tasks with given period
+ * scheduleWithFixedDelay executes series of tasks with given period.
+ * Eventually, it must hold that each task starts at least delay and at
+ * most 2 * delay after the termination of the previous task.
*/
public void testFixedDelaySequence() throws InterruptedException {
- ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
- try {
+ final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
+ try (PoolCleaner cleaner = cleaner(p)) {
for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) {
- long startTime = System.nanoTime();
- int cycles = 10;
+ final long startTime = System.nanoTime();
+ final AtomicLong previous = new AtomicLong(startTime);
+ final AtomicBoolean tryLongerDelay = new AtomicBoolean(false);
+ final int cycles = 8;
final CountDownLatch done = new CountDownLatch(cycles);
- Runnable task = new CheckedRunnable() {
- public void realRun() { done.countDown(); }};
- ScheduledFuture h =
+ final int d = delay;
+ final Runnable task = new CheckedRunnable() {
+ public void realRun() {
+ long now = System.nanoTime();
+ long elapsedMillis
+ = NANOSECONDS.toMillis(now - previous.get());
+ if (done.getCount() == cycles) { // first execution
+ if (elapsedMillis >= d)
+ tryLongerDelay.set(true);
+ } else {
+ assertTrue(elapsedMillis >= d);
+ if (elapsedMillis >= 2 * d)
+ tryLongerDelay.set(true);
+ }
+ previous.set(now);
+ done.countDown();
+ }};
+ final ScheduledFuture periodicTask =
p.scheduleWithFixedDelay(task, 0, delay, MILLISECONDS);
- done.await();
- h.cancel(true);
- double normalizedTime =
- (double) millisElapsedSince(startTime) / delay;
- if (normalizedTime >= cycles - 1 &&
- normalizedTime <= cycles)
+ final int totalDelayMillis = (cycles - 1) * delay;
+ await(done, totalDelayMillis + cycles * LONG_DELAY_MS);
+ periodicTask.cancel(true);
+ final long elapsedMillis = millisElapsedSince(startTime);
+ assertTrue(elapsedMillis >= totalDelayMillis);
+ if (!tryLongerDelay.get())
return;
+ // else retry with longer delay
}
- throw new AssertionError("unexpected execution rate");
- } finally {
- joinPool(p);
+ fail("unexpected execution rate");
}
}
@@ -216,108 +229,107 @@
* execute(null) throws NPE
*/
public void testExecuteNull() throws InterruptedException {
- ScheduledThreadPoolExecutor se = null;
- try {
- se = new ScheduledThreadPoolExecutor(1);
- se.execute(null);
- shouldThrow();
- } catch (NullPointerException success) {}
-
- joinPool(se);
+ final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
+ try (PoolCleaner cleaner = cleaner(p)) {
+ try {
+ p.execute(null);
+ shouldThrow();
+ } catch (NullPointerException success) {}
+ }
}
/**
* schedule(null) throws NPE
*/
public void testScheduleNull() throws InterruptedException {
- ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
- try {
- TrackedCallable callable = null;
- Future f = se.schedule(callable, SHORT_DELAY_MS, MILLISECONDS);
- shouldThrow();
- } catch (NullPointerException success) {}
- joinPool(se);
+ final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
+ try (PoolCleaner cleaner = cleaner(p)) {
+ try {
+ TrackedCallable callable = null;
+ Future f = p.schedule(callable, SHORT_DELAY_MS, MILLISECONDS);
+ shouldThrow();
+ } catch (NullPointerException success) {}
+ }
}
/**
* execute throws RejectedExecutionException if shutdown
*/
public void testSchedule1_RejectedExecutionException() throws InterruptedException {
- ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
- try {
- se.shutdown();
- se.schedule(new NoOpRunnable(),
- MEDIUM_DELAY_MS, MILLISECONDS);
- shouldThrow();
- } catch (RejectedExecutionException success) {
- } catch (SecurityException ok) {
+ final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
+ try (PoolCleaner cleaner = cleaner(p)) {
+ try {
+ p.shutdown();
+ p.schedule(new NoOpRunnable(),
+ MEDIUM_DELAY_MS, MILLISECONDS);
+ shouldThrow();
+ } catch (RejectedExecutionException success) {
+ } catch (SecurityException ok) {}
}
-
- joinPool(se);
}
/**
* schedule throws RejectedExecutionException if shutdown
*/
public void testSchedule2_RejectedExecutionException() throws InterruptedException {
- ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
- try {
- se.shutdown();
- se.schedule(new NoOpCallable(),
- MEDIUM_DELAY_MS, MILLISECONDS);
- shouldThrow();
- } catch (RejectedExecutionException success) {
- } catch (SecurityException ok) {
+ final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
+ try (PoolCleaner cleaner = cleaner(p)) {
+ try {
+ p.shutdown();
+ p.schedule(new NoOpCallable(),
+ MEDIUM_DELAY_MS, MILLISECONDS);
+ shouldThrow();
+ } catch (RejectedExecutionException success) {
+ } catch (SecurityException ok) {}
}
- joinPool(se);
}
/**
* schedule callable throws RejectedExecutionException if shutdown
*/
public void testSchedule3_RejectedExecutionException() throws InterruptedException {
- ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
- try {
- se.shutdown();
- se.schedule(new NoOpCallable(),
- MEDIUM_DELAY_MS, MILLISECONDS);
- shouldThrow();
- } catch (RejectedExecutionException success) {
- } catch (SecurityException ok) {
+ final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
+ try (PoolCleaner cleaner = cleaner(p)) {
+ try {
+ p.shutdown();
+ p.schedule(new NoOpCallable(),
+ MEDIUM_DELAY_MS, MILLISECONDS);
+ shouldThrow();
+ } catch (RejectedExecutionException success) {
+ } catch (SecurityException ok) {}
}
- joinPool(se);
}
/**
* scheduleAtFixedRate throws RejectedExecutionException if shutdown
*/
public void testScheduleAtFixedRate1_RejectedExecutionException() throws InterruptedException {
- ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
- try {
- se.shutdown();
- se.scheduleAtFixedRate(new NoOpRunnable(),
- MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS);
- shouldThrow();
- } catch (RejectedExecutionException success) {
- } catch (SecurityException ok) {
+ final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
+ try (PoolCleaner cleaner = cleaner(p)) {
+ try {
+ p.shutdown();
+ p.scheduleAtFixedRate(new NoOpRunnable(),
+ MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS);
+ shouldThrow();
+ } catch (RejectedExecutionException success) {
+ } catch (SecurityException ok) {}
}
- joinPool(se);
}
/**
* scheduleWithFixedDelay throws RejectedExecutionException if shutdown
*/
public void testScheduleWithFixedDelay1_RejectedExecutionException() throws InterruptedException {
- ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
- try {
- se.shutdown();
- se.scheduleWithFixedDelay(new NoOpRunnable(),
- MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS);
- shouldThrow();
- } catch (RejectedExecutionException success) {
- } catch (SecurityException ok) {
+ final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
+ try (PoolCleaner cleaner = cleaner(p)) {
+ try {
+ p.shutdown();
+ p.scheduleWithFixedDelay(new NoOpRunnable(),
+ MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS);
+ shouldThrow();
+ } catch (RejectedExecutionException success) {
+ } catch (SecurityException ok) {}
}
- joinPool(se);
}
/**
@@ -325,22 +337,19 @@
* thread becomes active
*/
public void testGetActiveCount() throws InterruptedException {
- final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(2);
- final CountDownLatch threadStarted = new CountDownLatch(1);
final CountDownLatch done = new CountDownLatch(1);
- try {
+ final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(2);
+ try (PoolCleaner cleaner = cleaner(p, done)) {
+ final CountDownLatch threadStarted = new CountDownLatch(1);
assertEquals(0, p.getActiveCount());
p.execute(new CheckedRunnable() {
public void realRun() throws InterruptedException {
threadStarted.countDown();
assertEquals(1, p.getActiveCount());
- done.await();
+ await(done);
}});
- assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
+ await(threadStarted);
assertEquals(1, p.getActiveCount());
- } finally {
- done.countDown();
- joinPool(p);
}
}
@@ -350,10 +359,10 @@
*/
public void testGetCompletedTaskCount() throws InterruptedException {
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(2);
- final CountDownLatch threadStarted = new CountDownLatch(1);
- final CountDownLatch threadProceed = new CountDownLatch(1);
- final CountDownLatch threadDone = new CountDownLatch(1);
- try {
+ try (PoolCleaner cleaner = cleaner(p)) {
+ final CountDownLatch threadStarted = new CountDownLatch(1);
+ final CountDownLatch threadProceed = new CountDownLatch(1);
+ final CountDownLatch threadDone = new CountDownLatch(1);
assertEquals(0, p.getCompletedTaskCount());
p.execute(new CheckedRunnable() {
public void realRun() throws InterruptedException {
@@ -372,8 +381,6 @@
fail("timed out");
Thread.yield();
}
- } finally {
- joinPool(p);
}
}
@@ -382,8 +389,9 @@
*/
public void testGetCorePoolSize() throws InterruptedException {
ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
- assertEquals(1, p.getCorePoolSize());
- joinPool(p);
+ try (PoolCleaner cleaner = cleaner(p)) {
+ assertEquals(1, p.getCorePoolSize());
+ }
}
/**
@@ -395,22 +403,19 @@
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(THREADS);
final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
final CountDownLatch done = new CountDownLatch(1);
- try {
+ try (PoolCleaner cleaner = cleaner(p, done)) {
assertEquals(0, p.getLargestPoolSize());
for (int i = 0; i < THREADS; i++)
p.execute(new CheckedRunnable() {
public void realRun() throws InterruptedException {
threadsStarted.countDown();
- done.await();
+ await(done);
assertEquals(THREADS, p.getLargestPoolSize());
}});
- assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS));
- assertEquals(THREADS, p.getLargestPoolSize());
- } finally {
- done.countDown();
- joinPool(p);
+ await(threadsStarted);
assertEquals(THREADS, p.getLargestPoolSize());
}
+ assertEquals(THREADS, p.getLargestPoolSize());
}
/**
@@ -421,19 +426,16 @@
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
final CountDownLatch threadStarted = new CountDownLatch(1);
final CountDownLatch done = new CountDownLatch(1);
- try {
+ try (PoolCleaner cleaner = cleaner(p, done)) {
assertEquals(0, p.getPoolSize());
p.execute(new CheckedRunnable() {
public void realRun() throws InterruptedException {
threadStarted.countDown();
assertEquals(1, p.getPoolSize());
- done.await();
+ await(done);
}});
- assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
+ await(threadStarted);
assertEquals(1, p.getPoolSize());
- } finally {
- done.countDown();
- joinPool(p);
}
}
@@ -442,58 +444,71 @@
* submitted
*/
public void testGetTaskCount() throws InterruptedException {
- final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
- final CountDownLatch threadStarted = new CountDownLatch(1);
+ final int TASKS = 3;
final CountDownLatch done = new CountDownLatch(1);
- final int TASKS = 5;
- try {
+ final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
+ try (PoolCleaner cleaner = cleaner(p, done)) {
+ final CountDownLatch threadStarted = new CountDownLatch(1);
assertEquals(0, p.getTaskCount());
- for (int i = 0; i < TASKS; i++)
+ assertEquals(0, p.getCompletedTaskCount());
+ p.execute(new CheckedRunnable() {
+ public void realRun() throws InterruptedException {
+ threadStarted.countDown();
+ await(done);
+ }});
+ await(threadStarted);
+ assertEquals(1, p.getTaskCount());
+ assertEquals(0, p.getCompletedTaskCount());
+ for (int i = 0; i < TASKS; i++) {
+ assertEquals(1 + i, p.getTaskCount());
p.execute(new CheckedRunnable() {
public void realRun() throws InterruptedException {
threadStarted.countDown();
- done.await();
+ assertEquals(1 + TASKS, p.getTaskCount());
+ await(done);
}});
- assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
- assertEquals(TASKS, p.getTaskCount());
- } finally {
- done.countDown();
- joinPool(p);
+ }
+ assertEquals(1 + TASKS, p.getTaskCount());
+ assertEquals(0, p.getCompletedTaskCount());
}
+ assertEquals(1 + TASKS, p.getTaskCount());
+ assertEquals(1 + TASKS, p.getCompletedTaskCount());
}
/**
* getThreadFactory returns factory in constructor if not set
*/
public void testGetThreadFactory() throws InterruptedException {
- ThreadFactory tf = new SimpleThreadFactory();
- ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1, tf);
- assertSame(tf, p.getThreadFactory());
- joinPool(p);
+ final ThreadFactory threadFactory = new SimpleThreadFactory();
+ final ScheduledThreadPoolExecutor p =
+ new ScheduledThreadPoolExecutor(1, threadFactory);
+ try (PoolCleaner cleaner = cleaner(p)) {
+ assertSame(threadFactory, p.getThreadFactory());
+ }
}
/**
* setThreadFactory sets the thread factory returned by getThreadFactory
*/
public void testSetThreadFactory() throws InterruptedException {
- ThreadFactory tf = new SimpleThreadFactory();
- ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
- p.setThreadFactory(tf);
- assertSame(tf, p.getThreadFactory());
- joinPool(p);
+ ThreadFactory threadFactory = new SimpleThreadFactory();
+ final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
+ try (PoolCleaner cleaner = cleaner(p)) {
+ p.setThreadFactory(threadFactory);
+ assertSame(threadFactory, p.getThreadFactory());
+ }
}
/**
* setThreadFactory(null) throws NPE
*/
public void testSetThreadFactoryNull() throws InterruptedException {
- ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
- try {
- p.setThreadFactory(null);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
- joinPool(p);
+ final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
+ try (PoolCleaner cleaner = cleaner(p)) {
+ try {
+ p.setThreadFactory(null);
+ shouldThrow();
+ } catch (NullPointerException success) {}
}
}
@@ -502,7 +517,7 @@
*/
public void testIsShutdown() {
- ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
+ final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
try {
assertFalse(p.isShutdown());
}
@@ -517,24 +532,23 @@
*/
public void testIsTerminated() throws InterruptedException {
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
- final CountDownLatch threadStarted = new CountDownLatch(1);
- final CountDownLatch done = new CountDownLatch(1);
- assertFalse(p.isTerminated());
- try {
+ try (PoolCleaner cleaner = cleaner(p)) {
+ final CountDownLatch threadStarted = new CountDownLatch(1);
+ final CountDownLatch done = new CountDownLatch(1);
+ assertFalse(p.isTerminated());
p.execute(new CheckedRunnable() {
public void realRun() throws InterruptedException {
assertFalse(p.isTerminated());
threadStarted.countDown();
- done.await();
+ await(done);
}});
- assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
+ await(threadStarted);
assertFalse(p.isTerminating());
done.countDown();
- } finally {
try { p.shutdown(); } catch (SecurityException ok) { return; }
+ assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
+ assertTrue(p.isTerminated());
}
- assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
- assertTrue(p.isTerminated());
}
/**
@@ -544,49 +558,45 @@
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
final CountDownLatch threadStarted = new CountDownLatch(1);
final CountDownLatch done = new CountDownLatch(1);
- try {
+ try (PoolCleaner cleaner = cleaner(p)) {
assertFalse(p.isTerminating());
p.execute(new CheckedRunnable() {
public void realRun() throws InterruptedException {
assertFalse(p.isTerminating());
threadStarted.countDown();
- done.await();
+ await(done);
}});
- assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
+ await(threadStarted);
assertFalse(p.isTerminating());
done.countDown();
- } finally {
try { p.shutdown(); } catch (SecurityException ok) { return; }
+ assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
+ assertTrue(p.isTerminated());
+ assertFalse(p.isTerminating());
}
- assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
- assertTrue(p.isTerminated());
- assertFalse(p.isTerminating());
}
/**
* getQueue returns the work queue, which contains queued tasks
*/
public void testGetQueue() throws InterruptedException {
- ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
- final CountDownLatch threadStarted = new CountDownLatch(1);
final CountDownLatch done = new CountDownLatch(1);
- try {
+ final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
+ try (PoolCleaner cleaner = cleaner(p, done)) {
+ final CountDownLatch threadStarted = new CountDownLatch(1);
ScheduledFuture[] tasks = new ScheduledFuture[5];
for (int i = 0; i < tasks.length; i++) {
Runnable r = new CheckedRunnable() {
public void realRun() throws InterruptedException {
threadStarted.countDown();
- done.await();
+ await(done);
}};
tasks[i] = p.schedule(r, 1, MILLISECONDS);
}
- assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
+ await(threadStarted);
BlockingQueue<Runnable> q = p.getQueue();
assertTrue(q.contains(tasks[tasks.length - 1]));
assertFalse(q.contains(tasks[0]));
- } finally {
- done.countDown();
- joinPool(p);
}
}
@@ -594,20 +604,20 @@
* remove(task) removes queued task, and fails to remove active task
*/
public void testRemove() throws InterruptedException {
- final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
- ScheduledFuture[] tasks = new ScheduledFuture[5];
- final CountDownLatch threadStarted = new CountDownLatch(1);
final CountDownLatch done = new CountDownLatch(1);
- try {
+ final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
+ try (PoolCleaner cleaner = cleaner(p, done)) {
+ ScheduledFuture[] tasks = new ScheduledFuture[5];
+ final CountDownLatch threadStarted = new CountDownLatch(1);
for (int i = 0; i < tasks.length; i++) {
Runnable r = new CheckedRunnable() {
public void realRun() throws InterruptedException {
threadStarted.countDown();
- done.await();
+ await(done);
}};
tasks[i] = p.schedule(r, 1, MILLISECONDS);
}
- assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
+ await(threadStarted);
BlockingQueue<Runnable> q = p.getQueue();
assertFalse(p.remove((Runnable)tasks[0]));
assertTrue(q.contains((Runnable)tasks[4]));
@@ -618,9 +628,6 @@
assertTrue(q.contains((Runnable)tasks[3]));
assertTrue(p.remove((Runnable)tasks[3]));
assertFalse(q.contains((Runnable)tasks[3]));
- } finally {
- done.countDown();
- joinPool(p);
}
}
@@ -628,12 +635,15 @@
* purge eventually removes cancelled tasks from the queue
*/
public void testPurge() throws InterruptedException {
- ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
- ScheduledFuture[] tasks = new ScheduledFuture[5];
- for (int i = 0; i < tasks.length; i++)
- tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(),
- LONG_DELAY_MS, MILLISECONDS);
- try {
+ final ScheduledFuture[] tasks = new ScheduledFuture[5];
+ final Runnable releaser = new Runnable() { public void run() {
+ for (ScheduledFuture task : tasks)
+ if (task != null) task.cancel(true); }};
+ final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
+ try (PoolCleaner cleaner = cleaner(p, releaser)) {
+ for (int i = 0; i < tasks.length; i++)
+ tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(),
+ LONG_DELAY_MS, MILLISECONDS);
int max = tasks.length;
if (tasks[4].cancel(true)) --max;
if (tasks[3].cancel(true)) --max;
@@ -645,159 +655,186 @@
long count = p.getTaskCount();
if (count == max)
return;
- } while (millisElapsedSince(startTime) < MEDIUM_DELAY_MS);
+ } while (millisElapsedSince(startTime) < LONG_DELAY_MS);
fail("Purge failed to remove cancelled tasks");
- } finally {
- for (ScheduledFuture task : tasks)
- task.cancel(true);
- joinPool(p);
}
}
/**
- * shutdownNow returns a list containing tasks that were not run
+ * shutdownNow returns a list containing tasks that were not run,
+ * and those tasks are drained from the queue
*/
- public void testShutdownNow() {
- ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
- for (int i = 0; i < 5; i++)
- p.schedule(new SmallPossiblyInterruptedRunnable(),
- LONG_DELAY_MS, MILLISECONDS);
+ public void testShutdownNow() throws InterruptedException {
+ final int poolSize = 2;
+ final int count = 5;
+ final AtomicInteger ran = new AtomicInteger(0);
+ final ScheduledThreadPoolExecutor p =
+ new ScheduledThreadPoolExecutor(poolSize);
+ final CountDownLatch threadsStarted = new CountDownLatch(poolSize);
+ Runnable waiter = new CheckedRunnable() { public void realRun() {
+ threadsStarted.countDown();
+ try {
+ MILLISECONDS.sleep(2 * LONG_DELAY_MS);
+ } catch (InterruptedException success) {}
+ ran.getAndIncrement();
+ }};
+ for (int i = 0; i < count; i++)
+ p.execute(waiter);
+ await(threadsStarted);
+ assertEquals(poolSize, p.getActiveCount());
+ assertEquals(0, p.getCompletedTaskCount());
+ final List<Runnable> queuedTasks;
try {
- List<Runnable> l = p.shutdownNow();
- assertTrue(p.isShutdown());
- assertEquals(5, l.size());
+ queuedTasks = p.shutdownNow();
} catch (SecurityException ok) {
- // Allowed in case test doesn't have privs
- } finally {
- joinPool(p);
+ return; // Allowed in case test doesn't have privs
}
- }
-
- /**
- * In default setting, shutdown cancels periodic but not delayed
- * tasks at shutdown
- */
- public void testShutdown1() throws InterruptedException {
- ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
- assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
- assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
-
- ScheduledFuture[] tasks = new ScheduledFuture[5];
- for (int i = 0; i < tasks.length; i++)
- tasks[i] = p.schedule(new NoOpRunnable(),
- SHORT_DELAY_MS, MILLISECONDS);
- try { p.shutdown(); } catch (SecurityException ok) { return; }
- BlockingQueue<Runnable> q = p.getQueue();
- for (ScheduledFuture task : tasks) {
- assertFalse(task.isDone());
- assertFalse(task.isCancelled());
- assertTrue(q.contains(task));
- }
- assertTrue(p.isShutdown());
- assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS));
- assertTrue(p.isTerminated());
- for (ScheduledFuture task : tasks) {
- assertTrue(task.isDone());
- assertFalse(task.isCancelled());
- }
- }
-
- /**
- * If setExecuteExistingDelayedTasksAfterShutdownPolicy is false,
- * delayed tasks are cancelled at shutdown
- */
- public void testShutdown2() throws InterruptedException {
- ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
- p.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
- assertFalse(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
- assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
- ScheduledFuture[] tasks = new ScheduledFuture[5];
- for (int i = 0; i < tasks.length; i++)
- tasks[i] = p.schedule(new NoOpRunnable(),
- SHORT_DELAY_MS, MILLISECONDS);
- BlockingQueue q = p.getQueue();
- assertEquals(tasks.length, q.size());
- try { p.shutdown(); } catch (SecurityException ok) { return; }
- assertTrue(p.isShutdown());
- assertTrue(q.isEmpty());
- assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS));
- assertTrue(p.isTerminated());
- for (ScheduledFuture task : tasks) {
- assertTrue(task.isDone());
- assertTrue(task.isCancelled());
- }
- }
-
- /**
- * If setContinueExistingPeriodicTasksAfterShutdownPolicy is set false,
- * periodic tasks are cancelled at shutdown
- */
- public void testShutdown3() throws InterruptedException {
- ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
- assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
- assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
- p.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
- assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
- assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
- long initialDelay = LONG_DELAY_MS;
- ScheduledFuture task =
- p.scheduleAtFixedRate(new NoOpRunnable(), initialDelay,
- 5, MILLISECONDS);
- try { p.shutdown(); } catch (SecurityException ok) { return; }
assertTrue(p.isShutdown());
assertTrue(p.getQueue().isEmpty());
- assertTrue(task.isDone());
- assertTrue(task.isCancelled());
- joinPool(p);
+ assertEquals(count - poolSize, queuedTasks.size());
+ assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
+ assertTrue(p.isTerminated());
+ assertEquals(poolSize, ran.get());
+ assertEquals(poolSize, p.getCompletedTaskCount());
}
/**
- * if setContinueExistingPeriodicTasksAfterShutdownPolicy is true,
- * periodic tasks are not cancelled at shutdown
+ * shutdownNow returns a list containing tasks that were not run,
+ * and those tasks are drained from the queue
*/
- public void testShutdown4() throws InterruptedException {
- ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
- final CountDownLatch counter = new CountDownLatch(2);
+ public void testShutdownNow_delayedTasks() throws InterruptedException {
+ final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
+ List<ScheduledFuture> tasks = new ArrayList<>();
+ for (int i = 0; i < 3; i++) {
+ Runnable r = new NoOpRunnable();
+ tasks.add(p.schedule(r, 9, SECONDS));
+ tasks.add(p.scheduleAtFixedRate(r, 9, 9, SECONDS));
+ tasks.add(p.scheduleWithFixedDelay(r, 9, 9, SECONDS));
+ }
+ if (testImplementationDetails)
+ assertEquals(new HashSet(tasks), new HashSet(p.getQueue()));
+ final List<Runnable> queuedTasks;
try {
- p.setContinueExistingPeriodicTasksAfterShutdownPolicy(true);
- assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
- assertTrue(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
- final Runnable r = new CheckedRunnable() {
- public void realRun() {
- counter.countDown();
- }};
- ScheduledFuture task =
- p.scheduleAtFixedRate(r, 1, 1, MILLISECONDS);
+ queuedTasks = p.shutdownNow();
+ } catch (SecurityException ok) {
+ return; // Allowed in case test doesn't have privs
+ }
+ assertTrue(p.isShutdown());
+ assertTrue(p.getQueue().isEmpty());
+ if (testImplementationDetails)
+ assertEquals(new HashSet(tasks), new HashSet(queuedTasks));
+ assertEquals(tasks.size(), queuedTasks.size());
+ for (ScheduledFuture task : tasks) {
assertFalse(task.isDone());
assertFalse(task.isCancelled());
- try { p.shutdown(); } catch (SecurityException ok) { return; }
- assertFalse(task.isCancelled());
- assertFalse(p.isTerminated());
- assertTrue(p.isShutdown());
- assertTrue(counter.await(SMALL_DELAY_MS, MILLISECONDS));
- assertFalse(task.isCancelled());
- assertTrue(task.cancel(false));
- assertTrue(task.isDone());
- assertTrue(task.isCancelled());
- assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS));
- assertTrue(p.isTerminated());
}
- finally {
- joinPool(p);
- }
+ assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
+ assertTrue(p.isTerminated());
}
/**
+ * By default, periodic tasks are cancelled at shutdown.
+ * By default, delayed tasks keep running after shutdown.
+ * Check that changing the default values work:
+ * - setExecuteExistingDelayedTasksAfterShutdownPolicy
+ * - setContinueExistingPeriodicTasksAfterShutdownPolicy
+ */
+ public void testShutdown_cancellation() throws Exception {
+ Boolean[] allBooleans = { null, Boolean.FALSE, Boolean.TRUE };
+ for (Boolean policy : allBooleans)
+ {
+ final int poolSize = 2;
+ final ScheduledThreadPoolExecutor p
+ = new ScheduledThreadPoolExecutor(poolSize);
+ final boolean effectiveDelayedPolicy = (policy != Boolean.FALSE);
+ final boolean effectivePeriodicPolicy = (policy == Boolean.TRUE);
+ final boolean effectiveRemovePolicy = (policy == Boolean.TRUE);
+ if (policy != null) {
+ p.setExecuteExistingDelayedTasksAfterShutdownPolicy(policy);
+ p.setContinueExistingPeriodicTasksAfterShutdownPolicy(policy);
+ p.setRemoveOnCancelPolicy(policy);
+ }
+ assertEquals(effectiveDelayedPolicy,
+ p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
+ assertEquals(effectivePeriodicPolicy,
+ p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
+ assertEquals(effectiveRemovePolicy,
+ p.getRemoveOnCancelPolicy());
+ // Strategy: Wedge the pool with poolSize "blocker" threads
+ final AtomicInteger ran = new AtomicInteger(0);
+ final CountDownLatch poolBlocked = new CountDownLatch(poolSize);
+ final CountDownLatch unblock = new CountDownLatch(1);
+ final CountDownLatch periodicLatch1 = new CountDownLatch(2);
+ final CountDownLatch periodicLatch2 = new CountDownLatch(2);
+ Runnable task = new CheckedRunnable() { public void realRun()
+ throws InterruptedException {
+ poolBlocked.countDown();
+ assertTrue(unblock.await(LONG_DELAY_MS, MILLISECONDS));
+ ran.getAndIncrement();
+ }};
+ List<Future<?>> blockers = new ArrayList<>();
+ List<Future<?>> periodics = new ArrayList<>();
+ List<Future<?>> delayeds = new ArrayList<>();
+ for (int i = 0; i < poolSize; i++)
+ blockers.add(p.submit(task));
+ assertTrue(poolBlocked.await(LONG_DELAY_MS, MILLISECONDS));
+
+ periodics.add(p.scheduleAtFixedRate(countDowner(periodicLatch1),
+ 1, 1, MILLISECONDS));
+ periodics.add(p.scheduleWithFixedDelay(countDowner(periodicLatch2),
+ 1, 1, MILLISECONDS));
+ delayeds.add(p.schedule(task, 1, MILLISECONDS));
+
+ assertTrue(p.getQueue().containsAll(periodics));
+ assertTrue(p.getQueue().containsAll(delayeds));
+ try { p.shutdown(); } catch (SecurityException ok) { return; }
+ assertTrue(p.isShutdown());
+ assertFalse(p.isTerminated());
+ for (Future<?> periodic : periodics) {
+ assertTrue(effectivePeriodicPolicy ^ periodic.isCancelled());
+ assertTrue(effectivePeriodicPolicy ^ periodic.isDone());
+ }
+ for (Future<?> delayed : delayeds) {
+ assertTrue(effectiveDelayedPolicy ^ delayed.isCancelled());
+ assertTrue(effectiveDelayedPolicy ^ delayed.isDone());
+ }
+ if (testImplementationDetails) {
+ assertEquals(effectivePeriodicPolicy,
+ p.getQueue().containsAll(periodics));
+ assertEquals(effectiveDelayedPolicy,
+ p.getQueue().containsAll(delayeds));
+ }
+ // Release all pool threads
+ unblock.countDown();
+
+ for (Future<?> delayed : delayeds) {
+ if (effectiveDelayedPolicy) {
+ assertNull(delayed.get());
+ }
+ }
+ if (effectivePeriodicPolicy) {
+ assertTrue(periodicLatch1.await(LONG_DELAY_MS, MILLISECONDS));
+ assertTrue(periodicLatch2.await(LONG_DELAY_MS, MILLISECONDS));
+ for (Future<?> periodic : periodics) {
+ assertTrue(periodic.cancel(false));
+ assertTrue(periodic.isCancelled());
+ assertTrue(periodic.isDone());
+ }
+ }
+ assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
+ assertTrue(p.isTerminated());
+ assertEquals(2 + (effectiveDelayedPolicy ? 1 : 0), ran.get());
+ }}
+
+ /**
* completed submit of callable returns result
*/
public void testSubmitCallable() throws Exception {
- ExecutorService e = new ScheduledThreadPoolExecutor(2);
- try {
+ final ExecutorService e = new ScheduledThreadPoolExecutor(2);
+ try (PoolCleaner cleaner = cleaner(e)) {
Future<String> future = e.submit(new StringTask());
String result = future.get();
assertSame(TEST_STRING, result);
- } finally {
- joinPool(e);
}
}
@@ -805,13 +842,11 @@
* completed submit of runnable returns successfully
*/
public void testSubmitRunnable() throws Exception {
- ExecutorService e = new ScheduledThreadPoolExecutor(2);
- try {
+ final ExecutorService e = new ScheduledThreadPoolExecutor(2);
+ try (PoolCleaner cleaner = cleaner(e)) {
Future<?> future = e.submit(new NoOpRunnable());
future.get();
assertTrue(future.isDone());
- } finally {
- joinPool(e);
}
}
@@ -819,13 +854,11 @@
* completed submit of (runnable, result) returns result
*/
public void testSubmitRunnable2() throws Exception {
- ExecutorService e = new ScheduledThreadPoolExecutor(2);
- try {
+ final ExecutorService e = new ScheduledThreadPoolExecutor(2);
+ try (PoolCleaner cleaner = cleaner(e)) {
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
String result = future.get();
assertSame(TEST_STRING, result);
- } finally {
- joinPool(e);
}
}
@@ -833,13 +866,12 @@
* invokeAny(null) throws NPE
*/
public void testInvokeAny1() throws Exception {
- ExecutorService e = new ScheduledThreadPoolExecutor(2);
- try {
- e.invokeAny(null);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
- joinPool(e);
+ final ExecutorService e = new ScheduledThreadPoolExecutor(2);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ try {
+ e.invokeAny(null);
+ shouldThrow();
+ } catch (NullPointerException success) {}
}
}
@@ -847,13 +879,12 @@
* invokeAny(empty collection) throws IAE
*/
public void testInvokeAny2() throws Exception {
- ExecutorService e = new ScheduledThreadPoolExecutor(2);
- try {
- e.invokeAny(new ArrayList<Callable<String>>());
- shouldThrow();
- } catch (IllegalArgumentException success) {
- } finally {
- joinPool(e);
+ final ExecutorService e = new ScheduledThreadPoolExecutor(2);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ try {
+ e.invokeAny(new ArrayList<Callable<String>>());
+ shouldThrow();
+ } catch (IllegalArgumentException success) {}
}
}
@@ -862,17 +893,16 @@
*/
public void testInvokeAny3() throws Exception {
CountDownLatch latch = new CountDownLatch(1);
- ExecutorService e = new ScheduledThreadPoolExecutor(2);
- List<Callable<String>> l = new ArrayList<Callable<String>>();
- l.add(latchAwaitingStringTask(latch));
- l.add(null);
- try {
- e.invokeAny(l);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
+ final ExecutorService e = new ScheduledThreadPoolExecutor(2);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ List<Callable<String>> l = new ArrayList<Callable<String>>();
+ l.add(latchAwaitingStringTask(latch));
+ l.add(null);
+ try {
+ e.invokeAny(l);
+ shouldThrow();
+ } catch (NullPointerException success) {}
latch.countDown();
- joinPool(e);
}
}
@@ -880,16 +910,16 @@
* invokeAny(c) throws ExecutionException if no task completes
*/
public void testInvokeAny4() throws Exception {
- ExecutorService e = new ScheduledThreadPoolExecutor(2);
- List<Callable<String>> l = new ArrayList<Callable<String>>();
- l.add(new NPETask());
- try {
- e.invokeAny(l);
- shouldThrow();
- } catch (ExecutionException success) {
- assertTrue(success.getCause() instanceof NullPointerException);
- } finally {
- joinPool(e);
+ final ExecutorService e = new ScheduledThreadPoolExecutor(2);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ List<Callable<String>> l = new ArrayList<Callable<String>>();
+ l.add(new NPETask());
+ try {
+ e.invokeAny(l);
+ shouldThrow();
+ } catch (ExecutionException success) {
+ assertTrue(success.getCause() instanceof NullPointerException);
+ }
}
}
@@ -897,15 +927,13 @@
* invokeAny(c) returns result of some task
*/
public void testInvokeAny5() throws Exception {
- ExecutorService e = new ScheduledThreadPoolExecutor(2);
- try {
+ final ExecutorService e = new ScheduledThreadPoolExecutor(2);
+ try (PoolCleaner cleaner = cleaner(e)) {
List<Callable<String>> l = new ArrayList<Callable<String>>();
l.add(new StringTask());
l.add(new StringTask());
String result = e.invokeAny(l);
assertSame(TEST_STRING, result);
- } finally {
- joinPool(e);
}
}
@@ -913,13 +941,12 @@
* invokeAll(null) throws NPE
*/
public void testInvokeAll1() throws Exception {
- ExecutorService e = new ScheduledThreadPoolExecutor(2);
- try {
- e.invokeAll(null);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
- joinPool(e);
+ final ExecutorService e = new ScheduledThreadPoolExecutor(2);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ try {
+ e.invokeAll(null);
+ shouldThrow();
+ } catch (NullPointerException success) {}
}
}
@@ -927,12 +954,10 @@
* invokeAll(empty collection) returns empty collection
*/
public void testInvokeAll2() throws Exception {
- ExecutorService e = new ScheduledThreadPoolExecutor(2);
- try {
+ final ExecutorService e = new ScheduledThreadPoolExecutor(2);
+ try (PoolCleaner cleaner = cleaner(e)) {
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
assertTrue(r.isEmpty());
- } finally {
- joinPool(e);
}
}
@@ -940,16 +965,15 @@
* invokeAll(c) throws NPE if c has null elements
*/
public void testInvokeAll3() throws Exception {
- ExecutorService e = new ScheduledThreadPoolExecutor(2);
- List<Callable<String>> l = new ArrayList<Callable<String>>();
- l.add(new StringTask());
- l.add(null);
- try {
- e.invokeAll(l);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
- joinPool(e);
+ final ExecutorService e = new ScheduledThreadPoolExecutor(2);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ List<Callable<String>> l = new ArrayList<Callable<String>>();
+ l.add(new StringTask());
+ l.add(null);
+ try {
+ e.invokeAll(l);
+ shouldThrow();
+ } catch (NullPointerException success) {}
}
}
@@ -957,18 +981,18 @@
* get of invokeAll(c) throws exception on failed task
*/
public void testInvokeAll4() throws Exception {
- ExecutorService e = new ScheduledThreadPoolExecutor(2);
- List<Callable<String>> l = new ArrayList<Callable<String>>();
- l.add(new NPETask());
- List<Future<String>> futures = e.invokeAll(l);
- assertEquals(1, futures.size());
- try {
- futures.get(0).get();
- shouldThrow();
- } catch (ExecutionException success) {
- assertTrue(success.getCause() instanceof NullPointerException);
- } finally {
- joinPool(e);
+ final ExecutorService e = new ScheduledThreadPoolExecutor(2);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ List<Callable<String>> l = new ArrayList<Callable<String>>();
+ l.add(new NPETask());
+ List<Future<String>> futures = e.invokeAll(l);
+ assertEquals(1, futures.size());
+ try {
+ futures.get(0).get();
+ shouldThrow();
+ } catch (ExecutionException success) {
+ assertTrue(success.getCause() instanceof NullPointerException);
+ }
}
}
@@ -976,8 +1000,8 @@
* invokeAll(c) returns results of all completed tasks
*/
public void testInvokeAll5() throws Exception {
- ExecutorService e = new ScheduledThreadPoolExecutor(2);
- try {
+ final ExecutorService e = new ScheduledThreadPoolExecutor(2);
+ try (PoolCleaner cleaner = cleaner(e)) {
List<Callable<String>> l = new ArrayList<Callable<String>>();
l.add(new StringTask());
l.add(new StringTask());
@@ -985,8 +1009,6 @@
assertEquals(2, futures.size());
for (Future<String> future : futures)
assertSame(TEST_STRING, future.get());
- } finally {
- joinPool(e);
}
}
@@ -994,13 +1016,12 @@
* timed invokeAny(null) throws NPE
*/
public void testTimedInvokeAny1() throws Exception {
- ExecutorService e = new ScheduledThreadPoolExecutor(2);
- try {
- e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
- joinPool(e);
+ final ExecutorService e = new ScheduledThreadPoolExecutor(2);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ try {
+ e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
+ shouldThrow();
+ } catch (NullPointerException success) {}
}
}
@@ -1008,15 +1029,14 @@
* timed invokeAny(,,null) throws NPE
*/
public void testTimedInvokeAnyNullTimeUnit() throws Exception {
- ExecutorService e = new ScheduledThreadPoolExecutor(2);
- List<Callable<String>> l = new ArrayList<Callable<String>>();
- l.add(new StringTask());
- try {
- e.invokeAny(l, MEDIUM_DELAY_MS, null);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
- joinPool(e);
+ final ExecutorService e = new ScheduledThreadPoolExecutor(2);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ List<Callable<String>> l = new ArrayList<Callable<String>>();
+ l.add(new StringTask());
+ try {
+ e.invokeAny(l, MEDIUM_DELAY_MS, null);
+ shouldThrow();
+ } catch (NullPointerException success) {}
}
}
@@ -1024,13 +1044,12 @@
* timed invokeAny(empty collection) throws IAE
*/
public void testTimedInvokeAny2() throws Exception {
- ExecutorService e = new ScheduledThreadPoolExecutor(2);
- try {
- e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
- shouldThrow();
- } catch (IllegalArgumentException success) {
- } finally {
- joinPool(e);
+ final ExecutorService e = new ScheduledThreadPoolExecutor(2);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ try {
+ e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
+ shouldThrow();
+ } catch (IllegalArgumentException success) {}
}
}
@@ -1039,17 +1058,16 @@
*/
public void testTimedInvokeAny3() throws Exception {
CountDownLatch latch = new CountDownLatch(1);
- ExecutorService e = new ScheduledThreadPoolExecutor(2);
- List<Callable<String>> l = new ArrayList<Callable<String>>();
- l.add(latchAwaitingStringTask(latch));
- l.add(null);
- try {
- e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
+ final ExecutorService e = new ScheduledThreadPoolExecutor(2);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ List<Callable<String>> l = new ArrayList<Callable<String>>();
+ l.add(latchAwaitingStringTask(latch));
+ l.add(null);
+ try {
+ e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
+ shouldThrow();
+ } catch (NullPointerException success) {}
latch.countDown();
- joinPool(e);
}
}
@@ -1057,16 +1075,18 @@
* timed invokeAny(c) throws ExecutionException if no task completes
*/
public void testTimedInvokeAny4() throws Exception {
- ExecutorService e = new ScheduledThreadPoolExecutor(2);
- List<Callable<String>> l = new ArrayList<Callable<String>>();
- l.add(new NPETask());
- try {
- e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
- shouldThrow();
- } catch (ExecutionException success) {
- assertTrue(success.getCause() instanceof NullPointerException);
- } finally {
- joinPool(e);
+ final ExecutorService e = new ScheduledThreadPoolExecutor(2);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ long startTime = System.nanoTime();
+ List<Callable<String>> l = new ArrayList<Callable<String>>();
+ l.add(new NPETask());
+ try {
+ e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
+ shouldThrow();
+ } catch (ExecutionException success) {
+ assertTrue(success.getCause() instanceof NullPointerException);
+ }
+ assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
}
}
@@ -1074,15 +1094,15 @@
* timed invokeAny(c) returns result of some task
*/
public void testTimedInvokeAny5() throws Exception {
- ExecutorService e = new ScheduledThreadPoolExecutor(2);
- try {
+ final ExecutorService e = new ScheduledThreadPoolExecutor(2);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ long startTime = System.nanoTime();
List<Callable<String>> l = new ArrayList<Callable<String>>();
l.add(new StringTask());
l.add(new StringTask());
- String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
+ String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
assertSame(TEST_STRING, result);
- } finally {
- joinPool(e);
+ assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
}
}
@@ -1090,13 +1110,12 @@
* timed invokeAll(null) throws NPE
*/
public void testTimedInvokeAll1() throws Exception {
- ExecutorService e = new ScheduledThreadPoolExecutor(2);
- try {
- e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
- joinPool(e);
+ final ExecutorService e = new ScheduledThreadPoolExecutor(2);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ try {
+ e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
+ shouldThrow();
+ } catch (NullPointerException success) {}
}
}
@@ -1104,15 +1123,14 @@
* timed invokeAll(,,null) throws NPE
*/
public void testTimedInvokeAllNullTimeUnit() throws Exception {
- ExecutorService e = new ScheduledThreadPoolExecutor(2);
- List<Callable<String>> l = new ArrayList<Callable<String>>();
- l.add(new StringTask());
- try {
- e.invokeAll(l, MEDIUM_DELAY_MS, null);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
- joinPool(e);
+ final ExecutorService e = new ScheduledThreadPoolExecutor(2);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ List<Callable<String>> l = new ArrayList<Callable<String>>();
+ l.add(new StringTask());
+ try {
+ e.invokeAll(l, MEDIUM_DELAY_MS, null);
+ shouldThrow();
+ } catch (NullPointerException success) {}
}
}
@@ -1120,12 +1138,11 @@
* timed invokeAll(empty collection) returns empty collection
*/
public void testTimedInvokeAll2() throws Exception {
- ExecutorService e = new ScheduledThreadPoolExecutor(2);
- try {
- List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
+ final ExecutorService e = new ScheduledThreadPoolExecutor(2);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(),
+ MEDIUM_DELAY_MS, MILLISECONDS);
assertTrue(r.isEmpty());
- } finally {
- joinPool(e);
}
}
@@ -1133,16 +1150,15 @@
* timed invokeAll(c) throws NPE if c has null elements
*/
public void testTimedInvokeAll3() throws Exception {
- ExecutorService e = new ScheduledThreadPoolExecutor(2);
- List<Callable<String>> l = new ArrayList<Callable<String>>();
- l.add(new StringTask());
- l.add(null);
- try {
- e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
- joinPool(e);
+ final ExecutorService e = new ScheduledThreadPoolExecutor(2);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ List<Callable<String>> l = new ArrayList<Callable<String>>();
+ l.add(new StringTask());
+ l.add(null);
+ try {
+ e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
+ shouldThrow();
+ } catch (NullPointerException success) {}
}
}
@@ -1150,19 +1166,19 @@
* get of element of invokeAll(c) throws exception on failed task
*/
public void testTimedInvokeAll4() throws Exception {
- ExecutorService e = new ScheduledThreadPoolExecutor(2);
- List<Callable<String>> l = new ArrayList<Callable<String>>();
- l.add(new NPETask());
- List<Future<String>> futures =
- e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
- assertEquals(1, futures.size());
- try {
- futures.get(0).get();
- shouldThrow();
- } catch (ExecutionException success) {
- assertTrue(success.getCause() instanceof NullPointerException);
- } finally {
- joinPool(e);
+ final ExecutorService e = new ScheduledThreadPoolExecutor(2);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ List<Callable<String>> l = new ArrayList<Callable<String>>();
+ l.add(new NPETask());
+ List<Future<String>> futures =
+ e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
+ assertEquals(1, futures.size());
+ try {
+ futures.get(0).get();
+ shouldThrow();
+ } catch (ExecutionException success) {
+ assertTrue(success.getCause() instanceof NullPointerException);
+ }
}
}
@@ -1170,18 +1186,16 @@
* timed invokeAll(c) returns results of all completed tasks
*/
public void testTimedInvokeAll5() throws Exception {
- ExecutorService e = new ScheduledThreadPoolExecutor(2);
- try {
+ final ExecutorService e = new ScheduledThreadPoolExecutor(2);
+ try (PoolCleaner cleaner = cleaner(e)) {
List<Callable<String>> l = new ArrayList<Callable<String>>();
l.add(new StringTask());
l.add(new StringTask());
List<Future<String>> futures =
- e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
+ e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
assertEquals(2, futures.size());
for (Future<String> future : futures)
assertSame(TEST_STRING, future.get());
- } finally {
- joinPool(e);
}
}
@@ -1189,21 +1203,60 @@
* timed invokeAll(c) cancels tasks not completed by timeout
*/
public void testTimedInvokeAll6() throws Exception {
- ExecutorService e = new ScheduledThreadPoolExecutor(2);
- try {
- List<Callable<String>> l = new ArrayList<Callable<String>>();
- l.add(new StringTask());
- l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
- l.add(new StringTask());
- List<Future<String>> futures =
- e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
- assertEquals(l.size(), futures.size());
- for (Future future : futures)
- assertTrue(future.isDone());
- assertFalse(futures.get(0).isCancelled());
- assertTrue(futures.get(1).isCancelled());
- } finally {
- joinPool(e);
+ for (long timeout = timeoutMillis();;) {
+ final CountDownLatch done = new CountDownLatch(1);
+ final Callable<String> waiter = new CheckedCallable<String>() {
+ public String realCall() {
+ try { done.await(LONG_DELAY_MS, MILLISECONDS); }
+ catch (InterruptedException ok) {}
+ return "1"; }};
+ final ExecutorService p = new ScheduledThreadPoolExecutor(2);
+ try (PoolCleaner cleaner = cleaner(p, done)) {
+ List<Callable<String>> tasks = new ArrayList<>();
+ tasks.add(new StringTask("0"));
+ tasks.add(waiter);
+ tasks.add(new StringTask("2"));
+ long startTime = System.nanoTime();
+ List<Future<String>> futures =
+ p.invokeAll(tasks, timeout, MILLISECONDS);
+ assertEquals(tasks.size(), futures.size());
+ assertTrue(millisElapsedSince(startTime) >= timeout);
+ for (Future future : futures)
+ assertTrue(future.isDone());
+ assertTrue(futures.get(1).isCancelled());
+ try {
+ assertEquals("0", futures.get(0).get());
+ assertEquals("2", futures.get(2).get());
+ break;
+ } catch (CancellationException retryWithLongerTimeout) {
+ timeout *= 2;
+ if (timeout >= LONG_DELAY_MS / 2)
+ fail("expected exactly one task to be cancelled");
+ }
+ }
+ }
+ }
+
+ /**
+ * A fixed delay task with overflowing period should not prevent a
+ * one-shot task from executing.
+ * https://bugs.openjdk.java.net/browse/JDK-8051859
+ */
+ public void testScheduleWithFixedDelay_overflow() throws Exception {
+ final CountDownLatch delayedDone = new CountDownLatch(1);
+ final CountDownLatch immediateDone = new CountDownLatch(1);
+ final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
+ try (PoolCleaner cleaner = cleaner(p)) {
+ final Runnable immediate = new Runnable() { public void run() {
+ immediateDone.countDown();
+ }};
+ final Runnable delayed = new Runnable() { public void run() {
+ delayedDone.countDown();
+ p.submit(immediate);
+ }};
+ p.scheduleWithFixedDelay(delayed, 0L, Long.MAX_VALUE, SECONDS);
+ await(delayedDone);
+ await(immediateDone);
}
}
diff --git a/jsr166-tests/src/test/java/jsr166/SemaphoreTest.java b/jsr166-tests/src/test/java/jsr166/SemaphoreTest.java
index db4f4b4..09c82c8 100644
--- a/jsr166-tests/src/test/java/jsr166/SemaphoreTest.java
+++ b/jsr166-tests/src/test/java/jsr166/SemaphoreTest.java
@@ -26,8 +26,9 @@
// main(suite(), args);
// }
// public static Test suite() {
- // return new TestSuite(...);
+ // return new TestSuite(SemaphoreTest.class);
// }
+
/**
* Subclass to expose protected methods
*/
@@ -471,11 +472,16 @@
clone.release();
assertEquals(2, s.availablePermits());
assertEquals(1, clone.availablePermits());
+ assertFalse(s.hasQueuedThreads());
+ assertFalse(clone.hasQueuedThreads());
+ } catch (InterruptedException e) { threadUnexpectedException(e); }
- s = new Semaphore(0, fair);
+ {
+ PublicSemaphore s = new PublicSemaphore(0, fair);
Thread t = newStartedThread(new InterruptibleLockRunnable(s));
- waitForQueuedThreads(s);
- clone = serialClone(s);
+ // waitForQueuedThreads(s); // suffers from "flicker", so ...
+ waitForQueuedThread(s, t); // ... we use this instead
+ PublicSemaphore clone = serialClone(s);
assertEquals(fair, s.isFair());
assertEquals(fair, clone.isFair());
assertEquals(0, s.availablePermits());
@@ -486,7 +492,7 @@
awaitTermination(t);
assertFalse(s.hasQueuedThreads());
assertFalse(clone.hasQueuedThreads());
- } catch (InterruptedException e) { threadUnexpectedException(e); }
+ }
}
/**
@@ -594,7 +600,7 @@
s.acquire(3);
}});
- waitForQueuedThreads(s);
+ waitForQueuedThread(s, t1);
Thread t2 = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
diff --git a/jsr166-tests/src/test/java/jsr166/StampedLockTest.java b/jsr166-tests/src/test/java/jsr166/StampedLockTest.java
new file mode 100644
index 0000000..d347c7d
--- /dev/null
+++ b/jsr166-tests/src/test/java/jsr166/StampedLockTest.java
@@ -0,0 +1,884 @@
+/*
+ * Written by Doug Lea and Martin Buchholz
+ * with assistance from members of JCP JSR-166 Expert Group and
+ * released to the public domain, as explained at
+ * http://creativecommons.org/publicdomain/zero/1.0/
+ */
+
+package jsr166;
+
+import static java.util.concurrent.TimeUnit.MILLISECONDS;
+
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.StampedLock;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+public class StampedLockTest 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(StampedLockTest.class);
+ // }
+
+ /**
+ * A runnable calling writeLockInterruptibly
+ */
+ class InterruptibleLockRunnable extends CheckedRunnable {
+ final StampedLock lock;
+ InterruptibleLockRunnable(StampedLock l) { lock = l; }
+ public void realRun() throws InterruptedException {
+ lock.writeLockInterruptibly();
+ }
+ }
+
+ /**
+ * A runnable calling writeLockInterruptibly that expects to be
+ * interrupted
+ */
+ class InterruptedLockRunnable extends CheckedInterruptedRunnable {
+ final StampedLock lock;
+ InterruptedLockRunnable(StampedLock l) { lock = l; }
+ public void realRun() throws InterruptedException {
+ lock.writeLockInterruptibly();
+ }
+ }
+
+ /**
+ * Releases write lock, checking isWriteLocked before and after
+ */
+ void releaseWriteLock(StampedLock lock, long s) {
+ assertTrue(lock.isWriteLocked());
+ lock.unlockWrite(s);
+ assertFalse(lock.isWriteLocked());
+ }
+
+ /**
+ * Constructed StampedLock is in unlocked state
+ */
+ public void testConstructor() {
+ StampedLock lock;
+ lock = new StampedLock();
+ assertFalse(lock.isWriteLocked());
+ assertFalse(lock.isReadLocked());
+ assertEquals(lock.getReadLockCount(), 0);
+ }
+
+ /**
+ * write-locking and read-locking an unlocked lock succeed
+ */
+ public void testLock() {
+ StampedLock lock = new StampedLock();
+ assertFalse(lock.isWriteLocked());
+ assertFalse(lock.isReadLocked());
+ assertEquals(lock.getReadLockCount(), 0);
+ long s = lock.writeLock();
+ assertTrue(lock.isWriteLocked());
+ assertFalse(lock.isReadLocked());
+ assertEquals(lock.getReadLockCount(), 0);
+ lock.unlockWrite(s);
+ assertFalse(lock.isWriteLocked());
+ assertFalse(lock.isReadLocked());
+ assertEquals(lock.getReadLockCount(), 0);
+ long rs = lock.readLock();
+ assertFalse(lock.isWriteLocked());
+ assertTrue(lock.isReadLocked());
+ assertEquals(lock.getReadLockCount(), 1);
+ lock.unlockRead(rs);
+ assertFalse(lock.isWriteLocked());
+ assertFalse(lock.isReadLocked());
+ assertEquals(lock.getReadLockCount(), 0);
+ }
+
+ /**
+ * unlock releases either a read or write lock
+ */
+ public void testUnlock() {
+ StampedLock lock = new StampedLock();
+ assertFalse(lock.isWriteLocked());
+ assertFalse(lock.isReadLocked());
+ assertEquals(lock.getReadLockCount(), 0);
+ long s = lock.writeLock();
+ assertTrue(lock.isWriteLocked());
+ assertFalse(lock.isReadLocked());
+ assertEquals(lock.getReadLockCount(), 0);
+ lock.unlock(s);
+ assertFalse(lock.isWriteLocked());
+ assertFalse(lock.isReadLocked());
+ assertEquals(lock.getReadLockCount(), 0);
+ long rs = lock.readLock();
+ assertFalse(lock.isWriteLocked());
+ assertTrue(lock.isReadLocked());
+ assertEquals(lock.getReadLockCount(), 1);
+ lock.unlock(rs);
+ assertFalse(lock.isWriteLocked());
+ assertFalse(lock.isReadLocked());
+ assertEquals(lock.getReadLockCount(), 0);
+ }
+
+ /**
+ * tryUnlockRead/Write succeeds if locked in associated mode else
+ * returns false
+ */
+ public void testTryUnlock() {
+ StampedLock lock = new StampedLock();
+ assertFalse(lock.isWriteLocked());
+ assertFalse(lock.isReadLocked());
+ assertEquals(lock.getReadLockCount(), 0);
+ long s = lock.writeLock();
+ assertTrue(lock.isWriteLocked());
+ assertFalse(lock.isReadLocked());
+ assertEquals(lock.getReadLockCount(), 0);
+ assertFalse(lock.tryUnlockRead());
+ assertTrue(lock.tryUnlockWrite());
+ assertFalse(lock.tryUnlockWrite());
+ assertFalse(lock.tryUnlockRead());
+ assertFalse(lock.isWriteLocked());
+ assertFalse(lock.isReadLocked());
+ assertEquals(lock.getReadLockCount(), 0);
+ long rs = lock.readLock();
+ assertFalse(lock.isWriteLocked());
+ assertTrue(lock.isReadLocked());
+ assertEquals(lock.getReadLockCount(), 1);
+ assertFalse(lock.tryUnlockWrite());
+ assertTrue(lock.tryUnlockRead());
+ assertFalse(lock.tryUnlockRead());
+ assertFalse(lock.tryUnlockWrite());
+ assertFalse(lock.isWriteLocked());
+ assertFalse(lock.isReadLocked());
+ assertEquals(lock.getReadLockCount(), 0);
+ }
+
+ /**
+ * write-unlocking an unlocked lock throws IllegalMonitorStateException
+ */
+ public void testWriteUnlock_IMSE() {
+ StampedLock lock = new StampedLock();
+ try {
+ lock.unlockWrite(0L);
+ shouldThrow();
+ } catch (IllegalMonitorStateException success) {}
+ }
+
+ /**
+ * write-unlocking an unlocked lock throws IllegalMonitorStateException
+ */
+ public void testWriteUnlock_IMSE2() {
+ StampedLock lock = new StampedLock();
+ long s = lock.writeLock();
+ lock.unlockWrite(s);
+ try {
+ lock.unlockWrite(s);
+ shouldThrow();
+ } catch (IllegalMonitorStateException success) {}
+ }
+
+ /**
+ * write-unlocking after readlock throws IllegalMonitorStateException
+ */
+ public void testWriteUnlock_IMSE3() {
+ StampedLock lock = new StampedLock();
+ long s = lock.readLock();
+ try {
+ lock.unlockWrite(s);
+ shouldThrow();
+ } catch (IllegalMonitorStateException success) {}
+ }
+
+ /**
+ * read-unlocking an unlocked lock throws IllegalMonitorStateException
+ */
+ public void testReadUnlock_IMSE() {
+ StampedLock lock = new StampedLock();
+ long s = lock.readLock();
+ lock.unlockRead(s);
+ try {
+ lock.unlockRead(s);
+ shouldThrow();
+ } catch (IllegalMonitorStateException success) {}
+ }
+
+ /**
+ * read-unlocking an unlocked lock throws IllegalMonitorStateException
+ */
+ public void testReadUnlock_IMSE2() {
+ StampedLock lock = new StampedLock();
+ try {
+ lock.unlockRead(0L);
+ shouldThrow();
+ } catch (IllegalMonitorStateException success) {}
+ }
+
+ /**
+ * read-unlocking after writeLock throws IllegalMonitorStateException
+ */
+ public void testReadUnlock_IMSE3() {
+ StampedLock lock = new StampedLock();
+ long s = lock.writeLock();
+ try {
+ lock.unlockRead(s);
+ shouldThrow();
+ } catch (IllegalMonitorStateException success) {}
+ }
+
+ /**
+ * validate(0) fails
+ */
+ public void testValidate0() {
+ StampedLock lock = new StampedLock();
+ assertFalse(lock.validate(0L));
+ }
+
+ /**
+ * A stamp obtained from a successful lock operation validates
+ */
+ public void testValidate() throws InterruptedException {
+ StampedLock lock = new StampedLock();
+ long s = lock.writeLock();
+ assertTrue(lock.validate(s));
+ lock.unlockWrite(s);
+ s = lock.readLock();
+ assertTrue(lock.validate(s));
+ lock.unlockRead(s);
+ assertTrue((s = lock.tryWriteLock()) != 0L);
+ assertTrue(lock.validate(s));
+ lock.unlockWrite(s);
+ assertTrue((s = lock.tryReadLock()) != 0L);
+ assertTrue(lock.validate(s));
+ lock.unlockRead(s);
+ assertTrue((s = lock.tryWriteLock(100L, MILLISECONDS)) != 0L);
+ assertTrue(lock.validate(s));
+ lock.unlockWrite(s);
+ assertTrue((s = lock.tryReadLock(100L, MILLISECONDS)) != 0L);
+ assertTrue(lock.validate(s));
+ lock.unlockRead(s);
+ assertTrue((s = lock.tryOptimisticRead()) != 0L);
+ }
+
+ /**
+ * A stamp obtained from an unsuccessful lock operation does not validate
+ */
+ public void testValidate2() throws InterruptedException {
+ StampedLock lock = new StampedLock();
+ long s;
+ assertTrue((s = lock.writeLock()) != 0L);
+ assertTrue(lock.validate(s));
+ assertFalse(lock.validate(lock.tryWriteLock()));
+ assertFalse(lock.validate(lock.tryWriteLock(10L, MILLISECONDS)));
+ assertFalse(lock.validate(lock.tryReadLock()));
+ assertFalse(lock.validate(lock.tryReadLock(10L, MILLISECONDS)));
+ assertFalse(lock.validate(lock.tryOptimisticRead()));
+ lock.unlockWrite(s);
+ }
+
+ /**
+ * writeLockInterruptibly is interruptible
+ */
+ public void testWriteLockInterruptibly_Interruptible()
+ throws InterruptedException {
+ final CountDownLatch running = new CountDownLatch(1);
+ final StampedLock lock = new StampedLock();
+ long s = lock.writeLock();
+ Thread t = newStartedThread(new CheckedInterruptedRunnable() {
+ public void realRun() throws InterruptedException {
+ running.countDown();
+ lock.writeLockInterruptibly();
+ }});
+
+ running.await();
+ waitForThreadToEnterWaitState(t, 100);
+ t.interrupt();
+ awaitTermination(t);
+ releaseWriteLock(lock, s);
+ }
+
+ /**
+ * timed tryWriteLock is interruptible
+ */
+ public void testWriteTryLock_Interruptible() throws InterruptedException {
+ final CountDownLatch running = new CountDownLatch(1);
+ final StampedLock lock = new StampedLock();
+ long s = lock.writeLock();
+ Thread t = newStartedThread(new CheckedInterruptedRunnable() {
+ public void realRun() throws InterruptedException {
+ running.countDown();
+ lock.tryWriteLock(2 * LONG_DELAY_MS, MILLISECONDS);
+ }});
+
+ running.await();
+ waitForThreadToEnterWaitState(t, 100);
+ t.interrupt();
+ awaitTermination(t);
+ releaseWriteLock(lock, s);
+ }
+
+ /**
+ * readLockInterruptibly is interruptible
+ */
+ public void testReadLockInterruptibly_Interruptible()
+ throws InterruptedException {
+ final CountDownLatch running = new CountDownLatch(1);
+ final StampedLock lock = new StampedLock();
+ long s = lock.writeLock();
+ Thread t = newStartedThread(new CheckedInterruptedRunnable() {
+ public void realRun() throws InterruptedException {
+ running.countDown();
+ lock.readLockInterruptibly();
+ }});
+
+ running.await();
+ waitForThreadToEnterWaitState(t, 100);
+ t.interrupt();
+ awaitTermination(t);
+ releaseWriteLock(lock, s);
+ }
+
+ /**
+ * timed tryReadLock is interruptible
+ */
+ public void testReadTryLock_Interruptible() throws InterruptedException {
+ final CountDownLatch running = new CountDownLatch(1);
+ final StampedLock lock = new StampedLock();
+ long s = lock.writeLock();
+ Thread t = newStartedThread(new CheckedInterruptedRunnable() {
+ public void realRun() throws InterruptedException {
+ running.countDown();
+ lock.tryReadLock(2 * LONG_DELAY_MS, MILLISECONDS);
+ }});
+
+ running.await();
+ waitForThreadToEnterWaitState(t, 100);
+ t.interrupt();
+ awaitTermination(t);
+ releaseWriteLock(lock, s);
+ }
+
+ /**
+ * tryWriteLock on an unlocked lock succeeds
+ */
+ public void testWriteTryLock() {
+ final StampedLock lock = new StampedLock();
+ long s = lock.tryWriteLock();
+ assertTrue(s != 0L);
+ assertTrue(lock.isWriteLocked());
+ long s2 = lock.tryWriteLock();
+ assertEquals(s2, 0L);
+ releaseWriteLock(lock, s);
+ }
+
+ /**
+ * tryWriteLock fails if locked
+ */
+ public void testWriteTryLockWhenLocked() {
+ final StampedLock lock = new StampedLock();
+ long s = lock.writeLock();
+ Thread t = newStartedThread(new CheckedRunnable() {
+ public void realRun() {
+ long ws = lock.tryWriteLock();
+ assertTrue(ws == 0L);
+ }});
+
+ awaitTermination(t);
+ releaseWriteLock(lock, s);
+ }
+
+ /**
+ * tryReadLock fails if write-locked
+ */
+ public void testReadTryLockWhenLocked() {
+ final StampedLock lock = new StampedLock();
+ long s = lock.writeLock();
+ Thread t = newStartedThread(new CheckedRunnable() {
+ public void realRun() {
+ long rs = lock.tryReadLock();
+ assertEquals(rs, 0L);
+ }});
+
+ awaitTermination(t);
+ releaseWriteLock(lock, s);
+ }
+
+ /**
+ * Multiple threads can hold a read lock when not write-locked
+ */
+ public void testMultipleReadLocks() {
+ final StampedLock lock = new StampedLock();
+ final long s = lock.readLock();
+ Thread t = newStartedThread(new CheckedRunnable() {
+ public void realRun() throws InterruptedException {
+ long s2 = lock.tryReadLock();
+ assertTrue(s2 != 0L);
+ lock.unlockRead(s2);
+ long s3 = lock.tryReadLock(LONG_DELAY_MS, MILLISECONDS);
+ assertTrue(s3 != 0L);
+ lock.unlockRead(s3);
+ long s4 = lock.readLock();
+ lock.unlockRead(s4);
+ }});
+
+ awaitTermination(t);
+ lock.unlockRead(s);
+ }
+
+ /**
+ * A writelock succeeds only after a reading thread unlocks
+ */
+ public void testWriteAfterReadLock() throws InterruptedException {
+ final CountDownLatch running = new CountDownLatch(1);
+ final StampedLock lock = new StampedLock();
+ long rs = lock.readLock();
+ Thread t = newStartedThread(new CheckedRunnable() {
+ public void realRun() {
+ running.countDown();
+ long s = lock.writeLock();
+ lock.unlockWrite(s);
+ }});
+
+ running.await();
+ waitForThreadToEnterWaitState(t, 100);
+ assertFalse(lock.isWriteLocked());
+ lock.unlockRead(rs);
+ awaitTermination(t);
+ assertFalse(lock.isWriteLocked());
+ }
+
+ /**
+ * A writelock succeeds only after reading threads unlock
+ */
+ public void testWriteAfterMultipleReadLocks() {
+ final StampedLock lock = new StampedLock();
+ long s = lock.readLock();
+ Thread t1 = newStartedThread(new CheckedRunnable() {
+ public void realRun() {
+ long rs = lock.readLock();
+ lock.unlockRead(rs);
+ }});
+
+ awaitTermination(t1);
+
+ Thread t2 = newStartedThread(new CheckedRunnable() {
+ public void realRun() {
+ long ws = lock.writeLock();
+ lock.unlockWrite(ws);
+ }});
+
+ assertFalse(lock.isWriteLocked());
+ lock.unlockRead(s);
+ awaitTermination(t2);
+ assertFalse(lock.isWriteLocked());
+ }
+
+ /**
+ * Readlocks succeed only after a writing thread unlocks
+ */
+ public void testReadAfterWriteLock() {
+ final StampedLock lock = new StampedLock();
+ final long s = lock.writeLock();
+ Thread t1 = newStartedThread(new CheckedRunnable() {
+ public void realRun() {
+ long rs = lock.readLock();
+ lock.unlockRead(rs);
+ }});
+ Thread t2 = newStartedThread(new CheckedRunnable() {
+ public void realRun() {
+ long rs = lock.readLock();
+ lock.unlockRead(rs);
+ }});
+
+ releaseWriteLock(lock, s);
+ awaitTermination(t1);
+ awaitTermination(t2);
+ }
+
+ /**
+ * tryReadLock succeeds if readlocked but not writelocked
+ */
+ public void testTryLockWhenReadLocked() {
+ final StampedLock lock = new StampedLock();
+ long s = lock.readLock();
+ Thread t = newStartedThread(new CheckedRunnable() {
+ public void realRun() {
+ long rs = lock.tryReadLock();
+ threadAssertTrue(rs != 0L);
+ lock.unlockRead(rs);
+ }});
+
+ awaitTermination(t);
+ lock.unlockRead(s);
+ }
+
+ /**
+ * tryWriteLock fails when readlocked
+ */
+ public void testWriteTryLockWhenReadLocked() {
+ final StampedLock lock = new StampedLock();
+ long s = lock.readLock();
+ Thread t = newStartedThread(new CheckedRunnable() {
+ public void realRun() {
+ long ws = lock.tryWriteLock();
+ threadAssertEquals(ws, 0L);
+ }});
+
+ awaitTermination(t);
+ lock.unlockRead(s);
+ }
+
+ /**
+ * timed tryWriteLock times out if locked
+ */
+ public void testWriteTryLock_Timeout() {
+ final StampedLock lock = new StampedLock();
+ long s = lock.writeLock();
+ Thread t = newStartedThread(new CheckedRunnable() {
+ public void realRun() throws InterruptedException {
+ long startTime = System.nanoTime();
+ long timeoutMillis = 10;
+ long ws = lock.tryWriteLock(timeoutMillis, MILLISECONDS);
+ assertEquals(ws, 0L);
+ assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
+ }});
+
+ awaitTermination(t);
+ releaseWriteLock(lock, s);
+ }
+
+ /**
+ * timed tryReadLock times out if write-locked
+ */
+ public void testReadTryLock_Timeout() {
+ final StampedLock lock = new StampedLock();
+ long s = lock.writeLock();
+ Thread t = newStartedThread(new CheckedRunnable() {
+ public void realRun() throws InterruptedException {
+ long startTime = System.nanoTime();
+ long timeoutMillis = 10;
+ long rs = lock.tryReadLock(timeoutMillis, MILLISECONDS);
+ assertEquals(rs, 0L);
+ assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
+ }});
+
+ awaitTermination(t);
+ assertTrue(lock.isWriteLocked());
+ lock.unlockWrite(s);
+ }
+
+ /**
+ * writeLockInterruptibly succeeds if unlocked, else is interruptible
+ */
+ public void testWriteLockInterruptibly() throws InterruptedException {
+ final CountDownLatch running = new CountDownLatch(1);
+ final StampedLock lock = new StampedLock();
+ long s = lock.writeLockInterruptibly();
+ Thread t = newStartedThread(new CheckedInterruptedRunnable() {
+ public void realRun() throws InterruptedException {
+ running.countDown();
+ lock.writeLockInterruptibly();
+ }});
+
+ running.await();
+ waitForThreadToEnterWaitState(t, 100);
+ t.interrupt();
+ assertTrue(lock.isWriteLocked());
+ awaitTermination(t);
+ releaseWriteLock(lock, s);
+ }
+
+ /**
+ * readLockInterruptibly succeeds if lock free else is interruptible
+ */
+ public void testReadLockInterruptibly() throws InterruptedException {
+ final CountDownLatch running = new CountDownLatch(1);
+ final StampedLock lock = new StampedLock();
+ long s;
+ s = lock.readLockInterruptibly();
+ lock.unlockRead(s);
+ s = lock.writeLockInterruptibly();
+ Thread t = newStartedThread(new CheckedInterruptedRunnable() {
+ public void realRun() throws InterruptedException {
+ running.countDown();
+ lock.readLockInterruptibly();
+ }});
+
+ running.await();
+ waitForThreadToEnterWaitState(t, 100);
+ t.interrupt();
+ awaitTermination(t);
+ releaseWriteLock(lock, s);
+ }
+
+ /**
+ * A serialized lock deserializes as unlocked
+ */
+ public void testSerialization() {
+ StampedLock lock = new StampedLock();
+ lock.writeLock();
+ StampedLock clone = serialClone(lock);
+ assertTrue(lock.isWriteLocked());
+ assertFalse(clone.isWriteLocked());
+ long s = clone.writeLock();
+ assertTrue(clone.isWriteLocked());
+ clone.unlockWrite(s);
+ assertFalse(clone.isWriteLocked());
+ }
+
+ /**
+ * toString indicates current lock state
+ */
+ public void testToString() {
+ StampedLock lock = new StampedLock();
+ assertTrue(lock.toString().contains("Unlocked"));
+ long s = lock.writeLock();
+ assertTrue(lock.toString().contains("Write-locked"));
+ lock.unlockWrite(s);
+ s = lock.readLock();
+ assertTrue(lock.toString().contains("Read-locks"));
+ }
+
+ /**
+ * tryOptimisticRead succeeds and validates if unlocked, fails if locked
+ */
+ public void testValidateOptimistic() throws InterruptedException {
+ StampedLock lock = new StampedLock();
+ long s, p;
+ assertTrue((p = lock.tryOptimisticRead()) != 0L);
+ assertTrue(lock.validate(p));
+ assertTrue((s = lock.writeLock()) != 0L);
+ assertFalse((p = lock.tryOptimisticRead()) != 0L);
+ assertTrue(lock.validate(s));
+ lock.unlockWrite(s);
+ assertTrue((p = lock.tryOptimisticRead()) != 0L);
+ assertTrue(lock.validate(p));
+ assertTrue((s = lock.readLock()) != 0L);
+ assertTrue(lock.validate(s));
+ assertTrue((p = lock.tryOptimisticRead()) != 0L);
+ assertTrue(lock.validate(p));
+ lock.unlockRead(s);
+ assertTrue((s = lock.tryWriteLock()) != 0L);
+ assertTrue(lock.validate(s));
+ assertFalse((p = lock.tryOptimisticRead()) != 0L);
+ lock.unlockWrite(s);
+ assertTrue((s = lock.tryReadLock()) != 0L);
+ assertTrue(lock.validate(s));
+ assertTrue((p = lock.tryOptimisticRead()) != 0L);
+ lock.unlockRead(s);
+ assertTrue(lock.validate(p));
+ assertTrue((s = lock.tryWriteLock(100L, MILLISECONDS)) != 0L);
+ assertFalse((p = lock.tryOptimisticRead()) != 0L);
+ assertTrue(lock.validate(s));
+ lock.unlockWrite(s);
+ assertTrue((s = lock.tryReadLock(100L, MILLISECONDS)) != 0L);
+ assertTrue(lock.validate(s));
+ assertTrue((p = lock.tryOptimisticRead()) != 0L);
+ lock.unlockRead(s);
+ assertTrue((p = lock.tryOptimisticRead()) != 0L);
+ }
+
+ /**
+ * tryOptimisticRead stamp does not validate if a write lock intervenes
+ */
+ public void testValidateOptimisticWriteLocked() {
+ StampedLock lock = new StampedLock();
+ long s, p;
+ assertTrue((p = lock.tryOptimisticRead()) != 0L);
+ assertTrue((s = lock.writeLock()) != 0L);
+ assertFalse(lock.validate(p));
+ assertFalse((p = lock.tryOptimisticRead()) != 0L);
+ assertTrue(lock.validate(s));
+ lock.unlockWrite(s);
+ }
+
+ /**
+ * tryOptimisticRead stamp does not validate if a write lock
+ * intervenes in another thread
+ */
+ public void testValidateOptimisticWriteLocked2()
+ throws InterruptedException {
+ final CountDownLatch running = new CountDownLatch(1);
+ final StampedLock lock = new StampedLock();
+ long s, p;
+ assertTrue((p = lock.tryOptimisticRead()) != 0L);
+ Thread t = newStartedThread(new CheckedInterruptedRunnable() {
+ public void realRun() throws InterruptedException {
+ lock.writeLockInterruptibly();
+ running.countDown();
+ lock.writeLockInterruptibly();
+ }});
+
+ running.await();
+ assertFalse(lock.validate(p));
+ assertFalse((p = lock.tryOptimisticRead()) != 0L);
+ t.interrupt();
+ awaitTermination(t);
+ }
+
+ /**
+ * tryConvertToOptimisticRead succeeds and validates if successfully locked,
+ */
+ public void testTryConvertToOptimisticRead() throws InterruptedException {
+ StampedLock lock = new StampedLock();
+ long s, p;
+ s = 0L;
+ assertFalse((p = lock.tryConvertToOptimisticRead(s)) != 0L);
+ assertTrue((s = lock.tryOptimisticRead()) != 0L);
+ assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
+ assertTrue((s = lock.writeLock()) != 0L);
+ assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
+ assertTrue(lock.validate(p));
+ assertTrue((s = lock.readLock()) != 0L);
+ assertTrue(lock.validate(s));
+ assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
+ assertTrue(lock.validate(p));
+ assertTrue((s = lock.tryWriteLock()) != 0L);
+ assertTrue(lock.validate(s));
+ assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
+ assertTrue(lock.validate(p));
+ assertTrue((s = lock.tryReadLock()) != 0L);
+ assertTrue(lock.validate(s));
+ assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
+ assertTrue(lock.validate(p));
+ assertTrue((s = lock.tryWriteLock(100L, MILLISECONDS)) != 0L);
+ assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
+ assertTrue(lock.validate(p));
+ assertTrue((s = lock.tryReadLock(100L, MILLISECONDS)) != 0L);
+ assertTrue(lock.validate(s));
+ assertTrue((p = lock.tryConvertToOptimisticRead(s)) != 0L);
+ assertTrue(lock.validate(p));
+ }
+
+ /**
+ * tryConvertToReadLock succeeds and validates if successfully locked
+ * or lock free;
+ */
+ public void testTryConvertToReadLock() throws InterruptedException {
+ StampedLock lock = new StampedLock();
+ long s, p;
+ s = 0L;
+ assertFalse((p = lock.tryConvertToReadLock(s)) != 0L);
+ assertTrue((s = lock.tryOptimisticRead()) != 0L);
+ assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
+ lock.unlockRead(p);
+ assertTrue((s = lock.writeLock()) != 0L);
+ assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
+ assertTrue(lock.validate(p));
+ lock.unlockRead(p);
+ assertTrue((s = lock.readLock()) != 0L);
+ assertTrue(lock.validate(s));
+ assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
+ assertTrue(lock.validate(p));
+ lock.unlockRead(p);
+ assertTrue((s = lock.tryWriteLock()) != 0L);
+ assertTrue(lock.validate(s));
+ assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
+ assertTrue(lock.validate(p));
+ lock.unlockRead(p);
+ assertTrue((s = lock.tryReadLock()) != 0L);
+ assertTrue(lock.validate(s));
+ assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
+ assertTrue(lock.validate(p));
+ lock.unlockRead(p);
+ assertTrue((s = lock.tryWriteLock(100L, MILLISECONDS)) != 0L);
+ assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
+ assertTrue(lock.validate(p));
+ lock.unlockRead(p);
+ assertTrue((s = lock.tryReadLock(100L, MILLISECONDS)) != 0L);
+ assertTrue(lock.validate(s));
+ assertTrue((p = lock.tryConvertToReadLock(s)) != 0L);
+ assertTrue(lock.validate(p));
+ lock.unlockRead(p);
+ }
+
+ /**
+ * tryConvertToWriteLock succeeds and validates if successfully locked
+ * or lock free;
+ */
+ public void testTryConvertToWriteLock() throws InterruptedException {
+ StampedLock lock = new StampedLock();
+ long s, p;
+ s = 0L;
+ assertFalse((p = lock.tryConvertToWriteLock(s)) != 0L);
+ assertTrue((s = lock.tryOptimisticRead()) != 0L);
+ assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
+ lock.unlockWrite(p);
+ assertTrue((s = lock.writeLock()) != 0L);
+ assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
+ assertTrue(lock.validate(p));
+ lock.unlockWrite(p);
+ assertTrue((s = lock.readLock()) != 0L);
+ assertTrue(lock.validate(s));
+ assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
+ assertTrue(lock.validate(p));
+ lock.unlockWrite(p);
+ assertTrue((s = lock.tryWriteLock()) != 0L);
+ assertTrue(lock.validate(s));
+ assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
+ assertTrue(lock.validate(p));
+ lock.unlockWrite(p);
+ assertTrue((s = lock.tryReadLock()) != 0L);
+ assertTrue(lock.validate(s));
+ assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
+ assertTrue(lock.validate(p));
+ lock.unlockWrite(p);
+ assertTrue((s = lock.tryWriteLock(100L, MILLISECONDS)) != 0L);
+ assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
+ assertTrue(lock.validate(p));
+ lock.unlockWrite(p);
+ assertTrue((s = lock.tryReadLock(100L, MILLISECONDS)) != 0L);
+ assertTrue(lock.validate(s));
+ assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
+ assertTrue(lock.validate(p));
+ lock.unlockWrite(p);
+ }
+
+ /**
+ * asWriteLock can be locked and unlocked
+ */
+ public void testAsWriteLock() {
+ StampedLock sl = new StampedLock();
+ Lock lock = sl.asWriteLock();
+ lock.lock();
+ assertFalse(lock.tryLock());
+ lock.unlock();
+ assertTrue(lock.tryLock());
+ }
+
+ /**
+ * asReadLock can be locked and unlocked
+ */
+ public void testAsReadLock() {
+ StampedLock sl = new StampedLock();
+ Lock lock = sl.asReadLock();
+ lock.lock();
+ lock.unlock();
+ assertTrue(lock.tryLock());
+ }
+
+ /**
+ * asReadWriteLock.writeLock can be locked and unlocked
+ */
+ public void testAsReadWriteLockWriteLock() {
+ StampedLock sl = new StampedLock();
+ Lock lock = sl.asReadWriteLock().writeLock();
+ lock.lock();
+ assertFalse(lock.tryLock());
+ lock.unlock();
+ assertTrue(lock.tryLock());
+ }
+
+ /**
+ * asReadWriteLock.readLock can be locked and unlocked
+ */
+ public void testAsReadWriteLockReadLock() {
+ StampedLock sl = new StampedLock();
+ Lock lock = sl.asReadWriteLock().readLock();
+ lock.lock();
+ lock.unlock();
+ assertTrue(lock.tryLock());
+ }
+
+}
diff --git a/jsr166-tests/src/test/java/jsr166/SynchronousQueueTest.java b/jsr166-tests/src/test/java/jsr166/SynchronousQueueTest.java
index 605a955..9d3f212 100644
--- a/jsr166-tests/src/test/java/jsr166/SynchronousQueueTest.java
+++ b/jsr166-tests/src/test/java/jsr166/SynchronousQueueTest.java
@@ -25,7 +25,7 @@
public class SynchronousQueueTest extends JSR166TestCase {
- // android-note: These tests have been moved into their own separate
+ // android-note: These tests have been moved into their own separate
// classes to work around CTS issues.
//
// public static class Fair extends BlockingQueueTest {
@@ -33,17 +33,19 @@
// return new SynchronousQueue(true);
// }
// }
- //
+
// public static class NonFair extends BlockingQueueTest {
// protected BlockingQueue emptyCollection() {
// return new SynchronousQueue(false);
// }
// }
+
+ // 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 newTestSuite(SynchronousQueueTest.class,
// new Fair().testSuite(),
@@ -262,7 +264,6 @@
pleaseOffer.countDown();
startTime = System.nanoTime();
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
- assertTrue(millisElapsedSince(startTime) < MEDIUM_DELAY_MS);
Thread.currentThread().interrupt();
try {
@@ -277,13 +278,15 @@
shouldThrow();
} catch (InterruptedException success) {}
assertFalse(Thread.interrupted());
+
+ assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
}});
await(pleaseOffer);
long startTime = System.nanoTime();
try { assertTrue(q.offer(zero, LONG_DELAY_MS, MILLISECONDS)); }
catch (InterruptedException e) { threadUnexpectedException(e); }
- assertTrue(millisElapsedSince(startTime) < MEDIUM_DELAY_MS);
+ assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
await(pleaseInterrupt);
assertThreadStaysAlive(t);
@@ -474,24 +477,24 @@
public void testOfferInExecutor_fair() { testOfferInExecutor(true); }
public void testOfferInExecutor(boolean fair) {
final SynchronousQueue q = new SynchronousQueue(fair);
- ExecutorService executor = Executors.newFixedThreadPool(2);
final CheckedBarrier threadsStarted = new CheckedBarrier(2);
+ final ExecutorService executor = Executors.newFixedThreadPool(2);
+ try (PoolCleaner cleaner = cleaner(executor)) {
- executor.execute(new CheckedRunnable() {
- public void realRun() throws InterruptedException {
- assertFalse(q.offer(one));
- threadsStarted.await();
- assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS));
- assertEquals(0, q.remainingCapacity());
- }});
+ executor.execute(new CheckedRunnable() {
+ public void realRun() throws InterruptedException {
+ assertFalse(q.offer(one));
+ threadsStarted.await();
+ assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS));
+ assertEquals(0, q.remainingCapacity());
+ }});
- executor.execute(new CheckedRunnable() {
- public void realRun() throws InterruptedException {
- threadsStarted.await();
- assertSame(one, q.take());
- }});
-
- joinPool(executor);
+ executor.execute(new CheckedRunnable() {
+ public void realRun() throws InterruptedException {
+ threadsStarted.await();
+ assertSame(one, q.take());
+ }});
+ }
}
/**
@@ -502,22 +505,22 @@
public void testPollInExecutor(boolean fair) {
final SynchronousQueue q = new SynchronousQueue(fair);
final CheckedBarrier threadsStarted = new CheckedBarrier(2);
- ExecutorService executor = Executors.newFixedThreadPool(2);
- executor.execute(new CheckedRunnable() {
- public void realRun() throws InterruptedException {
- assertNull(q.poll());
- threadsStarted.await();
- assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
- assertTrue(q.isEmpty());
- }});
+ final ExecutorService executor = Executors.newFixedThreadPool(2);
+ try (PoolCleaner cleaner = cleaner(executor)) {
+ executor.execute(new CheckedRunnable() {
+ public void realRun() throws InterruptedException {
+ assertNull(q.poll());
+ threadsStarted.await();
+ assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
+ assertTrue(q.isEmpty());
+ }});
- executor.execute(new CheckedRunnable() {
- public void realRun() throws InterruptedException {
- threadsStarted.await();
- q.put(one);
- }});
-
- joinPool(executor);
+ executor.execute(new CheckedRunnable() {
+ public void realRun() throws InterruptedException {
+ threadsStarted.await();
+ q.put(one);
+ }});
+ }
}
/**
@@ -595,10 +598,12 @@
}});
ArrayList l = new ArrayList();
- delay(SHORT_DELAY_MS);
- q.drainTo(l, 1);
+ int drained;
+ while ((drained = q.drainTo(l, 1)) == 0) Thread.yield();
+ assertEquals(1, drained);
assertEquals(1, l.size());
- q.drainTo(l, 1);
+ while ((drained = q.drainTo(l, 1)) == 0) Thread.yield();
+ assertEquals(1, drained);
assertEquals(2, l.size());
assertTrue(l.contains(one));
assertTrue(l.contains(two));
diff --git a/jsr166-tests/src/test/java/jsr166/SystemTest.java b/jsr166-tests/src/test/java/jsr166/SystemTest.java
index 6918374..412ce17 100644
--- a/jsr166-tests/src/test/java/jsr166/SystemTest.java
+++ b/jsr166-tests/src/test/java/jsr166/SystemTest.java
@@ -19,7 +19,7 @@
// main(suite(), args);
// }
// public static Test suite() {
- // return new TestSuite(...);
+ // return new TestSuite(SystemTest.class);
// }
/**
diff --git a/jsr166-tests/src/test/java/jsr166/ThreadLocalRandom8Test.java b/jsr166-tests/src/test/java/jsr166/ThreadLocalRandom8Test.java
new file mode 100644
index 0000000..614af83
--- /dev/null
+++ b/jsr166-tests/src/test/java/jsr166/ThreadLocalRandom8Test.java
@@ -0,0 +1,241 @@
+/*
+ * Written by Doug Lea with assistance from members of JCP JSR-166
+ * Expert Group and released to the public domain, as explained at
+ * http://creativecommons.org/publicdomain/zero/1.0/
+ */
+
+package jsr166;
+
+import java.util.concurrent.ThreadLocalRandom;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.LongAdder;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+public class ThreadLocalRandom8Test 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(ThreadLocalRandom8Test.class);
+ // }
+
+ // max sampled int bound
+ static final int MAX_INT_BOUND = (1 << 26);
+
+ // max sampled long bound
+ static final long MAX_LONG_BOUND = (1L << 42);
+
+ // Number of replications for other checks
+ static final int REPS =
+ Integer.getInteger("ThreadLocalRandom8Test.reps", 4);
+
+ /**
+ * Invoking sized ints, long, doubles, with negative sizes throws
+ * IllegalArgumentException
+ */
+ // TODO(streams):
+ // public void testBadStreamSize() {
+ // ThreadLocalRandom r = ThreadLocalRandom.current();
+ // Runnable[] throwingActions = {
+ // () -> r.ints(-1L),
+ // () -> r.ints(-1L, 2, 3),
+ // () -> r.longs(-1L),
+ // () -> r.longs(-1L, -1L, 1L),
+ // () -> r.doubles(-1L),
+ // () -> r.doubles(-1L, .5, .6),
+ // };
+ // assertThrows(IllegalArgumentException.class, throwingActions);
+ // }
+
+ // /**
+ // * Invoking bounded ints, long, doubles, with illegal bounds throws
+ // * IllegalArgumentException
+ // */
+ // public void testBadStreamBounds() {
+ // ThreadLocalRandom r = ThreadLocalRandom.current();
+ // Runnable[] throwingActions = {
+ // () -> r.ints(2, 1),
+ // () -> r.ints(10, 42, 42),
+ // () -> r.longs(-1L, -1L),
+ // () -> r.longs(10, 1L, -2L),
+ // () -> r.doubles(0.0, 0.0),
+ // () -> r.doubles(10, .5, .4),
+ // };
+ // assertThrows(IllegalArgumentException.class, throwingActions);
+ // }
+
+ // /**
+ // * A parallel sized stream of ints generates the given number of values
+ // */
+ // public void testIntsCount() {
+ // LongAdder counter = new LongAdder();
+ // ThreadLocalRandom r = ThreadLocalRandom.current();
+ // long size = 0;
+ // for (int reps = 0; reps < REPS; ++reps) {
+ // counter.reset();
+ // r.ints(size).parallel().forEach(x -> counter.increment());
+ // assertEquals(size, counter.sum());
+ // size += 524959;
+ // }
+ // }
+
+ // /**
+ // * A parallel sized stream of longs generates the given number of values
+ // */
+ // public void testLongsCount() {
+ // LongAdder counter = new LongAdder();
+ // ThreadLocalRandom r = ThreadLocalRandom.current();
+ // long size = 0;
+ // for (int reps = 0; reps < REPS; ++reps) {
+ // counter.reset();
+ // r.longs(size).parallel().forEach(x -> counter.increment());
+ // assertEquals(size, counter.sum());
+ // size += 524959;
+ // }
+ // }
+
+ // /**
+ // * A parallel sized stream of doubles generates the given number of values
+ // */
+ // public void testDoublesCount() {
+ // LongAdder counter = new LongAdder();
+ // ThreadLocalRandom r = ThreadLocalRandom.current();
+ // long size = 0;
+ // for (int reps = 0; reps < REPS; ++reps) {
+ // counter.reset();
+ // r.doubles(size).parallel().forEach(x -> counter.increment());
+ // assertEquals(size, counter.sum());
+ // size += 524959;
+ // }
+ // }
+
+ // /**
+ // * Each of a parallel sized stream of bounded ints is within bounds
+ // */
+ // public void testBoundedInts() {
+ // AtomicInteger fails = new AtomicInteger(0);
+ // ThreadLocalRandom r = ThreadLocalRandom.current();
+ // long size = 12345L;
+ // for (int least = -15485867; least < MAX_INT_BOUND; least += 524959) {
+ // for (int bound = least + 2; bound > least && bound < MAX_INT_BOUND; bound += 67867967) {
+ // final int lo = least, hi = bound;
+ // r.ints(size, lo, hi).parallel().forEach(
+ // x -> {
+ // if (x < lo || x >= hi)
+ // fails.getAndIncrement(); });
+ // }
+ // }
+ // assertEquals(0, fails.get());
+ // }
+
+ // /**
+ // * Each of a parallel sized stream of bounded longs is within bounds
+ // */
+ // public void testBoundedLongs() {
+ // AtomicInteger fails = new AtomicInteger(0);
+ // ThreadLocalRandom r = ThreadLocalRandom.current();
+ // long size = 123L;
+ // for (long least = -86028121; least < MAX_LONG_BOUND; least += 1982451653L) {
+ // for (long bound = least + 2; bound > least && bound < MAX_LONG_BOUND; bound += Math.abs(bound * 7919)) {
+ // final long lo = least, hi = bound;
+ // r.longs(size, lo, hi).parallel().forEach(
+ // x -> {
+ // if (x < lo || x >= hi)
+ // fails.getAndIncrement(); });
+ // }
+ // }
+ // assertEquals(0, fails.get());
+ // }
+
+ // /**
+ // * Each of a parallel sized stream of bounded doubles is within bounds
+ // */
+ // public void testBoundedDoubles() {
+ // AtomicInteger fails = new AtomicInteger(0);
+ // ThreadLocalRandom r = ThreadLocalRandom.current();
+ // long size = 456;
+ // for (double least = 0.00011; least < 1.0e20; least *= 9) {
+ // for (double bound = least * 1.0011; bound < 1.0e20; bound *= 17) {
+ // final double lo = least, hi = bound;
+ // r.doubles(size, lo, hi).parallel().forEach(
+ // x -> {
+ // if (x < lo || x >= hi)
+ // fails.getAndIncrement(); });
+ // }
+ // }
+ // assertEquals(0, fails.get());
+ // }
+
+ // /**
+ // * A parallel unsized stream of ints generates at least 100 values
+ // */
+ // public void testUnsizedIntsCount() {
+ // LongAdder counter = new LongAdder();
+ // ThreadLocalRandom r = ThreadLocalRandom.current();
+ // long size = 100;
+ // r.ints().limit(size).parallel().forEach(x -> counter.increment());
+ // assertEquals(size, counter.sum());
+ // }
+
+ // /**
+ // * A parallel unsized stream of longs generates at least 100 values
+ // */
+ // public void testUnsizedLongsCount() {
+ // LongAdder counter = new LongAdder();
+ // ThreadLocalRandom r = ThreadLocalRandom.current();
+ // long size = 100;
+ // r.longs().limit(size).parallel().forEach(x -> counter.increment());
+ // assertEquals(size, counter.sum());
+ // }
+
+ // /**
+ // * A parallel unsized stream of doubles generates at least 100 values
+ // */
+ // public void testUnsizedDoublesCount() {
+ // LongAdder counter = new LongAdder();
+ // ThreadLocalRandom r = ThreadLocalRandom.current();
+ // long size = 100;
+ // r.doubles().limit(size).parallel().forEach(x -> counter.increment());
+ // assertEquals(size, counter.sum());
+ // }
+
+ // /**
+ // * A sequential unsized stream of ints generates at least 100 values
+ // */
+ // public void testUnsizedIntsCountSeq() {
+ // LongAdder counter = new LongAdder();
+ // ThreadLocalRandom r = ThreadLocalRandom.current();
+ // long size = 100;
+ // r.ints().limit(size).forEach(x -> counter.increment());
+ // assertEquals(size, counter.sum());
+ // }
+
+ // /**
+ // * A sequential unsized stream of longs generates at least 100 values
+ // */
+ // public void testUnsizedLongsCountSeq() {
+ // LongAdder counter = new LongAdder();
+ // ThreadLocalRandom r = ThreadLocalRandom.current();
+ // long size = 100;
+ // r.longs().limit(size).forEach(x -> counter.increment());
+ // assertEquals(size, counter.sum());
+ // }
+
+ // /**
+ // * A sequential unsized stream of doubles generates at least 100 values
+ // */
+ // public void testUnsizedDoublesCountSeq() {
+ // LongAdder counter = new LongAdder();
+ // ThreadLocalRandom r = ThreadLocalRandom.current();
+ // long size = 100;
+ // r.doubles().limit(size).forEach(x -> counter.increment());
+ // assertEquals(size, counter.sum());
+ // }
+
+}
diff --git a/jsr166-tests/src/test/java/jsr166/ThreadLocalRandomTest.java b/jsr166-tests/src/test/java/jsr166/ThreadLocalRandomTest.java
index 4ae141d..5d9f894 100644
--- a/jsr166-tests/src/test/java/jsr166/ThreadLocalRandomTest.java
+++ b/jsr166-tests/src/test/java/jsr166/ThreadLocalRandomTest.java
@@ -22,7 +22,7 @@
// main(suite(), args);
// }
// public static Test suite() {
- // return new TestSuite(...);
+ // return new TestSuite(ThreadLocalRandomTest.class);
// }
/*
diff --git a/jsr166-tests/src/test/java/jsr166/ThreadLocalTest.java b/jsr166-tests/src/test/java/jsr166/ThreadLocalTest.java
index 7f5f072..8bfcf70 100644
--- a/jsr166-tests/src/test/java/jsr166/ThreadLocalTest.java
+++ b/jsr166-tests/src/test/java/jsr166/ThreadLocalTest.java
@@ -19,7 +19,7 @@
// main(suite(), args);
// }
// public static Test suite() {
- // return new TestSuite(...);
+ // return new TestSuite(ThreadLocalTest.class);
// }
static ThreadLocal<Integer> tl = new ThreadLocal<Integer>() {
diff --git a/jsr166-tests/src/test/java/jsr166/ThreadPoolExecutorSubclassTest.java b/jsr166-tests/src/test/java/jsr166/ThreadPoolExecutorSubclassTest.java
index 5f38d39..a502392 100644
--- a/jsr166-tests/src/test/java/jsr166/ThreadPoolExecutorSubclassTest.java
+++ b/jsr166-tests/src/test/java/jsr166/ThreadPoolExecutorSubclassTest.java
@@ -9,12 +9,14 @@
package jsr166;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
+import static java.util.concurrent.TimeUnit.SECONDS;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable;
+import java.util.concurrent.CancellationException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
@@ -30,6 +32,7 @@
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
@@ -44,7 +47,7 @@
// main(suite(), args);
// }
// public static Test suite() {
- // return new TestSuite(...);
+ // return new TestSuite(ThreadPoolExecutorSubclassTest.class);
// }
static class CustomTask<V> implements RunnableFuture<V> {
@@ -103,11 +106,13 @@
}
lock.lock();
try {
- result = v;
- exception = e;
- done = true;
- thread = null;
- cond.signalAll();
+ if (!done) {
+ result = v;
+ exception = e;
+ done = true;
+ thread = null;
+ cond.signalAll();
+ }
}
finally { lock.unlock(); }
}
@@ -116,6 +121,8 @@
try {
while (!done)
cond.await();
+ if (cancelled)
+ throw new CancellationException();
if (exception != null)
throw new ExecutionException(exception);
return result;
@@ -127,12 +134,13 @@
long nanos = unit.toNanos(timeout);
lock.lock();
try {
- for (;;) {
- if (done) break;
- if (nanos < 0)
+ while (!done) {
+ if (nanos <= 0L)
throw new TimeoutException();
nanos = cond.awaitNanos(nanos);
}
+ if (cancelled)
+ throw new CancellationException();
if (exception != null)
throw new ExecutionException(exception);
return result;
@@ -229,18 +237,14 @@
public void testExecute() throws InterruptedException {
final ThreadPoolExecutor p =
new CustomTPE(1, 1,
- LONG_DELAY_MS, MILLISECONDS,
+ 2 * LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
- final CountDownLatch done = new CountDownLatch(1);
- final Runnable task = new CheckedRunnable() {
- public void realRun() {
- done.countDown();
- }};
- try {
+ try (PoolCleaner cleaner = cleaner(p)) {
+ final CountDownLatch done = new CountDownLatch(1);
+ final Runnable task = new CheckedRunnable() {
+ public void realRun() { done.countDown(); }};
p.execute(task);
- assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS));
- } finally {
- joinPool(p);
+ assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
}
}
@@ -249,25 +253,22 @@
* thread becomes active
*/
public void testGetActiveCount() throws InterruptedException {
+ final CountDownLatch done = new CountDownLatch(1);
final ThreadPoolExecutor p =
new CustomTPE(2, 2,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
- final CountDownLatch threadStarted = new CountDownLatch(1);
- final CountDownLatch done = new CountDownLatch(1);
- try {
+ try (PoolCleaner cleaner = cleaner(p, done)) {
+ final CountDownLatch threadStarted = new CountDownLatch(1);
assertEquals(0, p.getActiveCount());
p.execute(new CheckedRunnable() {
public void realRun() throws InterruptedException {
threadStarted.countDown();
assertEquals(1, p.getActiveCount());
- done.await();
+ await(done);
}});
- assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
+ await(threadStarted);
assertEquals(1, p.getActiveCount());
- } finally {
- done.countDown();
- joinPool(p);
}
}
@@ -275,28 +276,48 @@
* prestartCoreThread starts a thread if under corePoolSize, else doesn't
*/
public void testPrestartCoreThread() {
- ThreadPoolExecutor p = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
- assertEquals(0, p.getPoolSize());
- assertTrue(p.prestartCoreThread());
- assertEquals(1, p.getPoolSize());
- assertTrue(p.prestartCoreThread());
- assertEquals(2, p.getPoolSize());
- assertFalse(p.prestartCoreThread());
- assertEquals(2, p.getPoolSize());
- joinPool(p);
+ final ThreadPoolExecutor p =
+ new CustomTPE(2, 6,
+ LONG_DELAY_MS, MILLISECONDS,
+ new ArrayBlockingQueue<Runnable>(10));
+ try (PoolCleaner cleaner = cleaner(p)) {
+ assertEquals(0, p.getPoolSize());
+ assertTrue(p.prestartCoreThread());
+ assertEquals(1, p.getPoolSize());
+ assertTrue(p.prestartCoreThread());
+ assertEquals(2, p.getPoolSize());
+ assertFalse(p.prestartCoreThread());
+ assertEquals(2, p.getPoolSize());
+ p.setCorePoolSize(4);
+ assertTrue(p.prestartCoreThread());
+ assertEquals(3, p.getPoolSize());
+ assertTrue(p.prestartCoreThread());
+ assertEquals(4, p.getPoolSize());
+ assertFalse(p.prestartCoreThread());
+ assertEquals(4, p.getPoolSize());
+ }
}
/**
* prestartAllCoreThreads starts all corePoolSize threads
*/
public void testPrestartAllCoreThreads() {
- ThreadPoolExecutor p = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
- assertEquals(0, p.getPoolSize());
- p.prestartAllCoreThreads();
- assertEquals(2, p.getPoolSize());
- p.prestartAllCoreThreads();
- assertEquals(2, p.getPoolSize());
- joinPool(p);
+ final ThreadPoolExecutor p =
+ new CustomTPE(2, 6,
+ LONG_DELAY_MS, MILLISECONDS,
+ new ArrayBlockingQueue<Runnable>(10));
+ try (PoolCleaner cleaner = cleaner(p)) {
+ assertEquals(0, p.getPoolSize());
+ p.prestartAllCoreThreads();
+ assertEquals(2, p.getPoolSize());
+ p.prestartAllCoreThreads();
+ assertEquals(2, p.getPoolSize());
+ p.setCorePoolSize(4);
+ p.prestartAllCoreThreads();
+ assertEquals(4, p.getPoolSize());
+ p.prestartAllCoreThreads();
+ assertEquals(4, p.getPoolSize());
+ }
}
/**
@@ -308,10 +329,10 @@
new CustomTPE(2, 2,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
- final CountDownLatch threadStarted = new CountDownLatch(1);
- final CountDownLatch threadProceed = new CountDownLatch(1);
- final CountDownLatch threadDone = new CountDownLatch(1);
- try {
+ try (PoolCleaner cleaner = cleaner(p)) {
+ final CountDownLatch threadStarted = new CountDownLatch(1);
+ final CountDownLatch threadProceed = new CountDownLatch(1);
+ final CountDownLatch threadDone = new CountDownLatch(1);
assertEquals(0, p.getCompletedTaskCount());
p.execute(new CheckedRunnable() {
public void realRun() throws InterruptedException {
@@ -330,8 +351,6 @@
fail("timed out");
Thread.yield();
}
- } finally {
- joinPool(p);
}
}
@@ -339,52 +358,72 @@
* getCorePoolSize returns size given in constructor if not otherwise set
*/
public void testGetCorePoolSize() {
- ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
- assertEquals(1, p.getCorePoolSize());
- joinPool(p);
+ final ThreadPoolExecutor p =
+ new CustomTPE(1, 1,
+ LONG_DELAY_MS, MILLISECONDS,
+ new ArrayBlockingQueue<Runnable>(10));
+ try (PoolCleaner cleaner = cleaner(p)) {
+ assertEquals(1, p.getCorePoolSize());
+ }
}
/**
* getKeepAliveTime returns value given in constructor if not otherwise set
*/
public void testGetKeepAliveTime() {
- ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
- assertEquals(1, p.getKeepAliveTime(TimeUnit.SECONDS));
- joinPool(p);
+ final ThreadPoolExecutor p =
+ new CustomTPE(2, 2,
+ 1000, MILLISECONDS,
+ new ArrayBlockingQueue<Runnable>(10));
+ try (PoolCleaner cleaner = cleaner(p)) {
+ assertEquals(1, p.getKeepAliveTime(SECONDS));
+ }
}
/**
* getThreadFactory returns factory in constructor if not set
*/
public void testGetThreadFactory() {
- ThreadFactory tf = new SimpleThreadFactory();
- ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), tf, new NoOpREHandler());
- assertSame(tf, p.getThreadFactory());
- joinPool(p);
+ final ThreadFactory threadFactory = new SimpleThreadFactory();
+ final ThreadPoolExecutor p =
+ new CustomTPE(1, 2,
+ LONG_DELAY_MS, MILLISECONDS,
+ new ArrayBlockingQueue<Runnable>(10),
+ threadFactory,
+ new NoOpREHandler());
+ try (PoolCleaner cleaner = cleaner(p)) {
+ assertSame(threadFactory, p.getThreadFactory());
+ }
}
/**
* setThreadFactory sets the thread factory returned by getThreadFactory
*/
public void testSetThreadFactory() {
- ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
- ThreadFactory tf = new SimpleThreadFactory();
- p.setThreadFactory(tf);
- assertSame(tf, p.getThreadFactory());
- joinPool(p);
+ final ThreadPoolExecutor p =
+ new CustomTPE(1, 2,
+ LONG_DELAY_MS, MILLISECONDS,
+ new ArrayBlockingQueue<Runnable>(10));
+ try (PoolCleaner cleaner = cleaner(p)) {
+ ThreadFactory threadFactory = new SimpleThreadFactory();
+ p.setThreadFactory(threadFactory);
+ assertSame(threadFactory, p.getThreadFactory());
+ }
}
/**
* setThreadFactory(null) throws NPE
*/
public void testSetThreadFactoryNull() {
- ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
- try {
- p.setThreadFactory(null);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
- joinPool(p);
+ final ThreadPoolExecutor p =
+ new CustomTPE(1, 2,
+ LONG_DELAY_MS, MILLISECONDS,
+ new ArrayBlockingQueue<Runnable>(10));
+ try (PoolCleaner cleaner = cleaner(p)) {
+ try {
+ p.setThreadFactory(null);
+ shouldThrow();
+ } catch (NullPointerException success) {}
}
}
@@ -392,10 +431,15 @@
* getRejectedExecutionHandler returns handler in constructor if not set
*/
public void testGetRejectedExecutionHandler() {
- RejectedExecutionHandler h = new NoOpREHandler();
- ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), h);
- assertSame(h, p.getRejectedExecutionHandler());
- joinPool(p);
+ final RejectedExecutionHandler handler = new NoOpREHandler();
+ final ThreadPoolExecutor p =
+ new CustomTPE(1, 2,
+ LONG_DELAY_MS, MILLISECONDS,
+ new ArrayBlockingQueue<Runnable>(10),
+ handler);
+ try (PoolCleaner cleaner = cleaner(p)) {
+ assertSame(handler, p.getRejectedExecutionHandler());
+ }
}
/**
@@ -403,24 +447,30 @@
* getRejectedExecutionHandler
*/
public void testSetRejectedExecutionHandler() {
- ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
- RejectedExecutionHandler h = new NoOpREHandler();
- p.setRejectedExecutionHandler(h);
- assertSame(h, p.getRejectedExecutionHandler());
- joinPool(p);
+ final ThreadPoolExecutor p =
+ new CustomTPE(1, 2,
+ LONG_DELAY_MS, MILLISECONDS,
+ new ArrayBlockingQueue<Runnable>(10));
+ try (PoolCleaner cleaner = cleaner(p)) {
+ RejectedExecutionHandler handler = new NoOpREHandler();
+ p.setRejectedExecutionHandler(handler);
+ assertSame(handler, p.getRejectedExecutionHandler());
+ }
}
/**
* setRejectedExecutionHandler(null) throws NPE
*/
public void testSetRejectedExecutionHandlerNull() {
- ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
- try {
- p.setRejectedExecutionHandler(null);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
- joinPool(p);
+ final ThreadPoolExecutor p =
+ new CustomTPE(1, 2,
+ LONG_DELAY_MS, MILLISECONDS,
+ new ArrayBlockingQueue<Runnable>(10));
+ try (PoolCleaner cleaner = cleaner(p)) {
+ try {
+ p.setRejectedExecutionHandler(null);
+ shouldThrow();
+ } catch (NullPointerException success) {}
}
}
@@ -430,28 +480,25 @@
*/
public void testGetLargestPoolSize() throws InterruptedException {
final int THREADS = 3;
+ final CountDownLatch done = new CountDownLatch(1);
final ThreadPoolExecutor p =
new CustomTPE(THREADS, THREADS,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
- final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
- final CountDownLatch done = new CountDownLatch(1);
- try {
+ try (PoolCleaner cleaner = cleaner(p, done)) {
assertEquals(0, p.getLargestPoolSize());
+ final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
for (int i = 0; i < THREADS; i++)
p.execute(new CheckedRunnable() {
public void realRun() throws InterruptedException {
threadsStarted.countDown();
- done.await();
+ await(done);
assertEquals(THREADS, p.getLargestPoolSize());
}});
- assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS));
- assertEquals(THREADS, p.getLargestPoolSize());
- } finally {
- done.countDown();
- joinPool(p);
+ await(threadsStarted);
assertEquals(THREADS, p.getLargestPoolSize());
}
+ assertEquals(THREADS, p.getLargestPoolSize());
}
/**
@@ -459,9 +506,17 @@
* otherwise set
*/
public void testGetMaximumPoolSize() {
- ThreadPoolExecutor p = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
- assertEquals(2, p.getMaximumPoolSize());
- joinPool(p);
+ final ThreadPoolExecutor p =
+ new CustomTPE(2, 3,
+ LONG_DELAY_MS, MILLISECONDS,
+ new ArrayBlockingQueue<Runnable>(10));
+ try (PoolCleaner cleaner = cleaner(p)) {
+ assertEquals(3, p.getMaximumPoolSize());
+ p.setMaximumPoolSize(5);
+ assertEquals(5, p.getMaximumPoolSize());
+ p.setMaximumPoolSize(4);
+ assertEquals(4, p.getMaximumPoolSize());
+ }
}
/**
@@ -469,25 +524,22 @@
* become active
*/
public void testGetPoolSize() throws InterruptedException {
+ final CountDownLatch done = new CountDownLatch(1);
final ThreadPoolExecutor p =
new CustomTPE(1, 1,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
- final CountDownLatch threadStarted = new CountDownLatch(1);
- final CountDownLatch done = new CountDownLatch(1);
- try {
+ try (PoolCleaner cleaner = cleaner(p, done)) {
assertEquals(0, p.getPoolSize());
+ final CountDownLatch threadStarted = new CountDownLatch(1);
p.execute(new CheckedRunnable() {
public void realRun() throws InterruptedException {
threadStarted.countDown();
assertEquals(1, p.getPoolSize());
- done.await();
+ await(done);
}});
- assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
+ await(threadStarted);
assertEquals(1, p.getPoolSize());
- } finally {
- done.countDown();
- joinPool(p);
}
}
@@ -495,38 +547,53 @@
* getTaskCount increases, but doesn't overestimate, when tasks submitted
*/
public void testGetTaskCount() throws InterruptedException {
+ final int TASKS = 3;
+ final CountDownLatch done = new CountDownLatch(1);
final ThreadPoolExecutor p =
new CustomTPE(1, 1,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
- final CountDownLatch threadStarted = new CountDownLatch(1);
- final CountDownLatch done = new CountDownLatch(1);
- try {
+ try (PoolCleaner cleaner = cleaner(p, done)) {
+ final CountDownLatch threadStarted = new CountDownLatch(1);
assertEquals(0, p.getTaskCount());
+ assertEquals(0, p.getCompletedTaskCount());
p.execute(new CheckedRunnable() {
public void realRun() throws InterruptedException {
threadStarted.countDown();
- assertEquals(1, p.getTaskCount());
- done.await();
+ await(done);
}});
- assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
+ await(threadStarted);
assertEquals(1, p.getTaskCount());
- } finally {
- done.countDown();
- joinPool(p);
+ assertEquals(0, p.getCompletedTaskCount());
+ for (int i = 0; i < TASKS; i++) {
+ assertEquals(1 + i, p.getTaskCount());
+ p.execute(new CheckedRunnable() {
+ public void realRun() throws InterruptedException {
+ threadStarted.countDown();
+ assertEquals(1 + TASKS, p.getTaskCount());
+ await(done);
+ }});
+ }
+ assertEquals(1 + TASKS, p.getTaskCount());
+ assertEquals(0, p.getCompletedTaskCount());
}
+ assertEquals(1 + TASKS, p.getTaskCount());
+ assertEquals(1 + TASKS, p.getCompletedTaskCount());
}
/**
* isShutdown is false before shutdown, true after
*/
public void testIsShutdown() {
-
- ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
- assertFalse(p.isShutdown());
- try { p.shutdown(); } catch (SecurityException ok) { return; }
- assertTrue(p.isShutdown());
- joinPool(p);
+ final ThreadPoolExecutor p =
+ new CustomTPE(1, 1,
+ LONG_DELAY_MS, MILLISECONDS,
+ new ArrayBlockingQueue<Runnable>(10));
+ try (PoolCleaner cleaner = cleaner(p)) {
+ assertFalse(p.isShutdown());
+ try { p.shutdown(); } catch (SecurityException ok) { return; }
+ assertTrue(p.isShutdown());
+ }
}
/**
@@ -537,25 +604,24 @@
new CustomTPE(1, 1,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
- final CountDownLatch threadStarted = new CountDownLatch(1);
- final CountDownLatch done = new CountDownLatch(1);
- try {
+ try (PoolCleaner cleaner = cleaner(p)) {
+ final CountDownLatch threadStarted = new CountDownLatch(1);
+ final CountDownLatch done = new CountDownLatch(1);
assertFalse(p.isTerminating());
p.execute(new CheckedRunnable() {
public void realRun() throws InterruptedException {
assertFalse(p.isTerminating());
threadStarted.countDown();
- done.await();
+ await(done);
}});
- assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
+ await(threadStarted);
assertFalse(p.isTerminating());
done.countDown();
- } finally {
try { p.shutdown(); } catch (SecurityException ok) { return; }
+ assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
+ assertTrue(p.isTerminated());
+ assertFalse(p.isTerminating());
}
- assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
- assertTrue(p.isTerminated());
- assertFalse(p.isTerminating());
}
/**
@@ -566,59 +632,55 @@
new CustomTPE(1, 1,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
- final CountDownLatch threadStarted = new CountDownLatch(1);
- final CountDownLatch done = new CountDownLatch(1);
- try {
+ try (PoolCleaner cleaner = cleaner(p)) {
+ final CountDownLatch threadStarted = new CountDownLatch(1);
+ final CountDownLatch done = new CountDownLatch(1);
assertFalse(p.isTerminating());
p.execute(new CheckedRunnable() {
public void realRun() throws InterruptedException {
assertFalse(p.isTerminating());
threadStarted.countDown();
- done.await();
+ await(done);
}});
- assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
+ await(threadStarted);
assertFalse(p.isTerminating());
done.countDown();
- } finally {
try { p.shutdown(); } catch (SecurityException ok) { return; }
+ assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
+ assertTrue(p.isTerminated());
+ assertFalse(p.isTerminating());
}
- assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
- assertTrue(p.isTerminated());
- assertFalse(p.isTerminating());
}
/**
* getQueue returns the work queue, which contains queued tasks
*/
public void testGetQueue() throws InterruptedException {
+ final CountDownLatch done = new CountDownLatch(1);
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
final ThreadPoolExecutor p =
new CustomTPE(1, 1,
LONG_DELAY_MS, MILLISECONDS,
q);
- final CountDownLatch threadStarted = new CountDownLatch(1);
- final CountDownLatch done = new CountDownLatch(1);
- try {
+ try (PoolCleaner cleaner = cleaner(p, done)) {
+ final CountDownLatch threadStarted = new CountDownLatch(1);
FutureTask[] tasks = new FutureTask[5];
for (int i = 0; i < tasks.length; i++) {
Callable task = new CheckedCallable<Boolean>() {
public Boolean realCall() throws InterruptedException {
threadStarted.countDown();
assertSame(q, p.getQueue());
- done.await();
+ await(done);
return Boolean.TRUE;
}};
tasks[i] = new FutureTask(task);
p.execute(tasks[i]);
}
- assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
+ await(threadStarted);
assertSame(q, p.getQueue());
assertFalse(q.contains(tasks[0]));
assertTrue(q.contains(tasks[tasks.length - 1]));
assertEquals(tasks.length - 1, q.size());
- } finally {
- done.countDown();
- joinPool(p);
}
}
@@ -626,24 +688,24 @@
* remove(task) removes queued task, and fails to remove active task
*/
public void testRemove() throws InterruptedException {
+ final CountDownLatch done = new CountDownLatch(1);
BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
final ThreadPoolExecutor p =
new CustomTPE(1, 1,
LONG_DELAY_MS, MILLISECONDS,
q);
- Runnable[] tasks = new Runnable[6];
- final CountDownLatch threadStarted = new CountDownLatch(1);
- final CountDownLatch done = new CountDownLatch(1);
- try {
+ try (PoolCleaner cleaner = cleaner(p, done)) {
+ Runnable[] tasks = new Runnable[6];
+ final CountDownLatch threadStarted = new CountDownLatch(1);
for (int i = 0; i < tasks.length; i++) {
tasks[i] = new CheckedRunnable() {
- public void realRun() throws InterruptedException {
- threadStarted.countDown();
- done.await();
- }};
+ public void realRun() throws InterruptedException {
+ threadStarted.countDown();
+ await(done);
+ }};
p.execute(tasks[i]);
}
- assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
+ await(threadStarted);
assertFalse(p.remove(tasks[0]));
assertTrue(q.contains(tasks[4]));
assertTrue(q.contains(tasks[3]));
@@ -653,9 +715,6 @@
assertTrue(q.contains(tasks[3]));
assertTrue(p.remove(tasks[3]));
assertFalse(q.contains(tasks[3]));
- } finally {
- done.countDown();
- joinPool(p);
}
}
@@ -670,19 +729,19 @@
new CustomTPE(1, 1,
LONG_DELAY_MS, MILLISECONDS,
q);
- FutureTask[] tasks = new FutureTask[5];
- try {
+ try (PoolCleaner cleaner = cleaner(p, done)) {
+ FutureTask[] tasks = new FutureTask[5];
for (int i = 0; i < tasks.length; i++) {
Callable task = new CheckedCallable<Boolean>() {
public Boolean realCall() throws InterruptedException {
threadStarted.countDown();
- done.await();
+ await(done);
return Boolean.TRUE;
}};
tasks[i] = new FutureTask(task);
p.execute(tasks[i]);
}
- assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
+ await(threadStarted);
assertEquals(tasks.length, p.getTaskCount());
assertEquals(tasks.length - 1, q.size());
assertEquals(1L, p.getActiveCount());
@@ -695,29 +754,47 @@
p.purge(); // Nothing to do
assertEquals(tasks.length - 3, q.size());
assertEquals(tasks.length - 2, p.getTaskCount());
- } finally {
- done.countDown();
- joinPool(p);
}
}
/**
- * shutdownNow returns a list containing tasks that were not run
+ * shutdownNow returns a list containing tasks that were not run,
+ * and those tasks are drained from the queue
*/
- public void testShutdownNow() {
- ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
- List l;
- try {
- for (int i = 0; i < 5; i++)
- p.execute(new MediumPossiblyInterruptedRunnable());
- }
- finally {
+ public void testShutdownNow() throws InterruptedException {
+ final int poolSize = 2;
+ final int count = 5;
+ final AtomicInteger ran = new AtomicInteger(0);
+ final ThreadPoolExecutor p =
+ new CustomTPE(poolSize, poolSize,
+ LONG_DELAY_MS, MILLISECONDS,
+ new ArrayBlockingQueue<Runnable>(10));
+ final CountDownLatch threadsStarted = new CountDownLatch(poolSize);
+ Runnable waiter = new CheckedRunnable() { public void realRun() {
+ threadsStarted.countDown();
try {
- l = p.shutdownNow();
- } catch (SecurityException ok) { return; }
+ MILLISECONDS.sleep(2 * LONG_DELAY_MS);
+ } catch (InterruptedException success) {}
+ ran.getAndIncrement();
+ }};
+ for (int i = 0; i < count; i++)
+ p.execute(waiter);
+ await(threadsStarted);
+ assertEquals(poolSize, p.getActiveCount());
+ assertEquals(0, p.getCompletedTaskCount());
+ final List<Runnable> queuedTasks;
+ try {
+ queuedTasks = p.shutdownNow();
+ } catch (SecurityException ok) {
+ return; // Allowed in case test doesn't have privs
}
assertTrue(p.isShutdown());
- assertTrue(l.size() <= 4);
+ assertTrue(p.getQueue().isEmpty());
+ assertEquals(count - poolSize, queuedTasks.size());
+ assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
+ assertTrue(p.isTerminated());
+ assertEquals(poolSize, ran.get());
+ assertEquals(poolSize, p.getCompletedTaskCount());
}
// Exception Tests
@@ -727,7 +804,8 @@
*/
public void testConstructor1() {
try {
- new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
+ new CustomTPE(-1, 1, 1L, SECONDS,
+ new ArrayBlockingQueue<Runnable>(10));
shouldThrow();
} catch (IllegalArgumentException success) {}
}
@@ -737,7 +815,8 @@
*/
public void testConstructor2() {
try {
- new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
+ new CustomTPE(1, -1, 1L, SECONDS,
+ new ArrayBlockingQueue<Runnable>(10));
shouldThrow();
} catch (IllegalArgumentException success) {}
}
@@ -747,7 +826,8 @@
*/
public void testConstructor3() {
try {
- new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
+ new CustomTPE(1, 0, 1L, SECONDS,
+ new ArrayBlockingQueue<Runnable>(10));
shouldThrow();
} catch (IllegalArgumentException success) {}
}
@@ -757,7 +837,8 @@
*/
public void testConstructor4() {
try {
- new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
+ new CustomTPE(1, 2, -1L, SECONDS,
+ new ArrayBlockingQueue<Runnable>(10));
shouldThrow();
} catch (IllegalArgumentException success) {}
}
@@ -767,7 +848,8 @@
*/
public void testConstructor5() {
try {
- new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
+ new CustomTPE(2, 1, 1L, SECONDS,
+ new ArrayBlockingQueue<Runnable>(10));
shouldThrow();
} catch (IllegalArgumentException success) {}
}
@@ -777,7 +859,7 @@
*/
public void testConstructorNullPointerException() {
try {
- new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null);
+ new CustomTPE(1, 2, 1L, SECONDS, null);
shouldThrow();
} catch (NullPointerException success) {}
}
@@ -787,7 +869,9 @@
*/
public void testConstructor6() {
try {
- new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
+ new CustomTPE(-1, 1, 1L, SECONDS,
+ new ArrayBlockingQueue<Runnable>(10),
+ new SimpleThreadFactory());
shouldThrow();
} catch (IllegalArgumentException success) {}
}
@@ -797,7 +881,9 @@
*/
public void testConstructor7() {
try {
- new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
+ new CustomTPE(1,-1, 1L, SECONDS,
+ new ArrayBlockingQueue<Runnable>(10),
+ new SimpleThreadFactory());
shouldThrow();
} catch (IllegalArgumentException success) {}
}
@@ -807,7 +893,9 @@
*/
public void testConstructor8() {
try {
- new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
+ new CustomTPE(1, 0, 1L, SECONDS,
+ new ArrayBlockingQueue<Runnable>(10),
+ new SimpleThreadFactory());
shouldThrow();
} catch (IllegalArgumentException success) {}
}
@@ -817,7 +905,9 @@
*/
public void testConstructor9() {
try {
- new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
+ new CustomTPE(1, 2, -1L, SECONDS,
+ new ArrayBlockingQueue<Runnable>(10),
+ new SimpleThreadFactory());
shouldThrow();
} catch (IllegalArgumentException success) {}
}
@@ -827,7 +917,9 @@
*/
public void testConstructor10() {
try {
- new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
+ new CustomTPE(2, 1, 1L, SECONDS,
+ new ArrayBlockingQueue<Runnable>(10),
+ new SimpleThreadFactory());
shouldThrow();
} catch (IllegalArgumentException success) {}
}
@@ -837,7 +929,7 @@
*/
public void testConstructorNullPointerException2() {
try {
- new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory());
+ new CustomTPE(1, 2, 1L, SECONDS, null, new SimpleThreadFactory());
shouldThrow();
} catch (NullPointerException success) {}
}
@@ -847,8 +939,9 @@
*/
public void testConstructorNullPointerException3() {
try {
- ThreadFactory f = null;
- new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f);
+ new CustomTPE(1, 2, 1L, SECONDS,
+ new ArrayBlockingQueue<Runnable>(10),
+ (ThreadFactory) null);
shouldThrow();
} catch (NullPointerException success) {}
}
@@ -858,7 +951,9 @@
*/
public void testConstructor11() {
try {
- new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
+ new CustomTPE(-1, 1, 1L, SECONDS,
+ new ArrayBlockingQueue<Runnable>(10),
+ new NoOpREHandler());
shouldThrow();
} catch (IllegalArgumentException success) {}
}
@@ -868,7 +963,9 @@
*/
public void testConstructor12() {
try {
- new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
+ new CustomTPE(1, -1, 1L, SECONDS,
+ new ArrayBlockingQueue<Runnable>(10),
+ new NoOpREHandler());
shouldThrow();
} catch (IllegalArgumentException success) {}
}
@@ -878,7 +975,9 @@
*/
public void testConstructor13() {
try {
- new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
+ new CustomTPE(1, 0, 1L, SECONDS,
+ new ArrayBlockingQueue<Runnable>(10),
+ new NoOpREHandler());
shouldThrow();
} catch (IllegalArgumentException success) {}
}
@@ -888,7 +987,9 @@
*/
public void testConstructor14() {
try {
- new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
+ new CustomTPE(1, 2, -1L, SECONDS,
+ new ArrayBlockingQueue<Runnable>(10),
+ new NoOpREHandler());
shouldThrow();
} catch (IllegalArgumentException success) {}
}
@@ -898,7 +999,9 @@
*/
public void testConstructor15() {
try {
- new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
+ new CustomTPE(2, 1, 1L, SECONDS,
+ new ArrayBlockingQueue<Runnable>(10),
+ new NoOpREHandler());
shouldThrow();
} catch (IllegalArgumentException success) {}
}
@@ -908,7 +1011,9 @@
*/
public void testConstructorNullPointerException4() {
try {
- new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new NoOpREHandler());
+ new CustomTPE(1, 2, 1L, SECONDS,
+ null,
+ new NoOpREHandler());
shouldThrow();
} catch (NullPointerException success) {}
}
@@ -918,8 +1023,9 @@
*/
public void testConstructorNullPointerException5() {
try {
- RejectedExecutionHandler r = null;
- new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r);
+ new CustomTPE(1, 2, 1L, SECONDS,
+ new ArrayBlockingQueue<Runnable>(10),
+ (RejectedExecutionHandler) null);
shouldThrow();
} catch (NullPointerException success) {}
}
@@ -929,7 +1035,10 @@
*/
public void testConstructor16() {
try {
- new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
+ new CustomTPE(-1, 1, 1L, SECONDS,
+ new ArrayBlockingQueue<Runnable>(10),
+ new SimpleThreadFactory(),
+ new NoOpREHandler());
shouldThrow();
} catch (IllegalArgumentException success) {}
}
@@ -939,7 +1048,10 @@
*/
public void testConstructor17() {
try {
- new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
+ new CustomTPE(1, -1, 1L, SECONDS,
+ new ArrayBlockingQueue<Runnable>(10),
+ new SimpleThreadFactory(),
+ new NoOpREHandler());
shouldThrow();
} catch (IllegalArgumentException success) {}
}
@@ -949,7 +1061,10 @@
*/
public void testConstructor18() {
try {
- new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
+ new CustomTPE(1, 0, 1L, SECONDS,
+ new ArrayBlockingQueue<Runnable>(10),
+ new SimpleThreadFactory(),
+ new NoOpREHandler());
shouldThrow();
} catch (IllegalArgumentException success) {}
}
@@ -959,7 +1074,10 @@
*/
public void testConstructor19() {
try {
- new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
+ new CustomTPE(1, 2, -1L, SECONDS,
+ new ArrayBlockingQueue<Runnable>(10),
+ new SimpleThreadFactory(),
+ new NoOpREHandler());
shouldThrow();
} catch (IllegalArgumentException success) {}
}
@@ -969,7 +1087,10 @@
*/
public void testConstructor20() {
try {
- new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
+ new CustomTPE(2, 1, 1L, SECONDS,
+ new ArrayBlockingQueue<Runnable>(10),
+ new SimpleThreadFactory(),
+ new NoOpREHandler());
shouldThrow();
} catch (IllegalArgumentException success) {}
}
@@ -979,7 +1100,10 @@
*/
public void testConstructorNullPointerException6() {
try {
- new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler());
+ new CustomTPE(1, 2, 1L, SECONDS,
+ null,
+ new SimpleThreadFactory(),
+ new NoOpREHandler());
shouldThrow();
} catch (NullPointerException success) {}
}
@@ -989,8 +1113,10 @@
*/
public void testConstructorNullPointerException7() {
try {
- RejectedExecutionHandler r = null;
- new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r);
+ new CustomTPE(1, 2, 1L, SECONDS,
+ new ArrayBlockingQueue<Runnable>(10),
+ new SimpleThreadFactory(),
+ (RejectedExecutionHandler) null);
shouldThrow();
} catch (NullPointerException success) {}
}
@@ -1000,8 +1126,7 @@
*/
public void testConstructorNullPointerException8() {
try {
- new CustomTPE(1, 2,
- LONG_DELAY_MS, MILLISECONDS,
+ new CustomTPE(1, 2, 1L, SECONDS,
new ArrayBlockingQueue<Runnable>(10),
(ThreadFactory) null,
new NoOpREHandler());
@@ -1013,15 +1138,15 @@
* execute throws RejectedExecutionException if saturated.
*/
public void testSaturatedExecute() {
- ThreadPoolExecutor p =
+ final CountDownLatch done = new CountDownLatch(1);
+ final ThreadPoolExecutor p =
new CustomTPE(1, 1,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(1));
- final CountDownLatch done = new CountDownLatch(1);
- try {
+ try (PoolCleaner cleaner = cleaner(p, done)) {
Runnable task = new CheckedRunnable() {
public void realRun() throws InterruptedException {
- done.await();
+ await(done);
}};
for (int i = 0; i < 2; ++i)
p.execute(task);
@@ -1032,9 +1157,6 @@
} catch (RejectedExecutionException success) {}
assertTrue(p.getTaskCount() <= 2);
}
- } finally {
- done.countDown();
- joinPool(p);
}
}
@@ -1042,24 +1164,26 @@
* executor using CallerRunsPolicy runs task if saturated.
*/
public void testSaturatedExecute2() {
- RejectedExecutionHandler h = new CustomTPE.CallerRunsPolicy();
- ThreadPoolExecutor p = new CustomTPE(1, 1,
- LONG_DELAY_MS, MILLISECONDS,
- new ArrayBlockingQueue<Runnable>(1),
- h);
- try {
+ final CountDownLatch done = new CountDownLatch(1);
+ final ThreadPoolExecutor p =
+ new CustomTPE(1, 1,
+ LONG_DELAY_MS, MILLISECONDS,
+ new ArrayBlockingQueue<Runnable>(1),
+ new CustomTPE.CallerRunsPolicy());
+ try (PoolCleaner cleaner = cleaner(p, done)) {
+ Runnable blocker = new CheckedRunnable() {
+ public void realRun() throws InterruptedException {
+ await(done);
+ }};
+ p.execute(blocker);
TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
- for (int i = 0; i < tasks.length; ++i)
+ for (int i = 0; i < tasks.length; i++)
tasks[i] = new TrackedNoOpRunnable();
- TrackedLongRunnable mr = new TrackedLongRunnable();
- p.execute(mr);
- for (int i = 0; i < tasks.length; ++i)
+ for (int i = 0; i < tasks.length; i++)
p.execute(tasks[i]);
- for (int i = 1; i < tasks.length; ++i)
+ for (int i = 1; i < tasks.length; i++)
assertTrue(tasks[i].done);
- try { p.shutdownNow(); } catch (SecurityException ok) { return; }
- } finally {
- joinPool(p);
+ assertFalse(tasks[0].done); // waiting in queue
}
}
@@ -1067,77 +1191,88 @@
* executor using DiscardPolicy drops task if saturated.
*/
public void testSaturatedExecute3() {
- RejectedExecutionHandler h = new CustomTPE.DiscardPolicy();
- ThreadPoolExecutor p =
+ final TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
+ for (int i = 0; i < tasks.length; ++i)
+ tasks[i] = new TrackedNoOpRunnable();
+ final CountDownLatch done = new CountDownLatch(1);
+ final ThreadPoolExecutor p =
new CustomTPE(1, 1,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(1),
- h);
- try {
- TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
- for (int i = 0; i < tasks.length; ++i)
- tasks[i] = new TrackedNoOpRunnable();
- p.execute(new TrackedLongRunnable());
+ new CustomTPE.DiscardPolicy());
+ try (PoolCleaner cleaner = cleaner(p, done)) {
+ p.execute(awaiter(done));
+
for (TrackedNoOpRunnable task : tasks)
p.execute(task);
- for (TrackedNoOpRunnable task : tasks)
- assertFalse(task.done);
- try { p.shutdownNow(); } catch (SecurityException ok) { return; }
- } finally {
- joinPool(p);
+ for (int i = 1; i < tasks.length; i++)
+ assertFalse(tasks[i].done);
}
+ for (int i = 1; i < tasks.length; i++)
+ assertFalse(tasks[i].done);
+ assertTrue(tasks[0].done); // was waiting in queue
}
/**
* executor using DiscardOldestPolicy drops oldest task if saturated.
*/
public void testSaturatedExecute4() {
- RejectedExecutionHandler h = new CustomTPE.DiscardOldestPolicy();
- ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
- try {
- p.execute(new TrackedLongRunnable());
- TrackedLongRunnable r2 = new TrackedLongRunnable();
+ final CountDownLatch done = new CountDownLatch(1);
+ LatchAwaiter r1 = awaiter(done);
+ LatchAwaiter r2 = awaiter(done);
+ LatchAwaiter r3 = awaiter(done);
+ final ThreadPoolExecutor p =
+ new CustomTPE(1, 1,
+ LONG_DELAY_MS, MILLISECONDS,
+ new ArrayBlockingQueue<Runnable>(1),
+ new CustomTPE.DiscardOldestPolicy());
+ try (PoolCleaner cleaner = cleaner(p, done)) {
+ assertEquals(LatchAwaiter.NEW, r1.state);
+ assertEquals(LatchAwaiter.NEW, r2.state);
+ assertEquals(LatchAwaiter.NEW, r3.state);
+ p.execute(r1);
p.execute(r2);
assertTrue(p.getQueue().contains(r2));
- TrackedNoOpRunnable r3 = new TrackedNoOpRunnable();
p.execute(r3);
assertFalse(p.getQueue().contains(r2));
assertTrue(p.getQueue().contains(r3));
- try { p.shutdownNow(); } catch (SecurityException ok) { return; }
- } finally {
- joinPool(p);
}
+ assertEquals(LatchAwaiter.DONE, r1.state);
+ assertEquals(LatchAwaiter.NEW, r2.state);
+ assertEquals(LatchAwaiter.DONE, r3.state);
}
/**
* execute throws RejectedExecutionException if shutdown
*/
public void testRejectedExecutionExceptionOnShutdown() {
- ThreadPoolExecutor p =
- new CustomTPE(1,1,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(1));
+ final ThreadPoolExecutor p =
+ new CustomTPE(1, 1,
+ LONG_DELAY_MS, MILLISECONDS,
+ new ArrayBlockingQueue<Runnable>(1));
try { p.shutdown(); } catch (SecurityException ok) { return; }
- try {
- p.execute(new NoOpRunnable());
- shouldThrow();
- } catch (RejectedExecutionException success) {}
-
- joinPool(p);
+ try (PoolCleaner cleaner = cleaner(p)) {
+ try {
+ p.execute(new NoOpRunnable());
+ shouldThrow();
+ } catch (RejectedExecutionException success) {}
+ }
}
/**
* execute using CallerRunsPolicy drops task on shutdown
*/
public void testCallerRunsOnShutdown() {
- RejectedExecutionHandler h = new CustomTPE.CallerRunsPolicy();
- ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
-
+ final ThreadPoolExecutor p =
+ new CustomTPE(1, 1,
+ LONG_DELAY_MS, MILLISECONDS,
+ new ArrayBlockingQueue<Runnable>(1),
+ new CustomTPE.CallerRunsPolicy());
try { p.shutdown(); } catch (SecurityException ok) { return; }
- try {
+ try (PoolCleaner cleaner = cleaner(p)) {
TrackedNoOpRunnable r = new TrackedNoOpRunnable();
p.execute(r);
assertFalse(r.done);
- } finally {
- joinPool(p);
}
}
@@ -1145,16 +1280,16 @@
* execute using DiscardPolicy drops task on shutdown
*/
public void testDiscardOnShutdown() {
- RejectedExecutionHandler h = new CustomTPE.DiscardPolicy();
- ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
-
+ final ThreadPoolExecutor p =
+ new CustomTPE(1, 1,
+ LONG_DELAY_MS, MILLISECONDS,
+ new ArrayBlockingQueue<Runnable>(1),
+ new CustomTPE.DiscardPolicy());
try { p.shutdown(); } catch (SecurityException ok) { return; }
- try {
+ try (PoolCleaner cleaner = cleaner(p)) {
TrackedNoOpRunnable r = new TrackedNoOpRunnable();
p.execute(r);
assertFalse(r.done);
- } finally {
- joinPool(p);
}
}
@@ -1162,16 +1297,17 @@
* execute using DiscardOldestPolicy drops task on shutdown
*/
public void testDiscardOldestOnShutdown() {
- RejectedExecutionHandler h = new CustomTPE.DiscardOldestPolicy();
- ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
+ final ThreadPoolExecutor p =
+ new CustomTPE(1, 1,
+ LONG_DELAY_MS, MILLISECONDS,
+ new ArrayBlockingQueue<Runnable>(1),
+ new CustomTPE.DiscardOldestPolicy());
try { p.shutdown(); } catch (SecurityException ok) { return; }
- try {
+ try (PoolCleaner cleaner = cleaner(p)) {
TrackedNoOpRunnable r = new TrackedNoOpRunnable();
p.execute(r);
assertFalse(r.done);
- } finally {
- joinPool(p);
}
}
@@ -1179,30 +1315,32 @@
* execute(null) throws NPE
*/
public void testExecuteNull() {
- ThreadPoolExecutor p = null;
- try {
- p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
- p.execute(null);
- shouldThrow();
- } catch (NullPointerException success) {}
-
- joinPool(p);
+ final ThreadPoolExecutor p =
+ new CustomTPE(1, 2,
+ 1L, SECONDS,
+ new ArrayBlockingQueue<Runnable>(10));
+ try (PoolCleaner cleaner = cleaner(p)) {
+ try {
+ p.execute(null);
+ shouldThrow();
+ } catch (NullPointerException success) {}
+ }
}
/**
* setCorePoolSize of negative value throws IllegalArgumentException
*/
public void testCorePoolSizeIllegalArgumentException() {
- ThreadPoolExecutor p =
- new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
- try {
- p.setCorePoolSize(-1);
- shouldThrow();
- } catch (IllegalArgumentException success) {
- } finally {
- try { p.shutdown(); } catch (SecurityException ok) { return; }
+ final ThreadPoolExecutor p =
+ new CustomTPE(1, 2,
+ LONG_DELAY_MS, MILLISECONDS,
+ new ArrayBlockingQueue<Runnable>(10));
+ try (PoolCleaner cleaner = cleaner(p)) {
+ try {
+ p.setCorePoolSize(-1);
+ shouldThrow();
+ } catch (IllegalArgumentException success) {}
}
- joinPool(p);
}
/**
@@ -1210,16 +1348,16 @@
* if given a value less the core pool size
*/
public void testMaximumPoolSizeIllegalArgumentException() {
- ThreadPoolExecutor p =
- new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
- try {
- p.setMaximumPoolSize(1);
- shouldThrow();
- } catch (IllegalArgumentException success) {
- } finally {
- try { p.shutdown(); } catch (SecurityException ok) { return; }
+ final ThreadPoolExecutor p =
+ new CustomTPE(2, 3,
+ LONG_DELAY_MS, MILLISECONDS,
+ new ArrayBlockingQueue<Runnable>(10));
+ try (PoolCleaner cleaner = cleaner(p)) {
+ try {
+ p.setMaximumPoolSize(1);
+ shouldThrow();
+ } catch (IllegalArgumentException success) {}
}
- joinPool(p);
}
/**
@@ -1227,16 +1365,16 @@
* if given a negative value
*/
public void testMaximumPoolSizeIllegalArgumentException2() {
- ThreadPoolExecutor p =
- new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
- try {
- p.setMaximumPoolSize(-1);
- shouldThrow();
- } catch (IllegalArgumentException success) {
- } finally {
- try { p.shutdown(); } catch (SecurityException ok) { return; }
+ final ThreadPoolExecutor p =
+ new CustomTPE(2, 3,
+ LONG_DELAY_MS,
+ MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
+ try (PoolCleaner cleaner = cleaner(p)) {
+ try {
+ p.setMaximumPoolSize(-1);
+ shouldThrow();
+ } catch (IllegalArgumentException success) {}
}
- joinPool(p);
}
/**
@@ -1244,17 +1382,16 @@
* when given a negative value
*/
public void testKeepAliveTimeIllegalArgumentException() {
- ThreadPoolExecutor p =
- new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
-
- try {
- p.setKeepAliveTime(-1,MILLISECONDS);
- shouldThrow();
- } catch (IllegalArgumentException success) {
- } finally {
- try { p.shutdown(); } catch (SecurityException ok) { return; }
+ final ThreadPoolExecutor p =
+ new CustomTPE(2, 3,
+ LONG_DELAY_MS, MILLISECONDS,
+ new ArrayBlockingQueue<Runnable>(10));
+ try (PoolCleaner cleaner = cleaner(p)) {
+ try {
+ p.setKeepAliveTime(-1, MILLISECONDS);
+ shouldThrow();
+ } catch (IllegalArgumentException success) {}
}
- joinPool(p);
}
/**
@@ -1262,9 +1399,11 @@
*/
public void testTerminated() {
CustomTPE p = new CustomTPE();
- try { p.shutdown(); } catch (SecurityException ok) { return; }
- assertTrue(p.terminatedCalled());
- joinPool(p);
+ try (PoolCleaner cleaner = cleaner(p)) {
+ try { p.shutdown(); } catch (SecurityException ok) { return; }
+ assertTrue(p.terminatedCalled());
+ assertTrue(p.isShutdown());
+ }
}
/**
@@ -1272,7 +1411,7 @@
*/
public void testBeforeAfter() throws InterruptedException {
CustomTPE p = new CustomTPE();
- try {
+ try (PoolCleaner cleaner = cleaner(p)) {
final CountDownLatch done = new CountDownLatch(1);
p.execute(new CheckedRunnable() {
public void realRun() {
@@ -1282,9 +1421,6 @@
assertEquals(0, done.getCount());
assertTrue(p.afterCalled());
assertTrue(p.beforeCalled());
- try { p.shutdown(); } catch (SecurityException ok) { return; }
- } finally {
- joinPool(p);
}
}
@@ -1292,13 +1428,14 @@
* completed submit of callable returns result
*/
public void testSubmitCallable() throws Exception {
- ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
- try {
+ final ExecutorService e =
+ new CustomTPE(2, 2,
+ LONG_DELAY_MS, MILLISECONDS,
+ new ArrayBlockingQueue<Runnable>(10));
+ try (PoolCleaner cleaner = cleaner(e)) {
Future<String> future = e.submit(new StringTask());
String result = future.get();
assertSame(TEST_STRING, result);
- } finally {
- joinPool(e);
}
}
@@ -1306,13 +1443,14 @@
* completed submit of runnable returns successfully
*/
public void testSubmitRunnable() throws Exception {
- ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
- try {
+ final ExecutorService e =
+ new CustomTPE(2, 2,
+ LONG_DELAY_MS, MILLISECONDS,
+ new ArrayBlockingQueue<Runnable>(10));
+ try (PoolCleaner cleaner = cleaner(e)) {
Future<?> future = e.submit(new NoOpRunnable());
future.get();
assertTrue(future.isDone());
- } finally {
- joinPool(e);
}
}
@@ -1320,13 +1458,14 @@
* completed submit of (runnable, result) returns result
*/
public void testSubmitRunnable2() throws Exception {
- ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
- try {
+ final ExecutorService e =
+ new CustomTPE(2, 2,
+ LONG_DELAY_MS, MILLISECONDS,
+ new ArrayBlockingQueue<Runnable>(10));
+ try (PoolCleaner cleaner = cleaner(e)) {
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
String result = future.get();
assertSame(TEST_STRING, result);
- } finally {
- joinPool(e);
}
}
@@ -1334,13 +1473,15 @@
* invokeAny(null) throws NPE
*/
public void testInvokeAny1() throws Exception {
- ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
- try {
- e.invokeAny(null);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
- joinPool(e);
+ final ExecutorService e =
+ new CustomTPE(2, 2,
+ LONG_DELAY_MS, MILLISECONDS,
+ new ArrayBlockingQueue<Runnable>(10));
+ try (PoolCleaner cleaner = cleaner(e)) {
+ try {
+ e.invokeAny(null);
+ shouldThrow();
+ } catch (NullPointerException success) {}
}
}
@@ -1348,13 +1489,15 @@
* invokeAny(empty collection) throws IAE
*/
public void testInvokeAny2() throws Exception {
- ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
- try {
- e.invokeAny(new ArrayList<Callable<String>>());
- shouldThrow();
- } catch (IllegalArgumentException success) {
- } finally {
- joinPool(e);
+ final ExecutorService e =
+ new CustomTPE(2, 2,
+ LONG_DELAY_MS, MILLISECONDS,
+ new ArrayBlockingQueue<Runnable>(10));
+ try (PoolCleaner cleaner = cleaner(e)) {
+ try {
+ e.invokeAny(new ArrayList<Callable<String>>());
+ shouldThrow();
+ } catch (IllegalArgumentException success) {}
}
}
@@ -1363,17 +1506,19 @@
*/
public void testInvokeAny3() throws Exception {
CountDownLatch latch = new CountDownLatch(1);
- ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
- List<Callable<String>> l = new ArrayList<Callable<String>>();
- l.add(latchAwaitingStringTask(latch));
- l.add(null);
- try {
- e.invokeAny(l);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
+ final ExecutorService e =
+ new CustomTPE(2, 2,
+ LONG_DELAY_MS, MILLISECONDS,
+ new ArrayBlockingQueue<Runnable>(10));
+ try (PoolCleaner cleaner = cleaner(e)) {
+ List<Callable<String>> l = new ArrayList<Callable<String>>();
+ l.add(latchAwaitingStringTask(latch));
+ l.add(null);
+ try {
+ e.invokeAny(l);
+ shouldThrow();
+ } catch (NullPointerException success) {}
latch.countDown();
- joinPool(e);
}
}
@@ -1381,16 +1526,19 @@
* invokeAny(c) throws ExecutionException if no task completes
*/
public void testInvokeAny4() throws Exception {
- ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
- List<Callable<String>> l = new ArrayList<Callable<String>>();
- l.add(new NPETask());
- try {
- e.invokeAny(l);
- shouldThrow();
- } catch (ExecutionException success) {
- assertTrue(success.getCause() instanceof NullPointerException);
- } finally {
- joinPool(e);
+ final ExecutorService e =
+ new CustomTPE(2, 2,
+ LONG_DELAY_MS, MILLISECONDS,
+ new ArrayBlockingQueue<Runnable>(10));
+ try (PoolCleaner cleaner = cleaner(e)) {
+ List<Callable<String>> l = new ArrayList<Callable<String>>();
+ l.add(new NPETask());
+ try {
+ e.invokeAny(l);
+ shouldThrow();
+ } catch (ExecutionException success) {
+ assertTrue(success.getCause() instanceof NullPointerException);
+ }
}
}
@@ -1398,15 +1546,16 @@
* invokeAny(c) returns result of some task
*/
public void testInvokeAny5() throws Exception {
- ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
- try {
+ final ExecutorService e =
+ new CustomTPE(2, 2,
+ LONG_DELAY_MS, MILLISECONDS,
+ new ArrayBlockingQueue<Runnable>(10));
+ try (PoolCleaner cleaner = cleaner(e)) {
List<Callable<String>> l = new ArrayList<Callable<String>>();
l.add(new StringTask());
l.add(new StringTask());
String result = e.invokeAny(l);
assertSame(TEST_STRING, result);
- } finally {
- joinPool(e);
}
}
@@ -1414,13 +1563,15 @@
* invokeAll(null) throws NPE
*/
public void testInvokeAll1() throws Exception {
- ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
- try {
- e.invokeAll(null);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
- joinPool(e);
+ final ExecutorService e =
+ new CustomTPE(2, 2,
+ LONG_DELAY_MS, MILLISECONDS,
+ new ArrayBlockingQueue<Runnable>(10));
+ try (PoolCleaner cleaner = cleaner(e)) {
+ try {
+ e.invokeAll(null);
+ shouldThrow();
+ } catch (NullPointerException success) {}
}
}
@@ -1428,12 +1579,13 @@
* invokeAll(empty collection) returns empty collection
*/
public void testInvokeAll2() throws Exception {
- ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
- try {
+ final ExecutorService e =
+ new CustomTPE(2, 2,
+ LONG_DELAY_MS, MILLISECONDS,
+ new ArrayBlockingQueue<Runnable>(10));
+ try (PoolCleaner cleaner = cleaner(e)) {
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
assertTrue(r.isEmpty());
- } finally {
- joinPool(e);
}
}
@@ -1441,16 +1593,18 @@
* invokeAll(c) throws NPE if c has null elements
*/
public void testInvokeAll3() throws Exception {
- ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
- List<Callable<String>> l = new ArrayList<Callable<String>>();
- l.add(new StringTask());
- l.add(null);
- try {
- e.invokeAll(l);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
- joinPool(e);
+ final ExecutorService e =
+ new CustomTPE(2, 2,
+ LONG_DELAY_MS, MILLISECONDS,
+ new ArrayBlockingQueue<Runnable>(10));
+ try (PoolCleaner cleaner = cleaner(e)) {
+ List<Callable<String>> l = new ArrayList<Callable<String>>();
+ l.add(new StringTask());
+ l.add(null);
+ try {
+ e.invokeAll(l);
+ shouldThrow();
+ } catch (NullPointerException success) {}
}
}
@@ -1458,18 +1612,21 @@
* get of element of invokeAll(c) throws exception on failed task
*/
public void testInvokeAll4() throws Exception {
- ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
- List<Callable<String>> l = new ArrayList<Callable<String>>();
- l.add(new NPETask());
- List<Future<String>> futures = e.invokeAll(l);
- assertEquals(1, futures.size());
- try {
- futures.get(0).get();
- shouldThrow();
- } catch (ExecutionException success) {
- assertTrue(success.getCause() instanceof NullPointerException);
- } finally {
- joinPool(e);
+ final ExecutorService e =
+ new CustomTPE(2, 2,
+ LONG_DELAY_MS, MILLISECONDS,
+ new ArrayBlockingQueue<Runnable>(10));
+ try (PoolCleaner cleaner = cleaner(e)) {
+ List<Callable<String>> l = new ArrayList<Callable<String>>();
+ l.add(new NPETask());
+ List<Future<String>> futures = e.invokeAll(l);
+ assertEquals(1, futures.size());
+ try {
+ futures.get(0).get();
+ shouldThrow();
+ } catch (ExecutionException success) {
+ assertTrue(success.getCause() instanceof NullPointerException);
+ }
}
}
@@ -1477,8 +1634,11 @@
* invokeAll(c) returns results of all completed tasks
*/
public void testInvokeAll5() throws Exception {
- ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
- try {
+ final ExecutorService e =
+ new CustomTPE(2, 2,
+ LONG_DELAY_MS, MILLISECONDS,
+ new ArrayBlockingQueue<Runnable>(10));
+ try (PoolCleaner cleaner = cleaner(e)) {
List<Callable<String>> l = new ArrayList<Callable<String>>();
l.add(new StringTask());
l.add(new StringTask());
@@ -1486,8 +1646,6 @@
assertEquals(2, futures.size());
for (Future<String> future : futures)
assertSame(TEST_STRING, future.get());
- } finally {
- joinPool(e);
}
}
@@ -1495,13 +1653,15 @@
* timed invokeAny(null) throws NPE
*/
public void testTimedInvokeAny1() throws Exception {
- ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
- try {
- e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
- joinPool(e);
+ final ExecutorService e =
+ new CustomTPE(2, 2,
+ LONG_DELAY_MS, MILLISECONDS,
+ new ArrayBlockingQueue<Runnable>(10));
+ try (PoolCleaner cleaner = cleaner(e)) {
+ try {
+ e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
+ shouldThrow();
+ } catch (NullPointerException success) {}
}
}
@@ -1509,15 +1669,17 @@
* timed invokeAny(,,null) throws NPE
*/
public void testTimedInvokeAnyNullTimeUnit() throws Exception {
- ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
- List<Callable<String>> l = new ArrayList<Callable<String>>();
- l.add(new StringTask());
- try {
- e.invokeAny(l, MEDIUM_DELAY_MS, null);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
- joinPool(e);
+ final ExecutorService e =
+ new CustomTPE(2, 2,
+ LONG_DELAY_MS, MILLISECONDS,
+ new ArrayBlockingQueue<Runnable>(10));
+ try (PoolCleaner cleaner = cleaner(e)) {
+ List<Callable<String>> l = new ArrayList<Callable<String>>();
+ l.add(new StringTask());
+ try {
+ e.invokeAny(l, MEDIUM_DELAY_MS, null);
+ shouldThrow();
+ } catch (NullPointerException success) {}
}
}
@@ -1525,13 +1687,16 @@
* timed invokeAny(empty collection) throws IAE
*/
public void testTimedInvokeAny2() throws Exception {
- ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
- try {
- e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
- shouldThrow();
- } catch (IllegalArgumentException success) {
- } finally {
- joinPool(e);
+ final ExecutorService e =
+ new CustomTPE(2, 2,
+ LONG_DELAY_MS, MILLISECONDS,
+ new ArrayBlockingQueue<Runnable>(10));
+ try (PoolCleaner cleaner = cleaner(e)) {
+ try {
+ e.invokeAny(new ArrayList<Callable<String>>(),
+ MEDIUM_DELAY_MS, MILLISECONDS);
+ shouldThrow();
+ } catch (IllegalArgumentException success) {}
}
}
@@ -1540,17 +1705,19 @@
*/
public void testTimedInvokeAny3() throws Exception {
CountDownLatch latch = new CountDownLatch(1);
- ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
- List<Callable<String>> l = new ArrayList<Callable<String>>();
- l.add(latchAwaitingStringTask(latch));
- l.add(null);
- try {
- e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
+ final ExecutorService e =
+ new CustomTPE(2, 2,
+ LONG_DELAY_MS, MILLISECONDS,
+ new ArrayBlockingQueue<Runnable>(10));
+ try (PoolCleaner cleaner = cleaner(e)) {
+ List<Callable<String>> l = new ArrayList<Callable<String>>();
+ l.add(latchAwaitingStringTask(latch));
+ l.add(null);
+ try {
+ e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
+ shouldThrow();
+ } catch (NullPointerException success) {}
latch.countDown();
- joinPool(e);
}
}
@@ -1558,16 +1725,21 @@
* timed invokeAny(c) throws ExecutionException if no task completes
*/
public void testTimedInvokeAny4() throws Exception {
- ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
- List<Callable<String>> l = new ArrayList<Callable<String>>();
- l.add(new NPETask());
- try {
- e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
- shouldThrow();
- } catch (ExecutionException success) {
- assertTrue(success.getCause() instanceof NullPointerException);
- } finally {
- joinPool(e);
+ final ExecutorService e =
+ new CustomTPE(2, 2,
+ LONG_DELAY_MS, MILLISECONDS,
+ new ArrayBlockingQueue<Runnable>(10));
+ try (PoolCleaner cleaner = cleaner(e)) {
+ long startTime = System.nanoTime();
+ List<Callable<String>> l = new ArrayList<Callable<String>>();
+ l.add(new NPETask());
+ try {
+ e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
+ shouldThrow();
+ } catch (ExecutionException success) {
+ assertTrue(success.getCause() instanceof NullPointerException);
+ }
+ assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
}
}
@@ -1575,15 +1747,18 @@
* timed invokeAny(c) returns result of some task
*/
public void testTimedInvokeAny5() throws Exception {
- ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
- try {
+ final ExecutorService e =
+ new CustomTPE(2, 2,
+ LONG_DELAY_MS, MILLISECONDS,
+ new ArrayBlockingQueue<Runnable>(10));
+ try (PoolCleaner cleaner = cleaner(e)) {
+ long startTime = System.nanoTime();
List<Callable<String>> l = new ArrayList<Callable<String>>();
l.add(new StringTask());
l.add(new StringTask());
- String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
+ String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
assertSame(TEST_STRING, result);
- } finally {
- joinPool(e);
+ assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
}
}
@@ -1591,13 +1766,15 @@
* timed invokeAll(null) throws NPE
*/
public void testTimedInvokeAll1() throws Exception {
- ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
- try {
- e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
- joinPool(e);
+ final ExecutorService e =
+ new CustomTPE(2, 2,
+ LONG_DELAY_MS, MILLISECONDS,
+ new ArrayBlockingQueue<Runnable>(10));
+ try (PoolCleaner cleaner = cleaner(e)) {
+ try {
+ e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
+ shouldThrow();
+ } catch (NullPointerException success) {}
}
}
@@ -1605,15 +1782,17 @@
* timed invokeAll(,,null) throws NPE
*/
public void testTimedInvokeAllNullTimeUnit() throws Exception {
- ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
- List<Callable<String>> l = new ArrayList<Callable<String>>();
- l.add(new StringTask());
- try {
- e.invokeAll(l, MEDIUM_DELAY_MS, null);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
- joinPool(e);
+ final ExecutorService e =
+ new CustomTPE(2, 2,
+ LONG_DELAY_MS, MILLISECONDS,
+ new ArrayBlockingQueue<Runnable>(10));
+ try (PoolCleaner cleaner = cleaner(e)) {
+ List<Callable<String>> l = new ArrayList<Callable<String>>();
+ l.add(new StringTask());
+ try {
+ e.invokeAll(l, MEDIUM_DELAY_MS, null);
+ shouldThrow();
+ } catch (NullPointerException success) {}
}
}
@@ -1621,12 +1800,14 @@
* timed invokeAll(empty collection) returns empty collection
*/
public void testTimedInvokeAll2() throws Exception {
- ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
- try {
- List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
+ final ExecutorService e =
+ new CustomTPE(2, 2,
+ LONG_DELAY_MS, MILLISECONDS,
+ new ArrayBlockingQueue<Runnable>(10));
+ try (PoolCleaner cleaner = cleaner(e)) {
+ List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(),
+ MEDIUM_DELAY_MS, MILLISECONDS);
assertTrue(r.isEmpty());
- } finally {
- joinPool(e);
}
}
@@ -1634,16 +1815,18 @@
* timed invokeAll(c) throws NPE if c has null elements
*/
public void testTimedInvokeAll3() throws Exception {
- ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
- List<Callable<String>> l = new ArrayList<Callable<String>>();
- l.add(new StringTask());
- l.add(null);
- try {
- e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
- joinPool(e);
+ final ExecutorService e =
+ new CustomTPE(2, 2,
+ LONG_DELAY_MS, MILLISECONDS,
+ new ArrayBlockingQueue<Runnable>(10));
+ try (PoolCleaner cleaner = cleaner(e)) {
+ List<Callable<String>> l = new ArrayList<Callable<String>>();
+ l.add(new StringTask());
+ l.add(null);
+ try {
+ e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
+ shouldThrow();
+ } catch (NullPointerException success) {}
}
}
@@ -1651,19 +1834,22 @@
* get of element of invokeAll(c) throws exception on failed task
*/
public void testTimedInvokeAll4() throws Exception {
- ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
- List<Callable<String>> l = new ArrayList<Callable<String>>();
- l.add(new NPETask());
- List<Future<String>> futures =
- e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
- assertEquals(1, futures.size());
- try {
- futures.get(0).get();
- shouldThrow();
- } catch (ExecutionException success) {
- assertTrue(success.getCause() instanceof NullPointerException);
- } finally {
- joinPool(e);
+ final ExecutorService e =
+ new CustomTPE(2, 2,
+ LONG_DELAY_MS, MILLISECONDS,
+ new ArrayBlockingQueue<Runnable>(10));
+ try (PoolCleaner cleaner = cleaner(e)) {
+ List<Callable<String>> l = new ArrayList<Callable<String>>();
+ l.add(new NPETask());
+ List<Future<String>> futures =
+ e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
+ assertEquals(1, futures.size());
+ try {
+ futures.get(0).get();
+ shouldThrow();
+ } catch (ExecutionException success) {
+ assertTrue(success.getCause() instanceof NullPointerException);
+ }
}
}
@@ -1671,18 +1857,19 @@
* timed invokeAll(c) returns results of all completed tasks
*/
public void testTimedInvokeAll5() throws Exception {
- ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
- try {
+ final ExecutorService e =
+ new CustomTPE(2, 2,
+ LONG_DELAY_MS, MILLISECONDS,
+ new ArrayBlockingQueue<Runnable>(10));
+ try (PoolCleaner cleaner = cleaner(e)) {
List<Callable<String>> l = new ArrayList<Callable<String>>();
l.add(new StringTask());
l.add(new StringTask());
List<Future<String>> futures =
- e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
+ e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
assertEquals(2, futures.size());
for (Future<String> future : futures)
assertSame(TEST_STRING, future.get());
- } finally {
- joinPool(e);
}
}
@@ -1690,21 +1877,40 @@
* timed invokeAll(c) cancels tasks not completed by timeout
*/
public void testTimedInvokeAll6() throws Exception {
- ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
- try {
- List<Callable<String>> l = new ArrayList<Callable<String>>();
- l.add(new StringTask());
- l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
- l.add(new StringTask());
- List<Future<String>> futures =
- e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
- assertEquals(l.size(), futures.size());
- for (Future future : futures)
- assertTrue(future.isDone());
- assertFalse(futures.get(0).isCancelled());
- assertTrue(futures.get(1).isCancelled());
- } finally {
- joinPool(e);
+ for (long timeout = timeoutMillis();;) {
+ final CountDownLatch done = new CountDownLatch(1);
+ final Callable<String> waiter = new CheckedCallable<String>() {
+ public String realCall() {
+ try { done.await(LONG_DELAY_MS, MILLISECONDS); }
+ catch (InterruptedException ok) {}
+ return "1"; }};
+ final ExecutorService p =
+ new CustomTPE(2, 2,
+ LONG_DELAY_MS, MILLISECONDS,
+ new ArrayBlockingQueue<Runnable>(10));
+ try (PoolCleaner cleaner = cleaner(p, done)) {
+ List<Callable<String>> tasks = new ArrayList<>();
+ tasks.add(new StringTask("0"));
+ tasks.add(waiter);
+ tasks.add(new StringTask("2"));
+ long startTime = System.nanoTime();
+ List<Future<String>> futures =
+ p.invokeAll(tasks, timeout, MILLISECONDS);
+ assertEquals(tasks.size(), futures.size());
+ assertTrue(millisElapsedSince(startTime) >= timeout);
+ for (Future future : futures)
+ assertTrue(future.isDone());
+ assertTrue(futures.get(1).isCancelled());
+ try {
+ assertEquals("0", futures.get(0).get());
+ assertEquals("2", futures.get(2).get());
+ break;
+ } catch (CancellationException retryWithLongerTimeout) {
+ timeout *= 2;
+ if (timeout >= LONG_DELAY_MS / 2)
+ fail("expected exactly one task to be cancelled");
+ }
+ }
}
}
@@ -1718,7 +1924,7 @@
LONG_DELAY_MS, MILLISECONDS,
new LinkedBlockingQueue<Runnable>(),
new FailingThreadFactory());
- try {
+ try (PoolCleaner cleaner = cleaner(e)) {
final int TASKS = 100;
final CountDownLatch done = new CountDownLatch(TASKS);
for (int k = 0; k < TASKS; ++k)
@@ -1727,8 +1933,6 @@
done.countDown();
}});
assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
- } finally {
- joinPool(e);
}
}
@@ -1736,38 +1940,40 @@
* allowsCoreThreadTimeOut is by default false.
*/
public void testAllowsCoreThreadTimeOut() {
- ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
- assertFalse(p.allowsCoreThreadTimeOut());
- joinPool(p);
+ final ThreadPoolExecutor p =
+ new CustomTPE(2, 2,
+ 1000, MILLISECONDS,
+ new ArrayBlockingQueue<Runnable>(10));
+ try (PoolCleaner cleaner = cleaner(p)) {
+ assertFalse(p.allowsCoreThreadTimeOut());
+ }
}
/**
* allowCoreThreadTimeOut(true) causes idle threads to time out
*/
public void testAllowCoreThreadTimeOut_true() throws Exception {
- long coreThreadTimeOut = SHORT_DELAY_MS;
+ long keepAliveTime = timeoutMillis();
final ThreadPoolExecutor p =
new CustomTPE(2, 10,
- coreThreadTimeOut, MILLISECONDS,
+ keepAliveTime, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
- final CountDownLatch threadStarted = new CountDownLatch(1);
- try {
+ try (PoolCleaner cleaner = cleaner(p)) {
+ final CountDownLatch threadStarted = new CountDownLatch(1);
p.allowCoreThreadTimeOut(true);
p.execute(new CheckedRunnable() {
- public void realRun() throws InterruptedException {
+ public void realRun() {
threadStarted.countDown();
assertEquals(1, p.getPoolSize());
}});
await(threadStarted);
- delay(coreThreadTimeOut);
+ delay(keepAliveTime);
long startTime = System.nanoTime();
while (p.getPoolSize() > 0
&& millisElapsedSince(startTime) < LONG_DELAY_MS)
Thread.yield();
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
assertEquals(0, p.getPoolSize());
- } finally {
- joinPool(p);
}
}
@@ -1775,23 +1981,59 @@
* allowCoreThreadTimeOut(false) causes idle threads not to time out
*/
public void testAllowCoreThreadTimeOut_false() throws Exception {
- long coreThreadTimeOut = SHORT_DELAY_MS;
+ long keepAliveTime = timeoutMillis();
final ThreadPoolExecutor p =
new CustomTPE(2, 10,
- coreThreadTimeOut, MILLISECONDS,
+ keepAliveTime, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
- final CountDownLatch threadStarted = new CountDownLatch(1);
- try {
+ try (PoolCleaner cleaner = cleaner(p)) {
+ final CountDownLatch threadStarted = new CountDownLatch(1);
p.allowCoreThreadTimeOut(false);
p.execute(new CheckedRunnable() {
public void realRun() throws InterruptedException {
threadStarted.countDown();
assertTrue(p.getPoolSize() >= 1);
}});
- delay(2 * coreThreadTimeOut);
+ delay(2 * keepAliveTime);
assertTrue(p.getPoolSize() >= 1);
- } finally {
- joinPool(p);
+ }
+ }
+
+ /**
+ * get(cancelled task) throws CancellationException
+ * (in part, a test of CustomTPE itself)
+ */
+ public void testGet_cancelled() throws Exception {
+ final CountDownLatch done = new CountDownLatch(1);
+ final ExecutorService e =
+ new CustomTPE(1, 1,
+ LONG_DELAY_MS, MILLISECONDS,
+ new LinkedBlockingQueue<Runnable>());
+ try (PoolCleaner cleaner = cleaner(e, done)) {
+ final CountDownLatch blockerStarted = new CountDownLatch(1);
+ final List<Future<?>> futures = new ArrayList<>();
+ for (int i = 0; i < 2; i++) {
+ Runnable r = new CheckedRunnable() { public void realRun()
+ throws Throwable {
+ blockerStarted.countDown();
+ assertTrue(done.await(2 * LONG_DELAY_MS, MILLISECONDS));
+ }};
+ futures.add(e.submit(r));
+ }
+ await(blockerStarted);
+ for (Future<?> future : futures) future.cancel(false);
+ for (Future<?> future : futures) {
+ try {
+ future.get();
+ shouldThrow();
+ } catch (CancellationException success) {}
+ try {
+ future.get(LONG_DELAY_MS, MILLISECONDS);
+ shouldThrow();
+ } catch (CancellationException success) {}
+ assertTrue(future.isCancelled());
+ assertTrue(future.isDone());
+ }
}
}
diff --git a/jsr166-tests/src/test/java/jsr166/ThreadPoolExecutorTest.java b/jsr166-tests/src/test/java/jsr166/ThreadPoolExecutorTest.java
index 52a7002..7fe26f4 100644
--- a/jsr166-tests/src/test/java/jsr166/ThreadPoolExecutorTest.java
+++ b/jsr166-tests/src/test/java/jsr166/ThreadPoolExecutorTest.java
@@ -10,12 +10,14 @@
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.NANOSECONDS;
+import static java.util.concurrent.TimeUnit.SECONDS;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable;
+import java.util.concurrent.CancellationException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
@@ -29,6 +31,7 @@
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
import junit.framework.Test;
import junit.framework.TestSuite;
@@ -41,7 +44,7 @@
// main(suite(), args);
// }
// public static Test suite() {
- // return new TestSuite(...);
+ // return new TestSuite(ThreadPoolExecutorTest.class);
// }
static class ExtendedTPE extends ThreadPoolExecutor {
@@ -89,16 +92,12 @@
new ThreadPoolExecutor(1, 1,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
- final CountDownLatch done = new CountDownLatch(1);
- final Runnable task = new CheckedRunnable() {
- public void realRun() {
- done.countDown();
- }};
- try {
+ try (PoolCleaner cleaner = cleaner(p)) {
+ final CountDownLatch done = new CountDownLatch(1);
+ final Runnable task = new CheckedRunnable() {
+ public void realRun() { done.countDown(); }};
p.execute(task);
- assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS));
- } finally {
- joinPool(p);
+ assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
}
}
@@ -107,25 +106,22 @@
* thread becomes active
*/
public void testGetActiveCount() throws InterruptedException {
+ final CountDownLatch done = new CountDownLatch(1);
final ThreadPoolExecutor p =
new ThreadPoolExecutor(2, 2,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
- final CountDownLatch threadStarted = new CountDownLatch(1);
- final CountDownLatch done = new CountDownLatch(1);
- try {
+ try (PoolCleaner cleaner = cleaner(p, done)) {
+ final CountDownLatch threadStarted = new CountDownLatch(1);
assertEquals(0, p.getActiveCount());
p.execute(new CheckedRunnable() {
public void realRun() throws InterruptedException {
threadStarted.countDown();
assertEquals(1, p.getActiveCount());
- done.await();
+ await(done);
}});
- assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
+ await(threadStarted);
assertEquals(1, p.getActiveCount());
- } finally {
- done.countDown();
- joinPool(p);
}
}
@@ -134,17 +130,25 @@
*/
public void testPrestartCoreThread() {
final ThreadPoolExecutor p =
- new ThreadPoolExecutor(2, 2,
+ new ThreadPoolExecutor(2, 6,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
- assertEquals(0, p.getPoolSize());
- assertTrue(p.prestartCoreThread());
- assertEquals(1, p.getPoolSize());
- assertTrue(p.prestartCoreThread());
- assertEquals(2, p.getPoolSize());
- assertFalse(p.prestartCoreThread());
- assertEquals(2, p.getPoolSize());
- joinPool(p);
+ try (PoolCleaner cleaner = cleaner(p)) {
+ assertEquals(0, p.getPoolSize());
+ assertTrue(p.prestartCoreThread());
+ assertEquals(1, p.getPoolSize());
+ assertTrue(p.prestartCoreThread());
+ assertEquals(2, p.getPoolSize());
+ assertFalse(p.prestartCoreThread());
+ assertEquals(2, p.getPoolSize());
+ p.setCorePoolSize(4);
+ assertTrue(p.prestartCoreThread());
+ assertEquals(3, p.getPoolSize());
+ assertTrue(p.prestartCoreThread());
+ assertEquals(4, p.getPoolSize());
+ assertFalse(p.prestartCoreThread());
+ assertEquals(4, p.getPoolSize());
+ }
}
/**
@@ -152,15 +156,21 @@
*/
public void testPrestartAllCoreThreads() {
final ThreadPoolExecutor p =
- new ThreadPoolExecutor(2, 2,
+ new ThreadPoolExecutor(2, 6,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
- assertEquals(0, p.getPoolSize());
- p.prestartAllCoreThreads();
- assertEquals(2, p.getPoolSize());
- p.prestartAllCoreThreads();
- assertEquals(2, p.getPoolSize());
- joinPool(p);
+ try (PoolCleaner cleaner = cleaner(p)) {
+ assertEquals(0, p.getPoolSize());
+ p.prestartAllCoreThreads();
+ assertEquals(2, p.getPoolSize());
+ p.prestartAllCoreThreads();
+ assertEquals(2, p.getPoolSize());
+ p.setCorePoolSize(4);
+ p.prestartAllCoreThreads();
+ assertEquals(4, p.getPoolSize());
+ p.prestartAllCoreThreads();
+ assertEquals(4, p.getPoolSize());
+ }
}
/**
@@ -172,10 +182,10 @@
new ThreadPoolExecutor(2, 2,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
- final CountDownLatch threadStarted = new CountDownLatch(1);
- final CountDownLatch threadProceed = new CountDownLatch(1);
- final CountDownLatch threadDone = new CountDownLatch(1);
- try {
+ try (PoolCleaner cleaner = cleaner(p)) {
+ final CountDownLatch threadStarted = new CountDownLatch(1);
+ final CountDownLatch threadProceed = new CountDownLatch(1);
+ final CountDownLatch threadDone = new CountDownLatch(1);
assertEquals(0, p.getCompletedTaskCount());
p.execute(new CheckedRunnable() {
public void realRun() throws InterruptedException {
@@ -194,8 +204,6 @@
fail("timed out");
Thread.yield();
}
- } finally {
- joinPool(p);
}
}
@@ -207,8 +215,9 @@
new ThreadPoolExecutor(1, 1,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
- assertEquals(1, p.getCorePoolSize());
- joinPool(p);
+ try (PoolCleaner cleaner = cleaner(p)) {
+ assertEquals(1, p.getCorePoolSize());
+ }
}
/**
@@ -219,23 +228,25 @@
new ThreadPoolExecutor(2, 2,
1000, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
- assertEquals(1, p.getKeepAliveTime(TimeUnit.SECONDS));
- joinPool(p);
+ try (PoolCleaner cleaner = cleaner(p)) {
+ assertEquals(1, p.getKeepAliveTime(SECONDS));
+ }
}
/**
* getThreadFactory returns factory in constructor if not set
*/
public void testGetThreadFactory() {
- ThreadFactory tf = new SimpleThreadFactory();
+ ThreadFactory threadFactory = new SimpleThreadFactory();
final ThreadPoolExecutor p =
new ThreadPoolExecutor(1, 2,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10),
- tf,
+ threadFactory,
new NoOpREHandler());
- assertSame(tf, p.getThreadFactory());
- joinPool(p);
+ try (PoolCleaner cleaner = cleaner(p)) {
+ assertSame(threadFactory, p.getThreadFactory());
+ }
}
/**
@@ -246,10 +257,11 @@
new ThreadPoolExecutor(1, 2,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
- ThreadFactory tf = new SimpleThreadFactory();
- p.setThreadFactory(tf);
- assertSame(tf, p.getThreadFactory());
- joinPool(p);
+ try (PoolCleaner cleaner = cleaner(p)) {
+ ThreadFactory threadFactory = new SimpleThreadFactory();
+ p.setThreadFactory(threadFactory);
+ assertSame(threadFactory, p.getThreadFactory());
+ }
}
/**
@@ -260,12 +272,11 @@
new ThreadPoolExecutor(1, 2,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
- try {
- p.setThreadFactory(null);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
- joinPool(p);
+ try (PoolCleaner cleaner = cleaner(p)) {
+ try {
+ p.setThreadFactory(null);
+ shouldThrow();
+ } catch (NullPointerException success) {}
}
}
@@ -273,14 +284,15 @@
* getRejectedExecutionHandler returns handler in constructor if not set
*/
public void testGetRejectedExecutionHandler() {
- final RejectedExecutionHandler h = new NoOpREHandler();
+ final RejectedExecutionHandler handler = new NoOpREHandler();
final ThreadPoolExecutor p =
new ThreadPoolExecutor(1, 2,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10),
- h);
- assertSame(h, p.getRejectedExecutionHandler());
- joinPool(p);
+ handler);
+ try (PoolCleaner cleaner = cleaner(p)) {
+ assertSame(handler, p.getRejectedExecutionHandler());
+ }
}
/**
@@ -292,10 +304,11 @@
new ThreadPoolExecutor(1, 2,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
- RejectedExecutionHandler h = new NoOpREHandler();
- p.setRejectedExecutionHandler(h);
- assertSame(h, p.getRejectedExecutionHandler());
- joinPool(p);
+ try (PoolCleaner cleaner = cleaner(p)) {
+ RejectedExecutionHandler handler = new NoOpREHandler();
+ p.setRejectedExecutionHandler(handler);
+ assertSame(handler, p.getRejectedExecutionHandler());
+ }
}
/**
@@ -306,12 +319,11 @@
new ThreadPoolExecutor(1, 2,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
- try {
- p.setRejectedExecutionHandler(null);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
- joinPool(p);
+ try (PoolCleaner cleaner = cleaner(p)) {
+ try {
+ p.setRejectedExecutionHandler(null);
+ shouldThrow();
+ } catch (NullPointerException success) {}
}
}
@@ -321,28 +333,25 @@
*/
public void testGetLargestPoolSize() throws InterruptedException {
final int THREADS = 3;
+ final CountDownLatch done = new CountDownLatch(1);
final ThreadPoolExecutor p =
new ThreadPoolExecutor(THREADS, THREADS,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
- final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
- final CountDownLatch done = new CountDownLatch(1);
- try {
+ try (PoolCleaner cleaner = cleaner(p, done)) {
assertEquals(0, p.getLargestPoolSize());
+ final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
for (int i = 0; i < THREADS; i++)
p.execute(new CheckedRunnable() {
public void realRun() throws InterruptedException {
threadsStarted.countDown();
- done.await();
+ await(done);
assertEquals(THREADS, p.getLargestPoolSize());
}});
- assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS));
- assertEquals(THREADS, p.getLargestPoolSize());
- } finally {
- done.countDown();
- joinPool(p);
+ await(threadsStarted);
assertEquals(THREADS, p.getLargestPoolSize());
}
+ assertEquals(THREADS, p.getLargestPoolSize());
}
/**
@@ -354,8 +363,13 @@
new ThreadPoolExecutor(2, 3,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
- assertEquals(3, p.getMaximumPoolSize());
- joinPool(p);
+ try (PoolCleaner cleaner = cleaner(p)) {
+ assertEquals(3, p.getMaximumPoolSize());
+ p.setMaximumPoolSize(5);
+ assertEquals(5, p.getMaximumPoolSize());
+ p.setMaximumPoolSize(4);
+ assertEquals(4, p.getMaximumPoolSize());
+ }
}
/**
@@ -363,25 +377,22 @@
* become active
*/
public void testGetPoolSize() throws InterruptedException {
+ final CountDownLatch done = new CountDownLatch(1);
final ThreadPoolExecutor p =
new ThreadPoolExecutor(1, 1,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
- final CountDownLatch threadStarted = new CountDownLatch(1);
- final CountDownLatch done = new CountDownLatch(1);
- try {
+ try (PoolCleaner cleaner = cleaner(p, done)) {
assertEquals(0, p.getPoolSize());
+ final CountDownLatch threadStarted = new CountDownLatch(1);
p.execute(new CheckedRunnable() {
public void realRun() throws InterruptedException {
threadStarted.countDown();
assertEquals(1, p.getPoolSize());
- done.await();
+ await(done);
}});
- assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
+ await(threadStarted);
assertEquals(1, p.getPoolSize());
- } finally {
- done.countDown();
- joinPool(p);
}
}
@@ -389,26 +400,38 @@
* getTaskCount increases, but doesn't overestimate, when tasks submitted
*/
public void testGetTaskCount() throws InterruptedException {
+ final int TASKS = 3;
+ final CountDownLatch done = new CountDownLatch(1);
final ThreadPoolExecutor p =
new ThreadPoolExecutor(1, 1,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
- final CountDownLatch threadStarted = new CountDownLatch(1);
- final CountDownLatch done = new CountDownLatch(1);
- try {
+ try (PoolCleaner cleaner = cleaner(p, done)) {
+ final CountDownLatch threadStarted = new CountDownLatch(1);
assertEquals(0, p.getTaskCount());
+ assertEquals(0, p.getCompletedTaskCount());
p.execute(new CheckedRunnable() {
public void realRun() throws InterruptedException {
threadStarted.countDown();
- assertEquals(1, p.getTaskCount());
- done.await();
+ await(done);
}});
- assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
+ await(threadStarted);
assertEquals(1, p.getTaskCount());
- } finally {
- done.countDown();
- joinPool(p);
+ assertEquals(0, p.getCompletedTaskCount());
+ for (int i = 0; i < TASKS; i++) {
+ assertEquals(1 + i, p.getTaskCount());
+ p.execute(new CheckedRunnable() {
+ public void realRun() throws InterruptedException {
+ threadStarted.countDown();
+ assertEquals(1 + TASKS, p.getTaskCount());
+ await(done);
+ }});
+ }
+ assertEquals(1 + TASKS, p.getTaskCount());
+ assertEquals(0, p.getCompletedTaskCount());
}
+ assertEquals(1 + TASKS, p.getTaskCount());
+ assertEquals(1 + TASKS, p.getCompletedTaskCount());
}
/**
@@ -419,10 +442,11 @@
new ThreadPoolExecutor(1, 1,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
- assertFalse(p.isShutdown());
- try { p.shutdown(); } catch (SecurityException ok) { return; }
- assertTrue(p.isShutdown());
- joinPool(p);
+ try (PoolCleaner cleaner = cleaner(p)) {
+ assertFalse(p.isShutdown());
+ try { p.shutdown(); } catch (SecurityException ok) { return; }
+ assertTrue(p.isShutdown());
+ }
}
/**
@@ -433,26 +457,28 @@
new ThreadPoolExecutor(1, 1,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
- assertFalse(p.isTerminated());
- assertFalse(p.awaitTermination(Long.MIN_VALUE, NANOSECONDS));
- assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS));
- assertFalse(p.awaitTermination(-1L, NANOSECONDS));
- assertFalse(p.awaitTermination(-1L, MILLISECONDS));
- assertFalse(p.awaitTermination(0L, NANOSECONDS));
- assertFalse(p.awaitTermination(0L, MILLISECONDS));
- long timeoutNanos = 999999L;
- long startTime = System.nanoTime();
- assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS));
- assertTrue(System.nanoTime() - startTime >= timeoutNanos);
- assertFalse(p.isTerminated());
- startTime = System.nanoTime();
- long timeoutMillis = timeoutMillis();
- assertFalse(p.awaitTermination(timeoutMillis, MILLISECONDS));
- assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
- assertFalse(p.isTerminated());
- p.shutdown();
- assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
- assertTrue(p.isTerminated());
+ try (PoolCleaner cleaner = cleaner(p)) {
+ assertFalse(p.isTerminated());
+ assertFalse(p.awaitTermination(Long.MIN_VALUE, NANOSECONDS));
+ assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS));
+ assertFalse(p.awaitTermination(-1L, NANOSECONDS));
+ assertFalse(p.awaitTermination(-1L, MILLISECONDS));
+ assertFalse(p.awaitTermination(0L, NANOSECONDS));
+ assertFalse(p.awaitTermination(0L, MILLISECONDS));
+ long timeoutNanos = 999999L;
+ long startTime = System.nanoTime();
+ assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS));
+ assertTrue(System.nanoTime() - startTime >= timeoutNanos);
+ assertFalse(p.isTerminated());
+ startTime = System.nanoTime();
+ long timeoutMillis = timeoutMillis();
+ assertFalse(p.awaitTermination(timeoutMillis, MILLISECONDS));
+ assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
+ assertFalse(p.isTerminated());
+ try { p.shutdown(); } catch (SecurityException ok) { return; }
+ assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
+ assertTrue(p.isTerminated());
+ }
}
/**
@@ -463,24 +489,24 @@
new ThreadPoolExecutor(1, 1,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
- final CountDownLatch threadStarted = new CountDownLatch(1);
- final CountDownLatch done = new CountDownLatch(1);
- assertFalse(p.isTerminated());
- try {
+ try (PoolCleaner cleaner = cleaner(p)) {
+ final CountDownLatch threadStarted = new CountDownLatch(1);
+ final CountDownLatch done = new CountDownLatch(1);
+ assertFalse(p.isTerminating());
p.execute(new CheckedRunnable() {
public void realRun() throws InterruptedException {
- assertFalse(p.isTerminated());
+ assertFalse(p.isTerminating());
threadStarted.countDown();
- done.await();
+ await(done);
}});
- assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
+ await(threadStarted);
assertFalse(p.isTerminating());
done.countDown();
- } finally {
try { p.shutdown(); } catch (SecurityException ok) { return; }
+ assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
+ assertTrue(p.isTerminated());
+ assertFalse(p.isTerminating());
}
- assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
- assertTrue(p.isTerminated());
}
/**
@@ -491,59 +517,55 @@
new ThreadPoolExecutor(1, 1,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
- final CountDownLatch threadStarted = new CountDownLatch(1);
- final CountDownLatch done = new CountDownLatch(1);
- try {
+ try (PoolCleaner cleaner = cleaner(p)) {
+ final CountDownLatch threadStarted = new CountDownLatch(1);
+ final CountDownLatch done = new CountDownLatch(1);
assertFalse(p.isTerminating());
p.execute(new CheckedRunnable() {
public void realRun() throws InterruptedException {
assertFalse(p.isTerminating());
threadStarted.countDown();
- done.await();
+ await(done);
}});
- assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
+ await(threadStarted);
assertFalse(p.isTerminating());
done.countDown();
- } finally {
try { p.shutdown(); } catch (SecurityException ok) { return; }
+ assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
+ assertTrue(p.isTerminated());
+ assertFalse(p.isTerminating());
}
- assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
- assertTrue(p.isTerminated());
- assertFalse(p.isTerminating());
}
/**
* getQueue returns the work queue, which contains queued tasks
*/
public void testGetQueue() throws InterruptedException {
+ final CountDownLatch done = new CountDownLatch(1);
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
final ThreadPoolExecutor p =
new ThreadPoolExecutor(1, 1,
LONG_DELAY_MS, MILLISECONDS,
q);
- final CountDownLatch threadStarted = new CountDownLatch(1);
- final CountDownLatch done = new CountDownLatch(1);
- try {
+ try (PoolCleaner cleaner = cleaner(p, done)) {
+ final CountDownLatch threadStarted = new CountDownLatch(1);
FutureTask[] tasks = new FutureTask[5];
for (int i = 0; i < tasks.length; i++) {
Callable task = new CheckedCallable<Boolean>() {
public Boolean realCall() throws InterruptedException {
threadStarted.countDown();
assertSame(q, p.getQueue());
- done.await();
+ await(done);
return Boolean.TRUE;
}};
tasks[i] = new FutureTask(task);
p.execute(tasks[i]);
}
- assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
+ await(threadStarted);
assertSame(q, p.getQueue());
assertFalse(q.contains(tasks[0]));
assertTrue(q.contains(tasks[tasks.length - 1]));
assertEquals(tasks.length - 1, q.size());
- } finally {
- done.countDown();
- joinPool(p);
}
}
@@ -551,24 +573,24 @@
* remove(task) removes queued task, and fails to remove active task
*/
public void testRemove() throws InterruptedException {
+ final CountDownLatch done = new CountDownLatch(1);
BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
final ThreadPoolExecutor p =
new ThreadPoolExecutor(1, 1,
LONG_DELAY_MS, MILLISECONDS,
q);
- Runnable[] tasks = new Runnable[5];
- final CountDownLatch threadStarted = new CountDownLatch(1);
- final CountDownLatch done = new CountDownLatch(1);
- try {
+ try (PoolCleaner cleaner = cleaner(p, done)) {
+ Runnable[] tasks = new Runnable[6];
+ final CountDownLatch threadStarted = new CountDownLatch(1);
for (int i = 0; i < tasks.length; i++) {
tasks[i] = new CheckedRunnable() {
public void realRun() throws InterruptedException {
threadStarted.countDown();
- done.await();
+ await(done);
}};
p.execute(tasks[i]);
}
- assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
+ await(threadStarted);
assertFalse(p.remove(tasks[0]));
assertTrue(q.contains(tasks[4]));
assertTrue(q.contains(tasks[3]));
@@ -578,9 +600,6 @@
assertTrue(q.contains(tasks[3]));
assertTrue(p.remove(tasks[3]));
assertFalse(q.contains(tasks[3]));
- } finally {
- done.countDown();
- joinPool(p);
}
}
@@ -595,19 +614,19 @@
new ThreadPoolExecutor(1, 1,
LONG_DELAY_MS, MILLISECONDS,
q);
- FutureTask[] tasks = new FutureTask[5];
- try {
+ try (PoolCleaner cleaner = cleaner(p, done)) {
+ FutureTask[] tasks = new FutureTask[5];
for (int i = 0; i < tasks.length; i++) {
Callable task = new CheckedCallable<Boolean>() {
public Boolean realCall() throws InterruptedException {
threadStarted.countDown();
- done.await();
+ await(done);
return Boolean.TRUE;
}};
tasks[i] = new FutureTask(task);
p.execute(tasks[i]);
}
- assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
+ await(threadStarted);
assertEquals(tasks.length, p.getTaskCount());
assertEquals(tasks.length - 1, q.size());
assertEquals(1L, p.getActiveCount());
@@ -620,32 +639,47 @@
p.purge(); // Nothing to do
assertEquals(tasks.length - 3, q.size());
assertEquals(tasks.length - 2, p.getTaskCount());
- } finally {
- done.countDown();
- joinPool(p);
}
}
/**
- * shutdownNow returns a list containing tasks that were not run
+ * shutdownNow returns a list containing tasks that were not run,
+ * and those tasks are drained from the queue
*/
- public void testShutdownNow() {
+ public void testShutdownNow() throws InterruptedException {
+ final int poolSize = 2;
+ final int count = 5;
+ final AtomicInteger ran = new AtomicInteger(0);
final ThreadPoolExecutor p =
- new ThreadPoolExecutor(1, 1,
+ new ThreadPoolExecutor(poolSize, poolSize,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
- List l;
- try {
- for (int i = 0; i < 5; i++)
- p.execute(new MediumPossiblyInterruptedRunnable());
- }
- finally {
+ final CountDownLatch threadsStarted = new CountDownLatch(poolSize);
+ Runnable waiter = new CheckedRunnable() { public void realRun() {
+ threadsStarted.countDown();
try {
- l = p.shutdownNow();
- } catch (SecurityException ok) { return; }
+ MILLISECONDS.sleep(2 * LONG_DELAY_MS);
+ } catch (InterruptedException success) {}
+ ran.getAndIncrement();
+ }};
+ for (int i = 0; i < count; i++)
+ p.execute(waiter);
+ await(threadsStarted);
+ assertEquals(poolSize, p.getActiveCount());
+ assertEquals(0, p.getCompletedTaskCount());
+ final List<Runnable> queuedTasks;
+ try {
+ queuedTasks = p.shutdownNow();
+ } catch (SecurityException ok) {
+ return; // Allowed in case test doesn't have privs
}
assertTrue(p.isShutdown());
- assertTrue(l.size() <= 4);
+ assertTrue(p.getQueue().isEmpty());
+ assertEquals(count - poolSize, queuedTasks.size());
+ assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
+ assertTrue(p.isTerminated());
+ assertEquals(poolSize, ran.get());
+ assertEquals(poolSize, p.getCompletedTaskCount());
}
// Exception Tests
@@ -655,8 +689,7 @@
*/
public void testConstructor1() {
try {
- new ThreadPoolExecutor(-1, 1,
- LONG_DELAY_MS, MILLISECONDS,
+ new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
new ArrayBlockingQueue<Runnable>(10));
shouldThrow();
} catch (IllegalArgumentException success) {}
@@ -667,8 +700,7 @@
*/
public void testConstructor2() {
try {
- new ThreadPoolExecutor(1, -1,
- LONG_DELAY_MS, MILLISECONDS,
+ new ThreadPoolExecutor(1, -1, 1L, SECONDS,
new ArrayBlockingQueue<Runnable>(10));
shouldThrow();
} catch (IllegalArgumentException success) {}
@@ -679,8 +711,7 @@
*/
public void testConstructor3() {
try {
- new ThreadPoolExecutor(1, 0,
- LONG_DELAY_MS, MILLISECONDS,
+ new ThreadPoolExecutor(1, 0, 1L, SECONDS,
new ArrayBlockingQueue<Runnable>(10));
shouldThrow();
} catch (IllegalArgumentException success) {}
@@ -691,8 +722,7 @@
*/
public void testConstructor4() {
try {
- new ThreadPoolExecutor(1, 2,
- -1L, MILLISECONDS,
+ new ThreadPoolExecutor(1, 2, -1L, SECONDS,
new ArrayBlockingQueue<Runnable>(10));
shouldThrow();
} catch (IllegalArgumentException success) {}
@@ -703,8 +733,7 @@
*/
public void testConstructor5() {
try {
- new ThreadPoolExecutor(2, 1,
- LONG_DELAY_MS, MILLISECONDS,
+ new ThreadPoolExecutor(2, 1, 1L, SECONDS,
new ArrayBlockingQueue<Runnable>(10));
shouldThrow();
} catch (IllegalArgumentException success) {}
@@ -715,8 +744,7 @@
*/
public void testConstructorNullPointerException() {
try {
- new ThreadPoolExecutor(1, 2,
- LONG_DELAY_MS, MILLISECONDS,
+ new ThreadPoolExecutor(1, 2, 1L, SECONDS,
(BlockingQueue) null);
shouldThrow();
} catch (NullPointerException success) {}
@@ -727,8 +755,7 @@
*/
public void testConstructor6() {
try {
- new ThreadPoolExecutor(-1, 1,
- LONG_DELAY_MS, MILLISECONDS,
+ new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
new ArrayBlockingQueue<Runnable>(10),
new SimpleThreadFactory());
shouldThrow();
@@ -740,8 +767,7 @@
*/
public void testConstructor7() {
try {
- new ThreadPoolExecutor(1, -1,
- LONG_DELAY_MS, MILLISECONDS,
+ new ThreadPoolExecutor(1, -1, 1L, SECONDS,
new ArrayBlockingQueue<Runnable>(10),
new SimpleThreadFactory());
shouldThrow();
@@ -753,8 +779,7 @@
*/
public void testConstructor8() {
try {
- new ThreadPoolExecutor(1, 0,
- LONG_DELAY_MS, MILLISECONDS,
+ new ThreadPoolExecutor(1, 0, 1L, SECONDS,
new ArrayBlockingQueue<Runnable>(10),
new SimpleThreadFactory());
shouldThrow();
@@ -766,8 +791,7 @@
*/
public void testConstructor9() {
try {
- new ThreadPoolExecutor(1, 2,
- -1L, MILLISECONDS,
+ new ThreadPoolExecutor(1, 2, -1L, SECONDS,
new ArrayBlockingQueue<Runnable>(10),
new SimpleThreadFactory());
shouldThrow();
@@ -779,8 +803,7 @@
*/
public void testConstructor10() {
try {
- new ThreadPoolExecutor(2, 1,
- LONG_DELAY_MS, MILLISECONDS,
+ new ThreadPoolExecutor(2, 1, 1L, SECONDS,
new ArrayBlockingQueue<Runnable>(10),
new SimpleThreadFactory());
shouldThrow();
@@ -792,8 +815,7 @@
*/
public void testConstructorNullPointerException2() {
try {
- new ThreadPoolExecutor(1, 2,
- LONG_DELAY_MS, MILLISECONDS,
+ new ThreadPoolExecutor(1, 2, 1L, SECONDS,
(BlockingQueue) null,
new SimpleThreadFactory());
shouldThrow();
@@ -805,8 +827,7 @@
*/
public void testConstructorNullPointerException3() {
try {
- new ThreadPoolExecutor(1, 2,
- LONG_DELAY_MS, MILLISECONDS,
+ new ThreadPoolExecutor(1, 2, 1L, SECONDS,
new ArrayBlockingQueue<Runnable>(10),
(ThreadFactory) null);
shouldThrow();
@@ -818,8 +839,7 @@
*/
public void testConstructor11() {
try {
- new ThreadPoolExecutor(-1, 1,
- LONG_DELAY_MS, MILLISECONDS,
+ new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
new ArrayBlockingQueue<Runnable>(10),
new NoOpREHandler());
shouldThrow();
@@ -831,8 +851,7 @@
*/
public void testConstructor12() {
try {
- new ThreadPoolExecutor(1, -1,
- LONG_DELAY_MS, MILLISECONDS,
+ new ThreadPoolExecutor(1, -1, 1L, SECONDS,
new ArrayBlockingQueue<Runnable>(10),
new NoOpREHandler());
shouldThrow();
@@ -844,8 +863,7 @@
*/
public void testConstructor13() {
try {
- new ThreadPoolExecutor(1, 0,
- LONG_DELAY_MS, MILLISECONDS,
+ new ThreadPoolExecutor(1, 0, 1L, SECONDS,
new ArrayBlockingQueue<Runnable>(10),
new NoOpREHandler());
shouldThrow();
@@ -857,8 +875,7 @@
*/
public void testConstructor14() {
try {
- new ThreadPoolExecutor(1, 2,
- -1L, MILLISECONDS,
+ new ThreadPoolExecutor(1, 2, -1L, SECONDS,
new ArrayBlockingQueue<Runnable>(10),
new NoOpREHandler());
shouldThrow();
@@ -870,8 +887,7 @@
*/
public void testConstructor15() {
try {
- new ThreadPoolExecutor(2, 1,
- LONG_DELAY_MS, MILLISECONDS,
+ new ThreadPoolExecutor(2, 1, 1L, SECONDS,
new ArrayBlockingQueue<Runnable>(10),
new NoOpREHandler());
shouldThrow();
@@ -883,8 +899,7 @@
*/
public void testConstructorNullPointerException4() {
try {
- new ThreadPoolExecutor(1, 2,
- LONG_DELAY_MS, MILLISECONDS,
+ new ThreadPoolExecutor(1, 2, 1L, SECONDS,
(BlockingQueue) null,
new NoOpREHandler());
shouldThrow();
@@ -896,8 +911,7 @@
*/
public void testConstructorNullPointerException5() {
try {
- new ThreadPoolExecutor(1, 2,
- LONG_DELAY_MS, MILLISECONDS,
+ new ThreadPoolExecutor(1, 2, 1L, SECONDS,
new ArrayBlockingQueue<Runnable>(10),
(RejectedExecutionHandler) null);
shouldThrow();
@@ -909,8 +923,7 @@
*/
public void testConstructor16() {
try {
- new ThreadPoolExecutor(-1, 1,
- LONG_DELAY_MS, MILLISECONDS,
+ new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
new ArrayBlockingQueue<Runnable>(10),
new SimpleThreadFactory(),
new NoOpREHandler());
@@ -923,8 +936,7 @@
*/
public void testConstructor17() {
try {
- new ThreadPoolExecutor(1, -1,
- LONG_DELAY_MS, MILLISECONDS,
+ new ThreadPoolExecutor(1, -1, 1L, SECONDS,
new ArrayBlockingQueue<Runnable>(10),
new SimpleThreadFactory(),
new NoOpREHandler());
@@ -937,8 +949,7 @@
*/
public void testConstructor18() {
try {
- new ThreadPoolExecutor(1, 0,
- LONG_DELAY_MS, MILLISECONDS,
+ new ThreadPoolExecutor(1, 0, 1L, SECONDS,
new ArrayBlockingQueue<Runnable>(10),
new SimpleThreadFactory(),
new NoOpREHandler());
@@ -951,8 +962,7 @@
*/
public void testConstructor19() {
try {
- new ThreadPoolExecutor(1, 2,
- -1L, MILLISECONDS,
+ new ThreadPoolExecutor(1, 2, -1L, SECONDS,
new ArrayBlockingQueue<Runnable>(10),
new SimpleThreadFactory(),
new NoOpREHandler());
@@ -965,8 +975,7 @@
*/
public void testConstructor20() {
try {
- new ThreadPoolExecutor(2, 1,
- LONG_DELAY_MS, MILLISECONDS,
+ new ThreadPoolExecutor(2, 1, 1L, SECONDS,
new ArrayBlockingQueue<Runnable>(10),
new SimpleThreadFactory(),
new NoOpREHandler());
@@ -979,8 +988,7 @@
*/
public void testConstructorNullPointerException6() {
try {
- new ThreadPoolExecutor(1, 2,
- LONG_DELAY_MS, MILLISECONDS,
+ new ThreadPoolExecutor(1, 2, 1L, SECONDS,
(BlockingQueue) null,
new SimpleThreadFactory(),
new NoOpREHandler());
@@ -993,8 +1001,7 @@
*/
public void testConstructorNullPointerException7() {
try {
- new ThreadPoolExecutor(1, 2,
- LONG_DELAY_MS, MILLISECONDS,
+ new ThreadPoolExecutor(1, 2, 1L, SECONDS,
new ArrayBlockingQueue<Runnable>(10),
new SimpleThreadFactory(),
(RejectedExecutionHandler) null);
@@ -1007,8 +1014,7 @@
*/
public void testConstructorNullPointerException8() {
try {
- new ThreadPoolExecutor(1, 2,
- LONG_DELAY_MS, MILLISECONDS,
+ new ThreadPoolExecutor(1, 2, 1L, SECONDS,
new ArrayBlockingQueue<Runnable>(10),
(ThreadFactory) null,
new NoOpREHandler());
@@ -1020,31 +1026,28 @@
* get of submitted callable throws InterruptedException if interrupted
*/
public void testInterruptedSubmit() throws InterruptedException {
+ final CountDownLatch done = new CountDownLatch(1);
final ThreadPoolExecutor p =
new ThreadPoolExecutor(1, 1,
- 60, TimeUnit.SECONDS,
+ 60, SECONDS,
new ArrayBlockingQueue<Runnable>(10));
- final CountDownLatch threadStarted = new CountDownLatch(1);
- final CountDownLatch done = new CountDownLatch(1);
- try {
+ try (PoolCleaner cleaner = cleaner(p, done)) {
+ final CountDownLatch threadStarted = new CountDownLatch(1);
Thread t = newStartedThread(new CheckedInterruptedRunnable() {
public void realRun() throws Exception {
Callable task = new CheckedCallable<Boolean>() {
public Boolean realCall() throws InterruptedException {
threadStarted.countDown();
- done.await();
+ await(done);
return Boolean.TRUE;
}};
p.submit(task).get();
}});
- assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
+ await(threadStarted);
t.interrupt();
- awaitTermination(t, MEDIUM_DELAY_MS);
- } finally {
- done.countDown();
- joinPool(p);
+ awaitTermination(t);
}
}
@@ -1052,15 +1055,15 @@
* execute throws RejectedExecutionException if saturated.
*/
public void testSaturatedExecute() {
- ThreadPoolExecutor p =
+ final CountDownLatch done = new CountDownLatch(1);
+ final ThreadPoolExecutor p =
new ThreadPoolExecutor(1, 1,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(1));
- final CountDownLatch done = new CountDownLatch(1);
- try {
+ try (PoolCleaner cleaner = cleaner(p, done)) {
Runnable task = new CheckedRunnable() {
public void realRun() throws InterruptedException {
- done.await();
+ await(done);
}};
for (int i = 0; i < 2; ++i)
p.execute(task);
@@ -1071,9 +1074,6 @@
} catch (RejectedExecutionException success) {}
assertTrue(p.getTaskCount() <= 2);
}
- } finally {
- done.countDown();
- joinPool(p);
}
}
@@ -1081,15 +1081,15 @@
* submit(runnable) throws RejectedExecutionException if saturated.
*/
public void testSaturatedSubmitRunnable() {
- ThreadPoolExecutor p =
+ final CountDownLatch done = new CountDownLatch(1);
+ final ThreadPoolExecutor p =
new ThreadPoolExecutor(1, 1,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(1));
- final CountDownLatch done = new CountDownLatch(1);
- try {
+ try (PoolCleaner cleaner = cleaner(p, done)) {
Runnable task = new CheckedRunnable() {
public void realRun() throws InterruptedException {
- done.await();
+ await(done);
}};
for (int i = 0; i < 2; ++i)
p.submit(task);
@@ -1100,9 +1100,6 @@
} catch (RejectedExecutionException success) {}
assertTrue(p.getTaskCount() <= 2);
}
- } finally {
- done.countDown();
- joinPool(p);
}
}
@@ -1110,15 +1107,15 @@
* submit(callable) throws RejectedExecutionException if saturated.
*/
public void testSaturatedSubmitCallable() {
- ThreadPoolExecutor p =
+ final CountDownLatch done = new CountDownLatch(1);
+ final ThreadPoolExecutor p =
new ThreadPoolExecutor(1, 1,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(1));
- final CountDownLatch done = new CountDownLatch(1);
- try {
+ try (PoolCleaner cleaner = cleaner(p, done)) {
Runnable task = new CheckedRunnable() {
public void realRun() throws InterruptedException {
- done.await();
+ await(done);
}};
for (int i = 0; i < 2; ++i)
p.submit(Executors.callable(task));
@@ -1129,9 +1126,6 @@
} catch (RejectedExecutionException success) {}
assertTrue(p.getTaskCount() <= 2);
}
- } finally {
- done.countDown();
- joinPool(p);
}
}
@@ -1139,26 +1133,28 @@
* executor using CallerRunsPolicy runs task if saturated.
*/
public void testSaturatedExecute2() {
- RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy();
final ThreadPoolExecutor p =
new ThreadPoolExecutor(1, 1,
LONG_DELAY_MS,
MILLISECONDS,
new ArrayBlockingQueue<Runnable>(1),
- h);
- try {
+ new ThreadPoolExecutor.CallerRunsPolicy());
+ try (PoolCleaner cleaner = cleaner(p)) {
+ final CountDownLatch done = new CountDownLatch(1);
+ Runnable blocker = new CheckedRunnable() {
+ public void realRun() throws InterruptedException {
+ await(done);
+ }};
+ p.execute(blocker);
TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
- for (int i = 0; i < tasks.length; ++i)
+ for (int i = 0; i < tasks.length; i++)
tasks[i] = new TrackedNoOpRunnable();
- TrackedLongRunnable mr = new TrackedLongRunnable();
- p.execute(mr);
- for (int i = 0; i < tasks.length; ++i)
+ for (int i = 0; i < tasks.length; i++)
p.execute(tasks[i]);
- for (int i = 1; i < tasks.length; ++i)
+ for (int i = 1; i < tasks.length; i++)
assertTrue(tasks[i].done);
- try { p.shutdownNow(); } catch (SecurityException ok) { return; }
- } finally {
- joinPool(p);
+ assertFalse(tasks[0].done); // waiting in queue
+ done.countDown();
}
}
@@ -1166,67 +1162,72 @@
* executor using DiscardPolicy drops task if saturated.
*/
public void testSaturatedExecute3() {
- RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy();
+ final CountDownLatch done = new CountDownLatch(1);
+ final TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
+ for (int i = 0; i < tasks.length; ++i)
+ tasks[i] = new TrackedNoOpRunnable();
final ThreadPoolExecutor p =
new ThreadPoolExecutor(1, 1,
- LONG_DELAY_MS, MILLISECONDS,
- new ArrayBlockingQueue<Runnable>(1),
- h);
- try {
- TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
- for (int i = 0; i < tasks.length; ++i)
- tasks[i] = new TrackedNoOpRunnable();
- p.execute(new TrackedLongRunnable());
+ LONG_DELAY_MS, MILLISECONDS,
+ new ArrayBlockingQueue<Runnable>(1),
+ new ThreadPoolExecutor.DiscardPolicy());
+ try (PoolCleaner cleaner = cleaner(p, done)) {
+ p.execute(awaiter(done));
+
for (TrackedNoOpRunnable task : tasks)
p.execute(task);
- for (TrackedNoOpRunnable task : tasks)
- assertFalse(task.done);
- try { p.shutdownNow(); } catch (SecurityException ok) { return; }
- } finally {
- joinPool(p);
+ for (int i = 1; i < tasks.length; i++)
+ assertFalse(tasks[i].done);
}
+ for (int i = 1; i < tasks.length; i++)
+ assertFalse(tasks[i].done);
+ assertTrue(tasks[0].done); // was waiting in queue
}
/**
* executor using DiscardOldestPolicy drops oldest task if saturated.
*/
public void testSaturatedExecute4() {
- RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy();
+ final CountDownLatch done = new CountDownLatch(1);
+ LatchAwaiter r1 = awaiter(done);
+ LatchAwaiter r2 = awaiter(done);
+ LatchAwaiter r3 = awaiter(done);
final ThreadPoolExecutor p =
new ThreadPoolExecutor(1, 1,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(1),
- h);
- try {
- p.execute(new TrackedLongRunnable());
- TrackedLongRunnable r2 = new TrackedLongRunnable();
+ new ThreadPoolExecutor.DiscardOldestPolicy());
+ try (PoolCleaner cleaner = cleaner(p, done)) {
+ assertEquals(LatchAwaiter.NEW, r1.state);
+ assertEquals(LatchAwaiter.NEW, r2.state);
+ assertEquals(LatchAwaiter.NEW, r3.state);
+ p.execute(r1);
p.execute(r2);
assertTrue(p.getQueue().contains(r2));
- TrackedNoOpRunnable r3 = new TrackedNoOpRunnable();
p.execute(r3);
assertFalse(p.getQueue().contains(r2));
assertTrue(p.getQueue().contains(r3));
- try { p.shutdownNow(); } catch (SecurityException ok) { return; }
- } finally {
- joinPool(p);
}
+ assertEquals(LatchAwaiter.DONE, r1.state);
+ assertEquals(LatchAwaiter.NEW, r2.state);
+ assertEquals(LatchAwaiter.DONE, r3.state);
}
/**
* execute throws RejectedExecutionException if shutdown
*/
public void testRejectedExecutionExceptionOnShutdown() {
- ThreadPoolExecutor p =
+ final ThreadPoolExecutor p =
new ThreadPoolExecutor(1, 1,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(1));
try { p.shutdown(); } catch (SecurityException ok) { return; }
- try {
- p.execute(new NoOpRunnable());
- shouldThrow();
- } catch (RejectedExecutionException success) {}
-
- joinPool(p);
+ try (PoolCleaner cleaner = cleaner(p)) {
+ try {
+ p.execute(new NoOpRunnable());
+ shouldThrow();
+ } catch (RejectedExecutionException success) {}
+ }
}
/**
@@ -1240,12 +1241,10 @@
new ArrayBlockingQueue<Runnable>(1), h);
try { p.shutdown(); } catch (SecurityException ok) { return; }
- try {
+ try (PoolCleaner cleaner = cleaner(p)) {
TrackedNoOpRunnable r = new TrackedNoOpRunnable();
p.execute(r);
assertFalse(r.done);
- } finally {
- joinPool(p);
}
}
@@ -1253,20 +1252,17 @@
* execute using DiscardPolicy drops task on shutdown
*/
public void testDiscardOnShutdown() {
- RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy();
- ThreadPoolExecutor p =
+ final ThreadPoolExecutor p =
new ThreadPoolExecutor(1, 1,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(1),
- h);
+ new ThreadPoolExecutor.DiscardPolicy());
try { p.shutdown(); } catch (SecurityException ok) { return; }
- try {
+ try (PoolCleaner cleaner = cleaner(p)) {
TrackedNoOpRunnable r = new TrackedNoOpRunnable();
p.execute(r);
assertFalse(r.done);
- } finally {
- joinPool(p);
}
}
@@ -1274,20 +1270,17 @@
* execute using DiscardOldestPolicy drops task on shutdown
*/
public void testDiscardOldestOnShutdown() {
- RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy();
- ThreadPoolExecutor p =
+ final ThreadPoolExecutor p =
new ThreadPoolExecutor(1, 1,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(1),
- h);
+ new ThreadPoolExecutor.DiscardOldestPolicy());
try { p.shutdown(); } catch (SecurityException ok) { return; }
- try {
+ try (PoolCleaner cleaner = cleaner(p)) {
TrackedNoOpRunnable r = new TrackedNoOpRunnable();
p.execute(r);
assertFalse(r.done);
- } finally {
- joinPool(p);
}
}
@@ -1295,34 +1288,32 @@
* execute(null) throws NPE
*/
public void testExecuteNull() {
- ThreadPoolExecutor p =
+ final ThreadPoolExecutor p =
new ThreadPoolExecutor(1, 2,
- LONG_DELAY_MS, MILLISECONDS,
+ 1L, SECONDS,
new ArrayBlockingQueue<Runnable>(10));
- try {
- p.execute(null);
- shouldThrow();
- } catch (NullPointerException success) {}
-
- joinPool(p);
+ try (PoolCleaner cleaner = cleaner(p)) {
+ try {
+ p.execute(null);
+ shouldThrow();
+ } catch (NullPointerException success) {}
+ }
}
/**
* setCorePoolSize of negative value throws IllegalArgumentException
*/
public void testCorePoolSizeIllegalArgumentException() {
- ThreadPoolExecutor p =
+ final ThreadPoolExecutor p =
new ThreadPoolExecutor(1, 2,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
- try {
- p.setCorePoolSize(-1);
- shouldThrow();
- } catch (IllegalArgumentException success) {
- } finally {
- try { p.shutdown(); } catch (SecurityException ok) { return; }
+ try (PoolCleaner cleaner = cleaner(p)) {
+ try {
+ p.setCorePoolSize(-1);
+ shouldThrow();
+ } catch (IllegalArgumentException success) {}
}
- joinPool(p);
}
/**
@@ -1330,18 +1321,16 @@
* given a value less the core pool size
*/
public void testMaximumPoolSizeIllegalArgumentException() {
- ThreadPoolExecutor p =
+ final ThreadPoolExecutor p =
new ThreadPoolExecutor(2, 3,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
- try {
- p.setMaximumPoolSize(1);
- shouldThrow();
- } catch (IllegalArgumentException success) {
- } finally {
- try { p.shutdown(); } catch (SecurityException ok) { return; }
+ try (PoolCleaner cleaner = cleaner(p)) {
+ try {
+ p.setMaximumPoolSize(1);
+ shouldThrow();
+ } catch (IllegalArgumentException success) {}
}
- joinPool(p);
}
/**
@@ -1349,18 +1338,45 @@
* if given a negative value
*/
public void testMaximumPoolSizeIllegalArgumentException2() {
- ThreadPoolExecutor p =
+ final ThreadPoolExecutor p =
new ThreadPoolExecutor(2, 3,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
- try {
- p.setMaximumPoolSize(-1);
- shouldThrow();
- } catch (IllegalArgumentException success) {
- } finally {
- try { p.shutdown(); } catch (SecurityException ok) { return; }
+ try (PoolCleaner cleaner = cleaner(p)) {
+ try {
+ p.setMaximumPoolSize(-1);
+ shouldThrow();
+ } catch (IllegalArgumentException success) {}
}
- joinPool(p);
+ }
+
+ /**
+ * Configuration changes that allow core pool size greater than
+ * max pool size result in IllegalArgumentException.
+ */
+ public void testPoolSizeInvariants() {
+ final ThreadPoolExecutor p =
+ new ThreadPoolExecutor(1, 1,
+ LONG_DELAY_MS, MILLISECONDS,
+ new ArrayBlockingQueue<Runnable>(10));
+ try (PoolCleaner cleaner = cleaner(p)) {
+ for (int s = 1; s < 5; s++) {
+ p.setMaximumPoolSize(s);
+ p.setCorePoolSize(s);
+ try {
+ p.setMaximumPoolSize(s - 1);
+ shouldThrow();
+ } catch (IllegalArgumentException success) {}
+ assertEquals(s, p.getCorePoolSize());
+ assertEquals(s, p.getMaximumPoolSize());
+ try {
+ p.setCorePoolSize(s + 1);
+ shouldThrow();
+ } catch (IllegalArgumentException success) {}
+ assertEquals(s, p.getCorePoolSize());
+ assertEquals(s, p.getMaximumPoolSize());
+ }
+ }
}
/**
@@ -1368,18 +1384,16 @@
* when given a negative value
*/
public void testKeepAliveTimeIllegalArgumentException() {
- ThreadPoolExecutor p =
+ final ThreadPoolExecutor p =
new ThreadPoolExecutor(2, 3,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
- try {
- p.setKeepAliveTime(-1,MILLISECONDS);
- shouldThrow();
- } catch (IllegalArgumentException success) {
- } finally {
- try { p.shutdown(); } catch (SecurityException ok) { return; }
+ try (PoolCleaner cleaner = cleaner(p)) {
+ try {
+ p.setKeepAliveTime(-1, MILLISECONDS);
+ shouldThrow();
+ } catch (IllegalArgumentException success) {}
}
- joinPool(p);
}
/**
@@ -1387,9 +1401,11 @@
*/
public void testTerminated() {
ExtendedTPE p = new ExtendedTPE();
- try { p.shutdown(); } catch (SecurityException ok) { return; }
- assertTrue(p.terminatedCalled());
- joinPool(p);
+ try (PoolCleaner cleaner = cleaner(p)) {
+ try { p.shutdown(); } catch (SecurityException ok) { return; }
+ assertTrue(p.terminatedCalled());
+ assertTrue(p.isShutdown());
+ }
}
/**
@@ -1397,7 +1413,7 @@
*/
public void testBeforeAfter() throws InterruptedException {
ExtendedTPE p = new ExtendedTPE();
- try {
+ try (PoolCleaner cleaner = cleaner(p)) {
final CountDownLatch done = new CountDownLatch(1);
p.execute(new CheckedRunnable() {
public void realRun() {
@@ -1407,9 +1423,6 @@
assertEquals(0, done.getCount());
assertTrue(p.afterCalled());
assertTrue(p.beforeCalled());
- try { p.shutdown(); } catch (SecurityException ok) { return; }
- } finally {
- joinPool(p);
}
}
@@ -1417,16 +1430,14 @@
* completed submit of callable returns result
*/
public void testSubmitCallable() throws Exception {
- ExecutorService e =
+ final ExecutorService e =
new ThreadPoolExecutor(2, 2,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
- try {
+ try (PoolCleaner cleaner = cleaner(e)) {
Future<String> future = e.submit(new StringTask());
String result = future.get();
assertSame(TEST_STRING, result);
- } finally {
- joinPool(e);
}
}
@@ -1434,16 +1445,14 @@
* completed submit of runnable returns successfully
*/
public void testSubmitRunnable() throws Exception {
- ExecutorService e =
+ final ExecutorService e =
new ThreadPoolExecutor(2, 2,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
- try {
+ try (PoolCleaner cleaner = cleaner(e)) {
Future<?> future = e.submit(new NoOpRunnable());
future.get();
assertTrue(future.isDone());
- } finally {
- joinPool(e);
}
}
@@ -1451,16 +1460,14 @@
* completed submit of (runnable, result) returns result
*/
public void testSubmitRunnable2() throws Exception {
- ExecutorService e =
+ final ExecutorService e =
new ThreadPoolExecutor(2, 2,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
- try {
+ try (PoolCleaner cleaner = cleaner(e)) {
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
String result = future.get();
assertSame(TEST_STRING, result);
- } finally {
- joinPool(e);
}
}
@@ -1468,16 +1475,15 @@
* invokeAny(null) throws NPE
*/
public void testInvokeAny1() throws Exception {
- ExecutorService e =
+ final ExecutorService e =
new ThreadPoolExecutor(2, 2,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
- try {
- e.invokeAny(null);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
- joinPool(e);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ try {
+ e.invokeAny(null);
+ shouldThrow();
+ } catch (NullPointerException success) {}
}
}
@@ -1485,16 +1491,15 @@
* invokeAny(empty collection) throws IAE
*/
public void testInvokeAny2() throws Exception {
- ExecutorService e =
+ final ExecutorService e =
new ThreadPoolExecutor(2, 2,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
- try {
- e.invokeAny(new ArrayList<Callable<String>>());
- shouldThrow();
- } catch (IllegalArgumentException success) {
- } finally {
- joinPool(e);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ try {
+ e.invokeAny(new ArrayList<Callable<String>>());
+ shouldThrow();
+ } catch (IllegalArgumentException success) {}
}
}
@@ -1507,16 +1512,15 @@
new ThreadPoolExecutor(2, 2,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
- List<Callable<String>> l = new ArrayList<Callable<String>>();
- l.add(latchAwaitingStringTask(latch));
- l.add(null);
- try {
- e.invokeAny(l);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
+ try (PoolCleaner cleaner = cleaner(e)) {
+ List<Callable<String>> l = new ArrayList<Callable<String>>();
+ l.add(latchAwaitingStringTask(latch));
+ l.add(null);
+ try {
+ e.invokeAny(l);
+ shouldThrow();
+ } catch (NullPointerException success) {}
latch.countDown();
- joinPool(e);
}
}
@@ -1524,19 +1528,19 @@
* invokeAny(c) throws ExecutionException if no task completes
*/
public void testInvokeAny4() throws Exception {
- ExecutorService e =
+ final ExecutorService e =
new ThreadPoolExecutor(2, 2,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
- List<Callable<String>> l = new ArrayList<Callable<String>>();
- l.add(new NPETask());
- try {
- e.invokeAny(l);
- shouldThrow();
- } catch (ExecutionException success) {
- assertTrue(success.getCause() instanceof NullPointerException);
- } finally {
- joinPool(e);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ List<Callable<String>> l = new ArrayList<Callable<String>>();
+ l.add(new NPETask());
+ try {
+ e.invokeAny(l);
+ shouldThrow();
+ } catch (ExecutionException success) {
+ assertTrue(success.getCause() instanceof NullPointerException);
+ }
}
}
@@ -1544,18 +1548,16 @@
* invokeAny(c) returns result of some task
*/
public void testInvokeAny5() throws Exception {
- ExecutorService e =
+ final ExecutorService e =
new ThreadPoolExecutor(2, 2,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
- try {
+ try (PoolCleaner cleaner = cleaner(e)) {
List<Callable<String>> l = new ArrayList<Callable<String>>();
l.add(new StringTask());
l.add(new StringTask());
String result = e.invokeAny(l);
assertSame(TEST_STRING, result);
- } finally {
- joinPool(e);
}
}
@@ -1563,16 +1565,15 @@
* invokeAll(null) throws NPE
*/
public void testInvokeAll1() throws Exception {
- ExecutorService e =
+ final ExecutorService e =
new ThreadPoolExecutor(2, 2,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
- try {
- e.invokeAll(null);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
- joinPool(e);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ try {
+ e.invokeAll(null);
+ shouldThrow();
+ } catch (NullPointerException success) {}
}
}
@@ -1580,15 +1581,13 @@
* invokeAll(empty collection) returns empty collection
*/
public void testInvokeAll2() throws InterruptedException {
- ExecutorService e =
+ final ExecutorService e =
new ThreadPoolExecutor(2, 2,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
- try {
+ try (PoolCleaner cleaner = cleaner(e)) {
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
assertTrue(r.isEmpty());
- } finally {
- joinPool(e);
}
}
@@ -1596,19 +1595,18 @@
* invokeAll(c) throws NPE if c has null elements
*/
public void testInvokeAll3() throws Exception {
- ExecutorService e =
+ final ExecutorService e =
new ThreadPoolExecutor(2, 2,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
- List<Callable<String>> l = new ArrayList<Callable<String>>();
- l.add(new StringTask());
- l.add(null);
- try {
- e.invokeAll(l);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
- joinPool(e);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ List<Callable<String>> l = new ArrayList<Callable<String>>();
+ l.add(new StringTask());
+ l.add(null);
+ try {
+ e.invokeAll(l);
+ shouldThrow();
+ } catch (NullPointerException success) {}
}
}
@@ -1616,11 +1614,11 @@
* get of element of invokeAll(c) throws exception on failed task
*/
public void testInvokeAll4() throws Exception {
- ExecutorService e =
+ final ExecutorService e =
new ThreadPoolExecutor(2, 2,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
- try {
+ try (PoolCleaner cleaner = cleaner(e)) {
List<Callable<String>> l = new ArrayList<Callable<String>>();
l.add(new NPETask());
List<Future<String>> futures = e.invokeAll(l);
@@ -1631,8 +1629,6 @@
} catch (ExecutionException success) {
assertTrue(success.getCause() instanceof NullPointerException);
}
- } finally {
- joinPool(e);
}
}
@@ -1640,11 +1636,11 @@
* invokeAll(c) returns results of all completed tasks
*/
public void testInvokeAll5() throws Exception {
- ExecutorService e =
+ final ExecutorService e =
new ThreadPoolExecutor(2, 2,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
- try {
+ try (PoolCleaner cleaner = cleaner(e)) {
List<Callable<String>> l = new ArrayList<Callable<String>>();
l.add(new StringTask());
l.add(new StringTask());
@@ -1652,8 +1648,6 @@
assertEquals(2, futures.size());
for (Future<String> future : futures)
assertSame(TEST_STRING, future.get());
- } finally {
- joinPool(e);
}
}
@@ -1661,16 +1655,15 @@
* timed invokeAny(null) throws NPE
*/
public void testTimedInvokeAny1() throws Exception {
- ExecutorService e =
+ final ExecutorService e =
new ThreadPoolExecutor(2, 2,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
- try {
- e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
- joinPool(e);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ try {
+ e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
+ shouldThrow();
+ } catch (NullPointerException success) {}
}
}
@@ -1678,18 +1671,17 @@
* timed invokeAny(,,null) throws NPE
*/
public void testTimedInvokeAnyNullTimeUnit() throws Exception {
- ExecutorService e =
+ final ExecutorService e =
new ThreadPoolExecutor(2, 2,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
- List<Callable<String>> l = new ArrayList<Callable<String>>();
- l.add(new StringTask());
- try {
- e.invokeAny(l, MEDIUM_DELAY_MS, null);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
- joinPool(e);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ List<Callable<String>> l = new ArrayList<Callable<String>>();
+ l.add(new StringTask());
+ try {
+ e.invokeAny(l, MEDIUM_DELAY_MS, null);
+ shouldThrow();
+ } catch (NullPointerException success) {}
}
}
@@ -1697,16 +1689,16 @@
* timed invokeAny(empty collection) throws IAE
*/
public void testTimedInvokeAny2() throws Exception {
- ExecutorService e =
+ final ExecutorService e =
new ThreadPoolExecutor(2, 2,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
- try {
- e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
- shouldThrow();
- } catch (IllegalArgumentException success) {
- } finally {
- joinPool(e);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ try {
+ e.invokeAny(new ArrayList<Callable<String>>(),
+ MEDIUM_DELAY_MS, MILLISECONDS);
+ shouldThrow();
+ } catch (IllegalArgumentException success) {}
}
}
@@ -1719,16 +1711,15 @@
new ThreadPoolExecutor(2, 2,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
- List<Callable<String>> l = new ArrayList<Callable<String>>();
- l.add(latchAwaitingStringTask(latch));
- l.add(null);
- try {
- e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
+ try (PoolCleaner cleaner = cleaner(e)) {
+ List<Callable<String>> l = new ArrayList<Callable<String>>();
+ l.add(latchAwaitingStringTask(latch));
+ l.add(null);
+ try {
+ e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
+ shouldThrow();
+ } catch (NullPointerException success) {}
latch.countDown();
- joinPool(e);
}
}
@@ -1736,19 +1727,21 @@
* timed invokeAny(c) throws ExecutionException if no task completes
*/
public void testTimedInvokeAny4() throws Exception {
- ExecutorService e =
+ final ExecutorService e =
new ThreadPoolExecutor(2, 2,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
- List<Callable<String>> l = new ArrayList<Callable<String>>();
- l.add(new NPETask());
- try {
- e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
- shouldThrow();
- } catch (ExecutionException success) {
- assertTrue(success.getCause() instanceof NullPointerException);
- } finally {
- joinPool(e);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ long startTime = System.nanoTime();
+ List<Callable<String>> l = new ArrayList<Callable<String>>();
+ l.add(new NPETask());
+ try {
+ e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
+ shouldThrow();
+ } catch (ExecutionException success) {
+ assertTrue(success.getCause() instanceof NullPointerException);
+ }
+ assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
}
}
@@ -1756,18 +1749,18 @@
* timed invokeAny(c) returns result of some task
*/
public void testTimedInvokeAny5() throws Exception {
- ExecutorService e =
+ final ExecutorService e =
new ThreadPoolExecutor(2, 2,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
- try {
+ try (PoolCleaner cleaner = cleaner(e)) {
+ long startTime = System.nanoTime();
List<Callable<String>> l = new ArrayList<Callable<String>>();
l.add(new StringTask());
l.add(new StringTask());
- String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
+ String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
assertSame(TEST_STRING, result);
- } finally {
- joinPool(e);
+ assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
}
}
@@ -1775,16 +1768,15 @@
* timed invokeAll(null) throws NPE
*/
public void testTimedInvokeAll1() throws Exception {
- ExecutorService e =
+ final ExecutorService e =
new ThreadPoolExecutor(2, 2,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
- try {
- e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
- joinPool(e);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ try {
+ e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
+ shouldThrow();
+ } catch (NullPointerException success) {}
}
}
@@ -1792,18 +1784,17 @@
* timed invokeAll(,,null) throws NPE
*/
public void testTimedInvokeAllNullTimeUnit() throws Exception {
- ExecutorService e =
+ final ExecutorService e =
new ThreadPoolExecutor(2, 2,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
- List<Callable<String>> l = new ArrayList<Callable<String>>();
- l.add(new StringTask());
- try {
- e.invokeAll(l, MEDIUM_DELAY_MS, null);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
- joinPool(e);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ List<Callable<String>> l = new ArrayList<Callable<String>>();
+ l.add(new StringTask());
+ try {
+ e.invokeAll(l, MEDIUM_DELAY_MS, null);
+ shouldThrow();
+ } catch (NullPointerException success) {}
}
}
@@ -1811,15 +1802,14 @@
* timed invokeAll(empty collection) returns empty collection
*/
public void testTimedInvokeAll2() throws InterruptedException {
- ExecutorService e =
+ final ExecutorService e =
new ThreadPoolExecutor(2, 2,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
- try {
- List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(),
+ MEDIUM_DELAY_MS, MILLISECONDS);
assertTrue(r.isEmpty());
- } finally {
- joinPool(e);
}
}
@@ -1827,19 +1817,18 @@
* timed invokeAll(c) throws NPE if c has null elements
*/
public void testTimedInvokeAll3() throws Exception {
- ExecutorService e =
+ final ExecutorService e =
new ThreadPoolExecutor(2, 2,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
- List<Callable<String>> l = new ArrayList<Callable<String>>();
- l.add(new StringTask());
- l.add(null);
- try {
- e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
- shouldThrow();
- } catch (NullPointerException success) {
- } finally {
- joinPool(e);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ List<Callable<String>> l = new ArrayList<Callable<String>>();
+ l.add(new StringTask());
+ l.add(null);
+ try {
+ e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
+ shouldThrow();
+ } catch (NullPointerException success) {}
}
}
@@ -1847,22 +1836,22 @@
* get of element of invokeAll(c) throws exception on failed task
*/
public void testTimedInvokeAll4() throws Exception {
- ExecutorService e =
+ final ExecutorService e =
new ThreadPoolExecutor(2, 2,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
- List<Callable<String>> l = new ArrayList<Callable<String>>();
- l.add(new NPETask());
- List<Future<String>> futures =
- e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
- assertEquals(1, futures.size());
- try {
- futures.get(0).get();
- shouldThrow();
- } catch (ExecutionException success) {
- assertTrue(success.getCause() instanceof NullPointerException);
- } finally {
- joinPool(e);
+ try (PoolCleaner cleaner = cleaner(e)) {
+ List<Callable<String>> l = new ArrayList<Callable<String>>();
+ l.add(new NPETask());
+ List<Future<String>> futures =
+ e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
+ assertEquals(1, futures.size());
+ try {
+ futures.get(0).get();
+ shouldThrow();
+ } catch (ExecutionException success) {
+ assertTrue(success.getCause() instanceof NullPointerException);
+ }
}
}
@@ -1870,21 +1859,19 @@
* timed invokeAll(c) returns results of all completed tasks
*/
public void testTimedInvokeAll5() throws Exception {
- ExecutorService e =
+ final ExecutorService e =
new ThreadPoolExecutor(2, 2,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
- try {
+ try (PoolCleaner cleaner = cleaner(e)) {
List<Callable<String>> l = new ArrayList<Callable<String>>();
l.add(new StringTask());
l.add(new StringTask());
List<Future<String>> futures =
- e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
+ e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
assertEquals(2, futures.size());
for (Future<String> future : futures)
assertSame(TEST_STRING, future.get());
- } finally {
- joinPool(e);
}
}
@@ -1892,24 +1879,40 @@
* timed invokeAll(c) cancels tasks not completed by timeout
*/
public void testTimedInvokeAll6() throws Exception {
- ExecutorService e =
- new ThreadPoolExecutor(2, 2,
- LONG_DELAY_MS, MILLISECONDS,
- new ArrayBlockingQueue<Runnable>(10));
- try {
- List<Callable<String>> l = new ArrayList<Callable<String>>();
- l.add(new StringTask());
- l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
- l.add(new StringTask());
- List<Future<String>> futures =
- e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
- assertEquals(l.size(), futures.size());
- for (Future future : futures)
- assertTrue(future.isDone());
- assertFalse(futures.get(0).isCancelled());
- assertTrue(futures.get(1).isCancelled());
- } finally {
- joinPool(e);
+ for (long timeout = timeoutMillis();;) {
+ final CountDownLatch done = new CountDownLatch(1);
+ final Callable<String> waiter = new CheckedCallable<String>() {
+ public String realCall() {
+ try { done.await(LONG_DELAY_MS, MILLISECONDS); }
+ catch (InterruptedException ok) {}
+ return "1"; }};
+ final ExecutorService p =
+ new ThreadPoolExecutor(2, 2,
+ LONG_DELAY_MS, MILLISECONDS,
+ new ArrayBlockingQueue<Runnable>(10));
+ try (PoolCleaner cleaner = cleaner(p, done)) {
+ List<Callable<String>> tasks = new ArrayList<>();
+ tasks.add(new StringTask("0"));
+ tasks.add(waiter);
+ tasks.add(new StringTask("2"));
+ long startTime = System.nanoTime();
+ List<Future<String>> futures =
+ p.invokeAll(tasks, timeout, MILLISECONDS);
+ assertEquals(tasks.size(), futures.size());
+ assertTrue(millisElapsedSince(startTime) >= timeout);
+ for (Future future : futures)
+ assertTrue(future.isDone());
+ assertTrue(futures.get(1).isCancelled());
+ try {
+ assertEquals("0", futures.get(0).get());
+ assertEquals("2", futures.get(2).get());
+ break;
+ } catch (CancellationException retryWithLongerTimeout) {
+ timeout *= 2;
+ if (timeout >= LONG_DELAY_MS / 2)
+ fail("expected exactly one task to be cancelled");
+ }
+ }
}
}
@@ -1923,7 +1926,7 @@
LONG_DELAY_MS, MILLISECONDS,
new LinkedBlockingQueue<Runnable>(),
new FailingThreadFactory());
- try {
+ try (PoolCleaner cleaner = cleaner(e)) {
final int TASKS = 100;
final CountDownLatch done = new CountDownLatch(TASKS);
for (int k = 0; k < TASKS; ++k)
@@ -1932,8 +1935,6 @@
done.countDown();
}});
assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
- } finally {
- joinPool(e);
}
}
@@ -1945,21 +1946,22 @@
new ThreadPoolExecutor(2, 2,
1000, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
- assertFalse(p.allowsCoreThreadTimeOut());
- joinPool(p);
+ try (PoolCleaner cleaner = cleaner(p)) {
+ assertFalse(p.allowsCoreThreadTimeOut());
+ }
}
/**
* allowCoreThreadTimeOut(true) causes idle threads to time out
*/
public void testAllowCoreThreadTimeOut_true() throws Exception {
- long coreThreadTimeOut = SHORT_DELAY_MS;
+ long keepAliveTime = timeoutMillis();
final ThreadPoolExecutor p =
new ThreadPoolExecutor(2, 10,
- coreThreadTimeOut, MILLISECONDS,
+ keepAliveTime, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
- final CountDownLatch threadStarted = new CountDownLatch(1);
- try {
+ try (PoolCleaner cleaner = cleaner(p)) {
+ final CountDownLatch threadStarted = new CountDownLatch(1);
p.allowCoreThreadTimeOut(true);
p.execute(new CheckedRunnable() {
public void realRun() {
@@ -1967,15 +1969,13 @@
assertEquals(1, p.getPoolSize());
}});
await(threadStarted);
- delay(coreThreadTimeOut);
+ delay(keepAliveTime);
long startTime = System.nanoTime();
while (p.getPoolSize() > 0
&& millisElapsedSince(startTime) < LONG_DELAY_MS)
Thread.yield();
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
assertEquals(0, p.getPoolSize());
- } finally {
- joinPool(p);
}
}
@@ -1983,23 +1983,21 @@
* allowCoreThreadTimeOut(false) causes idle threads not to time out
*/
public void testAllowCoreThreadTimeOut_false() throws Exception {
- long coreThreadTimeOut = SHORT_DELAY_MS;
+ long keepAliveTime = timeoutMillis();
final ThreadPoolExecutor p =
new ThreadPoolExecutor(2, 10,
- coreThreadTimeOut, MILLISECONDS,
+ keepAliveTime, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
- final CountDownLatch threadStarted = new CountDownLatch(1);
- try {
+ try (PoolCleaner cleaner = cleaner(p)) {
+ final CountDownLatch threadStarted = new CountDownLatch(1);
p.allowCoreThreadTimeOut(false);
p.execute(new CheckedRunnable() {
public void realRun() throws InterruptedException {
threadStarted.countDown();
assertTrue(p.getPoolSize() >= 1);
}});
- delay(2 * coreThreadTimeOut);
+ delay(2 * keepAliveTime);
assertTrue(p.getPoolSize() >= 1);
- } finally {
- joinPool(p);
}
}
@@ -2015,9 +2013,10 @@
done.countDown();
}};
final ThreadPoolExecutor p =
- new ThreadPoolExecutor(1, 30, 60, TimeUnit.SECONDS,
+ new ThreadPoolExecutor(1, 30,
+ 60, SECONDS,
new ArrayBlockingQueue(30));
- try {
+ try (PoolCleaner cleaner = cleaner(p)) {
for (int i = 0; i < nTasks; ++i) {
for (;;) {
try {
@@ -2029,8 +2028,43 @@
}
// enough time to run all tasks
assertTrue(done.await(nTasks * SHORT_DELAY_MS, MILLISECONDS));
- } finally {
- joinPool(p);
+ }
+ }
+
+ /**
+ * get(cancelled task) throws CancellationException
+ */
+ public void testGet_cancelled() throws Exception {
+ final CountDownLatch done = new CountDownLatch(1);
+ final ExecutorService e =
+ new ThreadPoolExecutor(1, 1,
+ LONG_DELAY_MS, MILLISECONDS,
+ new LinkedBlockingQueue<Runnable>());
+ try (PoolCleaner cleaner = cleaner(e, done)) {
+ final CountDownLatch blockerStarted = new CountDownLatch(1);
+ final List<Future<?>> futures = new ArrayList<>();
+ for (int i = 0; i < 2; i++) {
+ Runnable r = new CheckedRunnable() { public void realRun()
+ throws Throwable {
+ blockerStarted.countDown();
+ assertTrue(done.await(2 * LONG_DELAY_MS, MILLISECONDS));
+ }};
+ futures.add(e.submit(r));
+ }
+ await(blockerStarted);
+ for (Future<?> future : futures) future.cancel(false);
+ for (Future<?> future : futures) {
+ try {
+ future.get();
+ shouldThrow();
+ } catch (CancellationException success) {}
+ try {
+ future.get(LONG_DELAY_MS, MILLISECONDS);
+ shouldThrow();
+ } catch (CancellationException success) {}
+ assertTrue(future.isCancelled());
+ assertTrue(future.isDone());
+ }
}
}
diff --git a/jsr166-tests/src/test/java/jsr166/ThreadTest.java b/jsr166-tests/src/test/java/jsr166/ThreadTest.java
index 27f22ca..e69b422 100644
--- a/jsr166-tests/src/test/java/jsr166/ThreadTest.java
+++ b/jsr166-tests/src/test/java/jsr166/ThreadTest.java
@@ -19,7 +19,7 @@
// main(suite(), args);
// }
// public static Test suite() {
- // return new TestSuite(...);
+ // return new TestSuite(ThreadTest.class);
// }
static class MyHandler implements Thread.UncaughtExceptionHandler {
@@ -57,8 +57,7 @@
// default uncaught exception handler installed by the framework.
//
// assertEquals(null, Thread.getDefaultUncaughtExceptionHandler());
-
- // failure due to securityException is OK.
+ // failure due to SecurityException is OK.
// Would be nice to explicitly test both ways, but cannot yet.
Thread.UncaughtExceptionHandler defaultHandler
= Thread.getDefaultUncaughtExceptionHandler();
diff --git a/jsr166-tests/src/test/java/jsr166/TimeUnitTest.java b/jsr166-tests/src/test/java/jsr166/TimeUnitTest.java
index 2c9529b..b21fa7d 100644
--- a/jsr166-tests/src/test/java/jsr166/TimeUnitTest.java
+++ b/jsr166-tests/src/test/java/jsr166/TimeUnitTest.java
@@ -30,7 +30,7 @@
// main(suite(), args);
// }
// public static Test suite() {
- // return new TestSuite(...);
+ // return new TestSuite(TimeUnitTest.class);
// }
// (loops to 88888 check increments at all time divisions.)
@@ -433,8 +433,8 @@
* a deserialized serialized unit is the same instance
*/
public void testSerialization() throws Exception {
- TimeUnit x = MILLISECONDS;
- assertSame(x, serialClone(x));
+ for (TimeUnit x : TimeUnit.values())
+ assertSame(x, serialClone(x));
}
}
diff --git a/jsr166-tests/src/test/java/jsr166/TreeMapTest.java b/jsr166-tests/src/test/java/jsr166/TreeMapTest.java
index afc73de..e445609 100644
--- a/jsr166-tests/src/test/java/jsr166/TreeMapTest.java
+++ b/jsr166-tests/src/test/java/jsr166/TreeMapTest.java
@@ -29,7 +29,7 @@
// main(suite(), args);
// }
// public static Test suite() {
- // return new TestSuite(...);
+ // return new TestSuite(TreeMapTest.class);
// }
/**
diff --git a/jsr166-tests/src/test/java/jsr166/TreeSetTest.java b/jsr166-tests/src/test/java/jsr166/TreeSetTest.java
index a935637..c3093f6 100644
--- a/jsr166-tests/src/test/java/jsr166/TreeSetTest.java
+++ b/jsr166-tests/src/test/java/jsr166/TreeSetTest.java
@@ -29,7 +29,7 @@
// main(suite(), args);
// }
// public static Test suite() {
- // return new TestSuite(...);
+ // return new TestSuite(TreeSetTest.class);
// }
static class MyReverseComparator implements Comparator {
@@ -50,7 +50,7 @@
private TreeSet<Integer> populatedSet(int n) {
TreeSet<Integer> q = new TreeSet<Integer>();
assertTrue(q.isEmpty());
- for (int i = n-1; i >= 0; i -= 2)
+ for (int i = n - 1; i >= 0; i -= 2)
assertTrue(q.add(new Integer(i)));
for (int i = (n & 1); i < n; i += 2)
assertTrue(q.add(new Integer(i)));
@@ -96,8 +96,7 @@
*/
public void testConstructor4() {
try {
- Integer[] ints = new Integer[SIZE];
- new TreeSet(Arrays.asList(ints));
+ new TreeSet(Arrays.asList(new Integer[SIZE]));
shouldThrow();
} catch (NullPointerException success) {}
}
@@ -106,10 +105,10 @@
* Initializing from Collection with some null elements throws NPE
*/
public void testConstructor5() {
+ Integer[] ints = new Integer[SIZE];
+ for (int i = 0; i < SIZE - 1; ++i)
+ ints[i] = new Integer(i);
try {
- Integer[] ints = new Integer[SIZE];
- for (int i = 0; i < SIZE-1; ++i)
- ints[i] = new Integer(i);
new TreeSet(Arrays.asList(ints));
shouldThrow();
} catch (NullPointerException success) {}
@@ -138,7 +137,7 @@
for (int i = 0; i < SIZE; ++i)
ints[i] = new Integer(i);
q.addAll(Arrays.asList(ints));
- for (int i = SIZE-1; i >= 0; --i)
+ for (int i = SIZE - 1; i >= 0; --i)
assertEquals(ints[i], q.pollFirst());
}
@@ -162,7 +161,7 @@
public void testSize() {
TreeSet q = populatedSet(SIZE);
for (int i = 0; i < SIZE; ++i) {
- assertEquals(SIZE-i, q.size());
+ assertEquals(SIZE - i, q.size());
q.pollFirst();
}
for (int i = 0; i < SIZE; ++i) {
@@ -242,7 +241,7 @@
public void testAddAll3() {
TreeSet q = new TreeSet();
Integer[] ints = new Integer[SIZE];
- for (int i = 0; i < SIZE-1; ++i)
+ for (int i = 0; i < SIZE - 1; ++i)
ints[i] = new Integer(i);
try {
q.addAll(Arrays.asList(ints));
@@ -257,7 +256,7 @@
Integer[] empty = new Integer[0];
Integer[] ints = new Integer[SIZE];
for (int i = 0; i < SIZE; ++i)
- ints[i] = new Integer(SIZE-1-i);
+ ints[i] = new Integer(SIZE - 1 - i);
TreeSet q = new TreeSet();
assertFalse(q.addAll(Arrays.asList(empty)));
assertTrue(q.addAll(Arrays.asList(ints)));
@@ -281,7 +280,7 @@
*/
public void testPollLast() {
TreeSet q = populatedSet(SIZE);
- for (int i = SIZE-1; i >= 0; --i) {
+ for (int i = SIZE - 1; i >= 0; --i) {
assertEquals(i, q.pollLast());
}
assertNull(q.pollFirst());
@@ -296,14 +295,14 @@
assertTrue(q.contains(i));
assertTrue(q.remove(i));
assertFalse(q.contains(i));
- assertTrue(q.contains(i-1));
+ assertTrue(q.contains(i - 1));
}
for (int i = 0; i < SIZE; i += 2) {
assertTrue(q.contains(i));
assertTrue(q.remove(i));
assertFalse(q.contains(i));
- assertFalse(q.remove(i+1));
- assertFalse(q.contains(i+1));
+ assertFalse(q.remove(i + 1));
+ assertFalse(q.contains(i + 1));
}
assertTrue(q.isEmpty());
}
@@ -362,7 +361,7 @@
assertTrue(changed);
assertTrue(q.containsAll(p));
- assertEquals(SIZE-i, q.size());
+ assertEquals(SIZE - i, q.size());
p.pollFirst();
}
}
@@ -375,7 +374,7 @@
TreeSet q = populatedSet(SIZE);
TreeSet p = populatedSet(i);
assertTrue(q.removeAll(p));
- assertEquals(SIZE-i, q.size());
+ assertEquals(SIZE - i, q.size());
for (int j = 0; j < i; ++j) {
Integer x = (Integer)(p.pollFirst());
assertFalse(q.contains(x));
@@ -909,18 +908,18 @@
else if (element > max)
return -1;
int result = bs.nextSetBit(element);
- return result > max ? -1 : result;
+ return (result > max) ? -1 : result;
}
int higherAscending(int element) {
return ceilingAscending(element + 1);
}
private int firstAscending() {
int result = ceilingAscending(min);
- return result > max ? -1 : result;
+ return (result > max) ? -1 : result;
}
private int lastAscending() {
int result = floorAscending(max);
- return result < min ? -1 : result;
+ return (result < min) ? -1 : result;
}
}
ReferenceSet rs = new ReferenceSet();
@@ -981,7 +980,7 @@
}
static boolean eq(Integer i, int j) {
- return i == null ? j == -1 : i == j;
+ return (i == null) ? j == -1 : i == j;
}
}
diff --git a/jsr166-tests/src/test/java/jsr166/TreeSubMapTest.java b/jsr166-tests/src/test/java/jsr166/TreeSubMapTest.java
index 18a9e37..09b809e 100644
--- a/jsr166-tests/src/test/java/jsr166/TreeSubMapTest.java
+++ b/jsr166-tests/src/test/java/jsr166/TreeSubMapTest.java
@@ -27,7 +27,7 @@
// main(suite(), args);
// }
// public static Test suite() {
- // return new TestSuite(...);
+ // return new TestSuite(TreeSubMapTest.class);
// }
/**
diff --git a/jsr166-tests/src/test/java/jsr166/TreeSubSetTest.java b/jsr166-tests/src/test/java/jsr166/TreeSubSetTest.java
index 5398c4e..31403be 100644
--- a/jsr166-tests/src/test/java/jsr166/TreeSubSetTest.java
+++ b/jsr166-tests/src/test/java/jsr166/TreeSubSetTest.java
@@ -25,7 +25,7 @@
// main(suite(), args);
// }
// public static Test suite() {
- // return new TestSuite(...);
+ // return new TestSuite(TreeSubSetTest.class);
// }
static class MyReverseComparator implements Comparator {
@@ -42,7 +42,7 @@
TreeSet<Integer> q = new TreeSet<Integer>();
assertTrue(q.isEmpty());
- for (int i = n-1; i >= 0; i -= 2)
+ for (int i = n - 1; i >= 0; i -= 2)
assertTrue(q.add(new Integer(i)));
for (int i = (n & 1); i < n; i += 2)
assertTrue(q.add(new Integer(i)));
@@ -124,7 +124,7 @@
public void testSize() {
NavigableSet q = populatedSet(SIZE);
for (int i = 0; i < SIZE; ++i) {
- assertEquals(SIZE-i, q.size());
+ assertEquals(SIZE - i, q.size());
q.pollFirst();
}
for (int i = 0; i < SIZE; ++i) {
@@ -203,8 +203,8 @@
public void testAddAll3() {
NavigableSet q = set0();
Integer[] ints = new Integer[SIZE];
- for (int i = 0; i < SIZE-1; ++i)
- ints[i] = new Integer(i+SIZE);
+ for (int i = 0; i < SIZE - 1; ++i)
+ ints[i] = new Integer(i + SIZE);
try {
q.addAll(Arrays.asList(ints));
shouldThrow();
@@ -218,7 +218,7 @@
Integer[] empty = new Integer[0];
Integer[] ints = new Integer[SIZE];
for (int i = 0; i < SIZE; ++i)
- ints[i] = new Integer(SIZE-1- i);
+ ints[i] = new Integer(SIZE - 1 - i);
NavigableSet q = set0();
assertFalse(q.addAll(Arrays.asList(empty)));
assertTrue(q.addAll(Arrays.asList(ints)));
@@ -246,14 +246,14 @@
assertTrue(q.contains(i));
assertTrue(q.remove(i));
assertFalse(q.contains(i));
- assertTrue(q.contains(i-1));
+ assertTrue(q.contains(i - 1));
}
for (int i = 0; i < SIZE; i += 2) {
assertTrue(q.contains(i));
assertTrue(q.remove(i));
assertFalse(q.contains(i));
- assertFalse(q.remove(i+1));
- assertFalse(q.contains(i+1));
+ assertFalse(q.remove(i + 1));
+ assertFalse(q.contains(i + 1));
}
assertTrue(q.isEmpty());
}
@@ -312,7 +312,7 @@
assertTrue(changed);
assertTrue(q.containsAll(p));
- assertEquals(SIZE-i, q.size());
+ assertEquals(SIZE - i, q.size());
p.pollFirst();
}
}
@@ -325,7 +325,7 @@
NavigableSet q = populatedSet(SIZE);
NavigableSet p = populatedSet(i);
assertTrue(q.removeAll(p));
- assertEquals(SIZE-i, q.size());
+ assertEquals(SIZE - i, q.size());
for (int j = 0; j < i; ++j) {
Integer x = (Integer)(p.pollFirst());
assertFalse(q.contains(x));
@@ -620,7 +620,7 @@
public void testDescendingSize() {
NavigableSet q = populatedSet(SIZE);
for (int i = 0; i < SIZE; ++i) {
- assertEquals(SIZE-i, q.size());
+ assertEquals(SIZE - i, q.size());
q.pollFirst();
}
for (int i = 0; i < SIZE; ++i) {
@@ -688,8 +688,8 @@
public void testDescendingAddAll3() {
NavigableSet q = dset0();
Integer[] ints = new Integer[SIZE];
- for (int i = 0; i < SIZE-1; ++i)
- ints[i] = new Integer(i+SIZE);
+ for (int i = 0; i < SIZE - 1; ++i)
+ ints[i] = new Integer(i + SIZE);
try {
q.addAll(Arrays.asList(ints));
shouldThrow();
@@ -703,7 +703,7 @@
Integer[] empty = new Integer[0];
Integer[] ints = new Integer[SIZE];
for (int i = 0; i < SIZE; ++i)
- ints[i] = new Integer(SIZE-1- i);
+ ints[i] = new Integer(SIZE - 1 - i);
NavigableSet q = dset0();
assertFalse(q.addAll(Arrays.asList(empty)));
assertTrue(q.addAll(Arrays.asList(ints)));
@@ -732,7 +732,7 @@
}
for (int i = 0; i < SIZE; i += 2) {
assertTrue(q.remove(new Integer(i)));
- assertFalse(q.remove(new Integer(i+1)));
+ assertFalse(q.remove(new Integer(i + 1)));
}
assertTrue(q.isEmpty());
}
@@ -791,7 +791,7 @@
assertTrue(changed);
assertTrue(q.containsAll(p));
- assertEquals(SIZE-i, q.size());
+ assertEquals(SIZE - i, q.size());
p.pollFirst();
}
}
@@ -804,7 +804,7 @@
NavigableSet q = populatedSet(SIZE);
NavigableSet p = populatedSet(i);
assertTrue(q.removeAll(p));
- assertEquals(SIZE-i, q.size());
+ assertEquals(SIZE - i, q.size());
for (int j = 0; j < i; ++j) {
Integer x = (Integer)(p.pollFirst());
assertFalse(q.contains(x));