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

Comparing jsr166/src/test/tck/LinkedTransferQueueTest.java (file contents):
Revision 1.47 by jsr166, Mon May 30 22:43:20 2011 UTC vs.
Revision 1.65 by jsr166, Tue Oct 6 00:36:55 2015 UTC

# Line 5 | Line 5
5   * Other contributors include John Vint
6   */
7  
8 < import java.io.BufferedInputStream;
9 < import java.io.BufferedOutputStream;
10 < import java.io.ByteArrayInputStream;
11 < import java.io.ByteArrayOutputStream;
12 < import java.io.ObjectInputStream;
13 < import java.io.ObjectOutputStream;
8 > import static java.util.concurrent.TimeUnit.MILLISECONDS;
9 >
10   import java.util.ArrayList;
11   import java.util.Arrays;
12   import java.util.Collection;
13   import java.util.Iterator;
14   import java.util.List;
15   import java.util.NoSuchElementException;
16 + import java.util.Queue;
17   import java.util.concurrent.BlockingQueue;
18   import java.util.concurrent.CountDownLatch;
19   import java.util.concurrent.Executors;
20   import java.util.concurrent.ExecutorService;
21   import java.util.concurrent.LinkedTransferQueue;
22 < import static java.util.concurrent.TimeUnit.MILLISECONDS;
26 < import static java.util.concurrent.TimeUnit.NANOSECONDS;
22 >
23   import junit.framework.Test;
28 import junit.framework.TestSuite;
24  
25   @SuppressWarnings({"unchecked", "rawtypes"})
26   public class LinkedTransferQueueTest extends JSR166TestCase {
27 +    static class Implementation implements CollectionImplementation {
28 +        public Class<?> klazz() { return LinkedTransferQueue.class; }
29 +        public Collection emptyCollection() { return new LinkedTransferQueue(); }
30 +        public Object makeElement(int i) { return i; }
31 +        public boolean isConcurrent() { return true; }
32 +        public boolean permitsNulls() { return false; }
33 +    }
34  
35      public static class Generic extends BlockingQueueTest {
36          protected BlockingQueue emptyCollection() {
# Line 37 | Line 39 | public class LinkedTransferQueueTest ext
39      }
40  
41      public static void main(String[] args) {
42 <        junit.textui.TestRunner.run(suite());
42 >        main(suite(), args);
43      }
44  
45      public static Test suite() {
46          return newTestSuite(LinkedTransferQueueTest.class,
47 <                            new Generic().testSuite());
47 >                            new Generic().testSuite(),
48 >                            CollectionTest.testSuite(new Implementation()));
49      }
50  
51      /**
# Line 83 | Line 86 | public class LinkedTransferQueueTest ext
86       */
87      public void testConstructor4() {
88          Integer[] ints = new Integer[SIZE];
89 <        for (int i = 0; i < SIZE-1; ++i)
89 >        for (int i = 0; i < SIZE - 1; ++i)
90              ints[i] = i;
91          Collection<Integer> elements = Arrays.asList(ints);
92          try {
# Line 120 | Line 123 | public class LinkedTransferQueueTest ext
123       * remainingCapacity() always returns Integer.MAX_VALUE
124       */
125      public void testRemainingCapacity() {
126 <        LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
126 >        BlockingQueue q = populatedQueue(SIZE);
127          for (int i = 0; i < SIZE; ++i) {
128              assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
129              assertEquals(SIZE - i, q.size());
130 <            q.remove();
130 >            assertEquals(i, q.remove());
131          }
132          for (int i = 0; i < SIZE; ++i) {
133              assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
134              assertEquals(i, q.size());
135 <            q.add(i);
135 >            assertTrue(q.add(i));
136          }
137      }
138  
# Line 137 | Line 140 | public class LinkedTransferQueueTest ext
140       * addAll(this) throws IllegalArgumentException
141       */
142      public void testAddAllSelf() {
143 +        LinkedTransferQueue q = populatedQueue(SIZE);
144          try {
141            LinkedTransferQueue q = populatedQueue(SIZE);
145              q.addAll(q);
146              shouldThrow();
147          } catch (IllegalArgumentException success) {}
# Line 149 | Line 152 | public class LinkedTransferQueueTest ext
152       * NullPointerException after possibly adding some elements
153       */
154      public void testAddAll3() {
155 +        LinkedTransferQueue q = new LinkedTransferQueue();
156 +        Integer[] ints = new Integer[SIZE];
157 +        for (int i = 0; i < SIZE - 1; ++i)
158 +            ints[i] = i;
159          try {
153            LinkedTransferQueue q = new LinkedTransferQueue();
154            Integer[] ints = new Integer[SIZE];
155            for (int i = 0; i < SIZE - 1; ++i) {
156                ints[i] = i;
157            }
160              q.addAll(Arrays.asList(ints));
161              shouldThrow();
162          } catch (NullPointerException success) {}
# Line 183 | Line 185 | public class LinkedTransferQueueTest ext
185      public void testPut() {
186          LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
187          for (int i = 0; i < SIZE; ++i) {
188 <            assertEquals(q.size(), i);
188 >            assertEquals(i, q.size());
189              q.put(i);
190              assertTrue(q.contains(i));
191          }
# Line 281 | Line 283 | public class LinkedTransferQueueTest ext
283          final CountDownLatch aboutToWait = new CountDownLatch(1);
284          Thread t = newStartedThread(new CheckedRunnable() {
285              public void realRun() throws InterruptedException {
286 +                long startTime = System.nanoTime();
287                  for (int i = 0; i < SIZE; ++i) {
285                    long t0 = System.nanoTime();
288                      assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
287                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
289                  }
289                long t0 = System.nanoTime();
290                  aboutToWait.countDown();
291                  try {
292 <                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
292 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
293                      shouldThrow();
294                  } catch (InterruptedException success) {
295 <                    assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
295 >                    assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
296                  }
297              }});
298  
299          aboutToWait.await();
300 <        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
300 >        waitForThreadToEnterWaitState(t, LONG_DELAY_MS);
301          t.interrupt();
302 <        awaitTermination(t, MEDIUM_DELAY_MS);
302 >        awaitTermination(t);
303          checkEmpty(q);
304      }
305  
# Line 313 | Line 313 | public class LinkedTransferQueueTest ext
313              public void realRun() throws InterruptedException {
314                  Thread.currentThread().interrupt();
315                  for (int i = 0; i < SIZE; ++i) {
316 <                    long t0 = System.nanoTime();
316 >                    long startTime = System.nanoTime();
317                      assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
318 <                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
318 >                    assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
319                  }
320                  try {
321                      q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
# Line 374 | Line 374 | public class LinkedTransferQueueTest ext
374      }
375  
376      /**
377     * remove(x) removes x and returns true if present
378     */
379    public void testRemoveElement() throws InterruptedException {
380        LinkedTransferQueue q = populatedQueue(SIZE);
381        for (int i = 1; i < SIZE; i+=2) {
382            assertTrue(q.contains(i));
383            assertTrue(q.remove(i));
384            assertFalse(q.contains(i));
385            assertTrue(q.contains(i-1));
386        }
387        for (int i = 0; i < SIZE; i+=2) {
388            assertTrue(q.contains(i));
389            assertTrue(q.remove(i));
390            assertFalse(q.contains(i));
391            assertFalse(q.remove(i+1));
392            assertFalse(q.contains(i+1));
393        }
394        checkEmpty(q);
395    }
396
397    /**
377       * An add following remove(x) succeeds
378       */
379      public void testRemoveElementAndAdd() throws InterruptedException {
# Line 526 | Line 505 | public class LinkedTransferQueueTest ext
505      public void testIterator() throws InterruptedException {
506          LinkedTransferQueue q = populatedQueue(SIZE);
507          Iterator it = q.iterator();
508 <        int i = 0;
509 <        while (it.hasNext()) {
510 <            assertEquals(it.next(), i++);
532 <        }
508 >        int i;
509 >        for (i = 0; it.hasNext(); i++)
510 >            assertTrue(q.contains(it.next()));
511          assertEquals(i, SIZE);
512 +        assertIteratorExhausted(it);
513 +
514 +        it = q.iterator();
515 +        for (i = 0; it.hasNext(); i++)
516 +            assertEquals(it.next(), q.take());
517 +        assertEquals(i, SIZE);
518 +        assertIteratorExhausted(it);
519 +    }
520 +
521 +    /**
522 +     * iterator of empty collection has no elements
523 +     */
524 +    public void testEmptyIterator() {
525 +        assertIteratorExhausted(new LinkedTransferQueue().iterator());
526      }
527  
528      /**
# Line 602 | Line 594 | public class LinkedTransferQueueTest ext
594      public void testOfferInExecutor() {
595          final LinkedTransferQueue q = new LinkedTransferQueue();
596          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
597 <        ExecutorService executor = Executors.newFixedThreadPool(2);
597 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
598 >        try (PoolCleaner cleaner = cleaner(executor)) {
599  
600 <        executor.execute(new CheckedRunnable() {
601 <            public void realRun() throws InterruptedException {
602 <                threadsStarted.await();
603 <                assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS));
604 <            }});
612 <
613 <        executor.execute(new CheckedRunnable() {
614 <            public void realRun() throws InterruptedException {
615 <                threadsStarted.await();
616 <                assertSame(one, q.take());
617 <                checkEmpty(q);
618 <            }});
600 >            executor.execute(new CheckedRunnable() {
601 >                public void realRun() throws InterruptedException {
602 >                    threadsStarted.await();
603 >                    assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS));
604 >                }});
605  
606 <        joinPool(executor);
606 >            executor.execute(new CheckedRunnable() {
607 >                public void realRun() throws InterruptedException {
608 >                    threadsStarted.await();
609 >                    assertSame(one, q.take());
610 >                    checkEmpty(q);
611 >                }});
612 >        }
613      }
614  
615      /**
# Line 626 | Line 618 | public class LinkedTransferQueueTest ext
618      public void testPollInExecutor() {
619          final LinkedTransferQueue q = new LinkedTransferQueue();
620          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
621 <        ExecutorService executor = Executors.newFixedThreadPool(2);
621 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
622 >        try (PoolCleaner cleaner = cleaner(executor)) {
623  
624 <        executor.execute(new CheckedRunnable() {
625 <            public void realRun() throws InterruptedException {
626 <                assertNull(q.poll());
627 <                threadsStarted.await();
628 <                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
629 <                checkEmpty(q);
630 <            }});
638 <
639 <        executor.execute(new CheckedRunnable() {
640 <            public void realRun() throws InterruptedException {
641 <                threadsStarted.await();
642 <                q.put(one);
643 <            }});
624 >            executor.execute(new CheckedRunnable() {
625 >                public void realRun() throws InterruptedException {
626 >                    assertNull(q.poll());
627 >                    threadsStarted.await();
628 >                    assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
629 >                    checkEmpty(q);
630 >                }});
631  
632 <        joinPool(executor);
632 >            executor.execute(new CheckedRunnable() {
633 >                public void realRun() throws InterruptedException {
634 >                    threadsStarted.await();
635 >                    q.put(one);
636 >                }});
637 >        }
638      }
639  
640      /**
641       * A deserialized serialized queue has same elements in same order
642       */
643      public void testSerialization() throws Exception {
644 <        LinkedTransferQueue q = populatedQueue(SIZE);
644 >        Queue x = populatedQueue(SIZE);
645 >        Queue y = serialClone(x);
646  
647 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
648 <        ObjectOutputStream out
649 <            = new ObjectOutputStream(new BufferedOutputStream(bout));
650 <        out.writeObject(q);
651 <        out.close();
652 <
653 <        ByteArrayInputStream bin
661 <            = new ByteArrayInputStream(bout.toByteArray());
662 <        ObjectInputStream in
663 <            = new ObjectInputStream(new BufferedInputStream(bin));
664 <        LinkedTransferQueue r = (LinkedTransferQueue) in.readObject();
665 <
666 <        assertEquals(q.size(), r.size());
667 <        assertEquals(q.toString(), r.toString());
668 <        assertTrue(Arrays.equals(q.toArray(), r.toArray()));
669 <        while (!q.isEmpty()) {
670 <            assertEquals(q.remove(), r.remove());
647 >        assertNotSame(y, x);
648 >        assertEquals(x.size(), y.size());
649 >        assertEquals(x.toString(), y.toString());
650 >        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
651 >        while (!x.isEmpty()) {
652 >            assertFalse(y.isEmpty());
653 >            assertEquals(x.remove(), y.remove());
654          }
655 +        assertTrue(y.isEmpty());
656      }
657  
658      /**
# Line 678 | Line 662 | public class LinkedTransferQueueTest ext
662          LinkedTransferQueue q = populatedQueue(SIZE);
663          ArrayList l = new ArrayList();
664          q.drainTo(l);
665 <        assertEquals(q.size(), 0);
666 <        assertEquals(l.size(), SIZE);
665 >        assertEquals(0, q.size());
666 >        assertEquals(SIZE, l.size());
667          for (int i = 0; i < SIZE; ++i) {
668 <            assertEquals(l.get(i), i);
668 >            assertEquals(i, l.get(i));
669          }
670          q.add(zero);
671          q.add(one);
# Line 690 | Line 674 | public class LinkedTransferQueueTest ext
674          assertTrue(q.contains(one));
675          l.clear();
676          q.drainTo(l);
677 <        assertEquals(q.size(), 0);
678 <        assertEquals(l.size(), 2);
677 >        assertEquals(0, q.size());
678 >        assertEquals(2, l.size());
679          for (int i = 0; i < 2; ++i) {
680 <            assertEquals(l.get(i), i);
680 >            assertEquals(i, l.get(i));
681          }
682      }
683  
# Line 709 | Line 693 | public class LinkedTransferQueueTest ext
693          ArrayList l = new ArrayList();
694          q.drainTo(l);
695          assertTrue(l.size() >= SIZE);
696 <        for (int i = 0; i < SIZE; ++i) {
697 <            assertEquals(l.get(i), i);
714 <        }
696 >        for (int i = 0; i < SIZE; ++i)
697 >            assertEquals(i, l.get(i));
698          awaitTermination(t, MEDIUM_DELAY_MS);
699          assertTrue(q.size() + l.size() >= SIZE);
700      }
# Line 728 | Line 711 | public class LinkedTransferQueueTest ext
711              ArrayList l = new ArrayList();
712              q.drainTo(l, i);
713              int k = (i < SIZE) ? i : SIZE;
714 <            assertEquals(l.size(), k);
715 <            assertEquals(q.size(), SIZE - k);
716 <            for (int j = 0; j < k; ++j) {
717 <                assertEquals(l.get(j), j);
718 <            }
736 <            while (q.poll() != null)
737 <                ;
714 >            assertEquals(k, l.size());
715 >            assertEquals(SIZE - k, q.size());
716 >            for (int j = 0; j < k; ++j)
717 >                assertEquals(j, l.get(j));
718 >            do {} while (q.poll() != null);
719          }
720      }
721  
# Line 744 | Line 725 | public class LinkedTransferQueueTest ext
725       */
726      public void testWaitingConsumer() throws InterruptedException {
727          final LinkedTransferQueue q = new LinkedTransferQueue();
728 <        assertEquals(q.getWaitingConsumerCount(), 0);
728 >        assertEquals(0, q.getWaitingConsumerCount());
729          assertFalse(q.hasWaitingConsumer());
730          final CountDownLatch threadStarted = new CountDownLatch(1);
731  
# Line 752 | Line 733 | public class LinkedTransferQueueTest ext
733              public void realRun() throws InterruptedException {
734                  threadStarted.countDown();
735                  assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
736 <                assertEquals(q.getWaitingConsumerCount(), 0);
736 >                assertEquals(0, q.getWaitingConsumerCount());
737                  assertFalse(q.hasWaitingConsumer());
738              }});
739  
740          threadStarted.await();
741          waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
742 <        assertEquals(q.getWaitingConsumerCount(), 1);
742 >        assertEquals(1, q.getWaitingConsumerCount());
743          assertTrue(q.hasWaitingConsumer());
744  
745          assertTrue(q.offer(one));
746 <        assertEquals(q.getWaitingConsumerCount(), 0);
746 >        assertEquals(0, q.getWaitingConsumerCount());
747          assertFalse(q.hasWaitingConsumer());
748  
749          awaitTermination(t, MEDIUM_DELAY_MS);
# Line 886 | Line 867 | public class LinkedTransferQueueTest ext
867       * tryTransfer(null) throws NullPointerException
868       */
869      public void testTryTransfer1() {
870 +        final LinkedTransferQueue q = new LinkedTransferQueue();
871          try {
890            final LinkedTransferQueue q = new LinkedTransferQueue();
872              q.tryTransfer(null);
873              shouldThrow();
874          } catch (NullPointerException success) {}
# Line 988 | Line 969 | public class LinkedTransferQueueTest ext
969  
970          Thread t = newStartedThread(new CheckedRunnable() {
971              public void realRun() throws InterruptedException {
972 <                long t0 = System.nanoTime();
972 >                long startTime = System.nanoTime();
973                  assertFalse(q.tryTransfer(new Object(),
974                                            timeoutMillis(), MILLISECONDS));
975 <                assertTrue(millisElapsedSince(t0) >= timeoutMillis());
975 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
976                  checkEmpty(q);
977              }});
978  
# Line 1030 | Line 1011 | public class LinkedTransferQueueTest ext
1011          final LinkedTransferQueue q = new LinkedTransferQueue();
1012          assertTrue(q.offer(four));
1013          assertEquals(1, q.size());
1014 <        long t0 = System.nanoTime();
1014 >        long startTime = System.nanoTime();
1015          assertFalse(q.tryTransfer(five, timeoutMillis(), MILLISECONDS));
1016 <        assertTrue(millisElapsedSince(t0) >= timeoutMillis());
1016 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
1017          assertEquals(1, q.size());
1018          assertSame(four, q.poll());
1019          assertNull(q.poll());
# Line 1050 | Line 1031 | public class LinkedTransferQueueTest ext
1031          assertFalse(q.isEmpty());
1032          return q;
1033      }
1034 +
1035 +    /**
1036 +     * remove(null), contains(null) always return false
1037 +     */
1038 +    public void testNeverContainsNull() {
1039 +        Collection<?>[] qs = {
1040 +            new LinkedTransferQueue<Object>(),
1041 +            populatedQueue(2),
1042 +        };
1043 +
1044 +        for (Collection<?> q : qs) {
1045 +            assertFalse(q.contains(null));
1046 +            assertFalse(q.remove(null));
1047 +        }
1048 +    }
1049   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines