--- jsr166/src/test/tck/LinkedBlockingDequeTest.java 2010/11/03 16:46:34 1.30 +++ jsr166/src/test/tck/LinkedBlockingDequeTest.java 2011/04/14 22:55:08 1.36 @@ -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/ */ import junit.framework.*; @@ -38,8 +38,9 @@ public class LinkedBlockingDequeTest ext * Create a deque of given size containing consecutive * Integers 0 ... n. */ - private LinkedBlockingDeque populatedDeque(int n) { - LinkedBlockingDeque q = new LinkedBlockingDeque(n); + private LinkedBlockingDeque populatedDeque(int n) { + LinkedBlockingDeque q = + new LinkedBlockingDeque(n); assertTrue(q.isEmpty()); for (int i = 0; i < n; i++) assertTrue(q.offer(new Integer(i))); @@ -742,46 +743,54 @@ public class LinkedBlockingDequeTest ext * returning timeout status */ public void testInterruptedTimedPoll() throws InterruptedException { - Thread t = new Thread(new CheckedRunnable() { + final BlockingQueue q = populatedDeque(SIZE); + final CountDownLatch aboutToWait = new CountDownLatch(1); + Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { - LinkedBlockingDeque q = populatedDeque(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); + checkEmpty(q); } /** * putFirst(null) throws NPE */ - public void testPutFirstNull() throws InterruptedException { + public void testPutFirstNull() throws InterruptedException { try { LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); q.putFirst(null); shouldThrow(); } catch (NullPointerException success) {} - } + } /** * all elements successfully putFirst are contained */ - public void testPutFirst() throws InterruptedException { - LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); - for (int i = 0; i < SIZE; ++i) { - Integer I = new Integer(i); - q.putFirst(I); - assertTrue(q.contains(I)); - } - assertEquals(0, q.remainingCapacity()); + public void testPutFirst() throws InterruptedException { + LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); + for (int i = 0; i < SIZE; ++i) { + Integer I = new Integer(i); + q.putFirst(I); + assertTrue(q.contains(I)); + } + assertEquals(0, q.remainingCapacity()); } /** @@ -976,25 +985,25 @@ public class LinkedBlockingDequeTest ext /** * putLast(null) throws NPE */ - public void testPutLastNull() throws InterruptedException { + public void testPutLastNull() throws InterruptedException { try { LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); q.putLast(null); shouldThrow(); } catch (NullPointerException success) {} - } + } /** * all elements successfully putLast are contained */ - public void testPutLast() throws InterruptedException { - LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); - for (int i = 0; i < SIZE; ++i) { - Integer I = new Integer(i); - q.putLast(I); - assertTrue(q.contains(I)); - } - assertEquals(0, q.remainingCapacity()); + public void testPutLast() throws InterruptedException { + LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); + for (int i = 0; i < SIZE; ++i) { + Integer I = new Integer(i); + q.putLast(I); + assertTrue(q.contains(I)); + } + assertEquals(0, q.remainingCapacity()); } /** @@ -1207,11 +1216,17 @@ public class LinkedBlockingDequeTest ext public void testRemoveElement() { LinkedBlockingDeque q = populatedDeque(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()); } @@ -1294,24 +1309,25 @@ public class LinkedBlockingDequeTest ext } /** - * toArray contains all elements + * toArray contains all elements in FIFO order */ public void testToArray() throws InterruptedException{ LinkedBlockingDeque q = populatedDeque(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 { - LinkedBlockingDeque q = populatedDeque(SIZE); + public void testToArray2() { + LinkedBlockingDeque q = populatedDeque(SIZE); Integer[] ints = new Integer[SIZE]; - ints = (Integer[])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.remove()); } /**