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.7 by jsr166, Mon Aug 3 19:07:51 2009 UTC vs.
Revision 1.23 by jsr166, Tue Dec 1 06:28:43 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 31 | 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
60       * being true
# Line 48 | Line 72 | public class LinkedTransferQueueTest ext
72          try {
73              new LinkedTransferQueue(null);
74              shouldThrow();
75 <        } catch (NullPointerException success) {
52 <        }
75 >        } catch (NullPointerException success) {}
76      }
77  
78      /**
# Line 61 | Line 84 | public class LinkedTransferQueueTest ext
84              Integer[] ints = new Integer[SIZE];
85              new LinkedTransferQueue(Arrays.asList(ints));
86              shouldThrow();
87 <        } catch (NullPointerException success) {
65 <        }
87 >        } catch (NullPointerException success) {}
88      }
89  
90      /**
# Line 77 | Line 99 | public class LinkedTransferQueueTest ext
99              }
100              new LinkedTransferQueue(Arrays.asList(ints));
101              shouldThrow();
102 <        } catch (NullPointerException success) {
81 <        }
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
115 <                = new LinkedTransferQueue(Arrays.asList(ints));
116 <            for (int i = 0; i < SIZE; ++i) {
117 <                assertEquals(ints[i], q.poll());
118 <            }
119 <        } 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 124 | Line 151 | public class LinkedTransferQueueTest ext
151              LinkedTransferQueue q = new LinkedTransferQueue();
152              q.offer(null);
153              shouldThrow();
154 <        } catch (NullPointerException success) {
128 <        }
154 >        } catch (NullPointerException success) {}
155      }
156  
157      /**
# Line 136 | Line 162 | public class LinkedTransferQueueTest ext
162              LinkedTransferQueue q = new LinkedTransferQueue();
163              q.add(null);
164              shouldThrow();
165 <        } catch (NullPointerException success) {
140 <        }
165 >        } catch (NullPointerException success) {}
166      }
167  
168      /**
# Line 148 | Line 173 | public class LinkedTransferQueueTest ext
173              LinkedTransferQueue q = new LinkedTransferQueue();
174              q.addAll(null);
175              shouldThrow();
176 <        } catch (NullPointerException success) {
152 <        }
176 >        } catch (NullPointerException success) {}
177      }
178  
179      /**
# Line 160 | Line 184 | public class LinkedTransferQueueTest ext
184              LinkedTransferQueue q = populatedQueue(SIZE);
185              q.addAll(q);
186              shouldThrow();
187 <        } catch (IllegalArgumentException success) {
164 <        }
187 >        } catch (IllegalArgumentException success) {}
188      }
189  
190      /**
# Line 173 | Line 196 | public class LinkedTransferQueueTest ext
196              Integer[] ints = new Integer[SIZE];
197              q.addAll(Arrays.asList(ints));
198              shouldThrow();
199 <        } catch (NullPointerException success) {
177 <        }
199 >        } catch (NullPointerException success) {}
200      }
201  
202      /**
# Line 190 | Line 212 | public class LinkedTransferQueueTest ext
212              }
213              q.addAll(Arrays.asList(ints));
214              shouldThrow();
215 <        } catch (NullPointerException success) {
194 <        }
215 >        } catch (NullPointerException success) {}
216      }
217  
218      /**
# Line 228 | 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          }
234        assertEquals(q.size(), SIZE);
256      }
257  
258      /**
# Line 249 | 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 CheckedInterruptedRunnable() {
274 <            void realRun() throws InterruptedException {
273 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
274 >            public void realRun() throws InterruptedException {
275                  q.take();
276              }});
256        t.start();
277          Thread.sleep(SHORT_DELAY_MS);
278          t.interrupt();
279          t.join();
# Line 263 | 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 CheckedInterruptedRunnable() {
287 <            void realRun() throws InterruptedException {
288 <                LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
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 <                    threadAssertEquals(i, (int) q.take());
290 >                    assertEquals(i, (int) q.take());
291                  }
292 <                q.take();
292 >                try {
293 >                    q.take();
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 297 | Line 323 | public class LinkedTransferQueueTest ext
323              assertEquals(i, (int) q.poll(0, MILLISECONDS));
324          }
325          assertNull(q.poll(0, MILLISECONDS));
326 +        checkEmpty(q);
327      }
328  
329      /**
# Line 305 | 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, 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, MILLISECONDS));
341 +        checkEmpty(q);
342      }
343  
344      /**
# Line 315 | Line 346 | public class LinkedTransferQueueTest ext
346       * returning timeout status
347       */
348      public void testInterruptedTimedPoll() throws InterruptedException {
349 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
350 <            void realRun() throws InterruptedException {
351 <                LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
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 <                    threadAssertEquals(i, (int) q.poll(SHORT_DELAY_MS,
354 <                                                       MILLISECONDS));
353 >                    long t0 = System.nanoTime();
354 >                    assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
355 >                    long millisElapsed = (System.nanoTime() - t0)/(1024 * 1024);
356 >                    assertTrue(millisElapsed < SMALL_DELAY_MS);
357                  }
358 <                q.poll(SHORT_DELAY_MS, MILLISECONDS);
358 >                try {
359 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
360 >                    shouldThrow();
361 >                } catch (InterruptedException success) {}
362              }});
363 <        t.start();
364 <        Thread.sleep(SHORT_DELAY_MS);
363 >
364 >        Thread.sleep(SMALL_DELAY_MS);
365          t.interrupt();
366          t.join();
367 +        checkEmpty(q);
368      }
369  
370      /**
# Line 336 | Line 373 | public class LinkedTransferQueueTest ext
373       */
374      public void testTimedPollWithOffer() throws InterruptedException {
375          final LinkedTransferQueue q = new LinkedTransferQueue();
376 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
377 <            void realRun() throws InterruptedException {
378 <                threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
379 <                q.poll(LONG_DELAY_MS, MILLISECONDS);
380 <                q.poll(LONG_DELAY_MS, MILLISECONDS);
376 >        Thread t = new Thread(new CheckedRunnable() {
377 >            public void realRun() throws InterruptedException {
378 >                assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
379 >                assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
380 >                try {
381 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
382 >                    shouldThrow();
383 >                } catch (InterruptedException success) {}
384              }});
385 +
386          t.start();
387          Thread.sleep(SMALL_DELAY_MS);
388          assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
# Line 352 | Line 393 | public class LinkedTransferQueueTest ext
393      /**
394       * peek returns next element, or null if empty
395       */
396 <    public void testPeek() {
396 >    public void testPeek() throws InterruptedException {
397          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
398          for (int i = 0; i < SIZE; ++i) {
399              assertEquals(i, (int) q.peek());
# Line 361 | Line 402 | public class LinkedTransferQueueTest ext
402                         i != (int) q.peek());
403          }
404          assertNull(q.peek());
405 +        checkEmpty(q);
406      }
407  
408      /**
409       * element returns next element, or throws NoSuchElementException if empty
410       */
411 <    public void testElement() {
411 >    public void testElement() throws InterruptedException {
412          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
413          for (int i = 0; i < SIZE; ++i) {
414              assertEquals(i, (int) q.element());
# Line 375 | Line 417 | public class LinkedTransferQueueTest ext
417          try {
418              q.element();
419              shouldThrow();
420 <        } catch (NoSuchElementException success) {
421 <        }
420 >        } catch (NoSuchElementException success) {}
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 390 | Line 432 | public class LinkedTransferQueueTest ext
432          try {
433              q.remove();
434              shouldThrow();
435 <        } catch (NoSuchElementException success) {
436 <        }
435 >        } catch (NoSuchElementException success) {}
436 >        checkEmpty(q);
437      }
438  
439      /**
440       * remove(x) removes x and returns true if present
441       */
442 <    public void testRemoveElement() {
442 >    public void testRemoveElement() throws InterruptedException {
443          LinkedTransferQueue q = populatedQueue(SIZE);
444          for (int i = 1; i < SIZE; i += 2) {
445              assertTrue(q.remove(i));
# Line 406 | Line 448 | public class LinkedTransferQueueTest ext
448              assertTrue(q.remove(i));
449              assertFalse(q.remove(i + 1));
450          }
451 <        assertTrue(q.isEmpty());
451 >        checkEmpty(q);
452      }
453  
454      /**
# Line 419 | Line 461 | public class LinkedTransferQueueTest ext
461          assertTrue(q.remove(one));
462          assertTrue(q.remove(two));
463          assertTrue(q.add(three));
464 <        assertTrue(q.take() != null);
464 >        assertSame(q.take(), three);
465      }
466  
467      /**
# Line 437 | Line 479 | public class LinkedTransferQueueTest ext
479      /**
480       * clear removes all elements
481       */
482 <    public void testClear() {
482 >    public void testClear() throws InterruptedException {
483          LinkedTransferQueue q = populatedQueue(SIZE);
484          q.clear();
485 <        assertTrue(q.isEmpty());
444 <        assertEquals(0, q.size());
485 >        checkEmpty(q);
486          assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
487          q.add(one);
488          assertFalse(q.isEmpty());
489 +        assertEquals(1, q.size());
490          assertTrue(q.contains(one));
491          q.clear();
492 <        assertTrue(q.isEmpty());
492 >        checkEmpty(q);
493      }
494  
495      /**
# Line 501 | Line 543 | public class LinkedTransferQueueTest ext
543      }
544  
545      /**
546 <     * toArray contains all elements
546 >     * toArray() contains all elements
547       */
548      public void testToArray() throws InterruptedException {
549          LinkedTransferQueue q = populatedQueue(SIZE);
# Line 527 | Line 569 | public class LinkedTransferQueueTest ext
569       * toArray(null) throws NullPointerException
570       */
571      public void testToArray_BadArg() {
572 +        LinkedTransferQueue q = populatedQueue(SIZE);
573          try {
531            LinkedTransferQueue q = populatedQueue(SIZE);
574              Object o[] = q.toArray(null);
575              shouldThrow();
576 <        } catch (NullPointerException success) {
535 <        }
576 >        } catch (NullPointerException success) {}
577      }
578  
579      /**
580 <     * toArray with incompatible array type throws CCE
580 >     * toArray(incompatible array type) throws CCE
581       */
582      public void testToArray1_BadArg() {
583 +        LinkedTransferQueue q = populatedQueue(SIZE);
584          try {
543            LinkedTransferQueue q = populatedQueue(SIZE);
585              Object o[] = q.toArray(new String[10]);
586              shouldThrow();
587 <        } catch (ArrayStoreException success) {
547 <        }
587 >        } catch (ArrayStoreException success) {}
588      }
589  
590      /**
# Line 553 | Line 593 | public class LinkedTransferQueueTest ext
593      public void testIterator() throws InterruptedException {
594          LinkedTransferQueue q = populatedQueue(SIZE);
595          Iterator it = q.iterator();
596 +        int i = 0;
597          while (it.hasNext()) {
598 <            assertEquals(it.next(), q.take());
598 >            assertEquals(it.next(), i++);
599          }
600 +        assertEquals(i, SIZE);
601      }
602  
603      /**
604 <     * iterator.remove removes current element
604 >     * iterator.remove() removes current element
605       */
606      public void testIteratorRemove() {
607          final LinkedTransferQueue q = new LinkedTransferQueue();
# Line 631 | Line 673 | public class LinkedTransferQueueTest ext
673          ExecutorService executor = Executors.newFixedThreadPool(2);
674  
675          executor.execute(new CheckedRunnable() {
676 <            void realRun() {
677 <                threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS,
636 <                                         MILLISECONDS));
676 >            public void realRun() {
677 >                assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
678              }});
679  
680          executor.execute(new CheckedRunnable() {
681 <            void realRun() throws InterruptedException {
681 >            public void realRun() throws InterruptedException {
682                  Thread.sleep(SMALL_DELAY_MS);
683 <                threadAssertEquals(one, q.take());
683 >                assertEquals(one, q.take());
684              }});
685  
686          joinPool(executor);
687      }
688  
689      /**
690 <     * poll retrieves elements across Executor threads
690 >     * timed poll retrieves elements across Executor threads
691       */
692      public void testPollInExecutor() {
693          final LinkedTransferQueue q = new LinkedTransferQueue();
694          ExecutorService executor = Executors.newFixedThreadPool(2);
695  
696          executor.execute(new CheckedRunnable() {
697 <            void realRun() throws InterruptedException {
698 <                threadAssertNull(q.poll());
699 <                threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS,
700 <                                                MILLISECONDS));
660 <                threadAssertTrue(q.isEmpty());
697 >            public void realRun() throws InterruptedException {
698 >                assertNull(q.poll());
699 >                assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
700 >                assertTrue(q.isEmpty());
701              }});
702  
703          executor.execute(new CheckedRunnable() {
704 <            void realRun() throws InterruptedException {
704 >            public void realRun() throws InterruptedException {
705                  Thread.sleep(SMALL_DELAY_MS);
706                  q.put(one);
707              }});
# Line 701 | Line 741 | public class LinkedTransferQueueTest ext
741          try {
742              q.drainTo(null);
743              shouldThrow();
744 <        } catch (NullPointerException success) {
705 <        }
744 >        } catch (NullPointerException success) {}
745      }
746  
747      /**
# Line 713 | Line 752 | public class LinkedTransferQueueTest ext
752          try {
753              q.drainTo(q);
754              shouldThrow();
755 <        } catch (IllegalArgumentException success) {
717 <        }
755 >        } catch (IllegalArgumentException success) {}
756      }
757  
758      /**
# Line 744 | Line 782 | public class LinkedTransferQueueTest ext
782      }
783  
784      /**
785 <     * drainTo empties full queue, unblocking a waiting put.
785 >     * drainTo(c) empties full queue, unblocking a waiting put.
786       */
787      public void testDrainToWithActivePut() throws InterruptedException {
788          final LinkedTransferQueue q = populatedQueue(SIZE);
789 <        Thread t = new Thread(new CheckedRunnable() {
790 <            void realRun() {
789 >        Thread t = newStartedThread(new CheckedRunnable() {
790 >            public void realRun() {
791                  q.put(SIZE + 1);
792              }});
755        t.start();
793          ArrayList l = new ArrayList();
794          q.drainTo(l);
795          assertTrue(l.size() >= SIZE);
# Line 769 | Line 806 | public class LinkedTransferQueueTest ext
806      public void testDrainToNullN() {
807          LinkedTransferQueue q = populatedQueue(SIZE);
808          try {
809 <            q.drainTo(null, 0);
809 >            q.drainTo(null, SIZE);
810              shouldThrow();
811 <        } catch (NullPointerException success) {
775 <        }
811 >        } catch (NullPointerException success) {}
812      }
813  
814      /**
# Line 781 | Line 817 | public class LinkedTransferQueueTest ext
817      public void testDrainToSelfN() {
818          LinkedTransferQueue q = populatedQueue(SIZE);
819          try {
820 <            q.drainTo(q, 0);
820 >            q.drainTo(q, SIZE);
821              shouldThrow();
822 <        } catch (IllegalArgumentException success) {
787 <        }
822 >        } catch (IllegalArgumentException success) {}
823      }
824  
825      /**
# Line 810 | Line 845 | public class LinkedTransferQueueTest ext
845      }
846  
847      /**
848 <     * poll and take decrement the waiting consumer count
848 >     * timed poll() or take() increments the waiting consumer count;
849 >     * offer(e) decrements the waiting consumer count
850       */
851      public void testWaitingConsumer() throws InterruptedException {
852          final LinkedTransferQueue q = new LinkedTransferQueue();
853 <        final ConsumerObserver waiting = new ConsumerObserver();
854 <        new Thread(new CheckedRunnable() {
855 <            void realRun() {
856 <                threadAssertTrue(q.hasWaitingConsumer());
857 <                waiting.setWaitingConsumer(q.getWaitingConsumerCount());
858 <                threadAssertTrue(q.offer(new Object()));
859 <            }}).start();
860 <        assertTrue(q.poll(LONG_DELAY_MS, MILLISECONDS) != null);
861 <        assertTrue(q.getWaitingConsumerCount()
862 <                   < waiting.getWaitingConsumers());
853 >        assertEquals(q.getWaitingConsumerCount(), 0);
854 >        assertFalse(q.hasWaitingConsumer());
855 >
856 >        Thread t = newStartedThread(new CheckedRunnable() {
857 >            public void realRun() throws InterruptedException {
858 >                Thread.sleep(SMALL_DELAY_MS);
859 >                assertTrue(q.hasWaitingConsumer());
860 >                assertEquals(q.getWaitingConsumerCount(), 1);
861 >                assertTrue(q.offer(one));
862 >                assertFalse(q.hasWaitingConsumer());
863 >                assertEquals(q.getWaitingConsumerCount(), 0);
864 >            }});
865 >
866 >        assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
867 >        assertEquals(q.getWaitingConsumerCount(), 0);
868 >        assertFalse(q.hasWaitingConsumer());
869 >        t.join();
870      }
871  
872      /**
# Line 834 | Line 877 | public class LinkedTransferQueueTest ext
877              LinkedTransferQueue q = new LinkedTransferQueue();
878              q.transfer(null);
879              shouldThrow();
880 <        } catch (NullPointerException ex) {
838 <        }
880 >        } catch (NullPointerException success) {}
881      }
882  
883      /**
# Line 846 | Line 888 | public class LinkedTransferQueueTest ext
888          final LinkedTransferQueue<Integer> q
889              = new LinkedTransferQueue<Integer>();
890  
891 <        new Thread(new CheckedRunnable() {
892 <            void realRun() throws InterruptedException {
891 >        Thread t = newStartedThread(new CheckedRunnable() {
892 >            public void realRun() throws InterruptedException {
893                  q.transfer(SIZE);
894                  threadAssertTrue(q.isEmpty());
895 <            }}).start();
895 >            }});
896  
897          Thread.sleep(SHORT_DELAY_MS);
898          assertEquals(1, q.size());
899          assertEquals(SIZE, (int) q.poll());
900          assertTrue(q.isEmpty());
901 +        t.join();
902      }
903  
904      /**
# Line 865 | Line 908 | public class LinkedTransferQueueTest ext
908          final LinkedTransferQueue<Integer> q
909              = new LinkedTransferQueue<Integer>();
910  
911 <        Thread first = new Thread(new CheckedRunnable() {
912 <            void realRun() throws InterruptedException {
911 >        Thread first = newStartedThread(new CheckedRunnable() {
912 >            public void realRun() throws InterruptedException {
913                  Integer i = SIZE + 1;
914                  q.transfer(i);
915                  threadAssertTrue(!q.contains(i));
916                  threadAssertEquals(1, q.size());
917              }});
875        first.start();
918  
919 <        Thread interruptedThread = new Thread(new CheckedInterruptedRunnable() {
920 <            void realRun() throws InterruptedException {
921 <                while (q.size() == 0)
922 <                    Thread.yield();
923 <                q.transfer(SIZE);
924 <            }});
925 <        interruptedThread.start();
919 >        Thread interruptedThread = newStartedThread(
920 >            new CheckedInterruptedRunnable() {
921 >                public void realRun() throws InterruptedException {
922 >                    while (q.size() == 0)
923 >                        Thread.yield();
924 >                    q.transfer(SIZE);
925 >                }});
926  
927          while (q.size() < 2)
928              Thread.yield();
# Line 900 | Line 942 | public class LinkedTransferQueueTest ext
942       */
943      public void testTransfer4() throws InterruptedException {
944          final LinkedTransferQueue q = new LinkedTransferQueue();
945 <        new Thread(new CheckedRunnable() {
946 <            void realRun() throws InterruptedException {
945 >
946 >        Thread t = newStartedThread(new CheckedRunnable() {
947 >            public void realRun() throws InterruptedException {
948                  q.transfer(four);
949                  threadAssertFalse(q.contains(four));
950                  threadAssertEquals(three, q.poll());
951 <            }}).start();
952 <        Thread.sleep(MEDIUM_DELAY_MS);
951 >            }});
952 >
953 >        Thread.sleep(SHORT_DELAY_MS);
954          assertTrue(q.offer(three));
955          assertEquals(four, q.poll());
956 +        t.join();
957      }
958  
959      /**
# Line 919 | Line 964 | public class LinkedTransferQueueTest ext
964          final LinkedTransferQueue<Integer> q
965              = new LinkedTransferQueue<Integer>();
966  
967 <        new Thread(new CheckedRunnable() {
968 <            void realRun() throws InterruptedException {
967 >        Thread t = newStartedThread(new CheckedRunnable() {
968 >            public void realRun() throws InterruptedException {
969                  q.transfer(SIZE);
970 <                threadAssertTrue(q.isEmpty());
971 <            }}).start();
970 >                checkEmpty(q);
971 >            }});
972  
973          Thread.sleep(SHORT_DELAY_MS);
974          assertEquals(SIZE, (int) q.take());
975 <        assertTrue(q.isEmpty());
975 >        checkEmpty(q);
976 >        t.join();
977      }
978  
979      /**
# Line 937 | Line 983 | public class LinkedTransferQueueTest ext
983          try {
984              final LinkedTransferQueue q = new LinkedTransferQueue();
985              q.tryTransfer(null);
986 <            this.shouldThrow();
987 <        } catch (NullPointerException ex) {
942 <        }
986 >            shouldThrow();
987 >        } catch (NullPointerException success) {}
988      }
989  
990      /**
991       * tryTransfer returns false and does not enqueue if there are no
992       * consumers waiting to poll or take.
993       */
994 <    public void testTryTransfer2() {
994 >    public void testTryTransfer2() throws InterruptedException {
995          final LinkedTransferQueue q = new LinkedTransferQueue();
996          assertFalse(q.tryTransfer(new Object()));
997          assertFalse(q.hasWaitingConsumer());
998 <        assertTrue(q.isEmpty());
954 <        assertEquals(0, q.size());
998 >        checkEmpty(q);
999      }
1000  
1001      /**
# Line 961 | Line 1005 | public class LinkedTransferQueueTest ext
1005      public void testTryTransfer3() throws InterruptedException {
1006          final Object hotPotato = new Object();
1007          final LinkedTransferQueue q = new LinkedTransferQueue();
1008 <        new Thread(new CheckedRunnable() {
1009 <            void realRun() {
1008 >
1009 >        Thread t = newStartedThread(new CheckedRunnable() {
1010 >            public void realRun() {
1011                  while (! q.hasWaitingConsumer())
1012                      Thread.yield();
1013                  threadAssertTrue(q.hasWaitingConsumer());
1014                  threadAssertTrue(q.isEmpty());
1015                  threadAssertTrue(q.size() == 0);
1016                  threadAssertTrue(q.tryTransfer(hotPotato));
1017 <            }}).start();
1018 <        assertTrue(q.poll(LONG_DELAY_MS, MILLISECONDS) == hotPotato);
1019 <        assertTrue(q.isEmpty());
1017 >            }});
1018 >
1019 >        assertSame(hotPotato, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
1020 >        checkEmpty(q);
1021 >        t.join();
1022      }
1023  
1024      /**
# Line 981 | Line 1028 | public class LinkedTransferQueueTest ext
1028      public void testTryTransfer4() throws InterruptedException {
1029          final Object hotPotato = new Object();
1030          final LinkedTransferQueue q = new LinkedTransferQueue();
1031 <        new Thread(new CheckedRunnable() {
1032 <            void realRun() {
1031 >
1032 >        Thread t = newStartedThread(new CheckedRunnable() {
1033 >            public void realRun() {
1034                  while (! q.hasWaitingConsumer())
1035                      Thread.yield();
1036                  threadAssertTrue(q.hasWaitingConsumer());
1037                  threadAssertTrue(q.isEmpty());
1038                  threadAssertTrue(q.size() == 0);
1039                  threadAssertTrue(q.tryTransfer(hotPotato));
1040 <            }}).start();
1041 <        assertTrue(q.take() == hotPotato);
1042 <        assertTrue(q.isEmpty());
1040 >            }});
1041 >
1042 >        assertSame(q.take(), hotPotato);
1043 >        checkEmpty(q);
1044 >        t.join();
1045      }
1046  
1047      /**
# Line 1000 | Line 1050 | public class LinkedTransferQueueTest ext
1050       */
1051      public void testTryTransfer5() throws InterruptedException {
1052          final LinkedTransferQueue q = new LinkedTransferQueue();
1053 <        Thread toInterrupt = new Thread(new CheckedInterruptedRunnable() {
1054 <            void realRun() throws InterruptedException {
1053 >
1054 >        Thread toInterrupt = newStartedThread(new CheckedInterruptedRunnable() {
1055 >            public void realRun() throws InterruptedException {
1056                  q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS);
1057              }});
1058 <        toInterrupt.start();
1058 >
1059          Thread.sleep(SMALL_DELAY_MS);
1060          toInterrupt.interrupt();
1061 +        toInterrupt.join();
1062      }
1063  
1064      /**
# Line 1014 | Line 1066 | public class LinkedTransferQueueTest ext
1066       */
1067      public void testTryTransfer6() throws InterruptedException {
1068          final LinkedTransferQueue q = new LinkedTransferQueue();
1069 <        new Thread(new CheckedRunnable() {
1070 <            void realRun() throws InterruptedException {
1071 <                threadAssertFalse
1072 <                    (q.tryTransfer(new Object(),
1073 <                                   SHORT_DELAY_MS, MILLISECONDS));
1074 <            }}).start();
1075 <        Thread.sleep(LONG_DELAY_MS);
1076 <        assertTrue(q.isEmpty());
1069 >
1070 >        Thread t = newStartedThread(new CheckedRunnable() {
1071 >            public void realRun() throws InterruptedException {
1072 >                assertFalse(q.tryTransfer(new Object(),
1073 >                                          SHORT_DELAY_MS, MILLISECONDS));
1074 >            }});
1075 >
1076 >        Thread.sleep(SMALL_DELAY_MS);
1077 >        checkEmpty(q);
1078 >        t.join();
1079      }
1080  
1081      /**
# Line 1031 | Line 1085 | public class LinkedTransferQueueTest ext
1085      public void testTryTransfer7() throws InterruptedException {
1086          final LinkedTransferQueue q = new LinkedTransferQueue();
1087          assertTrue(q.offer(four));
1088 <        new Thread(new CheckedRunnable() {
1089 <            void realRun() throws InterruptedException {
1090 <                threadAssertTrue(q.tryTransfer(five,
1091 <                                               LONG_DELAY_MS, MILLISECONDS));
1092 <                threadAssertTrue(q.isEmpty());
1093 <            }}).start();
1088 >
1089 >        Thread t = newStartedThread(new CheckedRunnable() {
1090 >            public void realRun() throws InterruptedException {
1091 >                assertTrue(q.tryTransfer(five, MEDIUM_DELAY_MS, MILLISECONDS));
1092 >                assertTrue(q.isEmpty());
1093 >            }});
1094 >
1095          Thread.sleep(SHORT_DELAY_MS);
1096          assertEquals(2, q.size());
1097          assertEquals(four, q.poll());
1098          assertEquals(five, q.poll());
1099 <        assertTrue(q.isEmpty());
1099 >        checkEmpty(q);
1100 >        t.join();
1101      }
1102  
1103      /**
# Line 1055 | Line 1111 | public class LinkedTransferQueueTest ext
1111          assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS));
1112          assertEquals(1, q.size());
1113          assertEquals(four, q.poll());
1058        threadAssertTrue(q.isEmpty());
1114          assertNull(q.poll());
1115 +        checkEmpty(q);
1116      }
1117  
1118      private LinkedTransferQueue<Integer> populatedQueue(int n) {
# Line 1070 | Line 1126 | public class LinkedTransferQueueTest ext
1126          assertFalse(q.isEmpty());
1127          return q;
1128      }
1073
1074    private static class ConsumerObserver {
1075
1076        private int waitingConsumers;
1077
1078        private ConsumerObserver() {
1079        }
1080
1081        private void setWaitingConsumer(int i) {
1082            this.waitingConsumers = i;
1083        }
1084
1085        private int getWaitingConsumers() {
1086            return waitingConsumers;
1087        }
1088    }
1129   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines