--- jsr166/src/test/tck/SynchronousQueueTest.java 2011/12/08 19:09:58 1.41 +++ jsr166/src/test/tck/SynchronousQueueTest.java 2021/01/27 01:57:24 1.69 @@ -6,19 +6,20 @@ * Pat Fisher, Mike Judd. */ -import junit.framework.*; -import java.util.Arrays; +import static java.util.concurrent.TimeUnit.MILLISECONDS; + import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.Iterator; import java.util.NoSuchElementException; -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.SynchronousQueue; -import static java.util.concurrent.TimeUnit.MILLISECONDS; + +import junit.framework.Test; public class SynchronousQueueTest extends JSR166TestCase { @@ -35,7 +36,7 @@ public class SynchronousQueueTest extend } public static void main(String[] args) { - junit.textui.TestRunner.run(suite()); + main(suite(), args); } public static Test suite() { @@ -50,10 +51,10 @@ public class SynchronousQueueTest extend public void testEmptyFull() { testEmptyFull(false); } public void testEmptyFull_fair() { testEmptyFull(true); } public void testEmptyFull(boolean fair) { - final SynchronousQueue q = new SynchronousQueue(fair); + final SynchronousQueue q = new SynchronousQueue<>(fair); assertTrue(q.isEmpty()); - assertEquals(0, q.size()); - assertEquals(0, q.remainingCapacity()); + mustEqual(0, q.size()); + mustEqual(0, q.remainingCapacity()); assertFalse(q.offer(zero)); } @@ -63,7 +64,7 @@ public class SynchronousQueueTest extend public void testOffer() { testOffer(false); } public void testOffer_fair() { testOffer(true); } public void testOffer(boolean fair) { - SynchronousQueue q = new SynchronousQueue(fair); + SynchronousQueue q = new SynchronousQueue<>(fair); assertFalse(q.offer(one)); } @@ -73,8 +74,8 @@ public class SynchronousQueueTest extend public void testAdd() { testAdd(false); } public void testAdd_fair() { testAdd(true); } public void testAdd(boolean fair) { - SynchronousQueue q = new SynchronousQueue(fair); - assertEquals(0, q.remainingCapacity()); + SynchronousQueue q = new SynchronousQueue<>(fair); + mustEqual(0, q.remainingCapacity()); try { q.add(one); shouldThrow(); @@ -87,24 +88,22 @@ public class SynchronousQueueTest extend public void testAddAll_self() { testAddAll_self(false); } public void testAddAll_self_fair() { testAddAll_self(true); } public void testAddAll_self(boolean fair) { - SynchronousQueue q = new SynchronousQueue(fair); + SynchronousQueue q = new SynchronousQueue<>(fair); try { q.addAll(q); shouldThrow(); } catch (IllegalArgumentException success) {} } - /** - * addAll throws ISE if no active taker + /**S + * addAll throws IllegalStateException if no active taker */ public void testAddAll_ISE() { testAddAll_ISE(false); } public void testAddAll_ISE_fair() { testAddAll_ISE(true); } public void testAddAll_ISE(boolean fair) { - SynchronousQueue q = new SynchronousQueue(fair); - Integer[] ints = new Integer[1]; - for (int i = 0; i < ints.length; i++) - ints[i] = i; - Collection coll = Arrays.asList(ints); + SynchronousQueue q = new SynchronousQueue<>(fair); + Item[] items = seqItems(1); + Collection coll = Arrays.asList(items); try { q.addAll(coll); shouldThrow(); @@ -117,30 +116,30 @@ public class SynchronousQueueTest extend public void testBlockingPut() { testBlockingPut(false); } public void testBlockingPut_fair() { testBlockingPut(true); } public void testBlockingPut(boolean fair) { - final SynchronousQueue q = new SynchronousQueue(fair); + final SynchronousQueue q = new SynchronousQueue<>(fair); final CountDownLatch pleaseInterrupt = new CountDownLatch(1); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { Thread.currentThread().interrupt(); try { - q.put(99); + q.put(ninetynine); shouldThrow(); } catch (InterruptedException success) {} assertFalse(Thread.interrupted()); pleaseInterrupt.countDown(); try { - q.put(99); + q.put(ninetynine); shouldThrow(); } catch (InterruptedException success) {} assertFalse(Thread.interrupted()); }}); await(pleaseInterrupt); - assertThreadStaysAlive(t); + if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); t.interrupt(); awaitTermination(t); - assertEquals(0, q.remainingCapacity()); + mustEqual(0, q.remainingCapacity()); } /** @@ -149,7 +148,7 @@ public class SynchronousQueueTest extend public void testPutWithTake() { testPutWithTake(false); } public void testPutWithTake_fair() { testPutWithTake(true); } public void testPutWithTake(boolean fair) { - final SynchronousQueue q = new SynchronousQueue(fair); + final SynchronousQueue q = new SynchronousQueue<>(fair); final CountDownLatch pleaseTake = new CountDownLatch(1); final CountDownLatch pleaseInterrupt = new CountDownLatch(1); Thread t = newStartedThread(new CheckedRunnable() { @@ -157,48 +156,64 @@ public class SynchronousQueueTest extend pleaseTake.countDown(); q.put(one); + Thread.currentThread().interrupt(); + try { + q.put(ninetynine); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + pleaseInterrupt.countDown(); try { - q.put(99); + q.put(ninetynine); shouldThrow(); } catch (InterruptedException success) {} assertFalse(Thread.interrupted()); }}); await(pleaseTake); - assertEquals(0, q.remainingCapacity()); + mustEqual(0, q.remainingCapacity()); try { assertSame(one, q.take()); } catch (InterruptedException e) { threadUnexpectedException(e); } await(pleaseInterrupt); - assertThreadStaysAlive(t); + if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); t.interrupt(); awaitTermination(t); - assertEquals(0, q.remainingCapacity()); + mustEqual(0, q.remainingCapacity()); } /** * timed offer times out if elements not taken */ - public void testTimedOffer() { testTimedOffer(false); } - public void testTimedOffer_fair() { testTimedOffer(true); } - public void testTimedOffer(boolean fair) { - final SynchronousQueue q = new SynchronousQueue(fair); + public void testTimedOffer() { + final boolean fair = randomBoolean(); + final SynchronousQueue q = new SynchronousQueue<>(fair); final CountDownLatch pleaseInterrupt = new CountDownLatch(1); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { long startTime = System.nanoTime(); - assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS)); + + assertFalse(q.offer(zero, timeoutMillis(), MILLISECONDS)); assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); + + Thread.currentThread().interrupt(); + try { + q.offer(one, randomTimeout(), randomTimeUnit()); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + pleaseInterrupt.countDown(); try { - q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); + q.offer(two, LONGER_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); }}); await(pleaseInterrupt); - assertThreadStaysAlive(t); + if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); t.interrupt(); awaitTermination(t); } @@ -209,7 +224,7 @@ public class SynchronousQueueTest extend public void testPoll() { testPoll(false); } public void testPoll_fair() { testPoll(true); } public void testPoll(boolean fair) { - final SynchronousQueue q = new SynchronousQueue(fair); + final SynchronousQueue q = new SynchronousQueue<>(fair); assertNull(q.poll()); } @@ -219,7 +234,7 @@ public class SynchronousQueueTest extend public void testTimedPoll0() { testTimedPoll0(false); } public void testTimedPoll0_fair() { testTimedPoll0(true); } public void testTimedPoll0(boolean fair) { - final SynchronousQueue q = new SynchronousQueue(fair); + final SynchronousQueue q = new SynchronousQueue<>(fair); try { assertNull(q.poll(0, MILLISECONDS)); } catch (InterruptedException e) { threadUnexpectedException(e); } } @@ -227,11 +242,10 @@ public class SynchronousQueueTest extend /** * timed poll with nonzero timeout times out if no active putter */ - public void testTimedPoll() { testTimedPoll(false); } - public void testTimedPoll_fair() { testTimedPoll(true); } - public void testTimedPoll(boolean fair) { - final SynchronousQueue q = new SynchronousQueue(fair); - long startTime = System.nanoTime(); + public void testTimedPoll() { + final boolean fair = randomBoolean(); + final SynchronousQueue q = new SynchronousQueue<>(fair); + final long startTime = System.nanoTime(); try { assertNull(q.poll(timeoutMillis(), MILLISECONDS)); } catch (InterruptedException e) { threadUnexpectedException(e); } assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); @@ -241,10 +255,9 @@ public class SynchronousQueueTest extend * timed poll before a delayed offer times out, returning null; * after offer succeeds; on interruption throws */ - public void testTimedPollWithOffer() { testTimedPollWithOffer(false); } - public void testTimedPollWithOffer_fair() { testTimedPollWithOffer(true); } - public void testTimedPollWithOffer(boolean fair) { - final SynchronousQueue q = new SynchronousQueue(fair); + public void testTimedPollWithOffer() { + final boolean fair = randomBoolean(); + final SynchronousQueue q = new SynchronousQueue<>(fair); final CountDownLatch pleaseOffer = new CountDownLatch(1); final CountDownLatch pleaseInterrupt = new CountDownLatch(1); Thread t = newStartedThread(new CheckedRunnable() { @@ -256,11 +269,10 @@ public class SynchronousQueueTest extend pleaseOffer.countDown(); startTime = System.nanoTime(); assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); - assertTrue(millisElapsedSince(startTime) < MEDIUM_DELAY_MS); Thread.currentThread().interrupt(); try { - q.poll(LONG_DELAY_MS, MILLISECONDS); + q.poll(randomTimeout(), randomTimeUnit()); shouldThrow(); } catch (InterruptedException success) {} assertFalse(Thread.interrupted()); @@ -271,16 +283,18 @@ public class SynchronousQueueTest extend shouldThrow(); } catch (InterruptedException success) {} assertFalse(Thread.interrupted()); + + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); }}); await(pleaseOffer); long startTime = System.nanoTime(); try { assertTrue(q.offer(zero, LONG_DELAY_MS, MILLISECONDS)); } catch (InterruptedException e) { threadUnexpectedException(e); } - assertTrue(millisElapsedSince(startTime) < MEDIUM_DELAY_MS); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); await(pleaseInterrupt); - assertThreadStaysAlive(t); + if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); t.interrupt(); awaitTermination(t); } @@ -291,7 +305,7 @@ public class SynchronousQueueTest extend public void testPeek() { testPeek(false); } public void testPeek_fair() { testPeek(true); } public void testPeek(boolean fair) { - final SynchronousQueue q = new SynchronousQueue(fair); + final SynchronousQueue q = new SynchronousQueue<>(fair); assertNull(q.peek()); } @@ -301,7 +315,7 @@ public class SynchronousQueueTest extend public void testElement() { testElement(false); } public void testElement_fair() { testElement(true); } public void testElement(boolean fair) { - final SynchronousQueue q = new SynchronousQueue(fair); + final SynchronousQueue q = new SynchronousQueue<>(fair); try { q.element(); shouldThrow(); @@ -314,7 +328,7 @@ public class SynchronousQueueTest extend public void testRemove() { testRemove(false); } public void testRemove_fair() { testRemove(true); } public void testRemove(boolean fair) { - final SynchronousQueue q = new SynchronousQueue(fair); + final SynchronousQueue q = new SynchronousQueue<>(fair); try { q.remove(); shouldThrow(); @@ -327,7 +341,7 @@ public class SynchronousQueueTest extend public void testContains() { testContains(false); } public void testContains_fair() { testContains(true); } public void testContains(boolean fair) { - final SynchronousQueue q = new SynchronousQueue(fair); + final SynchronousQueue q = new SynchronousQueue<>(fair); assertFalse(q.contains(zero)); } @@ -337,7 +351,7 @@ public class SynchronousQueueTest extend public void testClear() { testClear(false); } public void testClear_fair() { testClear(true); } public void testClear(boolean fair) { - final SynchronousQueue q = new SynchronousQueue(fair); + final SynchronousQueue q = new SynchronousQueue<>(fair); q.clear(); assertTrue(q.isEmpty()); } @@ -348,11 +362,11 @@ public class SynchronousQueueTest extend public void testContainsAll() { testContainsAll(false); } public void testContainsAll_fair() { testContainsAll(true); } public void testContainsAll(boolean fair) { - final SynchronousQueue q = new SynchronousQueue(fair); - Integer[] empty = new Integer[0]; + final SynchronousQueue q = new SynchronousQueue<>(fair); + Item[] empty = new Item[0]; assertTrue(q.containsAll(Arrays.asList(empty))); - Integer[] ints = new Integer[1]; ints[0] = zero; - assertFalse(q.containsAll(Arrays.asList(ints))); + Item[] items = new Item[1]; items[0] = zero; + assertFalse(q.containsAll(Arrays.asList(items))); } /** @@ -361,11 +375,11 @@ public class SynchronousQueueTest extend public void testRetainAll() { testRetainAll(false); } public void testRetainAll_fair() { testRetainAll(true); } public void testRetainAll(boolean fair) { - final SynchronousQueue q = new SynchronousQueue(fair); - Integer[] empty = new Integer[0]; + final SynchronousQueue q = new SynchronousQueue<>(fair); + Item[] empty = new Item[0]; assertFalse(q.retainAll(Arrays.asList(empty))); - Integer[] ints = new Integer[1]; ints[0] = zero; - assertFalse(q.retainAll(Arrays.asList(ints))); + Item[] items = new Item[1]; items[0] = zero; + assertFalse(q.retainAll(Arrays.asList(items))); } /** @@ -374,11 +388,11 @@ public class SynchronousQueueTest extend public void testRemoveAll() { testRemoveAll(false); } public void testRemoveAll_fair() { testRemoveAll(true); } public void testRemoveAll(boolean fair) { - final SynchronousQueue q = new SynchronousQueue(fair); - Integer[] empty = new Integer[0]; + final SynchronousQueue q = new SynchronousQueue<>(fair); + Item[] empty = new Item[0]; assertFalse(q.removeAll(Arrays.asList(empty))); - Integer[] ints = new Integer[1]; ints[0] = zero; - assertFalse(q.containsAll(Arrays.asList(ints))); + Item[] items = new Item[1]; items[0] = zero; + assertFalse(q.containsAll(Arrays.asList(items))); } /** @@ -387,31 +401,30 @@ public class SynchronousQueueTest extend public void testToArray() { testToArray(false); } public void testToArray_fair() { testToArray(true); } public void testToArray(boolean fair) { - final SynchronousQueue q = new SynchronousQueue(fair); + final SynchronousQueue q = new SynchronousQueue<>(fair); Object[] o = q.toArray(); - assertEquals(0, o.length); + mustEqual(0, o.length); } /** - * toArray(Integer array) returns its argument with the first + * toArray(Item array) returns its argument with the first * element (if present) nulled out */ public void testToArray2() { testToArray2(false); } public void testToArray2_fair() { testToArray2(true); } public void testToArray2(boolean fair) { - final SynchronousQueue q - = new SynchronousQueue(fair); - Integer[] a; + final SynchronousQueue q = new SynchronousQueue<>(fair); + Item[] a; - a = new Integer[0]; + a = new Item[0]; assertSame(a, q.toArray(a)); - a = new Integer[3]; - Arrays.fill(a, 42); + a = new Item[3]; + Arrays.fill(a, fortytwo); assertSame(a, q.toArray(a)); assertNull(a[0]); for (int i = 1; i < a.length; i++) - assertEquals(42, (int) a[i]); + mustEqual(42, a[i]); } /** @@ -420,9 +433,9 @@ public class SynchronousQueueTest extend public void testToArray_null() { testToArray_null(false); } public void testToArray_null_fair() { testToArray_null(true); } public void testToArray_null(boolean fair) { - final SynchronousQueue q = new SynchronousQueue(fair); + final SynchronousQueue q = new SynchronousQueue<>(fair); try { - Object o[] = q.toArray(null); + Object[] unused = q.toArray((Object[])null); shouldThrow(); } catch (NullPointerException success) {} } @@ -433,23 +446,17 @@ public class SynchronousQueueTest extend public void testIterator() { testIterator(false); } public void testIterator_fair() { testIterator(true); } public void testIterator(boolean fair) { - final SynchronousQueue q = new SynchronousQueue(fair); - Iterator it = q.iterator(); - assertFalse(it.hasNext()); - try { - Object x = it.next(); - shouldThrow(); - } catch (NoSuchElementException success) {} + assertIteratorExhausted(new SynchronousQueue(fair).iterator()); } /** - * iterator remove throws ISE + * iterator remove throws IllegalStateException */ public void testIteratorRemove() { testIteratorRemove(false); } public void testIteratorRemove_fair() { testIteratorRemove(true); } public void testIteratorRemove(boolean fair) { - final SynchronousQueue q = new SynchronousQueue(fair); - Iterator it = q.iterator(); + final SynchronousQueue q = new SynchronousQueue<>(fair); + Iterator it = q.iterator(); try { it.remove(); shouldThrow(); @@ -462,7 +469,7 @@ public class SynchronousQueueTest extend public void testToString() { testToString(false); } public void testToString_fair() { testToString(true); } public void testToString(boolean fair) { - final SynchronousQueue q = new SynchronousQueue(fair); + final SynchronousQueue q = new SynchronousQueue<>(fair); String s = q.toString(); assertNotNull(s); } @@ -473,25 +480,25 @@ public class SynchronousQueueTest extend public void testOfferInExecutor() { testOfferInExecutor(false); } public void testOfferInExecutor_fair() { testOfferInExecutor(true); } public void testOfferInExecutor(boolean fair) { - final SynchronousQueue q = new SynchronousQueue(fair); - ExecutorService executor = Executors.newFixedThreadPool(2); + final SynchronousQueue q = new SynchronousQueue<>(fair); final CheckedBarrier threadsStarted = new CheckedBarrier(2); + final ExecutorService executor = Executors.newFixedThreadPool(2); + try (PoolCleaner cleaner = cleaner(executor)) { - executor.execute(new CheckedRunnable() { - public void realRun() throws InterruptedException { - assertFalse(q.offer(one)); - threadsStarted.await(); - assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS)); - assertEquals(0, q.remainingCapacity()); - }}); - - executor.execute(new CheckedRunnable() { - public void realRun() throws InterruptedException { - threadsStarted.await(); - assertSame(one, q.take()); - }}); - - joinPool(executor); + executor.execute(new CheckedRunnable() { + public void realRun() throws InterruptedException { + assertFalse(q.offer(one)); + threadsStarted.await(); + assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS)); + mustEqual(0, q.remainingCapacity()); + }}); + + executor.execute(new CheckedRunnable() { + public void realRun() throws InterruptedException { + threadsStarted.await(); + assertSame(one, q.take()); + }}); + } } /** @@ -500,43 +507,45 @@ public class SynchronousQueueTest extend public void testPollInExecutor() { testPollInExecutor(false); } public void testPollInExecutor_fair() { testPollInExecutor(true); } public void testPollInExecutor(boolean fair) { - final SynchronousQueue q = new SynchronousQueue(fair); + final SynchronousQueue q = new SynchronousQueue<>(fair); final CheckedBarrier threadsStarted = new CheckedBarrier(2); - ExecutorService executor = Executors.newFixedThreadPool(2); - executor.execute(new CheckedRunnable() { - public void realRun() throws InterruptedException { - assertNull(q.poll()); - threadsStarted.await(); - assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); - assertTrue(q.isEmpty()); - }}); - - executor.execute(new CheckedRunnable() { - public void realRun() throws InterruptedException { - threadsStarted.await(); - q.put(one); - }}); - - joinPool(executor); + final ExecutorService executor = Executors.newFixedThreadPool(2); + try (PoolCleaner cleaner = cleaner(executor)) { + executor.execute(new CheckedRunnable() { + public void realRun() throws InterruptedException { + assertNull(q.poll()); + threadsStarted.await(); + assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); + assertTrue(q.isEmpty()); + }}); + + executor.execute(new CheckedRunnable() { + public void realRun() throws InterruptedException { + threadsStarted.await(); + q.put(one); + }}); + } } /** - * a deserialized serialized queue is usable + * a deserialized/reserialized queue is usable */ public void testSerialization() { - final SynchronousQueue x = new SynchronousQueue(); - final SynchronousQueue y = new SynchronousQueue(false); - final SynchronousQueue z = new SynchronousQueue(true); + final SynchronousQueue x = new SynchronousQueue<>(); + final SynchronousQueue y = new SynchronousQueue<>(false); + final SynchronousQueue z = new SynchronousQueue<>(true); assertSerialEquals(x, y); assertNotSerialEquals(x, z); - SynchronousQueue[] qs = { x, y, z }; - for (SynchronousQueue q : qs) { - SynchronousQueue clone = serialClone(q); + SynchronousQueue[] rqs = { x, y, z }; + @SuppressWarnings("unchecked") + SynchronousQueue[] qs = (SynchronousQueue[])rqs; + for (SynchronousQueue q : qs) { + SynchronousQueue clone = serialClone(q); assertNotSame(q, clone); assertSerialEquals(q, clone); assertTrue(clone.isEmpty()); - assertEquals(0, clone.size()); - assertEquals(0, clone.remainingCapacity()); + mustEqual(0, clone.size()); + mustEqual(0, clone.remainingCapacity()); assertFalse(clone.offer(zero)); } } @@ -547,11 +556,11 @@ public class SynchronousQueueTest extend public void testDrainTo() { testDrainTo(false); } public void testDrainTo_fair() { testDrainTo(true); } public void testDrainTo(boolean fair) { - final SynchronousQueue q = new SynchronousQueue(fair); - ArrayList l = new ArrayList(); + final SynchronousQueue q = new SynchronousQueue<>(fair); + ArrayList l = new ArrayList<>(); q.drainTo(l); - assertEquals(0, q.size()); - assertEquals(0, l.size()); + mustEqual(0, q.size()); + mustEqual(0, l.size()); } /** @@ -560,13 +569,13 @@ public class SynchronousQueueTest extend public void testDrainToWithActivePut() { testDrainToWithActivePut(false); } public void testDrainToWithActivePut_fair() { testDrainToWithActivePut(true); } public void testDrainToWithActivePut(boolean fair) { - final SynchronousQueue q = new SynchronousQueue(fair); + final SynchronousQueue q = new SynchronousQueue<>(fair); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { q.put(one); }}); - ArrayList l = new ArrayList(); + ArrayList l = new ArrayList<>(); long startTime = System.nanoTime(); while (l.isEmpty()) { q.drainTo(l); @@ -574,7 +583,7 @@ public class SynchronousQueueTest extend fail("timed out"); Thread.yield(); } - assertTrue(l.size() == 1); + mustEqual(1, l.size()); assertSame(one, l.get(0)); awaitTermination(t); } @@ -583,7 +592,7 @@ public class SynchronousQueueTest extend * drainTo(c, n) empties up to n elements of queue into c */ public void testDrainToN() throws InterruptedException { - final SynchronousQueue q = new SynchronousQueue(); + final SynchronousQueue q = new SynchronousQueue<>(); Thread t1 = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { q.put(one); @@ -594,16 +603,27 @@ public class SynchronousQueueTest extend q.put(two); }}); - ArrayList l = new ArrayList(); - delay(SHORT_DELAY_MS); - q.drainTo(l, 1); - assertEquals(1, l.size()); - q.drainTo(l, 1); - assertEquals(2, l.size()); + ArrayList l = new ArrayList<>(); + int drained; + while ((drained = q.drainTo(l, 1)) == 0) Thread.yield(); + mustEqual(1, drained); + mustEqual(1, l.size()); + while ((drained = q.drainTo(l, 1)) == 0) Thread.yield(); + mustEqual(1, drained); + mustEqual(2, l.size()); assertTrue(l.contains(one)); assertTrue(l.contains(two)); awaitTermination(t1); awaitTermination(t2); } + /** + * remove(null), contains(null) always return false + */ + public void testNeverContainsNull() { + Collection q = new SynchronousQueue<>(); + assertFalse(q.contains(null)); + assertFalse(q.remove(null)); + } + }