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.17 by jsr166, Sat Nov 21 19:45:16 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() {
273 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
274              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() {
286 >        final LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
287 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
288              void realRun() throws InterruptedException {
268                LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
289                  for (int i = 0; i < SIZE; ++i) {
290                      threadAssertEquals(i, (int) q.take());
291                  }
292                  q.take();
293              }});
294 <        t.start();
275 <        Thread.sleep(SHORT_DELAY_MS);
294 >        Thread.sleep(SMALL_DELAY_MS);
295          t.interrupt();
296          t.join();
297 +        checkEmpty(q);
298      }
299  
300      /**
301       * poll succeeds unless empty
302       */
303 <    public void testPoll() {
303 >    public void testPoll() throws InterruptedException {
304          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
305          for (int i = 0; i < SIZE; ++i) {
306              assertEquals(i, (int) q.poll());
307          }
308          assertNull(q.poll());
309 +        checkEmpty(q);
310      }
311  
312      /**
# Line 297 | Line 318 | public class LinkedTransferQueueTest ext
318              assertEquals(i, (int) q.poll(0, MILLISECONDS));
319          }
320          assertNull(q.poll(0, MILLISECONDS));
321 +        checkEmpty(q);
322      }
323  
324      /**
# Line 305 | Line 327 | public class LinkedTransferQueueTest ext
327      public void testTimedPoll() throws InterruptedException {
328          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
329          for (int i = 0; i < SIZE; ++i) {
330 <            assertEquals(i, (int) q.poll(SHORT_DELAY_MS, MILLISECONDS));
330 >            long t0 = System.nanoTime();
331 >            assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
332 >            long millisElapsed = (System.nanoTime() - t0)/(1024 * 1024);
333 >            assertTrue(millisElapsed < SMALL_DELAY_MS);
334          }
335          assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
336 +        checkEmpty(q);
337      }
338  
339      /**
# Line 315 | Line 341 | public class LinkedTransferQueueTest ext
341       * returning timeout status
342       */
343      public void testInterruptedTimedPoll() throws InterruptedException {
344 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
344 >        final LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
345 >        Thread t = newStartedThread(new CheckedRunnable() {
346              void realRun() throws InterruptedException {
320                LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
347                  for (int i = 0; i < SIZE; ++i) {
348 <                    threadAssertEquals(i, (int) q.poll(SHORT_DELAY_MS,
348 >                    long t0 = System.nanoTime();
349 >                    threadAssertEquals(i, (int) q.poll(LONG_DELAY_MS,
350                                                         MILLISECONDS));
351 +                    long millisElapsed = (System.nanoTime() - t0)/(1024 * 1024);
352 +                    assertTrue(millisElapsed < SMALL_DELAY_MS);
353                  }
354 <                q.poll(SHORT_DELAY_MS, MILLISECONDS);
354 >                try {
355 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
356 >                    shouldThrow();
357 >                } catch (InterruptedException success) {}
358              }});
359 <        t.start();
360 <        Thread.sleep(SHORT_DELAY_MS);
359 >
360 >        Thread.sleep(SMALL_DELAY_MS);
361          t.interrupt();
362          t.join();
363 +        checkEmpty(q);
364      }
365  
366      /**
# Line 336 | Line 369 | public class LinkedTransferQueueTest ext
369       */
370      public void testTimedPollWithOffer() throws InterruptedException {
371          final LinkedTransferQueue q = new LinkedTransferQueue();
372 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
372 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
373              void realRun() throws InterruptedException {
374                  threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
375                  q.poll(LONG_DELAY_MS, MILLISECONDS);
376                  q.poll(LONG_DELAY_MS, MILLISECONDS);
377              }});
345        t.start();
378          Thread.sleep(SMALL_DELAY_MS);
379          assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
380          t.interrupt();
# Line 352 | Line 384 | public class LinkedTransferQueueTest ext
384      /**
385       * peek returns next element, or null if empty
386       */
387 <    public void testPeek() {
387 >    public void testPeek() throws InterruptedException {
388          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
389          for (int i = 0; i < SIZE; ++i) {
390              assertEquals(i, (int) q.peek());
# Line 361 | Line 393 | public class LinkedTransferQueueTest ext
393                         i != (int) q.peek());
394          }
395          assertNull(q.peek());
396 +        checkEmpty(q);
397      }
398  
399      /**
400       * element returns next element, or throws NoSuchElementException if empty
401       */
402 <    public void testElement() {
402 >    public void testElement() throws InterruptedException {
403          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
404          for (int i = 0; i < SIZE; ++i) {
405              assertEquals(i, (int) q.element());
# Line 375 | Line 408 | public class LinkedTransferQueueTest ext
408          try {
409              q.element();
410              shouldThrow();
411 <        } catch (NoSuchElementException success) {
412 <        }
411 >        } catch (NoSuchElementException success) {}
412 >        checkEmpty(q);
413      }
414  
415      /**
416       * remove removes next element, or throws NoSuchElementException if empty
417       */
418 <    public void testRemove() {
418 >    public void testRemove() throws InterruptedException {
419          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
420          for (int i = 0; i < SIZE; ++i) {
421              assertEquals(i, (int) q.remove());
# Line 390 | Line 423 | public class LinkedTransferQueueTest ext
423          try {
424              q.remove();
425              shouldThrow();
426 <        } catch (NoSuchElementException success) {
427 <        }
426 >        } catch (NoSuchElementException success) {}
427 >        checkEmpty(q);
428      }
429  
430      /**
431       * remove(x) removes x and returns true if present
432       */
433 <    public void testRemoveElement() {
433 >    public void testRemoveElement() throws InterruptedException {
434          LinkedTransferQueue q = populatedQueue(SIZE);
435          for (int i = 1; i < SIZE; i += 2) {
436              assertTrue(q.remove(i));
# Line 406 | Line 439 | public class LinkedTransferQueueTest ext
439              assertTrue(q.remove(i));
440              assertFalse(q.remove(i + 1));
441          }
442 <        assertTrue(q.isEmpty());
442 >        checkEmpty(q);
443      }
444  
445      /**
# Line 419 | Line 452 | public class LinkedTransferQueueTest ext
452          assertTrue(q.remove(one));
453          assertTrue(q.remove(two));
454          assertTrue(q.add(three));
455 <        assertTrue(q.take() != null);
455 >        assertTrue(q.take() == three);
456      }
457  
458      /**
# Line 437 | Line 470 | public class LinkedTransferQueueTest ext
470      /**
471       * clear removes all elements
472       */
473 <    public void testClear() {
473 >    public void testClear() throws InterruptedException {
474          LinkedTransferQueue q = populatedQueue(SIZE);
475          q.clear();
476 <        assertTrue(q.isEmpty());
444 <        assertEquals(0, q.size());
476 >        checkEmpty(q);
477          assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
478          q.add(one);
479          assertFalse(q.isEmpty());
480 +        assertEquals(1, q.size());
481          assertTrue(q.contains(one));
482          q.clear();
483 <        assertTrue(q.isEmpty());
483 >        checkEmpty(q);
484      }
485  
486      /**
# Line 501 | Line 534 | public class LinkedTransferQueueTest ext
534      }
535  
536      /**
537 <     * toArray contains all elements
537 >     * toArray() contains all elements
538       */
539      public void testToArray() throws InterruptedException {
540          LinkedTransferQueue q = populatedQueue(SIZE);
# Line 531 | Line 564 | public class LinkedTransferQueueTest ext
564              LinkedTransferQueue q = populatedQueue(SIZE);
565              Object o[] = q.toArray(null);
566              shouldThrow();
567 <        } catch (NullPointerException success) {
535 <        }
567 >        } catch (NullPointerException success) {}
568      }
569  
570      /**
571 <     * toArray with incompatible array type throws CCE
571 >     * toArray(incompatible array type) throws CCE
572       */
573      public void testToArray1_BadArg() {
574          try {
575              LinkedTransferQueue q = populatedQueue(SIZE);
576              Object o[] = q.toArray(new String[10]);
577              shouldThrow();
578 <        } catch (ArrayStoreException success) {
547 <        }
578 >        } catch (ArrayStoreException success) {}
579      }
580  
581      /**
# Line 553 | Line 584 | public class LinkedTransferQueueTest ext
584      public void testIterator() throws InterruptedException {
585          LinkedTransferQueue q = populatedQueue(SIZE);
586          Iterator it = q.iterator();
587 +        int i = 0;
588          while (it.hasNext()) {
589 <            assertEquals(it.next(), q.take());
589 >            assertEquals(it.next(), i++);
590          }
591 +        assertEquals(i, SIZE);
592      }
593  
594      /**
595 <     * iterator.remove removes current element
595 >     * iterator.remove() removes current element
596       */
597      public void testIteratorRemove() {
598          final LinkedTransferQueue q = new LinkedTransferQueue();
# Line 646 | Line 679 | public class LinkedTransferQueueTest ext
679      }
680  
681      /**
682 <     * poll retrieves elements across Executor threads
682 >     * timed poll retrieves elements across Executor threads
683       */
684      public void testPollInExecutor() {
685          final LinkedTransferQueue q = new LinkedTransferQueue();
# Line 701 | Line 734 | public class LinkedTransferQueueTest ext
734          try {
735              q.drainTo(null);
736              shouldThrow();
737 <        } catch (NullPointerException success) {
705 <        }
737 >        } catch (NullPointerException success) {}
738      }
739  
740      /**
# Line 713 | Line 745 | public class LinkedTransferQueueTest ext
745          try {
746              q.drainTo(q);
747              shouldThrow();
748 <        } catch (IllegalArgumentException success) {
717 <        }
748 >        } catch (IllegalArgumentException success) {}
749      }
750  
751      /**
# Line 744 | Line 775 | public class LinkedTransferQueueTest ext
775      }
776  
777      /**
778 <     * drainTo empties full queue, unblocking a waiting put.
778 >     * drainTo(c) empties full queue, unblocking a waiting put.
779       */
780      public void testDrainToWithActivePut() throws InterruptedException {
781          final LinkedTransferQueue q = populatedQueue(SIZE);
782 <        Thread t = new Thread(new CheckedRunnable() {
782 >        Thread t = newStartedThread(new CheckedRunnable() {
783              void realRun() {
784                  q.put(SIZE + 1);
785              }});
755        t.start();
786          ArrayList l = new ArrayList();
787          q.drainTo(l);
788          assertTrue(l.size() >= SIZE);
# Line 769 | Line 799 | public class LinkedTransferQueueTest ext
799      public void testDrainToNullN() {
800          LinkedTransferQueue q = populatedQueue(SIZE);
801          try {
802 <            q.drainTo(null, 0);
802 >            q.drainTo(null, SIZE);
803              shouldThrow();
804 <        } catch (NullPointerException success) {
775 <        }
804 >        } catch (NullPointerException success) {}
805      }
806  
807      /**
# Line 781 | Line 810 | public class LinkedTransferQueueTest ext
810      public void testDrainToSelfN() {
811          LinkedTransferQueue q = populatedQueue(SIZE);
812          try {
813 <            q.drainTo(q, 0);
813 >            q.drainTo(q, SIZE);
814              shouldThrow();
815 <        } catch (IllegalArgumentException success) {
787 <        }
815 >        } catch (IllegalArgumentException success) {}
816      }
817  
818      /**
# Line 810 | Line 838 | public class LinkedTransferQueueTest ext
838      }
839  
840      /**
841 <     * poll and take decrement the waiting consumer count
841 >     * timed poll() or take() increments the waiting consumer count;
842 >     * offer(e) decrements the waiting consumer count
843       */
844      public void testWaitingConsumer() throws InterruptedException {
845          final LinkedTransferQueue q = new LinkedTransferQueue();
846 <        final ConsumerObserver waiting = new ConsumerObserver();
847 <        new Thread(new CheckedRunnable() {
848 <            void realRun() {
846 >        assertEquals(q.getWaitingConsumerCount(), 0);
847 >        assertFalse(q.hasWaitingConsumer());
848 >
849 >        Thread t = newStartedThread(new CheckedRunnable() {
850 >            void realRun() throws InterruptedException {
851 >                Thread.sleep(SMALL_DELAY_MS);
852                  threadAssertTrue(q.hasWaitingConsumer());
853 <                waiting.setWaitingConsumer(q.getWaitingConsumerCount());
853 >                threadAssertEquals(q.getWaitingConsumerCount(), 1);
854                  threadAssertTrue(q.offer(new Object()));
855 <            }}).start();
855 >                threadAssertFalse(q.hasWaitingConsumer());
856 >                threadAssertEquals(q.getWaitingConsumerCount(), 0);
857 >            }});
858 >
859          assertTrue(q.poll(LONG_DELAY_MS, MILLISECONDS) != null);
860 <        assertTrue(q.getWaitingConsumerCount()
861 <                   < waiting.getWaitingConsumers());
860 >        assertEquals(q.getWaitingConsumerCount(), 0);
861 >        assertFalse(q.hasWaitingConsumer());
862 >        t.join();
863      }
864  
865      /**
# Line 834 | Line 870 | public class LinkedTransferQueueTest ext
870              LinkedTransferQueue q = new LinkedTransferQueue();
871              q.transfer(null);
872              shouldThrow();
873 <        } catch (NullPointerException ex) {
838 <        }
873 >        } catch (NullPointerException success) {}
874      }
875  
876      /**
# Line 846 | Line 881 | public class LinkedTransferQueueTest ext
881          final LinkedTransferQueue<Integer> q
882              = new LinkedTransferQueue<Integer>();
883  
884 <        new Thread(new CheckedRunnable() {
884 >        Thread t = newStartedThread(new CheckedRunnable() {
885              void realRun() throws InterruptedException {
886                  q.transfer(SIZE);
887                  threadAssertTrue(q.isEmpty());
888 <            }}).start();
888 >            }});
889  
890          Thread.sleep(SHORT_DELAY_MS);
891          assertEquals(1, q.size());
892          assertEquals(SIZE, (int) q.poll());
893          assertTrue(q.isEmpty());
894 +        t.join();
895      }
896  
897      /**
# Line 865 | Line 901 | public class LinkedTransferQueueTest ext
901          final LinkedTransferQueue<Integer> q
902              = new LinkedTransferQueue<Integer>();
903  
904 <        Thread first = new Thread(new CheckedRunnable() {
904 >        Thread first = newStartedThread(new CheckedRunnable() {
905              void realRun() throws InterruptedException {
906                  Integer i = SIZE + 1;
907                  q.transfer(i);
908                  threadAssertTrue(!q.contains(i));
909                  threadAssertEquals(1, q.size());
910              }});
875        first.start();
911  
912 <        Thread interruptedThread = new Thread(new CheckedInterruptedRunnable() {
913 <            void realRun() throws InterruptedException {
914 <                while (q.size() == 0)
915 <                    Thread.yield();
916 <                q.transfer(SIZE);
917 <            }});
918 <        interruptedThread.start();
912 >        Thread interruptedThread = newStartedThread(
913 >            new CheckedInterruptedRunnable() {
914 >                void realRun() throws InterruptedException {
915 >                    while (q.size() == 0)
916 >                        Thread.yield();
917 >                    q.transfer(SIZE);
918 >                }});
919  
920          while (q.size() < 2)
921              Thread.yield();
# Line 900 | Line 935 | public class LinkedTransferQueueTest ext
935       */
936      public void testTransfer4() throws InterruptedException {
937          final LinkedTransferQueue q = new LinkedTransferQueue();
938 <        new Thread(new CheckedRunnable() {
938 >
939 >        Thread t = newStartedThread(new CheckedRunnable() {
940              void realRun() throws InterruptedException {
941                  q.transfer(four);
942                  threadAssertFalse(q.contains(four));
943                  threadAssertEquals(three, q.poll());
944 <            }}).start();
945 <        Thread.sleep(MEDIUM_DELAY_MS);
944 >            }});
945 >
946 >        Thread.sleep(SHORT_DELAY_MS);
947          assertTrue(q.offer(three));
948          assertEquals(four, q.poll());
949 +        t.join();
950      }
951  
952      /**
# Line 919 | Line 957 | public class LinkedTransferQueueTest ext
957          final LinkedTransferQueue<Integer> q
958              = new LinkedTransferQueue<Integer>();
959  
960 <        new Thread(new CheckedRunnable() {
960 >        Thread t = newStartedThread(new CheckedRunnable() {
961              void realRun() throws InterruptedException {
962                  q.transfer(SIZE);
963 <                threadAssertTrue(q.isEmpty());
964 <            }}).start();
963 >                checkEmpty(q);
964 >            }});
965  
966          Thread.sleep(SHORT_DELAY_MS);
967          assertEquals(SIZE, (int) q.take());
968 <        assertTrue(q.isEmpty());
968 >        checkEmpty(q);
969 >        t.join();
970      }
971  
972      /**
# Line 937 | Line 976 | public class LinkedTransferQueueTest ext
976          try {
977              final LinkedTransferQueue q = new LinkedTransferQueue();
978              q.tryTransfer(null);
979 <            this.shouldThrow();
980 <        } catch (NullPointerException ex) {
942 <        }
979 >            shouldThrow();
980 >        } catch (NullPointerException success) {}
981      }
982  
983      /**
984       * tryTransfer returns false and does not enqueue if there are no
985       * consumers waiting to poll or take.
986       */
987 <    public void testTryTransfer2() {
987 >    public void testTryTransfer2() throws InterruptedException {
988          final LinkedTransferQueue q = new LinkedTransferQueue();
989          assertFalse(q.tryTransfer(new Object()));
990          assertFalse(q.hasWaitingConsumer());
991 <        assertTrue(q.isEmpty());
954 <        assertEquals(0, q.size());
991 >        checkEmpty(q);
992      }
993  
994      /**
# Line 961 | Line 998 | public class LinkedTransferQueueTest ext
998      public void testTryTransfer3() throws InterruptedException {
999          final Object hotPotato = new Object();
1000          final LinkedTransferQueue q = new LinkedTransferQueue();
1001 <        new Thread(new CheckedRunnable() {
1001 >
1002 >        Thread t = newStartedThread(new CheckedRunnable() {
1003              void realRun() {
1004                  while (! q.hasWaitingConsumer())
1005                      Thread.yield();
# Line 969 | Line 1007 | public class LinkedTransferQueueTest ext
1007                  threadAssertTrue(q.isEmpty());
1008                  threadAssertTrue(q.size() == 0);
1009                  threadAssertTrue(q.tryTransfer(hotPotato));
1010 <            }}).start();
1011 <        assertTrue(q.poll(LONG_DELAY_MS, MILLISECONDS) == hotPotato);
1012 <        assertTrue(q.isEmpty());
1010 >            }});
1011 >
1012 >        assertTrue(q.poll(MEDIUM_DELAY_MS, MILLISECONDS) == hotPotato);
1013 >        checkEmpty(q);
1014 >        t.join();
1015      }
1016  
1017      /**
# Line 981 | Line 1021 | public class LinkedTransferQueueTest ext
1021      public void testTryTransfer4() throws InterruptedException {
1022          final Object hotPotato = new Object();
1023          final LinkedTransferQueue q = new LinkedTransferQueue();
1024 <        new Thread(new CheckedRunnable() {
1024 >
1025 >        Thread t = newStartedThread(new CheckedRunnable() {
1026              void realRun() {
1027                  while (! q.hasWaitingConsumer())
1028                      Thread.yield();
# Line 989 | Line 1030 | public class LinkedTransferQueueTest ext
1030                  threadAssertTrue(q.isEmpty());
1031                  threadAssertTrue(q.size() == 0);
1032                  threadAssertTrue(q.tryTransfer(hotPotato));
1033 <            }}).start();
1033 >            }});
1034 >
1035          assertTrue(q.take() == hotPotato);
1036 <        assertTrue(q.isEmpty());
1036 >        checkEmpty(q);
1037 >        t.join();
1038      }
1039  
1040      /**
# Line 1000 | Line 1043 | public class LinkedTransferQueueTest ext
1043       */
1044      public void testTryTransfer5() throws InterruptedException {
1045          final LinkedTransferQueue q = new LinkedTransferQueue();
1046 <        Thread toInterrupt = new Thread(new CheckedInterruptedRunnable() {
1046 >
1047 >        Thread toInterrupt = newStartedThread(new CheckedInterruptedRunnable() {
1048              void realRun() throws InterruptedException {
1049                  q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS);
1050              }});
1051 <        toInterrupt.start();
1051 >
1052          Thread.sleep(SMALL_DELAY_MS);
1053          toInterrupt.interrupt();
1054 +        toInterrupt.join();
1055      }
1056  
1057      /**
# Line 1014 | Line 1059 | public class LinkedTransferQueueTest ext
1059       */
1060      public void testTryTransfer6() throws InterruptedException {
1061          final LinkedTransferQueue q = new LinkedTransferQueue();
1062 <        new Thread(new CheckedRunnable() {
1062 >
1063 >        Thread t = newStartedThread(new CheckedRunnable() {
1064              void realRun() throws InterruptedException {
1065                  threadAssertFalse
1066                      (q.tryTransfer(new Object(),
1067                                     SHORT_DELAY_MS, MILLISECONDS));
1068 <            }}).start();
1069 <        Thread.sleep(LONG_DELAY_MS);
1070 <        assertTrue(q.isEmpty());
1068 >            }});
1069 >
1070 >        Thread.sleep(SMALL_DELAY_MS);
1071 >        checkEmpty(q);
1072 >        t.join();
1073      }
1074  
1075      /**
# Line 1031 | Line 1079 | public class LinkedTransferQueueTest ext
1079      public void testTryTransfer7() throws InterruptedException {
1080          final LinkedTransferQueue q = new LinkedTransferQueue();
1081          assertTrue(q.offer(four));
1082 <        new Thread(new CheckedRunnable() {
1082 >
1083 >        Thread t = newStartedThread(new CheckedRunnable() {
1084              void realRun() throws InterruptedException {
1085                  threadAssertTrue(q.tryTransfer(five,
1086 <                                               LONG_DELAY_MS, MILLISECONDS));
1086 >                                               MEDIUM_DELAY_MS, MILLISECONDS));
1087                  threadAssertTrue(q.isEmpty());
1088 <            }}).start();
1088 >            }});
1089 >
1090          Thread.sleep(SHORT_DELAY_MS);
1091          assertEquals(2, q.size());
1092          assertEquals(four, q.poll());
1093          assertEquals(five, q.poll());
1094 <        assertTrue(q.isEmpty());
1094 >        checkEmpty(q);
1095 >        t.join();
1096      }
1097  
1098      /**
# Line 1055 | Line 1106 | public class LinkedTransferQueueTest ext
1106          assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS));
1107          assertEquals(1, q.size());
1108          assertEquals(four, q.poll());
1058        threadAssertTrue(q.isEmpty());
1109          assertNull(q.poll());
1110 +        checkEmpty(q);
1111      }
1112  
1113      private LinkedTransferQueue<Integer> populatedQueue(int n) {
# Line 1070 | Line 1121 | public class LinkedTransferQueueTest ext
1121          assertFalse(q.isEmpty());
1122          return q;
1123      }
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    }
1124   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines