--- jsr166/src/test/tck/LinkedTransferQueueTest.java 2010/10/29 06:21:16 1.32 +++ jsr166/src/test/tck/LinkedTransferQueueTest.java 2011/03/15 19:47:06 1.44 @@ -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 */ @@ -40,33 +40,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 @@ -354,13 +327,16 @@ public class LinkedTransferQueueTest ext for (int i = 0; i < SIZE; ++i) { long t0 = System.nanoTime(); assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); - assertTrue(millisElapsedSince(t0) < SHORT_DELAY_MS); + assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); } + long t0 = System.nanoTime(); aboutToWait.countDown(); try { q.poll(MEDIUM_DELAY_MS, MILLISECONDS); shouldThrow(); - } catch (InterruptedException success) {} + } catch (InterruptedException success) { + assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); + } }}); aboutToWait.await(); @@ -382,7 +358,7 @@ public class LinkedTransferQueueTest ext for (int i = 0; i < SIZE; ++i) { long t0 = System.nanoTime(); assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); - assertTrue(millisElapsedSince(t0) < SHORT_DELAY_MS); + assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); } try { q.poll(MEDIUM_DELAY_MS, MILLISECONDS); @@ -445,12 +421,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); } @@ -547,46 +529,47 @@ public class LinkedTransferQueueTest ext } /** - * toArray() contains all elements + * toArray() contains all elements in FIFO order */ - public void testToArray() throws InterruptedException { + public void testToArray() { LinkedTransferQueue q = populatedQueue(SIZE); Object[] o = q.toArray(); for (int i = 0; i < o.length; i++) { - assertEquals(o[i], q.take()); + assertSame(o[i], q.poll()); } } /** - * toArray(a) contains all elements + * toArray(a) contains all elements in FIFO order */ - public void testToArray2() throws InterruptedException { + public void testToArray2() { LinkedTransferQueue q = populatedQueue(SIZE); Integer[] ints = new Integer[SIZE]; - ints = q.toArray(ints); + Integer[] array = q.toArray(ints); + assertSame(ints, array); for (int i = 0; i < ints.length; i++) { - assertEquals(ints[i], q.take()); + assertSame(ints[i], q.poll()); } } /** * toArray(null) throws NullPointerException */ - public void testToArray_BadArg() { + public void testToArray_NullArg() { LinkedTransferQueue q = populatedQueue(SIZE); try { - Object o[] = q.toArray(null); + q.toArray(null); shouldThrow(); } catch (NullPointerException success) {} } /** - * toArray(incompatible array type) throws CCE + * toArray(incompatible array type) throws ArrayStoreException */ public void testToArray1_BadArg() { LinkedTransferQueue q = populatedQueue(SIZE); try { - Object o[] = q.toArray(new String[10]); + q.toArray(new String[10]); shouldThrow(); } catch (ArrayStoreException success) {} } @@ -910,15 +893,14 @@ public class LinkedTransferQueueTest ext Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { threadStarted.countDown(); - q.transfer(SIZE); + q.transfer(five); checkEmpty(q); }}); threadStarted.await(); waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); - assertTrue(t.isAlive()); assertEquals(1, q.size()); - assertEquals(SIZE, (int) q.poll()); + assertSame(five, q.poll()); checkEmpty(q); awaitTermination(t, MEDIUM_DELAY_MS); } @@ -1069,12 +1051,13 @@ public class LinkedTransferQueueTest ext } /** - * tryTransfer waits the amount given if interrupted, and - * throws interrupted exception + * tryTransfer waits the amount given, and throws + * InterruptedException when interrupted. */ public void testTryTransfer5() throws InterruptedException { final LinkedTransferQueue q = new LinkedTransferQueue(); final CountDownLatch threadStarted = new CountDownLatch(1); + assertTrue(q.isEmpty()); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { @@ -1085,9 +1068,12 @@ public class LinkedTransferQueueTest ext shouldThrow(); } catch (InterruptedException success) {} assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); + assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); }}); threadStarted.await(); + while (q.isEmpty()) + Thread.yield(); Thread.sleep(SHORT_DELAY_MS); t.interrupt(); awaitTermination(t, MEDIUM_DELAY_MS); @@ -1095,7 +1081,7 @@ public class LinkedTransferQueueTest ext } /** - * tryTransfer gives up after the timeout and return false + * tryTransfer gives up after the timeout and returns false */ public void testTryTransfer6() throws InterruptedException { final LinkedTransferQueue q = new LinkedTransferQueue(); @@ -1137,8 +1123,8 @@ public class LinkedTransferQueueTest ext } /** - * tryTransfer attempts to enqueue into the q and fails returning - * false not enqueueing and the successive poll is null + * tryTransfer attempts to enqueue into the queue and fails + * returning false not enqueueing and the successive poll is null */ public void testTryTransfer8() throws InterruptedException { final LinkedTransferQueue q = new LinkedTransferQueue();