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.35 by jsr166, Fri Oct 29 07:27:26 2010 UTC vs.
Revision 1.63 by jsr166, Sun Oct 4 18:49:02 2015 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
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.concurrent.*;
17 < import static java.util.concurrent.TimeUnit.MILLISECONDS;
18 < import static java.util.concurrent.TimeUnit.NANOSECONDS;
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 >
23   import junit.framework.Test;
23 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 32 | 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());
48 <    }
42 <
43 <    void checkEmpty(BlockingQueue q) {
44 <        try {
45 <            assertTrue(q.isEmpty());
46 <            assertEquals(0, q.size());
47 <            assertNull(q.peek());
48 <            assertNull(q.poll());
49 <            assertNull(q.poll(0, MILLISECONDS));
50 <            assertEquals(q.toString(), "[]");
51 <            assertTrue(Arrays.equals(q.toArray(), new Object[0]));
52 <            assertFalse(q.iterator().hasNext());
53 <            try {
54 <                q.element();
55 <                shouldThrow();
56 <            } catch (NoSuchElementException success) {}
57 <            try {
58 <                q.iterator().next();
59 <                shouldThrow();
60 <            } catch (NoSuchElementException success) {}
61 <            try {
62 <                q.remove();
63 <                shouldThrow();
64 <            } catch (NoSuchElementException success) {}
65 <        } catch (InterruptedException ie) {
66 <            threadUnexpectedException(ie);
67 <        }
47 >                            new Generic().testSuite(),
48 >                            CollectionTest.testSuite(new Implementation()));
49      }
50  
51      /**
# Line 92 | Line 73 | public class LinkedTransferQueueTest ext
73       * NullPointerException
74       */
75      public void testConstructor3() {
76 +        Collection<Integer> elements = Arrays.asList(new Integer[SIZE]);
77          try {
78 <            Integer[] ints = new Integer[SIZE];
97 <            new LinkedTransferQueue(Arrays.asList(ints));
78 >            new LinkedTransferQueue(elements);
79              shouldThrow();
80          } catch (NullPointerException success) {}
81      }
# Line 104 | Line 85 | public class LinkedTransferQueueTest ext
85       * throws NullPointerException
86       */
87      public void testConstructor4() {
88 +        Integer[] ints = new Integer[SIZE];
89 +        for (int i = 0; i < SIZE - 1; ++i)
90 +            ints[i] = i;
91 +        Collection<Integer> elements = Arrays.asList(ints);
92          try {
93 <            Integer[] ints = new Integer[SIZE];
109 <            for (int i = 0; i < SIZE - 1; ++i) {
110 <                ints[i] = i;
111 <            }
112 <            new LinkedTransferQueue(Arrays.asList(ints));
93 >            new LinkedTransferQueue(elements);
94              shouldThrow();
95          } catch (NullPointerException success) {}
96      }
# Line 142 | 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  
139      /**
159     * offer(null) throws NullPointerException
160     */
161    public void testOfferNull() {
162        try {
163            LinkedTransferQueue q = new LinkedTransferQueue();
164            q.offer(null);
165            shouldThrow();
166        } catch (NullPointerException success) {}
167    }
168
169    /**
170     * add(null) throws NullPointerException
171     */
172    public void testAddNull() {
173        try {
174            LinkedTransferQueue q = new LinkedTransferQueue();
175            q.add(null);
176            shouldThrow();
177        } catch (NullPointerException success) {}
178    }
179
180    /**
181     * addAll(null) throws NullPointerException
182     */
183    public void testAddAll1() {
184        try {
185            LinkedTransferQueue q = new LinkedTransferQueue();
186            q.addAll(null);
187            shouldThrow();
188        } catch (NullPointerException success) {}
189    }
190
191    /**
140       * addAll(this) throws IllegalArgumentException
141       */
142      public void testAddAllSelf() {
143 +        LinkedTransferQueue q = populatedQueue(SIZE);
144          try {
196            LinkedTransferQueue q = populatedQueue(SIZE);
145              q.addAll(q);
146              shouldThrow();
147          } catch (IllegalArgumentException success) {}
148      }
149  
150      /**
203     * addAll of a collection with null elements throws NullPointerException
204     */
205    public void testAddAll2() {
206        try {
207            LinkedTransferQueue q = new LinkedTransferQueue();
208            Integer[] ints = new Integer[SIZE];
209            q.addAll(Arrays.asList(ints));
210            shouldThrow();
211        } catch (NullPointerException success) {}
212    }
213
214    /**
151       * addAll of a collection with any null elements throws
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 {
220            LinkedTransferQueue q = new LinkedTransferQueue();
221            Integer[] ints = new Integer[SIZE];
222            for (int i = 0; i < SIZE - 1; ++i) {
223                ints[i] = i;
224            }
160              q.addAll(Arrays.asList(ints));
161              shouldThrow();
162          } catch (NullPointerException success) {}
# Line 245 | Line 180 | public class LinkedTransferQueueTest ext
180      }
181  
182      /**
248     * put(null) throws NullPointerException
249     */
250    public void testPutNull() throws InterruptedException {
251        try {
252            LinkedTransferQueue q = new LinkedTransferQueue();
253            q.put(null);
254            shouldThrow();
255        } catch (NullPointerException success) {}
256    }
257
258    /**
183       * all elements successfully put are contained
184       */
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 205 | public class LinkedTransferQueueTest ext
205       * take removes existing elements until empty, then blocks interruptibly
206       */
207      public void testBlockingTake() throws InterruptedException {
208 <        final BlockingQueue<Integer> q = populatedQueue(SIZE);
209 <        final CountDownLatch aboutToWait = new CountDownLatch(1);
208 >        final BlockingQueue q = populatedQueue(SIZE);
209 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
210          Thread t = newStartedThread(new CheckedRunnable() {
211              public void realRun() throws InterruptedException {
212                  for (int i = 0; i < SIZE; ++i) {
213 <                    assertEquals(i, (int) q.take());
213 >                    assertEquals(i, q.take());
214                  }
215 <                aboutToWait.countDown();
215 >
216 >                Thread.currentThread().interrupt();
217 >                try {
218 >                    q.take();
219 >                    shouldThrow();
220 >                } catch (InterruptedException success) {}
221 >                assertFalse(Thread.interrupted());
222 >
223 >                pleaseInterrupt.countDown();
224                  try {
225                      q.take();
226                      shouldThrow();
227                  } catch (InterruptedException success) {}
228 +                assertFalse(Thread.interrupted());
229              }});
230  
231 <        aboutToWait.await();
232 <        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
231 >        await(pleaseInterrupt);
232 >        assertThreadStaysAlive(t);
233          t.interrupt();
234 <        awaitTermination(t, MEDIUM_DELAY_MS);
302 <        checkEmpty(q);
234 >        awaitTermination(t);
235      }
236  
237      /**
# Line 332 | Line 264 | public class LinkedTransferQueueTest ext
264      public void testTimedPoll() throws InterruptedException {
265          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
266          for (int i = 0; i < SIZE; ++i) {
267 <            long t0 = System.nanoTime();
268 <            assertEquals(i, (int) q.poll(SMALL_DELAY_MS, MILLISECONDS));
269 <            assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
270 <        }
271 <        long t0 = System.nanoTime();
272 <        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
273 <        assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
267 >            long startTime = System.nanoTime();
268 >            assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
269 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
270 >        }
271 >        long startTime = System.nanoTime();
272 >        assertNull(q.poll(timeoutMillis(), MILLISECONDS));
273 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
274          checkEmpty(q);
275      }
276  
# Line 444 | Line 376 | public class LinkedTransferQueueTest ext
376      }
377  
378      /**
447     * remove(x) removes x and returns true if present
448     */
449    public void testRemoveElement() throws InterruptedException {
450        LinkedTransferQueue q = populatedQueue(SIZE);
451        for (int i = 1; i < SIZE; i += 2) {
452            assertTrue(q.remove(i));
453        }
454        for (int i = 0; i < SIZE; i += 2) {
455            assertTrue(q.remove(i));
456            assertFalse(q.remove(i + 1));
457        }
458        checkEmpty(q);
459    }
460
461    /**
379       * An add following remove(x) succeeds
380       */
381      public void testRemoveElementAndAdd() throws InterruptedException {
# Line 550 | Line 467 | public class LinkedTransferQueueTest ext
467      }
468  
469      /**
470 <     * toArray() contains all elements
470 >     * toArray() contains all elements in FIFO order
471       */
472 <    public void testToArray() throws InterruptedException {
472 >    public void testToArray() {
473          LinkedTransferQueue q = populatedQueue(SIZE);
474          Object[] o = q.toArray();
475          for (int i = 0; i < o.length; i++) {
476 <            assertEquals(o[i], q.take());
476 >            assertSame(o[i], q.poll());
477          }
478      }
479  
480      /**
481 <     * toArray(a) contains all elements
481 >     * toArray(a) contains all elements in FIFO order
482       */
483 <    public void testToArray2() throws InterruptedException {
483 >    public void testToArray2() {
484          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
485          Integer[] ints = new Integer[SIZE];
486 <        ints = q.toArray(ints);
486 >        Integer[] array = q.toArray(ints);
487 >        assertSame(ints, array);
488          for (int i = 0; i < ints.length; i++) {
489 <            assertEquals(ints[i], q.take());
489 >            assertSame(ints[i], q.poll());
490          }
491      }
492  
493      /**
494 <     * toArray(null) throws NullPointerException
577 <     */
578 <    public void testToArray_BadArg() {
579 <        LinkedTransferQueue q = populatedQueue(SIZE);
580 <        try {
581 <            Object o[] = q.toArray(null);
582 <            shouldThrow();
583 <        } catch (NullPointerException success) {}
584 <    }
585 <
586 <    /**
587 <     * toArray(incompatible array type) throws CCE
494 >     * toArray(incompatible array type) throws ArrayStoreException
495       */
496      public void testToArray1_BadArg() {
497          LinkedTransferQueue q = populatedQueue(SIZE);
498          try {
499 <            Object o[] = q.toArray(new String[10]);
499 >            q.toArray(new String[10]);
500              shouldThrow();
501          } catch (ArrayStoreException success) {}
502      }
# Line 600 | 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 666 | Line 586 | public class LinkedTransferQueueTest ext
586          LinkedTransferQueue q = populatedQueue(SIZE);
587          String s = q.toString();
588          for (int i = 0; i < SIZE; ++i) {
589 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
589 >            assertTrue(s.contains(String.valueOf(i)));
590          }
591      }
592  
# Line 675 | Line 595 | public class LinkedTransferQueueTest ext
595       */
596      public void testOfferInExecutor() {
597          final LinkedTransferQueue q = new LinkedTransferQueue();
598 <        final CountDownLatch threadsStarted = new CountDownLatch(2);
599 <        ExecutorService executor = Executors.newFixedThreadPool(2);
598 >        final CheckedBarrier threadsStarted = new CheckedBarrier(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.countDown();
605 <                threadsStarted.await();
606 <                assertTrue(q.offer(one, MEDIUM_DELAY_MS, MILLISECONDS));
686 <            }});
687 <
688 <        executor.execute(new CheckedRunnable() {
689 <            public void realRun() throws InterruptedException {
690 <                threadsStarted.countDown();
691 <                threadsStarted.await();
692 <                assertSame(one, q.take());
693 <                checkEmpty(q);
694 <            }});
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 701 | Line 619 | public class LinkedTransferQueueTest ext
619       */
620      public void testPollInExecutor() {
621          final LinkedTransferQueue q = new LinkedTransferQueue();
622 <        final CountDownLatch threadsStarted = new CountDownLatch(2);
623 <        ExecutorService executor = Executors.newFixedThreadPool(2);
624 <
707 <        executor.execute(new CheckedRunnable() {
708 <            public void realRun() throws InterruptedException {
709 <                assertNull(q.poll());
710 <                threadsStarted.countDown();
711 <                threadsStarted.await();
712 <                assertSame(one, q.poll(SMALL_DELAY_MS, MILLISECONDS));
713 <                checkEmpty(q);
714 <            }});
622 >        final CheckedBarrier threadsStarted = new CheckedBarrier(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 <                threadsStarted.countDown();
629 <                threadsStarted.await();
630 <                q.put(one);
631 <            }});
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);
647 <
732 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
733 <        ObjectOutputStream out
734 <            = new ObjectOutputStream(new BufferedOutputStream(bout));
735 <        out.writeObject(q);
736 <        out.close();
646 >        Queue x = populatedQueue(SIZE);
647 >        Queue y = serialClone(x);
648  
649 <        ByteArrayInputStream bin
650 <            = new ByteArrayInputStream(bout.toByteArray());
651 <        ObjectInputStream in
652 <            = new ObjectInputStream(new BufferedInputStream(bin));
653 <        LinkedTransferQueue r = (LinkedTransferQueue) in.readObject();
654 <
655 <        assertEquals(q.size(), r.size());
745 <        assertEquals(q.toString(), r.toString());
746 <        assertTrue(Arrays.equals(q.toArray(), r.toArray()));
747 <        while (!q.isEmpty()) {
748 <            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 <    }
751 <
752 <    /**
753 <     * drainTo(null) throws NullPointerException
754 <     */
755 <    public void testDrainToNull() {
756 <        LinkedTransferQueue q = populatedQueue(SIZE);
757 <        try {
758 <            q.drainTo(null);
759 <            shouldThrow();
760 <        } catch (NullPointerException success) {}
761 <    }
762 <
763 <    /**
764 <     * drainTo(this) throws IllegalArgumentException
765 <     */
766 <    public void testDrainToSelf() {
767 <        LinkedTransferQueue q = populatedQueue(SIZE);
768 <        try {
769 <            q.drainTo(q);
770 <            shouldThrow();
771 <        } catch (IllegalArgumentException success) {}
657 >        assertTrue(y.isEmpty());
658      }
659  
660      /**
# Line 778 | 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 790 | 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 809 | 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);
814 <        }
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      }
703  
704      /**
820     * drainTo(null, n) throws NullPointerException
821     */
822    public void testDrainToNullN() {
823        LinkedTransferQueue q = populatedQueue(SIZE);
824        try {
825            q.drainTo(null, SIZE);
826            shouldThrow();
827        } catch (NullPointerException success) {}
828    }
829
830    /**
831     * drainTo(this, n) throws IllegalArgumentException
832     */
833    public void testDrainToSelfN() {
834        LinkedTransferQueue q = populatedQueue(SIZE);
835        try {
836            q.drainTo(q, SIZE);
837            shouldThrow();
838        } catch (IllegalArgumentException success) {}
839    }
840
841    /**
705       * drainTo(c, n) empties first min(n, size) elements of queue into c
706       */
707      public void testDrainToN() {
# Line 850 | 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 <            }
858 <            while (q.poll() != null)
859 <                ;
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 866 | 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 874 | 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 1008 | Line 869 | public class LinkedTransferQueueTest ext
869       * tryTransfer(null) throws NullPointerException
870       */
871      public void testTryTransfer1() {
872 +        final LinkedTransferQueue q = new LinkedTransferQueue();
873          try {
1012            final LinkedTransferQueue q = new LinkedTransferQueue();
874              q.tryTransfer(null);
875              shouldThrow();
876          } catch (NullPointerException success) {}
# Line 1071 | Line 932 | public class LinkedTransferQueueTest ext
932      }
933  
934      /**
935 <     * tryTransfer waits the amount given if interrupted, and
1075 <     * throws interrupted exception
935 >     * tryTransfer blocks interruptibly if no takers
936       */
937      public void testTryTransfer5() throws InterruptedException {
938          final LinkedTransferQueue q = new LinkedTransferQueue();
939 <        final CountDownLatch threadStarted = new CountDownLatch(1);
939 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
940 >        assertTrue(q.isEmpty());
941  
942          Thread t = newStartedThread(new CheckedRunnable() {
943              public void realRun() throws InterruptedException {
944 <                long t0 = System.nanoTime();
945 <                threadStarted.countDown();
944 >                Thread.currentThread().interrupt();
945 >                try {
946 >                    q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS);
947 >                    shouldThrow();
948 >                } catch (InterruptedException success) {}
949 >                assertFalse(Thread.interrupted());
950 >
951 >                pleaseInterrupt.countDown();
952                  try {
953                      q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS);
954                      shouldThrow();
955                  } catch (InterruptedException success) {}
956 <                assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
956 >                assertFalse(Thread.interrupted());
957              }});
958  
959 <        threadStarted.await();
960 <        Thread.sleep(SHORT_DELAY_MS);
959 >        await(pleaseInterrupt);
960 >        assertThreadStaysAlive(t);
961          t.interrupt();
962 <        awaitTermination(t, MEDIUM_DELAY_MS);
962 >        awaitTermination(t);
963          checkEmpty(q);
964      }
965  
# Line 1106 | Line 973 | public class LinkedTransferQueueTest ext
973              public void realRun() throws InterruptedException {
974                  long t0 = System.nanoTime();
975                  assertFalse(q.tryTransfer(new Object(),
976 <                                          SHORT_DELAY_MS, MILLISECONDS));
977 <                assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
976 >                                          timeoutMillis(), MILLISECONDS));
977 >                assertTrue(millisElapsedSince(t0) >= timeoutMillis());
978                  checkEmpty(q);
979              }});
980  
981 <        awaitTermination(t, MEDIUM_DELAY_MS);
981 >        awaitTermination(t);
982          checkEmpty(q);
983      }
984  
# Line 1139 | Line 1006 | public class LinkedTransferQueueTest ext
1006      }
1007  
1008      /**
1009 <     * tryTransfer attempts to enqueue into the q and fails returning
1010 <     * false not enqueueing and the successive poll is null
1009 >     * tryTransfer attempts to enqueue into the queue and fails
1010 >     * returning false not enqueueing and the successive poll is null
1011       */
1012      public void testTryTransfer8() throws InterruptedException {
1013          final LinkedTransferQueue q = new LinkedTransferQueue();
1014          assertTrue(q.offer(four));
1015          assertEquals(1, q.size());
1016          long t0 = System.nanoTime();
1017 <        assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS));
1018 <        assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
1017 >        assertFalse(q.tryTransfer(five, timeoutMillis(), MILLISECONDS));
1018 >        assertTrue(millisElapsedSince(t0) >= timeoutMillis());
1019          assertEquals(1, q.size());
1020          assertSame(four, q.poll());
1021          assertNull(q.poll());
# Line 1166 | 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