ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/SynchronousQueueTest.java
(Generate patch)

Comparing jsr166/src/test/tck/SynchronousQueueTest.java (file contents):
Revision 1.36 by jsr166, Sat May 28 13:40:20 2011 UTC vs.
Revision 1.57 by jsr166, Sun May 14 03:48:35 2017 UTC

# Line 6 | Line 6
6   * Pat Fisher, Mike Judd.
7   */
8  
9 import junit.framework.*;
10 import java.util.*;
11 import java.util.concurrent.*;
9   import static java.util.concurrent.TimeUnit.MILLISECONDS;
10 < import java.io.*;
10 >
11 > import java.util.ArrayList;
12 > import java.util.Arrays;
13 > import java.util.Collection;
14 > import java.util.Iterator;
15 > import java.util.NoSuchElementException;
16 > import java.util.concurrent.BlockingQueue;
17 > import java.util.concurrent.CountDownLatch;
18 > import java.util.concurrent.Executors;
19 > import java.util.concurrent.ExecutorService;
20 > import java.util.concurrent.SynchronousQueue;
21 > import java.util.concurrent.ThreadLocalRandom;
22 >
23 > import junit.framework.Test;
24  
25   public class SynchronousQueueTest extends JSR166TestCase {
26  
# Line 27 | Line 37 | public class SynchronousQueueTest extend
37      }
38  
39      public static void main(String[] args) {
40 <        junit.textui.TestRunner.run(suite());
40 >        main(suite(), args);
41      }
42  
43      public static Test suite() {
# Line 50 | Line 60 | public class SynchronousQueueTest extend
60      }
61  
62      /**
53     * offer(null) throws NullPointerException
54     */
55    public void testOfferNull()      { testOfferNull(false); }
56    public void testOfferNull_fair() { testOfferNull(true); }
57    public void testOfferNull(boolean fair) {
58        SynchronousQueue q = new SynchronousQueue(fair);
59        try {
60            q.offer(null);
61            shouldThrow();
62        } catch (NullPointerException success) {}
63    }
64
65    /**
66     * add(null) throws NullPointerException
67     */
68    public void testAddNull()      { testAddNull(false); }
69    public void testAddNull_fair() { testAddNull(true); }
70    public void testAddNull(boolean fair) {
71        SynchronousQueue q = new SynchronousQueue(fair);
72        try {
73            q.add(null);
74            shouldThrow();
75        } catch (NullPointerException success) {}
76    }
77
78    /**
63       * offer fails if no active taker
64       */
65      public void testOffer()      { testOffer(false); }
# Line 100 | Line 84 | public class SynchronousQueueTest extend
84      }
85  
86      /**
103     * addAll(null) throws NullPointerException
104     */
105    public void testAddAll_null()      { testAddAll_null(false); }
106    public void testAddAll_null_fair() { testAddAll_null(true); }
107    public void testAddAll_null(boolean fair) {
108        SynchronousQueue q = new SynchronousQueue(fair);
109        try {
110            q.addAll(null);
111            shouldThrow();
112        } catch (NullPointerException success) {}
113    }
114
115    /**
87       * addAll(this) throws IllegalArgumentException
88       */
89      public void testAddAll_self()      { testAddAll_self(false); }
# Line 126 | Line 97 | public class SynchronousQueueTest extend
97      }
98  
99      /**
129     * addAll of a collection with null elements throws NullPointerException
130     */
131    public void testAddAll_null2()      { testAddAll_null2(false); }
132    public void testAddAll_null2_fair() { testAddAll_null2(true); }
133    public void testAddAll_null2(boolean fair) {
134        SynchronousQueue q = new SynchronousQueue(fair);
135        Collection<Integer> ints = Arrays.asList(new Integer[1]);
136        try {
137            q.addAll(ints);
138            shouldThrow();
139        } catch (NullPointerException success) {}
140    }
141
142    /**
100       * addAll throws ISE if no active taker
101       */
102      public void testAddAll_ISE()      { testAddAll_ISE(false); }
# Line 157 | Line 114 | public class SynchronousQueueTest extend
114      }
115  
116      /**
160     * put(null) throws NPE
161     */
162    public void testPutNull() throws InterruptedException {
163        try {
164            SynchronousQueue q = new SynchronousQueue();
165            q.put(null);
166            shouldThrow();
167        } catch (NullPointerException success) {}
168    }
169
170    /**
117       * put blocks interruptibly if no active taker
118       */
119      public void testBlockingPut()      { testBlockingPut(false); }
# Line 193 | Line 139 | public class SynchronousQueueTest extend
139              }});
140  
141          await(pleaseInterrupt);
142 <        assertThreadStaysAlive(t);
142 >        assertThreadBlocks(t, Thread.State.WAITING);
143          t.interrupt();
144          awaitTermination(t);
145          assertEquals(0, q.remainingCapacity());
# Line 222 | Line 168 | public class SynchronousQueueTest extend
168              }});
169  
170          await(pleaseTake);
171 <        assertEquals(q.remainingCapacity(), 0);
171 >        assertEquals(0, q.remainingCapacity());
172          try { assertSame(one, q.take()); }
173          catch (InterruptedException e) { threadUnexpectedException(e); }
174  
175          await(pleaseInterrupt);
176 <        assertThreadStaysAlive(t);
176 >        assertThreadBlocks(t, Thread.State.WAITING);
177          t.interrupt();
178          awaitTermination(t);
179 <        assertEquals(q.remainingCapacity(), 0);
179 >        assertEquals(0, q.remainingCapacity());
180      }
181  
182      /**
183       * timed offer times out if elements not taken
184       */
185 <    public void testTimedOffer()      { testTimedOffer(false); }
186 <    public void testTimedOffer_fair() { testTimedOffer(true); }
241 <    public void testTimedOffer(boolean fair) {
185 >    public void testTimedOffer() {
186 >        final boolean fair = ThreadLocalRandom.current().nextBoolean();
187          final SynchronousQueue q = new SynchronousQueue(fair);
188          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
189          Thread t = newStartedThread(new CheckedRunnable() {
# Line 251 | Line 196 | public class SynchronousQueueTest extend
196                      q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
197                      shouldThrow();
198                  } catch (InterruptedException success) {}
199 +                assertFalse(Thread.interrupted());
200              }});
201  
202          await(pleaseInterrupt);
203 <        assertThreadStaysAlive(t);
203 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
204          t.interrupt();
205          awaitTermination(t);
206      }
# Line 283 | Line 229 | public class SynchronousQueueTest extend
229      /**
230       * timed poll with nonzero timeout times out if no active putter
231       */
232 <    public void testTimedPoll()      { testTimedPoll(false); }
233 <    public void testTimedPoll_fair() { testTimedPoll(true); }
288 <    public void testTimedPoll(boolean fair) {
232 >    public void testTimedPoll() {
233 >        final boolean fair = ThreadLocalRandom.current().nextBoolean();
234          final SynchronousQueue q = new SynchronousQueue(fair);
235 <        long startTime = System.nanoTime();
235 >        final long startTime = System.nanoTime();
236          try { assertNull(q.poll(timeoutMillis(), MILLISECONDS)); }
237          catch (InterruptedException e) { threadUnexpectedException(e); }
238          assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
# Line 297 | Line 242 | public class SynchronousQueueTest extend
242       * timed poll before a delayed offer times out, returning null;
243       * after offer succeeds; on interruption throws
244       */
245 <    public void testTimedPollWithOffer()      { testTimedPollWithOffer(false); }
246 <    public void testTimedPollWithOffer_fair() { testTimedPollWithOffer(true); }
302 <    public void testTimedPollWithOffer(boolean fair) {
245 >    public void testTimedPollWithOffer() {
246 >        final boolean fair = ThreadLocalRandom.current().nextBoolean();
247          final SynchronousQueue q = new SynchronousQueue(fair);
248          final CountDownLatch pleaseOffer = new CountDownLatch(1);
249          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
# Line 312 | Line 256 | public class SynchronousQueueTest extend
256                  pleaseOffer.countDown();
257                  startTime = System.nanoTime();
258                  assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
315                assertTrue(millisElapsedSince(startTime) < MEDIUM_DELAY_MS);
259  
260                  Thread.currentThread().interrupt();
261                  try {
# Line 327 | Line 270 | public class SynchronousQueueTest extend
270                      shouldThrow();
271                  } catch (InterruptedException success) {}
272                  assertFalse(Thread.interrupted());
273 +
274 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
275              }});
276  
277          await(pleaseOffer);
278          long startTime = System.nanoTime();
279          try { assertTrue(q.offer(zero, LONG_DELAY_MS, MILLISECONDS)); }
280          catch (InterruptedException e) { threadUnexpectedException(e); }
281 <        assertTrue(millisElapsedSince(startTime) < MEDIUM_DELAY_MS);
281 >        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
282  
283          await(pleaseInterrupt);
284 <        assertThreadStaysAlive(t);
284 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
285          t.interrupt();
286          awaitTermination(t);
287      }
# Line 378 | Line 323 | public class SynchronousQueueTest extend
323      }
324  
325      /**
381     * remove(x) returns false
382     */
383    public void testRemoveElement()      { testRemoveElement(false); }
384    public void testRemoveElement_fair() { testRemoveElement(true); }
385    public void testRemoveElement(boolean fair) {
386        final SynchronousQueue q = new SynchronousQueue(fair);
387        assertFalse(q.remove(zero));
388        assertTrue(q.isEmpty());
389    }
390
391    /**
326       * contains returns false
327       */
328      public void testContains()      { testContains(false); }
# Line 456 | Line 390 | public class SynchronousQueueTest extend
390      public void testToArray(boolean fair) {
391          final SynchronousQueue q = new SynchronousQueue(fair);
392          Object[] o = q.toArray();
393 <        assertEquals(o.length, 0);
393 >        assertEquals(0, o.length);
394      }
395  
396      /**
397 <     * toArray(a) is nulled at position 0
397 >     * toArray(Integer array) returns its argument with the first
398 >     * element (if present) nulled out
399       */
400      public void testToArray2()      { testToArray2(false); }
401      public void testToArray2_fair() { testToArray2(true); }
402      public void testToArray2(boolean fair) {
403 <        final SynchronousQueue q = new SynchronousQueue(fair);
404 <        Integer[] ints = new Integer[1];
405 <        assertNull(ints[0]);
403 >        final SynchronousQueue<Integer> q = new SynchronousQueue<>(fair);
404 >        Integer[] a;
405 >
406 >        a = new Integer[0];
407 >        assertSame(a, q.toArray(a));
408 >
409 >        a = new Integer[3];
410 >        Arrays.fill(a, 42);
411 >        assertSame(a, q.toArray(a));
412 >        assertNull(a[0]);
413 >        for (int i = 1; i < a.length; i++)
414 >            assertEquals(42, (int) a[i]);
415      }
416  
417      /**
# Line 478 | Line 422 | public class SynchronousQueueTest extend
422      public void testToArray_null(boolean fair) {
423          final SynchronousQueue q = new SynchronousQueue(fair);
424          try {
425 <            Object o[] = q.toArray(null);
425 >            Object[] o = q.toArray(null);
426              shouldThrow();
427          } catch (NullPointerException success) {}
428      }
# Line 489 | Line 433 | public class SynchronousQueueTest extend
433      public void testIterator()      { testIterator(false); }
434      public void testIterator_fair() { testIterator(true); }
435      public void testIterator(boolean fair) {
436 <        final SynchronousQueue q = new SynchronousQueue(fair);
493 <        Iterator it = q.iterator();
494 <        assertFalse(it.hasNext());
495 <        try {
496 <            Object x = it.next();
497 <            shouldThrow();
498 <        } catch (NoSuchElementException success) {}
436 >        assertIteratorExhausted(new SynchronousQueue(fair).iterator());
437      }
438  
439      /**
# Line 530 | Line 468 | public class SynchronousQueueTest extend
468      public void testOfferInExecutor_fair() { testOfferInExecutor(true); }
469      public void testOfferInExecutor(boolean fair) {
470          final SynchronousQueue q = new SynchronousQueue(fair);
533        ExecutorService executor = Executors.newFixedThreadPool(2);
471          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
472 +        final ExecutorService executor = Executors.newFixedThreadPool(2);
473 +        try (PoolCleaner cleaner = cleaner(executor)) {
474  
475 <        executor.execute(new CheckedRunnable() {
476 <            public void realRun() throws InterruptedException {
477 <                assertFalse(q.offer(one));
478 <                threadsStarted.await();
479 <                assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS));
480 <                assertEquals(0, q.remainingCapacity());
481 <            }});
482 <
483 <        executor.execute(new CheckedRunnable() {
484 <            public void realRun() throws InterruptedException {
485 <                threadsStarted.await();
486 <                assertSame(one, q.take());
487 <            }});
488 <
550 <        joinPool(executor);
475 >            executor.execute(new CheckedRunnable() {
476 >                public void realRun() throws InterruptedException {
477 >                    assertFalse(q.offer(one));
478 >                    threadsStarted.await();
479 >                    assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS));
480 >                    assertEquals(0, q.remainingCapacity());
481 >                }});
482 >
483 >            executor.execute(new CheckedRunnable() {
484 >                public void realRun() throws InterruptedException {
485 >                    threadsStarted.await();
486 >                    assertSame(one, q.take());
487 >                }});
488 >        }
489      }
490  
491      /**
# Line 558 | Line 496 | public class SynchronousQueueTest extend
496      public void testPollInExecutor(boolean fair) {
497          final SynchronousQueue q = new SynchronousQueue(fair);
498          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
499 <        ExecutorService executor = Executors.newFixedThreadPool(2);
500 <        executor.execute(new CheckedRunnable() {
501 <            public void realRun() throws InterruptedException {
502 <                assertNull(q.poll());
503 <                threadsStarted.await();
504 <                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
505 <                assertTrue(q.isEmpty());
506 <            }});
507 <
508 <        executor.execute(new CheckedRunnable() {
509 <            public void realRun() throws InterruptedException {
510 <                threadsStarted.await();
511 <                q.put(one);
512 <            }});
513 <
514 <        joinPool(executor);
499 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
500 >        try (PoolCleaner cleaner = cleaner(executor)) {
501 >            executor.execute(new CheckedRunnable() {
502 >                public void realRun() throws InterruptedException {
503 >                    assertNull(q.poll());
504 >                    threadsStarted.await();
505 >                    assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
506 >                    assertTrue(q.isEmpty());
507 >                }});
508 >
509 >            executor.execute(new CheckedRunnable() {
510 >                public void realRun() throws InterruptedException {
511 >                    threadsStarted.await();
512 >                    q.put(one);
513 >                }});
514 >        }
515      }
516  
517      /**
518       * a deserialized serialized queue is usable
519       */
520 <    public void testSerialization()      { testSerialization(false); }
521 <    public void testSerialization_fair() { testSerialization(true); }
522 <    public void testSerialization(boolean fair) {
523 <        final SynchronousQueue q = new SynchronousQueue(fair);
524 <        final SynchronousQueue r = serialClone(q);
525 <        assertTrue(q != r);
526 <        assertEquals(q.size(), r.size());
527 <        while (!q.isEmpty())
528 <            assertEquals(q.remove(), r.remove());
529 <    }
530 <
531 <    /**
532 <     * drainTo(null) throws NPE
533 <     */
534 <    public void testDrainToNull()      { testDrainToNull(false); }
535 <    public void testDrainToNull_fair() { testDrainToNull(true); }
598 <    public void testDrainToNull(boolean fair) {
599 <        final SynchronousQueue q = new SynchronousQueue(fair);
600 <        try {
601 <            q.drainTo(null);
602 <            shouldThrow();
603 <        } catch (NullPointerException success) {}
604 <    }
605 <
606 <    /**
607 <     * drainTo(this) throws IAE
608 <     */
609 <    public void testDrainToSelf()      { testDrainToSelf(false); }
610 <    public void testDrainToSelf_fair() { testDrainToSelf(true); }
611 <    public void testDrainToSelf(boolean fair) {
612 <        final SynchronousQueue q = new SynchronousQueue(fair);
613 <        try {
614 <            q.drainTo(q);
615 <            shouldThrow();
616 <        } catch (IllegalArgumentException success) {}
520 >    public void testSerialization() {
521 >        final SynchronousQueue x = new SynchronousQueue();
522 >        final SynchronousQueue y = new SynchronousQueue(false);
523 >        final SynchronousQueue z = new SynchronousQueue(true);
524 >        assertSerialEquals(x, y);
525 >        assertNotSerialEquals(x, z);
526 >        SynchronousQueue[] qs = { x, y, z };
527 >        for (SynchronousQueue q : qs) {
528 >            SynchronousQueue clone = serialClone(q);
529 >            assertNotSame(q, clone);
530 >            assertSerialEquals(q, clone);
531 >            assertTrue(clone.isEmpty());
532 >            assertEquals(0, clone.size());
533 >            assertEquals(0, clone.remainingCapacity());
534 >            assertFalse(clone.offer(zero));
535 >        }
536      }
537  
538      /**
# Line 625 | Line 544 | public class SynchronousQueueTest extend
544          final SynchronousQueue q = new SynchronousQueue(fair);
545          ArrayList l = new ArrayList();
546          q.drainTo(l);
547 <        assertEquals(q.size(), 0);
548 <        assertEquals(l.size(), 0);
547 >        assertEquals(0, q.size());
548 >        assertEquals(0, l.size());
549      }
550  
551      /**
# Line 649 | Line 568 | public class SynchronousQueueTest extend
568                  fail("timed out");
569              Thread.yield();
570          }
571 <        assertTrue(l.size() == 1);
571 >        assertEquals(1, l.size());
572          assertSame(one, l.get(0));
573          awaitTermination(t);
574      }
575  
576      /**
658     * drainTo(null, n) throws NullPointerException
659     */
660    public void testDrainToNullN()      { testDrainToNullN(false); }
661    public void testDrainToNullN_fair() { testDrainToNullN(true); }
662    public void testDrainToNullN(boolean fair) {
663        final SynchronousQueue q = new SynchronousQueue(fair);
664        try {
665            q.drainTo(null, 0);
666            shouldThrow();
667        } catch (NullPointerException success) {}
668    }
669
670    /**
671     * drainTo(this, n) throws IllegalArgumentException
672     */
673    public void testDrainToSelfN()      { testDrainToSelfN(false); }
674    public void testDrainToSelfN_fair() { testDrainToSelfN(true); }
675    public void testDrainToSelfN(boolean fair) {
676        final SynchronousQueue q = new SynchronousQueue(fair);
677        try {
678            q.drainTo(q, 0);
679            shouldThrow();
680        } catch (IllegalArgumentException success) {}
681    }
682
683    /**
577       * drainTo(c, n) empties up to n elements of queue into c
578       */
579      public void testDrainToN() throws InterruptedException {
# Line 696 | Line 589 | public class SynchronousQueueTest extend
589              }});
590  
591          ArrayList l = new ArrayList();
592 <        delay(SHORT_DELAY_MS);
593 <        q.drainTo(l, 1);
592 >        int drained;
593 >        while ((drained = q.drainTo(l, 1)) == 0) Thread.yield();
594 >        assertEquals(1, drained);
595          assertEquals(1, l.size());
596 <        q.drainTo(l, 1);
596 >        while ((drained = q.drainTo(l, 1)) == 0) Thread.yield();
597 >        assertEquals(1, drained);
598          assertEquals(2, l.size());
599          assertTrue(l.contains(one));
600          assertTrue(l.contains(two));
# Line 707 | Line 602 | public class SynchronousQueueTest extend
602          awaitTermination(t2);
603      }
604  
605 +    /**
606 +     * remove(null), contains(null) always return false
607 +     */
608 +    public void testNeverContainsNull() {
609 +        Collection<?> q = new SynchronousQueue();
610 +        assertFalse(q.contains(null));
611 +        assertFalse(q.remove(null));
612 +    }
613 +
614   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines