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.18 by jsr166, Sat Nov 21 21:00:34 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() {
372 >        Thread t = new Thread(new CheckedRunnable() {
373 >            void realRun() throws InterruptedException {
374 >                assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
375 >                assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
376                  try {
377 <                    threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
378 <                    q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
379 <                    q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
356 <                    threadShouldThrow();
357 <                } catch (InterruptedException success) {
358 <                } catch (Throwable ex) {
359 <                    threadUnexpectedException(ex);
360 <                }
377 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
378 >                    shouldThrow();
379 >                } catch (InterruptedException success) {}
380              }});
381 <        t.start();
381 >
382          Thread.sleep(SMALL_DELAY_MS);
383 <        assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
383 >        assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
384          t.interrupt();
385          t.join();
386      }
# Line 369 | Line 388 | public class LinkedTransferQueueTest ext
388      /**
389       * peek returns next element, or null if empty
390       */
391 <    public void testPeek() {
391 >    public void testPeek() throws InterruptedException {
392          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
393          for (int i = 0; i < SIZE; ++i) {
394              assertEquals(i, (int) q.peek());
# Line 378 | Line 397 | public class LinkedTransferQueueTest ext
397                         i != (int) q.peek());
398          }
399          assertNull(q.peek());
400 +        checkEmpty(q);
401      }
402  
403      /**
404       * element returns next element, or throws NoSuchElementException if empty
405       */
406 <    public void testElement() {
406 >    public void testElement() throws InterruptedException {
407          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
408          for (int i = 0; i < SIZE; ++i) {
409              assertEquals(i, (int) q.element());
# Line 392 | Line 412 | public class LinkedTransferQueueTest ext
412          try {
413              q.element();
414              shouldThrow();
415 <        } catch (NoSuchElementException success) {
416 <        }
415 >        } catch (NoSuchElementException success) {}
416 >        checkEmpty(q);
417      }
418  
419      /**
420       * remove removes next element, or throws NoSuchElementException if empty
421       */
422 <    public void testRemove() {
422 >    public void testRemove() throws InterruptedException {
423          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
424          for (int i = 0; i < SIZE; ++i) {
425              assertEquals(i, (int) q.remove());
# Line 407 | Line 427 | public class LinkedTransferQueueTest ext
427          try {
428              q.remove();
429              shouldThrow();
430 <        } catch (NoSuchElementException success) {
431 <        }
430 >        } catch (NoSuchElementException success) {}
431 >        checkEmpty(q);
432      }
433  
434      /**
435       * remove(x) removes x and returns true if present
436       */
437 <    public void testRemoveElement() {
437 >    public void testRemoveElement() throws InterruptedException {
438          LinkedTransferQueue q = populatedQueue(SIZE);
439          for (int i = 1; i < SIZE; i += 2) {
440              assertTrue(q.remove(i));
# Line 423 | Line 443 | public class LinkedTransferQueueTest ext
443              assertTrue(q.remove(i));
444              assertFalse(q.remove(i + 1));
445          }
446 <        assertTrue(q.isEmpty());
446 >        checkEmpty(q);
447      }
448  
449      /**
# Line 436 | Line 456 | public class LinkedTransferQueueTest ext
456          assertTrue(q.remove(one));
457          assertTrue(q.remove(two));
458          assertTrue(q.add(three));
459 <        assertTrue(q.take() != null);
459 >        assertTrue(q.take() == three);
460      }
461  
462      /**
# Line 454 | Line 474 | public class LinkedTransferQueueTest ext
474      /**
475       * clear removes all elements
476       */
477 <    public void testClear() {
477 >    public void testClear() throws InterruptedException {
478          LinkedTransferQueue q = populatedQueue(SIZE);
479          q.clear();
480 <        assertTrue(q.isEmpty());
461 <        assertEquals(0, q.size());
480 >        checkEmpty(q);
481          assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
482          q.add(one);
483          assertFalse(q.isEmpty());
484 +        assertEquals(1, q.size());
485          assertTrue(q.contains(one));
486          q.clear();
487 <        assertTrue(q.isEmpty());
487 >        checkEmpty(q);
488      }
489  
490      /**
# Line 482 | Line 502 | public class LinkedTransferQueueTest ext
502      }
503  
504      /**
505 <     * retainAll(c) retains only those elements of c and reports true if changed
505 >     * retainAll(c) retains only those elements of c and reports true
506 >     * if changed
507       */
508      public void testRetainAll() {
509          LinkedTransferQueue q = populatedQueue(SIZE);
# Line 501 | Line 522 | public class LinkedTransferQueueTest ext
522      }
523  
524      /**
525 <     * removeAll(c) removes only those elements of c and reports true if changed
525 >     * removeAll(c) removes only those elements of c and reports true
526 >     * if changed
527       */
528      public void testRemoveAll() {
529          for (int i = 1; i < SIZE; ++i) {
# Line 516 | Line 538 | public class LinkedTransferQueueTest ext
538      }
539  
540      /**
541 <     * toArray contains all elements
541 >     * toArray() contains all elements
542       */
543      public void testToArray() throws InterruptedException {
544          LinkedTransferQueue q = populatedQueue(SIZE);
# Line 546 | Line 568 | public class LinkedTransferQueueTest ext
568              LinkedTransferQueue q = populatedQueue(SIZE);
569              Object o[] = q.toArray(null);
570              shouldThrow();
571 <        } catch (NullPointerException success) {
550 <        }
571 >        } catch (NullPointerException success) {}
572      }
573  
574      /**
575 <     * toArray with incompatible array type throws CCE
575 >     * toArray(incompatible array type) throws CCE
576       */
577      public void testToArray1_BadArg() {
578          try {
579              LinkedTransferQueue q = populatedQueue(SIZE);
580              Object o[] = q.toArray(new String[10]);
581              shouldThrow();
582 <        } catch (ArrayStoreException success) {
562 <        }
582 >        } catch (ArrayStoreException success) {}
583      }
584  
585      /**
# Line 568 | Line 588 | public class LinkedTransferQueueTest ext
588      public void testIterator() throws InterruptedException {
589          LinkedTransferQueue q = populatedQueue(SIZE);
590          Iterator it = q.iterator();
591 +        int i = 0;
592          while (it.hasNext()) {
593 <            assertEquals(it.next(), q.take());
593 >            assertEquals(it.next(), i++);
594          }
595 +        assertEquals(i, SIZE);
596      }
597  
598      /**
599 <     * iterator.remove removes current element
599 >     * iterator.remove() removes current element
600       */
601      public void testIteratorRemove() {
602          final LinkedTransferQueue q = new LinkedTransferQueue();
# Line 596 | Line 618 | public class LinkedTransferQueueTest ext
618       * iterator ordering is FIFO
619       */
620      public void testIteratorOrdering() {
621 <        final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
621 >        final LinkedTransferQueue<Integer> q
622 >            = new LinkedTransferQueue<Integer>();
623          assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
624          q.add(one);
625          q.add(two);
# Line 643 | Line 666 | public class LinkedTransferQueueTest ext
666          q.add(one);
667          q.add(two);
668          ExecutorService executor = Executors.newFixedThreadPool(2);
669 <        executor.execute(new Runnable() {
670 <            public void run() {
671 <                try {
672 <                    threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
673 <                } catch (Throwable ex) {
651 <                    threadUnexpectedException(ex);
652 <                }
669 >
670 >        executor.execute(new CheckedRunnable() {
671 >            void realRun() {
672 >                threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS,
673 >                                         MILLISECONDS));
674              }});
675  
676 <        executor.execute(new Runnable() {
677 <            public void run() {
678 <                try {
679 <                    Thread.sleep(SMALL_DELAY_MS);
659 <                    threadAssertEquals(one, q.take());
660 <                } catch (Throwable ex) {
661 <                    threadUnexpectedException(ex);
662 <                }
676 >        executor.execute(new CheckedRunnable() {
677 >            void realRun() throws InterruptedException {
678 >                Thread.sleep(SMALL_DELAY_MS);
679 >                threadAssertEquals(one, q.take());
680              }});
681  
682          joinPool(executor);
683      }
684  
685      /**
686 <     * poll retrieves elements across Executor threads
686 >     * timed poll retrieves elements across Executor threads
687       */
688      public void testPollInExecutor() {
689          final LinkedTransferQueue q = new LinkedTransferQueue();
690          ExecutorService executor = Executors.newFixedThreadPool(2);
691 <        executor.execute(new Runnable() {
692 <            public void run() {
693 <                try {
694 <                    threadAssertNull(q.poll());
695 <                    threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
696 <                    threadAssertTrue(q.isEmpty());
697 <                } catch (Throwable  ex) {
681 <                    threadUnexpectedException(ex);
682 <                }
691 >
692 >        executor.execute(new CheckedRunnable() {
693 >            void realRun() throws InterruptedException {
694 >                threadAssertNull(q.poll());
695 >                threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS,
696 >                                                MILLISECONDS));
697 >                threadAssertTrue(q.isEmpty());
698              }});
699  
700 <        executor.execute(new Runnable() {
701 <            public void run() {
702 <                try {
703 <                    Thread.sleep(SMALL_DELAY_MS);
689 <                    q.put(one);
690 <                } catch (Throwable ex) {
691 <                    threadUnexpectedException(ex);
692 <                }
700 >        executor.execute(new CheckedRunnable() {
701 >            void realRun() throws InterruptedException {
702 >                Thread.sleep(SMALL_DELAY_MS);
703 >                q.put(one);
704              }});
705  
706          joinPool(executor);
# Line 702 | Line 713 | public class LinkedTransferQueueTest ext
713          LinkedTransferQueue q = populatedQueue(SIZE);
714  
715          ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
716 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
716 >        ObjectOutputStream out
717 >            = new ObjectOutputStream(new BufferedOutputStream(bout));
718          out.writeObject(q);
719          out.close();
720  
721 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
722 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
721 >        ByteArrayInputStream bin
722 >            = new ByteArrayInputStream(bout.toByteArray());
723 >        ObjectInputStream in
724 >            = new ObjectInputStream(new BufferedInputStream(bin));
725          LinkedTransferQueue r = (LinkedTransferQueue) in.readObject();
726  
727          assertEquals(q.size(), r.size());
# Line 724 | Line 738 | public class LinkedTransferQueueTest ext
738          try {
739              q.drainTo(null);
740              shouldThrow();
741 <        } catch (NullPointerException success) {
728 <        }
741 >        } catch (NullPointerException success) {}
742      }
743  
744      /**
# Line 736 | Line 749 | public class LinkedTransferQueueTest ext
749          try {
750              q.drainTo(q);
751              shouldThrow();
752 <        } catch (IllegalArgumentException success) {
740 <        }
752 >        } catch (IllegalArgumentException success) {}
753      }
754  
755      /**
# Line 767 | Line 779 | public class LinkedTransferQueueTest ext
779      }
780  
781      /**
782 <     * drainTo empties full queue, unblocking a waiting put.
782 >     * drainTo(c) empties full queue, unblocking a waiting put.
783       */
784      public void testDrainToWithActivePut() throws InterruptedException {
785          final LinkedTransferQueue q = populatedQueue(SIZE);
786 <        Thread t = new Thread(new Runnable() {
787 <            public void run() {
788 <                try {
777 <                    q.put(SIZE + 1);
778 <                } catch (Throwable ex) {
779 <                    threadUnexpectedException(ex);
780 <                }
786 >        Thread t = newStartedThread(new CheckedRunnable() {
787 >            void realRun() {
788 >                q.put(SIZE + 1);
789              }});
782        t.start();
790          ArrayList l = new ArrayList();
791          q.drainTo(l);
792          assertTrue(l.size() >= SIZE);
# Line 796 | Line 803 | public class LinkedTransferQueueTest ext
803      public void testDrainToNullN() {
804          LinkedTransferQueue q = populatedQueue(SIZE);
805          try {
806 <            q.drainTo(null, 0);
806 >            q.drainTo(null, SIZE);
807              shouldThrow();
808 <        } catch (NullPointerException success) {
802 <        }
808 >        } catch (NullPointerException success) {}
809      }
810  
811      /**
# Line 808 | Line 814 | public class LinkedTransferQueueTest ext
814      public void testDrainToSelfN() {
815          LinkedTransferQueue q = populatedQueue(SIZE);
816          try {
817 <            q.drainTo(q, 0);
817 >            q.drainTo(q, SIZE);
818              shouldThrow();
819 <        } catch (IllegalArgumentException success) {
814 <        }
819 >        } catch (IllegalArgumentException success) {}
820      }
821  
822      /**
# Line 837 | Line 842 | public class LinkedTransferQueueTest ext
842      }
843  
844      /**
845 <     * poll and take should decrement the waiting consumer count
845 >     * timed poll() or take() increments the waiting consumer count;
846 >     * offer(e) decrements the waiting consumer count
847       */
848      public void testWaitingConsumer() throws InterruptedException {
849          final LinkedTransferQueue q = new LinkedTransferQueue();
850 <        final ConsumerObserver waiting = new ConsumerObserver();
851 <        new Thread(new Runnable() {
852 <            public void run() {
853 <                try {
854 <                    threadAssertTrue(q.hasWaitingConsumer());
855 <                    waiting.setWaitingConsumer(q.getWaitingConsumerCount());
856 <                    threadAssertTrue(q.offer(new Object()));
857 <                } catch (Throwable ex) {
858 <                    threadUnexpectedException(ex);
859 <                }
860 <            }}).start();
861 <        assertTrue(q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS) != null);
862 <        assertTrue(q.getWaitingConsumerCount() < waiting.getWaitingConsumers());
850 >        assertEquals(q.getWaitingConsumerCount(), 0);
851 >        assertFalse(q.hasWaitingConsumer());
852 >
853 >        Thread t = newStartedThread(new CheckedRunnable() {
854 >            void realRun() throws InterruptedException {
855 >                Thread.sleep(SMALL_DELAY_MS);
856 >                threadAssertTrue(q.hasWaitingConsumer());
857 >                threadAssertEquals(q.getWaitingConsumerCount(), 1);
858 >                threadAssertTrue(q.offer(new Object()));
859 >                threadAssertFalse(q.hasWaitingConsumer());
860 >                threadAssertEquals(q.getWaitingConsumerCount(), 0);
861 >            }});
862 >
863 >        assertTrue(q.poll(LONG_DELAY_MS, MILLISECONDS) != null);
864 >        assertEquals(q.getWaitingConsumerCount(), 0);
865 >        assertFalse(q.hasWaitingConsumer());
866 >        t.join();
867      }
868  
869      /**
# Line 864 | Line 874 | public class LinkedTransferQueueTest ext
874              LinkedTransferQueue q = new LinkedTransferQueue();
875              q.transfer(null);
876              shouldThrow();
877 <        } catch (NullPointerException ex) {
868 <        }
877 >        } catch (NullPointerException success) {}
878      }
879  
880      /**
881 <     * transfer attempts to insert into the queue then wait until that
882 <     * object is removed via take or poll.
881 >     * transfer waits until a poll occurs. The transfered element
882 >     * is returned by this associated poll.
883       */
884      public void testTransfer2() throws InterruptedException {
885 <        final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
885 >        final LinkedTransferQueue<Integer> q
886 >            = new LinkedTransferQueue<Integer>();
887  
888 <        new Thread(new Runnable() {
889 <            public void run() {
890 <                try {
891 <                    q.transfer(SIZE);
892 <                    threadAssertTrue(q.isEmpty());
883 <                } catch (Throwable ex) {
884 <                    threadUnexpectedException(ex);
885 <                }
886 <            }}).start();
888 >        Thread t = newStartedThread(new CheckedRunnable() {
889 >            void realRun() throws InterruptedException {
890 >                q.transfer(SIZE);
891 >                threadAssertTrue(q.isEmpty());
892 >            }});
893  
894          Thread.sleep(SHORT_DELAY_MS);
895          assertEquals(1, q.size());
896          assertEquals(SIZE, (int) q.poll());
897          assertTrue(q.isEmpty());
898 +        t.join();
899      }
900  
901      /**
902 <     * transfer will attempt to transfer in fifo order and continue
896 <     * waiting if the element being transfered is not polled or taken
902 >     * transfer waits until a poll occurs, and then transfers in fifo order
903       */
904      public void testTransfer3() throws InterruptedException {
905 <        final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
905 >        final LinkedTransferQueue<Integer> q
906 >            = new LinkedTransferQueue<Integer>();
907  
908 <        Thread first =
909 <            new Thread(new Runnable() {
910 <                public void run() {
911 <                    try {
912 <                        Integer i = SIZE + 1;
913 <                        q.transfer(i);
914 <                        threadAssertTrue(!q.contains(i));
908 <                        threadAssertEquals(1, q.size());
909 <                    } catch (Throwable ex) {
910 <                        threadUnexpectedException(ex);
911 <                    }
912 <                }});
913 <        first.start();
908 >        Thread first = newStartedThread(new CheckedRunnable() {
909 >            void realRun() throws InterruptedException {
910 >                Integer i = SIZE + 1;
911 >                q.transfer(i);
912 >                threadAssertTrue(!q.contains(i));
913 >                threadAssertEquals(1, q.size());
914 >            }});
915  
916 <        Thread interruptedThread =
917 <            new Thread(new Runnable() {
918 <                public void run() {
919 <                    try {
920 <                        while (q.size() == 0)
921 <                            Thread.yield();
921 <                        q.transfer(SIZE);
922 <                        threadShouldThrow();
923 <                    } catch (InterruptedException success) {
924 <                    } catch (Throwable ex) {
925 <                        threadUnexpectedException(ex);
926 <                    }
916 >        Thread interruptedThread = newStartedThread(
917 >            new CheckedInterruptedRunnable() {
918 >                void realRun() throws InterruptedException {
919 >                    while (q.size() == 0)
920 >                        Thread.yield();
921 >                    q.transfer(SIZE);
922                  }});
928        interruptedThread.start();
923  
924          while (q.size() < 2)
925              Thread.yield();
# Line 940 | Line 934 | public class LinkedTransferQueueTest ext
934      }
935  
936      /**
937 <     * transfer will wait as long as a poll or take occurs if one does occur
938 <     * the waiting is finished and the thread that tries to poll/take
945 <     * wins in retrieving the element
937 >     * transfer waits until a poll occurs, at which point the polling
938 >     * thread returns the element
939       */
940      public void testTransfer4() throws InterruptedException {
941          final LinkedTransferQueue q = new LinkedTransferQueue();
942 <        new Thread(new Runnable() {
943 <            public void run() {
944 <                try {
945 <                    q.transfer(four);
946 <                    threadAssertFalse(q.contains(four));
947 <                    threadAssertEquals(three, q.poll());
948 <                } catch (Throwable ex) {
949 <                    threadUnexpectedException(ex);
950 <                }
958 <            }}).start();
959 <        Thread.sleep(MEDIUM_DELAY_MS);
942 >
943 >        Thread t = newStartedThread(new CheckedRunnable() {
944 >            void realRun() throws InterruptedException {
945 >                q.transfer(four);
946 >                threadAssertFalse(q.contains(four));
947 >                threadAssertEquals(three, q.poll());
948 >            }});
949 >
950 >        Thread.sleep(SHORT_DELAY_MS);
951          assertTrue(q.offer(three));
952          assertEquals(four, q.poll());
953 +        t.join();
954 +    }
955 +
956 +    /**
957 +     * transfer waits until a take occurs. The transfered element
958 +     * is returned by this associated take.
959 +     */
960 +    public void testTransfer5() throws InterruptedException {
961 +        final LinkedTransferQueue<Integer> q
962 +            = new LinkedTransferQueue<Integer>();
963 +
964 +        Thread t = newStartedThread(new CheckedRunnable() {
965 +            void realRun() throws InterruptedException {
966 +                q.transfer(SIZE);
967 +                checkEmpty(q);
968 +            }});
969 +
970 +        Thread.sleep(SHORT_DELAY_MS);
971 +        assertEquals(SIZE, (int) q.take());
972 +        checkEmpty(q);
973 +        t.join();
974      }
975  
976      /**
# Line 968 | Line 980 | public class LinkedTransferQueueTest ext
980          try {
981              final LinkedTransferQueue q = new LinkedTransferQueue();
982              q.tryTransfer(null);
983 <            this.shouldThrow();
984 <        } catch (NullPointerException ex) {
973 <        }
983 >            shouldThrow();
984 >        } catch (NullPointerException success) {}
985      }
986  
987      /**
988       * tryTransfer returns false and does not enqueue if there are no
989       * consumers waiting to poll or take.
990       */
991 <    public void testTryTransfer2() {
991 >    public void testTryTransfer2() throws InterruptedException {
992          final LinkedTransferQueue q = new LinkedTransferQueue();
993          assertFalse(q.tryTransfer(new Object()));
994          assertFalse(q.hasWaitingConsumer());
995 <        assertTrue(q.isEmpty());
985 <        assertEquals(0, q.size());
995 >        checkEmpty(q);
996      }
997  
998      /**
# Line 992 | Line 1002 | public class LinkedTransferQueueTest ext
1002      public void testTryTransfer3() throws InterruptedException {
1003          final Object hotPotato = new Object();
1004          final LinkedTransferQueue q = new LinkedTransferQueue();
1005 <        new Thread(new Runnable() {
1006 <            public void run() {
1007 <                try {
1008 <                    while (! q.hasWaitingConsumer())
1009 <                        Thread.yield();
1010 <                    threadAssertTrue(q.hasWaitingConsumer());
1011 <                    threadAssertTrue(q.isEmpty());
1012 <                    threadAssertTrue(q.size() == 0);
1013 <                    threadAssertTrue(q.tryTransfer(hotPotato));
1014 <                } catch (Throwable ex) {
1015 <                    threadUnexpectedException(ex);
1016 <                }
1017 <            }}).start();
1018 <        assertTrue(q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS) == hotPotato);
1009 <        assertTrue(q.isEmpty());
1005 >
1006 >        Thread t = newStartedThread(new CheckedRunnable() {
1007 >            void realRun() {
1008 >                while (! q.hasWaitingConsumer())
1009 >                    Thread.yield();
1010 >                threadAssertTrue(q.hasWaitingConsumer());
1011 >                threadAssertTrue(q.isEmpty());
1012 >                threadAssertTrue(q.size() == 0);
1013 >                threadAssertTrue(q.tryTransfer(hotPotato));
1014 >            }});
1015 >
1016 >        assertTrue(q.poll(MEDIUM_DELAY_MS, MILLISECONDS) == hotPotato);
1017 >        checkEmpty(q);
1018 >        t.join();
1019      }
1020  
1021      /**
# Line 1016 | Line 1025 | public class LinkedTransferQueueTest ext
1025      public void testTryTransfer4() throws InterruptedException {
1026          final Object hotPotato = new Object();
1027          final LinkedTransferQueue q = new LinkedTransferQueue();
1028 <        new Thread(new Runnable() {
1029 <            public void run() {
1030 <                try {
1031 <                    while (! q.hasWaitingConsumer())
1032 <                        Thread.yield();
1033 <                    threadAssertTrue(q.hasWaitingConsumer());
1034 <                    threadAssertTrue(q.isEmpty());
1035 <                    threadAssertTrue(q.size() == 0);
1036 <                    threadAssertTrue(q.tryTransfer(hotPotato));
1037 <                } catch (Throwable ex) {
1038 <                    threadUnexpectedException(ex);
1030 <                }
1031 <            }}).start();
1028 >
1029 >        Thread t = newStartedThread(new CheckedRunnable() {
1030 >            void realRun() {
1031 >                while (! q.hasWaitingConsumer())
1032 >                    Thread.yield();
1033 >                threadAssertTrue(q.hasWaitingConsumer());
1034 >                threadAssertTrue(q.isEmpty());
1035 >                threadAssertTrue(q.size() == 0);
1036 >                threadAssertTrue(q.tryTransfer(hotPotato));
1037 >            }});
1038 >
1039          assertTrue(q.take() == hotPotato);
1040 <        assertTrue(q.isEmpty());
1040 >        checkEmpty(q);
1041 >        t.join();
1042      }
1043  
1044      /**
1045 <     * tryTransfer waits the amount given if interrupted, show an
1046 <     * interrupted exception
1045 >     * tryTransfer waits the amount given if interrupted, and
1046 >     * throws interrupted exception
1047       */
1048      public void testTryTransfer5() throws InterruptedException {
1049          final LinkedTransferQueue q = new LinkedTransferQueue();
1050 <        Thread toInterrupt = new Thread(new Runnable() {
1051 <            public void run() {
1052 <                try {
1053 <                    q.tryTransfer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
1046 <                    threadShouldThrow();
1047 <                } catch (InterruptedException success) {
1048 <                } catch (Throwable ex) {
1049 <                    threadUnexpectedException(ex);
1050 <                }
1050 >
1051 >        Thread toInterrupt = newStartedThread(new CheckedInterruptedRunnable() {
1052 >            void realRun() throws InterruptedException {
1053 >                q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS);
1054              }});
1055 <        toInterrupt.start();
1055 >
1056          Thread.sleep(SMALL_DELAY_MS);
1057          toInterrupt.interrupt();
1058 +        toInterrupt.join();
1059      }
1060  
1061      /**
# Line 1059 | Line 1063 | public class LinkedTransferQueueTest ext
1063       */
1064      public void testTryTransfer6() throws InterruptedException {
1065          final LinkedTransferQueue q = new LinkedTransferQueue();
1066 <        new Thread(new Runnable() {
1067 <            public void run() {
1068 <                try {
1069 <                    threadAssertFalse(q.tryTransfer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1070 <                } catch (Throwable ex) {
1071 <                    threadUnexpectedException(ex);
1072 <                }
1073 <            }}).start();
1074 <        Thread.sleep(LONG_DELAY_MS);
1075 <        assertTrue(q.isEmpty());
1066 >
1067 >        Thread t = newStartedThread(new CheckedRunnable() {
1068 >            void realRun() throws InterruptedException {
1069 >                threadAssertFalse
1070 >                    (q.tryTransfer(new Object(),
1071 >                                   SHORT_DELAY_MS, MILLISECONDS));
1072 >            }});
1073 >
1074 >        Thread.sleep(SMALL_DELAY_MS);
1075 >        checkEmpty(q);
1076 >        t.join();
1077      }
1078  
1079      /**
# Line 1078 | Line 1083 | public class LinkedTransferQueueTest ext
1083      public void testTryTransfer7() throws InterruptedException {
1084          final LinkedTransferQueue q = new LinkedTransferQueue();
1085          assertTrue(q.offer(four));
1086 <        new Thread(new Runnable() {
1087 <            public void run() {
1088 <                try {
1089 <                    threadAssertTrue(q.tryTransfer(five, LONG_DELAY_MS, TimeUnit.MILLISECONDS));
1090 <                    threadAssertTrue(q.isEmpty());
1091 <                } catch (Throwable ex) {
1092 <                    threadUnexpectedException(ex);
1093 <                }
1089 <            }}).start();
1086 >
1087 >        Thread t = newStartedThread(new CheckedRunnable() {
1088 >            void realRun() throws InterruptedException {
1089 >                threadAssertTrue(q.tryTransfer(five,
1090 >                                               MEDIUM_DELAY_MS, MILLISECONDS));
1091 >                threadAssertTrue(q.isEmpty());
1092 >            }});
1093 >
1094          Thread.sleep(SHORT_DELAY_MS);
1095          assertEquals(2, q.size());
1096          assertEquals(four, q.poll());
1097          assertEquals(five, q.poll());
1098 <        assertTrue(q.isEmpty());
1098 >        checkEmpty(q);
1099 >        t.join();
1100      }
1101  
1102      /**
# Line 1102 | Line 1107 | public class LinkedTransferQueueTest ext
1107          final LinkedTransferQueue q = new LinkedTransferQueue();
1108          assertTrue(q.offer(four));
1109          assertEquals(1, q.size());
1110 <        assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1110 >        assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS));
1111          assertEquals(1, q.size());
1112          assertEquals(four, q.poll());
1108        threadAssertTrue(q.isEmpty());
1113          assertNull(q.poll());
1114 +        checkEmpty(q);
1115      }
1116  
1117      private LinkedTransferQueue<Integer> populatedQueue(int n) {
# Line 1120 | Line 1125 | public class LinkedTransferQueueTest ext
1125          assertFalse(q.isEmpty());
1126          return q;
1127      }
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    }
1128   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines