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.17 by jsr166, Sat Nov 21 19:45:16 2009 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 junit.framework.Test;
22   import junit.framework.TestSuite;
23  
24 + @SuppressWarnings({"unchecked", "rawtypes"})
25   public class LinkedTransferQueueTest extends JSR166TestCase {
26  
27      public static void main(String[] args) {
# Line 30 | Line 32 | public class LinkedTransferQueueTest ext
32          return new TestSuite(LinkedTransferQueueTest.class);
33      }
34  
35 +    void checkEmpty(LinkedTransferQueue q) throws InterruptedException {
36 +        assertTrue(q.isEmpty());
37 +        assertEquals(0, q.size());
38 +        assertNull(q.peek());
39 +        assertNull(q.poll());
40 +        assertNull(q.poll(0, MILLISECONDS));
41 +        assertEquals(q.toString(), "[]");
42 +        assertTrue(Arrays.equals(q.toArray(), new Object[0]));
43 +        assertFalse(q.iterator().hasNext());
44 +        try {
45 +            q.element();
46 +            shouldThrow();
47 +        } catch (NoSuchElementException success) {}
48 +        try {
49 +            q.iterator().next();
50 +            shouldThrow();
51 +        } catch (NoSuchElementException success) {}
52 +        try {
53 +            q.remove();
54 +            shouldThrow();
55 +        } catch (NoSuchElementException success) {}
56 +    }
57 +
58      /**
59 <     * Constructor builds new queue with size being zero and empty being true
59 >     * Constructor builds new queue with size being zero and empty
60 >     * being true
61       */
62      public void testConstructor1() {
63          assertEquals(0, new LinkedTransferQueue().size());
# Line 39 | Line 65 | public class LinkedTransferQueueTest ext
65      }
66  
67      /**
68 <     * Initializing constructor with null collection throws NullPointerException
68 >     * Initializing constructor with null collection throws
69 >     * NullPointerException
70       */
71      public void testConstructor2() {
72          try {
73              new LinkedTransferQueue(null);
74              shouldThrow();
75 <        } catch (NullPointerException success) {
49 <        }
75 >        } catch (NullPointerException success) {}
76      }
77  
78      /**
79 <     * Initializing from Collection of null elements throws NullPointerException
79 >     * Initializing from Collection of null elements throws
80 >     * NullPointerException
81       */
82      public void testConstructor3() {
83          try {
84              Integer[] ints = new Integer[SIZE];
85 <            LinkedTransferQueue q = new LinkedTransferQueue(Arrays.asList(ints));
85 >            new LinkedTransferQueue(Arrays.asList(ints));
86              shouldThrow();
87 <        } catch (NullPointerException success) {
61 <        }
87 >        } catch (NullPointerException success) {}
88      }
89  
90      /**
# Line 71 | Line 97 | public class LinkedTransferQueueTest ext
97              for (int i = 0; i < SIZE - 1; ++i) {
98                  ints[i] = i;
99              }
100 <            LinkedTransferQueue q = new LinkedTransferQueue(Arrays.asList(ints));
100 >            new LinkedTransferQueue(Arrays.asList(ints));
101              shouldThrow();
102 <        } catch (NullPointerException success) {
77 <        }
102 >        } catch (NullPointerException success) {}
103      }
104  
105      /**
106       * Queue contains all elements of the collection it is initialized by
107       */
108      public void testConstructor5() {
109 <        try {
110 <            Integer[] ints = new Integer[SIZE];
111 <            for (int i = 0; i < SIZE; ++i) {
112 <                ints[i] = i;
113 <            }
114 <            LinkedTransferQueue q = new LinkedTransferQueue(Arrays.asList(ints));
115 <            for (int i = 0; i < SIZE; ++i) {
116 <                assertEquals(ints[i], q.poll());
117 <            }
118 <        } finally {
109 >        Integer[] ints = new Integer[SIZE];
110 >        for (int i = 0; i < SIZE; ++i) {
111 >            ints[i] = i;
112 >        }
113 >        List intList = Arrays.asList(ints);
114 >        LinkedTransferQueue q
115 >            = new LinkedTransferQueue(intList);
116 >        assertEquals(q.size(), intList.size());
117 >        assertEquals(q.toString(), intList.toString());
118 >        assertTrue(Arrays.equals(q.toArray(),
119 >                                     intList.toArray()));
120 >        assertTrue(Arrays.equals(q.toArray(new Object[0]),
121 >                                 intList.toArray(new Object[0])));
122 >        assertTrue(Arrays.equals(q.toArray(new Object[SIZE]),
123 >                                 intList.toArray(new Object[SIZE])));
124 >        for (int i = 0; i < SIZE; ++i) {
125 >            assertEquals(ints[i], q.poll());
126          }
127      }
128  
129      /**
130 <     * Remaining capacity never decrease nor increase on add or remove
130 >     * remainingCapacity() always returns Integer.MAX_VALUE
131       */
132      public void testRemainingCapacity() {
133          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
# Line 119 | Line 151 | public class LinkedTransferQueueTest ext
151              LinkedTransferQueue q = new LinkedTransferQueue();
152              q.offer(null);
153              shouldThrow();
154 <        } catch (NullPointerException success) {
123 <        }
154 >        } catch (NullPointerException success) {}
155      }
156  
157      /**
# Line 131 | Line 162 | public class LinkedTransferQueueTest ext
162              LinkedTransferQueue q = new LinkedTransferQueue();
163              q.add(null);
164              shouldThrow();
165 <        } catch (NullPointerException success) {
135 <        }
165 >        } catch (NullPointerException success) {}
166      }
167  
168      /**
# Line 143 | Line 173 | public class LinkedTransferQueueTest ext
173              LinkedTransferQueue q = new LinkedTransferQueue();
174              q.addAll(null);
175              shouldThrow();
176 <        } catch (NullPointerException success) {
147 <        }
176 >        } catch (NullPointerException success) {}
177      }
178  
179      /**
# Line 155 | Line 184 | public class LinkedTransferQueueTest ext
184              LinkedTransferQueue q = populatedQueue(SIZE);
185              q.addAll(q);
186              shouldThrow();
187 <        } catch (IllegalArgumentException success) {
159 <        }
187 >        } catch (IllegalArgumentException success) {}
188      }
189  
190      /**
# Line 168 | Line 196 | public class LinkedTransferQueueTest ext
196              Integer[] ints = new Integer[SIZE];
197              q.addAll(Arrays.asList(ints));
198              shouldThrow();
199 <        } catch (NullPointerException success) {
172 <        }
199 >        } catch (NullPointerException success) {}
200      }
201  
202      /**
203 <     * addAll of a collection with any null elements throws NullPointerException after
204 <     * possibly adding some elements
203 >     * addAll of a collection with any null elements throws
204 >     * NullPointerException after possibly adding some elements
205       */
206      public void testAddAll3() {
207          try {
# Line 185 | Line 212 | public class LinkedTransferQueueTest ext
212              }
213              q.addAll(Arrays.asList(ints));
214              shouldThrow();
215 <        } catch (NullPointerException success) {
189 <        }
215 >        } catch (NullPointerException success) {}
216      }
217  
218      /**
# Line 223 | Line 249 | public class LinkedTransferQueueTest ext
249      public void testPut() {
250          LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
251          for (int i = 0; i < SIZE; ++i) {
252 +            assertEquals(q.size(), i);
253              q.put(i);
254              assertTrue(q.contains(i));
255          }
229        assertEquals(q.size(), SIZE);
256      }
257  
258      /**
# Line 244 | Line 270 | public class LinkedTransferQueueTest ext
270       */
271      public void testTakeFromEmpty() throws InterruptedException {
272          final LinkedTransferQueue q = new LinkedTransferQueue();
273 <        Thread t = new Thread(new Runnable() {
274 <            public void run() {
275 <                try {
250 <                    q.take();
251 <                    threadShouldThrow();
252 <                } catch (InterruptedException success) {
253 <                } catch (Throwable ex) {
254 <                    threadUnexpectedException(ex);
255 <                }
273 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
274 >            void realRun() throws InterruptedException {
275 >                q.take();
276              }});
257        t.start();
277          Thread.sleep(SHORT_DELAY_MS);
278          t.interrupt();
279          t.join();
# Line 264 | Line 283 | public class LinkedTransferQueueTest ext
283       * Take removes existing elements until empty, then blocks interruptibly
284       */
285      public void testBlockingTake() throws InterruptedException {
286 <        Thread t = new Thread(new Runnable() {
287 <            public void run() {
288 <                try {
289 <                    LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
290 <                    for (int i = 0; i < SIZE; ++i) {
272 <                        threadAssertEquals(i, (int) q.take());
273 <                    }
274 <                    q.take();
275 <                    threadShouldThrow();
276 <                } catch (InterruptedException success) {
277 <                } catch (Throwable ex) {
278 <                    threadUnexpectedException(ex);
286 >        final LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
287 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
288 >            void realRun() throws InterruptedException {
289 >                for (int i = 0; i < SIZE; ++i) {
290 >                    threadAssertEquals(i, (int) q.take());
291                  }
292 +                q.take();
293              }});
294 <        t.start();
282 <        Thread.sleep(SHORT_DELAY_MS);
294 >        Thread.sleep(SMALL_DELAY_MS);
295          t.interrupt();
296          t.join();
297 +        checkEmpty(q);
298      }
299  
300      /**
301       * poll succeeds unless empty
302       */
303 <    public void testPoll() {
303 >    public void testPoll() throws InterruptedException {
304          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
305          for (int i = 0; i < SIZE; ++i) {
306              assertEquals(i, (int) q.poll());
307          }
308          assertNull(q.poll());
309 +        checkEmpty(q);
310      }
311  
312      /**
# Line 301 | Line 315 | public class LinkedTransferQueueTest ext
315      public void testTimedPoll0() throws InterruptedException {
316          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
317          for (int i = 0; i < SIZE; ++i) {
318 <            assertEquals(i, (int) q.poll(0, TimeUnit.MILLISECONDS));
318 >            assertEquals(i, (int) q.poll(0, MILLISECONDS));
319          }
320 <        assertNull(q.poll(0, TimeUnit.MILLISECONDS));
320 >        assertNull(q.poll(0, MILLISECONDS));
321 >        checkEmpty(q);
322      }
323  
324      /**
# Line 312 | Line 327 | public class LinkedTransferQueueTest ext
327      public void testTimedPoll() throws InterruptedException {
328          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
329          for (int i = 0; i < SIZE; ++i) {
330 <            assertEquals(i, (int) q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
330 >            long t0 = System.nanoTime();
331 >            assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
332 >            long millisElapsed = (System.nanoTime() - t0)/(1024 * 1024);
333 >            assertTrue(millisElapsed < SMALL_DELAY_MS);
334          }
335 <        assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
335 >        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
336 >        checkEmpty(q);
337      }
338  
339      /**
# Line 322 | Line 341 | public class LinkedTransferQueueTest ext
341       * returning timeout status
342       */
343      public void testInterruptedTimedPoll() throws InterruptedException {
344 <        Thread t = new Thread(new Runnable() {
345 <            public void run() {
346 <                try {
347 <                    LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
348 <                    for (int i = 0; i < SIZE; ++i) {
349 <                        threadAssertEquals(i, (int) q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
350 <                    }
351 <                    threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
352 <                } catch (InterruptedException success) {
334 <                } catch (Throwable ex) {
335 <                    threadUnexpectedException(ex);
344 >        final LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
345 >        Thread t = newStartedThread(new CheckedRunnable() {
346 >            void realRun() throws InterruptedException {
347 >                for (int i = 0; i < SIZE; ++i) {
348 >                    long t0 = System.nanoTime();
349 >                    threadAssertEquals(i, (int) q.poll(LONG_DELAY_MS,
350 >                                                       MILLISECONDS));
351 >                    long millisElapsed = (System.nanoTime() - t0)/(1024 * 1024);
352 >                    assertTrue(millisElapsed < SMALL_DELAY_MS);
353                  }
354 +                try {
355 +                    q.poll(LONG_DELAY_MS, MILLISECONDS);
356 +                    shouldThrow();
357 +                } catch (InterruptedException success) {}
358              }});
359 <        t.start();
360 <        Thread.sleep(SHORT_DELAY_MS);
359 >
360 >        Thread.sleep(SMALL_DELAY_MS);
361          t.interrupt();
362          t.join();
363 +        checkEmpty(q);
364      }
365  
366      /**
# Line 347 | Line 369 | public class LinkedTransferQueueTest ext
369       */
370      public void testTimedPollWithOffer() throws InterruptedException {
371          final LinkedTransferQueue q = new LinkedTransferQueue();
372 <        Thread t = new Thread(new Runnable() {
373 <            public void run() {
374 <                try {
375 <                    threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
376 <                    q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
355 <                    q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
356 <                    threadShouldThrow();
357 <                } catch (InterruptedException success) {
358 <                } catch (Throwable ex) {
359 <                    threadUnexpectedException(ex);
360 <                }
372 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
373 >            void realRun() throws InterruptedException {
374 >                threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
375 >                q.poll(LONG_DELAY_MS, MILLISECONDS);
376 >                q.poll(LONG_DELAY_MS, MILLISECONDS);
377              }});
362        t.start();
378          Thread.sleep(SMALL_DELAY_MS);
379 <        assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
379 >        assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
380          t.interrupt();
381          t.join();
382      }
# Line 369 | Line 384 | public class LinkedTransferQueueTest ext
384      /**
385       * peek returns next element, or null if empty
386       */
387 <    public void testPeek() {
387 >    public void testPeek() throws InterruptedException {
388          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
389          for (int i = 0; i < SIZE; ++i) {
390              assertEquals(i, (int) q.peek());
# Line 378 | Line 393 | public class LinkedTransferQueueTest ext
393                         i != (int) q.peek());
394          }
395          assertNull(q.peek());
396 +        checkEmpty(q);
397      }
398  
399      /**
400       * element returns next element, or throws NoSuchElementException if empty
401       */
402 <    public void testElement() {
402 >    public void testElement() throws InterruptedException {
403          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
404          for (int i = 0; i < SIZE; ++i) {
405              assertEquals(i, (int) q.element());
# Line 392 | Line 408 | public class LinkedTransferQueueTest ext
408          try {
409              q.element();
410              shouldThrow();
411 <        } catch (NoSuchElementException success) {
412 <        }
411 >        } catch (NoSuchElementException success) {}
412 >        checkEmpty(q);
413      }
414  
415      /**
416       * remove removes next element, or throws NoSuchElementException if empty
417       */
418 <    public void testRemove() {
418 >    public void testRemove() throws InterruptedException {
419          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
420          for (int i = 0; i < SIZE; ++i) {
421              assertEquals(i, (int) q.remove());
# Line 407 | Line 423 | public class LinkedTransferQueueTest ext
423          try {
424              q.remove();
425              shouldThrow();
426 <        } catch (NoSuchElementException success) {
427 <        }
426 >        } catch (NoSuchElementException success) {}
427 >        checkEmpty(q);
428      }
429  
430      /**
431       * remove(x) removes x and returns true if present
432       */
433 <    public void testRemoveElement() {
433 >    public void testRemoveElement() throws InterruptedException {
434          LinkedTransferQueue q = populatedQueue(SIZE);
435          for (int i = 1; i < SIZE; i += 2) {
436              assertTrue(q.remove(i));
# Line 423 | Line 439 | public class LinkedTransferQueueTest ext
439              assertTrue(q.remove(i));
440              assertFalse(q.remove(i + 1));
441          }
442 <        assertTrue(q.isEmpty());
442 >        checkEmpty(q);
443      }
444  
445      /**
# Line 436 | Line 452 | public class LinkedTransferQueueTest ext
452          assertTrue(q.remove(one));
453          assertTrue(q.remove(two));
454          assertTrue(q.add(three));
455 <        assertTrue(q.take() != null);
455 >        assertTrue(q.take() == three);
456      }
457  
458      /**
# Line 454 | Line 470 | public class LinkedTransferQueueTest ext
470      /**
471       * clear removes all elements
472       */
473 <    public void testClear() {
473 >    public void testClear() throws InterruptedException {
474          LinkedTransferQueue q = populatedQueue(SIZE);
475          q.clear();
476 <        assertTrue(q.isEmpty());
461 <        assertEquals(0, q.size());
476 >        checkEmpty(q);
477          assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
478          q.add(one);
479          assertFalse(q.isEmpty());
480 +        assertEquals(1, q.size());
481          assertTrue(q.contains(one));
482          q.clear();
483 <        assertTrue(q.isEmpty());
483 >        checkEmpty(q);
484      }
485  
486      /**
# Line 482 | Line 498 | public class LinkedTransferQueueTest ext
498      }
499  
500      /**
501 <     * retainAll(c) retains only those elements of c and reports true if changed
501 >     * retainAll(c) retains only those elements of c and reports true
502 >     * if changed
503       */
504      public void testRetainAll() {
505          LinkedTransferQueue q = populatedQueue(SIZE);
# Line 501 | Line 518 | public class LinkedTransferQueueTest ext
518      }
519  
520      /**
521 <     * removeAll(c) removes only those elements of c and reports true if changed
521 >     * removeAll(c) removes only those elements of c and reports true
522 >     * if changed
523       */
524      public void testRemoveAll() {
525          for (int i = 1; i < SIZE; ++i) {
# Line 516 | Line 534 | public class LinkedTransferQueueTest ext
534      }
535  
536      /**
537 <     * toArray contains all elements
537 >     * toArray() contains all elements
538       */
539      public void testToArray() throws InterruptedException {
540          LinkedTransferQueue q = populatedQueue(SIZE);
# Line 546 | Line 564 | public class LinkedTransferQueueTest ext
564              LinkedTransferQueue q = populatedQueue(SIZE);
565              Object o[] = q.toArray(null);
566              shouldThrow();
567 <        } catch (NullPointerException success) {
550 <        }
567 >        } catch (NullPointerException success) {}
568      }
569  
570      /**
571 <     * toArray with incompatible array type throws CCE
571 >     * toArray(incompatible array type) throws CCE
572       */
573      public void testToArray1_BadArg() {
574          try {
575              LinkedTransferQueue q = populatedQueue(SIZE);
576              Object o[] = q.toArray(new String[10]);
577              shouldThrow();
578 <        } catch (ArrayStoreException success) {
562 <        }
578 >        } catch (ArrayStoreException success) {}
579      }
580  
581      /**
# Line 568 | Line 584 | public class LinkedTransferQueueTest ext
584      public void testIterator() throws InterruptedException {
585          LinkedTransferQueue q = populatedQueue(SIZE);
586          Iterator it = q.iterator();
587 +        int i = 0;
588          while (it.hasNext()) {
589 <            assertEquals(it.next(), q.take());
589 >            assertEquals(it.next(), i++);
590          }
591 +        assertEquals(i, SIZE);
592      }
593  
594      /**
595 <     * iterator.remove removes current element
595 >     * iterator.remove() removes current element
596       */
597      public void testIteratorRemove() {
598          final LinkedTransferQueue q = new LinkedTransferQueue();
# Line 596 | Line 614 | public class LinkedTransferQueueTest ext
614       * iterator ordering is FIFO
615       */
616      public void testIteratorOrdering() {
617 <        final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
617 >        final LinkedTransferQueue<Integer> q
618 >            = new LinkedTransferQueue<Integer>();
619          assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
620          q.add(one);
621          q.add(two);
# Line 643 | Line 662 | public class LinkedTransferQueueTest ext
662          q.add(one);
663          q.add(two);
664          ExecutorService executor = Executors.newFixedThreadPool(2);
665 <        executor.execute(new Runnable() {
666 <            public void run() {
667 <                try {
668 <                    threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
669 <                } catch (Throwable ex) {
651 <                    threadUnexpectedException(ex);
652 <                }
665 >
666 >        executor.execute(new CheckedRunnable() {
667 >            void realRun() {
668 >                threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS,
669 >                                         MILLISECONDS));
670              }});
671  
672 <        executor.execute(new Runnable() {
673 <            public void run() {
674 <                try {
675 <                    Thread.sleep(SMALL_DELAY_MS);
659 <                    threadAssertEquals(one, q.take());
660 <                } catch (Throwable ex) {
661 <                    threadUnexpectedException(ex);
662 <                }
672 >        executor.execute(new CheckedRunnable() {
673 >            void realRun() throws InterruptedException {
674 >                Thread.sleep(SMALL_DELAY_MS);
675 >                threadAssertEquals(one, q.take());
676              }});
677  
678          joinPool(executor);
679      }
680  
681      /**
682 <     * poll retrieves elements across Executor threads
682 >     * timed poll retrieves elements across Executor threads
683       */
684      public void testPollInExecutor() {
685          final LinkedTransferQueue q = new LinkedTransferQueue();
686          ExecutorService executor = Executors.newFixedThreadPool(2);
687 <        executor.execute(new Runnable() {
688 <            public void run() {
689 <                try {
690 <                    threadAssertNull(q.poll());
691 <                    threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
692 <                    threadAssertTrue(q.isEmpty());
693 <                } catch (Throwable  ex) {
681 <                    threadUnexpectedException(ex);
682 <                }
687 >
688 >        executor.execute(new CheckedRunnable() {
689 >            void realRun() throws InterruptedException {
690 >                threadAssertNull(q.poll());
691 >                threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS,
692 >                                                MILLISECONDS));
693 >                threadAssertTrue(q.isEmpty());
694              }});
695  
696 <        executor.execute(new Runnable() {
697 <            public void run() {
698 <                try {
699 <                    Thread.sleep(SMALL_DELAY_MS);
689 <                    q.put(one);
690 <                } catch (Throwable ex) {
691 <                    threadUnexpectedException(ex);
692 <                }
696 >        executor.execute(new CheckedRunnable() {
697 >            void realRun() throws InterruptedException {
698 >                Thread.sleep(SMALL_DELAY_MS);
699 >                q.put(one);
700              }});
701  
702          joinPool(executor);
# Line 702 | Line 709 | public class LinkedTransferQueueTest ext
709          LinkedTransferQueue q = populatedQueue(SIZE);
710  
711          ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
712 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
712 >        ObjectOutputStream out
713 >            = new ObjectOutputStream(new BufferedOutputStream(bout));
714          out.writeObject(q);
715          out.close();
716  
717 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
718 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
717 >        ByteArrayInputStream bin
718 >            = new ByteArrayInputStream(bout.toByteArray());
719 >        ObjectInputStream in
720 >            = new ObjectInputStream(new BufferedInputStream(bin));
721          LinkedTransferQueue r = (LinkedTransferQueue) in.readObject();
722  
723          assertEquals(q.size(), r.size());
# Line 724 | Line 734 | public class LinkedTransferQueueTest ext
734          try {
735              q.drainTo(null);
736              shouldThrow();
737 <        } catch (NullPointerException success) {
728 <        }
737 >        } catch (NullPointerException success) {}
738      }
739  
740      /**
# Line 736 | Line 745 | public class LinkedTransferQueueTest ext
745          try {
746              q.drainTo(q);
747              shouldThrow();
748 <        } catch (IllegalArgumentException success) {
740 <        }
748 >        } catch (IllegalArgumentException success) {}
749      }
750  
751      /**
# Line 767 | Line 775 | public class LinkedTransferQueueTest ext
775      }
776  
777      /**
778 <     * drainTo empties full queue, unblocking a waiting put.
778 >     * drainTo(c) empties full queue, unblocking a waiting put.
779       */
780      public void testDrainToWithActivePut() throws InterruptedException {
781          final LinkedTransferQueue q = populatedQueue(SIZE);
782 <        Thread t = new Thread(new Runnable() {
783 <            public void run() {
784 <                try {
777 <                    q.put(SIZE + 1);
778 <                } catch (Throwable ex) {
779 <                    threadUnexpectedException(ex);
780 <                }
782 >        Thread t = newStartedThread(new CheckedRunnable() {
783 >            void realRun() {
784 >                q.put(SIZE + 1);
785              }});
782        t.start();
786          ArrayList l = new ArrayList();
787          q.drainTo(l);
788          assertTrue(l.size() >= SIZE);
# Line 796 | Line 799 | public class LinkedTransferQueueTest ext
799      public void testDrainToNullN() {
800          LinkedTransferQueue q = populatedQueue(SIZE);
801          try {
802 <            q.drainTo(null, 0);
802 >            q.drainTo(null, SIZE);
803              shouldThrow();
804 <        } catch (NullPointerException success) {
802 <        }
804 >        } catch (NullPointerException success) {}
805      }
806  
807      /**
# Line 808 | Line 810 | public class LinkedTransferQueueTest ext
810      public void testDrainToSelfN() {
811          LinkedTransferQueue q = populatedQueue(SIZE);
812          try {
813 <            q.drainTo(q, 0);
813 >            q.drainTo(q, SIZE);
814              shouldThrow();
815 <        } catch (IllegalArgumentException success) {
814 <        }
815 >        } catch (IllegalArgumentException success) {}
816      }
817  
818      /**
# Line 837 | Line 838 | public class LinkedTransferQueueTest ext
838      }
839  
840      /**
841 <     * poll and take should decrement the waiting consumer count
841 >     * timed poll() or take() increments the waiting consumer count;
842 >     * offer(e) decrements the waiting consumer count
843       */
844      public void testWaitingConsumer() throws InterruptedException {
845          final LinkedTransferQueue q = new LinkedTransferQueue();
846 <        final ConsumerObserver waiting = new ConsumerObserver();
847 <        new Thread(new Runnable() {
848 <            public void run() {
849 <                try {
850 <                    threadAssertTrue(q.hasWaitingConsumer());
851 <                    waiting.setWaitingConsumer(q.getWaitingConsumerCount());
852 <                    threadAssertTrue(q.offer(new Object()));
853 <                } catch (Throwable ex) {
854 <                    threadUnexpectedException(ex);
855 <                }
856 <            }}).start();
857 <        assertTrue(q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS) != null);
858 <        assertTrue(q.getWaitingConsumerCount() < waiting.getWaitingConsumers());
846 >        assertEquals(q.getWaitingConsumerCount(), 0);
847 >        assertFalse(q.hasWaitingConsumer());
848 >
849 >        Thread t = newStartedThread(new CheckedRunnable() {
850 >            void realRun() throws InterruptedException {
851 >                Thread.sleep(SMALL_DELAY_MS);
852 >                threadAssertTrue(q.hasWaitingConsumer());
853 >                threadAssertEquals(q.getWaitingConsumerCount(), 1);
854 >                threadAssertTrue(q.offer(new Object()));
855 >                threadAssertFalse(q.hasWaitingConsumer());
856 >                threadAssertEquals(q.getWaitingConsumerCount(), 0);
857 >            }});
858 >
859 >        assertTrue(q.poll(LONG_DELAY_MS, MILLISECONDS) != null);
860 >        assertEquals(q.getWaitingConsumerCount(), 0);
861 >        assertFalse(q.hasWaitingConsumer());
862 >        t.join();
863      }
864  
865      /**
# Line 864 | Line 870 | public class LinkedTransferQueueTest ext
870              LinkedTransferQueue q = new LinkedTransferQueue();
871              q.transfer(null);
872              shouldThrow();
873 <        } catch (NullPointerException ex) {
868 <        }
873 >        } catch (NullPointerException success) {}
874      }
875  
876      /**
877 <     * transfer attempts to insert into the queue then wait until that
878 <     * object is removed via take or poll.
877 >     * transfer waits until a poll occurs. The transfered element
878 >     * is returned by this associated poll.
879       */
880      public void testTransfer2() throws InterruptedException {
881 <        final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
881 >        final LinkedTransferQueue<Integer> q
882 >            = new LinkedTransferQueue<Integer>();
883  
884 <        new Thread(new Runnable() {
885 <            public void run() {
886 <                try {
887 <                    q.transfer(SIZE);
888 <                    threadAssertTrue(q.isEmpty());
883 <                } catch (Throwable ex) {
884 <                    threadUnexpectedException(ex);
885 <                }
886 <            }}).start();
884 >        Thread t = newStartedThread(new CheckedRunnable() {
885 >            void realRun() throws InterruptedException {
886 >                q.transfer(SIZE);
887 >                threadAssertTrue(q.isEmpty());
888 >            }});
889  
890          Thread.sleep(SHORT_DELAY_MS);
891          assertEquals(1, q.size());
892          assertEquals(SIZE, (int) q.poll());
893          assertTrue(q.isEmpty());
894 +        t.join();
895      }
896  
897      /**
898 <     * transfer will attempt to transfer in fifo order and continue
896 <     * waiting if the element being transfered is not polled or taken
898 >     * transfer waits until a poll occurs, and then transfers in fifo order
899       */
900      public void testTransfer3() throws InterruptedException {
901 <        final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
901 >        final LinkedTransferQueue<Integer> q
902 >            = new LinkedTransferQueue<Integer>();
903  
904 <        Thread first =
905 <            new Thread(new Runnable() {
906 <                public void run() {
907 <                    try {
908 <                        Integer i = SIZE + 1;
909 <                        q.transfer(i);
910 <                        threadAssertTrue(!q.contains(i));
908 <                        threadAssertEquals(1, q.size());
909 <                    } catch (Throwable ex) {
910 <                        threadUnexpectedException(ex);
911 <                    }
912 <                }});
913 <        first.start();
904 >        Thread first = newStartedThread(new CheckedRunnable() {
905 >            void realRun() throws InterruptedException {
906 >                Integer i = SIZE + 1;
907 >                q.transfer(i);
908 >                threadAssertTrue(!q.contains(i));
909 >                threadAssertEquals(1, q.size());
910 >            }});
911  
912 <        Thread interruptedThread =
913 <            new Thread(new Runnable() {
914 <                public void run() {
915 <                    try {
916 <                        while (q.size() == 0)
917 <                            Thread.yield();
921 <                        q.transfer(SIZE);
922 <                        threadShouldThrow();
923 <                    } catch (InterruptedException success) {
924 <                    } catch (Throwable ex) {
925 <                        threadUnexpectedException(ex);
926 <                    }
912 >        Thread interruptedThread = newStartedThread(
913 >            new CheckedInterruptedRunnable() {
914 >                void realRun() throws InterruptedException {
915 >                    while (q.size() == 0)
916 >                        Thread.yield();
917 >                    q.transfer(SIZE);
918                  }});
928        interruptedThread.start();
919  
920          while (q.size() < 2)
921              Thread.yield();
# Line 940 | Line 930 | public class LinkedTransferQueueTest ext
930      }
931  
932      /**
933 <     * transfer will wait as long as a poll or take occurs if one does occur
934 <     * the waiting is finished and the thread that tries to poll/take
945 <     * wins in retrieving the element
933 >     * transfer waits until a poll occurs, at which point the polling
934 >     * thread returns the element
935       */
936      public void testTransfer4() throws InterruptedException {
937          final LinkedTransferQueue q = new LinkedTransferQueue();
938 <        new Thread(new Runnable() {
939 <            public void run() {
940 <                try {
941 <                    q.transfer(four);
942 <                    threadAssertFalse(q.contains(four));
943 <                    threadAssertEquals(three, q.poll());
944 <                } catch (Throwable ex) {
945 <                    threadUnexpectedException(ex);
946 <                }
958 <            }}).start();
959 <        Thread.sleep(MEDIUM_DELAY_MS);
938 >
939 >        Thread t = newStartedThread(new CheckedRunnable() {
940 >            void realRun() throws InterruptedException {
941 >                q.transfer(four);
942 >                threadAssertFalse(q.contains(four));
943 >                threadAssertEquals(three, q.poll());
944 >            }});
945 >
946 >        Thread.sleep(SHORT_DELAY_MS);
947          assertTrue(q.offer(three));
948          assertEquals(four, q.poll());
949 +        t.join();
950 +    }
951 +
952 +    /**
953 +     * transfer waits until a take occurs. The transfered element
954 +     * is returned by this associated take.
955 +     */
956 +    public void testTransfer5() throws InterruptedException {
957 +        final LinkedTransferQueue<Integer> q
958 +            = new LinkedTransferQueue<Integer>();
959 +
960 +        Thread t = newStartedThread(new CheckedRunnable() {
961 +            void realRun() throws InterruptedException {
962 +                q.transfer(SIZE);
963 +                checkEmpty(q);
964 +            }});
965 +
966 +        Thread.sleep(SHORT_DELAY_MS);
967 +        assertEquals(SIZE, (int) q.take());
968 +        checkEmpty(q);
969 +        t.join();
970      }
971  
972      /**
# Line 968 | Line 976 | public class LinkedTransferQueueTest ext
976          try {
977              final LinkedTransferQueue q = new LinkedTransferQueue();
978              q.tryTransfer(null);
979 <            this.shouldThrow();
980 <        } catch (NullPointerException ex) {
973 <        }
979 >            shouldThrow();
980 >        } catch (NullPointerException success) {}
981      }
982  
983      /**
984       * tryTransfer returns false and does not enqueue if there are no
985       * consumers waiting to poll or take.
986       */
987 <    public void testTryTransfer2() {
987 >    public void testTryTransfer2() throws InterruptedException {
988          final LinkedTransferQueue q = new LinkedTransferQueue();
989          assertFalse(q.tryTransfer(new Object()));
990          assertFalse(q.hasWaitingConsumer());
991 <        assertTrue(q.isEmpty());
985 <        assertEquals(0, q.size());
991 >        checkEmpty(q);
992      }
993  
994      /**
# Line 992 | Line 998 | public class LinkedTransferQueueTest ext
998      public void testTryTransfer3() throws InterruptedException {
999          final Object hotPotato = new Object();
1000          final LinkedTransferQueue q = new LinkedTransferQueue();
1001 <        new Thread(new Runnable() {
1002 <            public void run() {
1003 <                try {
1004 <                    while (! q.hasWaitingConsumer())
1005 <                        Thread.yield();
1006 <                    threadAssertTrue(q.hasWaitingConsumer());
1007 <                    threadAssertTrue(q.isEmpty());
1008 <                    threadAssertTrue(q.size() == 0);
1009 <                    threadAssertTrue(q.tryTransfer(hotPotato));
1010 <                } catch (Throwable ex) {
1011 <                    threadUnexpectedException(ex);
1012 <                }
1013 <            }}).start();
1014 <        assertTrue(q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS) == hotPotato);
1009 <        assertTrue(q.isEmpty());
1001 >
1002 >        Thread t = newStartedThread(new CheckedRunnable() {
1003 >            void realRun() {
1004 >                while (! q.hasWaitingConsumer())
1005 >                    Thread.yield();
1006 >                threadAssertTrue(q.hasWaitingConsumer());
1007 >                threadAssertTrue(q.isEmpty());
1008 >                threadAssertTrue(q.size() == 0);
1009 >                threadAssertTrue(q.tryTransfer(hotPotato));
1010 >            }});
1011 >
1012 >        assertTrue(q.poll(MEDIUM_DELAY_MS, MILLISECONDS) == hotPotato);
1013 >        checkEmpty(q);
1014 >        t.join();
1015      }
1016  
1017      /**
# Line 1016 | Line 1021 | public class LinkedTransferQueueTest ext
1021      public void testTryTransfer4() throws InterruptedException {
1022          final Object hotPotato = new Object();
1023          final LinkedTransferQueue q = new LinkedTransferQueue();
1024 <        new Thread(new Runnable() {
1025 <            public void run() {
1026 <                try {
1027 <                    while (! q.hasWaitingConsumer())
1028 <                        Thread.yield();
1029 <                    threadAssertTrue(q.hasWaitingConsumer());
1030 <                    threadAssertTrue(q.isEmpty());
1031 <                    threadAssertTrue(q.size() == 0);
1032 <                    threadAssertTrue(q.tryTransfer(hotPotato));
1033 <                } catch (Throwable ex) {
1034 <                    threadUnexpectedException(ex);
1030 <                }
1031 <            }}).start();
1024 >
1025 >        Thread t = newStartedThread(new CheckedRunnable() {
1026 >            void realRun() {
1027 >                while (! q.hasWaitingConsumer())
1028 >                    Thread.yield();
1029 >                threadAssertTrue(q.hasWaitingConsumer());
1030 >                threadAssertTrue(q.isEmpty());
1031 >                threadAssertTrue(q.size() == 0);
1032 >                threadAssertTrue(q.tryTransfer(hotPotato));
1033 >            }});
1034 >
1035          assertTrue(q.take() == hotPotato);
1036 <        assertTrue(q.isEmpty());
1036 >        checkEmpty(q);
1037 >        t.join();
1038      }
1039  
1040      /**
1041 <     * tryTransfer waits the amount given if interrupted, show an
1042 <     * interrupted exception
1041 >     * tryTransfer waits the amount given if interrupted, and
1042 >     * throws interrupted exception
1043       */
1044      public void testTryTransfer5() throws InterruptedException {
1045          final LinkedTransferQueue q = new LinkedTransferQueue();
1046 <        Thread toInterrupt = new Thread(new Runnable() {
1047 <            public void run() {
1048 <                try {
1049 <                    q.tryTransfer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
1046 <                    threadShouldThrow();
1047 <                } catch (InterruptedException success) {
1048 <                } catch (Throwable ex) {
1049 <                    threadUnexpectedException(ex);
1050 <                }
1046 >
1047 >        Thread toInterrupt = newStartedThread(new CheckedInterruptedRunnable() {
1048 >            void realRun() throws InterruptedException {
1049 >                q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS);
1050              }});
1051 <        toInterrupt.start();
1051 >
1052          Thread.sleep(SMALL_DELAY_MS);
1053          toInterrupt.interrupt();
1054 +        toInterrupt.join();
1055      }
1056  
1057      /**
# Line 1059 | Line 1059 | public class LinkedTransferQueueTest ext
1059       */
1060      public void testTryTransfer6() throws InterruptedException {
1061          final LinkedTransferQueue q = new LinkedTransferQueue();
1062 <        new Thread(new Runnable() {
1063 <            public void run() {
1064 <                try {
1065 <                    threadAssertFalse(q.tryTransfer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1066 <                } catch (Throwable ex) {
1067 <                    threadUnexpectedException(ex);
1068 <                }
1069 <            }}).start();
1070 <        Thread.sleep(LONG_DELAY_MS);
1071 <        assertTrue(q.isEmpty());
1062 >
1063 >        Thread t = newStartedThread(new CheckedRunnable() {
1064 >            void realRun() throws InterruptedException {
1065 >                threadAssertFalse
1066 >                    (q.tryTransfer(new Object(),
1067 >                                   SHORT_DELAY_MS, MILLISECONDS));
1068 >            }});
1069 >
1070 >        Thread.sleep(SMALL_DELAY_MS);
1071 >        checkEmpty(q);
1072 >        t.join();
1073      }
1074  
1075      /**
# Line 1078 | Line 1079 | public class LinkedTransferQueueTest ext
1079      public void testTryTransfer7() throws InterruptedException {
1080          final LinkedTransferQueue q = new LinkedTransferQueue();
1081          assertTrue(q.offer(four));
1082 <        new Thread(new Runnable() {
1083 <            public void run() {
1084 <                try {
1085 <                    threadAssertTrue(q.tryTransfer(five, LONG_DELAY_MS, TimeUnit.MILLISECONDS));
1086 <                    threadAssertTrue(q.isEmpty());
1087 <                } catch (Throwable ex) {
1088 <                    threadUnexpectedException(ex);
1089 <                }
1089 <            }}).start();
1082 >
1083 >        Thread t = newStartedThread(new CheckedRunnable() {
1084 >            void realRun() throws InterruptedException {
1085 >                threadAssertTrue(q.tryTransfer(five,
1086 >                                               MEDIUM_DELAY_MS, MILLISECONDS));
1087 >                threadAssertTrue(q.isEmpty());
1088 >            }});
1089 >
1090          Thread.sleep(SHORT_DELAY_MS);
1091          assertEquals(2, q.size());
1092          assertEquals(four, q.poll());
1093          assertEquals(five, q.poll());
1094 <        assertTrue(q.isEmpty());
1094 >        checkEmpty(q);
1095 >        t.join();
1096      }
1097  
1098      /**
# Line 1102 | Line 1103 | public class LinkedTransferQueueTest ext
1103          final LinkedTransferQueue q = new LinkedTransferQueue();
1104          assertTrue(q.offer(four));
1105          assertEquals(1, q.size());
1106 <        assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1106 >        assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS));
1107          assertEquals(1, q.size());
1108          assertEquals(four, q.poll());
1108        threadAssertTrue(q.isEmpty());
1109          assertNull(q.poll());
1110 +        checkEmpty(q);
1111      }
1112  
1113      private LinkedTransferQueue<Integer> populatedQueue(int n) {
# Line 1120 | Line 1121 | public class LinkedTransferQueueTest ext
1121          assertFalse(q.isEmpty());
1122          return q;
1123      }
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    }
1124   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines