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.14 by jsr166, Sat Aug 15 03:20:36 2009 UTC vs.
Revision 1.39 by jsr166, Fri Nov 5 00:17:22 2010 UTC

# Line 18 | Line 18 | import java.util.List;
18   import java.util.NoSuchElementException;
19   import java.util.concurrent.*;
20   import static java.util.concurrent.TimeUnit.MILLISECONDS;
21 + import static java.util.concurrent.TimeUnit.NANOSECONDS;
22   import junit.framework.Test;
23   import junit.framework.TestSuite;
24  
25   @SuppressWarnings({"unchecked", "rawtypes"})
26   public class LinkedTransferQueueTest extends JSR166TestCase {
27  
28 +    public static class Generic extends BlockingQueueTest {
29 +        protected BlockingQueue emptyCollection() {
30 +            return new LinkedTransferQueue();
31 +        }
32 +    }
33 +
34      public static void main(String[] args) {
35          junit.textui.TestRunner.run(suite());
36      }
37  
38      public static Test suite() {
39 <        return new TestSuite(LinkedTransferQueueTest.class);
39 >        return newTestSuite(LinkedTransferQueueTest.class,
40 >                            new Generic().testSuite());
41      }
42  
43 <    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());
43 >    void checkEmpty(BlockingQueue q) {
44          try {
45 <            q.element();
46 <            shouldThrow();
47 <        } catch (NoSuchElementException success) {
48 <        }
49 <        try {
50 <            q.iterator().next();
51 <            shouldThrow();
52 <        } catch (NoSuchElementException success) {
53 <        }
54 <        try {
55 <            q.remove();
56 <            shouldThrow();
57 <        } catch (NoSuchElementException success) {
45 >            assertTrue(q.isEmpty());
46 >            assertEquals(0, q.size());
47 >            assertNull(q.peek());
48 >            assertNull(q.poll());
49 >            assertNull(q.poll(0, MILLISECONDS));
50 >            assertEquals(q.toString(), "[]");
51 >            assertTrue(Arrays.equals(q.toArray(), new Object[0]));
52 >            assertFalse(q.iterator().hasNext());
53 >            try {
54 >                q.element();
55 >                shouldThrow();
56 >            } catch (NoSuchElementException success) {}
57 >            try {
58 >                q.iterator().next();
59 >                shouldThrow();
60 >            } catch (NoSuchElementException success) {}
61 >            try {
62 >                q.remove();
63 >                shouldThrow();
64 >            } catch (NoSuchElementException success) {}
65 >        } catch (InterruptedException ie) {
66 >            threadUnexpectedException(ie);
67          }
68      }
69  
# Line 75 | Line 84 | public class LinkedTransferQueueTest ext
84          try {
85              new LinkedTransferQueue(null);
86              shouldThrow();
87 <        } catch (NullPointerException success) {
79 <        }
87 >        } catch (NullPointerException success) {}
88      }
89  
90      /**
# Line 88 | Line 96 | public class LinkedTransferQueueTest ext
96              Integer[] ints = new Integer[SIZE];
97              new LinkedTransferQueue(Arrays.asList(ints));
98              shouldThrow();
99 <        } catch (NullPointerException success) {
92 <        }
99 >        } catch (NullPointerException success) {}
100      }
101  
102      /**
# Line 104 | Line 111 | public class LinkedTransferQueueTest ext
111              }
112              new LinkedTransferQueue(Arrays.asList(ints));
113              shouldThrow();
114 <        } catch (NullPointerException success) {
108 <        }
114 >        } catch (NullPointerException success) {}
115      }
116  
117      /**
# Line 157 | Line 163 | public class LinkedTransferQueueTest ext
163              LinkedTransferQueue q = new LinkedTransferQueue();
164              q.offer(null);
165              shouldThrow();
166 <        } catch (NullPointerException success) {
161 <        }
166 >        } catch (NullPointerException success) {}
167      }
168  
169      /**
# Line 169 | Line 174 | public class LinkedTransferQueueTest ext
174              LinkedTransferQueue q = new LinkedTransferQueue();
175              q.add(null);
176              shouldThrow();
177 <        } catch (NullPointerException success) {
173 <        }
177 >        } catch (NullPointerException success) {}
178      }
179  
180      /**
# Line 181 | Line 185 | public class LinkedTransferQueueTest ext
185              LinkedTransferQueue q = new LinkedTransferQueue();
186              q.addAll(null);
187              shouldThrow();
188 <        } catch (NullPointerException success) {
185 <        }
188 >        } catch (NullPointerException success) {}
189      }
190  
191      /**
# Line 193 | Line 196 | public class LinkedTransferQueueTest ext
196              LinkedTransferQueue q = populatedQueue(SIZE);
197              q.addAll(q);
198              shouldThrow();
199 <        } catch (IllegalArgumentException success) {
197 <        }
199 >        } catch (IllegalArgumentException success) {}
200      }
201  
202      /**
# Line 206 | Line 208 | public class LinkedTransferQueueTest ext
208              Integer[] ints = new Integer[SIZE];
209              q.addAll(Arrays.asList(ints));
210              shouldThrow();
211 <        } catch (NullPointerException success) {
210 <        }
211 >        } catch (NullPointerException success) {}
212      }
213  
214      /**
# Line 223 | Line 224 | public class LinkedTransferQueueTest ext
224              }
225              q.addAll(Arrays.asList(ints));
226              shouldThrow();
227 <        } catch (NullPointerException success) {
227 <        }
227 >        } catch (NullPointerException success) {}
228      }
229  
230      /**
# Line 278 | Line 278 | public class LinkedTransferQueueTest ext
278      }
279  
280      /**
281 <     * take blocks interruptibly when empty
282 <     */
283 <    public void testTakeFromEmpty() throws InterruptedException {
284 <        final LinkedTransferQueue q = new LinkedTransferQueue();
285 <        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
286 <            void realRun() throws InterruptedException {
287 <                q.take();
288 <            }});
289 <        Thread.sleep(SHORT_DELAY_MS);
290 <        t.interrupt();
291 <        t.join();
292 <    }
293 <
294 <    /**
295 <     * Take removes existing elements until empty, then blocks interruptibly
281 >     * take removes existing elements until empty, then blocks interruptibly
282       */
283      public void testBlockingTake() throws InterruptedException {
284 <        final LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
285 <        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
286 <            void realRun() throws InterruptedException {
284 >        final BlockingQueue<Integer> q = populatedQueue(SIZE);
285 >        final CountDownLatch aboutToWait = new CountDownLatch(1);
286 >        Thread t = newStartedThread(new CheckedRunnable() {
287 >            public void realRun() throws InterruptedException {
288                  for (int i = 0; i < SIZE; ++i) {
289 <                    threadAssertEquals(i, (int) q.take());
289 >                    assertEquals(i, (int) q.take());
290                  }
291 <                q.take();
291 >                aboutToWait.countDown();
292 >                try {
293 >                    q.take();
294 >                    shouldThrow();
295 >                } catch (InterruptedException success) {}
296              }});
297 <        Thread.sleep(SMALL_DELAY_MS);
297 >
298 >        aboutToWait.await();
299 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
300          t.interrupt();
301 <        t.join();
301 >        awaitTermination(t, MEDIUM_DELAY_MS);
302          checkEmpty(q);
303      }
304  
# Line 322 | Line 315 | public class LinkedTransferQueueTest ext
315      }
316  
317      /**
318 <     * timed pool with zero timeout succeeds when non-empty, else times out
318 >     * timed poll with zero timeout succeeds when non-empty, else times out
319       */
320      public void testTimedPoll0() throws InterruptedException {
321          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
# Line 334 | Line 327 | public class LinkedTransferQueueTest ext
327      }
328  
329      /**
330 <     * timed pool with nonzero timeout succeeds when non-empty, else times out
330 >     * timed poll with nonzero timeout succeeds when non-empty, else times out
331       */
332      public void testTimedPoll() throws InterruptedException {
333          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
334          for (int i = 0; i < SIZE; ++i) {
335              long t0 = System.nanoTime();
336 <            assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
337 <            long millisElapsed = (System.nanoTime() - t0)/(1024 * 1024);
345 <            assertTrue(millisElapsed < SMALL_DELAY_MS);
336 >            assertEquals(i, (int) q.poll(SMALL_DELAY_MS, MILLISECONDS));
337 >            assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
338          }
339 +        long t0 = System.nanoTime();
340          assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
341 +        assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
342          checkEmpty(q);
343      }
344  
# Line 353 | Line 347 | public class LinkedTransferQueueTest ext
347       * returning timeout status
348       */
349      public void testInterruptedTimedPoll() throws InterruptedException {
350 <        final LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
351 <        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
352 <            void realRun() throws InterruptedException {
350 >        final BlockingQueue<Integer> q = populatedQueue(SIZE);
351 >        final CountDownLatch aboutToWait = new CountDownLatch(1);
352 >        Thread t = newStartedThread(new CheckedRunnable() {
353 >            public void realRun() throws InterruptedException {
354                  for (int i = 0; i < SIZE; ++i) {
355                      long t0 = System.nanoTime();
356 <                    threadAssertEquals(i, (int) q.poll(LONG_DELAY_MS,
357 <                                                       MILLISECONDS));
358 <                    long millisElapsed = (System.nanoTime() - t0)/(1024 * 1024);
359 <                    assertTrue(millisElapsed < SMALL_DELAY_MS);
356 >                    assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
357 >                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
358 >                }
359 >                long t0 = System.nanoTime();
360 >                aboutToWait.countDown();
361 >                try {
362 >                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
363 >                    shouldThrow();
364 >                } catch (InterruptedException success) {
365 >                    assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
366                  }
366                q.poll(LONG_DELAY_MS, MILLISECONDS);
367              }});
368 <        Thread.sleep(SMALL_DELAY_MS);
368 >
369 >        aboutToWait.await();
370 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
371          t.interrupt();
372 <        t.join();
372 >        awaitTermination(t, MEDIUM_DELAY_MS);
373          checkEmpty(q);
374      }
375  
376      /**
377 <     * timed poll before a delayed offer fails; after offer succeeds;
378 <     * on interruption throws
377 >     * timed poll after thread interrupted throws InterruptedException
378 >     * instead of returning timeout status
379       */
380 <    public void testTimedPollWithOffer() throws InterruptedException {
381 <        final LinkedTransferQueue q = new LinkedTransferQueue();
382 <        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
383 <            void realRun() throws InterruptedException {
384 <                threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
385 <                q.poll(LONG_DELAY_MS, MILLISECONDS);
386 <                q.poll(LONG_DELAY_MS, MILLISECONDS);
380 >    public void testTimedPollAfterInterrupt() throws InterruptedException {
381 >        final BlockingQueue<Integer> q = populatedQueue(SIZE);
382 >        Thread t = newStartedThread(new CheckedRunnable() {
383 >            public void realRun() throws InterruptedException {
384 >                Thread.currentThread().interrupt();
385 >                for (int i = 0; i < SIZE; ++i) {
386 >                    long t0 = System.nanoTime();
387 >                    assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
388 >                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
389 >                }
390 >                try {
391 >                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
392 >                    shouldThrow();
393 >                } catch (InterruptedException success) {}
394              }});
395 <        Thread.sleep(SMALL_DELAY_MS);
396 <        assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
397 <        t.interrupt();
389 <        t.join();
395 >
396 >        awaitTermination(t, MEDIUM_DELAY_MS);
397 >        checkEmpty(q);
398      }
399  
400      /**
# Line 416 | Line 424 | public class LinkedTransferQueueTest ext
424          try {
425              q.element();
426              shouldThrow();
427 <        } catch (NoSuchElementException success) {
420 <        }
427 >        } catch (NoSuchElementException success) {}
428          checkEmpty(q);
429      }
430  
# Line 432 | Line 439 | public class LinkedTransferQueueTest ext
439          try {
440              q.remove();
441              shouldThrow();
442 <        } catch (NoSuchElementException success) {
436 <        }
442 >        } catch (NoSuchElementException success) {}
443          checkEmpty(q);
444      }
445  
# Line 462 | Line 468 | public class LinkedTransferQueueTest ext
468          assertTrue(q.remove(one));
469          assertTrue(q.remove(two));
470          assertTrue(q.add(three));
471 <        assertTrue(q.take() == three);
471 >        assertSame(q.take(), three);
472      }
473  
474      /**
# Line 544 | Line 550 | public class LinkedTransferQueueTest ext
550      }
551  
552      /**
553 <     * toArray() contains all elements
553 >     * toArray() contains all elements in FIFO order
554       */
555 <    public void testToArray() throws InterruptedException {
555 >    public void testToArray() {
556          LinkedTransferQueue q = populatedQueue(SIZE);
557          Object[] o = q.toArray();
558          for (int i = 0; i < o.length; i++) {
559 <            assertEquals(o[i], q.take());
559 >            assertSame(o[i], q.poll());
560          }
561      }
562  
563      /**
564 <     * toArray(a) contains all elements
564 >     * toArray(a) contains all elements in FIFO order
565       */
566 <    public void testToArray2() throws InterruptedException {
566 >    public void testToArray2() {
567          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
568          Integer[] ints = new Integer[SIZE];
569 <        ints = q.toArray(ints);
569 >        Integer[] array = q.toArray(ints);
570 >        assertSame(ints, array);
571          for (int i = 0; i < ints.length; i++) {
572 <            assertEquals(ints[i], q.take());
572 >            assertSame(ints[i], q.poll());
573          }
574      }
575  
576      /**
577       * toArray(null) throws NullPointerException
578       */
579 <    public void testToArray_BadArg() {
579 >    public void testToArray_NullArg() {
580 >        LinkedTransferQueue q = populatedQueue(SIZE);
581          try {
582 <            LinkedTransferQueue q = populatedQueue(SIZE);
575 <            Object o[] = q.toArray(null);
582 >            q.toArray(null);
583              shouldThrow();
584 <        } catch (NullPointerException success) {
578 <        }
584 >        } catch (NullPointerException success) {}
585      }
586  
587      /**
588 <     * toArray(incompatible array type) throws CCE
588 >     * toArray(incompatible array type) throws ArrayStoreException
589       */
590      public void testToArray1_BadArg() {
591 +        LinkedTransferQueue q = populatedQueue(SIZE);
592          try {
593 <            LinkedTransferQueue q = populatedQueue(SIZE);
587 <            Object o[] = q.toArray(new String[10]);
593 >            q.toArray(new String[10]);
594              shouldThrow();
595 <        } catch (ArrayStoreException success) {
590 <        }
595 >        } catch (ArrayStoreException success) {}
596      }
597  
598      /**
# Line 617 | Line 622 | public class LinkedTransferQueueTest ext
622          it.remove();
623  
624          it = q.iterator();
625 <        assertEquals(it.next(), one);
626 <        assertEquals(it.next(), three);
625 >        assertSame(it.next(), one);
626 >        assertSame(it.next(), three);
627          assertFalse(it.hasNext());
628      }
629  
# Line 671 | Line 676 | public class LinkedTransferQueueTest ext
676       */
677      public void testOfferInExecutor() {
678          final LinkedTransferQueue q = new LinkedTransferQueue();
679 <        q.add(one);
675 <        q.add(two);
679 >        final CountDownLatch threadsStarted = new CountDownLatch(2);
680          ExecutorService executor = Executors.newFixedThreadPool(2);
681  
682          executor.execute(new CheckedRunnable() {
683 <            void realRun() {
684 <                threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS,
685 <                                         MILLISECONDS));
683 >            public void realRun() throws InterruptedException {
684 >                threadsStarted.countDown();
685 >                threadsStarted.await();
686 >                assertTrue(q.offer(one, MEDIUM_DELAY_MS, MILLISECONDS));
687              }});
688  
689          executor.execute(new CheckedRunnable() {
690 <            void realRun() throws InterruptedException {
691 <                Thread.sleep(SMALL_DELAY_MS);
692 <                threadAssertEquals(one, q.take());
690 >            public void realRun() throws InterruptedException {
691 >                threadsStarted.countDown();
692 >                threadsStarted.await();
693 >                assertSame(one, q.take());
694 >                checkEmpty(q);
695              }});
696  
697          joinPool(executor);
# Line 695 | Line 702 | public class LinkedTransferQueueTest ext
702       */
703      public void testPollInExecutor() {
704          final LinkedTransferQueue q = new LinkedTransferQueue();
705 +        final CountDownLatch threadsStarted = new CountDownLatch(2);
706          ExecutorService executor = Executors.newFixedThreadPool(2);
707  
708          executor.execute(new CheckedRunnable() {
709 <            void realRun() throws InterruptedException {
710 <                threadAssertNull(q.poll());
711 <                threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS,
712 <                                                MILLISECONDS));
713 <                threadAssertTrue(q.isEmpty());
709 >            public void realRun() throws InterruptedException {
710 >                assertNull(q.poll());
711 >                threadsStarted.countDown();
712 >                threadsStarted.await();
713 >                assertSame(one, q.poll(SMALL_DELAY_MS, MILLISECONDS));
714 >                checkEmpty(q);
715              }});
716  
717          executor.execute(new CheckedRunnable() {
718 <            void realRun() throws InterruptedException {
719 <                Thread.sleep(SMALL_DELAY_MS);
718 >            public void realRun() throws InterruptedException {
719 >                threadsStarted.countDown();
720 >                threadsStarted.await();
721                  q.put(one);
722              }});
723  
# Line 733 | Line 743 | public class LinkedTransferQueueTest ext
743          LinkedTransferQueue r = (LinkedTransferQueue) in.readObject();
744  
745          assertEquals(q.size(), r.size());
746 +        assertEquals(q.toString(), r.toString());
747 +        assertTrue(Arrays.equals(q.toArray(), r.toArray()));
748          while (!q.isEmpty()) {
749              assertEquals(q.remove(), r.remove());
750          }
# Line 746 | Line 758 | public class LinkedTransferQueueTest ext
758          try {
759              q.drainTo(null);
760              shouldThrow();
761 <        } catch (NullPointerException success) {
750 <        }
761 >        } catch (NullPointerException success) {}
762      }
763  
764      /**
# Line 758 | Line 769 | public class LinkedTransferQueueTest ext
769          try {
770              q.drainTo(q);
771              shouldThrow();
772 <        } catch (IllegalArgumentException success) {
762 <        }
772 >        } catch (IllegalArgumentException success) {}
773      }
774  
775      /**
# Line 794 | Line 804 | public class LinkedTransferQueueTest ext
804      public void testDrainToWithActivePut() throws InterruptedException {
805          final LinkedTransferQueue q = populatedQueue(SIZE);
806          Thread t = newStartedThread(new CheckedRunnable() {
807 <            void realRun() {
807 >            public void realRun() {
808                  q.put(SIZE + 1);
809              }});
810          ArrayList l = new ArrayList();
# Line 803 | Line 813 | public class LinkedTransferQueueTest ext
813          for (int i = 0; i < SIZE; ++i) {
814              assertEquals(l.get(i), i);
815          }
816 <        t.join();
816 >        awaitTermination(t, MEDIUM_DELAY_MS);
817          assertTrue(q.size() + l.size() >= SIZE);
818      }
819  
# Line 815 | Line 825 | public class LinkedTransferQueueTest ext
825          try {
826              q.drainTo(null, SIZE);
827              shouldThrow();
828 <        } catch (NullPointerException success) {
819 <        }
828 >        } catch (NullPointerException success) {}
829      }
830  
831      /**
# Line 827 | Line 836 | public class LinkedTransferQueueTest ext
836          try {
837              q.drainTo(q, SIZE);
838              shouldThrow();
839 <        } catch (IllegalArgumentException success) {
831 <        }
839 >        } catch (IllegalArgumentException success) {}
840      }
841  
842      /**
843 <     * drainTo(c, n) empties first max {n, size} elements of queue into c
843 >     * drainTo(c, n) empties first min(n, size) elements of queue into c
844       */
845      public void testDrainToN() {
846          LinkedTransferQueue q = new LinkedTransferQueue();
# Line 861 | Line 869 | public class LinkedTransferQueueTest ext
869          final LinkedTransferQueue q = new LinkedTransferQueue();
870          assertEquals(q.getWaitingConsumerCount(), 0);
871          assertFalse(q.hasWaitingConsumer());
872 +        final CountDownLatch threadStarted = new CountDownLatch(1);
873  
874          Thread t = newStartedThread(new CheckedRunnable() {
875 <            void realRun() throws InterruptedException {
876 <                Thread.sleep(SMALL_DELAY_MS);
877 <                threadAssertTrue(q.hasWaitingConsumer());
878 <                threadAssertEquals(q.getWaitingConsumerCount(), 1);
879 <                threadAssertTrue(q.offer(new Object()));
871 <                threadAssertFalse(q.hasWaitingConsumer());
872 <                threadAssertEquals(q.getWaitingConsumerCount(), 0);
875 >            public void realRun() throws InterruptedException {
876 >                threadStarted.countDown();
877 >                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
878 >                assertEquals(q.getWaitingConsumerCount(), 0);
879 >                assertFalse(q.hasWaitingConsumer());
880              }});
881  
882 <        assertTrue(q.poll(LONG_DELAY_MS, MILLISECONDS) != null);
882 >        threadStarted.await();
883 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
884 >        assertEquals(q.getWaitingConsumerCount(), 1);
885 >        assertTrue(q.hasWaitingConsumer());
886 >
887 >        assertTrue(q.offer(one));
888          assertEquals(q.getWaitingConsumerCount(), 0);
889          assertFalse(q.hasWaitingConsumer());
890 <        t.join();
890 >
891 >        awaitTermination(t, MEDIUM_DELAY_MS);
892      }
893  
894      /**
# Line 886 | Line 899 | public class LinkedTransferQueueTest ext
899              LinkedTransferQueue q = new LinkedTransferQueue();
900              q.transfer(null);
901              shouldThrow();
902 <        } catch (NullPointerException ex) {
890 <        }
902 >        } catch (NullPointerException success) {}
903      }
904  
905      /**
# Line 897 | Line 909 | public class LinkedTransferQueueTest ext
909      public void testTransfer2() throws InterruptedException {
910          final LinkedTransferQueue<Integer> q
911              = new LinkedTransferQueue<Integer>();
912 +        final CountDownLatch threadStarted = new CountDownLatch(1);
913  
914          Thread t = newStartedThread(new CheckedRunnable() {
915 <            void realRun() throws InterruptedException {
916 <                q.transfer(SIZE);
917 <                threadAssertTrue(q.isEmpty());
915 >            public void realRun() throws InterruptedException {
916 >                threadStarted.countDown();
917 >                q.transfer(five);
918 >                checkEmpty(q);
919              }});
920  
921 <        Thread.sleep(SHORT_DELAY_MS);
921 >        threadStarted.await();
922 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
923          assertEquals(1, q.size());
924 <        assertEquals(SIZE, (int) q.poll());
925 <        assertTrue(q.isEmpty());
926 <        t.join();
924 >        assertSame(five, q.poll());
925 >        checkEmpty(q);
926 >        awaitTermination(t, MEDIUM_DELAY_MS);
927      }
928  
929      /**
# Line 919 | Line 934 | public class LinkedTransferQueueTest ext
934              = new LinkedTransferQueue<Integer>();
935  
936          Thread first = newStartedThread(new CheckedRunnable() {
937 <            void realRun() throws InterruptedException {
938 <                Integer i = SIZE + 1;
939 <                q.transfer(i);
940 <                threadAssertTrue(!q.contains(i));
926 <                threadAssertEquals(1, q.size());
937 >            public void realRun() throws InterruptedException {
938 >                q.transfer(four);
939 >                assertTrue(!q.contains(four));
940 >                assertEquals(1, q.size());
941              }});
942  
943          Thread interruptedThread = newStartedThread(
944              new CheckedInterruptedRunnable() {
945 <                void realRun() throws InterruptedException {
946 <                    while (q.size() == 0)
945 >                public void realRun() throws InterruptedException {
946 >                    while (q.isEmpty())
947                          Thread.yield();
948 <                    q.transfer(SIZE);
948 >                    q.transfer(five);
949                  }});
950  
951          while (q.size() < 2)
952              Thread.yield();
953          assertEquals(2, q.size());
954 <        assertEquals(SIZE + 1, (int) q.poll());
954 >        assertSame(four, q.poll());
955          first.join();
956          assertEquals(1, q.size());
957          interruptedThread.interrupt();
958          interruptedThread.join();
959 <        assertEquals(0, q.size());
946 <        assertTrue(q.isEmpty());
959 >        checkEmpty(q);
960      }
961  
962      /**
# Line 954 | Line 967 | public class LinkedTransferQueueTest ext
967          final LinkedTransferQueue q = new LinkedTransferQueue();
968  
969          Thread t = newStartedThread(new CheckedRunnable() {
970 <            void realRun() throws InterruptedException {
970 >            public void realRun() throws InterruptedException {
971                  q.transfer(four);
972 <                threadAssertFalse(q.contains(four));
973 <                threadAssertEquals(three, q.poll());
972 >                assertFalse(q.contains(four));
973 >                assertSame(three, q.poll());
974              }});
975  
976 <        Thread.sleep(SHORT_DELAY_MS);
976 >        while (q.isEmpty())
977 >            Thread.yield();
978 >        assertFalse(q.isEmpty());
979 >        assertEquals(1, q.size());
980          assertTrue(q.offer(three));
981 <        assertEquals(four, q.poll());
982 <        t.join();
981 >        assertSame(four, q.poll());
982 >        awaitTermination(t, MEDIUM_DELAY_MS);
983      }
984  
985      /**
# Line 975 | Line 991 | public class LinkedTransferQueueTest ext
991              = new LinkedTransferQueue<Integer>();
992  
993          Thread t = newStartedThread(new CheckedRunnable() {
994 <            void realRun() throws InterruptedException {
995 <                q.transfer(SIZE);
994 >            public void realRun() throws InterruptedException {
995 >                q.transfer(four);
996                  checkEmpty(q);
997              }});
998  
999 <        Thread.sleep(SHORT_DELAY_MS);
1000 <        assertEquals(SIZE, (int) q.take());
999 >        while (q.isEmpty())
1000 >            Thread.yield();
1001 >        assertFalse(q.isEmpty());
1002 >        assertEquals(1, q.size());
1003 >        assertSame(four, q.take());
1004          checkEmpty(q);
1005 <        t.join();
1005 >        awaitTermination(t, MEDIUM_DELAY_MS);
1006      }
1007  
1008      /**
# Line 994 | Line 1013 | public class LinkedTransferQueueTest ext
1013              final LinkedTransferQueue q = new LinkedTransferQueue();
1014              q.tryTransfer(null);
1015              shouldThrow();
1016 <        } catch (NullPointerException ex) {
998 <        }
1016 >        } catch (NullPointerException success) {}
1017      }
1018  
1019      /**
# Line 1018 | Line 1036 | public class LinkedTransferQueueTest ext
1036          final LinkedTransferQueue q = new LinkedTransferQueue();
1037  
1038          Thread t = newStartedThread(new CheckedRunnable() {
1039 <            void realRun() {
1039 >            public void realRun() {
1040                  while (! q.hasWaitingConsumer())
1041                      Thread.yield();
1042 <                threadAssertTrue(q.hasWaitingConsumer());
1043 <                threadAssertTrue(q.isEmpty());
1044 <                threadAssertTrue(q.size() == 0);
1027 <                threadAssertTrue(q.tryTransfer(hotPotato));
1042 >                assertTrue(q.hasWaitingConsumer());
1043 >                checkEmpty(q);
1044 >                assertTrue(q.tryTransfer(hotPotato));
1045              }});
1046  
1047 <        assertTrue(q.poll(MEDIUM_DELAY_MS, MILLISECONDS) == hotPotato);
1047 >        assertSame(hotPotato, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
1048          checkEmpty(q);
1049 <        t.join();
1049 >        awaitTermination(t, MEDIUM_DELAY_MS);
1050      }
1051  
1052      /**
# Line 1041 | Line 1058 | public class LinkedTransferQueueTest ext
1058          final LinkedTransferQueue q = new LinkedTransferQueue();
1059  
1060          Thread t = newStartedThread(new CheckedRunnable() {
1061 <            void realRun() {
1061 >            public void realRun() {
1062                  while (! q.hasWaitingConsumer())
1063                      Thread.yield();
1064 <                threadAssertTrue(q.hasWaitingConsumer());
1065 <                threadAssertTrue(q.isEmpty());
1066 <                threadAssertTrue(q.size() == 0);
1050 <                threadAssertTrue(q.tryTransfer(hotPotato));
1064 >                assertTrue(q.hasWaitingConsumer());
1065 >                checkEmpty(q);
1066 >                assertTrue(q.tryTransfer(hotPotato));
1067              }});
1068  
1069 <        assertTrue(q.take() == hotPotato);
1069 >        assertSame(q.take(), hotPotato);
1070          checkEmpty(q);
1071 <        t.join();
1071 >        awaitTermination(t, MEDIUM_DELAY_MS);
1072      }
1073  
1074      /**
# Line 1061 | Line 1077 | public class LinkedTransferQueueTest ext
1077       */
1078      public void testTryTransfer5() throws InterruptedException {
1079          final LinkedTransferQueue q = new LinkedTransferQueue();
1080 +        final CountDownLatch threadStarted = new CountDownLatch(1);
1081  
1082 <        Thread toInterrupt = newStartedThread(new CheckedInterruptedRunnable() {
1083 <            void realRun() throws InterruptedException {
1084 <                q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS);
1082 >        Thread t = newStartedThread(new CheckedRunnable() {
1083 >            public void realRun() throws InterruptedException {
1084 >                long t0 = System.nanoTime();
1085 >                threadStarted.countDown();
1086 >                try {
1087 >                    q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS);
1088 >                    shouldThrow();
1089 >                } catch (InterruptedException success) {}
1090 >                assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
1091              }});
1092  
1093 <        Thread.sleep(SMALL_DELAY_MS);
1094 <        toInterrupt.interrupt();
1095 <        toInterrupt.join();
1093 >        threadStarted.await();
1094 >        Thread.sleep(SHORT_DELAY_MS);
1095 >        t.interrupt();
1096 >        awaitTermination(t, MEDIUM_DELAY_MS);
1097 >        checkEmpty(q);
1098      }
1099  
1100      /**
1101 <     * tryTransfer gives up after the timeout and return false
1101 >     * tryTransfer gives up after the timeout and returns false
1102       */
1103      public void testTryTransfer6() throws InterruptedException {
1104          final LinkedTransferQueue q = new LinkedTransferQueue();
1105  
1106          Thread t = newStartedThread(new CheckedRunnable() {
1107 <            void realRun() throws InterruptedException {
1108 <                threadAssertFalse
1109 <                    (q.tryTransfer(new Object(),
1110 <                                   SHORT_DELAY_MS, MILLISECONDS));
1107 >            public void realRun() throws InterruptedException {
1108 >                long t0 = System.nanoTime();
1109 >                assertFalse(q.tryTransfer(new Object(),
1110 >                                          SHORT_DELAY_MS, MILLISECONDS));
1111 >                assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
1112 >                checkEmpty(q);
1113              }});
1114  
1115 <        Thread.sleep(SMALL_DELAY_MS);
1115 >        awaitTermination(t, MEDIUM_DELAY_MS);
1116          checkEmpty(q);
1090        t.join();
1117      }
1118  
1119      /**
# Line 1099 | Line 1125 | public class LinkedTransferQueueTest ext
1125          assertTrue(q.offer(four));
1126  
1127          Thread t = newStartedThread(new CheckedRunnable() {
1128 <            void realRun() throws InterruptedException {
1129 <                threadAssertTrue(q.tryTransfer(five,
1130 <                                               MEDIUM_DELAY_MS, MILLISECONDS));
1105 <                threadAssertTrue(q.isEmpty());
1128 >            public void realRun() throws InterruptedException {
1129 >                assertTrue(q.tryTransfer(five, MEDIUM_DELAY_MS, MILLISECONDS));
1130 >                checkEmpty(q);
1131              }});
1132  
1133 <        Thread.sleep(SHORT_DELAY_MS);
1133 >        while (q.size() != 2)
1134 >            Thread.yield();
1135          assertEquals(2, q.size());
1136 <        assertEquals(four, q.poll());
1137 <        assertEquals(five, q.poll());
1136 >        assertSame(four, q.poll());
1137 >        assertSame(five, q.poll());
1138          checkEmpty(q);
1139 <        t.join();
1139 >        awaitTermination(t, MEDIUM_DELAY_MS);
1140      }
1141  
1142      /**
# Line 1121 | Line 1147 | public class LinkedTransferQueueTest ext
1147          final LinkedTransferQueue q = new LinkedTransferQueue();
1148          assertTrue(q.offer(four));
1149          assertEquals(1, q.size());
1150 +        long t0 = System.nanoTime();
1151          assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS));
1152 +        assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
1153          assertEquals(1, q.size());
1154 <        assertEquals(four, q.poll());
1154 >        assertSame(four, q.poll());
1155          assertNull(q.poll());
1156          checkEmpty(q);
1157      }
1158  
1159      private LinkedTransferQueue<Integer> populatedQueue(int n) {
1160          LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
1161 <        assertTrue(q.isEmpty());
1161 >        checkEmpty(q);
1162          for (int i = 0; i < n; i++) {
1163              assertEquals(i, q.size());
1164              assertTrue(q.offer(i));

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines