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.32 by jsr166, Fri Oct 29 06:21:16 2010 UTC vs.
Revision 1.76 by jsr166, Sun May 14 00:48:20 2017 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.Callable;
19 > import java.util.concurrent.CountDownLatch;
20 > import java.util.concurrent.Executors;
21 > import java.util.concurrent.ExecutorService;
22 > import java.util.concurrent.LinkedTransferQueue;
23 >
24   import junit.framework.Test;
23 import junit.framework.TestSuite;
25  
26   @SuppressWarnings({"unchecked", "rawtypes"})
27   public class LinkedTransferQueueTest extends JSR166TestCase {
27
28      public static class Generic extends BlockingQueueTest {
29          protected BlockingQueue emptyCollection() {
30              return new LinkedTransferQueue();
# Line 32 | Line 32 | public class LinkedTransferQueueTest ext
32      }
33  
34      public static void main(String[] args) {
35 <        junit.textui.TestRunner.run(suite());
35 >        main(suite(), args);
36      }
37  
38      public static Test suite() {
39 <        return newTestSuite(LinkedTransferQueueTest.class,
40 <                            new Generic().testSuite());
41 <    }
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);
39 >        class Implementation implements CollectionImplementation {
40 >            public Class<?> klazz() { return LinkedTransferQueue.class; }
41 >            public Collection emptyCollection() { return new LinkedTransferQueue(); }
42 >            public Object makeElement(int i) { return i; }
43 >            public boolean isConcurrent() { return true; }
44 >            public boolean permitsNulls() { return false; }
45          }
46 +        return newTestSuite(LinkedTransferQueueTest.class,
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>();
186 >        LinkedTransferQueue<Integer> q = new LinkedTransferQueue<>();
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());
214 <                }
291 <                aboutToWait.countDown();
212 >                for (int i = 0; i < SIZE; i++) assertEquals(i, q.take());
213 >
214 >                Thread.currentThread().interrupt();
215                  try {
216                      q.take();
217                      shouldThrow();
218                  } catch (InterruptedException success) {}
219 +                assertFalse(Thread.interrupted());
220 +
221 +                pleaseInterrupt.countDown();
222 +                try {
223 +                    q.take();
224 +                    shouldThrow();
225 +                } catch (InterruptedException success) {}
226 +                assertFalse(Thread.interrupted());
227              }});
228  
229 <        aboutToWait.await();
230 <        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
229 >        await(pleaseInterrupt);
230 >        assertThreadBlocks(t, Thread.State.WAITING);
231          t.interrupt();
232 <        awaitTermination(t, MEDIUM_DELAY_MS);
302 <        checkEmpty(q);
232 >        awaitTermination(t);
233      }
234  
235      /**
# Line 331 | Line 261 | public class LinkedTransferQueueTest ext
261       */
262      public void testTimedPoll() throws InterruptedException {
263          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
264 <        for (int i = 0; i < SIZE; ++i) {
265 <            long t0 = System.nanoTime();
266 <            assertEquals(i, (int) q.poll(SMALL_DELAY_MS, MILLISECONDS));
267 <            assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
268 <        }
269 <        long t0 = System.nanoTime();
270 <        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
271 <        assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
264 >        long startTime = System.nanoTime();
265 >        for (int i = 0; i < SIZE; ++i)
266 >            assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
267 >        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
268 >
269 >        startTime = System.nanoTime();
270 >        assertNull(q.poll(timeoutMillis(), MILLISECONDS));
271 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
272          checkEmpty(q);
273      }
274  
# Line 348 | Line 278 | public class LinkedTransferQueueTest ext
278       */
279      public void testInterruptedTimedPoll() throws InterruptedException {
280          final BlockingQueue<Integer> q = populatedQueue(SIZE);
281 <        final CountDownLatch aboutToWait = new CountDownLatch(1);
281 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
282          Thread t = newStartedThread(new CheckedRunnable() {
283              public void realRun() throws InterruptedException {
284 <                for (int i = 0; i < SIZE; ++i) {
285 <                    long t0 = System.nanoTime();
284 >                long startTime = System.nanoTime();
285 >                for (int i = 0; i < SIZE; ++i)
286                      assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
287 <                    assertTrue(millisElapsedSince(t0) < SHORT_DELAY_MS);
288 <                }
359 <                aboutToWait.countDown();
287 >
288 >                pleaseInterrupt.countDown();
289                  try {
290 <                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
290 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
291                      shouldThrow();
292                  } catch (InterruptedException success) {}
293 +                assertFalse(Thread.interrupted());
294 +
295 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
296              }});
297  
298 <        aboutToWait.await();
299 <        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
298 >        await(pleaseInterrupt);
299 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
300          t.interrupt();
301 <        awaitTermination(t, MEDIUM_DELAY_MS);
301 >        awaitTermination(t);
302          checkEmpty(q);
303      }
304  
# Line 378 | Line 310 | public class LinkedTransferQueueTest ext
310          final BlockingQueue<Integer> q = populatedQueue(SIZE);
311          Thread t = newStartedThread(new CheckedRunnable() {
312              public void realRun() throws InterruptedException {
313 +                long startTime = System.nanoTime();
314                  Thread.currentThread().interrupt();
315 <                for (int i = 0; i < SIZE; ++i) {
383 <                    long t0 = System.nanoTime();
315 >                for (int i = 0; i < SIZE; ++i)
316                      assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
385                    assertTrue(millisElapsedSince(t0) < SHORT_DELAY_MS);
386                }
317                  try {
318 <                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
318 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
319                      shouldThrow();
320                  } catch (InterruptedException success) {}
321 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
322              }});
323  
324 <        awaitTermination(t, MEDIUM_DELAY_MS);
324 >        awaitTermination(t);
325          checkEmpty(q);
326      }
327  
# Line 441 | Line 372 | public class LinkedTransferQueueTest ext
372      }
373  
374      /**
444     * remove(x) removes x and returns true if present
445     */
446    public void testRemoveElement() throws InterruptedException {
447        LinkedTransferQueue q = populatedQueue(SIZE);
448        for (int i = 1; i < SIZE; i += 2) {
449            assertTrue(q.remove(i));
450        }
451        for (int i = 0; i < SIZE; i += 2) {
452            assertTrue(q.remove(i));
453            assertFalse(q.remove(i + 1));
454        }
455        checkEmpty(q);
456    }
457
458    /**
375       * An add following remove(x) succeeds
376       */
377      public void testRemoveElementAndAdd() throws InterruptedException {
# Line 501 | Line 417 | public class LinkedTransferQueueTest ext
417       */
418      public void testContainsAll() {
419          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
420 <        LinkedTransferQueue<Integer> p = new LinkedTransferQueue<Integer>();
420 >        LinkedTransferQueue<Integer> p = new LinkedTransferQueue<>();
421          for (int i = 0; i < SIZE; ++i) {
422              assertTrue(q.containsAll(p));
423              assertFalse(p.containsAll(q));
# Line 547 | Line 463 | public class LinkedTransferQueueTest ext
463      }
464  
465      /**
466 <     * toArray() contains all elements
466 >     * toArray() contains all elements in FIFO order
467       */
468 <    public void testToArray() throws InterruptedException {
468 >    public void testToArray() {
469          LinkedTransferQueue q = populatedQueue(SIZE);
470          Object[] o = q.toArray();
471          for (int i = 0; i < o.length; i++) {
472 <            assertEquals(o[i], q.take());
472 >            assertSame(o[i], q.poll());
473          }
474      }
475  
476      /**
477 <     * toArray(a) contains all elements
477 >     * toArray(a) contains all elements in FIFO order
478       */
479 <    public void testToArray2() throws InterruptedException {
479 >    public void testToArray2() {
480          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
481          Integer[] ints = new Integer[SIZE];
482 <        ints = q.toArray(ints);
482 >        Integer[] array = q.toArray(ints);
483 >        assertSame(ints, array);
484          for (int i = 0; i < ints.length; i++) {
485 <            assertEquals(ints[i], q.take());
485 >            assertSame(ints[i], q.poll());
486          }
487      }
488  
489      /**
490 <     * toArray(null) throws NullPointerException
574 <     */
575 <    public void testToArray_BadArg() {
576 <        LinkedTransferQueue q = populatedQueue(SIZE);
577 <        try {
578 <            Object o[] = q.toArray(null);
579 <            shouldThrow();
580 <        } catch (NullPointerException success) {}
581 <    }
582 <
583 <    /**
584 <     * toArray(incompatible array type) throws CCE
490 >     * toArray(incompatible array type) throws ArrayStoreException
491       */
492      public void testToArray1_BadArg() {
493          LinkedTransferQueue q = populatedQueue(SIZE);
494          try {
495 <            Object o[] = q.toArray(new String[10]);
495 >            q.toArray(new String[10]);
496              shouldThrow();
497          } catch (ArrayStoreException success) {}
498      }
# Line 597 | Line 503 | public class LinkedTransferQueueTest ext
503      public void testIterator() throws InterruptedException {
504          LinkedTransferQueue q = populatedQueue(SIZE);
505          Iterator it = q.iterator();
506 <        int i = 0;
507 <        while (it.hasNext()) {
508 <            assertEquals(it.next(), i++);
509 <        }
506 >        int i;
507 >        for (i = 0; it.hasNext(); i++)
508 >            assertTrue(q.contains(it.next()));
509 >        assertEquals(i, SIZE);
510 >        assertIteratorExhausted(it);
511 >
512 >        it = q.iterator();
513 >        for (i = 0; it.hasNext(); i++)
514 >            assertEquals(it.next(), q.take());
515          assertEquals(i, SIZE);
516 +        assertIteratorExhausted(it);
517 +    }
518 +
519 +    /**
520 +     * iterator of empty collection has no elements
521 +     */
522 +    public void testEmptyIterator() {
523 +        assertIteratorExhausted(new LinkedTransferQueue().iterator());
524      }
525  
526      /**
# Line 627 | Line 546 | public class LinkedTransferQueueTest ext
546       * iterator ordering is FIFO
547       */
548      public void testIteratorOrdering() {
549 <        final LinkedTransferQueue<Integer> q
631 <            = new LinkedTransferQueue<Integer>();
549 >        final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<>();
550          assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
551          q.add(one);
552          q.add(two);
# Line 663 | Line 581 | public class LinkedTransferQueueTest ext
581          LinkedTransferQueue q = populatedQueue(SIZE);
582          String s = q.toString();
583          for (int i = 0; i < SIZE; ++i) {
584 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
584 >            assertTrue(s.contains(String.valueOf(i)));
585          }
586      }
587  
# Line 672 | Line 590 | public class LinkedTransferQueueTest ext
590       */
591      public void testOfferInExecutor() {
592          final LinkedTransferQueue q = new LinkedTransferQueue();
593 <        final CountDownLatch threadsStarted = new CountDownLatch(2);
594 <        ExecutorService executor = Executors.newFixedThreadPool(2);
595 <
678 <        executor.execute(new CheckedRunnable() {
679 <            public void realRun() throws InterruptedException {
680 <                threadsStarted.countDown();
681 <                threadsStarted.await();
682 <                assertTrue(q.offer(one, MEDIUM_DELAY_MS, MILLISECONDS));
683 <            }});
593 >        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
594 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
595 >        try (PoolCleaner cleaner = cleaner(executor)) {
596  
597 <        executor.execute(new CheckedRunnable() {
598 <            public void realRun() throws InterruptedException {
599 <                threadsStarted.countDown();
600 <                threadsStarted.await();
601 <                assertSame(one, q.take());
602 <                checkEmpty(q);
603 <            }});
597 >            executor.execute(new CheckedRunnable() {
598 >                public void realRun() throws InterruptedException {
599 >                    threadsStarted.await();
600 >                    long startTime = System.nanoTime();
601 >                    assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS));
602 >                    assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
603 >                }});
604  
605 <        joinPool(executor);
605 >            executor.execute(new CheckedRunnable() {
606 >                public void realRun() throws InterruptedException {
607 >                    threadsStarted.await();
608 >                    assertSame(one, q.take());
609 >                    checkEmpty(q);
610 >                }});
611 >        }
612      }
613  
614      /**
# Line 698 | Line 616 | public class LinkedTransferQueueTest ext
616       */
617      public void testPollInExecutor() {
618          final LinkedTransferQueue q = new LinkedTransferQueue();
619 <        final CountDownLatch threadsStarted = new CountDownLatch(2);
620 <        ExecutorService executor = Executors.newFixedThreadPool(2);
619 >        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
620 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
621 >        try (PoolCleaner cleaner = cleaner(executor)) {
622  
623 <        executor.execute(new CheckedRunnable() {
624 <            public void realRun() throws InterruptedException {
625 <                assertNull(q.poll());
626 <                threadsStarted.countDown();
627 <                threadsStarted.await();
628 <                assertSame(one, q.poll(SMALL_DELAY_MS, MILLISECONDS));
629 <                checkEmpty(q);
630 <            }});
631 <
713 <        executor.execute(new CheckedRunnable() {
714 <            public void realRun() throws InterruptedException {
715 <                threadsStarted.countDown();
716 <                threadsStarted.await();
717 <                q.put(one);
718 <            }});
623 >            executor.execute(new CheckedRunnable() {
624 >                public void realRun() throws InterruptedException {
625 >                    assertNull(q.poll());
626 >                    threadsStarted.await();
627 >                    long startTime = System.nanoTime();
628 >                    assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
629 >                    assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
630 >                    checkEmpty(q);
631 >                }});
632  
633 <        joinPool(executor);
633 >            executor.execute(new CheckedRunnable() {
634 >                public void realRun() throws InterruptedException {
635 >                    threadsStarted.await();
636 >                    q.put(one);
637 >                }});
638 >        }
639      }
640  
641      /**
642       * A deserialized serialized queue has same elements in same order
643       */
644      public void testSerialization() throws Exception {
645 <        LinkedTransferQueue q = populatedQueue(SIZE);
646 <
729 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
730 <        ObjectOutputStream out
731 <            = new ObjectOutputStream(new BufferedOutputStream(bout));
732 <        out.writeObject(q);
733 <        out.close();
734 <
735 <        ByteArrayInputStream bin
736 <            = new ByteArrayInputStream(bout.toByteArray());
737 <        ObjectInputStream in
738 <            = new ObjectInputStream(new BufferedInputStream(bin));
739 <        LinkedTransferQueue r = (LinkedTransferQueue) in.readObject();
645 >        Queue x = populatedQueue(SIZE);
646 >        Queue y = serialClone(x);
647  
648 <        assertEquals(q.size(), r.size());
649 <        assertEquals(q.toString(), r.toString());
650 <        assertTrue(Arrays.equals(q.toArray(), r.toArray()));
651 <        while (!q.isEmpty()) {
652 <            assertEquals(q.remove(), r.remove());
648 >        assertNotSame(y, x);
649 >        assertEquals(x.size(), y.size());
650 >        assertEquals(x.toString(), y.toString());
651 >        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
652 >        while (!x.isEmpty()) {
653 >            assertFalse(y.isEmpty());
654 >            assertEquals(x.remove(), y.remove());
655          }
656 <    }
748 <
749 <    /**
750 <     * drainTo(null) throws NullPointerException
751 <     */
752 <    public void testDrainToNull() {
753 <        LinkedTransferQueue q = populatedQueue(SIZE);
754 <        try {
755 <            q.drainTo(null);
756 <            shouldThrow();
757 <        } catch (NullPointerException success) {}
758 <    }
759 <
760 <    /**
761 <     * drainTo(this) throws IllegalArgumentException
762 <     */
763 <    public void testDrainToSelf() {
764 <        LinkedTransferQueue q = populatedQueue(SIZE);
765 <        try {
766 <            q.drainTo(q);
767 <            shouldThrow();
768 <        } catch (IllegalArgumentException success) {}
656 >        assertTrue(y.isEmpty());
657      }
658  
659      /**
# Line 775 | Line 663 | public class LinkedTransferQueueTest ext
663          LinkedTransferQueue q = populatedQueue(SIZE);
664          ArrayList l = new ArrayList();
665          q.drainTo(l);
666 <        assertEquals(q.size(), 0);
667 <        assertEquals(l.size(), SIZE);
666 >        assertEquals(0, q.size());
667 >        assertEquals(SIZE, l.size());
668          for (int i = 0; i < SIZE; ++i) {
669 <            assertEquals(l.get(i), i);
669 >            assertEquals(i, l.get(i));
670          }
671          q.add(zero);
672          q.add(one);
# Line 787 | Line 675 | public class LinkedTransferQueueTest ext
675          assertTrue(q.contains(one));
676          l.clear();
677          q.drainTo(l);
678 <        assertEquals(q.size(), 0);
679 <        assertEquals(l.size(), 2);
678 >        assertEquals(0, q.size());
679 >        assertEquals(2, l.size());
680          for (int i = 0; i < 2; ++i) {
681 <            assertEquals(l.get(i), i);
681 >            assertEquals(i, l.get(i));
682          }
683      }
684  
# Line 806 | Line 694 | public class LinkedTransferQueueTest ext
694          ArrayList l = new ArrayList();
695          q.drainTo(l);
696          assertTrue(l.size() >= SIZE);
697 <        for (int i = 0; i < SIZE; ++i) {
698 <            assertEquals(l.get(i), i);
699 <        }
812 <        awaitTermination(t, MEDIUM_DELAY_MS);
697 >        for (int i = 0; i < SIZE; ++i)
698 >            assertEquals(i, l.get(i));
699 >        awaitTermination(t);
700          assertTrue(q.size() + l.size() >= SIZE);
701      }
702  
703      /**
817     * drainTo(null, n) throws NullPointerException
818     */
819    public void testDrainToNullN() {
820        LinkedTransferQueue q = populatedQueue(SIZE);
821        try {
822            q.drainTo(null, SIZE);
823            shouldThrow();
824        } catch (NullPointerException success) {}
825    }
826
827    /**
828     * drainTo(this, n) throws IllegalArgumentException
829     */
830    public void testDrainToSelfN() {
831        LinkedTransferQueue q = populatedQueue(SIZE);
832        try {
833            q.drainTo(q, SIZE);
834            shouldThrow();
835        } catch (IllegalArgumentException success) {}
836    }
837
838    /**
704       * drainTo(c, n) empties first min(n, size) elements of queue into c
705       */
706      public void testDrainToN() {
# Line 847 | Line 712 | public class LinkedTransferQueueTest ext
712              ArrayList l = new ArrayList();
713              q.drainTo(l, i);
714              int k = (i < SIZE) ? i : SIZE;
715 <            assertEquals(l.size(), k);
716 <            assertEquals(q.size(), SIZE - k);
717 <            for (int j = 0; j < k; ++j) {
718 <                assertEquals(l.get(j), j);
719 <            }
855 <            while (q.poll() != null)
856 <                ;
715 >            assertEquals(k, l.size());
716 >            assertEquals(SIZE - k, q.size());
717 >            for (int j = 0; j < k; ++j)
718 >                assertEquals(j, l.get(j));
719 >            do {} while (q.poll() != null);
720          }
721      }
722  
# Line 863 | Line 726 | public class LinkedTransferQueueTest ext
726       */
727      public void testWaitingConsumer() throws InterruptedException {
728          final LinkedTransferQueue q = new LinkedTransferQueue();
729 <        assertEquals(q.getWaitingConsumerCount(), 0);
729 >        assertEquals(0, q.getWaitingConsumerCount());
730          assertFalse(q.hasWaitingConsumer());
731          final CountDownLatch threadStarted = new CountDownLatch(1);
732  
733          Thread t = newStartedThread(new CheckedRunnable() {
734              public void realRun() throws InterruptedException {
735                  threadStarted.countDown();
736 +                long startTime = System.nanoTime();
737                  assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
738 <                assertEquals(q.getWaitingConsumerCount(), 0);
738 >                assertEquals(0, q.getWaitingConsumerCount());
739                  assertFalse(q.hasWaitingConsumer());
740 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
741              }});
742  
743          threadStarted.await();
744 <        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
745 <        assertEquals(q.getWaitingConsumerCount(), 1);
746 <        assertTrue(q.hasWaitingConsumer());
744 >        Callable<Boolean> oneConsumer
745 >            = new Callable<Boolean>() { public Boolean call() {
746 >                return q.hasWaitingConsumer()
747 >                && q.getWaitingConsumerCount() == 1; }};
748 >        waitForThreadToEnterWaitState(t, oneConsumer);
749  
750          assertTrue(q.offer(one));
751 <        assertEquals(q.getWaitingConsumerCount(), 0);
751 >        assertEquals(0, q.getWaitingConsumerCount());
752          assertFalse(q.hasWaitingConsumer());
753  
754 <        awaitTermination(t, MEDIUM_DELAY_MS);
754 >        awaitTermination(t);
755      }
756  
757      /**
# Line 900 | Line 767 | public class LinkedTransferQueueTest ext
767  
768      /**
769       * transfer waits until a poll occurs. The transfered element
770 <     * is returned by this associated poll.
770 >     * is returned by the associated poll.
771       */
772      public void testTransfer2() throws InterruptedException {
773 <        final LinkedTransferQueue<Integer> q
907 <            = new LinkedTransferQueue<Integer>();
773 >        final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<>();
774          final CountDownLatch threadStarted = new CountDownLatch(1);
775  
776          Thread t = newStartedThread(new CheckedRunnable() {
777              public void realRun() throws InterruptedException {
778                  threadStarted.countDown();
779 <                q.transfer(SIZE);
779 >                q.transfer(five);
780                  checkEmpty(q);
781              }});
782  
783          threadStarted.await();
784 <        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
785 <        assertTrue(t.isAlive());
786 <        assertEquals(1, q.size());
787 <        assertEquals(SIZE, (int) q.poll());
784 >        Callable<Boolean> oneElement
785 >            = new Callable<Boolean>() { public Boolean call() {
786 >                return !q.isEmpty() && q.size() == 1; }};
787 >        waitForThreadToEnterWaitState(t, oneElement);
788 >
789 >        assertSame(five, q.poll());
790          checkEmpty(q);
791 <        awaitTermination(t, MEDIUM_DELAY_MS);
791 >        awaitTermination(t);
792      }
793  
794      /**
795       * transfer waits until a poll occurs, and then transfers in fifo order
796       */
797      public void testTransfer3() throws InterruptedException {
798 <        final LinkedTransferQueue<Integer> q
931 <            = new LinkedTransferQueue<Integer>();
798 >        final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<>();
799  
800          Thread first = newStartedThread(new CheckedRunnable() {
801              public void realRun() throws InterruptedException {
802                  q.transfer(four);
803 <                assertTrue(!q.contains(four));
803 >                assertFalse(q.contains(four));
804                  assertEquals(1, q.size());
805              }});
806  
# Line 976 | Line 843 | public class LinkedTransferQueueTest ext
843          assertEquals(1, q.size());
844          assertTrue(q.offer(three));
845          assertSame(four, q.poll());
846 <        awaitTermination(t, MEDIUM_DELAY_MS);
846 >        awaitTermination(t);
847      }
848  
849      /**
850       * transfer waits until a take occurs. The transfered element
851 <     * is returned by this associated take.
851 >     * is returned by the associated take.
852       */
853      public void testTransfer5() throws InterruptedException {
854 <        final LinkedTransferQueue<Integer> q
988 <            = new LinkedTransferQueue<Integer>();
854 >        final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<>();
855  
856          Thread t = newStartedThread(new CheckedRunnable() {
857              public void realRun() throws InterruptedException {
# Line 999 | Line 865 | public class LinkedTransferQueueTest ext
865          assertEquals(1, q.size());
866          assertSame(four, q.take());
867          checkEmpty(q);
868 <        awaitTermination(t, MEDIUM_DELAY_MS);
868 >        awaitTermination(t);
869      }
870  
871      /**
872       * tryTransfer(null) throws NullPointerException
873       */
874      public void testTryTransfer1() {
875 +        final LinkedTransferQueue q = new LinkedTransferQueue();
876          try {
1010            final LinkedTransferQueue q = new LinkedTransferQueue();
877              q.tryTransfer(null);
878              shouldThrow();
879          } catch (NullPointerException success) {}
# Line 1041 | Line 907 | public class LinkedTransferQueueTest ext
907                  assertTrue(q.tryTransfer(hotPotato));
908              }});
909  
910 <        assertSame(hotPotato, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
910 >        long startTime = System.nanoTime();
911 >        assertSame(hotPotato, q.poll(LONG_DELAY_MS, MILLISECONDS));
912 >        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
913          checkEmpty(q);
914 <        awaitTermination(t, MEDIUM_DELAY_MS);
914 >        awaitTermination(t);
915      }
916  
917      /**
# Line 1065 | Line 933 | public class LinkedTransferQueueTest ext
933  
934          assertSame(q.take(), hotPotato);
935          checkEmpty(q);
936 <        awaitTermination(t, MEDIUM_DELAY_MS);
936 >        awaitTermination(t);
937      }
938  
939      /**
940 <     * tryTransfer waits the amount given if interrupted, and
1073 <     * throws interrupted exception
940 >     * tryTransfer blocks interruptibly if no takers
941       */
942      public void testTryTransfer5() throws InterruptedException {
943          final LinkedTransferQueue q = new LinkedTransferQueue();
944 <        final CountDownLatch threadStarted = new CountDownLatch(1);
944 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
945 >        assertTrue(q.isEmpty());
946  
947          Thread t = newStartedThread(new CheckedRunnable() {
948              public void realRun() throws InterruptedException {
949 <                long t0 = System.nanoTime();
950 <                threadStarted.countDown();
949 >                long startTime = System.nanoTime();
950 >                Thread.currentThread().interrupt();
951                  try {
952                      q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS);
953                      shouldThrow();
954                  } catch (InterruptedException success) {}
955 <                assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
955 >                assertFalse(Thread.interrupted());
956 >
957 >                pleaseInterrupt.countDown();
958 >                try {
959 >                    q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS);
960 >                    shouldThrow();
961 >                } catch (InterruptedException success) {}
962 >                assertFalse(Thread.interrupted());
963 >                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
964              }});
965  
966 <        threadStarted.await();
967 <        Thread.sleep(SHORT_DELAY_MS);
966 >        await(pleaseInterrupt);
967 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
968          t.interrupt();
969 <        awaitTermination(t, MEDIUM_DELAY_MS);
969 >        awaitTermination(t);
970          checkEmpty(q);
971      }
972  
973      /**
974 <     * tryTransfer gives up after the timeout and return false
974 >     * tryTransfer gives up after the timeout and returns false
975       */
976      public void testTryTransfer6() throws InterruptedException {
977          final LinkedTransferQueue q = new LinkedTransferQueue();
978  
979          Thread t = newStartedThread(new CheckedRunnable() {
980              public void realRun() throws InterruptedException {
981 <                long t0 = System.nanoTime();
981 >                long startTime = System.nanoTime();
982                  assertFalse(q.tryTransfer(new Object(),
983 <                                          SHORT_DELAY_MS, MILLISECONDS));
984 <                assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
983 >                                          timeoutMillis(), MILLISECONDS));
984 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
985                  checkEmpty(q);
986              }});
987  
988 <        awaitTermination(t, MEDIUM_DELAY_MS);
988 >        awaitTermination(t);
989          checkEmpty(q);
990      }
991  
# Line 1123 | Line 999 | public class LinkedTransferQueueTest ext
999  
1000          Thread t = newStartedThread(new CheckedRunnable() {
1001              public void realRun() throws InterruptedException {
1002 <                assertTrue(q.tryTransfer(five, MEDIUM_DELAY_MS, MILLISECONDS));
1002 >                long startTime = System.nanoTime();
1003 >                assertTrue(q.tryTransfer(five, LONG_DELAY_MS, MILLISECONDS));
1004 >                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1005                  checkEmpty(q);
1006              }});
1007  
# Line 1133 | Line 1011 | public class LinkedTransferQueueTest ext
1011          assertSame(four, q.poll());
1012          assertSame(five, q.poll());
1013          checkEmpty(q);
1014 <        awaitTermination(t, MEDIUM_DELAY_MS);
1014 >        awaitTermination(t);
1015      }
1016  
1017      /**
1018 <     * tryTransfer attempts to enqueue into the q and fails returning
1019 <     * false not enqueueing and the successive poll is null
1018 >     * tryTransfer attempts to enqueue into the queue and fails
1019 >     * returning false not enqueueing and the successive poll is null
1020       */
1021      public void testTryTransfer8() throws InterruptedException {
1022          final LinkedTransferQueue q = new LinkedTransferQueue();
1023          assertTrue(q.offer(four));
1024          assertEquals(1, q.size());
1025 <        long t0 = System.nanoTime();
1026 <        assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS));
1027 <        assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
1025 >        long startTime = System.nanoTime();
1026 >        assertFalse(q.tryTransfer(five, timeoutMillis(), MILLISECONDS));
1027 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
1028          assertEquals(1, q.size());
1029          assertSame(four, q.poll());
1030          assertNull(q.poll());
# Line 1154 | Line 1032 | public class LinkedTransferQueueTest ext
1032      }
1033  
1034      private LinkedTransferQueue<Integer> populatedQueue(int n) {
1035 <        LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
1035 >        LinkedTransferQueue<Integer> q = new LinkedTransferQueue<>();
1036          checkEmpty(q);
1037          for (int i = 0; i < n; i++) {
1038              assertEquals(i, q.size());
# Line 1164 | Line 1042 | public class LinkedTransferQueueTest ext
1042          assertFalse(q.isEmpty());
1043          return q;
1044      }
1045 +
1046 +    /**
1047 +     * remove(null), contains(null) always return false
1048 +     */
1049 +    public void testNeverContainsNull() {
1050 +        Collection<?>[] qs = {
1051 +            new LinkedTransferQueue<Object>(),
1052 +            populatedQueue(2),
1053 +        };
1054 +
1055 +        for (Collection<?> q : qs) {
1056 +            assertFalse(q.contains(null));
1057 +            assertFalse(q.remove(null));
1058 +        }
1059 +    }
1060   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines