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.51 by jsr166, Wed Jan 4 06:09:58 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 >
22 > import junit.framework.Test;
23  
24   public class SynchronousQueueTest extends JSR166TestCase {
25  
# Line 27 | Line 36 | public class SynchronousQueueTest extend
36      }
37  
38      public static void main(String[] args) {
39 <        junit.textui.TestRunner.run(suite());
39 >        main(suite(), args);
40      }
41  
42      public static Test suite() {
# Line 50 | Line 59 | public class SynchronousQueueTest extend
59      }
60  
61      /**
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    /**
62       * offer fails if no active taker
63       */
64      public void testOffer()      { testOffer(false); }
# Line 100 | Line 83 | public class SynchronousQueueTest extend
83      }
84  
85      /**
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    /**
86       * addAll(this) throws IllegalArgumentException
87       */
88      public void testAddAll_self()      { testAddAll_self(false); }
# Line 126 | Line 96 | public class SynchronousQueueTest extend
96      }
97  
98      /**
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    /**
99       * addAll throws ISE if no active taker
100       */
101      public void testAddAll_ISE()      { testAddAll_ISE(false); }
# Line 157 | Line 113 | public class SynchronousQueueTest extend
113      }
114  
115      /**
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    /**
116       * put blocks interruptibly if no active taker
117       */
118      public void testBlockingPut()      { testBlockingPut(false); }
# Line 222 | Line 167 | public class SynchronousQueueTest extend
167              }});
168  
169          await(pleaseTake);
170 <        assertEquals(q.remainingCapacity(), 0);
170 >        assertEquals(0, q.remainingCapacity());
171          try { assertSame(one, q.take()); }
172          catch (InterruptedException e) { threadUnexpectedException(e); }
173  
# Line 230 | Line 175 | public class SynchronousQueueTest extend
175          assertThreadStaysAlive(t);
176          t.interrupt();
177          awaitTermination(t);
178 <        assertEquals(q.remainingCapacity(), 0);
178 >        assertEquals(0, q.remainingCapacity());
179      }
180  
181      /**
# Line 312 | Line 257 | public class SynchronousQueueTest extend
257                  pleaseOffer.countDown();
258                  startTime = System.nanoTime();
259                  assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
315                assertTrue(millisElapsedSince(startTime) < MEDIUM_DELAY_MS);
260  
261                  Thread.currentThread().interrupt();
262                  try {
# Line 327 | Line 271 | public class SynchronousQueueTest extend
271                      shouldThrow();
272                  } catch (InterruptedException success) {}
273                  assertFalse(Thread.interrupted());
274 +
275 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
276              }});
277  
278          await(pleaseOffer);
279          long startTime = System.nanoTime();
280          try { assertTrue(q.offer(zero, LONG_DELAY_MS, MILLISECONDS)); }
281          catch (InterruptedException e) { threadUnexpectedException(e); }
282 <        assertTrue(millisElapsedSince(startTime) < MEDIUM_DELAY_MS);
282 >        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
283  
284          await(pleaseInterrupt);
285          assertThreadStaysAlive(t);
# Line 378 | Line 324 | public class SynchronousQueueTest extend
324      }
325  
326      /**
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    /**
327       * contains returns false
328       */
329      public void testContains()      { testContains(false); }
# Line 456 | Line 391 | public class SynchronousQueueTest extend
391      public void testToArray(boolean fair) {
392          final SynchronousQueue q = new SynchronousQueue(fair);
393          Object[] o = q.toArray();
394 <        assertEquals(o.length, 0);
394 >        assertEquals(0, o.length);
395      }
396  
397      /**
398 <     * toArray(a) is nulled at position 0
398 >     * toArray(Integer array) returns its argument with the first
399 >     * element (if present) nulled out
400       */
401      public void testToArray2()      { testToArray2(false); }
402      public void testToArray2_fair() { testToArray2(true); }
403      public void testToArray2(boolean fair) {
404 <        final SynchronousQueue q = new SynchronousQueue(fair);
405 <        Integer[] ints = new Integer[1];
406 <        assertNull(ints[0]);
404 >        final SynchronousQueue<Integer> q = new SynchronousQueue<>(fair);
405 >        Integer[] a;
406 >
407 >        a = new Integer[0];
408 >        assertSame(a, q.toArray(a));
409 >
410 >        a = new Integer[3];
411 >        Arrays.fill(a, 42);
412 >        assertSame(a, q.toArray(a));
413 >        assertNull(a[0]);
414 >        for (int i = 1; i < a.length; i++)
415 >            assertEquals(42, (int) a[i]);
416      }
417  
418      /**
# Line 478 | Line 423 | public class SynchronousQueueTest extend
423      public void testToArray_null(boolean fair) {
424          final SynchronousQueue q = new SynchronousQueue(fair);
425          try {
426 <            Object o[] = q.toArray(null);
426 >            Object[] o = q.toArray(null);
427              shouldThrow();
428          } catch (NullPointerException success) {}
429      }
# Line 489 | Line 434 | public class SynchronousQueueTest extend
434      public void testIterator()      { testIterator(false); }
435      public void testIterator_fair() { testIterator(true); }
436      public void testIterator(boolean fair) {
437 <        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) {}
437 >        assertIteratorExhausted(new SynchronousQueue(fair).iterator());
438      }
439  
440      /**
# Line 530 | Line 469 | public class SynchronousQueueTest extend
469      public void testOfferInExecutor_fair() { testOfferInExecutor(true); }
470      public void testOfferInExecutor(boolean fair) {
471          final SynchronousQueue q = new SynchronousQueue(fair);
533        ExecutorService executor = Executors.newFixedThreadPool(2);
472          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
473 +        final ExecutorService executor = Executors.newFixedThreadPool(2);
474 +        try (PoolCleaner cleaner = cleaner(executor)) {
475  
476 <        executor.execute(new CheckedRunnable() {
477 <            public void realRun() throws InterruptedException {
478 <                assertFalse(q.offer(one));
479 <                threadsStarted.await();
480 <                assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS));
481 <                assertEquals(0, q.remainingCapacity());
482 <            }});
483 <
484 <        executor.execute(new CheckedRunnable() {
485 <            public void realRun() throws InterruptedException {
486 <                threadsStarted.await();
487 <                assertSame(one, q.take());
488 <            }});
489 <
550 <        joinPool(executor);
476 >            executor.execute(new CheckedRunnable() {
477 >                public void realRun() throws InterruptedException {
478 >                    assertFalse(q.offer(one));
479 >                    threadsStarted.await();
480 >                    assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS));
481 >                    assertEquals(0, q.remainingCapacity());
482 >                }});
483 >
484 >            executor.execute(new CheckedRunnable() {
485 >                public void realRun() throws InterruptedException {
486 >                    threadsStarted.await();
487 >                    assertSame(one, q.take());
488 >                }});
489 >        }
490      }
491  
492      /**
# Line 558 | Line 497 | public class SynchronousQueueTest extend
497      public void testPollInExecutor(boolean fair) {
498          final SynchronousQueue q = new SynchronousQueue(fair);
499          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
500 <        ExecutorService executor = Executors.newFixedThreadPool(2);
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 <        joinPool(executor);
500 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
501 >        try (PoolCleaner cleaner = cleaner(executor)) {
502 >            executor.execute(new CheckedRunnable() {
503 >                public void realRun() throws InterruptedException {
504 >                    assertNull(q.poll());
505 >                    threadsStarted.await();
506 >                    assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
507 >                    assertTrue(q.isEmpty());
508 >                }});
509 >
510 >            executor.execute(new CheckedRunnable() {
511 >                public void realRun() throws InterruptedException {
512 >                    threadsStarted.await();
513 >                    q.put(one);
514 >                }});
515 >        }
516      }
517  
518      /**
519       * a deserialized serialized queue is usable
520       */
521 <    public void testSerialization()      { testSerialization(false); }
522 <    public void testSerialization_fair() { testSerialization(true); }
523 <    public void testSerialization(boolean fair) {
524 <        final SynchronousQueue q = new SynchronousQueue(fair);
525 <        final SynchronousQueue r = serialClone(q);
526 <        assertTrue(q != r);
527 <        assertEquals(q.size(), r.size());
528 <        while (!q.isEmpty())
529 <            assertEquals(q.remove(), r.remove());
530 <    }
531 <
532 <    /**
533 <     * drainTo(null) throws NPE
534 <     */
535 <    public void testDrainToNull()      { testDrainToNull(false); }
536 <    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) {}
521 >    public void testSerialization() {
522 >        final SynchronousQueue x = new SynchronousQueue();
523 >        final SynchronousQueue y = new SynchronousQueue(false);
524 >        final SynchronousQueue z = new SynchronousQueue(true);
525 >        assertSerialEquals(x, y);
526 >        assertNotSerialEquals(x, z);
527 >        SynchronousQueue[] qs = { x, y, z };
528 >        for (SynchronousQueue q : qs) {
529 >            SynchronousQueue clone = serialClone(q);
530 >            assertNotSame(q, clone);
531 >            assertSerialEquals(q, clone);
532 >            assertTrue(clone.isEmpty());
533 >            assertEquals(0, clone.size());
534 >            assertEquals(0, clone.remainingCapacity());
535 >            assertFalse(clone.offer(zero));
536 >        }
537      }
538  
539      /**
# Line 625 | Line 545 | public class SynchronousQueueTest extend
545          final SynchronousQueue q = new SynchronousQueue(fair);
546          ArrayList l = new ArrayList();
547          q.drainTo(l);
548 <        assertEquals(q.size(), 0);
549 <        assertEquals(l.size(), 0);
548 >        assertEquals(0, q.size());
549 >        assertEquals(0, l.size());
550      }
551  
552      /**
# Line 655 | Line 575 | public class SynchronousQueueTest extend
575      }
576  
577      /**
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    /**
578       * drainTo(c, n) empties up to n elements of queue into c
579       */
580      public void testDrainToN() throws InterruptedException {
# Line 696 | Line 590 | public class SynchronousQueueTest extend
590              }});
591  
592          ArrayList l = new ArrayList();
593 <        delay(SHORT_DELAY_MS);
594 <        q.drainTo(l, 1);
593 >        int drained;
594 >        while ((drained = q.drainTo(l, 1)) == 0) Thread.yield();
595 >        assertEquals(1, drained);
596          assertEquals(1, l.size());
597 <        q.drainTo(l, 1);
597 >        while ((drained = q.drainTo(l, 1)) == 0) Thread.yield();
598 >        assertEquals(1, drained);
599          assertEquals(2, l.size());
600          assertTrue(l.contains(one));
601          assertTrue(l.contains(two));
# Line 707 | Line 603 | public class SynchronousQueueTest extend
603          awaitTermination(t2);
604      }
605  
606 +    /**
607 +     * remove(null), contains(null) always return false
608 +     */
609 +    public void testNeverContainsNull() {
610 +        Collection<?> q = new SynchronousQueue();
611 +        assertFalse(q.contains(null));
612 +        assertFalse(q.remove(null));
613 +    }
614 +
615   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines