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.8 by jsr166, Tue Aug 4 20:35:11 2009 UTC vs.
Revision 1.21 by jsr166, Thu Nov 26 15:42:15 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,
353 >                    long t0 = System.nanoTime();
354 >                    threadAssertEquals(i, (int) q.poll(LONG_DELAY_MS,
355                                                         MILLISECONDS));
356 +                    long millisElapsed = (System.nanoTime() - t0)/(1024 * 1024);
357 +                    assertTrue(millisElapsed < SMALL_DELAY_MS);
358                  }
359 <                q.poll(SHORT_DELAY_MS, MILLISECONDS);
359 >                try {
360 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
361 >                    shouldThrow();
362 >                } catch (InterruptedException success) {}
363              }});
364 <        t.start();
365 <        Thread.sleep(SHORT_DELAY_MS);
364 >
365 >        Thread.sleep(SMALL_DELAY_MS);
366          t.interrupt();
367          t.join();
368 +        checkEmpty(q);
369      }
370  
371      /**
# Line 336 | Line 374 | public class LinkedTransferQueueTest ext
374       */
375      public void testTimedPollWithOffer() throws InterruptedException {
376          final LinkedTransferQueue q = new LinkedTransferQueue();
377 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
378 <            void realRun() throws InterruptedException {
379 <                threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
380 <                q.poll(LONG_DELAY_MS, MILLISECONDS);
381 <                q.poll(LONG_DELAY_MS, MILLISECONDS);
377 >        Thread t = new Thread(new CheckedRunnable() {
378 >            public void realRun() throws InterruptedException {
379 >                assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
380 >                assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
381 >                try {
382 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
383 >                    shouldThrow();
384 >                } catch (InterruptedException success) {}
385              }});
386 +
387          t.start();
388          Thread.sleep(SMALL_DELAY_MS);
389          assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
# Line 352 | Line 394 | public class LinkedTransferQueueTest ext
394      /**
395       * peek returns next element, or null if empty
396       */
397 <    public void testPeek() {
397 >    public void testPeek() throws InterruptedException {
398          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
399          for (int i = 0; i < SIZE; ++i) {
400              assertEquals(i, (int) q.peek());
# Line 361 | Line 403 | public class LinkedTransferQueueTest ext
403                         i != (int) q.peek());
404          }
405          assertNull(q.peek());
406 +        checkEmpty(q);
407      }
408  
409      /**
410       * element returns next element, or throws NoSuchElementException if empty
411       */
412 <    public void testElement() {
412 >    public void testElement() throws InterruptedException {
413          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
414          for (int i = 0; i < SIZE; ++i) {
415              assertEquals(i, (int) q.element());
# Line 375 | Line 418 | public class LinkedTransferQueueTest ext
418          try {
419              q.element();
420              shouldThrow();
421 <        } catch (NoSuchElementException success) {
422 <        }
421 >        } catch (NoSuchElementException success) {}
422 >        checkEmpty(q);
423      }
424  
425      /**
426       * remove removes next element, or throws NoSuchElementException if empty
427       */
428 <    public void testRemove() {
428 >    public void testRemove() throws InterruptedException {
429          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
430          for (int i = 0; i < SIZE; ++i) {
431              assertEquals(i, (int) q.remove());
# Line 390 | Line 433 | public class LinkedTransferQueueTest ext
433          try {
434              q.remove();
435              shouldThrow();
436 <        } catch (NoSuchElementException success) {
437 <        }
436 >        } catch (NoSuchElementException success) {}
437 >        checkEmpty(q);
438      }
439  
440      /**
441       * remove(x) removes x and returns true if present
442       */
443 <    public void testRemoveElement() {
443 >    public void testRemoveElement() throws InterruptedException {
444          LinkedTransferQueue q = populatedQueue(SIZE);
445          for (int i = 1; i < SIZE; i += 2) {
446              assertTrue(q.remove(i));
# Line 406 | 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 419 | 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 437 | 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());
444 <        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 501 | 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 527 | Line 570 | public class LinkedTransferQueueTest ext
570       * toArray(null) throws NullPointerException
571       */
572      public void testToArray_BadArg() {
573 +        LinkedTransferQueue q = populatedQueue(SIZE);
574          try {
531            LinkedTransferQueue q = populatedQueue(SIZE);
575              Object o[] = q.toArray(null);
576              shouldThrow();
577 <        } catch (NullPointerException success) {
535 <        }
577 >        } catch (NullPointerException success) {}
578      }
579  
580      /**
581 <     * toArray with incompatible array type throws CCE
581 >     * toArray(incompatible array type) throws CCE
582       */
583      public void testToArray1_BadArg() {
584 +        LinkedTransferQueue q = populatedQueue(SIZE);
585          try {
543            LinkedTransferQueue q = populatedQueue(SIZE);
586              Object o[] = q.toArray(new String[10]);
587              shouldThrow();
588 <        } catch (ArrayStoreException success) {
547 <        }
588 >        } catch (ArrayStoreException success) {}
589      }
590  
591      /**
# Line 553 | Line 594 | public class LinkedTransferQueueTest ext
594      public void testIterator() throws InterruptedException {
595          LinkedTransferQueue q = populatedQueue(SIZE);
596          Iterator it = q.iterator();
597 +        int i = 0;
598          while (it.hasNext()) {
599 <            assertEquals(it.next(), q.take());
599 >            assertEquals(it.next(), i++);
600          }
601 +        assertEquals(i, SIZE);
602      }
603  
604      /**
605 <     * iterator.remove removes current element
605 >     * iterator.remove() removes current element
606       */
607      public void testIteratorRemove() {
608          final LinkedTransferQueue q = new LinkedTransferQueue();
# Line 631 | Line 674 | public class LinkedTransferQueueTest ext
674          ExecutorService executor = Executors.newFixedThreadPool(2);
675  
676          executor.execute(new CheckedRunnable() {
677 <            void realRun() {
677 >            public void realRun() {
678                  threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS,
679                                           MILLISECONDS));
680              }});
681  
682          executor.execute(new CheckedRunnable() {
683 <            void realRun() throws InterruptedException {
683 >            public void realRun() throws InterruptedException {
684                  Thread.sleep(SMALL_DELAY_MS);
685                  threadAssertEquals(one, q.take());
686              }});
# Line 646 | Line 689 | public class LinkedTransferQueueTest ext
689      }
690  
691      /**
692 <     * poll retrieves elements across Executor threads
692 >     * timed poll retrieves elements across Executor threads
693       */
694      public void testPollInExecutor() {
695          final LinkedTransferQueue q = new LinkedTransferQueue();
696          ExecutorService executor = Executors.newFixedThreadPool(2);
697  
698          executor.execute(new CheckedRunnable() {
699 <            void realRun() throws InterruptedException {
699 >            public void realRun() throws InterruptedException {
700                  threadAssertNull(q.poll());
701                  threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS,
702                                                  MILLISECONDS));
# Line 661 | Line 704 | public class LinkedTransferQueueTest ext
704              }});
705  
706          executor.execute(new CheckedRunnable() {
707 <            void realRun() throws InterruptedException {
707 >            public void realRun() throws InterruptedException {
708                  Thread.sleep(SMALL_DELAY_MS);
709                  q.put(one);
710              }});
# Line 701 | Line 744 | public class LinkedTransferQueueTest ext
744          try {
745              q.drainTo(null);
746              shouldThrow();
747 <        } catch (NullPointerException success) {
705 <        }
747 >        } catch (NullPointerException success) {}
748      }
749  
750      /**
# Line 713 | Line 755 | public class LinkedTransferQueueTest ext
755          try {
756              q.drainTo(q);
757              shouldThrow();
758 <        } catch (IllegalArgumentException success) {
717 <        }
758 >        } catch (IllegalArgumentException success) {}
759      }
760  
761      /**
# Line 744 | Line 785 | public class LinkedTransferQueueTest ext
785      }
786  
787      /**
788 <     * drainTo empties full queue, unblocking a waiting put.
788 >     * drainTo(c) empties full queue, unblocking a waiting put.
789       */
790      public void testDrainToWithActivePut() throws InterruptedException {
791          final LinkedTransferQueue q = populatedQueue(SIZE);
792 <        Thread t = new Thread(new CheckedRunnable() {
793 <            void realRun() {
792 >        Thread t = newStartedThread(new CheckedRunnable() {
793 >            public void realRun() {
794                  q.put(SIZE + 1);
795              }});
755        t.start();
796          ArrayList l = new ArrayList();
797          q.drainTo(l);
798          assertTrue(l.size() >= SIZE);
# Line 769 | Line 809 | public class LinkedTransferQueueTest ext
809      public void testDrainToNullN() {
810          LinkedTransferQueue q = populatedQueue(SIZE);
811          try {
812 <            q.drainTo(null, 0);
812 >            q.drainTo(null, SIZE);
813              shouldThrow();
814 <        } catch (NullPointerException success) {
775 <        }
814 >        } catch (NullPointerException success) {}
815      }
816  
817      /**
# Line 781 | Line 820 | public class LinkedTransferQueueTest ext
820      public void testDrainToSelfN() {
821          LinkedTransferQueue q = populatedQueue(SIZE);
822          try {
823 <            q.drainTo(q, 0);
823 >            q.drainTo(q, SIZE);
824              shouldThrow();
825 <        } catch (IllegalArgumentException success) {
787 <        }
825 >        } catch (IllegalArgumentException success) {}
826      }
827  
828      /**
# Line 810 | Line 848 | public class LinkedTransferQueueTest ext
848      }
849  
850      /**
851 <     * poll and take decrement the waiting consumer count
851 >     * timed poll() or take() increments the waiting consumer count;
852 >     * offer(e) decrements the waiting consumer count
853       */
854      public void testWaitingConsumer() throws InterruptedException {
855          final LinkedTransferQueue q = new LinkedTransferQueue();
856 <        final ConsumerObserver waiting = new ConsumerObserver();
857 <        new Thread(new CheckedRunnable() {
858 <            void realRun() {
856 >        assertEquals(q.getWaitingConsumerCount(), 0);
857 >        assertFalse(q.hasWaitingConsumer());
858 >
859 >        Thread t = newStartedThread(new CheckedRunnable() {
860 >            public void realRun() throws InterruptedException {
861 >                Thread.sleep(SMALL_DELAY_MS);
862                  threadAssertTrue(q.hasWaitingConsumer());
863 <                waiting.setWaitingConsumer(q.getWaitingConsumerCount());
863 >                threadAssertEquals(q.getWaitingConsumerCount(), 1);
864                  threadAssertTrue(q.offer(new Object()));
865 <            }}).start();
865 >                threadAssertFalse(q.hasWaitingConsumer());
866 >                threadAssertEquals(q.getWaitingConsumerCount(), 0);
867 >            }});
868 >
869          assertTrue(q.poll(LONG_DELAY_MS, MILLISECONDS) != null);
870 <        assertTrue(q.getWaitingConsumerCount()
871 <                   < waiting.getWaitingConsumers());
870 >        assertEquals(q.getWaitingConsumerCount(), 0);
871 >        assertFalse(q.hasWaitingConsumer());
872 >        t.join();
873      }
874  
875      /**
# Line 834 | Line 880 | public class LinkedTransferQueueTest ext
880              LinkedTransferQueue q = new LinkedTransferQueue();
881              q.transfer(null);
882              shouldThrow();
883 <        } catch (NullPointerException ex) {
838 <        }
883 >        } catch (NullPointerException success) {}
884      }
885  
886      /**
# Line 846 | Line 891 | public class LinkedTransferQueueTest ext
891          final LinkedTransferQueue<Integer> q
892              = new LinkedTransferQueue<Integer>();
893  
894 <        new Thread(new CheckedRunnable() {
895 <            void realRun() throws InterruptedException {
894 >        Thread t = newStartedThread(new CheckedRunnable() {
895 >            public void realRun() throws InterruptedException {
896                  q.transfer(SIZE);
897                  threadAssertTrue(q.isEmpty());
898 <            }}).start();
898 >            }});
899  
900          Thread.sleep(SHORT_DELAY_MS);
901          assertEquals(1, q.size());
902          assertEquals(SIZE, (int) q.poll());
903          assertTrue(q.isEmpty());
904 +        t.join();
905      }
906  
907      /**
# Line 865 | Line 911 | public class LinkedTransferQueueTest ext
911          final LinkedTransferQueue<Integer> q
912              = new LinkedTransferQueue<Integer>();
913  
914 <        Thread first = new Thread(new CheckedRunnable() {
915 <            void realRun() throws InterruptedException {
914 >        Thread first = newStartedThread(new CheckedRunnable() {
915 >            public void realRun() throws InterruptedException {
916                  Integer i = SIZE + 1;
917                  q.transfer(i);
918                  threadAssertTrue(!q.contains(i));
919                  threadAssertEquals(1, q.size());
920              }});
875        first.start();
921  
922 <        Thread interruptedThread = new Thread(new CheckedInterruptedRunnable() {
923 <            void realRun() throws InterruptedException {
924 <                while (q.size() == 0)
925 <                    Thread.yield();
926 <                q.transfer(SIZE);
927 <            }});
928 <        interruptedThread.start();
922 >        Thread interruptedThread = newStartedThread(
923 >            new CheckedInterruptedRunnable() {
924 >                public void realRun() throws InterruptedException {
925 >                    while (q.size() == 0)
926 >                        Thread.yield();
927 >                    q.transfer(SIZE);
928 >                }});
929  
930          while (q.size() < 2)
931              Thread.yield();
# Line 900 | Line 945 | public class LinkedTransferQueueTest ext
945       */
946      public void testTransfer4() throws InterruptedException {
947          final LinkedTransferQueue q = new LinkedTransferQueue();
948 <        new Thread(new CheckedRunnable() {
949 <            void realRun() throws InterruptedException {
948 >
949 >        Thread t = newStartedThread(new CheckedRunnable() {
950 >            public void realRun() throws InterruptedException {
951                  q.transfer(four);
952                  threadAssertFalse(q.contains(four));
953                  threadAssertEquals(three, q.poll());
954 <            }}).start();
955 <        Thread.sleep(MEDIUM_DELAY_MS);
954 >            }});
955 >
956 >        Thread.sleep(SHORT_DELAY_MS);
957          assertTrue(q.offer(three));
958          assertEquals(four, q.poll());
959 +        t.join();
960      }
961  
962      /**
# Line 919 | Line 967 | public class LinkedTransferQueueTest ext
967          final LinkedTransferQueue<Integer> q
968              = new LinkedTransferQueue<Integer>();
969  
970 <        new Thread(new CheckedRunnable() {
971 <            void realRun() throws InterruptedException {
970 >        Thread t = newStartedThread(new CheckedRunnable() {
971 >            public void realRun() throws InterruptedException {
972                  q.transfer(SIZE);
973 <                threadAssertTrue(q.isEmpty());
974 <            }}).start();
973 >                checkEmpty(q);
974 >            }});
975  
976          Thread.sleep(SHORT_DELAY_MS);
977          assertEquals(SIZE, (int) q.take());
978 <        assertTrue(q.isEmpty());
978 >        checkEmpty(q);
979 >        t.join();
980      }
981  
982      /**
# Line 938 | Line 987 | public class LinkedTransferQueueTest ext
987              final LinkedTransferQueue q = new LinkedTransferQueue();
988              q.tryTransfer(null);
989              shouldThrow();
990 <        } catch (NullPointerException ex) {
942 <        }
990 >        } catch (NullPointerException success) {}
991      }
992  
993      /**
994       * tryTransfer returns false and does not enqueue if there are no
995       * consumers waiting to poll or take.
996       */
997 <    public void testTryTransfer2() {
997 >    public void testTryTransfer2() throws InterruptedException {
998          final LinkedTransferQueue q = new LinkedTransferQueue();
999          assertFalse(q.tryTransfer(new Object()));
1000          assertFalse(q.hasWaitingConsumer());
1001 <        assertTrue(q.isEmpty());
954 <        assertEquals(0, q.size());
1001 >        checkEmpty(q);
1002      }
1003  
1004      /**
# Line 961 | Line 1008 | public class LinkedTransferQueueTest ext
1008      public void testTryTransfer3() throws InterruptedException {
1009          final Object hotPotato = new Object();
1010          final LinkedTransferQueue q = new LinkedTransferQueue();
1011 <        new Thread(new CheckedRunnable() {
1012 <            void realRun() {
1011 >
1012 >        Thread t = newStartedThread(new CheckedRunnable() {
1013 >            public void realRun() {
1014                  while (! q.hasWaitingConsumer())
1015                      Thread.yield();
1016                  threadAssertTrue(q.hasWaitingConsumer());
1017                  threadAssertTrue(q.isEmpty());
1018                  threadAssertTrue(q.size() == 0);
1019                  threadAssertTrue(q.tryTransfer(hotPotato));
1020 <            }}).start();
1021 <        assertTrue(q.poll(LONG_DELAY_MS, MILLISECONDS) == hotPotato);
1022 <        assertTrue(q.isEmpty());
1020 >            }});
1021 >
1022 >        assertTrue(q.poll(MEDIUM_DELAY_MS, MILLISECONDS) == hotPotato);
1023 >        checkEmpty(q);
1024 >        t.join();
1025      }
1026  
1027      /**
# Line 981 | Line 1031 | public class LinkedTransferQueueTest ext
1031      public void testTryTransfer4() throws InterruptedException {
1032          final Object hotPotato = new Object();
1033          final LinkedTransferQueue q = new LinkedTransferQueue();
1034 <        new Thread(new CheckedRunnable() {
1035 <            void realRun() {
1034 >
1035 >        Thread t = newStartedThread(new CheckedRunnable() {
1036 >            public void realRun() {
1037                  while (! q.hasWaitingConsumer())
1038                      Thread.yield();
1039                  threadAssertTrue(q.hasWaitingConsumer());
1040                  threadAssertTrue(q.isEmpty());
1041                  threadAssertTrue(q.size() == 0);
1042                  threadAssertTrue(q.tryTransfer(hotPotato));
1043 <            }}).start();
1043 >            }});
1044 >
1045          assertTrue(q.take() == hotPotato);
1046 <        assertTrue(q.isEmpty());
1046 >        checkEmpty(q);
1047 >        t.join();
1048      }
1049  
1050      /**
# Line 1000 | Line 1053 | public class LinkedTransferQueueTest ext
1053       */
1054      public void testTryTransfer5() throws InterruptedException {
1055          final LinkedTransferQueue q = new LinkedTransferQueue();
1056 <        Thread toInterrupt = new Thread(new CheckedInterruptedRunnable() {
1057 <            void realRun() throws InterruptedException {
1056 >
1057 >        Thread toInterrupt = newStartedThread(new CheckedInterruptedRunnable() {
1058 >            public void realRun() throws InterruptedException {
1059                  q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS);
1060              }});
1061 <        toInterrupt.start();
1061 >
1062          Thread.sleep(SMALL_DELAY_MS);
1063          toInterrupt.interrupt();
1064 +        toInterrupt.join();
1065      }
1066  
1067      /**
# Line 1014 | Line 1069 | public class LinkedTransferQueueTest ext
1069       */
1070      public void testTryTransfer6() throws InterruptedException {
1071          final LinkedTransferQueue q = new LinkedTransferQueue();
1072 <        new Thread(new CheckedRunnable() {
1073 <            void realRun() throws InterruptedException {
1072 >
1073 >        Thread t = newStartedThread(new CheckedRunnable() {
1074 >            public void realRun() throws InterruptedException {
1075                  threadAssertFalse
1076                      (q.tryTransfer(new Object(),
1077                                     SHORT_DELAY_MS, MILLISECONDS));
1078 <            }}).start();
1079 <        Thread.sleep(LONG_DELAY_MS);
1080 <        assertTrue(q.isEmpty());
1078 >            }});
1079 >
1080 >        Thread.sleep(SMALL_DELAY_MS);
1081 >        checkEmpty(q);
1082 >        t.join();
1083      }
1084  
1085      /**
# Line 1031 | Line 1089 | public class LinkedTransferQueueTest ext
1089      public void testTryTransfer7() throws InterruptedException {
1090          final LinkedTransferQueue q = new LinkedTransferQueue();
1091          assertTrue(q.offer(four));
1092 <        new Thread(new CheckedRunnable() {
1093 <            void realRun() throws InterruptedException {
1092 >
1093 >        Thread t = newStartedThread(new CheckedRunnable() {
1094 >            public void realRun() throws InterruptedException {
1095                  threadAssertTrue(q.tryTransfer(five,
1096 <                                               LONG_DELAY_MS, MILLISECONDS));
1096 >                                               MEDIUM_DELAY_MS, MILLISECONDS));
1097                  threadAssertTrue(q.isEmpty());
1098 <            }}).start();
1098 >            }});
1099 >
1100          Thread.sleep(SHORT_DELAY_MS);
1101          assertEquals(2, q.size());
1102          assertEquals(four, q.poll());
1103          assertEquals(five, q.poll());
1104 <        assertTrue(q.isEmpty());
1104 >        checkEmpty(q);
1105 >        t.join();
1106      }
1107  
1108      /**
# Line 1055 | Line 1116 | public class LinkedTransferQueueTest ext
1116          assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS));
1117          assertEquals(1, q.size());
1118          assertEquals(four, q.poll());
1058        threadAssertTrue(q.isEmpty());
1119          assertNull(q.poll());
1120 +        checkEmpty(q);
1121      }
1122  
1123      private LinkedTransferQueue<Integer> populatedQueue(int n) {
# Line 1070 | Line 1131 | public class LinkedTransferQueueTest ext
1131          assertFalse(q.isEmpty());
1132          return q;
1133      }
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    }
1134   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines