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.59 by jsr166, Sun May 14 04:14:10 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 213 | Line 159 | public class SynchronousQueueTest extend
159                  pleaseTake.countDown();
160                  q.put(one);
161  
162 +                Thread.currentThread().interrupt();
163 +                try {
164 +                    q.put(99);
165 +                    shouldThrow();
166 +                } catch (InterruptedException success) {}
167 +                assertFalse(Thread.interrupted());
168 +
169                  pleaseInterrupt.countDown();
170                  try {
171                      q.put(99);
# Line 222 | Line 175 | public class SynchronousQueueTest extend
175              }});
176  
177          await(pleaseTake);
178 <        assertEquals(q.remainingCapacity(), 0);
178 >        assertEquals(0, q.remainingCapacity());
179          try { assertSame(one, q.take()); }
180          catch (InterruptedException e) { threadUnexpectedException(e); }
181  
182          await(pleaseInterrupt);
183 <        assertThreadStaysAlive(t);
183 >        assertThreadBlocks(t, Thread.State.WAITING);
184          t.interrupt();
185          awaitTermination(t);
186 <        assertEquals(q.remainingCapacity(), 0);
186 >        assertEquals(0, q.remainingCapacity());
187      }
188  
189      /**
190       * timed offer times out if elements not taken
191       */
192 <    public void testTimedOffer()      { testTimedOffer(false); }
193 <    public void testTimedOffer_fair() { testTimedOffer(true); }
241 <    public void testTimedOffer(boolean fair) {
192 >    public void testTimedOffer() {
193 >        final boolean fair = ThreadLocalRandom.current().nextBoolean();
194          final SynchronousQueue q = new SynchronousQueue(fair);
195          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
196          Thread t = newStartedThread(new CheckedRunnable() {
# Line 246 | Line 198 | public class SynchronousQueueTest extend
198                  long startTime = System.nanoTime();
199                  assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS));
200                  assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
201 +
202 +                Thread.currentThread().interrupt();
203 +                try {
204 +                    q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
205 +                    shouldThrow();
206 +                } catch (InterruptedException success) {}
207 +                assertFalse(Thread.interrupted());
208 +
209                  pleaseInterrupt.countDown();
210                  try {
211                      q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
212                      shouldThrow();
213                  } catch (InterruptedException success) {}
214 +                assertFalse(Thread.interrupted());
215              }});
216  
217          await(pleaseInterrupt);
218 <        assertThreadStaysAlive(t);
218 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
219          t.interrupt();
220          awaitTermination(t);
221      }
# Line 283 | Line 244 | public class SynchronousQueueTest extend
244      /**
245       * timed poll with nonzero timeout times out if no active putter
246       */
247 <    public void testTimedPoll()      { testTimedPoll(false); }
248 <    public void testTimedPoll_fair() { testTimedPoll(true); }
288 <    public void testTimedPoll(boolean fair) {
247 >    public void testTimedPoll() {
248 >        final boolean fair = ThreadLocalRandom.current().nextBoolean();
249          final SynchronousQueue q = new SynchronousQueue(fair);
250 <        long startTime = System.nanoTime();
250 >        final long startTime = System.nanoTime();
251          try { assertNull(q.poll(timeoutMillis(), MILLISECONDS)); }
252          catch (InterruptedException e) { threadUnexpectedException(e); }
253          assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
# Line 297 | Line 257 | public class SynchronousQueueTest extend
257       * timed poll before a delayed offer times out, returning null;
258       * after offer succeeds; on interruption throws
259       */
260 <    public void testTimedPollWithOffer()      { testTimedPollWithOffer(false); }
261 <    public void testTimedPollWithOffer_fair() { testTimedPollWithOffer(true); }
302 <    public void testTimedPollWithOffer(boolean fair) {
260 >    public void testTimedPollWithOffer() {
261 >        final boolean fair = ThreadLocalRandom.current().nextBoolean();
262          final SynchronousQueue q = new SynchronousQueue(fair);
263          final CountDownLatch pleaseOffer = new CountDownLatch(1);
264          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
# Line 312 | Line 271 | public class SynchronousQueueTest extend
271                  pleaseOffer.countDown();
272                  startTime = System.nanoTime();
273                  assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
315                assertTrue(millisElapsedSince(startTime) < MEDIUM_DELAY_MS);
274  
275                  Thread.currentThread().interrupt();
276                  try {
# Line 327 | Line 285 | public class SynchronousQueueTest extend
285                      shouldThrow();
286                  } catch (InterruptedException success) {}
287                  assertFalse(Thread.interrupted());
288 +
289 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
290              }});
291  
292          await(pleaseOffer);
293          long startTime = System.nanoTime();
294          try { assertTrue(q.offer(zero, LONG_DELAY_MS, MILLISECONDS)); }
295          catch (InterruptedException e) { threadUnexpectedException(e); }
296 <        assertTrue(millisElapsedSince(startTime) < MEDIUM_DELAY_MS);
296 >        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
297  
298          await(pleaseInterrupt);
299 <        assertThreadStaysAlive(t);
299 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
300          t.interrupt();
301          awaitTermination(t);
302      }
# Line 378 | Line 338 | public class SynchronousQueueTest extend
338      }
339  
340      /**
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    /**
341       * contains returns false
342       */
343      public void testContains()      { testContains(false); }
# Line 456 | Line 405 | public class SynchronousQueueTest extend
405      public void testToArray(boolean fair) {
406          final SynchronousQueue q = new SynchronousQueue(fair);
407          Object[] o = q.toArray();
408 <        assertEquals(o.length, 0);
408 >        assertEquals(0, o.length);
409      }
410  
411      /**
412 <     * toArray(a) is nulled at position 0
412 >     * toArray(Integer array) returns its argument with the first
413 >     * element (if present) nulled out
414       */
415      public void testToArray2()      { testToArray2(false); }
416      public void testToArray2_fair() { testToArray2(true); }
417      public void testToArray2(boolean fair) {
418 <        final SynchronousQueue q = new SynchronousQueue(fair);
419 <        Integer[] ints = new Integer[1];
420 <        assertNull(ints[0]);
418 >        final SynchronousQueue<Integer> q = new SynchronousQueue<>(fair);
419 >        Integer[] a;
420 >
421 >        a = new Integer[0];
422 >        assertSame(a, q.toArray(a));
423 >
424 >        a = new Integer[3];
425 >        Arrays.fill(a, 42);
426 >        assertSame(a, q.toArray(a));
427 >        assertNull(a[0]);
428 >        for (int i = 1; i < a.length; i++)
429 >            assertEquals(42, (int) a[i]);
430      }
431  
432      /**
# Line 478 | Line 437 | public class SynchronousQueueTest extend
437      public void testToArray_null(boolean fair) {
438          final SynchronousQueue q = new SynchronousQueue(fair);
439          try {
440 <            Object o[] = q.toArray(null);
440 >            Object[] o = q.toArray(null);
441              shouldThrow();
442          } catch (NullPointerException success) {}
443      }
# Line 489 | Line 448 | public class SynchronousQueueTest extend
448      public void testIterator()      { testIterator(false); }
449      public void testIterator_fair() { testIterator(true); }
450      public void testIterator(boolean fair) {
451 <        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) {}
451 >        assertIteratorExhausted(new SynchronousQueue(fair).iterator());
452      }
453  
454      /**
# Line 530 | Line 483 | public class SynchronousQueueTest extend
483      public void testOfferInExecutor_fair() { testOfferInExecutor(true); }
484      public void testOfferInExecutor(boolean fair) {
485          final SynchronousQueue q = new SynchronousQueue(fair);
533        ExecutorService executor = Executors.newFixedThreadPool(2);
486          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
487 +        final ExecutorService executor = Executors.newFixedThreadPool(2);
488 +        try (PoolCleaner cleaner = cleaner(executor)) {
489  
490 <        executor.execute(new CheckedRunnable() {
491 <            public void realRun() throws InterruptedException {
492 <                assertFalse(q.offer(one));
493 <                threadsStarted.await();
494 <                assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS));
495 <                assertEquals(0, q.remainingCapacity());
496 <            }});
497 <
498 <        executor.execute(new CheckedRunnable() {
499 <            public void realRun() throws InterruptedException {
500 <                threadsStarted.await();
501 <                assertSame(one, q.take());
502 <            }});
503 <
550 <        joinPool(executor);
490 >            executor.execute(new CheckedRunnable() {
491 >                public void realRun() throws InterruptedException {
492 >                    assertFalse(q.offer(one));
493 >                    threadsStarted.await();
494 >                    assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS));
495 >                    assertEquals(0, q.remainingCapacity());
496 >                }});
497 >
498 >            executor.execute(new CheckedRunnable() {
499 >                public void realRun() throws InterruptedException {
500 >                    threadsStarted.await();
501 >                    assertSame(one, q.take());
502 >                }});
503 >        }
504      }
505  
506      /**
# Line 558 | Line 511 | public class SynchronousQueueTest extend
511      public void testPollInExecutor(boolean fair) {
512          final SynchronousQueue q = new SynchronousQueue(fair);
513          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
514 <        ExecutorService executor = Executors.newFixedThreadPool(2);
515 <        executor.execute(new CheckedRunnable() {
516 <            public void realRun() throws InterruptedException {
517 <                assertNull(q.poll());
518 <                threadsStarted.await();
519 <                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
520 <                assertTrue(q.isEmpty());
521 <            }});
522 <
523 <        executor.execute(new CheckedRunnable() {
524 <            public void realRun() throws InterruptedException {
525 <                threadsStarted.await();
526 <                q.put(one);
527 <            }});
528 <
529 <        joinPool(executor);
514 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
515 >        try (PoolCleaner cleaner = cleaner(executor)) {
516 >            executor.execute(new CheckedRunnable() {
517 >                public void realRun() throws InterruptedException {
518 >                    assertNull(q.poll());
519 >                    threadsStarted.await();
520 >                    assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
521 >                    assertTrue(q.isEmpty());
522 >                }});
523 >
524 >            executor.execute(new CheckedRunnable() {
525 >                public void realRun() throws InterruptedException {
526 >                    threadsStarted.await();
527 >                    q.put(one);
528 >                }});
529 >        }
530      }
531  
532      /**
533       * a deserialized serialized queue is usable
534       */
535 <    public void testSerialization()      { testSerialization(false); }
536 <    public void testSerialization_fair() { testSerialization(true); }
537 <    public void testSerialization(boolean fair) {
538 <        final SynchronousQueue q = new SynchronousQueue(fair);
539 <        final SynchronousQueue r = serialClone(q);
540 <        assertTrue(q != r);
541 <        assertEquals(q.size(), r.size());
542 <        while (!q.isEmpty())
543 <            assertEquals(q.remove(), r.remove());
544 <    }
545 <
546 <    /**
547 <     * drainTo(null) throws NPE
548 <     */
549 <    public void testDrainToNull()      { testDrainToNull(false); }
550 <    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) {}
535 >    public void testSerialization() {
536 >        final SynchronousQueue x = new SynchronousQueue();
537 >        final SynchronousQueue y = new SynchronousQueue(false);
538 >        final SynchronousQueue z = new SynchronousQueue(true);
539 >        assertSerialEquals(x, y);
540 >        assertNotSerialEquals(x, z);
541 >        SynchronousQueue[] qs = { x, y, z };
542 >        for (SynchronousQueue q : qs) {
543 >            SynchronousQueue clone = serialClone(q);
544 >            assertNotSame(q, clone);
545 >            assertSerialEquals(q, clone);
546 >            assertTrue(clone.isEmpty());
547 >            assertEquals(0, clone.size());
548 >            assertEquals(0, clone.remainingCapacity());
549 >            assertFalse(clone.offer(zero));
550 >        }
551      }
552  
553      /**
# Line 625 | Line 559 | public class SynchronousQueueTest extend
559          final SynchronousQueue q = new SynchronousQueue(fair);
560          ArrayList l = new ArrayList();
561          q.drainTo(l);
562 <        assertEquals(q.size(), 0);
563 <        assertEquals(l.size(), 0);
562 >        assertEquals(0, q.size());
563 >        assertEquals(0, l.size());
564      }
565  
566      /**
# Line 649 | Line 583 | public class SynchronousQueueTest extend
583                  fail("timed out");
584              Thread.yield();
585          }
586 <        assertTrue(l.size() == 1);
586 >        assertEquals(1, l.size());
587          assertSame(one, l.get(0));
588          awaitTermination(t);
589      }
590  
591      /**
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    /**
592       * drainTo(c, n) empties up to n elements of queue into c
593       */
594      public void testDrainToN() throws InterruptedException {
# Line 696 | Line 604 | public class SynchronousQueueTest extend
604              }});
605  
606          ArrayList l = new ArrayList();
607 <        delay(SHORT_DELAY_MS);
608 <        q.drainTo(l, 1);
607 >        int drained;
608 >        while ((drained = q.drainTo(l, 1)) == 0) Thread.yield();
609 >        assertEquals(1, drained);
610          assertEquals(1, l.size());
611 <        q.drainTo(l, 1);
611 >        while ((drained = q.drainTo(l, 1)) == 0) Thread.yield();
612 >        assertEquals(1, drained);
613          assertEquals(2, l.size());
614          assertTrue(l.contains(one));
615          assertTrue(l.contains(two));
# Line 707 | Line 617 | public class SynchronousQueueTest extend
617          awaitTermination(t2);
618      }
619  
620 +    /**
621 +     * remove(null), contains(null) always return false
622 +     */
623 +    public void testNeverContainsNull() {
624 +        Collection<?> q = new SynchronousQueue();
625 +        assertFalse(q.contains(null));
626 +        assertFalse(q.remove(null));
627 +    }
628 +
629   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines