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.5 by jsr166, Sun Aug 2 08:17:31 2009 UTC vs.
Revision 1.27 by jsr166, Thu Oct 28 17:22:13 2010 UTC

# Line 13 | Line 13 | import java.io.ObjectInputStream;
13   import java.io.ObjectOutputStream;
14   import java.util.ArrayList;
15   import java.util.Arrays;
16 import java.util.ConcurrentModificationException;
16   import java.util.Iterator;
17 + import java.util.List;
18   import java.util.NoSuchElementException;
19   import java.util.concurrent.*;
20 + import static java.util.concurrent.TimeUnit.MILLISECONDS;
21 + import static java.util.concurrent.TimeUnit.NANOSECONDS;
22   import junit.framework.Test;
23   import junit.framework.TestSuite;
24  
25 + @SuppressWarnings({"unchecked", "rawtypes"})
26   public class LinkedTransferQueueTest extends JSR166TestCase {
27  
28 +    public static class Generic extends BlockingQueueTest {
29 +        protected BlockingQueue emptyCollection() {
30 +            return new LinkedTransferQueue();
31 +        }
32 +    }
33 +
34      public static void main(String[] args) {
35          junit.textui.TestRunner.run(suite());
36      }
37  
38      public static Test suite() {
39 <        return new TestSuite(LinkedTransferQueueTest.class);
39 >        return newTestSuite(LinkedTransferQueueTest.class,
40 >                            new Generic().testSuite());
41 >    }
42 >
43 >    void checkEmpty(LinkedTransferQueue q) throws InterruptedException {
44 >        assertTrue(q.isEmpty());
45 >        assertEquals(0, q.size());
46 >        assertNull(q.peek());
47 >        assertNull(q.poll());
48 >        assertNull(q.poll(0, MILLISECONDS));
49 >        assertEquals(q.toString(), "[]");
50 >        assertTrue(Arrays.equals(q.toArray(), new Object[0]));
51 >        assertFalse(q.iterator().hasNext());
52 >        try {
53 >            q.element();
54 >            shouldThrow();
55 >        } catch (NoSuchElementException success) {}
56 >        try {
57 >            q.iterator().next();
58 >            shouldThrow();
59 >        } catch (NoSuchElementException success) {}
60 >        try {
61 >            q.remove();
62 >            shouldThrow();
63 >        } catch (NoSuchElementException success) {}
64      }
65  
66      /**
67 <     * Constructor builds new queue with size being zero and empty being true
67 >     * Constructor builds new queue with size being zero and empty
68 >     * being true
69       */
70      public void testConstructor1() {
71          assertEquals(0, new LinkedTransferQueue().size());
# Line 39 | Line 73 | public class LinkedTransferQueueTest ext
73      }
74  
75      /**
76 <     * Initializing constructor with null collection throws NullPointerException
76 >     * Initializing constructor with null collection throws
77 >     * NullPointerException
78       */
79      public void testConstructor2() {
80          try {
81              new LinkedTransferQueue(null);
82              shouldThrow();
83 <        } catch (NullPointerException success) {
49 <        }
83 >        } catch (NullPointerException success) {}
84      }
85  
86      /**
87 <     * Initializing from Collection of null elements throws NullPointerException
87 >     * Initializing from Collection of null elements throws
88 >     * NullPointerException
89       */
90      public void testConstructor3() {
91          try {
92              Integer[] ints = new Integer[SIZE];
93 <            LinkedTransferQueue q = new LinkedTransferQueue(Arrays.asList(ints));
93 >            new LinkedTransferQueue(Arrays.asList(ints));
94              shouldThrow();
95 <        } catch (NullPointerException success) {
61 <        }
95 >        } catch (NullPointerException success) {}
96      }
97  
98      /**
# Line 71 | Line 105 | public class LinkedTransferQueueTest ext
105              for (int i = 0; i < SIZE - 1; ++i) {
106                  ints[i] = i;
107              }
108 <            LinkedTransferQueue q = new LinkedTransferQueue(Arrays.asList(ints));
108 >            new LinkedTransferQueue(Arrays.asList(ints));
109              shouldThrow();
110 <        } catch (NullPointerException success) {
77 <        }
110 >        } catch (NullPointerException success) {}
111      }
112  
113      /**
114       * Queue contains all elements of the collection it is initialized by
115       */
116      public void testConstructor5() {
117 <        try {
118 <            Integer[] ints = new Integer[SIZE];
119 <            for (int i = 0; i < SIZE; ++i) {
120 <                ints[i] = i;
121 <            }
122 <            LinkedTransferQueue q = new LinkedTransferQueue(Arrays.asList(ints));
123 <            for (int i = 0; i < SIZE; ++i) {
124 <                assertEquals(ints[i], q.poll());
125 <            }
126 <        } finally {
117 >        Integer[] ints = new Integer[SIZE];
118 >        for (int i = 0; i < SIZE; ++i) {
119 >            ints[i] = i;
120 >        }
121 >        List intList = Arrays.asList(ints);
122 >        LinkedTransferQueue q
123 >            = new LinkedTransferQueue(intList);
124 >        assertEquals(q.size(), intList.size());
125 >        assertEquals(q.toString(), intList.toString());
126 >        assertTrue(Arrays.equals(q.toArray(),
127 >                                     intList.toArray()));
128 >        assertTrue(Arrays.equals(q.toArray(new Object[0]),
129 >                                 intList.toArray(new Object[0])));
130 >        assertTrue(Arrays.equals(q.toArray(new Object[SIZE]),
131 >                                 intList.toArray(new Object[SIZE])));
132 >        for (int i = 0; i < SIZE; ++i) {
133 >            assertEquals(ints[i], q.poll());
134          }
135      }
136  
137      /**
138 <     * Remaining capacity never decrease nor increase on add or remove
138 >     * remainingCapacity() always returns Integer.MAX_VALUE
139       */
140      public void testRemainingCapacity() {
141          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
# Line 119 | Line 159 | public class LinkedTransferQueueTest ext
159              LinkedTransferQueue q = new LinkedTransferQueue();
160              q.offer(null);
161              shouldThrow();
162 <        } catch (NullPointerException success) {
123 <        }
162 >        } catch (NullPointerException success) {}
163      }
164  
165      /**
# Line 131 | Line 170 | public class LinkedTransferQueueTest ext
170              LinkedTransferQueue q = new LinkedTransferQueue();
171              q.add(null);
172              shouldThrow();
173 <        } catch (NullPointerException success) {
135 <        }
173 >        } catch (NullPointerException success) {}
174      }
175  
176      /**
# Line 143 | Line 181 | public class LinkedTransferQueueTest ext
181              LinkedTransferQueue q = new LinkedTransferQueue();
182              q.addAll(null);
183              shouldThrow();
184 <        } catch (NullPointerException success) {
147 <        }
184 >        } catch (NullPointerException success) {}
185      }
186  
187      /**
# Line 155 | Line 192 | public class LinkedTransferQueueTest ext
192              LinkedTransferQueue q = populatedQueue(SIZE);
193              q.addAll(q);
194              shouldThrow();
195 <        } catch (IllegalArgumentException success) {
159 <        }
195 >        } catch (IllegalArgumentException success) {}
196      }
197  
198      /**
# Line 168 | Line 204 | public class LinkedTransferQueueTest ext
204              Integer[] ints = new Integer[SIZE];
205              q.addAll(Arrays.asList(ints));
206              shouldThrow();
207 <        } catch (NullPointerException success) {
172 <        }
207 >        } catch (NullPointerException success) {}
208      }
209  
210      /**
211 <     * addAll of a collection with any null elements throws NullPointerException after
212 <     * possibly adding some elements
211 >     * addAll of a collection with any null elements throws
212 >     * NullPointerException after possibly adding some elements
213       */
214      public void testAddAll3() {
215          try {
# Line 185 | Line 220 | public class LinkedTransferQueueTest ext
220              }
221              q.addAll(Arrays.asList(ints));
222              shouldThrow();
223 <        } catch (NullPointerException success) {
189 <        }
223 >        } catch (NullPointerException success) {}
224      }
225  
226      /**
# Line 223 | Line 257 | public class LinkedTransferQueueTest ext
257      public void testPut() {
258          LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
259          for (int i = 0; i < SIZE; ++i) {
260 +            assertEquals(q.size(), i);
261              q.put(i);
262              assertTrue(q.contains(i));
263          }
229        assertEquals(q.size(), SIZE);
264      }
265  
266      /**
# Line 244 | Line 278 | public class LinkedTransferQueueTest ext
278       */
279      public void testTakeFromEmpty() throws InterruptedException {
280          final LinkedTransferQueue q = new LinkedTransferQueue();
281 <        Thread t = new Thread(new Runnable() {
282 <            public void run() {
283 <                try {
250 <                    q.take();
251 <                    threadShouldThrow();
252 <                } catch (InterruptedException success) {
253 <                } catch (Throwable ex) {
254 <                    threadUnexpectedException(ex);
255 <                }
281 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
282 >            public void realRun() throws InterruptedException {
283 >                q.take();
284              }});
257        t.start();
285          Thread.sleep(SHORT_DELAY_MS);
286          t.interrupt();
287          t.join();
# Line 264 | Line 291 | public class LinkedTransferQueueTest ext
291       * Take removes existing elements until empty, then blocks interruptibly
292       */
293      public void testBlockingTake() throws InterruptedException {
294 <        Thread t = new Thread(new Runnable() {
295 <            public void run() {
294 >        final LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
295 >        Thread t = new Thread(new CheckedRunnable() {
296 >            public void realRun() throws InterruptedException {
297 >                for (int i = 0; i < SIZE; ++i) {
298 >                    assertEquals(i, (int) q.take());
299 >                }
300                  try {
270                    LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
271                    for (int i = 0; i < SIZE; ++i) {
272                        threadAssertEquals(i, (int) q.take());
273                    }
301                      q.take();
302 <                    threadShouldThrow();
303 <                } catch (InterruptedException success) {
277 <                } catch (Throwable ex) {
278 <                    threadUnexpectedException(ex);
279 <                }
302 >                    shouldThrow();
303 >                } catch (InterruptedException success) {}
304              }});
305 +
306          t.start();
307          Thread.sleep(SHORT_DELAY_MS);
308          t.interrupt();
309          t.join();
310 +        checkEmpty(q);
311      }
312  
313      /**
314       * poll succeeds unless empty
315       */
316 <    public void testPoll() {
316 >    public void testPoll() throws InterruptedException {
317          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
318          for (int i = 0; i < SIZE; ++i) {
319              assertEquals(i, (int) q.poll());
320          }
321          assertNull(q.poll());
322 +        checkEmpty(q);
323      }
324  
325      /**
# Line 301 | Line 328 | public class LinkedTransferQueueTest ext
328      public void testTimedPoll0() throws InterruptedException {
329          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
330          for (int i = 0; i < SIZE; ++i) {
331 <            assertEquals(i, (int) q.poll(0, TimeUnit.MILLISECONDS));
331 >            assertEquals(i, (int) q.poll(0, MILLISECONDS));
332          }
333 <        assertNull(q.poll(0, TimeUnit.MILLISECONDS));
333 >        assertNull(q.poll(0, MILLISECONDS));
334 >        checkEmpty(q);
335      }
336  
337      /**
# Line 312 | Line 340 | public class LinkedTransferQueueTest ext
340      public void testTimedPoll() throws InterruptedException {
341          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
342          for (int i = 0; i < SIZE; ++i) {
343 <            assertEquals(i, (int) q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
343 >            long t0 = System.nanoTime();
344 >            assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
345 >            long millisElapsed = (System.nanoTime() - t0)/(1024 * 1024);
346 >            assertTrue(millisElapsed < SMALL_DELAY_MS);
347          }
348 <        assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
348 >        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
349 >        checkEmpty(q);
350      }
351  
352      /**
# Line 322 | Line 354 | public class LinkedTransferQueueTest ext
354       * returning timeout status
355       */
356      public void testInterruptedTimedPoll() throws InterruptedException {
357 <        Thread t = new Thread(new Runnable() {
358 <            public void run() {
359 <                try {
360 <                    LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
361 <                    for (int i = 0; i < SIZE; ++i) {
362 <                        threadAssertEquals(i, (int) q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
363 <                    }
364 <                    threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
333 <                } catch (InterruptedException success) {
334 <                } catch (Throwable ex) {
335 <                    threadUnexpectedException(ex);
357 >        final LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
358 >        Thread t = newStartedThread(new CheckedRunnable() {
359 >            public void realRun() throws InterruptedException {
360 >                for (int i = 0; i < SIZE; ++i) {
361 >                    long t0 = System.nanoTime();
362 >                    assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
363 >                    long millisElapsed = (System.nanoTime() - t0)/(1024 * 1024);
364 >                    assertTrue(millisElapsed < SMALL_DELAY_MS);
365                  }
337            }});
338        t.start();
339        Thread.sleep(SHORT_DELAY_MS);
340        t.interrupt();
341        t.join();
342    }
343
344    /**
345     * timed poll before a delayed offer fails; after offer succeeds;
346     * on interruption throws
347     */
348    public void testTimedPollWithOffer() throws InterruptedException {
349        final LinkedTransferQueue q = new LinkedTransferQueue();
350        Thread t = new Thread(new Runnable() {
351            public void run() {
366                  try {
367 <                    threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
368 <                    q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
369 <                    q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
356 <                    threadShouldThrow();
357 <                } catch (InterruptedException success) {
358 <                } catch (Throwable ex) {
359 <                    threadUnexpectedException(ex);
360 <                }
367 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
368 >                    shouldThrow();
369 >                } catch (InterruptedException success) {}
370              }});
371 <        t.start();
371 >
372          Thread.sleep(SMALL_DELAY_MS);
364        assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
373          t.interrupt();
374          t.join();
375 +        checkEmpty(q);
376      }
377  
378      /**
379       * peek returns next element, or null if empty
380       */
381 <    public void testPeek() {
381 >    public void testPeek() throws InterruptedException {
382          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
383          for (int i = 0; i < SIZE; ++i) {
384              assertEquals(i, (int) q.peek());
# Line 378 | Line 387 | public class LinkedTransferQueueTest ext
387                         i != (int) q.peek());
388          }
389          assertNull(q.peek());
390 +        checkEmpty(q);
391      }
392  
393      /**
394       * element returns next element, or throws NoSuchElementException if empty
395       */
396 <    public void testElement() {
396 >    public void testElement() throws InterruptedException {
397          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
398          for (int i = 0; i < SIZE; ++i) {
399              assertEquals(i, (int) q.element());
# Line 392 | Line 402 | public class LinkedTransferQueueTest ext
402          try {
403              q.element();
404              shouldThrow();
405 <        } catch (NoSuchElementException success) {
406 <        }
405 >        } catch (NoSuchElementException success) {}
406 >        checkEmpty(q);
407      }
408  
409      /**
410       * remove removes next element, or throws NoSuchElementException if empty
411       */
412 <    public void testRemove() {
412 >    public void testRemove() throws InterruptedException {
413          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
414          for (int i = 0; i < SIZE; ++i) {
415              assertEquals(i, (int) q.remove());
# Line 407 | Line 417 | public class LinkedTransferQueueTest ext
417          try {
418              q.remove();
419              shouldThrow();
420 <        } catch (NoSuchElementException success) {
421 <        }
420 >        } catch (NoSuchElementException success) {}
421 >        checkEmpty(q);
422      }
423  
424      /**
425       * remove(x) removes x and returns true if present
426       */
427 <    public void testRemoveElement() {
427 >    public void testRemoveElement() throws InterruptedException {
428          LinkedTransferQueue q = populatedQueue(SIZE);
429          for (int i = 1; i < SIZE; i += 2) {
430              assertTrue(q.remove(i));
# Line 423 | Line 433 | public class LinkedTransferQueueTest ext
433              assertTrue(q.remove(i));
434              assertFalse(q.remove(i + 1));
435          }
436 <        assertTrue(q.isEmpty());
436 >        checkEmpty(q);
437      }
438  
439      /**
# Line 436 | Line 446 | public class LinkedTransferQueueTest ext
446          assertTrue(q.remove(one));
447          assertTrue(q.remove(two));
448          assertTrue(q.add(three));
449 <        assertTrue(q.take() != null);
449 >        assertSame(q.take(), three);
450      }
451  
452      /**
# Line 454 | Line 464 | public class LinkedTransferQueueTest ext
464      /**
465       * clear removes all elements
466       */
467 <    public void testClear() {
467 >    public void testClear() throws InterruptedException {
468          LinkedTransferQueue q = populatedQueue(SIZE);
469          q.clear();
470 <        assertTrue(q.isEmpty());
461 <        assertEquals(0, q.size());
470 >        checkEmpty(q);
471          assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
472          q.add(one);
473          assertFalse(q.isEmpty());
474 +        assertEquals(1, q.size());
475          assertTrue(q.contains(one));
476          q.clear();
477 <        assertTrue(q.isEmpty());
477 >        checkEmpty(q);
478      }
479  
480      /**
# Line 482 | Line 492 | public class LinkedTransferQueueTest ext
492      }
493  
494      /**
495 <     * retainAll(c) retains only those elements of c and reports true if changed
495 >     * retainAll(c) retains only those elements of c and reports true
496 >     * if changed
497       */
498      public void testRetainAll() {
499          LinkedTransferQueue q = populatedQueue(SIZE);
# Line 501 | Line 512 | public class LinkedTransferQueueTest ext
512      }
513  
514      /**
515 <     * removeAll(c) removes only those elements of c and reports true if changed
515 >     * removeAll(c) removes only those elements of c and reports true
516 >     * if changed
517       */
518      public void testRemoveAll() {
519          for (int i = 1; i < SIZE; ++i) {
# Line 516 | Line 528 | public class LinkedTransferQueueTest ext
528      }
529  
530      /**
531 <     * toArray contains all elements
531 >     * toArray() contains all elements
532       */
533      public void testToArray() throws InterruptedException {
534          LinkedTransferQueue q = populatedQueue(SIZE);
# Line 542 | Line 554 | public class LinkedTransferQueueTest ext
554       * toArray(null) throws NullPointerException
555       */
556      public void testToArray_BadArg() {
557 +        LinkedTransferQueue q = populatedQueue(SIZE);
558          try {
546            LinkedTransferQueue q = populatedQueue(SIZE);
559              Object o[] = q.toArray(null);
560              shouldThrow();
561 <        } catch (NullPointerException success) {
550 <        }
561 >        } catch (NullPointerException success) {}
562      }
563  
564      /**
565 <     * toArray with incompatible array type throws CCE
565 >     * toArray(incompatible array type) throws CCE
566       */
567      public void testToArray1_BadArg() {
568 +        LinkedTransferQueue q = populatedQueue(SIZE);
569          try {
558            LinkedTransferQueue q = populatedQueue(SIZE);
570              Object o[] = q.toArray(new String[10]);
571              shouldThrow();
572 <        } catch (ArrayStoreException success) {
562 <        }
572 >        } catch (ArrayStoreException success) {}
573      }
574  
575      /**
# Line 568 | Line 578 | public class LinkedTransferQueueTest ext
578      public void testIterator() throws InterruptedException {
579          LinkedTransferQueue q = populatedQueue(SIZE);
580          Iterator it = q.iterator();
581 +        int i = 0;
582          while (it.hasNext()) {
583 <            assertEquals(it.next(), q.take());
583 >            assertEquals(it.next(), i++);
584          }
585 +        assertEquals(i, SIZE);
586      }
587  
588      /**
589 <     * iterator.remove removes current element
589 >     * iterator.remove() removes current element
590       */
591      public void testIteratorRemove() {
592          final LinkedTransferQueue q = new LinkedTransferQueue();
# Line 587 | Line 599 | public class LinkedTransferQueueTest ext
599          it.remove();
600  
601          it = q.iterator();
602 <        assertEquals(it.next(), one);
603 <        assertEquals(it.next(), three);
602 >        assertSame(it.next(), one);
603 >        assertSame(it.next(), three);
604          assertFalse(it.hasNext());
605      }
606  
# Line 596 | Line 608 | public class LinkedTransferQueueTest ext
608       * iterator ordering is FIFO
609       */
610      public void testIteratorOrdering() {
611 <        final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
611 >        final LinkedTransferQueue<Integer> q
612 >            = new LinkedTransferQueue<Integer>();
613          assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
614          q.add(one);
615          q.add(two);
# Line 643 | Line 656 | public class LinkedTransferQueueTest ext
656          q.add(one);
657          q.add(two);
658          ExecutorService executor = Executors.newFixedThreadPool(2);
659 <        executor.execute(new Runnable() {
660 <            public void run() {
661 <                try {
662 <                    threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
650 <                } catch (Throwable ex) {
651 <                    threadUnexpectedException(ex);
652 <                }
659 >
660 >        executor.execute(new CheckedRunnable() {
661 >            public void realRun() {
662 >                assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
663              }});
664  
665 <        executor.execute(new Runnable() {
666 <            public void run() {
667 <                try {
668 <                    Thread.sleep(SMALL_DELAY_MS);
659 <                    threadAssertEquals(one, q.take());
660 <                } catch (Throwable ex) {
661 <                    threadUnexpectedException(ex);
662 <                }
665 >        executor.execute(new CheckedRunnable() {
666 >            public void realRun() throws InterruptedException {
667 >                Thread.sleep(SMALL_DELAY_MS);
668 >                assertSame(one, q.take());
669              }});
670  
671          joinPool(executor);
672      }
673  
674      /**
675 <     * poll retrieves elements across Executor threads
675 >     * timed poll retrieves elements across Executor threads
676       */
677      public void testPollInExecutor() {
678          final LinkedTransferQueue q = new LinkedTransferQueue();
679          ExecutorService executor = Executors.newFixedThreadPool(2);
680 <        executor.execute(new Runnable() {
681 <            public void run() {
682 <                try {
683 <                    threadAssertNull(q.poll());
684 <                    threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
685 <                    threadAssertTrue(q.isEmpty());
680 <                } catch (Throwable  ex) {
681 <                    threadUnexpectedException(ex);
682 <                }
680 >
681 >        executor.execute(new CheckedRunnable() {
682 >            public void realRun() throws InterruptedException {
683 >                assertNull(q.poll());
684 >                assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
685 >                assertTrue(q.isEmpty());
686              }});
687  
688 <        executor.execute(new Runnable() {
689 <            public void run() {
690 <                try {
691 <                    Thread.sleep(SMALL_DELAY_MS);
689 <                    q.put(one);
690 <                } catch (Throwable ex) {
691 <                    threadUnexpectedException(ex);
692 <                }
688 >        executor.execute(new CheckedRunnable() {
689 >            public void realRun() throws InterruptedException {
690 >                Thread.sleep(SMALL_DELAY_MS);
691 >                q.put(one);
692              }});
693  
694          joinPool(executor);
# Line 702 | Line 701 | public class LinkedTransferQueueTest ext
701          LinkedTransferQueue q = populatedQueue(SIZE);
702  
703          ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
704 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
704 >        ObjectOutputStream out
705 >            = new ObjectOutputStream(new BufferedOutputStream(bout));
706          out.writeObject(q);
707          out.close();
708  
709 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
710 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
709 >        ByteArrayInputStream bin
710 >            = new ByteArrayInputStream(bout.toByteArray());
711 >        ObjectInputStream in
712 >            = new ObjectInputStream(new BufferedInputStream(bin));
713          LinkedTransferQueue r = (LinkedTransferQueue) in.readObject();
714  
715          assertEquals(q.size(), r.size());
# Line 724 | Line 726 | public class LinkedTransferQueueTest ext
726          try {
727              q.drainTo(null);
728              shouldThrow();
729 <        } catch (NullPointerException success) {
728 <        }
729 >        } catch (NullPointerException success) {}
730      }
731  
732      /**
# Line 736 | Line 737 | public class LinkedTransferQueueTest ext
737          try {
738              q.drainTo(q);
739              shouldThrow();
740 <        } catch (IllegalArgumentException success) {
740 <        }
740 >        } catch (IllegalArgumentException success) {}
741      }
742  
743      /**
# Line 767 | Line 767 | public class LinkedTransferQueueTest ext
767      }
768  
769      /**
770 <     * drainTo empties full queue, unblocking a waiting put.
770 >     * drainTo(c) empties full queue, unblocking a waiting put.
771       */
772      public void testDrainToWithActivePut() throws InterruptedException {
773          final LinkedTransferQueue q = populatedQueue(SIZE);
774 <        Thread t = new Thread(new Runnable() {
775 <            public void run() {
776 <                try {
777 <                    q.put(SIZE + 1);
778 <                } catch (Throwable ex) {
779 <                    threadUnexpectedException(ex);
780 <                }
774 >        Thread t = newStartedThread(new CheckedRunnable() {
775 >            public void realRun() {
776 >                q.put(SIZE + 1);
777              }});
782        t.start();
778          ArrayList l = new ArrayList();
779          q.drainTo(l);
780          assertTrue(l.size() >= SIZE);
# Line 796 | Line 791 | public class LinkedTransferQueueTest ext
791      public void testDrainToNullN() {
792          LinkedTransferQueue q = populatedQueue(SIZE);
793          try {
794 <            q.drainTo(null, 0);
794 >            q.drainTo(null, SIZE);
795              shouldThrow();
796 <        } catch (NullPointerException success) {
802 <        }
796 >        } catch (NullPointerException success) {}
797      }
798  
799      /**
# Line 808 | Line 802 | public class LinkedTransferQueueTest ext
802      public void testDrainToSelfN() {
803          LinkedTransferQueue q = populatedQueue(SIZE);
804          try {
805 <            q.drainTo(q, 0);
805 >            q.drainTo(q, SIZE);
806              shouldThrow();
807 <        } catch (IllegalArgumentException success) {
814 <        }
807 >        } catch (IllegalArgumentException success) {}
808      }
809  
810      /**
811 <     * drainTo(c, n) empties first max {n, size} elements of queue into c
811 >     * drainTo(c, n) empties first min(n, size) elements of queue into c
812       */
813      public void testDrainToN() {
814          LinkedTransferQueue q = new LinkedTransferQueue();
# Line 837 | Line 830 | public class LinkedTransferQueueTest ext
830      }
831  
832      /**
833 <     * poll and take should decrement the waiting consumer count
833 >     * timed poll() or take() increments the waiting consumer count;
834 >     * offer(e) decrements the waiting consumer count
835       */
836      public void testWaitingConsumer() throws InterruptedException {
837          final LinkedTransferQueue q = new LinkedTransferQueue();
838 <        final ConsumerObserver waiting = new ConsumerObserver();
839 <        new Thread(new Runnable() {
840 <            public void run() {
841 <                try {
842 <                    threadAssertTrue(q.hasWaitingConsumer());
843 <                    waiting.setWaitingConsumer(q.getWaitingConsumerCount());
844 <                    threadAssertTrue(q.offer(new Object()));
845 <                } catch (Throwable ex) {
846 <                    threadUnexpectedException(ex);
847 <                }
848 <            }}).start();
849 <        assertTrue(q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS) != null);
850 <        assertTrue(q.getWaitingConsumerCount() < waiting.getWaitingConsumers());
838 >        assertEquals(q.getWaitingConsumerCount(), 0);
839 >        assertFalse(q.hasWaitingConsumer());
840 >
841 >        Thread t = newStartedThread(new CheckedRunnable() {
842 >            public void realRun() throws InterruptedException {
843 >                Thread.sleep(SMALL_DELAY_MS);
844 >                assertTrue(q.hasWaitingConsumer());
845 >                assertEquals(q.getWaitingConsumerCount(), 1);
846 >                assertTrue(q.offer(one));
847 >                assertFalse(q.hasWaitingConsumer());
848 >                assertEquals(q.getWaitingConsumerCount(), 0);
849 >            }});
850 >
851 >        assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
852 >        assertEquals(q.getWaitingConsumerCount(), 0);
853 >        assertFalse(q.hasWaitingConsumer());
854 >        t.join();
855      }
856  
857      /**
# Line 864 | Line 862 | public class LinkedTransferQueueTest ext
862              LinkedTransferQueue q = new LinkedTransferQueue();
863              q.transfer(null);
864              shouldThrow();
865 <        } catch (NullPointerException ex) {
868 <        }
865 >        } catch (NullPointerException success) {}
866      }
867  
868      /**
869 <     * transfer attempts to insert into the queue then wait until that
870 <     * object is removed via take or poll.
869 >     * transfer waits until a poll occurs. The transfered element
870 >     * is returned by this associated poll.
871       */
872      public void testTransfer2() throws InterruptedException {
873 <        final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
873 >        final LinkedTransferQueue<Integer> q
874 >            = new LinkedTransferQueue<Integer>();
875  
876 <        new Thread(new Runnable() {
877 <            public void run() {
878 <                try {
879 <                    q.transfer(SIZE);
880 <                    threadAssertTrue(q.isEmpty());
883 <                } catch (Throwable ex) {
884 <                    threadUnexpectedException(ex);
885 <                }
886 <            }}).start();
876 >        Thread t = newStartedThread(new CheckedRunnable() {
877 >            public void realRun() throws InterruptedException {
878 >                q.transfer(SIZE);
879 >                assertTrue(q.isEmpty());
880 >            }});
881  
882          Thread.sleep(SHORT_DELAY_MS);
883          assertEquals(1, q.size());
884          assertEquals(SIZE, (int) q.poll());
885          assertTrue(q.isEmpty());
886 +        t.join();
887      }
888  
889      /**
890 <     * transfer will attempt to transfer in fifo order and continue
896 <     * waiting if the element being transfered is not polled or taken
890 >     * transfer waits until a poll occurs, and then transfers in fifo order
891       */
892      public void testTransfer3() throws InterruptedException {
893 <        final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
893 >        final LinkedTransferQueue<Integer> q
894 >            = new LinkedTransferQueue<Integer>();
895  
896 <        Thread first =
897 <            new Thread(new Runnable() {
898 <                public void run() {
899 <                    try {
900 <                        Integer i = SIZE + 1;
901 <                        q.transfer(i);
902 <                        threadAssertTrue(!q.contains(i));
908 <                        threadAssertEquals(1, q.size());
909 <                    } catch (Throwable ex) {
910 <                        threadUnexpectedException(ex);
911 <                    }
912 <                }});
913 <        first.start();
896 >        Thread first = newStartedThread(new CheckedRunnable() {
897 >            public void realRun() throws InterruptedException {
898 >                Integer i = SIZE + 1;
899 >                q.transfer(i);
900 >                assertTrue(!q.contains(i));
901 >                assertEquals(1, q.size());
902 >            }});
903  
904 <        Thread interruptedThread =
905 <            new Thread(new Runnable() {
906 <                public void run() {
907 <                    try {
908 <                        while (q.size() == 0)
909 <                            Thread.yield();
921 <                        q.transfer(SIZE);
922 <                        threadShouldThrow();
923 <                    } catch (InterruptedException success) {
924 <                    } catch (Throwable ex) {
925 <                        threadUnexpectedException(ex);
926 <                    }
904 >        Thread interruptedThread = newStartedThread(
905 >            new CheckedInterruptedRunnable() {
906 >                public void realRun() throws InterruptedException {
907 >                    while (q.size() == 0)
908 >                        Thread.yield();
909 >                    q.transfer(SIZE);
910                  }});
928        interruptedThread.start();
911  
912          while (q.size() < 2)
913              Thread.yield();
# Line 940 | Line 922 | public class LinkedTransferQueueTest ext
922      }
923  
924      /**
925 <     * transfer will wait as long as a poll or take occurs if one does occur
926 <     * the waiting is finished and the thread that tries to poll/take
945 <     * wins in retrieving the element
925 >     * transfer waits until a poll occurs, at which point the polling
926 >     * thread returns the element
927       */
928      public void testTransfer4() throws InterruptedException {
929          final LinkedTransferQueue q = new LinkedTransferQueue();
930 <        new Thread(new Runnable() {
931 <            public void run() {
932 <                try {
933 <                    q.transfer(four);
934 <                    threadAssertFalse(q.contains(four));
935 <                    threadAssertEquals(three, q.poll());
936 <                } catch (Throwable ex) {
937 <                    threadUnexpectedException(ex);
938 <                }
958 <            }}).start();
959 <        Thread.sleep(MEDIUM_DELAY_MS);
930 >
931 >        Thread t = newStartedThread(new CheckedRunnable() {
932 >            public void realRun() throws InterruptedException {
933 >                q.transfer(four);
934 >                assertFalse(q.contains(four));
935 >                assertSame(three, q.poll());
936 >            }});
937 >
938 >        Thread.sleep(SHORT_DELAY_MS);
939          assertTrue(q.offer(three));
940 <        assertEquals(four, q.poll());
940 >        assertSame(four, q.poll());
941 >        t.join();
942 >    }
943 >
944 >    /**
945 >     * transfer waits until a take occurs. The transfered element
946 >     * is returned by this associated take.
947 >     */
948 >    public void testTransfer5() throws InterruptedException {
949 >        final LinkedTransferQueue<Integer> q
950 >            = new LinkedTransferQueue<Integer>();
951 >
952 >        Thread t = newStartedThread(new CheckedRunnable() {
953 >            public void realRun() throws InterruptedException {
954 >                q.transfer(SIZE);
955 >                checkEmpty(q);
956 >            }});
957 >
958 >        Thread.sleep(SHORT_DELAY_MS);
959 >        assertEquals(SIZE, (int) q.take());
960 >        checkEmpty(q);
961 >        t.join();
962      }
963  
964      /**
# Line 968 | Line 968 | public class LinkedTransferQueueTest ext
968          try {
969              final LinkedTransferQueue q = new LinkedTransferQueue();
970              q.tryTransfer(null);
971 <            this.shouldThrow();
972 <        } catch (NullPointerException ex) {
973 <        }
971 >            shouldThrow();
972 >        } catch (NullPointerException success) {}
973      }
974  
975      /**
976       * tryTransfer returns false and does not enqueue if there are no
977       * consumers waiting to poll or take.
978       */
979 <    public void testTryTransfer2() {
979 >    public void testTryTransfer2() throws InterruptedException {
980          final LinkedTransferQueue q = new LinkedTransferQueue();
981          assertFalse(q.tryTransfer(new Object()));
982          assertFalse(q.hasWaitingConsumer());
983 <        assertTrue(q.isEmpty());
985 <        assertEquals(0, q.size());
983 >        checkEmpty(q);
984      }
985  
986      /**
# Line 992 | Line 990 | public class LinkedTransferQueueTest ext
990      public void testTryTransfer3() throws InterruptedException {
991          final Object hotPotato = new Object();
992          final LinkedTransferQueue q = new LinkedTransferQueue();
993 <        new Thread(new Runnable() {
994 <            public void run() {
995 <                try {
996 <                    while (! q.hasWaitingConsumer())
997 <                        Thread.yield();
998 <                    threadAssertTrue(q.hasWaitingConsumer());
999 <                    threadAssertTrue(q.isEmpty());
1000 <                    threadAssertTrue(q.size() == 0);
1001 <                    threadAssertTrue(q.tryTransfer(hotPotato));
1002 <                } catch (Throwable ex) {
1003 <                    threadUnexpectedException(ex);
1004 <                }
1005 <            }}).start();
1006 <        assertTrue(q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS) == hotPotato);
1009 <        assertTrue(q.isEmpty());
993 >
994 >        Thread t = newStartedThread(new CheckedRunnable() {
995 >            public void realRun() {
996 >                while (! q.hasWaitingConsumer())
997 >                    Thread.yield();
998 >                assertTrue(q.hasWaitingConsumer());
999 >                assertTrue(q.isEmpty());
1000 >                assertEquals(q.size(), 0);
1001 >                assertTrue(q.tryTransfer(hotPotato));
1002 >            }});
1003 >
1004 >        assertSame(hotPotato, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
1005 >        checkEmpty(q);
1006 >        t.join();
1007      }
1008  
1009      /**
# Line 1016 | Line 1013 | public class LinkedTransferQueueTest ext
1013      public void testTryTransfer4() throws InterruptedException {
1014          final Object hotPotato = new Object();
1015          final LinkedTransferQueue q = new LinkedTransferQueue();
1016 <        new Thread(new Runnable() {
1017 <            public void run() {
1018 <                try {
1019 <                    while (! q.hasWaitingConsumer())
1020 <                        Thread.yield();
1021 <                    threadAssertTrue(q.hasWaitingConsumer());
1022 <                    threadAssertTrue(q.isEmpty());
1023 <                    threadAssertTrue(q.size() == 0);
1024 <                    threadAssertTrue(q.tryTransfer(hotPotato));
1025 <                } catch (Throwable ex) {
1026 <                    threadUnexpectedException(ex);
1027 <                }
1028 <            }}).start();
1029 <        assertTrue(q.take() == hotPotato);
1033 <        assertTrue(q.isEmpty());
1016 >
1017 >        Thread t = newStartedThread(new CheckedRunnable() {
1018 >            public void realRun() {
1019 >                while (! q.hasWaitingConsumer())
1020 >                    Thread.yield();
1021 >                assertTrue(q.hasWaitingConsumer());
1022 >                assertTrue(q.isEmpty());
1023 >                assertEquals(q.size(), 0);
1024 >                assertTrue(q.tryTransfer(hotPotato));
1025 >            }});
1026 >
1027 >        assertSame(q.take(), hotPotato);
1028 >        checkEmpty(q);
1029 >        t.join();
1030      }
1031  
1032      /**
1033 <     * tryTransfer waits the amount given if interrupted, show an
1034 <     * interrupted exception
1033 >     * tryTransfer waits the amount given if interrupted, and
1034 >     * throws interrupted exception
1035       */
1036      public void testTryTransfer5() throws InterruptedException {
1037          final LinkedTransferQueue q = new LinkedTransferQueue();
1038 <        Thread toInterrupt = new Thread(new Runnable() {
1039 <            public void run() {
1040 <                try {
1041 <                    q.tryTransfer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
1046 <                    threadShouldThrow();
1047 <                } catch (InterruptedException success) {
1048 <                } catch (Throwable ex) {
1049 <                    threadUnexpectedException(ex);
1050 <                }
1038 >
1039 >        Thread toInterrupt = newStartedThread(new CheckedInterruptedRunnable() {
1040 >            public void realRun() throws InterruptedException {
1041 >                q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS);
1042              }});
1043 <        toInterrupt.start();
1043 >
1044          Thread.sleep(SMALL_DELAY_MS);
1045          toInterrupt.interrupt();
1046 +        toInterrupt.join();
1047      }
1048  
1049      /**
# Line 1059 | Line 1051 | public class LinkedTransferQueueTest ext
1051       */
1052      public void testTryTransfer6() throws InterruptedException {
1053          final LinkedTransferQueue q = new LinkedTransferQueue();
1054 <        new Thread(new Runnable() {
1055 <            public void run() {
1056 <                try {
1057 <                    threadAssertFalse(q.tryTransfer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1058 <                } catch (Throwable ex) {
1059 <                    threadUnexpectedException(ex);
1060 <                }
1061 <            }}).start();
1062 <        Thread.sleep(LONG_DELAY_MS);
1063 <        assertTrue(q.isEmpty());
1054 >
1055 >        Thread t = newStartedThread(new CheckedRunnable() {
1056 >            public void realRun() throws InterruptedException {
1057 >                long t0 = System.nanoTime();
1058 >                assertFalse(q.tryTransfer(new Object(),
1059 >                                          SHORT_DELAY_MS, MILLISECONDS));
1060 >                long elapsed = NANOSECONDS.toMillis(System.nanoTime() - t0);
1061 >                assertTrue(elapsed >= SHORT_DELAY_MS);
1062 >            }});
1063 >
1064 >        checkEmpty(q);
1065 >        awaitTermination(t, MEDIUM_DELAY_MS);
1066 >        checkEmpty(q);
1067      }
1068  
1069      /**
# Line 1078 | Line 1073 | public class LinkedTransferQueueTest ext
1073      public void testTryTransfer7() throws InterruptedException {
1074          final LinkedTransferQueue q = new LinkedTransferQueue();
1075          assertTrue(q.offer(four));
1076 <        new Thread(new Runnable() {
1077 <            public void run() {
1078 <                try {
1079 <                    threadAssertTrue(q.tryTransfer(five, LONG_DELAY_MS, TimeUnit.MILLISECONDS));
1080 <                    threadAssertTrue(q.isEmpty());
1081 <                } catch (Throwable ex) {
1082 <                    threadUnexpectedException(ex);
1088 <                }
1089 <            }}).start();
1076 >
1077 >        Thread t = newStartedThread(new CheckedRunnable() {
1078 >            public void realRun() throws InterruptedException {
1079 >                assertTrue(q.tryTransfer(five, MEDIUM_DELAY_MS, MILLISECONDS));
1080 >                assertTrue(q.isEmpty());
1081 >            }});
1082 >
1083          Thread.sleep(SHORT_DELAY_MS);
1084          assertEquals(2, q.size());
1085 <        assertEquals(four, q.poll());
1086 <        assertEquals(five, q.poll());
1087 <        assertTrue(q.isEmpty());
1085 >        assertSame(four, q.poll());
1086 >        assertSame(five, q.poll());
1087 >        checkEmpty(q);
1088 >        t.join();
1089      }
1090  
1091      /**
# Line 1102 | Line 1096 | public class LinkedTransferQueueTest ext
1096          final LinkedTransferQueue q = new LinkedTransferQueue();
1097          assertTrue(q.offer(four));
1098          assertEquals(1, q.size());
1099 <        assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1099 >        assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS));
1100          assertEquals(1, q.size());
1101 <        assertEquals(four, q.poll());
1108 <        threadAssertTrue(q.isEmpty());
1101 >        assertSame(four, q.poll());
1102          assertNull(q.poll());
1103 +        checkEmpty(q);
1104      }
1105  
1106      private LinkedTransferQueue<Integer> populatedQueue(int n) {
# Line 1120 | Line 1114 | public class LinkedTransferQueueTest ext
1114          assertFalse(q.isEmpty());
1115          return q;
1116      }
1123
1124    private static class ConsumerObserver {
1125
1126        private int waitingConsumers;
1127
1128        private ConsumerObserver() {
1129        }
1130
1131        private void setWaitingConsumer(int i) {
1132            this.waitingConsumers = i;
1133        }
1134
1135        private int getWaitingConsumers() {
1136            return waitingConsumers;
1137        }
1138    }
1117   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines