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.6 by dl, Sun Aug 2 12:31:24 2009 UTC vs.
Revision 1.22 by jsr166, Tue Dec 1 06:03:49 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 >            public 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() {
286 >        final LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
287 >        Thread t = new Thread(new CheckedRunnable() {
288 >            public void realRun() throws InterruptedException {
289 >                for (int i = 0; i < SIZE; ++i) {
290 >                    assertEquals(i, (int) q.take());
291 >                }
292                  try {
270                    LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
271                    for (int i = 0; i < SIZE; ++i) {
272                        threadAssertEquals(i, (int) q.take());
273                    }
293                      q.take();
294 <                    threadShouldThrow();
295 <                } catch (InterruptedException success) {
277 <                } catch (Throwable ex) {
278 <                    threadUnexpectedException(ex);
279 <                }
294 >                    shouldThrow();
295 >                } catch (InterruptedException success) {}
296              }});
297 +
298          t.start();
299          Thread.sleep(SHORT_DELAY_MS);
300          t.interrupt();
301          t.join();
302 +        checkEmpty(q);
303      }
304  
305      /**
306       * poll succeeds unless empty
307       */
308 <    public void testPoll() {
308 >    public void testPoll() throws InterruptedException {
309          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
310          for (int i = 0; i < SIZE; ++i) {
311              assertEquals(i, (int) q.poll());
312          }
313          assertNull(q.poll());
314 +        checkEmpty(q);
315      }
316  
317      /**
# Line 301 | Line 320 | public class LinkedTransferQueueTest ext
320      public void testTimedPoll0() throws InterruptedException {
321          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
322          for (int i = 0; i < SIZE; ++i) {
323 <            assertEquals(i, (int) q.poll(0, TimeUnit.MILLISECONDS));
323 >            assertEquals(i, (int) q.poll(0, MILLISECONDS));
324          }
325 <        assertNull(q.poll(0, TimeUnit.MILLISECONDS));
325 >        assertNull(q.poll(0, MILLISECONDS));
326 >        checkEmpty(q);
327      }
328  
329      /**
# Line 312 | Line 332 | public class LinkedTransferQueueTest ext
332      public void testTimedPoll() throws InterruptedException {
333          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
334          for (int i = 0; i < SIZE; ++i) {
335 <            assertEquals(i, (int) q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
335 >            long t0 = System.nanoTime();
336 >            assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
337 >            long millisElapsed = (System.nanoTime() - t0)/(1024 * 1024);
338 >            assertTrue(millisElapsed < SMALL_DELAY_MS);
339          }
340 <        assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
340 >        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
341 >        checkEmpty(q);
342      }
343  
344      /**
# Line 322 | Line 346 | public class LinkedTransferQueueTest ext
346       * returning timeout status
347       */
348      public void testInterruptedTimedPoll() throws InterruptedException {
349 <        Thread t = new Thread(new Runnable() {
350 <            public void run() {
351 <                try {
352 <                    LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
353 <                    for (int i = 0; i < SIZE; ++i) {
354 <                        threadAssertEquals(i, (int) q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
355 <                    }
356 <                    threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
357 <                } catch (InterruptedException success) {
334 <                } catch (Throwable ex) {
335 <                    threadUnexpectedException(ex);
349 >        final LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
350 >        Thread t = newStartedThread(new CheckedRunnable() {
351 >            public void realRun() throws InterruptedException {
352 >                for (int i = 0; i < SIZE; ++i) {
353 >                    long t0 = System.nanoTime();
354 >                    threadAssertEquals(i, (int) q.poll(LONG_DELAY_MS,
355 >                                                       MILLISECONDS));
356 >                    long millisElapsed = (System.nanoTime() - t0)/(1024 * 1024);
357 >                    assertTrue(millisElapsed < SMALL_DELAY_MS);
358                  }
359 +                try {
360 +                    q.poll(LONG_DELAY_MS, MILLISECONDS);
361 +                    shouldThrow();
362 +                } catch (InterruptedException success) {}
363              }});
364 <        t.start();
365 <        Thread.sleep(SHORT_DELAY_MS);
364 >
365 >        Thread.sleep(SMALL_DELAY_MS);
366          t.interrupt();
367          t.join();
368 +        checkEmpty(q);
369      }
370  
371      /**
# Line 347 | Line 374 | public class LinkedTransferQueueTest ext
374       */
375      public void testTimedPollWithOffer() throws InterruptedException {
376          final LinkedTransferQueue q = new LinkedTransferQueue();
377 <        Thread t = new Thread(new Runnable() {
378 <            public void run() {
377 >        Thread t = new Thread(new CheckedRunnable() {
378 >            public void realRun() throws InterruptedException {
379 >                assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
380 >                assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
381                  try {
382 <                    threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
383 <                    q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
384 <                    q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
356 <                    threadShouldThrow();
357 <                } catch (InterruptedException success) {
358 <                } catch (Throwable ex) {
359 <                    threadUnexpectedException(ex);
360 <                }
382 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
383 >                    shouldThrow();
384 >                } catch (InterruptedException success) {}
385              }});
386 +
387          t.start();
388          Thread.sleep(SMALL_DELAY_MS);
389 <        assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
389 >        assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
390          t.interrupt();
391          t.join();
392      }
# Line 369 | Line 394 | public class LinkedTransferQueueTest ext
394      /**
395       * peek returns next element, or null if empty
396       */
397 <    public void testPeek() {
397 >    public void testPeek() throws InterruptedException {
398          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
399          for (int i = 0; i < SIZE; ++i) {
400              assertEquals(i, (int) q.peek());
# Line 378 | Line 403 | public class LinkedTransferQueueTest ext
403                         i != (int) q.peek());
404          }
405          assertNull(q.peek());
406 +        checkEmpty(q);
407      }
408  
409      /**
410       * element returns next element, or throws NoSuchElementException if empty
411       */
412 <    public void testElement() {
412 >    public void testElement() throws InterruptedException {
413          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
414          for (int i = 0; i < SIZE; ++i) {
415              assertEquals(i, (int) q.element());
# Line 392 | Line 418 | public class LinkedTransferQueueTest ext
418          try {
419              q.element();
420              shouldThrow();
421 <        } catch (NoSuchElementException success) {
422 <        }
421 >        } catch (NoSuchElementException success) {}
422 >        checkEmpty(q);
423      }
424  
425      /**
426       * remove removes next element, or throws NoSuchElementException if empty
427       */
428 <    public void testRemove() {
428 >    public void testRemove() throws InterruptedException {
429          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
430          for (int i = 0; i < SIZE; ++i) {
431              assertEquals(i, (int) q.remove());
# Line 407 | Line 433 | public class LinkedTransferQueueTest ext
433          try {
434              q.remove();
435              shouldThrow();
436 <        } catch (NoSuchElementException success) {
437 <        }
436 >        } catch (NoSuchElementException success) {}
437 >        checkEmpty(q);
438      }
439  
440      /**
441       * remove(x) removes x and returns true if present
442       */
443 <    public void testRemoveElement() {
443 >    public void testRemoveElement() throws InterruptedException {
444          LinkedTransferQueue q = populatedQueue(SIZE);
445          for (int i = 1; i < SIZE; i += 2) {
446              assertTrue(q.remove(i));
# Line 423 | Line 449 | public class LinkedTransferQueueTest ext
449              assertTrue(q.remove(i));
450              assertFalse(q.remove(i + 1));
451          }
452 <        assertTrue(q.isEmpty());
452 >        checkEmpty(q);
453      }
454  
455      /**
# Line 436 | Line 462 | public class LinkedTransferQueueTest ext
462          assertTrue(q.remove(one));
463          assertTrue(q.remove(two));
464          assertTrue(q.add(three));
465 <        assertTrue(q.take() != null);
465 >        assertTrue(q.take() == three);
466      }
467  
468      /**
# Line 454 | Line 480 | public class LinkedTransferQueueTest ext
480      /**
481       * clear removes all elements
482       */
483 <    public void testClear() {
483 >    public void testClear() throws InterruptedException {
484          LinkedTransferQueue q = populatedQueue(SIZE);
485          q.clear();
486 <        assertTrue(q.isEmpty());
461 <        assertEquals(0, q.size());
486 >        checkEmpty(q);
487          assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
488          q.add(one);
489          assertFalse(q.isEmpty());
490 +        assertEquals(1, q.size());
491          assertTrue(q.contains(one));
492          q.clear();
493 <        assertTrue(q.isEmpty());
493 >        checkEmpty(q);
494      }
495  
496      /**
# Line 482 | Line 508 | public class LinkedTransferQueueTest ext
508      }
509  
510      /**
511 <     * retainAll(c) retains only those elements of c and reports true if changed
511 >     * retainAll(c) retains only those elements of c and reports true
512 >     * if changed
513       */
514      public void testRetainAll() {
515          LinkedTransferQueue q = populatedQueue(SIZE);
# Line 501 | Line 528 | public class LinkedTransferQueueTest ext
528      }
529  
530      /**
531 <     * removeAll(c) removes only those elements of c and reports true if changed
531 >     * removeAll(c) removes only those elements of c and reports true
532 >     * if changed
533       */
534      public void testRemoveAll() {
535          for (int i = 1; i < SIZE; ++i) {
# Line 516 | Line 544 | public class LinkedTransferQueueTest ext
544      }
545  
546      /**
547 <     * toArray contains all elements
547 >     * toArray() contains all elements
548       */
549      public void testToArray() throws InterruptedException {
550          LinkedTransferQueue q = populatedQueue(SIZE);
# Line 542 | Line 570 | public class LinkedTransferQueueTest ext
570       * toArray(null) throws NullPointerException
571       */
572      public void testToArray_BadArg() {
573 +        LinkedTransferQueue q = populatedQueue(SIZE);
574          try {
546            LinkedTransferQueue q = populatedQueue(SIZE);
575              Object o[] = q.toArray(null);
576              shouldThrow();
577 <        } catch (NullPointerException success) {
550 <        }
577 >        } catch (NullPointerException success) {}
578      }
579  
580      /**
581 <     * toArray with incompatible array type throws CCE
581 >     * toArray(incompatible array type) throws CCE
582       */
583      public void testToArray1_BadArg() {
584 +        LinkedTransferQueue q = populatedQueue(SIZE);
585          try {
558            LinkedTransferQueue q = populatedQueue(SIZE);
586              Object o[] = q.toArray(new String[10]);
587              shouldThrow();
588 <        } catch (ArrayStoreException success) {
562 <        }
588 >        } catch (ArrayStoreException success) {}
589      }
590  
591      /**
# Line 568 | Line 594 | public class LinkedTransferQueueTest ext
594      public void testIterator() throws InterruptedException {
595          LinkedTransferQueue q = populatedQueue(SIZE);
596          Iterator it = q.iterator();
597 +        int i = 0;
598          while (it.hasNext()) {
599 <            assertEquals(it.next(), q.take());
599 >            assertEquals(it.next(), i++);
600          }
601 +        assertEquals(i, SIZE);
602      }
603  
604      /**
605 <     * iterator.remove removes current element
605 >     * iterator.remove() removes current element
606       */
607      public void testIteratorRemove() {
608          final LinkedTransferQueue q = new LinkedTransferQueue();
# Line 596 | Line 624 | public class LinkedTransferQueueTest ext
624       * iterator ordering is FIFO
625       */
626      public void testIteratorOrdering() {
627 <        final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
627 >        final LinkedTransferQueue<Integer> q
628 >            = new LinkedTransferQueue<Integer>();
629          assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
630          q.add(one);
631          q.add(two);
# Line 643 | Line 672 | public class LinkedTransferQueueTest ext
672          q.add(one);
673          q.add(two);
674          ExecutorService executor = Executors.newFixedThreadPool(2);
675 <        executor.execute(new Runnable() {
676 <            public void run() {
677 <                try {
678 <                    threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
679 <                } catch (Throwable ex) {
651 <                    threadUnexpectedException(ex);
652 <                }
675 >
676 >        executor.execute(new CheckedRunnable() {
677 >            public void realRun() {
678 >                threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS,
679 >                                         MILLISECONDS));
680              }});
681  
682 <        executor.execute(new Runnable() {
683 <            public void run() {
684 <                try {
685 <                    Thread.sleep(SMALL_DELAY_MS);
659 <                    threadAssertEquals(one, q.take());
660 <                } catch (Throwable ex) {
661 <                    threadUnexpectedException(ex);
662 <                }
682 >        executor.execute(new CheckedRunnable() {
683 >            public void realRun() throws InterruptedException {
684 >                Thread.sleep(SMALL_DELAY_MS);
685 >                threadAssertEquals(one, q.take());
686              }});
687  
688          joinPool(executor);
689      }
690  
691      /**
692 <     * poll retrieves elements across Executor threads
692 >     * timed poll retrieves elements across Executor threads
693       */
694      public void testPollInExecutor() {
695          final LinkedTransferQueue q = new LinkedTransferQueue();
696          ExecutorService executor = Executors.newFixedThreadPool(2);
697 <        executor.execute(new Runnable() {
698 <            public void run() {
699 <                try {
700 <                    threadAssertNull(q.poll());
701 <                    threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
702 <                    threadAssertTrue(q.isEmpty());
680 <                } catch (Throwable  ex) {
681 <                    threadUnexpectedException(ex);
682 <                }
697 >
698 >        executor.execute(new CheckedRunnable() {
699 >            public void realRun() throws InterruptedException {
700 >                assertNull(q.poll());
701 >                assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
702 >                assertTrue(q.isEmpty());
703              }});
704  
705 <        executor.execute(new Runnable() {
706 <            public void run() {
707 <                try {
708 <                    Thread.sleep(SMALL_DELAY_MS);
689 <                    q.put(one);
690 <                } catch (Throwable ex) {
691 <                    threadUnexpectedException(ex);
692 <                }
705 >        executor.execute(new CheckedRunnable() {
706 >            public void realRun() throws InterruptedException {
707 >                Thread.sleep(SMALL_DELAY_MS);
708 >                q.put(one);
709              }});
710  
711          joinPool(executor);
# Line 702 | Line 718 | public class LinkedTransferQueueTest ext
718          LinkedTransferQueue q = populatedQueue(SIZE);
719  
720          ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
721 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
721 >        ObjectOutputStream out
722 >            = new ObjectOutputStream(new BufferedOutputStream(bout));
723          out.writeObject(q);
724          out.close();
725  
726 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
727 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
726 >        ByteArrayInputStream bin
727 >            = new ByteArrayInputStream(bout.toByteArray());
728 >        ObjectInputStream in
729 >            = new ObjectInputStream(new BufferedInputStream(bin));
730          LinkedTransferQueue r = (LinkedTransferQueue) in.readObject();
731  
732          assertEquals(q.size(), r.size());
# Line 724 | Line 743 | public class LinkedTransferQueueTest ext
743          try {
744              q.drainTo(null);
745              shouldThrow();
746 <        } catch (NullPointerException success) {
728 <        }
746 >        } catch (NullPointerException success) {}
747      }
748  
749      /**
# Line 736 | Line 754 | public class LinkedTransferQueueTest ext
754          try {
755              q.drainTo(q);
756              shouldThrow();
757 <        } catch (IllegalArgumentException success) {
740 <        }
757 >        } catch (IllegalArgumentException success) {}
758      }
759  
760      /**
# Line 767 | Line 784 | public class LinkedTransferQueueTest ext
784      }
785  
786      /**
787 <     * drainTo empties full queue, unblocking a waiting put.
787 >     * drainTo(c) empties full queue, unblocking a waiting put.
788       */
789      public void testDrainToWithActivePut() throws InterruptedException {
790          final LinkedTransferQueue q = populatedQueue(SIZE);
791 <        Thread t = new Thread(new Runnable() {
792 <            public void run() {
793 <                try {
777 <                    q.put(SIZE + 1);
778 <                } catch (Throwable ex) {
779 <                    threadUnexpectedException(ex);
780 <                }
791 >        Thread t = newStartedThread(new CheckedRunnable() {
792 >            public void realRun() {
793 >                q.put(SIZE + 1);
794              }});
782        t.start();
795          ArrayList l = new ArrayList();
796          q.drainTo(l);
797          assertTrue(l.size() >= SIZE);
# Line 796 | Line 808 | public class LinkedTransferQueueTest ext
808      public void testDrainToNullN() {
809          LinkedTransferQueue q = populatedQueue(SIZE);
810          try {
811 <            q.drainTo(null, 0);
811 >            q.drainTo(null, SIZE);
812              shouldThrow();
813 <        } catch (NullPointerException success) {
802 <        }
813 >        } catch (NullPointerException success) {}
814      }
815  
816      /**
# Line 808 | Line 819 | public class LinkedTransferQueueTest ext
819      public void testDrainToSelfN() {
820          LinkedTransferQueue q = populatedQueue(SIZE);
821          try {
822 <            q.drainTo(q, 0);
822 >            q.drainTo(q, SIZE);
823              shouldThrow();
824 <        } catch (IllegalArgumentException success) {
814 <        }
824 >        } catch (IllegalArgumentException success) {}
825      }
826  
827      /**
# Line 837 | Line 847 | public class LinkedTransferQueueTest ext
847      }
848  
849      /**
850 <     * poll and take decrement the waiting consumer count
850 >     * timed poll() or take() increments the waiting consumer count;
851 >     * offer(e) decrements the waiting consumer count
852       */
853      public void testWaitingConsumer() throws InterruptedException {
854          final LinkedTransferQueue q = new LinkedTransferQueue();
855 <        final ConsumerObserver waiting = new ConsumerObserver();
856 <        new Thread(new Runnable() {
857 <            public void run() {
858 <                try {
859 <                    threadAssertTrue(q.hasWaitingConsumer());
860 <                    waiting.setWaitingConsumer(q.getWaitingConsumerCount());
861 <                    threadAssertTrue(q.offer(new Object()));
862 <                } catch (Throwable ex) {
863 <                    threadUnexpectedException(ex);
864 <                }
865 <            }}).start();
866 <        assertTrue(q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS) != null);
867 <        assertTrue(q.getWaitingConsumerCount() < waiting.getWaitingConsumers());
855 >        assertEquals(q.getWaitingConsumerCount(), 0);
856 >        assertFalse(q.hasWaitingConsumer());
857 >
858 >        Thread t = newStartedThread(new CheckedRunnable() {
859 >            public void realRun() throws InterruptedException {
860 >                Thread.sleep(SMALL_DELAY_MS);
861 >                assertTrue(q.hasWaitingConsumer());
862 >                assertEquals(q.getWaitingConsumerCount(), 1);
863 >                assertTrue(q.offer(one));
864 >                assertFalse(q.hasWaitingConsumer());
865 >                assertEquals(q.getWaitingConsumerCount(), 0);
866 >            }});
867 >
868 >        assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
869 >        assertEquals(q.getWaitingConsumerCount(), 0);
870 >        assertFalse(q.hasWaitingConsumer());
871 >        t.join();
872      }
873  
874      /**
# Line 864 | Line 879 | public class LinkedTransferQueueTest ext
879              LinkedTransferQueue q = new LinkedTransferQueue();
880              q.transfer(null);
881              shouldThrow();
882 <        } catch (NullPointerException ex) {
868 <        }
882 >        } catch (NullPointerException success) {}
883      }
884  
885      /**
# Line 873 | Line 887 | public class LinkedTransferQueueTest ext
887       * is returned by this associated poll.
888       */
889      public void testTransfer2() throws InterruptedException {
890 <        final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
890 >        final LinkedTransferQueue<Integer> q
891 >            = new LinkedTransferQueue<Integer>();
892  
893 <        new Thread(new Runnable() {
894 <            public void run() {
895 <                try {
896 <                    q.transfer(SIZE);
897 <                    threadAssertTrue(q.isEmpty());
883 <                } catch (Throwable ex) {
884 <                    threadUnexpectedException(ex);
885 <                }
886 <            }}).start();
893 >        Thread t = newStartedThread(new CheckedRunnable() {
894 >            public void realRun() throws InterruptedException {
895 >                q.transfer(SIZE);
896 >                threadAssertTrue(q.isEmpty());
897 >            }});
898  
899          Thread.sleep(SHORT_DELAY_MS);
900          assertEquals(1, q.size());
901          assertEquals(SIZE, (int) q.poll());
902          assertTrue(q.isEmpty());
903 +        t.join();
904      }
905  
906      /**
907       * transfer waits until a poll occurs, and then transfers in fifo order
908       */
909      public void testTransfer3() throws InterruptedException {
910 <        final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
910 >        final LinkedTransferQueue<Integer> q
911 >            = new LinkedTransferQueue<Integer>();
912  
913 <        Thread first =
914 <            new Thread(new Runnable() {
915 <                public void run() {
916 <                    try {
917 <                        Integer i = SIZE + 1;
918 <                        q.transfer(i);
919 <                        threadAssertTrue(!q.contains(i));
907 <                        threadAssertEquals(1, q.size());
908 <                    } catch (Throwable ex) {
909 <                        threadUnexpectedException(ex);
910 <                    }
911 <                }});
912 <        first.start();
913 >        Thread first = newStartedThread(new CheckedRunnable() {
914 >            public void realRun() throws InterruptedException {
915 >                Integer i = SIZE + 1;
916 >                q.transfer(i);
917 >                threadAssertTrue(!q.contains(i));
918 >                threadAssertEquals(1, q.size());
919 >            }});
920  
921 <        Thread interruptedThread =
922 <            new Thread(new Runnable() {
923 <                public void run() {
924 <                    try {
925 <                        while (q.size() == 0)
926 <                            Thread.yield();
920 <                        q.transfer(SIZE);
921 <                        threadShouldThrow();
922 <                    } catch (InterruptedException success) {
923 <                    } catch (Throwable ex) {
924 <                        threadUnexpectedException(ex);
925 <                    }
921 >        Thread interruptedThread = newStartedThread(
922 >            new CheckedInterruptedRunnable() {
923 >                public void realRun() throws InterruptedException {
924 >                    while (q.size() == 0)
925 >                        Thread.yield();
926 >                    q.transfer(SIZE);
927                  }});
927        interruptedThread.start();
928  
929          while (q.size() < 2)
930              Thread.yield();
# Line 944 | Line 944 | public class LinkedTransferQueueTest ext
944       */
945      public void testTransfer4() throws InterruptedException {
946          final LinkedTransferQueue q = new LinkedTransferQueue();
947 <        new Thread(new Runnable() {
948 <            public void run() {
949 <                try {
950 <                    q.transfer(four);
951 <                    threadAssertFalse(q.contains(four));
952 <                    threadAssertEquals(three, q.poll());
953 <                } catch (Throwable ex) {
954 <                    threadUnexpectedException(ex);
955 <                }
956 <            }}).start();
957 <        Thread.sleep(MEDIUM_DELAY_MS);
947 >
948 >        Thread t = newStartedThread(new CheckedRunnable() {
949 >            public void realRun() throws InterruptedException {
950 >                q.transfer(four);
951 >                threadAssertFalse(q.contains(four));
952 >                threadAssertEquals(three, q.poll());
953 >            }});
954 >
955 >        Thread.sleep(SHORT_DELAY_MS);
956          assertTrue(q.offer(three));
957          assertEquals(four, q.poll());
958 +        t.join();
959      }
960  
961      /**
# Line 964 | Line 963 | public class LinkedTransferQueueTest ext
963       * is returned by this associated take.
964       */
965      public void testTransfer5() throws InterruptedException {
966 <        final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
966 >        final LinkedTransferQueue<Integer> q
967 >            = new LinkedTransferQueue<Integer>();
968  
969 <        new Thread(new Runnable() {
970 <            public void run() {
971 <                try {
972 <                    q.transfer(SIZE);
973 <                    threadAssertTrue(q.isEmpty());
974 <                } catch (Throwable ex) {
975 <                    threadUnexpectedException(ex);
976 <                }
977 <            }}).start();
969 >        Thread t = newStartedThread(new CheckedRunnable() {
970 >            public void realRun() throws InterruptedException {
971 >                q.transfer(SIZE);
972 >                checkEmpty(q);
973 >            }});
974  
975          Thread.sleep(SHORT_DELAY_MS);
976          assertEquals(SIZE, (int) q.take());
977 <        assertTrue(q.isEmpty());
977 >        checkEmpty(q);
978 >        t.join();
979      }
980  
981      /**
# Line 988 | Line 985 | public class LinkedTransferQueueTest ext
985          try {
986              final LinkedTransferQueue q = new LinkedTransferQueue();
987              q.tryTransfer(null);
988 <            this.shouldThrow();
989 <        } catch (NullPointerException ex) {
993 <        }
988 >            shouldThrow();
989 >        } catch (NullPointerException success) {}
990      }
991  
992      /**
993       * tryTransfer returns false and does not enqueue if there are no
994       * consumers waiting to poll or take.
995       */
996 <    public void testTryTransfer2() {
996 >    public void testTryTransfer2() throws InterruptedException {
997          final LinkedTransferQueue q = new LinkedTransferQueue();
998          assertFalse(q.tryTransfer(new Object()));
999          assertFalse(q.hasWaitingConsumer());
1000 <        assertTrue(q.isEmpty());
1005 <        assertEquals(0, q.size());
1000 >        checkEmpty(q);
1001      }
1002  
1003      /**
# Line 1012 | Line 1007 | public class LinkedTransferQueueTest ext
1007      public void testTryTransfer3() throws InterruptedException {
1008          final Object hotPotato = new Object();
1009          final LinkedTransferQueue q = new LinkedTransferQueue();
1010 <        new Thread(new Runnable() {
1011 <            public void run() {
1012 <                try {
1013 <                    while (! q.hasWaitingConsumer())
1014 <                        Thread.yield();
1015 <                    threadAssertTrue(q.hasWaitingConsumer());
1016 <                    threadAssertTrue(q.isEmpty());
1017 <                    threadAssertTrue(q.size() == 0);
1018 <                    threadAssertTrue(q.tryTransfer(hotPotato));
1019 <                } catch (Throwable ex) {
1020 <                    threadUnexpectedException(ex);
1021 <                }
1022 <            }}).start();
1023 <        assertTrue(q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS) == hotPotato);
1029 <        assertTrue(q.isEmpty());
1010 >
1011 >        Thread t = newStartedThread(new CheckedRunnable() {
1012 >            public void realRun() {
1013 >                while (! q.hasWaitingConsumer())
1014 >                    Thread.yield();
1015 >                threadAssertTrue(q.hasWaitingConsumer());
1016 >                threadAssertTrue(q.isEmpty());
1017 >                threadAssertTrue(q.size() == 0);
1018 >                threadAssertTrue(q.tryTransfer(hotPotato));
1019 >            }});
1020 >
1021 >        assertTrue(q.poll(MEDIUM_DELAY_MS, MILLISECONDS) == hotPotato);
1022 >        checkEmpty(q);
1023 >        t.join();
1024      }
1025  
1026      /**
# Line 1036 | Line 1030 | public class LinkedTransferQueueTest ext
1030      public void testTryTransfer4() throws InterruptedException {
1031          final Object hotPotato = new Object();
1032          final LinkedTransferQueue q = new LinkedTransferQueue();
1033 <        new Thread(new Runnable() {
1034 <            public void run() {
1035 <                try {
1036 <                    while (! q.hasWaitingConsumer())
1037 <                        Thread.yield();
1038 <                    threadAssertTrue(q.hasWaitingConsumer());
1039 <                    threadAssertTrue(q.isEmpty());
1040 <                    threadAssertTrue(q.size() == 0);
1041 <                    threadAssertTrue(q.tryTransfer(hotPotato));
1042 <                } catch (Throwable ex) {
1043 <                    threadUnexpectedException(ex);
1050 <                }
1051 <            }}).start();
1033 >
1034 >        Thread t = newStartedThread(new CheckedRunnable() {
1035 >            public void realRun() {
1036 >                while (! q.hasWaitingConsumer())
1037 >                    Thread.yield();
1038 >                threadAssertTrue(q.hasWaitingConsumer());
1039 >                threadAssertTrue(q.isEmpty());
1040 >                threadAssertTrue(q.size() == 0);
1041 >                threadAssertTrue(q.tryTransfer(hotPotato));
1042 >            }});
1043 >
1044          assertTrue(q.take() == hotPotato);
1045 <        assertTrue(q.isEmpty());
1045 >        checkEmpty(q);
1046 >        t.join();
1047      }
1048  
1049      /**
# Line 1059 | Line 1052 | public class LinkedTransferQueueTest ext
1052       */
1053      public void testTryTransfer5() throws InterruptedException {
1054          final LinkedTransferQueue q = new LinkedTransferQueue();
1055 <        Thread toInterrupt = new Thread(new Runnable() {
1056 <            public void run() {
1057 <                try {
1058 <                    q.tryTransfer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
1066 <                    threadShouldThrow();
1067 <                } catch (InterruptedException success) {
1068 <                } catch (Throwable ex) {
1069 <                    threadUnexpectedException(ex);
1070 <                }
1055 >
1056 >        Thread toInterrupt = newStartedThread(new CheckedInterruptedRunnable() {
1057 >            public void realRun() throws InterruptedException {
1058 >                q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS);
1059              }});
1060 <        toInterrupt.start();
1060 >
1061          Thread.sleep(SMALL_DELAY_MS);
1062          toInterrupt.interrupt();
1063 +        toInterrupt.join();
1064      }
1065  
1066      /**
# Line 1079 | Line 1068 | public class LinkedTransferQueueTest ext
1068       */
1069      public void testTryTransfer6() throws InterruptedException {
1070          final LinkedTransferQueue q = new LinkedTransferQueue();
1071 <        new Thread(new Runnable() {
1072 <            public void run() {
1073 <                try {
1074 <                    threadAssertFalse(q.tryTransfer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1075 <                } catch (Throwable ex) {
1076 <                    threadUnexpectedException(ex);
1077 <                }
1078 <            }}).start();
1079 <        Thread.sleep(LONG_DELAY_MS);
1080 <        assertTrue(q.isEmpty());
1071 >
1072 >        Thread t = newStartedThread(new CheckedRunnable() {
1073 >            public void realRun() throws InterruptedException {
1074 >                threadAssertFalse
1075 >                    (q.tryTransfer(new Object(),
1076 >                                   SHORT_DELAY_MS, MILLISECONDS));
1077 >            }});
1078 >
1079 >        Thread.sleep(SMALL_DELAY_MS);
1080 >        checkEmpty(q);
1081 >        t.join();
1082      }
1083  
1084      /**
# Line 1098 | Line 1088 | public class LinkedTransferQueueTest ext
1088      public void testTryTransfer7() throws InterruptedException {
1089          final LinkedTransferQueue q = new LinkedTransferQueue();
1090          assertTrue(q.offer(four));
1091 <        new Thread(new Runnable() {
1092 <            public void run() {
1093 <                try {
1094 <                    threadAssertTrue(q.tryTransfer(five, LONG_DELAY_MS, TimeUnit.MILLISECONDS));
1095 <                    threadAssertTrue(q.isEmpty());
1096 <                } catch (Throwable ex) {
1097 <                    threadUnexpectedException(ex);
1098 <                }
1109 <            }}).start();
1091 >
1092 >        Thread t = newStartedThread(new CheckedRunnable() {
1093 >            public void realRun() throws InterruptedException {
1094 >                threadAssertTrue(q.tryTransfer(five,
1095 >                                               MEDIUM_DELAY_MS, MILLISECONDS));
1096 >                threadAssertTrue(q.isEmpty());
1097 >            }});
1098 >
1099          Thread.sleep(SHORT_DELAY_MS);
1100          assertEquals(2, q.size());
1101          assertEquals(four, q.poll());
1102          assertEquals(five, q.poll());
1103 <        assertTrue(q.isEmpty());
1103 >        checkEmpty(q);
1104 >        t.join();
1105      }
1106  
1107      /**
# Line 1122 | Line 1112 | public class LinkedTransferQueueTest ext
1112          final LinkedTransferQueue q = new LinkedTransferQueue();
1113          assertTrue(q.offer(four));
1114          assertEquals(1, q.size());
1115 <        assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1115 >        assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS));
1116          assertEquals(1, q.size());
1117          assertEquals(four, q.poll());
1128        threadAssertTrue(q.isEmpty());
1118          assertNull(q.poll());
1119 +        checkEmpty(q);
1120      }
1121  
1122      private LinkedTransferQueue<Integer> populatedQueue(int n) {
# Line 1140 | Line 1130 | public class LinkedTransferQueueTest ext
1130          assertFalse(q.isEmpty());
1131          return q;
1132      }
1143
1144    private static class ConsumerObserver {
1145
1146        private int waitingConsumers;
1147
1148        private ConsumerObserver() {
1149        }
1150
1151        private void setWaitingConsumer(int i) {
1152            this.waitingConsumers = i;
1153        }
1154
1155        private int getWaitingConsumers() {
1156            return waitingConsumers;
1157        }
1158    }
1133   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines