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.63 by jsr166, Sun Oct 4 18:49:02 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 374 | Line 376 | public class LinkedTransferQueueTest ext
376      }
377  
378      /**
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    /**
379       * An add following remove(x) succeeds
380       */
381      public void testRemoveElementAndAdd() throws InterruptedException {
# Line 526 | Line 507 | public class LinkedTransferQueueTest ext
507      public void testIterator() throws InterruptedException {
508          LinkedTransferQueue q = populatedQueue(SIZE);
509          Iterator it = q.iterator();
510 <        int i = 0;
511 <        while (it.hasNext()) {
512 <            assertEquals(it.next(), i++);
513 <        }
510 >        int i;
511 >        for (i = 0; it.hasNext(); i++)
512 >            assertTrue(q.contains(it.next()));
513 >        assertEquals(i, SIZE);
514 >        assertIteratorExhausted(it);
515 >
516 >        it = q.iterator();
517 >        for (i = 0; it.hasNext(); i++)
518 >            assertEquals(it.next(), q.take());
519          assertEquals(i, SIZE);
520 +        assertIteratorExhausted(it);
521 +    }
522 +
523 +    /**
524 +     * iterator of empty collection has no elements
525 +     */
526 +    public void testEmptyIterator() {
527 +        assertIteratorExhausted(new LinkedTransferQueue().iterator());
528      }
529  
530      /**
# Line 602 | Line 596 | public class LinkedTransferQueueTest ext
596      public void testOfferInExecutor() {
597          final LinkedTransferQueue q = new LinkedTransferQueue();
598          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
599 <        ExecutorService executor = Executors.newFixedThreadPool(2);
599 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
600 >        try (PoolCleaner cleaner = cleaner(executor)) {
601  
602 <        executor.execute(new CheckedRunnable() {
603 <            public void realRun() throws InterruptedException {
604 <                threadsStarted.await();
605 <                assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS));
606 <            }});
612 <
613 <        executor.execute(new CheckedRunnable() {
614 <            public void realRun() throws InterruptedException {
615 <                threadsStarted.await();
616 <                assertSame(one, q.take());
617 <                checkEmpty(q);
618 <            }});
602 >            executor.execute(new CheckedRunnable() {
603 >                public void realRun() throws InterruptedException {
604 >                    threadsStarted.await();
605 >                    assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS));
606 >                }});
607  
608 <        joinPool(executor);
608 >            executor.execute(new CheckedRunnable() {
609 >                public void realRun() throws InterruptedException {
610 >                    threadsStarted.await();
611 >                    assertSame(one, q.take());
612 >                    checkEmpty(q);
613 >                }});
614 >        }
615      }
616  
617      /**
# Line 626 | Line 620 | public class LinkedTransferQueueTest ext
620      public void testPollInExecutor() {
621          final LinkedTransferQueue q = new LinkedTransferQueue();
622          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
623 <        ExecutorService executor = Executors.newFixedThreadPool(2);
623 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
624 >        try (PoolCleaner cleaner = cleaner(executor)) {
625  
626 <        executor.execute(new CheckedRunnable() {
627 <            public void realRun() throws InterruptedException {
628 <                assertNull(q.poll());
629 <                threadsStarted.await();
630 <                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
631 <                checkEmpty(q);
632 <            }});
638 <
639 <        executor.execute(new CheckedRunnable() {
640 <            public void realRun() throws InterruptedException {
641 <                threadsStarted.await();
642 <                q.put(one);
643 <            }});
626 >            executor.execute(new CheckedRunnable() {
627 >                public void realRun() throws InterruptedException {
628 >                    assertNull(q.poll());
629 >                    threadsStarted.await();
630 >                    assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
631 >                    checkEmpty(q);
632 >                }});
633  
634 <        joinPool(executor);
634 >            executor.execute(new CheckedRunnable() {
635 >                public void realRun() throws InterruptedException {
636 >                    threadsStarted.await();
637 >                    q.put(one);
638 >                }});
639 >        }
640      }
641  
642      /**
643       * A deserialized serialized queue has same elements in same order
644       */
645      public void testSerialization() throws Exception {
646 <        LinkedTransferQueue q = populatedQueue(SIZE);
646 >        Queue x = populatedQueue(SIZE);
647 >        Queue y = serialClone(x);
648  
649 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
650 <        ObjectOutputStream out
651 <            = new ObjectOutputStream(new BufferedOutputStream(bout));
652 <        out.writeObject(q);
653 <        out.close();
654 <
655 <        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());
649 >        assertNotSame(y, x);
650 >        assertEquals(x.size(), y.size());
651 >        assertEquals(x.toString(), y.toString());
652 >        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
653 >        while (!x.isEmpty()) {
654 >            assertFalse(y.isEmpty());
655 >            assertEquals(x.remove(), y.remove());
656          }
657 +        assertTrue(y.isEmpty());
658      }
659  
660      /**
# Line 678 | Line 664 | public class LinkedTransferQueueTest ext
664          LinkedTransferQueue q = populatedQueue(SIZE);
665          ArrayList l = new ArrayList();
666          q.drainTo(l);
667 <        assertEquals(q.size(), 0);
668 <        assertEquals(l.size(), SIZE);
667 >        assertEquals(0, q.size());
668 >        assertEquals(SIZE, l.size());
669          for (int i = 0; i < SIZE; ++i) {
670 <            assertEquals(l.get(i), i);
670 >            assertEquals(i, l.get(i));
671          }
672          q.add(zero);
673          q.add(one);
# Line 690 | Line 676 | public class LinkedTransferQueueTest ext
676          assertTrue(q.contains(one));
677          l.clear();
678          q.drainTo(l);
679 <        assertEquals(q.size(), 0);
680 <        assertEquals(l.size(), 2);
679 >        assertEquals(0, q.size());
680 >        assertEquals(2, l.size());
681          for (int i = 0; i < 2; ++i) {
682 <            assertEquals(l.get(i), i);
682 >            assertEquals(i, l.get(i));
683          }
684      }
685  
# Line 709 | Line 695 | public class LinkedTransferQueueTest ext
695          ArrayList l = new ArrayList();
696          q.drainTo(l);
697          assertTrue(l.size() >= SIZE);
698 <        for (int i = 0; i < SIZE; ++i) {
699 <            assertEquals(l.get(i), i);
714 <        }
698 >        for (int i = 0; i < SIZE; ++i)
699 >            assertEquals(i, l.get(i));
700          awaitTermination(t, MEDIUM_DELAY_MS);
701          assertTrue(q.size() + l.size() >= SIZE);
702      }
# Line 728 | Line 713 | public class LinkedTransferQueueTest ext
713              ArrayList l = new ArrayList();
714              q.drainTo(l, i);
715              int k = (i < SIZE) ? i : SIZE;
716 <            assertEquals(l.size(), k);
717 <            assertEquals(q.size(), SIZE - k);
718 <            for (int j = 0; j < k; ++j) {
719 <                assertEquals(l.get(j), j);
720 <            }
736 <            while (q.poll() != null)
737 <                ;
716 >            assertEquals(k, l.size());
717 >            assertEquals(SIZE - k, q.size());
718 >            for (int j = 0; j < k; ++j)
719 >                assertEquals(j, l.get(j));
720 >            do {} while (q.poll() != null);
721          }
722      }
723  
# Line 744 | Line 727 | public class LinkedTransferQueueTest ext
727       */
728      public void testWaitingConsumer() throws InterruptedException {
729          final LinkedTransferQueue q = new LinkedTransferQueue();
730 <        assertEquals(q.getWaitingConsumerCount(), 0);
730 >        assertEquals(0, q.getWaitingConsumerCount());
731          assertFalse(q.hasWaitingConsumer());
732          final CountDownLatch threadStarted = new CountDownLatch(1);
733  
# Line 752 | Line 735 | public class LinkedTransferQueueTest ext
735              public void realRun() throws InterruptedException {
736                  threadStarted.countDown();
737                  assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
738 <                assertEquals(q.getWaitingConsumerCount(), 0);
738 >                assertEquals(0, q.getWaitingConsumerCount());
739                  assertFalse(q.hasWaitingConsumer());
740              }});
741  
742          threadStarted.await();
743          waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
744 <        assertEquals(q.getWaitingConsumerCount(), 1);
744 >        assertEquals(1, q.getWaitingConsumerCount());
745          assertTrue(q.hasWaitingConsumer());
746  
747          assertTrue(q.offer(one));
748 <        assertEquals(q.getWaitingConsumerCount(), 0);
748 >        assertEquals(0, q.getWaitingConsumerCount());
749          assertFalse(q.hasWaitingConsumer());
750  
751          awaitTermination(t, MEDIUM_DELAY_MS);
# Line 886 | Line 869 | public class LinkedTransferQueueTest ext
869       * tryTransfer(null) throws NullPointerException
870       */
871      public void testTryTransfer1() {
872 +        final LinkedTransferQueue q = new LinkedTransferQueue();
873          try {
890            final LinkedTransferQueue q = new LinkedTransferQueue();
874              q.tryTransfer(null);
875              shouldThrow();
876          } catch (NullPointerException success) {}
# Line 1050 | Line 1033 | public class LinkedTransferQueueTest ext
1033          assertFalse(q.isEmpty());
1034          return q;
1035      }
1036 +
1037 +    /**
1038 +     * remove(null), contains(null) always return false
1039 +     */
1040 +    public void testNeverContainsNull() {
1041 +        Collection<?>[] qs = {
1042 +            new LinkedTransferQueue<Object>(),
1043 +            populatedQueue(2),
1044 +        };
1045 +
1046 +        for (Collection<?> q : qs) {
1047 +            assertFalse(q.contains(null));
1048 +            assertFalse(q.remove(null));
1049 +        }
1050 +    }
1051   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines