--- jsr166/src/test/tck/SynchronousQueueTest.java 2011/05/30 22:43:20 1.37 +++ jsr166/src/test/tck/SynchronousQueueTest.java 2017/05/13 22:38:09 1.55 @@ -6,9 +6,10 @@ * 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; @@ -17,8 +18,8 @@ import java.util.concurrent.CountDownLat import java.util.concurrent.Executors; import java.util.concurrent.ExecutorService; import java.util.concurrent.SynchronousQueue; -import static java.util.concurrent.TimeUnit.MILLISECONDS; -import java.io.*; + +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() { @@ -137,7 +138,7 @@ public class SynchronousQueueTest extend }}); await(pleaseInterrupt); - assertThreadStaysAlive(t); + assertThreadBlocks(t, Thread.State.WAITING); t.interrupt(); awaitTermination(t); assertEquals(0, q.remainingCapacity()); @@ -166,15 +167,15 @@ public class SynchronousQueueTest extend }}); await(pleaseTake); - assertEquals(q.remainingCapacity(), 0); + assertEquals(0, q.remainingCapacity()); try { assertSame(one, q.take()); } catch (InterruptedException e) { threadUnexpectedException(e); } await(pleaseInterrupt); - assertThreadStaysAlive(t); + assertThreadBlocks(t, Thread.State.WAITING); t.interrupt(); awaitTermination(t); - assertEquals(q.remainingCapacity(), 0); + assertEquals(0, q.remainingCapacity()); } /** @@ -198,7 +199,7 @@ public class SynchronousQueueTest extend }}); await(pleaseInterrupt); - assertThreadStaysAlive(t); + assertThreadBlocks(t, Thread.State.TIMED_WAITING); t.interrupt(); awaitTermination(t); } @@ -256,7 +257,6 @@ 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 { @@ -271,16 +271,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); + assertThreadBlocks(t, Thread.State.TIMED_WAITING); t.interrupt(); awaitTermination(t); } @@ -322,17 +324,6 @@ public class SynchronousQueueTest extend } /** - * remove(x) returns false - */ - public void testRemoveElement() { testRemoveElement(false); } - public void testRemoveElement_fair() { testRemoveElement(true); } - public void testRemoveElement(boolean fair) { - final SynchronousQueue q = new SynchronousQueue(fair); - assertFalse(q.remove(zero)); - assertTrue(q.isEmpty()); - } - - /** * contains returns false */ public void testContains() { testContains(false); } @@ -400,18 +391,28 @@ public class SynchronousQueueTest extend public void testToArray(boolean fair) { final SynchronousQueue q = new SynchronousQueue(fair); Object[] o = q.toArray(); - assertEquals(o.length, 0); + assertEquals(0, o.length); } /** - * toArray(a) is nulled at position 0 + * toArray(Integer 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[] ints = new Integer[1]; - assertNull(ints[0]); + final SynchronousQueue q = new SynchronousQueue<>(fair); + Integer[] a; + + a = new Integer[0]; + assertSame(a, q.toArray(a)); + + a = new Integer[3]; + Arrays.fill(a, 42); + assertSame(a, q.toArray(a)); + assertNull(a[0]); + for (int i = 1; i < a.length; i++) + assertEquals(42, (int) a[i]); } /** @@ -422,7 +423,7 @@ public class SynchronousQueueTest extend public void testToArray_null(boolean fair) { final SynchronousQueue q = new SynchronousQueue(fair); try { - Object o[] = q.toArray(null); + Object[] o = q.toArray(null); shouldThrow(); } catch (NullPointerException success) {} } @@ -433,13 +434,7 @@ 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()); } /** @@ -474,24 +469,24 @@ public class SynchronousQueueTest extend public void testOfferInExecutor_fair() { testOfferInExecutor(true); } public void testOfferInExecutor(boolean fair) { final SynchronousQueue q = new SynchronousQueue(fair); - ExecutorService executor = Executors.newFixedThreadPool(2); 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)); + assertEquals(0, q.remainingCapacity()); + }}); + + executor.execute(new CheckedRunnable() { + public void realRun() throws InterruptedException { + threadsStarted.await(); + assertSame(one, q.take()); + }}); + } } /** @@ -502,36 +497,43 @@ public class SynchronousQueueTest extend public void testPollInExecutor(boolean 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 */ - public void testSerialization() { testSerialization(false); } - public void testSerialization_fair() { testSerialization(true); } - public void testSerialization(boolean fair) { - final SynchronousQueue q = new SynchronousQueue(fair); - final SynchronousQueue r = serialClone(q); - assertTrue(q != r); - assertEquals(q.size(), r.size()); - while (!q.isEmpty()) - assertEquals(q.remove(), r.remove()); + public void testSerialization() { + 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); + assertNotSame(q, clone); + assertSerialEquals(q, clone); + assertTrue(clone.isEmpty()); + assertEquals(0, clone.size()); + assertEquals(0, clone.remainingCapacity()); + assertFalse(clone.offer(zero)); + } } /** @@ -543,8 +545,8 @@ public class SynchronousQueueTest extend final SynchronousQueue q = new SynchronousQueue(fair); ArrayList l = new ArrayList(); q.drainTo(l); - assertEquals(q.size(), 0); - assertEquals(l.size(), 0); + assertEquals(0, q.size()); + assertEquals(0, l.size()); } /** @@ -567,7 +569,7 @@ public class SynchronousQueueTest extend fail("timed out"); Thread.yield(); } - assertTrue(l.size() == 1); + assertEquals(1, l.size()); assertSame(one, l.get(0)); awaitTermination(t); } @@ -588,10 +590,12 @@ public class SynchronousQueueTest extend }}); ArrayList l = new ArrayList(); - delay(SHORT_DELAY_MS); - q.drainTo(l, 1); + int drained; + while ((drained = q.drainTo(l, 1)) == 0) Thread.yield(); + assertEquals(1, drained); assertEquals(1, l.size()); - q.drainTo(l, 1); + while ((drained = q.drainTo(l, 1)) == 0) Thread.yield(); + assertEquals(1, drained); assertEquals(2, l.size()); assertTrue(l.contains(one)); assertTrue(l.contains(two)); @@ -599,4 +603,13 @@ public class SynchronousQueueTest extend awaitTermination(t2); } + /** + * remove(null), contains(null) always return false + */ + public void testNeverContainsNull() { + Collection q = new SynchronousQueue(); + assertFalse(q.contains(null)); + assertFalse(q.remove(null)); + } + }