summaryrefslogtreecommitdiff
path: root/jsr166-tests/src/test/java/jsr166/SynchronousQueueTest.java
diff options
context:
space:
mode:
authorPrzemyslaw Szczepaniak <pszczepaniak@google.com>2016-03-11 15:59:10 +0000
committerPrzemyslaw Szczepaniak <pszczepaniak@google.com>2016-03-15 11:14:14 +0000
commite8b323c7cb7d55be9a4df579231e44f04f53d766 (patch)
treec40a0cadb5caa371c28b117b017daddef4b72c28 /jsr166-tests/src/test/java/jsr166/SynchronousQueueTest.java
parent0eedcb21289c1916e876810982c2c8d473edf24f (diff)
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 (cherry picked from commit b8b75116273ecfdb8ffdd1869b1c0dd04570a95e)
Diffstat (limited to 'jsr166-tests/src/test/java/jsr166/SynchronousQueueTest.java')
-rw-r--r--jsr166-tests/src/test/java/jsr166/SynchronousQueueTest.java87
1 files changed, 46 insertions, 41 deletions
diff --git a/jsr166-tests/src/test/java/jsr166/SynchronousQueueTest.java b/jsr166-tests/src/test/java/jsr166/SynchronousQueueTest.java
index 605a95595b..9d3f212588 100644
--- a/jsr166-tests/src/test/java/jsr166/SynchronousQueueTest.java
+++ b/jsr166-tests/src/test/java/jsr166/SynchronousQueueTest.java
@@ -25,7 +25,7 @@ import junit.framework.Test;
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 @@ public class SynchronousQueueTest extends JSR166TestCase {
// 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 @@ public class SynchronousQueueTest extends JSR166TestCase {
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 @@ public class SynchronousQueueTest extends JSR166TestCase {
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 class SynchronousQueueTest extends JSR166TestCase {
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);
-
- 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);
+ 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 {
+ threadsStarted.await();
+ assertSame(one, q.take());
+ }});
+ }
}
/**
@@ -502,22 +505,22 @@ public class SynchronousQueueTest extends JSR166TestCase {
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());
- }});
-
- executor.execute(new CheckedRunnable() {
- public void realRun() throws InterruptedException {
- threadsStarted.await();
- q.put(one);
- }});
-
- joinPool(executor);
+ 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);
+ }});
+ }
}
/**
@@ -595,10 +598,12 @@ public class SynchronousQueueTest extends JSR166TestCase {
}});
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));