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.14 by jsr166, Sat Aug 15 03:20:36 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 +        }
49 +        try {
50 +            q.iterator().next();
51 +            shouldThrow();
52 +        } catch (NoSuchElementException success) {
53 +        }
54 +        try {
55 +            q.remove();
56 +            shouldThrow();
57 +        } catch (NoSuchElementException success) {
58 +        }
59 +    }
60 +
61      /**
62 <     * Constructor builds new queue with size being zero and empty being true
62 >     * Constructor builds new queue with size being zero and empty
63 >     * being true
64       */
65      public void testConstructor1() {
66          assertEquals(0, new LinkedTransferQueue().size());
# Line 39 | Line 68 | public class LinkedTransferQueueTest ext
68      }
69  
70      /**
71 <     * Initializing constructor with null collection throws NullPointerException
71 >     * Initializing constructor with null collection throws
72 >     * NullPointerException
73       */
74      public void testConstructor2() {
75          try {
# Line 50 | Line 80 | public class LinkedTransferQueueTest ext
80      }
81  
82      /**
83 <     * Initializing from Collection of null elements throws NullPointerException
83 >     * Initializing from Collection of null elements throws
84 >     * NullPointerException
85       */
86      public void testConstructor3() {
87          try {
88              Integer[] ints = new Integer[SIZE];
89 <            LinkedTransferQueue q = new LinkedTransferQueue(Arrays.asList(ints));
89 >            new LinkedTransferQueue(Arrays.asList(ints));
90              shouldThrow();
91          } catch (NullPointerException success) {
92          }
# Line 71 | Line 102 | public class LinkedTransferQueueTest ext
102              for (int i = 0; i < SIZE - 1; ++i) {
103                  ints[i] = i;
104              }
105 <            LinkedTransferQueue q = new LinkedTransferQueue(Arrays.asList(ints));
105 >            new LinkedTransferQueue(Arrays.asList(ints));
106              shouldThrow();
107          } catch (NullPointerException success) {
108          }
# Line 81 | Line 112 | public class LinkedTransferQueueTest ext
112       * Queue contains all elements of the collection it is initialized by
113       */
114      public void testConstructor5() {
115 <        try {
116 <            Integer[] ints = new Integer[SIZE];
117 <            for (int i = 0; i < SIZE; ++i) {
118 <                ints[i] = i;
119 <            }
120 <            LinkedTransferQueue q = new LinkedTransferQueue(Arrays.asList(ints));
121 <            for (int i = 0; i < SIZE; ++i) {
122 <                assertEquals(ints[i], q.poll());
123 <            }
124 <        } finally {
115 >        Integer[] ints = new Integer[SIZE];
116 >        for (int i = 0; i < SIZE; ++i) {
117 >            ints[i] = i;
118 >        }
119 >        List intList = Arrays.asList(ints);
120 >        LinkedTransferQueue q
121 >            = new LinkedTransferQueue(intList);
122 >        assertEquals(q.size(), intList.size());
123 >        assertEquals(q.toString(), intList.toString());
124 >        assertTrue(Arrays.equals(q.toArray(),
125 >                                     intList.toArray()));
126 >        assertTrue(Arrays.equals(q.toArray(new Object[0]),
127 >                                 intList.toArray(new Object[0])));
128 >        assertTrue(Arrays.equals(q.toArray(new Object[SIZE]),
129 >                                 intList.toArray(new Object[SIZE])));
130 >        for (int i = 0; i < SIZE; ++i) {
131 >            assertEquals(ints[i], q.poll());
132          }
133      }
134  
135      /**
136 <     * Remaining capacity never decrease nor increase on add or remove
136 >     * remainingCapacity() always returns Integer.MAX_VALUE
137       */
138      public void testRemainingCapacity() {
139          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
# Line 173 | Line 211 | public class LinkedTransferQueueTest ext
211      }
212  
213      /**
214 <     * addAll of a collection with any null elements throws NullPointerException after
215 <     * possibly adding some elements
214 >     * addAll of a collection with any null elements throws
215 >     * NullPointerException after possibly adding some elements
216       */
217      public void testAddAll3() {
218          try {
# Line 223 | Line 261 | public class LinkedTransferQueueTest ext
261      public void testPut() {
262          LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
263          for (int i = 0; i < SIZE; ++i) {
264 +            assertEquals(q.size(), i);
265              q.put(i);
266              assertTrue(q.contains(i));
267          }
229        assertEquals(q.size(), SIZE);
268      }
269  
270      /**
# Line 244 | Line 282 | public class LinkedTransferQueueTest ext
282       */
283      public void testTakeFromEmpty() throws InterruptedException {
284          final LinkedTransferQueue q = new LinkedTransferQueue();
285 <        Thread t = new Thread(new Runnable() {
286 <            public void run() {
287 <                try {
250 <                    q.take();
251 <                    threadShouldThrow();
252 <                } catch (InterruptedException success) {
253 <                } catch (Throwable ex) {
254 <                    threadUnexpectedException(ex);
255 <                }
285 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
286 >            void realRun() throws InterruptedException {
287 >                q.take();
288              }});
257        t.start();
289          Thread.sleep(SHORT_DELAY_MS);
290          t.interrupt();
291          t.join();
# Line 264 | Line 295 | public class LinkedTransferQueueTest ext
295       * Take removes existing elements until empty, then blocks interruptibly
296       */
297      public void testBlockingTake() throws InterruptedException {
298 <        Thread t = new Thread(new Runnable() {
299 <            public void run() {
300 <                try {
301 <                    LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
302 <                    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);
298 >        final LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
299 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
300 >            void realRun() throws InterruptedException {
301 >                for (int i = 0; i < SIZE; ++i) {
302 >                    threadAssertEquals(i, (int) q.take());
303                  }
304 +                q.take();
305              }});
306 <        t.start();
282 <        Thread.sleep(SHORT_DELAY_MS);
306 >        Thread.sleep(SMALL_DELAY_MS);
307          t.interrupt();
308          t.join();
309 +        checkEmpty(q);
310      }
311  
312      /**
313       * poll succeeds unless empty
314       */
315 <    public void testPoll() {
315 >    public void testPoll() throws InterruptedException {
316          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
317          for (int i = 0; i < SIZE; ++i) {
318              assertEquals(i, (int) q.poll());
319          }
320          assertNull(q.poll());
321 +        checkEmpty(q);
322      }
323  
324      /**
# Line 301 | Line 327 | public class LinkedTransferQueueTest ext
327      public void testTimedPoll0() throws InterruptedException {
328          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
329          for (int i = 0; i < SIZE; ++i) {
330 <            assertEquals(i, (int) q.poll(0, TimeUnit.MILLISECONDS));
330 >            assertEquals(i, (int) q.poll(0, MILLISECONDS));
331          }
332 <        assertNull(q.poll(0, TimeUnit.MILLISECONDS));
332 >        assertNull(q.poll(0, MILLISECONDS));
333 >        checkEmpty(q);
334      }
335  
336      /**
# Line 312 | Line 339 | public class LinkedTransferQueueTest ext
339      public void testTimedPoll() throws InterruptedException {
340          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
341          for (int i = 0; i < SIZE; ++i) {
342 <            assertEquals(i, (int) q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
342 >            long t0 = System.nanoTime();
343 >            assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
344 >            long millisElapsed = (System.nanoTime() - t0)/(1024 * 1024);
345 >            assertTrue(millisElapsed < SMALL_DELAY_MS);
346          }
347 <        assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
347 >        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
348 >        checkEmpty(q);
349      }
350  
351      /**
# Line 322 | Line 353 | public class LinkedTransferQueueTest ext
353       * returning timeout status
354       */
355      public void testInterruptedTimedPoll() throws InterruptedException {
356 <        Thread t = new Thread(new Runnable() {
357 <            public void run() {
358 <                try {
359 <                    LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
360 <                    for (int i = 0; i < SIZE; ++i) {
361 <                        threadAssertEquals(i, (int) q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
362 <                    }
363 <                    threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
364 <                } catch (InterruptedException success) {
334 <                } catch (Throwable ex) {
335 <                    threadUnexpectedException(ex);
356 >        final LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
357 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
358 >            void realRun() throws InterruptedException {
359 >                for (int i = 0; i < SIZE; ++i) {
360 >                    long t0 = System.nanoTime();
361 >                    threadAssertEquals(i, (int) q.poll(LONG_DELAY_MS,
362 >                                                       MILLISECONDS));
363 >                    long millisElapsed = (System.nanoTime() - t0)/(1024 * 1024);
364 >                    assertTrue(millisElapsed < SMALL_DELAY_MS);
365                  }
366 +                q.poll(LONG_DELAY_MS, MILLISECONDS);
367              }});
368 <        t.start();
339 <        Thread.sleep(SHORT_DELAY_MS);
368 >        Thread.sleep(SMALL_DELAY_MS);
369          t.interrupt();
370          t.join();
371 +        checkEmpty(q);
372      }
373  
374      /**
# Line 347 | Line 377 | public class LinkedTransferQueueTest ext
377       */
378      public void testTimedPollWithOffer() throws InterruptedException {
379          final LinkedTransferQueue q = new LinkedTransferQueue();
380 <        Thread t = new Thread(new Runnable() {
381 <            public void run() {
382 <                try {
383 <                    threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
384 <                    q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
355 <                    q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
356 <                    threadShouldThrow();
357 <                } catch (InterruptedException success) {
358 <                } catch (Throwable ex) {
359 <                    threadUnexpectedException(ex);
360 <                }
380 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
381 >            void realRun() throws InterruptedException {
382 >                threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
383 >                q.poll(LONG_DELAY_MS, MILLISECONDS);
384 >                q.poll(LONG_DELAY_MS, MILLISECONDS);
385              }});
362        t.start();
386          Thread.sleep(SMALL_DELAY_MS);
387 <        assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
387 >        assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
388          t.interrupt();
389          t.join();
390      }
# Line 369 | Line 392 | public class LinkedTransferQueueTest ext
392      /**
393       * peek returns next element, or null if empty
394       */
395 <    public void testPeek() {
395 >    public void testPeek() throws InterruptedException {
396          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
397          for (int i = 0; i < SIZE; ++i) {
398              assertEquals(i, (int) q.peek());
# Line 378 | Line 401 | public class LinkedTransferQueueTest ext
401                         i != (int) q.peek());
402          }
403          assertNull(q.peek());
404 +        checkEmpty(q);
405      }
406  
407      /**
408       * element returns next element, or throws NoSuchElementException if empty
409       */
410 <    public void testElement() {
410 >    public void testElement() throws InterruptedException {
411          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
412          for (int i = 0; i < SIZE; ++i) {
413              assertEquals(i, (int) q.element());
# Line 394 | Line 418 | public class LinkedTransferQueueTest ext
418              shouldThrow();
419          } catch (NoSuchElementException success) {
420          }
421 +        checkEmpty(q);
422      }
423  
424      /**
425       * remove removes next element, or throws NoSuchElementException if empty
426       */
427 <    public void testRemove() {
427 >    public void testRemove() throws InterruptedException {
428          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
429          for (int i = 0; i < SIZE; ++i) {
430              assertEquals(i, (int) q.remove());
# Line 409 | Line 434 | public class LinkedTransferQueueTest ext
434              shouldThrow();
435          } catch (NoSuchElementException success) {
436          }
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 551 | Line 579 | public class LinkedTransferQueueTest ext
579      }
580  
581      /**
582 <     * toArray with incompatible array type throws CCE
582 >     * toArray(incompatible array type) throws CCE
583       */
584      public void testToArray1_BadArg() {
585          try {
# Line 568 | Line 596 | public class LinkedTransferQueueTest ext
596      public void testIterator() throws InterruptedException {
597          LinkedTransferQueue q = populatedQueue(SIZE);
598          Iterator it = q.iterator();
599 +        int i = 0;
600          while (it.hasNext()) {
601 <            assertEquals(it.next(), q.take());
601 >            assertEquals(it.next(), i++);
602          }
603 +        assertEquals(i, SIZE);
604      }
605  
606      /**
607 <     * iterator.remove removes current element
607 >     * iterator.remove() removes current element
608       */
609      public void testIteratorRemove() {
610          final LinkedTransferQueue q = new LinkedTransferQueue();
# Line 596 | Line 626 | public class LinkedTransferQueueTest ext
626       * iterator ordering is FIFO
627       */
628      public void testIteratorOrdering() {
629 <        final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
629 >        final LinkedTransferQueue<Integer> q
630 >            = new LinkedTransferQueue<Integer>();
631          assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
632          q.add(one);
633          q.add(two);
# Line 643 | Line 674 | public class LinkedTransferQueueTest ext
674          q.add(one);
675          q.add(two);
676          ExecutorService executor = Executors.newFixedThreadPool(2);
677 <        executor.execute(new Runnable() {
678 <            public void run() {
679 <                try {
680 <                    threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
681 <                } catch (Throwable ex) {
651 <                    threadUnexpectedException(ex);
652 <                }
677 >
678 >        executor.execute(new CheckedRunnable() {
679 >            void realRun() {
680 >                threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS,
681 >                                         MILLISECONDS));
682              }});
683  
684 <        executor.execute(new Runnable() {
685 <            public void run() {
686 <                try {
687 <                    Thread.sleep(SMALL_DELAY_MS);
659 <                    threadAssertEquals(one, q.take());
660 <                } catch (Throwable ex) {
661 <                    threadUnexpectedException(ex);
662 <                }
684 >        executor.execute(new CheckedRunnable() {
685 >            void realRun() throws InterruptedException {
686 >                Thread.sleep(SMALL_DELAY_MS);
687 >                threadAssertEquals(one, q.take());
688              }});
689  
690          joinPool(executor);
691      }
692  
693      /**
694 <     * poll retrieves elements across Executor threads
694 >     * timed poll retrieves elements across Executor threads
695       */
696      public void testPollInExecutor() {
697          final LinkedTransferQueue q = new LinkedTransferQueue();
698          ExecutorService executor = Executors.newFixedThreadPool(2);
699 <        executor.execute(new Runnable() {
700 <            public void run() {
701 <                try {
702 <                    threadAssertNull(q.poll());
703 <                    threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
704 <                    threadAssertTrue(q.isEmpty());
705 <                } catch (Throwable  ex) {
681 <                    threadUnexpectedException(ex);
682 <                }
699 >
700 >        executor.execute(new CheckedRunnable() {
701 >            void realRun() throws InterruptedException {
702 >                threadAssertNull(q.poll());
703 >                threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS,
704 >                                                MILLISECONDS));
705 >                threadAssertTrue(q.isEmpty());
706              }});
707  
708 <        executor.execute(new Runnable() {
709 <            public void run() {
710 <                try {
711 <                    Thread.sleep(SMALL_DELAY_MS);
689 <                    q.put(one);
690 <                } catch (Throwable ex) {
691 <                    threadUnexpectedException(ex);
692 <                }
708 >        executor.execute(new CheckedRunnable() {
709 >            void realRun() throws InterruptedException {
710 >                Thread.sleep(SMALL_DELAY_MS);
711 >                q.put(one);
712              }});
713  
714          joinPool(executor);
# Line 702 | Line 721 | public class LinkedTransferQueueTest ext
721          LinkedTransferQueue q = populatedQueue(SIZE);
722  
723          ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
724 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
724 >        ObjectOutputStream out
725 >            = new ObjectOutputStream(new BufferedOutputStream(bout));
726          out.writeObject(q);
727          out.close();
728  
729 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
730 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
729 >        ByteArrayInputStream bin
730 >            = new ByteArrayInputStream(bout.toByteArray());
731 >        ObjectInputStream in
732 >            = new ObjectInputStream(new BufferedInputStream(bin));
733          LinkedTransferQueue r = (LinkedTransferQueue) in.readObject();
734  
735          assertEquals(q.size(), r.size());
# Line 767 | Line 789 | public class LinkedTransferQueueTest ext
789      }
790  
791      /**
792 <     * drainTo empties full queue, unblocking a waiting put.
792 >     * drainTo(c) empties full queue, unblocking a waiting put.
793       */
794      public void testDrainToWithActivePut() throws InterruptedException {
795          final LinkedTransferQueue q = populatedQueue(SIZE);
796 <        Thread t = new Thread(new Runnable() {
797 <            public void run() {
798 <                try {
777 <                    q.put(SIZE + 1);
778 <                } catch (Throwable ex) {
779 <                    threadUnexpectedException(ex);
780 <                }
796 >        Thread t = newStartedThread(new CheckedRunnable() {
797 >            void realRun() {
798 >                q.put(SIZE + 1);
799              }});
782        t.start();
800          ArrayList l = new ArrayList();
801          q.drainTo(l);
802          assertTrue(l.size() >= SIZE);
# Line 796 | Line 813 | public class LinkedTransferQueueTest ext
813      public void testDrainToNullN() {
814          LinkedTransferQueue q = populatedQueue(SIZE);
815          try {
816 <            q.drainTo(null, 0);
816 >            q.drainTo(null, SIZE);
817              shouldThrow();
818          } catch (NullPointerException success) {
819          }
# Line 808 | Line 825 | public class LinkedTransferQueueTest ext
825      public void testDrainToSelfN() {
826          LinkedTransferQueue q = populatedQueue(SIZE);
827          try {
828 <            q.drainTo(q, 0);
828 >            q.drainTo(q, SIZE);
829              shouldThrow();
830          } catch (IllegalArgumentException success) {
831          }
# Line 837 | Line 854 | public class LinkedTransferQueueTest ext
854      }
855  
856      /**
857 <     * poll and take decrement the waiting consumer count
857 >     * timed poll() or take() increments the waiting consumer count;
858 >     * offer(e) decrements the waiting consumer count
859       */
860      public void testWaitingConsumer() throws InterruptedException {
861          final LinkedTransferQueue q = new LinkedTransferQueue();
862 <        final ConsumerObserver waiting = new ConsumerObserver();
863 <        new Thread(new Runnable() {
864 <            public void run() {
865 <                try {
866 <                    threadAssertTrue(q.hasWaitingConsumer());
867 <                    waiting.setWaitingConsumer(q.getWaitingConsumerCount());
868 <                    threadAssertTrue(q.offer(new Object()));
869 <                } catch (Throwable ex) {
870 <                    threadUnexpectedException(ex);
871 <                }
872 <            }}).start();
873 <        assertTrue(q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS) != null);
874 <        assertTrue(q.getWaitingConsumerCount() < waiting.getWaitingConsumers());
862 >        assertEquals(q.getWaitingConsumerCount(), 0);
863 >        assertFalse(q.hasWaitingConsumer());
864 >
865 >        Thread t = newStartedThread(new CheckedRunnable() {
866 >            void realRun() throws InterruptedException {
867 >                Thread.sleep(SMALL_DELAY_MS);
868 >                threadAssertTrue(q.hasWaitingConsumer());
869 >                threadAssertEquals(q.getWaitingConsumerCount(), 1);
870 >                threadAssertTrue(q.offer(new Object()));
871 >                threadAssertFalse(q.hasWaitingConsumer());
872 >                threadAssertEquals(q.getWaitingConsumerCount(), 0);
873 >            }});
874 >
875 >        assertTrue(q.poll(LONG_DELAY_MS, MILLISECONDS) != null);
876 >        assertEquals(q.getWaitingConsumerCount(), 0);
877 >        assertFalse(q.hasWaitingConsumer());
878 >        t.join();
879      }
880  
881      /**
# Line 873 | Line 895 | public class LinkedTransferQueueTest ext
895       * is returned by this associated poll.
896       */
897      public void testTransfer2() throws InterruptedException {
898 <        final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
898 >        final LinkedTransferQueue<Integer> q
899 >            = new LinkedTransferQueue<Integer>();
900  
901 <        new Thread(new Runnable() {
902 <            public void run() {
903 <                try {
904 <                    q.transfer(SIZE);
905 <                    threadAssertTrue(q.isEmpty());
883 <                } catch (Throwable ex) {
884 <                    threadUnexpectedException(ex);
885 <                }
886 <            }}).start();
901 >        Thread t = newStartedThread(new CheckedRunnable() {
902 >            void realRun() throws InterruptedException {
903 >                q.transfer(SIZE);
904 >                threadAssertTrue(q.isEmpty());
905 >            }});
906  
907          Thread.sleep(SHORT_DELAY_MS);
908          assertEquals(1, q.size());
909          assertEquals(SIZE, (int) q.poll());
910          assertTrue(q.isEmpty());
911 +        t.join();
912      }
913  
914      /**
915       * transfer waits until a poll occurs, and then transfers in fifo order
916       */
917      public void testTransfer3() throws InterruptedException {
918 <        final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
918 >        final LinkedTransferQueue<Integer> q
919 >            = new LinkedTransferQueue<Integer>();
920  
921 <        Thread first =
922 <            new Thread(new Runnable() {
923 <                public void run() {
924 <                    try {
925 <                        Integer i = SIZE + 1;
926 <                        q.transfer(i);
927 <                        threadAssertTrue(!q.contains(i));
907 <                        threadAssertEquals(1, q.size());
908 <                    } catch (Throwable ex) {
909 <                        threadUnexpectedException(ex);
910 <                    }
911 <                }});
912 <        first.start();
921 >        Thread first = newStartedThread(new CheckedRunnable() {
922 >            void realRun() throws InterruptedException {
923 >                Integer i = SIZE + 1;
924 >                q.transfer(i);
925 >                threadAssertTrue(!q.contains(i));
926 >                threadAssertEquals(1, q.size());
927 >            }});
928  
929 <        Thread interruptedThread =
930 <            new Thread(new Runnable() {
931 <                public void run() {
932 <                    try {
933 <                        while (q.size() == 0)
934 <                            Thread.yield();
920 <                        q.transfer(SIZE);
921 <                        threadShouldThrow();
922 <                    } catch (InterruptedException success) {
923 <                    } catch (Throwable ex) {
924 <                        threadUnexpectedException(ex);
925 <                    }
929 >        Thread interruptedThread = newStartedThread(
930 >            new CheckedInterruptedRunnable() {
931 >                void realRun() throws InterruptedException {
932 >                    while (q.size() == 0)
933 >                        Thread.yield();
934 >                    q.transfer(SIZE);
935                  }});
927        interruptedThread.start();
936  
937          while (q.size() < 2)
938              Thread.yield();
# Line 944 | Line 952 | public class LinkedTransferQueueTest ext
952       */
953      public void testTransfer4() throws InterruptedException {
954          final LinkedTransferQueue q = new LinkedTransferQueue();
955 <        new Thread(new Runnable() {
956 <            public void run() {
957 <                try {
958 <                    q.transfer(four);
959 <                    threadAssertFalse(q.contains(four));
960 <                    threadAssertEquals(three, q.poll());
961 <                } catch (Throwable ex) {
962 <                    threadUnexpectedException(ex);
963 <                }
956 <            }}).start();
957 <        Thread.sleep(MEDIUM_DELAY_MS);
955 >
956 >        Thread t = newStartedThread(new CheckedRunnable() {
957 >            void realRun() throws InterruptedException {
958 >                q.transfer(four);
959 >                threadAssertFalse(q.contains(four));
960 >                threadAssertEquals(three, q.poll());
961 >            }});
962 >
963 >        Thread.sleep(SHORT_DELAY_MS);
964          assertTrue(q.offer(three));
965          assertEquals(four, q.poll());
966 +        t.join();
967      }
968  
969      /**
# Line 964 | Line 971 | public class LinkedTransferQueueTest ext
971       * is returned by this associated take.
972       */
973      public void testTransfer5() throws InterruptedException {
974 <        final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
974 >        final LinkedTransferQueue<Integer> q
975 >            = new LinkedTransferQueue<Integer>();
976  
977 <        new Thread(new Runnable() {
978 <            public void run() {
979 <                try {
980 <                    q.transfer(SIZE);
981 <                    threadAssertTrue(q.isEmpty());
974 <                } catch (Throwable ex) {
975 <                    threadUnexpectedException(ex);
976 <                }
977 <            }}).start();
977 >        Thread t = newStartedThread(new CheckedRunnable() {
978 >            void realRun() throws InterruptedException {
979 >                q.transfer(SIZE);
980 >                checkEmpty(q);
981 >            }});
982  
983          Thread.sleep(SHORT_DELAY_MS);
984          assertEquals(SIZE, (int) q.take());
985 <        assertTrue(q.isEmpty());
985 >        checkEmpty(q);
986 >        t.join();
987      }
988  
989      /**
# Line 988 | Line 993 | public class LinkedTransferQueueTest ext
993          try {
994              final LinkedTransferQueue q = new LinkedTransferQueue();
995              q.tryTransfer(null);
996 <            this.shouldThrow();
996 >            shouldThrow();
997          } catch (NullPointerException ex) {
998          }
999      }
# Line 997 | Line 1002 | public class LinkedTransferQueueTest ext
1002       * tryTransfer returns false and does not enqueue if there are no
1003       * consumers waiting to poll or take.
1004       */
1005 <    public void testTryTransfer2() {
1005 >    public void testTryTransfer2() throws InterruptedException {
1006          final LinkedTransferQueue q = new LinkedTransferQueue();
1007          assertFalse(q.tryTransfer(new Object()));
1008          assertFalse(q.hasWaitingConsumer());
1009 <        assertTrue(q.isEmpty());
1005 <        assertEquals(0, q.size());
1009 >        checkEmpty(q);
1010      }
1011  
1012      /**
# Line 1012 | Line 1016 | public class LinkedTransferQueueTest ext
1016      public void testTryTransfer3() throws InterruptedException {
1017          final Object hotPotato = new Object();
1018          final LinkedTransferQueue q = new LinkedTransferQueue();
1019 <        new Thread(new Runnable() {
1020 <            public void run() {
1021 <                try {
1022 <                    while (! q.hasWaitingConsumer())
1023 <                        Thread.yield();
1024 <                    threadAssertTrue(q.hasWaitingConsumer());
1025 <                    threadAssertTrue(q.isEmpty());
1026 <                    threadAssertTrue(q.size() == 0);
1027 <                    threadAssertTrue(q.tryTransfer(hotPotato));
1028 <                } catch (Throwable ex) {
1029 <                    threadUnexpectedException(ex);
1030 <                }
1031 <            }}).start();
1032 <        assertTrue(q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS) == hotPotato);
1029 <        assertTrue(q.isEmpty());
1019 >
1020 >        Thread t = newStartedThread(new CheckedRunnable() {
1021 >            void realRun() {
1022 >                while (! q.hasWaitingConsumer())
1023 >                    Thread.yield();
1024 >                threadAssertTrue(q.hasWaitingConsumer());
1025 >                threadAssertTrue(q.isEmpty());
1026 >                threadAssertTrue(q.size() == 0);
1027 >                threadAssertTrue(q.tryTransfer(hotPotato));
1028 >            }});
1029 >
1030 >        assertTrue(q.poll(MEDIUM_DELAY_MS, MILLISECONDS) == hotPotato);
1031 >        checkEmpty(q);
1032 >        t.join();
1033      }
1034  
1035      /**
# Line 1036 | Line 1039 | public class LinkedTransferQueueTest ext
1039      public void testTryTransfer4() throws InterruptedException {
1040          final Object hotPotato = new Object();
1041          final LinkedTransferQueue q = new LinkedTransferQueue();
1042 <        new Thread(new Runnable() {
1043 <            public void run() {
1044 <                try {
1045 <                    while (! q.hasWaitingConsumer())
1046 <                        Thread.yield();
1047 <                    threadAssertTrue(q.hasWaitingConsumer());
1048 <                    threadAssertTrue(q.isEmpty());
1049 <                    threadAssertTrue(q.size() == 0);
1050 <                    threadAssertTrue(q.tryTransfer(hotPotato));
1051 <                } catch (Throwable ex) {
1052 <                    threadUnexpectedException(ex);
1050 <                }
1051 <            }}).start();
1042 >
1043 >        Thread t = newStartedThread(new CheckedRunnable() {
1044 >            void realRun() {
1045 >                while (! q.hasWaitingConsumer())
1046 >                    Thread.yield();
1047 >                threadAssertTrue(q.hasWaitingConsumer());
1048 >                threadAssertTrue(q.isEmpty());
1049 >                threadAssertTrue(q.size() == 0);
1050 >                threadAssertTrue(q.tryTransfer(hotPotato));
1051 >            }});
1052 >
1053          assertTrue(q.take() == hotPotato);
1054 <        assertTrue(q.isEmpty());
1054 >        checkEmpty(q);
1055 >        t.join();
1056      }
1057  
1058      /**
# Line 1059 | Line 1061 | public class LinkedTransferQueueTest ext
1061       */
1062      public void testTryTransfer5() throws InterruptedException {
1063          final LinkedTransferQueue q = new LinkedTransferQueue();
1064 <        Thread toInterrupt = new Thread(new Runnable() {
1065 <            public void run() {
1066 <                try {
1067 <                    q.tryTransfer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
1066 <                    threadShouldThrow();
1067 <                } catch (InterruptedException success) {
1068 <                } catch (Throwable ex) {
1069 <                    threadUnexpectedException(ex);
1070 <                }
1064 >
1065 >        Thread toInterrupt = newStartedThread(new CheckedInterruptedRunnable() {
1066 >            void realRun() throws InterruptedException {
1067 >                q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS);
1068              }});
1069 <        toInterrupt.start();
1069 >
1070          Thread.sleep(SMALL_DELAY_MS);
1071          toInterrupt.interrupt();
1072 +        toInterrupt.join();
1073      }
1074  
1075      /**
# Line 1079 | Line 1077 | public class LinkedTransferQueueTest ext
1077       */
1078      public void testTryTransfer6() throws InterruptedException {
1079          final LinkedTransferQueue q = new LinkedTransferQueue();
1080 <        new Thread(new Runnable() {
1081 <            public void run() {
1082 <                try {
1083 <                    threadAssertFalse(q.tryTransfer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1084 <                } catch (Throwable ex) {
1085 <                    threadUnexpectedException(ex);
1086 <                }
1087 <            }}).start();
1088 <        Thread.sleep(LONG_DELAY_MS);
1089 <        assertTrue(q.isEmpty());
1080 >
1081 >        Thread t = newStartedThread(new CheckedRunnable() {
1082 >            void realRun() throws InterruptedException {
1083 >                threadAssertFalse
1084 >                    (q.tryTransfer(new Object(),
1085 >                                   SHORT_DELAY_MS, MILLISECONDS));
1086 >            }});
1087 >
1088 >        Thread.sleep(SMALL_DELAY_MS);
1089 >        checkEmpty(q);
1090 >        t.join();
1091      }
1092  
1093      /**
# Line 1098 | Line 1097 | public class LinkedTransferQueueTest ext
1097      public void testTryTransfer7() throws InterruptedException {
1098          final LinkedTransferQueue q = new LinkedTransferQueue();
1099          assertTrue(q.offer(four));
1100 <        new Thread(new Runnable() {
1101 <            public void run() {
1102 <                try {
1103 <                    threadAssertTrue(q.tryTransfer(five, LONG_DELAY_MS, TimeUnit.MILLISECONDS));
1104 <                    threadAssertTrue(q.isEmpty());
1105 <                } catch (Throwable ex) {
1106 <                    threadUnexpectedException(ex);
1107 <                }
1109 <            }}).start();
1100 >
1101 >        Thread t = newStartedThread(new CheckedRunnable() {
1102 >            void realRun() throws InterruptedException {
1103 >                threadAssertTrue(q.tryTransfer(five,
1104 >                                               MEDIUM_DELAY_MS, MILLISECONDS));
1105 >                threadAssertTrue(q.isEmpty());
1106 >            }});
1107 >
1108          Thread.sleep(SHORT_DELAY_MS);
1109          assertEquals(2, q.size());
1110          assertEquals(four, q.poll());
1111          assertEquals(five, q.poll());
1112 <        assertTrue(q.isEmpty());
1112 >        checkEmpty(q);
1113 >        t.join();
1114      }
1115  
1116      /**
# Line 1122 | Line 1121 | public class LinkedTransferQueueTest ext
1121          final LinkedTransferQueue q = new LinkedTransferQueue();
1122          assertTrue(q.offer(four));
1123          assertEquals(1, q.size());
1124 <        assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1124 >        assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS));
1125          assertEquals(1, q.size());
1126          assertEquals(four, q.poll());
1128        threadAssertTrue(q.isEmpty());
1127          assertNull(q.poll());
1128 +        checkEmpty(q);
1129      }
1130  
1131      private LinkedTransferQueue<Integer> populatedQueue(int n) {
# Line 1140 | Line 1139 | public class LinkedTransferQueueTest ext
1139          assertFalse(q.isEmpty());
1140          return q;
1141      }
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    }
1142   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines