--- jsr166/src/test/tck/LinkedTransferQueueTest.java 2010/10/28 19:05:04 1.29 +++ jsr166/src/test/tck/LinkedTransferQueueTest.java 2011/05/31 16:16:24 1.48 @@ -1,26 +1,25 @@ /* * 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 */ -import java.io.BufferedInputStream; -import java.io.BufferedOutputStream; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; -import java.util.ArrayList; +import junit.framework.*; import java.util.Arrays; +import java.util.ArrayList; +import java.util.Collection; import java.util.Iterator; import java.util.List; import java.util.NoSuchElementException; -import java.util.concurrent.*; +import java.util.Queue; +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; -import junit.framework.TestSuite; @SuppressWarnings({"unchecked", "rawtypes"}) public class LinkedTransferQueueTest extends JSR166TestCase { @@ -40,29 +39,6 @@ public class LinkedTransferQueueTest ext new Generic().testSuite()); } - void checkEmpty(LinkedTransferQueue q) throws InterruptedException { - 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) {} - } - /** * Constructor builds new queue with size being zero and empty * being true @@ -88,9 +64,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) {} } @@ -100,12 +76,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) {} } @@ -152,39 +128,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() { @@ -196,18 +139,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 */ @@ -241,17 +172,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() { @@ -277,23 +197,33 @@ public class LinkedTransferQueueTest ext * take removes existing elements until empty, then blocks interruptibly */ public void testBlockingTake() throws InterruptedException { - final LinkedTransferQueue 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, (int) q.take()); + 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(); - awaitTermination(t, MEDIUM_DELAY_MS); - checkEmpty(q); + awaitTermination(t); } /** @@ -309,7 +239,7 @@ public class LinkedTransferQueueTest ext } /** - * 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 { LinkedTransferQueue q = populatedQueue(SIZE); @@ -321,16 +251,18 @@ public class LinkedTransferQueueTest ext } /** - * 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 { LinkedTransferQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - long t0 = System.nanoTime(); + long startTime = System.nanoTime(); assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); - assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); } - assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); + long startTime = System.nanoTime(); + assertNull(q.poll(timeoutMillis(), MILLISECONDS)); + assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); checkEmpty(q); } @@ -339,7 +271,8 @@ public class LinkedTransferQueueTest ext * returning timeout status */ public void testInterruptedTimedPoll() throws InterruptedException { - final LinkedTransferQueue q = populatedQueue(SIZE); + final BlockingQueue q = populatedQueue(SIZE); + final CountDownLatch aboutToWait = new CountDownLatch(1); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { for (int i = 0; i < SIZE; ++i) { @@ -347,19 +280,48 @@ public class LinkedTransferQueueTest ext assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); } + long t0 = System.nanoTime(); + aboutToWait.countDown(); try { - q.poll(LONG_DELAY_MS, MILLISECONDS); + q.poll(MEDIUM_DELAY_MS, MILLISECONDS); shouldThrow(); - } catch (InterruptedException success) {} + } catch (InterruptedException success) { + assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); + } }}); - Thread.sleep(SMALL_DELAY_MS); + aboutToWait.await(); + waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); t.interrupt(); awaitTermination(t, MEDIUM_DELAY_MS); checkEmpty(q); } /** + * timed poll after thread interrupted throws InterruptedException + * instead of returning timeout status + */ + public void testTimedPollAfterInterrupt() throws InterruptedException { + final BlockingQueue q = populatedQueue(SIZE); + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + Thread.currentThread().interrupt(); + for (int i = 0; i < SIZE; ++i) { + long t0 = System.nanoTime(); + assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); + assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); + } + try { + q.poll(MEDIUM_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + }}); + + awaitTermination(t, MEDIUM_DELAY_MS); + checkEmpty(q); + } + + /** * peek returns next element, or null if empty */ public void testPeek() throws InterruptedException { @@ -410,12 +372,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); } @@ -512,46 +480,36 @@ 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() { - LinkedTransferQueue q = populatedQueue(SIZE); - try { - Object o[] = 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) {} } @@ -628,7 +586,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))); } } @@ -637,19 +595,20 @@ public class LinkedTransferQueueTest ext */ public void testOfferInExecutor() { final LinkedTransferQueue q = new LinkedTransferQueue(); - q.add(one); - q.add(two); + final CheckedBarrier threadsStarted = new CheckedBarrier(2); ExecutorService executor = Executors.newFixedThreadPool(2); executor.execute(new CheckedRunnable() { - public void realRun() { - assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); + public void realRun() throws InterruptedException { + threadsStarted.await(); + assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS)); }}); executor.execute(new CheckedRunnable() { public void realRun() throws InterruptedException { - Thread.sleep(SMALL_DELAY_MS); + threadsStarted.await(); assertSame(one, q.take()); + checkEmpty(q); }}); joinPool(executor); @@ -660,18 +619,20 @@ public class LinkedTransferQueueTest ext */ public void testPollInExecutor() { final LinkedTransferQueue q = new LinkedTransferQueue(); + 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); }}); @@ -682,46 +643,18 @@ public class LinkedTransferQueueTest ext * A deserialized serialized queue has same elements in same order */ public void testSerialization() throws Exception { - LinkedTransferQueue q = populatedQueue(SIZE); + Queue x = populatedQueue(SIZE); + Queue y = serialClone(x); - ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); - ObjectOutputStream out - = new ObjectOutputStream(new BufferedOutputStream(bout)); - out.writeObject(q); - out.close(); - - ByteArrayInputStream bin - = new ByteArrayInputStream(bout.toByteArray()); - ObjectInputStream in - = new ObjectInputStream(new BufferedInputStream(bin)); - LinkedTransferQueue r = (LinkedTransferQueue) in.readObject(); - - assertEquals(q.size(), r.size()); - while (!q.isEmpty()) { - assertEquals(q.remove(), r.remove()); + assertTrue(x != y); + assertEquals(x.size(), y.size()); + assertEquals(x.toString(), y.toString()); + assertTrue(Arrays.equals(x.toArray(), y.toArray())); + while (!x.isEmpty()) { + assertFalse(y.isEmpty()); + assertEquals(x.remove(), y.remove()); } - } - - /** - * 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) {} + assertTrue(y.isEmpty()); } /** @@ -770,28 +703,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() { @@ -821,20 +732,25 @@ public class LinkedTransferQueueTest ext final LinkedTransferQueue q = new LinkedTransferQueue(); assertEquals(q.getWaitingConsumerCount(), 0); assertFalse(q.hasWaitingConsumer()); + final CountDownLatch threadStarted = new CountDownLatch(1); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { - Thread.sleep(SMALL_DELAY_MS); - assertTrue(q.hasWaitingConsumer()); - assertEquals(q.getWaitingConsumerCount(), 1); - assertTrue(q.offer(one)); - assertFalse(q.hasWaitingConsumer()); + threadStarted.countDown(); + assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); assertEquals(q.getWaitingConsumerCount(), 0); + assertFalse(q.hasWaitingConsumer()); }}); - assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); + threadStarted.await(); + waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); + assertEquals(q.getWaitingConsumerCount(), 1); + assertTrue(q.hasWaitingConsumer()); + + assertTrue(q.offer(one)); assertEquals(q.getWaitingConsumerCount(), 0); assertFalse(q.hasWaitingConsumer()); + awaitTermination(t, MEDIUM_DELAY_MS); } @@ -856,17 +772,20 @@ public class LinkedTransferQueueTest ext public void testTransfer2() throws InterruptedException { final LinkedTransferQueue q = new LinkedTransferQueue(); + final CountDownLatch threadStarted = new CountDownLatch(1); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { - q.transfer(SIZE); - assertTrue(q.isEmpty()); + threadStarted.countDown(); + q.transfer(five); + checkEmpty(q); }}); - Thread.sleep(SHORT_DELAY_MS); + threadStarted.await(); + waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); assertEquals(1, q.size()); - assertEquals(SIZE, (int) q.poll()); - assertTrue(q.isEmpty()); + assertSame(five, q.poll()); + checkEmpty(q); awaitTermination(t, MEDIUM_DELAY_MS); } @@ -879,30 +798,28 @@ public class LinkedTransferQueueTest ext Thread first = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { - Integer i = SIZE + 1; - q.transfer(i); - assertTrue(!q.contains(i)); + q.transfer(four); + assertTrue(!q.contains(four)); assertEquals(1, q.size()); }}); Thread interruptedThread = newStartedThread( new CheckedInterruptedRunnable() { public void realRun() throws InterruptedException { - while (q.size() == 0) + while (q.isEmpty()) Thread.yield(); - q.transfer(SIZE); + q.transfer(five); }}); while (q.size() < 2) Thread.yield(); assertEquals(2, q.size()); - assertEquals(SIZE + 1, (int) q.poll()); + assertSame(four, q.poll()); first.join(); assertEquals(1, q.size()); interruptedThread.interrupt(); interruptedThread.join(); - assertEquals(0, q.size()); - assertTrue(q.isEmpty()); + checkEmpty(q); } /** @@ -919,7 +836,10 @@ public class LinkedTransferQueueTest ext assertSame(three, q.poll()); }}); - Thread.sleep(SHORT_DELAY_MS); + while (q.isEmpty()) + Thread.yield(); + assertFalse(q.isEmpty()); + assertEquals(1, q.size()); assertTrue(q.offer(three)); assertSame(four, q.poll()); awaitTermination(t, MEDIUM_DELAY_MS); @@ -935,12 +855,15 @@ public class LinkedTransferQueueTest ext Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { - q.transfer(SIZE); + q.transfer(four); checkEmpty(q); }}); - Thread.sleep(SHORT_DELAY_MS); - assertEquals(SIZE, (int) q.take()); + while (q.isEmpty()) + Thread.yield(); + assertFalse(q.isEmpty()); + assertEquals(1, q.size()); + assertSame(four, q.take()); checkEmpty(q); awaitTermination(t, MEDIUM_DELAY_MS); } @@ -980,8 +903,7 @@ public class LinkedTransferQueueTest ext while (! q.hasWaitingConsumer()) Thread.yield(); assertTrue(q.hasWaitingConsumer()); - assertTrue(q.isEmpty()); - assertEquals(q.size(), 0); + checkEmpty(q); assertTrue(q.tryTransfer(hotPotato)); }}); @@ -1003,8 +925,7 @@ public class LinkedTransferQueueTest ext while (! q.hasWaitingConsumer()) Thread.yield(); assertTrue(q.hasWaitingConsumer()); - assertTrue(q.isEmpty()); - assertEquals(q.size(), 0); + checkEmpty(q); assertTrue(q.tryTransfer(hotPotato)); }}); @@ -1014,24 +935,39 @@ public class LinkedTransferQueueTest ext } /** - * tryTransfer waits the amount given if interrupted, and - * throws interrupted exception + * tryTransfer blocks interruptibly if no takers */ public void testTryTransfer5() throws InterruptedException { final LinkedTransferQueue q = new LinkedTransferQueue(); + final CountDownLatch pleaseInterrupt = new CountDownLatch(1); + assertTrue(q.isEmpty()); - Thread toInterrupt = newStartedThread(new CheckedInterruptedRunnable() { + Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { - q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); + 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) {} + assertFalse(Thread.interrupted()); }}); - Thread.sleep(SMALL_DELAY_MS); - toInterrupt.interrupt(); - toInterrupt.join(); + await(pleaseInterrupt); + assertThreadStaysAlive(t); + t.interrupt(); + awaitTermination(t); + checkEmpty(q); } /** - * 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(); @@ -1040,12 +976,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); }}); - checkEmpty(q); - awaitTermination(t, MEDIUM_DELAY_MS); + awaitTermination(t); checkEmpty(q); } @@ -1060,10 +996,11 @@ public class LinkedTransferQueueTest ext Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { assertTrue(q.tryTransfer(five, MEDIUM_DELAY_MS, MILLISECONDS)); - assertTrue(q.isEmpty()); + checkEmpty(q); }}); - Thread.sleep(SHORT_DELAY_MS); + while (q.size() != 2) + Thread.yield(); assertEquals(2, q.size()); assertSame(four, q.poll()); assertSame(five, q.poll()); @@ -1072,14 +1009,16 @@ 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(); assertTrue(q.offer(four)); assertEquals(1, q.size()); - assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS)); + long t0 = System.nanoTime(); + assertFalse(q.tryTransfer(five, timeoutMillis(), MILLISECONDS)); + assertTrue(millisElapsedSince(t0) >= timeoutMillis()); assertEquals(1, q.size()); assertSame(four, q.poll()); assertNull(q.poll()); @@ -1088,7 +1027,7 @@ public class LinkedTransferQueueTest ext private LinkedTransferQueue populatedQueue(int n) { LinkedTransferQueue q = new LinkedTransferQueue(); - assertTrue(q.isEmpty()); + checkEmpty(q); for (int i = 0; i < n; i++) { assertEquals(i, q.size()); assertTrue(q.offer(i));