--- jsr166/src/test/tck/LinkedBlockingQueueTest.java 2010/11/03 16:46:34 1.35 +++ jsr166/src/test/tck/LinkedBlockingQueueTest.java 2011/05/27 20:07:24 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 Andrew Wright, Jeffrey Hayes, * Pat Fisher, Mike Judd. */ @@ -36,13 +36,13 @@ public class LinkedBlockingQueueTest ext new Bounded().testSuite()); } - /** * Create a queue of given size containing consecutive * Integers 0 ... n. */ - private LinkedBlockingQueue populatedQueue(int n) { - LinkedBlockingQueue q = new LinkedBlockingQueue(n); + private LinkedBlockingQueue populatedQueue(int n) { + LinkedBlockingQueue q = + new LinkedBlockingQueue(n); assertTrue(q.isEmpty()); for (int i = 0; i < n; i++) assertTrue(q.offer(new Integer(i))); @@ -276,13 +276,13 @@ public class LinkedBlockingQueueTest ext /** * put(null) throws NPE */ - public void testPutNull() throws InterruptedException { + public void testPutNull() throws InterruptedException { try { LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); q.put(null); shouldThrow(); } catch (NullPointerException success) {} - } + } /** * all elements successfully put are contained @@ -302,72 +302,95 @@ public class LinkedBlockingQueueTest ext */ public void testBlockingPut() throws InterruptedException { final LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); - Thread t = new Thread(new CheckedRunnable() { + final CountDownLatch pleaseInterrupt = new CountDownLatch(1); + Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { for (int i = 0; i < SIZE; ++i) q.put(i); assertEquals(SIZE, q.size()); assertEquals(0, q.remainingCapacity()); + + Thread.currentThread().interrupt(); try { q.put(99); shouldThrow(); } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + + pleaseInterrupt.countDown(); + try { + q.put(99); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); }}); - t.start(); - Thread.sleep(SHORT_DELAY_MS); + await(pleaseInterrupt); + assertThreadStaysAlive(t); t.interrupt(); - t.join(); + awaitTermination(t); assertEquals(SIZE, q.size()); assertEquals(0, q.remainingCapacity()); } /** - * put blocks waiting for take when full + * put blocks interruptibly waiting for take when full */ public void testPutWithTake() throws InterruptedException { final int capacity = 2; final LinkedBlockingQueue q = new LinkedBlockingQueue(2); - Thread t = new Thread(new CheckedRunnable() { + final CountDownLatch pleaseTake = new CountDownLatch(1); + final CountDownLatch pleaseInterrupt = new CountDownLatch(1); + Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { - for (int i = 0; i < capacity + 1; i++) + for (int i = 0; i < capacity; i++) q.put(i); + pleaseTake.countDown(); + q.put(86); + + pleaseInterrupt.countDown(); try { q.put(99); shouldThrow(); } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); }}); - t.start(); - Thread.sleep(SHORT_DELAY_MS); + await(pleaseTake); assertEquals(q.remainingCapacity(), 0); assertEquals(0, q.take()); - Thread.sleep(SHORT_DELAY_MS); + + await(pleaseInterrupt); + assertThreadStaysAlive(t); t.interrupt(); - t.join(); + awaitTermination(t); assertEquals(q.remainingCapacity(), 0); } /** * timed offer times out if full and elements not taken */ - public void testTimedOffer() throws InterruptedException { + public void testTimedOffer() { final LinkedBlockingQueue q = new LinkedBlockingQueue(2); - Thread t = new Thread(new CheckedRunnable() { + final CountDownLatch pleaseInterrupt = new CountDownLatch(1); + Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { q.put(new Object()); q.put(new Object()); - assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); + long startTime = System.nanoTime(); + assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS)); + assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); + pleaseInterrupt.countDown(); try { - q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); + q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (InterruptedException success) {} }}); - t.start(); - Thread.sleep(SMALL_DELAY_MS); + await(pleaseInterrupt); + assertThreadStaysAlive(t); t.interrupt(); - t.join(); + awaitTermination(t); } /** @@ -384,22 +407,33 @@ public class LinkedBlockingQueueTest ext * Take removes existing elements until empty, then blocks interruptibly */ public void testBlockingTake() throws InterruptedException { - final LinkedBlockingQueue q = populatedQueue(SIZE); - Thread t = new Thread(new CheckedRunnable() { + 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, q.take()); } + + 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()); }}); - t.start(); - Thread.sleep(SHORT_DELAY_MS); + await(pleaseInterrupt); + assertThreadStaysAlive(t); t.interrupt(); - t.join(); + awaitTermination(t); } /** @@ -428,11 +462,16 @@ public class LinkedBlockingQueueTest ext * timed poll with nonzero timeout succeeds when non-empty, else times out */ public void testTimedPoll() throws InterruptedException { - LinkedBlockingQueue q = populatedQueue(SIZE); + LinkedBlockingQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); - } - assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); + 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); } /** @@ -440,22 +479,30 @@ public class LinkedBlockingQueueTest ext * 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 { - LinkedBlockingQueue 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); + checkEmpty(q); } /** @@ -507,11 +554,17 @@ public class LinkedBlockingQueueTest ext public void testRemoveElement() { LinkedBlockingQueue 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()); } @@ -607,24 +660,25 @@ public class LinkedBlockingQueueTest ext } /** - * toArray contains all elements + * toArray contains all elements in FIFO order */ - public void testToArray() throws InterruptedException { + public void testToArray() { LinkedBlockingQueue 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 { - LinkedBlockingQueue q = populatedQueue(SIZE); + LinkedBlockingQueue q = populatedQueue(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.poll()); } /** @@ -649,7 +703,6 @@ public class LinkedBlockingQueueTest ext } catch (ArrayStoreException success) {} } - /** * iterator iterates through all elements */ @@ -680,7 +733,6 @@ public class LinkedBlockingQueueTest ext assertFalse(it.hasNext()); } - /** * iterator ordering is FIFO */ @@ -712,7 +764,6 @@ public class LinkedBlockingQueueTest ext assertEquals(0, q.size()); } - /** * toString contains toStrings of elements */ @@ -720,11 +771,10 @@ public class LinkedBlockingQueueTest ext LinkedBlockingQueue 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))); } } - /** * offer transfers elements across Executor tasks */ @@ -733,16 +783,18 @@ public class LinkedBlockingQueueTest ext 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)); - assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); + threadsStarted.await(); + assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); assertEquals(0, q.remainingCapacity()); }}); executor.execute(new CheckedRunnable() { public void realRun() throws InterruptedException { - Thread.sleep(SMALL_DELAY_MS); + threadsStarted.await(); assertSame(one, q.take()); }}); @@ -750,21 +802,23 @@ public class LinkedBlockingQueueTest ext } /** - * poll retrieves elements across Executor threads + * timed poll retrieves elements across Executor threads */ public void testPollInExecutor() { final LinkedBlockingQueue q = new LinkedBlockingQueue(2); + final CheckedBarrier threadsStarted = new CheckedBarrier(2); ExecutorService executor = Executors.newFixedThreadPool(2); executor.execute(new CheckedRunnable() { public void realRun() throws InterruptedException { assertNull(q.poll()); - assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); - assertTrue(q.isEmpty()); + threadsStarted.await(); + assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); + checkEmpty(q); }}); executor.execute(new CheckedRunnable() { public void realRun() throws InterruptedException { - Thread.sleep(SMALL_DELAY_MS); + threadsStarted.await(); q.put(one); }});