--- jsr166/src/test/tck/LinkedTransferQueueTest.java 2010/11/18 18:49:44 1.41 +++ jsr166/src/test/tck/LinkedTransferQueueTest.java 2011/05/30 22:43:20 1.47 @@ -1,7 +1,7 @@ /* * 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/licenses/publicdomain + * http://creativecommons.org/publicdomain/zero/1.0/ * Other contributors include John Vint */ @@ -13,10 +13,15 @@ import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; import java.util.Iterator; import java.util.List; import java.util.NoSuchElementException; -import java.util.concurrent.*; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Executors; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.LinkedTransferQueue; import static java.util.concurrent.TimeUnit.MILLISECONDS; import static java.util.concurrent.TimeUnit.NANOSECONDS; import junit.framework.Test; @@ -40,33 +45,6 @@ public class LinkedTransferQueueTest ext new Generic().testSuite()); } - void checkEmpty(BlockingQueue q) { - try { - assertTrue(q.isEmpty()); - assertEquals(0, q.size()); - assertNull(q.peek()); - assertNull(q.poll()); - assertNull(q.poll(0, MILLISECONDS)); - assertEquals(q.toString(), "[]"); - assertTrue(Arrays.equals(q.toArray(), new Object[0])); - assertFalse(q.iterator().hasNext()); - try { - q.element(); - shouldThrow(); - } catch (NoSuchElementException success) {} - try { - q.iterator().next(); - shouldThrow(); - } catch (NoSuchElementException success) {} - try { - q.remove(); - shouldThrow(); - } catch (NoSuchElementException success) {} - } catch (InterruptedException ie) { - threadUnexpectedException(ie); - } - } - /** * Constructor builds new queue with size being zero and empty * being true @@ -92,9 +70,9 @@ public class LinkedTransferQueueTest ext * NullPointerException */ public void testConstructor3() { + Collection elements = Arrays.asList(new Integer[SIZE]); try { - Integer[] ints = new Integer[SIZE]; - new LinkedTransferQueue(Arrays.asList(ints)); + new LinkedTransferQueue(elements); shouldThrow(); } catch (NullPointerException success) {} } @@ -104,12 +82,12 @@ public class LinkedTransferQueueTest ext * throws NullPointerException */ public void testConstructor4() { + Integer[] ints = new Integer[SIZE]; + for (int i = 0; i < SIZE-1; ++i) + ints[i] = i; + Collection elements = Arrays.asList(ints); try { - Integer[] ints = new Integer[SIZE]; - for (int i = 0; i < SIZE - 1; ++i) { - ints[i] = i; - } - new LinkedTransferQueue(Arrays.asList(ints)); + new LinkedTransferQueue(elements); shouldThrow(); } catch (NullPointerException success) {} } @@ -156,39 +134,6 @@ public class LinkedTransferQueueTest ext } /** - * offer(null) throws NullPointerException - */ - public void testOfferNull() { - try { - LinkedTransferQueue q = new LinkedTransferQueue(); - q.offer(null); - shouldThrow(); - } catch (NullPointerException success) {} - } - - /** - * add(null) throws NullPointerException - */ - public void testAddNull() { - try { - LinkedTransferQueue q = new LinkedTransferQueue(); - q.add(null); - shouldThrow(); - } catch (NullPointerException success) {} - } - - /** - * addAll(null) throws NullPointerException - */ - public void testAddAll1() { - try { - LinkedTransferQueue q = new LinkedTransferQueue(); - q.addAll(null); - shouldThrow(); - } catch (NullPointerException success) {} - } - - /** * addAll(this) throws IllegalArgumentException */ public void testAddAllSelf() { @@ -200,18 +145,6 @@ public class LinkedTransferQueueTest ext } /** - * addAll of a collection with null elements throws NullPointerException - */ - public void testAddAll2() { - try { - LinkedTransferQueue q = new LinkedTransferQueue(); - Integer[] ints = new Integer[SIZE]; - q.addAll(Arrays.asList(ints)); - shouldThrow(); - } catch (NullPointerException success) {} - } - - /** * addAll of a collection with any null elements throws * NullPointerException after possibly adding some elements */ @@ -245,17 +178,6 @@ public class LinkedTransferQueueTest ext } /** - * put(null) throws NullPointerException - */ - public void testPutNull() throws InterruptedException { - try { - LinkedTransferQueue q = new LinkedTransferQueue(); - q.put(null); - shouldThrow(); - } catch (NullPointerException success) {} - } - - /** * all elements successfully put are contained */ public void testPut() { @@ -281,25 +203,33 @@ public class LinkedTransferQueueTest ext * take removes existing elements until empty, then blocks interruptibly */ public void testBlockingTake() throws InterruptedException { - final BlockingQueue q = populatedQueue(SIZE); - final CountDownLatch aboutToWait = new CountDownLatch(1); + final BlockingQueue q = populatedQueue(SIZE); + final CountDownLatch pleaseInterrupt = new CountDownLatch(1); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { for (int i = 0; i < SIZE; ++i) { - assertEquals(i, (int) q.take()); + assertEquals(i, q.take()); } - aboutToWait.countDown(); + + Thread.currentThread().interrupt(); + try { + q.take(); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + + pleaseInterrupt.countDown(); try { q.take(); shouldThrow(); } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); }}); - aboutToWait.await(); - waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); + await(pleaseInterrupt); + assertThreadStaysAlive(t); t.interrupt(); - awaitTermination(t, MEDIUM_DELAY_MS); - checkEmpty(q); + awaitTermination(t); } /** @@ -332,13 +262,13 @@ public class LinkedTransferQueueTest ext public void testTimedPoll() throws InterruptedException { LinkedTransferQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - long t0 = System.nanoTime(); - assertEquals(i, (int) q.poll(SMALL_DELAY_MS, MILLISECONDS)); - assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); - } - long t0 = System.nanoTime(); - assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); - assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); + long startTime = System.nanoTime(); + assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); + } + long startTime = System.nanoTime(); + assertNull(q.poll(timeoutMillis(), MILLISECONDS)); + assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); checkEmpty(q); } @@ -448,12 +378,18 @@ public class LinkedTransferQueueTest ext */ public void testRemoveElement() throws InterruptedException { LinkedTransferQueue q = populatedQueue(SIZE); - for (int i = 1; i < SIZE; i += 2) { + for (int i = 1; i < SIZE; i+=2) { + assertTrue(q.contains(i)); assertTrue(q.remove(i)); + assertFalse(q.contains(i)); + assertTrue(q.contains(i-1)); } - for (int i = 0; i < SIZE; i += 2) { + for (int i = 0; i < SIZE; i+=2) { + assertTrue(q.contains(i)); assertTrue(q.remove(i)); - assertFalse(q.remove(i + 1)); + assertFalse(q.contains(i)); + assertFalse(q.remove(i+1)); + assertFalse(q.contains(i+1)); } checkEmpty(q); } @@ -574,17 +510,6 @@ public class LinkedTransferQueueTest ext } /** - * toArray(null) throws NullPointerException - */ - public void testToArray_NullArg() { - LinkedTransferQueue q = populatedQueue(SIZE); - try { - q.toArray(null); - shouldThrow(); - } catch (NullPointerException success) {} - } - - /** * toArray(incompatible array type) throws ArrayStoreException */ public void testToArray1_BadArg() { @@ -667,7 +592,7 @@ public class LinkedTransferQueueTest ext LinkedTransferQueue q = populatedQueue(SIZE); String s = q.toString(); for (int i = 0; i < SIZE; ++i) { - assertTrue(s.indexOf(String.valueOf(i)) >= 0); + assertTrue(s.contains(String.valueOf(i))); } } @@ -676,19 +601,17 @@ public class LinkedTransferQueueTest ext */ public void testOfferInExecutor() { final LinkedTransferQueue q = new LinkedTransferQueue(); - final CountDownLatch threadsStarted = new CountDownLatch(2); + final CheckedBarrier threadsStarted = new CheckedBarrier(2); ExecutorService executor = Executors.newFixedThreadPool(2); executor.execute(new CheckedRunnable() { public void realRun() throws InterruptedException { - threadsStarted.countDown(); threadsStarted.await(); - assertTrue(q.offer(one, MEDIUM_DELAY_MS, MILLISECONDS)); + assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS)); }}); executor.execute(new CheckedRunnable() { public void realRun() throws InterruptedException { - threadsStarted.countDown(); threadsStarted.await(); assertSame(one, q.take()); checkEmpty(q); @@ -702,21 +625,19 @@ public class LinkedTransferQueueTest ext */ public void testPollInExecutor() { final LinkedTransferQueue q = new LinkedTransferQueue(); - final CountDownLatch threadsStarted = new CountDownLatch(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.countDown(); threadsStarted.await(); - assertSame(one, q.poll(SMALL_DELAY_MS, MILLISECONDS)); + assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); checkEmpty(q); }}); executor.execute(new CheckedRunnable() { public void realRun() throws InterruptedException { - threadsStarted.countDown(); threadsStarted.await(); q.put(one); }}); @@ -751,28 +672,6 @@ public class LinkedTransferQueueTest ext } /** - * drainTo(null) throws NullPointerException - */ - public void testDrainToNull() { - LinkedTransferQueue q = populatedQueue(SIZE); - try { - q.drainTo(null); - shouldThrow(); - } catch (NullPointerException success) {} - } - - /** - * drainTo(this) throws IllegalArgumentException - */ - public void testDrainToSelf() { - LinkedTransferQueue q = populatedQueue(SIZE); - try { - q.drainTo(q); - shouldThrow(); - } catch (IllegalArgumentException success) {} - } - - /** * drainTo(c) empties queue into another collection c */ public void testDrainTo() { @@ -818,28 +717,6 @@ public class LinkedTransferQueueTest ext } /** - * drainTo(null, n) throws NullPointerException - */ - public void testDrainToNullN() { - LinkedTransferQueue q = populatedQueue(SIZE); - try { - q.drainTo(null, SIZE); - shouldThrow(); - } catch (NullPointerException success) {} - } - - /** - * drainTo(this, n) throws IllegalArgumentException - */ - public void testDrainToSelfN() { - LinkedTransferQueue q = populatedQueue(SIZE); - try { - q.drainTo(q, SIZE); - shouldThrow(); - } catch (IllegalArgumentException success) {} - } - - /** * drainTo(c, n) empties first min(n, size) elements of queue into c */ public void testDrainToN() { @@ -1072,32 +949,34 @@ public class LinkedTransferQueueTest ext } /** - * tryTransfer waits the amount given, and throws - * InterruptedException when interrupted. + * tryTransfer blocks interruptibly if no takers */ public void testTryTransfer5() throws InterruptedException { final LinkedTransferQueue q = new LinkedTransferQueue(); - final CountDownLatch threadStarted = new CountDownLatch(1); + final CountDownLatch pleaseInterrupt = new CountDownLatch(1); assertTrue(q.isEmpty()); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { - long t0 = System.nanoTime(); - threadStarted.countDown(); + Thread.currentThread().interrupt(); + try { + q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + + pleaseInterrupt.countDown(); try { q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (InterruptedException success) {} - assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); - assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); + assertFalse(Thread.interrupted()); }}); - threadStarted.await(); - while (q.isEmpty()) - Thread.yield(); - Thread.sleep(SHORT_DELAY_MS); + await(pleaseInterrupt); + assertThreadStaysAlive(t); t.interrupt(); - awaitTermination(t, MEDIUM_DELAY_MS); + awaitTermination(t); checkEmpty(q); } @@ -1111,12 +990,12 @@ public class LinkedTransferQueueTest ext public void realRun() throws InterruptedException { long t0 = System.nanoTime(); assertFalse(q.tryTransfer(new Object(), - SHORT_DELAY_MS, MILLISECONDS)); - assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); + timeoutMillis(), MILLISECONDS)); + assertTrue(millisElapsedSince(t0) >= timeoutMillis()); checkEmpty(q); }}); - awaitTermination(t, MEDIUM_DELAY_MS); + awaitTermination(t); checkEmpty(q); } @@ -1152,8 +1031,8 @@ public class LinkedTransferQueueTest ext assertTrue(q.offer(four)); assertEquals(1, q.size()); long t0 = System.nanoTime(); - assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS)); - assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); + assertFalse(q.tryTransfer(five, timeoutMillis(), MILLISECONDS)); + assertTrue(millisElapsedSince(t0) >= timeoutMillis()); assertEquals(1, q.size()); assertSame(four, q.poll()); assertNull(q.poll());