--- jsr166/src/test/tck/PriorityBlockingQueueTest.java 2010/10/09 19:30:35 1.30 +++ jsr166/src/test/tck/PriorityBlockingQueueTest.java 2011/04/14 22:55:08 1.42 @@ -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 Andrew Wright, Jeffrey Hayes, * Pat Fisher, Mike Judd. */ @@ -49,8 +49,9 @@ public class PriorityBlockingQueueTest e * Create a queue of given size containing consecutive * Integers 0 ... n. */ - private PriorityBlockingQueue populatedQueue(int n) { - PriorityBlockingQueue q = new PriorityBlockingQueue(n); + private PriorityBlockingQueue populatedQueue(int n) { + PriorityBlockingQueue q = + new PriorityBlockingQueue(n); assertTrue(q.isEmpty()); for (int i = n-1; i >= 0; i-=2) assertTrue(q.offer(new Integer(i))); @@ -295,25 +296,25 @@ public class PriorityBlockingQueueTest e /** * put(null) throws NPE */ - public void testPutNull() { + public void testPutNull() { try { PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); q.put(null); shouldThrow(); } catch (NullPointerException success) {} - } + } /** * all elements successfully put are contained */ - public void testPut() { - PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); - for (int i = 0; i < SIZE; ++i) { - Integer I = new Integer(i); - q.put(I); - assertTrue(q.contains(I)); - } - assertEquals(SIZE, q.size()); + public void testPut() { + PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); + for (int i = 0; i < SIZE; ++i) { + Integer I = new Integer(i); + q.put(I); + assertTrue(q.contains(I)); + } + assertEquals(SIZE, q.size()); } /** @@ -366,22 +367,6 @@ public class PriorityBlockingQueueTest e } /** - * take blocks interruptibly when empty - */ - public void testTakeFromEmpty() throws InterruptedException { - final PriorityBlockingQueue q = new PriorityBlockingQueue(2); - Thread t = new Thread(new CheckedInterruptedRunnable() { - public void realRun() throws InterruptedException { - q.take(); - }}); - - t.start(); - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - t.join(); - } - - /** * Take removes existing elements until empty, then blocks interruptibly */ public void testBlockingTake() throws InterruptedException { @@ -416,7 +401,7 @@ public class PriorityBlockingQueueTest e } /** - * timed pool with zero timeout succeeds when non-empty, else times out + * timed poll with zero timeout succeeds when non-empty, else times out */ public void testTimedPoll0() throws InterruptedException { PriorityBlockingQueue q = populatedQueue(SIZE); @@ -427,7 +412,7 @@ public class PriorityBlockingQueueTest e } /** - * timed pool with nonzero timeout succeeds when non-empty, else times out + * timed poll with nonzero timeout succeeds when non-empty, else times out */ public void testTimedPoll() throws InterruptedException { PriorityBlockingQueue q = populatedQueue(SIZE); @@ -442,22 +427,29 @@ public class PriorityBlockingQueueTest e * returning timeout status */ public void testInterruptedTimedPoll() throws InterruptedException { - Thread t = new Thread(new CheckedRunnable() { + final BlockingQueue q = populatedQueue(SIZE); + final CountDownLatch aboutToWait = new CountDownLatch(1); + Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { - PriorityBlockingQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); + 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(SMALL_DELAY_MS, MILLISECONDS); + q.poll(MEDIUM_DELAY_MS, MILLISECONDS); shouldThrow(); - } catch (InterruptedException success) {} + } catch (InterruptedException success) { + assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); + } }}); - t.start(); - Thread.sleep(SHORT_DELAY_MS); + aboutToWait.await(); + waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); t.interrupt(); - t.join(); + awaitTermination(t, MEDIUM_DELAY_MS); } /** @@ -509,11 +501,17 @@ public class PriorityBlockingQueueTest e public void testRemoveElement() { PriorityBlockingQueue q = populatedQueue(SIZE); for (int i = 1; i < SIZE; i+=2) { - assertTrue(q.remove(new Integer(i))); + 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) { - assertTrue(q.remove(new Integer(i))); - assertFalse(q.remove(new Integer(i+1))); + assertTrue(q.contains(i)); + assertTrue(q.remove(i)); + assertFalse(q.contains(i)); + assertFalse(q.remove(i+1)); + assertFalse(q.contains(i+1)); } assertTrue(q.isEmpty()); } @@ -602,39 +600,40 @@ public class PriorityBlockingQueueTest e Object[] o = q.toArray(); Arrays.sort(o); for (int i = 0; i < o.length; i++) - assertEquals(o[i], q.take()); + assertSame(o[i], q.take()); } /** * toArray(a) contains all elements */ public void testToArray2() throws InterruptedException { - PriorityBlockingQueue q = populatedQueue(SIZE); + PriorityBlockingQueue q = populatedQueue(SIZE); Integer[] ints = new Integer[SIZE]; - ints = (Integer[])q.toArray(ints); + Integer[] array = q.toArray(ints); + assertSame(ints, array); Arrays.sort(ints); for (int i = 0; i < ints.length; i++) - assertEquals(ints[i], q.take()); + assertSame(ints[i], q.take()); } /** - * toArray(null) throws NPE + * toArray(null) throws NullPointerException */ - public void testToArray_BadArg() { + public void testToArray_NullArg() { PriorityBlockingQueue q = populatedQueue(SIZE); try { - Object o[] = q.toArray(null); + q.toArray(null); shouldThrow(); } catch (NullPointerException success) {} } /** - * toArray with incompatible array type throws CCE + * toArray(incompatible array type) throws ArrayStoreException */ public void testToArray1_BadArg() { PriorityBlockingQueue q = populatedQueue(SIZE); try { - Object o[] = q.toArray(new String[10]); + q.toArray(new String[10]); shouldThrow(); } catch (ArrayStoreException success) {} } @@ -813,7 +812,7 @@ public class PriorityBlockingQueueTest e } /** - * drainTo(c, n) empties first max {n, size} elements of queue into c + * drainTo(c, n) empties first min(n, size) elements of queue into c */ public void testDrainToN() { PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE*2); @@ -822,7 +821,7 @@ public class PriorityBlockingQueueTest e assertTrue(q.offer(new Integer(j))); ArrayList l = new ArrayList(); q.drainTo(l, i); - int k = (i < SIZE)? i : SIZE; + int k = (i < SIZE) ? i : SIZE; assertEquals(l.size(), k); assertEquals(q.size(), SIZE-k); for (int j = 0; j < k; ++j)