summaryrefslogtreecommitdiff
path: root/jsr166-tests/src/test/java/jsr166/ArrayBlockingQueueTest.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/ArrayBlockingQueueTest.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/ArrayBlockingQueueTest.java')
-rw-r--r--jsr166-tests/src/test/java/jsr166/ArrayBlockingQueueTest.java120
1 files changed, 60 insertions, 60 deletions
diff --git a/jsr166-tests/src/test/java/jsr166/ArrayBlockingQueueTest.java b/jsr166-tests/src/test/java/jsr166/ArrayBlockingQueueTest.java
index 247c90e70e..902ae405e4 100644
--- a/jsr166-tests/src/test/java/jsr166/ArrayBlockingQueueTest.java
+++ b/jsr166-tests/src/test/java/jsr166/ArrayBlockingQueueTest.java
@@ -26,7 +26,7 @@ import junit.framework.Test;
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 @@ public class ArrayBlockingQueueTest extends JSR166TestCase {
// 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 class ArrayBlockingQueueTest extends JSR166TestCase {
*/
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 @@ public class ArrayBlockingQueueTest extends JSR166TestCase {
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 class ArrayBlockingQueueTest extends JSR166TestCase {
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 @@ public class ArrayBlockingQueueTest extends JSR166TestCase {
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 @@ public class ArrayBlockingQueueTest extends JSR166TestCase {
assertTrue(changed);
assertTrue(q.containsAll(p));
- assertEquals(SIZE-i, q.size());
+ assertEquals(SIZE - i, q.size());
p.remove();
}
}
@@ -590,7 +590,7 @@ public class ArrayBlockingQueueTest extends JSR166TestCase {
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 @@ public class ArrayBlockingQueueTest extends JSR166TestCase {
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 @@ public class ArrayBlockingQueueTest extends JSR166TestCase {
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 @@ public class ArrayBlockingQueueTest extends JSR166TestCase {
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 @@ public class ArrayBlockingQueueTest extends JSR166TestCase {
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());
- }});
-
- executor.execute(new CheckedRunnable() {
- public void realRun() throws InterruptedException {
- threadsStarted.await();
- assertEquals(0, q.remainingCapacity());
- 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(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());
+ }});
+ }
}
/**
@@ -819,22 +819,22 @@ public class ArrayBlockingQueueTest extends JSR166TestCase {
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);
- }});
-
- 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));
+ checkEmpty(q);
+ }});
+
+ executor.execute(new CheckedRunnable() {
+ public void realRun() throws InterruptedException {
+ threadsStarted.await();
+ q.put(one);
+ }});
+ }
}
/**
@@ -886,7 +886,7 @@ public class ArrayBlockingQueueTest extends JSR166TestCase {
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 @@ public class ArrayBlockingQueueTest extends JSR166TestCase {
* 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 @@ public class ArrayBlockingQueueTest extends JSR166TestCase {
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);