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.44 by jsr166, Tue Mar 15 19:47:06 2011 UTC vs.
Revision 1.64 by jsr166, Tue Oct 6 00:03: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.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());
47 >                            new Generic().testSuite(),
48 >                            CollectionTest.testSuite(new Implementation()));
49      }
50  
51      /**
# Line 65 | 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];
70 <            new LinkedTransferQueue(Arrays.asList(ints));
78 >            new LinkedTransferQueue(elements);
79              shouldThrow();
80          } catch (NullPointerException success) {}
81      }
# Line 77 | 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];
82 <            for (int i = 0; i < SIZE - 1; ++i) {
83 <                ints[i] = i;
84 <            }
85 <            new LinkedTransferQueue(Arrays.asList(ints));
93 >            new LinkedTransferQueue(elements);
94              shouldThrow();
95          } catch (NullPointerException success) {}
96      }
# Line 115 | 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      /**
132     * offer(null) throws NullPointerException
133     */
134    public void testOfferNull() {
135        try {
136            LinkedTransferQueue q = new LinkedTransferQueue();
137            q.offer(null);
138            shouldThrow();
139        } catch (NullPointerException success) {}
140    }
141
142    /**
143     * add(null) throws NullPointerException
144     */
145    public void testAddNull() {
146        try {
147            LinkedTransferQueue q = new LinkedTransferQueue();
148            q.add(null);
149            shouldThrow();
150        } catch (NullPointerException success) {}
151    }
152
153    /**
154     * addAll(null) throws NullPointerException
155     */
156    public void testAddAll1() {
157        try {
158            LinkedTransferQueue q = new LinkedTransferQueue();
159            q.addAll(null);
160            shouldThrow();
161        } catch (NullPointerException success) {}
162    }
163
164    /**
140       * addAll(this) throws IllegalArgumentException
141       */
142      public void testAddAllSelf() {
143 +        LinkedTransferQueue q = populatedQueue(SIZE);
144          try {
169            LinkedTransferQueue q = populatedQueue(SIZE);
145              q.addAll(q);
146              shouldThrow();
147          } catch (IllegalArgumentException success) {}
148      }
149  
150      /**
176     * addAll of a collection with null elements throws NullPointerException
177     */
178    public void testAddAll2() {
179        try {
180            LinkedTransferQueue q = new LinkedTransferQueue();
181            Integer[] ints = new Integer[SIZE];
182            q.addAll(Arrays.asList(ints));
183            shouldThrow();
184        } catch (NullPointerException success) {}
185    }
186
187    /**
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 {
193            LinkedTransferQueue q = new LinkedTransferQueue();
194            Integer[] ints = new Integer[SIZE];
195            for (int i = 0; i < SIZE - 1; ++i) {
196                ints[i] = i;
197            }
160              q.addAll(Arrays.asList(ints));
161              shouldThrow();
162          } catch (NullPointerException success) {}
# Line 218 | Line 180 | public class LinkedTransferQueueTest ext
180      }
181  
182      /**
221     * put(null) throws NullPointerException
222     */
223    public void testPutNull() throws InterruptedException {
224        try {
225            LinkedTransferQueue q = new LinkedTransferQueue();
226            q.put(null);
227            shouldThrow();
228        } catch (NullPointerException success) {}
229    }
230
231    /**
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 254 | 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);
275 <        checkEmpty(q);
234 >        awaitTermination(t);
235      }
236  
237      /**
# Line 305 | 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 324 | 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) {
328                    long t0 = System.nanoTime();
288                      assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
330                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
289                  }
332                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 417 | Line 374 | public class LinkedTransferQueueTest ext
374      }
375  
376      /**
420     * remove(x) removes x and returns true if present
421     */
422    public void testRemoveElement() throws InterruptedException {
423        LinkedTransferQueue q = populatedQueue(SIZE);
424        for (int i = 1; i < SIZE; i+=2) {
425            assertTrue(q.contains(i));
426            assertTrue(q.remove(i));
427            assertFalse(q.contains(i));
428            assertTrue(q.contains(i-1));
429        }
430        for (int i = 0; i < SIZE; i+=2) {
431            assertTrue(q.contains(i));
432            assertTrue(q.remove(i));
433            assertFalse(q.contains(i));
434            assertFalse(q.remove(i+1));
435            assertFalse(q.contains(i+1));
436        }
437        checkEmpty(q);
438    }
439
440    /**
377       * An add following remove(x) succeeds
378       */
379      public void testRemoveElementAndAdd() throws InterruptedException {
# Line 553 | Line 489 | public class LinkedTransferQueueTest ext
489      }
490  
491      /**
556     * toArray(null) throws NullPointerException
557     */
558    public void testToArray_NullArg() {
559        LinkedTransferQueue q = populatedQueue(SIZE);
560        try {
561            q.toArray(null);
562            shouldThrow();
563        } catch (NullPointerException success) {}
564    }
565
566    /**
492       * toArray(incompatible array type) throws ArrayStoreException
493       */
494      public void testToArray1_BadArg() {
# Line 580 | 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++);
586 <        }
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 646 | Line 584 | public class LinkedTransferQueueTest ext
584          LinkedTransferQueue q = populatedQueue(SIZE);
585          String s = q.toString();
586          for (int i = 0; i < SIZE; ++i) {
587 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
587 >            assertTrue(s.contains(String.valueOf(i)));
588          }
589      }
590  
# Line 655 | Line 593 | public class LinkedTransferQueueTest ext
593       */
594      public void testOfferInExecutor() {
595          final LinkedTransferQueue q = new LinkedTransferQueue();
596 <        final CountDownLatch threadsStarted = new CountDownLatch(2);
597 <        ExecutorService executor = Executors.newFixedThreadPool(2);
598 <
661 <        executor.execute(new CheckedRunnable() {
662 <            public void realRun() throws InterruptedException {
663 <                threadsStarted.countDown();
664 <                threadsStarted.await();
665 <                assertTrue(q.offer(one, MEDIUM_DELAY_MS, MILLISECONDS));
666 <            }});
596 >        final CheckedBarrier threadsStarted = new CheckedBarrier(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.countDown();
603 <                threadsStarted.await();
604 <                assertSame(one, q.take());
673 <                checkEmpty(q);
674 <            }});
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 681 | Line 617 | public class LinkedTransferQueueTest ext
617       */
618      public void testPollInExecutor() {
619          final LinkedTransferQueue q = new LinkedTransferQueue();
620 <        final CountDownLatch threadsStarted = new CountDownLatch(2);
621 <        ExecutorService executor = Executors.newFixedThreadPool(2);
620 >        final CheckedBarrier threadsStarted = new CheckedBarrier(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.countDown();
628 <                threadsStarted.await();
629 <                assertSame(one, q.poll(SMALL_DELAY_MS, MILLISECONDS));
630 <                checkEmpty(q);
694 <            }});
695 <
696 <        executor.execute(new CheckedRunnable() {
697 <            public void realRun() throws InterruptedException {
698 <                threadsStarted.countDown();
699 <                threadsStarted.await();
700 <                q.put(one);
701 <            }});
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
719 <            = new ByteArrayInputStream(bout.toByteArray());
720 <        ObjectInputStream in
721 <            = new ObjectInputStream(new BufferedInputStream(bin));
722 <        LinkedTransferQueue r = (LinkedTransferQueue) in.readObject();
723 <
724 <        assertEquals(q.size(), r.size());
725 <        assertEquals(q.toString(), r.toString());
726 <        assertTrue(Arrays.equals(q.toArray(), r.toArray()));
727 <        while (!q.isEmpty()) {
728 <            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 <    }
731 <
732 <    /**
733 <     * drainTo(null) throws NullPointerException
734 <     */
735 <    public void testDrainToNull() {
736 <        LinkedTransferQueue q = populatedQueue(SIZE);
737 <        try {
738 <            q.drainTo(null);
739 <            shouldThrow();
740 <        } catch (NullPointerException success) {}
741 <    }
742 <
743 <    /**
744 <     * drainTo(this) throws IllegalArgumentException
745 <     */
746 <    public void testDrainToSelf() {
747 <        LinkedTransferQueue q = populatedQueue(SIZE);
748 <        try {
749 <            q.drainTo(q);
750 <            shouldThrow();
751 <        } catch (IllegalArgumentException success) {}
655 >        assertTrue(y.isEmpty());
656      }
657  
658      /**
# Line 758 | 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 770 | 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 789 | 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);
794 <        }
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      }
701  
702      /**
800     * drainTo(null, n) throws NullPointerException
801     */
802    public void testDrainToNullN() {
803        LinkedTransferQueue q = populatedQueue(SIZE);
804        try {
805            q.drainTo(null, SIZE);
806            shouldThrow();
807        } catch (NullPointerException success) {}
808    }
809
810    /**
811     * drainTo(this, n) throws IllegalArgumentException
812     */
813    public void testDrainToSelfN() {
814        LinkedTransferQueue q = populatedQueue(SIZE);
815        try {
816            q.drainTo(q, SIZE);
817            shouldThrow();
818        } catch (IllegalArgumentException success) {}
819    }
820
821    /**
703       * drainTo(c, n) empties first min(n, size) elements of queue into c
704       */
705      public void testDrainToN() {
# Line 830 | 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 <            }
838 <            while (q.poll() != null)
839 <                ;
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 846 | 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 854 | 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 988 | Line 867 | public class LinkedTransferQueueTest ext
867       * tryTransfer(null) throws NullPointerException
868       */
869      public void testTryTransfer1() {
870 +        final LinkedTransferQueue q = new LinkedTransferQueue();
871          try {
992            final LinkedTransferQueue q = new LinkedTransferQueue();
872              q.tryTransfer(null);
873              shouldThrow();
874          } catch (NullPointerException success) {}
# Line 1051 | Line 930 | public class LinkedTransferQueueTest ext
930      }
931  
932      /**
933 <     * tryTransfer waits the amount given, and throws
1055 <     * InterruptedException when interrupted.
933 >     * tryTransfer blocks interruptibly if no takers
934       */
935      public void testTryTransfer5() throws InterruptedException {
936          final LinkedTransferQueue q = new LinkedTransferQueue();
937 <        final CountDownLatch threadStarted = new CountDownLatch(1);
937 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
938          assertTrue(q.isEmpty());
939  
940          Thread t = newStartedThread(new CheckedRunnable() {
941              public void realRun() throws InterruptedException {
942 <                long t0 = System.nanoTime();
1065 <                threadStarted.countDown();
942 >                Thread.currentThread().interrupt();
943                  try {
944                      q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS);
945                      shouldThrow();
946                  } catch (InterruptedException success) {}
947 <                assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
948 <                assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
947 >                assertFalse(Thread.interrupted());
948 >
949 >                pleaseInterrupt.countDown();
950 >                try {
951 >                    q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS);
952 >                    shouldThrow();
953 >                } catch (InterruptedException success) {}
954 >                assertFalse(Thread.interrupted());
955              }});
956  
957 <        threadStarted.await();
958 <        while (q.isEmpty())
1076 <            Thread.yield();
1077 <        Thread.sleep(SHORT_DELAY_MS);
957 >        await(pleaseInterrupt);
958 >        assertThreadStaysAlive(t);
959          t.interrupt();
960 <        awaitTermination(t, MEDIUM_DELAY_MS);
960 >        awaitTermination(t);
961          checkEmpty(q);
962      }
963  
# Line 1090 | Line 971 | public class LinkedTransferQueueTest ext
971              public void realRun() throws InterruptedException {
972                  long t0 = System.nanoTime();
973                  assertFalse(q.tryTransfer(new Object(),
974 <                                          SHORT_DELAY_MS, MILLISECONDS));
975 <                assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
974 >                                          timeoutMillis(), MILLISECONDS));
975 >                assertTrue(millisElapsedSince(t0) >= timeoutMillis());
976                  checkEmpty(q);
977              }});
978  
979 <        awaitTermination(t, MEDIUM_DELAY_MS);
979 >        awaitTermination(t);
980          checkEmpty(q);
981      }
982  
# Line 1131 | Line 1012 | public class LinkedTransferQueueTest ext
1012          assertTrue(q.offer(four));
1013          assertEquals(1, q.size());
1014          long t0 = System.nanoTime();
1015 <        assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS));
1016 <        assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
1015 >        assertFalse(q.tryTransfer(five, timeoutMillis(), MILLISECONDS));
1016 >        assertTrue(millisElapsedSince(t0) >= timeoutMillis());
1017          assertEquals(1, q.size());
1018          assertSame(four, q.poll());
1019          assertNull(q.poll());
# Line 1150 | 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