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.9 by jsr166, Wed Aug 5 00:43:23 2009 UTC vs.
Revision 1.25 by jsr166, Wed Oct 6 07:49:22 2010 UTC

# Line 13 | Line 13 | import java.io.ObjectInputStream;
13   import java.io.ObjectOutputStream;
14   import java.util.ArrayList;
15   import java.util.Arrays;
16 import java.util.ConcurrentModificationException;
16   import java.util.Iterator;
17 + import java.util.List;
18   import java.util.NoSuchElementException;
19   import java.util.concurrent.*;
20   import static java.util.concurrent.TimeUnit.MILLISECONDS;
21   import junit.framework.Test;
22   import junit.framework.TestSuite;
23  
24 + @SuppressWarnings({"unchecked", "rawtypes"})
25   public class LinkedTransferQueueTest extends JSR166TestCase {
26  
27 +    public static class Generic extends BlockingQueueTest {
28 +        protected BlockingQueue emptyCollection() {
29 +            return new LinkedTransferQueue();
30 +        }
31 +    }
32 +
33      public static void main(String[] args) {
34          junit.textui.TestRunner.run(suite());
35      }
36  
37      public static Test suite() {
38 <        return new TestSuite(LinkedTransferQueueTest.class);
38 >        return newTestSuite(LinkedTransferQueueTest.class,
39 >                            new Generic().testSuite());
40 >    }
41 >
42 >    void checkEmpty(LinkedTransferQueue q) throws InterruptedException {
43 >        assertTrue(q.isEmpty());
44 >        assertEquals(0, q.size());
45 >        assertNull(q.peek());
46 >        assertNull(q.poll());
47 >        assertNull(q.poll(0, MILLISECONDS));
48 >        assertEquals(q.toString(), "[]");
49 >        assertTrue(Arrays.equals(q.toArray(), new Object[0]));
50 >        assertFalse(q.iterator().hasNext());
51 >        try {
52 >            q.element();
53 >            shouldThrow();
54 >        } catch (NoSuchElementException success) {}
55 >        try {
56 >            q.iterator().next();
57 >            shouldThrow();
58 >        } catch (NoSuchElementException success) {}
59 >        try {
60 >            q.remove();
61 >            shouldThrow();
62 >        } catch (NoSuchElementException success) {}
63      }
64  
65      /**
# Line 48 | Line 79 | public class LinkedTransferQueueTest ext
79          try {
80              new LinkedTransferQueue(null);
81              shouldThrow();
82 <        } catch (NullPointerException success) {
52 <        }
82 >        } catch (NullPointerException success) {}
83      }
84  
85      /**
# Line 61 | Line 91 | public class LinkedTransferQueueTest ext
91              Integer[] ints = new Integer[SIZE];
92              new LinkedTransferQueue(Arrays.asList(ints));
93              shouldThrow();
94 <        } catch (NullPointerException success) {
65 <        }
94 >        } catch (NullPointerException success) {}
95      }
96  
97      /**
# Line 77 | Line 106 | public class LinkedTransferQueueTest ext
106              }
107              new LinkedTransferQueue(Arrays.asList(ints));
108              shouldThrow();
109 <        } catch (NullPointerException success) {
81 <        }
109 >        } catch (NullPointerException success) {}
110      }
111  
112      /**
113       * Queue contains all elements of the collection it is initialized by
114       */
115      public void testConstructor5() {
116 <        try {
117 <            Integer[] ints = new Integer[SIZE];
118 <            for (int i = 0; i < SIZE; ++i) {
119 <                ints[i] = i;
120 <            }
121 <            LinkedTransferQueue q
122 <                = new LinkedTransferQueue(Arrays.asList(ints));
123 <            for (int i = 0; i < SIZE; ++i) {
124 <                assertEquals(ints[i], q.poll());
125 <            }
126 <        } finally {
116 >        Integer[] ints = new Integer[SIZE];
117 >        for (int i = 0; i < SIZE; ++i) {
118 >            ints[i] = i;
119 >        }
120 >        List intList = Arrays.asList(ints);
121 >        LinkedTransferQueue q
122 >            = new LinkedTransferQueue(intList);
123 >        assertEquals(q.size(), intList.size());
124 >        assertEquals(q.toString(), intList.toString());
125 >        assertTrue(Arrays.equals(q.toArray(),
126 >                                     intList.toArray()));
127 >        assertTrue(Arrays.equals(q.toArray(new Object[0]),
128 >                                 intList.toArray(new Object[0])));
129 >        assertTrue(Arrays.equals(q.toArray(new Object[SIZE]),
130 >                                 intList.toArray(new Object[SIZE])));
131 >        for (int i = 0; i < SIZE; ++i) {
132 >            assertEquals(ints[i], q.poll());
133          }
134      }
135  
136      /**
137 <     * Remaining capacity never decrease nor increase on add or remove
137 >     * remainingCapacity() always returns Integer.MAX_VALUE
138       */
139      public void testRemainingCapacity() {
140          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
# Line 124 | Line 158 | public class LinkedTransferQueueTest ext
158              LinkedTransferQueue q = new LinkedTransferQueue();
159              q.offer(null);
160              shouldThrow();
161 <        } catch (NullPointerException success) {
128 <        }
161 >        } catch (NullPointerException success) {}
162      }
163  
164      /**
# Line 136 | Line 169 | public class LinkedTransferQueueTest ext
169              LinkedTransferQueue q = new LinkedTransferQueue();
170              q.add(null);
171              shouldThrow();
172 <        } catch (NullPointerException success) {
140 <        }
172 >        } catch (NullPointerException success) {}
173      }
174  
175      /**
# Line 148 | Line 180 | public class LinkedTransferQueueTest ext
180              LinkedTransferQueue q = new LinkedTransferQueue();
181              q.addAll(null);
182              shouldThrow();
183 <        } catch (NullPointerException success) {
152 <        }
183 >        } catch (NullPointerException success) {}
184      }
185  
186      /**
# Line 160 | Line 191 | public class LinkedTransferQueueTest ext
191              LinkedTransferQueue q = populatedQueue(SIZE);
192              q.addAll(q);
193              shouldThrow();
194 <        } catch (IllegalArgumentException success) {
164 <        }
194 >        } catch (IllegalArgumentException success) {}
195      }
196  
197      /**
# Line 173 | Line 203 | public class LinkedTransferQueueTest ext
203              Integer[] ints = new Integer[SIZE];
204              q.addAll(Arrays.asList(ints));
205              shouldThrow();
206 <        } catch (NullPointerException success) {
177 <        }
206 >        } catch (NullPointerException success) {}
207      }
208  
209      /**
# Line 190 | Line 219 | public class LinkedTransferQueueTest ext
219              }
220              q.addAll(Arrays.asList(ints));
221              shouldThrow();
222 <        } catch (NullPointerException success) {
194 <        }
222 >        } catch (NullPointerException success) {}
223      }
224  
225      /**
# Line 228 | Line 256 | public class LinkedTransferQueueTest ext
256      public void testPut() {
257          LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
258          for (int i = 0; i < SIZE; ++i) {
259 +            assertEquals(q.size(), i);
260              q.put(i);
261              assertTrue(q.contains(i));
262          }
234        assertEquals(q.size(), SIZE);
263      }
264  
265      /**
# Line 250 | Line 278 | public class LinkedTransferQueueTest ext
278      public void testTakeFromEmpty() throws InterruptedException {
279          final LinkedTransferQueue q = new LinkedTransferQueue();
280          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
281 <            void realRun() throws InterruptedException {
281 >            public void realRun() throws InterruptedException {
282                  q.take();
283              }});
284          Thread.sleep(SHORT_DELAY_MS);
# Line 262 | Line 290 | public class LinkedTransferQueueTest ext
290       * Take removes existing elements until empty, then blocks interruptibly
291       */
292      public void testBlockingTake() throws InterruptedException {
293 <        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
294 <            void realRun() throws InterruptedException {
295 <                LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
293 >        final LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
294 >        Thread t = new Thread(new CheckedRunnable() {
295 >            public void realRun() throws InterruptedException {
296                  for (int i = 0; i < SIZE; ++i) {
297 <                    threadAssertEquals(i, (int) q.take());
297 >                    assertEquals(i, (int) q.take());
298                  }
299 <                q.take();
299 >                try {
300 >                    q.take();
301 >                    shouldThrow();
302 >                } catch (InterruptedException success) {}
303              }});
304 +
305 +        t.start();
306          Thread.sleep(SHORT_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 295 | Line 330 | public class LinkedTransferQueueTest ext
330              assertEquals(i, (int) q.poll(0, MILLISECONDS));
331          }
332          assertNull(q.poll(0, MILLISECONDS));
333 +        checkEmpty(q);
334      }
335  
336      /**
# Line 303 | 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, 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, MILLISECONDS));
348 +        checkEmpty(q);
349      }
350  
351      /**
# Line 314 | Line 354 | public class LinkedTransferQueueTest ext
354       */
355      public void testInterruptedTimedPoll() throws InterruptedException {
356          final LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
357 <        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
358 <            void realRun() throws InterruptedException {
357 >        Thread t = newStartedThread(new CheckedRunnable() {
358 >            public void realRun() throws InterruptedException {
359                  for (int i = 0; i < SIZE; ++i) {
360 <                    threadAssertEquals(i, (int) q.poll(LONG_DELAY_MS,
361 <                                                       MILLISECONDS));
360 >                    long t0 = System.nanoTime();
361 >                    assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
362 >                    long millisElapsed = (System.nanoTime() - t0)/(1024 * 1024);
363 >                    assertTrue(millisElapsed < SMALL_DELAY_MS);
364                  }
365 <                q.poll(LONG_DELAY_MS, MILLISECONDS);
365 >                try {
366 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
367 >                    shouldThrow();
368 >                } catch (InterruptedException success) {}
369              }});
325        Thread.sleep(SMALL_DELAY_MS);
326        t.interrupt();
327        t.join();
328        assertEquals(q.size(), 0);
329        assertNull(q.poll());
330    }
370  
332    /**
333     * timed poll before a delayed offer fails; after offer succeeds;
334     * on interruption throws
335     */
336    public void testTimedPollWithOffer() throws InterruptedException {
337        final LinkedTransferQueue q = new LinkedTransferQueue();
338        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
339            void realRun() throws InterruptedException {
340                threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
341                q.poll(LONG_DELAY_MS, MILLISECONDS);
342                q.poll(LONG_DELAY_MS, MILLISECONDS);
343            }});
371          Thread.sleep(SMALL_DELAY_MS);
345        assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
372          t.interrupt();
373          t.join();
374 +        checkEmpty(q);
375      }
376  
377      /**
378       * peek returns next element, or null if empty
379       */
380 <    public void testPeek() {
380 >    public void testPeek() throws InterruptedException {
381          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
382          for (int i = 0; i < SIZE; ++i) {
383              assertEquals(i, (int) q.peek());
# Line 359 | Line 386 | public class LinkedTransferQueueTest ext
386                         i != (int) q.peek());
387          }
388          assertNull(q.peek());
389 +        checkEmpty(q);
390      }
391  
392      /**
393       * element returns next element, or throws NoSuchElementException if empty
394       */
395 <    public void testElement() {
395 >    public void testElement() throws InterruptedException {
396          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
397          for (int i = 0; i < SIZE; ++i) {
398              assertEquals(i, (int) q.element());
# Line 373 | Line 401 | public class LinkedTransferQueueTest ext
401          try {
402              q.element();
403              shouldThrow();
404 <        } catch (NoSuchElementException success) {
405 <        }
404 >        } catch (NoSuchElementException success) {}
405 >        checkEmpty(q);
406      }
407  
408      /**
409       * remove removes next element, or throws NoSuchElementException if empty
410       */
411 <    public void testRemove() {
411 >    public void testRemove() throws InterruptedException {
412          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
413          for (int i = 0; i < SIZE; ++i) {
414              assertEquals(i, (int) q.remove());
# Line 388 | Line 416 | public class LinkedTransferQueueTest ext
416          try {
417              q.remove();
418              shouldThrow();
419 <        } catch (NoSuchElementException success) {
420 <        }
419 >        } catch (NoSuchElementException success) {}
420 >        checkEmpty(q);
421      }
422  
423      /**
424       * remove(x) removes x and returns true if present
425       */
426 <    public void testRemoveElement() {
426 >    public void testRemoveElement() throws InterruptedException {
427          LinkedTransferQueue q = populatedQueue(SIZE);
428          for (int i = 1; i < SIZE; i += 2) {
429              assertTrue(q.remove(i));
# Line 404 | Line 432 | public class LinkedTransferQueueTest ext
432              assertTrue(q.remove(i));
433              assertFalse(q.remove(i + 1));
434          }
435 <        assertTrue(q.isEmpty());
435 >        checkEmpty(q);
436      }
437  
438      /**
# Line 417 | Line 445 | public class LinkedTransferQueueTest ext
445          assertTrue(q.remove(one));
446          assertTrue(q.remove(two));
447          assertTrue(q.add(three));
448 <        assertTrue(q.take() != null);
448 >        assertSame(q.take(), three);
449      }
450  
451      /**
# Line 435 | Line 463 | public class LinkedTransferQueueTest ext
463      /**
464       * clear removes all elements
465       */
466 <    public void testClear() {
466 >    public void testClear() throws InterruptedException {
467          LinkedTransferQueue q = populatedQueue(SIZE);
468          q.clear();
469 <        assertTrue(q.isEmpty());
442 <        assertEquals(0, q.size());
469 >        checkEmpty(q);
470          assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
471          q.add(one);
472          assertFalse(q.isEmpty());
473 +        assertEquals(1, q.size());
474          assertTrue(q.contains(one));
475          q.clear();
476 <        assertTrue(q.isEmpty());
476 >        checkEmpty(q);
477      }
478  
479      /**
# Line 499 | Line 527 | public class LinkedTransferQueueTest ext
527      }
528  
529      /**
530 <     * toArray contains all elements
530 >     * toArray() contains all elements
531       */
532      public void testToArray() throws InterruptedException {
533          LinkedTransferQueue q = populatedQueue(SIZE);
# Line 525 | Line 553 | public class LinkedTransferQueueTest ext
553       * toArray(null) throws NullPointerException
554       */
555      public void testToArray_BadArg() {
556 +        LinkedTransferQueue q = populatedQueue(SIZE);
557          try {
529            LinkedTransferQueue q = populatedQueue(SIZE);
558              Object o[] = q.toArray(null);
559              shouldThrow();
560 <        } catch (NullPointerException success) {
533 <        }
560 >        } catch (NullPointerException success) {}
561      }
562  
563      /**
564 <     * toArray with incompatible array type throws CCE
564 >     * toArray(incompatible array type) throws CCE
565       */
566      public void testToArray1_BadArg() {
567 +        LinkedTransferQueue q = populatedQueue(SIZE);
568          try {
541            LinkedTransferQueue q = populatedQueue(SIZE);
569              Object o[] = q.toArray(new String[10]);
570              shouldThrow();
571 <        } catch (ArrayStoreException success) {
545 <        }
571 >        } catch (ArrayStoreException success) {}
572      }
573  
574      /**
# Line 551 | Line 577 | public class LinkedTransferQueueTest ext
577      public void testIterator() throws InterruptedException {
578          LinkedTransferQueue q = populatedQueue(SIZE);
579          Iterator it = q.iterator();
580 +        int i = 0;
581          while (it.hasNext()) {
582 <            assertEquals(it.next(), q.take());
582 >            assertEquals(it.next(), i++);
583          }
584 +        assertEquals(i, SIZE);
585      }
586  
587      /**
588 <     * iterator.remove removes current element
588 >     * iterator.remove() removes current element
589       */
590      public void testIteratorRemove() {
591          final LinkedTransferQueue q = new LinkedTransferQueue();
# Line 570 | Line 598 | public class LinkedTransferQueueTest ext
598          it.remove();
599  
600          it = q.iterator();
601 <        assertEquals(it.next(), one);
602 <        assertEquals(it.next(), three);
601 >        assertSame(it.next(), one);
602 >        assertSame(it.next(), three);
603          assertFalse(it.hasNext());
604      }
605  
# Line 629 | Line 657 | public class LinkedTransferQueueTest ext
657          ExecutorService executor = Executors.newFixedThreadPool(2);
658  
659          executor.execute(new CheckedRunnable() {
660 <            void realRun() {
661 <                threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS,
634 <                                         MILLISECONDS));
660 >            public void realRun() {
661 >                assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
662              }});
663  
664          executor.execute(new CheckedRunnable() {
665 <            void realRun() throws InterruptedException {
665 >            public void realRun() throws InterruptedException {
666                  Thread.sleep(SMALL_DELAY_MS);
667 <                threadAssertEquals(one, q.take());
667 >                assertSame(one, q.take());
668              }});
669  
670          joinPool(executor);
671      }
672  
673      /**
674 <     * poll retrieves elements across Executor threads
674 >     * timed poll retrieves elements across Executor threads
675       */
676      public void testPollInExecutor() {
677          final LinkedTransferQueue q = new LinkedTransferQueue();
678          ExecutorService executor = Executors.newFixedThreadPool(2);
679  
680          executor.execute(new CheckedRunnable() {
681 <            void realRun() throws InterruptedException {
682 <                threadAssertNull(q.poll());
683 <                threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS,
684 <                                                MILLISECONDS));
658 <                threadAssertTrue(q.isEmpty());
681 >            public void realRun() throws InterruptedException {
682 >                assertNull(q.poll());
683 >                assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
684 >                assertTrue(q.isEmpty());
685              }});
686  
687          executor.execute(new CheckedRunnable() {
688 <            void realRun() throws InterruptedException {
688 >            public void realRun() throws InterruptedException {
689                  Thread.sleep(SMALL_DELAY_MS);
690                  q.put(one);
691              }});
# Line 699 | Line 725 | public class LinkedTransferQueueTest ext
725          try {
726              q.drainTo(null);
727              shouldThrow();
728 <        } catch (NullPointerException success) {
703 <        }
728 >        } catch (NullPointerException success) {}
729      }
730  
731      /**
# Line 711 | Line 736 | public class LinkedTransferQueueTest ext
736          try {
737              q.drainTo(q);
738              shouldThrow();
739 <        } catch (IllegalArgumentException success) {
715 <        }
739 >        } catch (IllegalArgumentException success) {}
740      }
741  
742      /**
# Line 742 | Line 766 | public class LinkedTransferQueueTest ext
766      }
767  
768      /**
769 <     * drainTo empties full queue, unblocking a waiting put.
769 >     * drainTo(c) empties full queue, unblocking a waiting put.
770       */
771      public void testDrainToWithActivePut() throws InterruptedException {
772          final LinkedTransferQueue q = populatedQueue(SIZE);
773          Thread t = newStartedThread(new CheckedRunnable() {
774 <            void realRun() {
774 >            public void realRun() {
775                  q.put(SIZE + 1);
776              }});
777          ArrayList l = new ArrayList();
# Line 766 | Line 790 | public class LinkedTransferQueueTest ext
790      public void testDrainToNullN() {
791          LinkedTransferQueue q = populatedQueue(SIZE);
792          try {
793 <            q.drainTo(null, 0);
793 >            q.drainTo(null, SIZE);
794              shouldThrow();
795 <        } catch (NullPointerException success) {
772 <        }
795 >        } catch (NullPointerException success) {}
796      }
797  
798      /**
# Line 778 | Line 801 | public class LinkedTransferQueueTest ext
801      public void testDrainToSelfN() {
802          LinkedTransferQueue q = populatedQueue(SIZE);
803          try {
804 <            q.drainTo(q, 0);
804 >            q.drainTo(q, SIZE);
805              shouldThrow();
806 <        } catch (IllegalArgumentException success) {
784 <        }
806 >        } catch (IllegalArgumentException success) {}
807      }
808  
809      /**
# Line 807 | Line 829 | public class LinkedTransferQueueTest ext
829      }
830  
831      /**
832 <     * poll and take decrement the waiting consumer count
832 >     * timed poll() or take() increments the waiting consumer count;
833 >     * offer(e) decrements the waiting consumer count
834       */
835      public void testWaitingConsumer() throws InterruptedException {
836          final LinkedTransferQueue q = new LinkedTransferQueue();
837 <        final ConsumerObserver waiting = new ConsumerObserver();
837 >        assertEquals(q.getWaitingConsumerCount(), 0);
838 >        assertFalse(q.hasWaitingConsumer());
839  
840          Thread t = newStartedThread(new CheckedRunnable() {
841 <            void realRun() throws InterruptedException {
841 >            public void realRun() throws InterruptedException {
842                  Thread.sleep(SMALL_DELAY_MS);
843 <                threadAssertTrue(q.hasWaitingConsumer());
844 <                waiting.setWaitingConsumer(q.getWaitingConsumerCount());
845 <                threadAssertTrue(q.offer(new Object()));
843 >                assertTrue(q.hasWaitingConsumer());
844 >                assertEquals(q.getWaitingConsumerCount(), 1);
845 >                assertTrue(q.offer(one));
846 >                assertFalse(q.hasWaitingConsumer());
847 >                assertEquals(q.getWaitingConsumerCount(), 0);
848              }});
849  
850 <        assertTrue(q.poll(LONG_DELAY_MS, MILLISECONDS) != null);
851 <        assertTrue(q.getWaitingConsumerCount()
852 <                   < waiting.getWaitingConsumers());
850 >        assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
851 >        assertEquals(q.getWaitingConsumerCount(), 0);
852 >        assertFalse(q.hasWaitingConsumer());
853          t.join();
854      }
855  
# Line 835 | Line 861 | public class LinkedTransferQueueTest ext
861              LinkedTransferQueue q = new LinkedTransferQueue();
862              q.transfer(null);
863              shouldThrow();
864 <        } catch (NullPointerException ex) {
839 <        }
864 >        } catch (NullPointerException success) {}
865      }
866  
867      /**
# Line 848 | Line 873 | public class LinkedTransferQueueTest ext
873              = new LinkedTransferQueue<Integer>();
874  
875          Thread t = newStartedThread(new CheckedRunnable() {
876 <            void realRun() throws InterruptedException {
876 >            public void realRun() throws InterruptedException {
877                  q.transfer(SIZE);
878 <                threadAssertTrue(q.isEmpty());
878 >                assertTrue(q.isEmpty());
879              }});
880  
881          Thread.sleep(SHORT_DELAY_MS);
# Line 868 | Line 893 | public class LinkedTransferQueueTest ext
893              = new LinkedTransferQueue<Integer>();
894  
895          Thread first = newStartedThread(new CheckedRunnable() {
896 <            void realRun() throws InterruptedException {
896 >            public void realRun() throws InterruptedException {
897                  Integer i = SIZE + 1;
898                  q.transfer(i);
899 <                threadAssertTrue(!q.contains(i));
900 <                threadAssertEquals(1, q.size());
899 >                assertTrue(!q.contains(i));
900 >                assertEquals(1, q.size());
901              }});
902  
903          Thread interruptedThread = newStartedThread(
904              new CheckedInterruptedRunnable() {
905 <                void realRun() throws InterruptedException {
905 >                public void realRun() throws InterruptedException {
906                      while (q.size() == 0)
907                          Thread.yield();
908                      q.transfer(SIZE);
# Line 903 | Line 928 | public class LinkedTransferQueueTest ext
928          final LinkedTransferQueue q = new LinkedTransferQueue();
929  
930          Thread t = newStartedThread(new CheckedRunnable() {
931 <            void realRun() throws InterruptedException {
931 >            public void realRun() throws InterruptedException {
932                  q.transfer(four);
933 <                threadAssertFalse(q.contains(four));
934 <                threadAssertEquals(three, q.poll());
933 >                assertFalse(q.contains(four));
934 >                assertSame(three, q.poll());
935              }});
936  
937          Thread.sleep(SHORT_DELAY_MS);
938          assertTrue(q.offer(three));
939 <        assertEquals(four, q.poll());
939 >        assertSame(four, q.poll());
940          t.join();
941      }
942  
# Line 924 | Line 949 | public class LinkedTransferQueueTest ext
949              = new LinkedTransferQueue<Integer>();
950  
951          Thread t = newStartedThread(new CheckedRunnable() {
952 <            void realRun() throws InterruptedException {
952 >            public void realRun() throws InterruptedException {
953                  q.transfer(SIZE);
954 <                threadAssertTrue(q.isEmpty());
954 >                checkEmpty(q);
955              }});
956  
957          Thread.sleep(SHORT_DELAY_MS);
958          assertEquals(SIZE, (int) q.take());
959 <        assertTrue(q.isEmpty());
959 >        checkEmpty(q);
960          t.join();
961      }
962  
# Line 943 | Line 968 | public class LinkedTransferQueueTest ext
968              final LinkedTransferQueue q = new LinkedTransferQueue();
969              q.tryTransfer(null);
970              shouldThrow();
971 <        } catch (NullPointerException ex) {
947 <        }
971 >        } catch (NullPointerException success) {}
972      }
973  
974      /**
975       * tryTransfer returns false and does not enqueue if there are no
976       * consumers waiting to poll or take.
977       */
978 <    public void testTryTransfer2() {
978 >    public void testTryTransfer2() throws InterruptedException {
979          final LinkedTransferQueue q = new LinkedTransferQueue();
980          assertFalse(q.tryTransfer(new Object()));
981          assertFalse(q.hasWaitingConsumer());
982 <        assertTrue(q.isEmpty());
959 <        assertEquals(0, q.size());
982 >        checkEmpty(q);
983      }
984  
985      /**
# Line 968 | Line 991 | public class LinkedTransferQueueTest ext
991          final LinkedTransferQueue q = new LinkedTransferQueue();
992  
993          Thread t = newStartedThread(new CheckedRunnable() {
994 <            void realRun() {
994 >            public void realRun() {
995                  while (! q.hasWaitingConsumer())
996                      Thread.yield();
997 <                threadAssertTrue(q.hasWaitingConsumer());
998 <                threadAssertTrue(q.isEmpty());
999 <                threadAssertTrue(q.size() == 0);
1000 <                threadAssertTrue(q.tryTransfer(hotPotato));
997 >                assertTrue(q.hasWaitingConsumer());
998 >                assertTrue(q.isEmpty());
999 >                assertEquals(q.size(), 0);
1000 >                assertTrue(q.tryTransfer(hotPotato));
1001              }});
1002  
1003 <        assertTrue(q.poll(MEDIUM_DELAY_MS, MILLISECONDS) == hotPotato);
1004 <        assertTrue(q.isEmpty());
1003 >        assertSame(hotPotato, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
1004 >        checkEmpty(q);
1005          t.join();
1006      }
1007  
# Line 991 | Line 1014 | public class LinkedTransferQueueTest ext
1014          final LinkedTransferQueue q = new LinkedTransferQueue();
1015  
1016          Thread t = newStartedThread(new CheckedRunnable() {
1017 <            void realRun() {
1017 >            public void realRun() {
1018                  while (! q.hasWaitingConsumer())
1019                      Thread.yield();
1020 <                threadAssertTrue(q.hasWaitingConsumer());
1021 <                threadAssertTrue(q.isEmpty());
1022 <                threadAssertTrue(q.size() == 0);
1023 <                threadAssertTrue(q.tryTransfer(hotPotato));
1020 >                assertTrue(q.hasWaitingConsumer());
1021 >                assertTrue(q.isEmpty());
1022 >                assertEquals(q.size(), 0);
1023 >                assertTrue(q.tryTransfer(hotPotato));
1024              }});
1025  
1026 <        assertTrue(q.take() == hotPotato);
1027 <        assertTrue(q.isEmpty());
1026 >        assertSame(q.take(), hotPotato);
1027 >        checkEmpty(q);
1028          t.join();
1029      }
1030  
# Line 1013 | Line 1036 | public class LinkedTransferQueueTest ext
1036          final LinkedTransferQueue q = new LinkedTransferQueue();
1037  
1038          Thread toInterrupt = newStartedThread(new CheckedInterruptedRunnable() {
1039 <            void realRun() throws InterruptedException {
1039 >            public void realRun() throws InterruptedException {
1040                  q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS);
1041              }});
1042  
# Line 1029 | Line 1052 | public class LinkedTransferQueueTest ext
1052          final LinkedTransferQueue q = new LinkedTransferQueue();
1053  
1054          Thread t = newStartedThread(new CheckedRunnable() {
1055 <            void realRun() throws InterruptedException {
1056 <                threadAssertFalse
1057 <                    (q.tryTransfer(new Object(),
1035 <                                   SHORT_DELAY_MS, MILLISECONDS));
1055 >            public void realRun() throws InterruptedException {
1056 >                assertFalse(q.tryTransfer(new Object(),
1057 >                                          SHORT_DELAY_MS, MILLISECONDS));
1058              }});
1059  
1060          Thread.sleep(SMALL_DELAY_MS);
1061 <        assertTrue(q.isEmpty());
1061 >        checkEmpty(q);
1062          t.join();
1063      }
1064  
# Line 1049 | Line 1071 | public class LinkedTransferQueueTest ext
1071          assertTrue(q.offer(four));
1072  
1073          Thread t = newStartedThread(new CheckedRunnable() {
1074 <            void realRun() throws InterruptedException {
1075 <                threadAssertTrue(q.tryTransfer(five,
1076 <                                               MEDIUM_DELAY_MS, MILLISECONDS));
1055 <                threadAssertTrue(q.isEmpty());
1074 >            public void realRun() throws InterruptedException {
1075 >                assertTrue(q.tryTransfer(five, MEDIUM_DELAY_MS, MILLISECONDS));
1076 >                assertTrue(q.isEmpty());
1077              }});
1078  
1079          Thread.sleep(SHORT_DELAY_MS);
1080          assertEquals(2, q.size());
1081 <        assertEquals(four, q.poll());
1082 <        assertEquals(five, q.poll());
1083 <        assertTrue(q.isEmpty());
1081 >        assertSame(four, q.poll());
1082 >        assertSame(five, q.poll());
1083 >        checkEmpty(q);
1084          t.join();
1085      }
1086  
# Line 1073 | Line 1094 | public class LinkedTransferQueueTest ext
1094          assertEquals(1, q.size());
1095          assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS));
1096          assertEquals(1, q.size());
1097 <        assertEquals(four, q.poll());
1077 <        threadAssertTrue(q.isEmpty());
1097 >        assertSame(four, q.poll());
1098          assertNull(q.poll());
1099 +        checkEmpty(q);
1100      }
1101  
1102      private LinkedTransferQueue<Integer> populatedQueue(int n) {
# Line 1089 | Line 1110 | public class LinkedTransferQueueTest ext
1110          assertFalse(q.isEmpty());
1111          return q;
1112      }
1092
1093    private static class ConsumerObserver {
1094
1095        private int waitingConsumers;
1096
1097        private ConsumerObserver() {
1098        }
1099
1100        private void setWaitingConsumer(int i) {
1101            this.waitingConsumers = i;
1102        }
1103
1104        private int getWaitingConsumers() {
1105            return waitingConsumers;
1106        }
1107    }
1113   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines