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.12 by jsr166, Sat Aug 15 00:35:01 2009 UTC vs.
Revision 1.47 by jsr166, Mon May 30 22:43:20 2011 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   * Other contributors include John Vint
6   */
7  
# 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.Collection;
17   import java.util.Iterator;
18   import java.util.List;
19   import java.util.NoSuchElementException;
20 < import java.util.concurrent.*;
20 > import java.util.concurrent.BlockingQueue;
21 > import java.util.concurrent.CountDownLatch;
22 > import java.util.concurrent.Executors;
23 > import java.util.concurrent.ExecutorService;
24 > import java.util.concurrent.LinkedTransferQueue;
25   import static java.util.concurrent.TimeUnit.MILLISECONDS;
26 + import static java.util.concurrent.TimeUnit.NANOSECONDS;
27   import junit.framework.Test;
28   import junit.framework.TestSuite;
29  
30   @SuppressWarnings({"unchecked", "rawtypes"})
31   public class LinkedTransferQueueTest extends JSR166TestCase {
32  
33 +    public static class Generic extends BlockingQueueTest {
34 +        protected BlockingQueue emptyCollection() {
35 +            return new LinkedTransferQueue();
36 +        }
37 +    }
38 +
39      public static void main(String[] args) {
40          junit.textui.TestRunner.run(suite());
41      }
42  
43      public static Test suite() {
44 <        return new TestSuite(LinkedTransferQueueTest.class);
45 <    }
35 <
36 <    void checkEmpty(LinkedTransferQueue q) throws InterruptedException {
37 <        assertTrue(q.isEmpty());
38 <        assertEquals(0, q.size());
39 <        assertNull(q.peek());
40 <        assertNull(q.poll());
41 <        assertNull(q.poll(0, MILLISECONDS));
42 <        assertEquals(q.toString(), "[]");
43 <        assertTrue(Arrays.equals(q.toArray(), new Object[0]));
44 <        assertFalse(q.iterator().hasNext());
45 <        try {
46 <            q.element();
47 <            shouldThrow();
48 <        } catch (NoSuchElementException success) {
49 <        }
50 <        try {
51 <            q.iterator().next();
52 <            shouldThrow();
53 <        } catch (NoSuchElementException success) {
54 <        }
55 <        try {
56 <            q.remove();
57 <            shouldThrow();
58 <        } catch (NoSuchElementException success) {
59 <        }
44 >        return newTestSuite(LinkedTransferQueueTest.class,
45 >                            new Generic().testSuite());
46      }
47  
48      /**
# Line 76 | Line 62 | public class LinkedTransferQueueTest ext
62          try {
63              new LinkedTransferQueue(null);
64              shouldThrow();
65 <        } catch (NullPointerException success) {
80 <        }
65 >        } catch (NullPointerException success) {}
66      }
67  
68      /**
# Line 85 | Line 70 | public class LinkedTransferQueueTest ext
70       * NullPointerException
71       */
72      public void testConstructor3() {
73 +        Collection<Integer> elements = Arrays.asList(new Integer[SIZE]);
74          try {
75 <            Integer[] ints = new Integer[SIZE];
90 <            new LinkedTransferQueue(Arrays.asList(ints));
75 >            new LinkedTransferQueue(elements);
76              shouldThrow();
77 <        } catch (NullPointerException success) {
93 <        }
77 >        } catch (NullPointerException success) {}
78      }
79  
80      /**
# Line 98 | Line 82 | public class LinkedTransferQueueTest ext
82       * throws NullPointerException
83       */
84      public void testConstructor4() {
85 +        Integer[] ints = new Integer[SIZE];
86 +        for (int i = 0; i < SIZE-1; ++i)
87 +            ints[i] = i;
88 +        Collection<Integer> elements = Arrays.asList(ints);
89          try {
90 <            Integer[] ints = new Integer[SIZE];
103 <            for (int i = 0; i < SIZE - 1; ++i) {
104 <                ints[i] = i;
105 <            }
106 <            new LinkedTransferQueue(Arrays.asList(ints));
90 >            new LinkedTransferQueue(elements);
91              shouldThrow();
92 <        } catch (NullPointerException success) {
109 <        }
92 >        } catch (NullPointerException success) {}
93      }
94  
95      /**
# Line 151 | Line 134 | public class LinkedTransferQueueTest ext
134      }
135  
136      /**
154     * offer(null) throws NullPointerException
155     */
156    public void testOfferNull() {
157        try {
158            LinkedTransferQueue q = new LinkedTransferQueue();
159            q.offer(null);
160            shouldThrow();
161        } catch (NullPointerException success) {
162        }
163    }
164
165    /**
166     * add(null) throws NullPointerException
167     */
168    public void testAddNull() {
169        try {
170            LinkedTransferQueue q = new LinkedTransferQueue();
171            q.add(null);
172            shouldThrow();
173        } catch (NullPointerException success) {
174        }
175    }
176
177    /**
178     * addAll(null) throws NullPointerException
179     */
180    public void testAddAll1() {
181        try {
182            LinkedTransferQueue q = new LinkedTransferQueue();
183            q.addAll(null);
184            shouldThrow();
185        } catch (NullPointerException success) {
186        }
187    }
188
189    /**
137       * addAll(this) throws IllegalArgumentException
138       */
139      public void testAddAllSelf() {
# Line 194 | Line 141 | public class LinkedTransferQueueTest ext
141              LinkedTransferQueue q = populatedQueue(SIZE);
142              q.addAll(q);
143              shouldThrow();
144 <        } catch (IllegalArgumentException success) {
198 <        }
199 <    }
200 <
201 <    /**
202 <     * addAll of a collection with null elements throws NullPointerException
203 <     */
204 <    public void testAddAll2() {
205 <        try {
206 <            LinkedTransferQueue q = new LinkedTransferQueue();
207 <            Integer[] ints = new Integer[SIZE];
208 <            q.addAll(Arrays.asList(ints));
209 <            shouldThrow();
210 <        } catch (NullPointerException success) {
211 <        }
144 >        } catch (IllegalArgumentException success) {}
145      }
146  
147      /**
# Line 224 | Line 157 | public class LinkedTransferQueueTest ext
157              }
158              q.addAll(Arrays.asList(ints));
159              shouldThrow();
160 <        } catch (NullPointerException success) {
228 <        }
160 >        } catch (NullPointerException success) {}
161      }
162  
163      /**
# Line 246 | Line 178 | public class LinkedTransferQueueTest ext
178      }
179  
180      /**
249     * put(null) throws NullPointerException
250     */
251    public void testPutNull() throws InterruptedException {
252        try {
253            LinkedTransferQueue q = new LinkedTransferQueue();
254            q.put(null);
255            shouldThrow();
256        } catch (NullPointerException success) {}
257    }
258
259    /**
181       * all elements successfully put are contained
182       */
183      public void testPut() {
# Line 279 | Line 200 | public class LinkedTransferQueueTest ext
200      }
201  
202      /**
203 <     * take blocks interruptibly when empty
283 <     */
284 <    public void testTakeFromEmpty() throws InterruptedException {
285 <        final LinkedTransferQueue q = new LinkedTransferQueue();
286 <        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
287 <            void realRun() throws InterruptedException {
288 <                q.take();
289 <            }});
290 <        Thread.sleep(SHORT_DELAY_MS);
291 <        t.interrupt();
292 <        t.join();
293 <    }
294 <
295 <    /**
296 <     * Take removes existing elements until empty, then blocks interruptibly
203 >     * take removes existing elements until empty, then blocks interruptibly
204       */
205      public void testBlockingTake() throws InterruptedException {
206 <        final LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
207 <        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
208 <            void realRun() throws InterruptedException {
206 >        final BlockingQueue q = populatedQueue(SIZE);
207 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
208 >        Thread t = newStartedThread(new CheckedRunnable() {
209 >            public void realRun() throws InterruptedException {
210                  for (int i = 0; i < SIZE; ++i) {
211 <                    threadAssertEquals(i, (int) q.take());
211 >                    assertEquals(i, q.take());
212                  }
213 <                q.take();
213 >
214 >                Thread.currentThread().interrupt();
215 >                try {
216 >                    q.take();
217 >                    shouldThrow();
218 >                } catch (InterruptedException success) {}
219 >                assertFalse(Thread.interrupted());
220 >
221 >                pleaseInterrupt.countDown();
222 >                try {
223 >                    q.take();
224 >                    shouldThrow();
225 >                } catch (InterruptedException success) {}
226 >                assertFalse(Thread.interrupted());
227              }});
228 <        Thread.sleep(SMALL_DELAY_MS);
228 >
229 >        await(pleaseInterrupt);
230 >        assertThreadStaysAlive(t);
231          t.interrupt();
232 <        t.join();
310 <        checkEmpty(q);
232 >        awaitTermination(t);
233      }
234  
235      /**
# Line 323 | Line 245 | public class LinkedTransferQueueTest ext
245      }
246  
247      /**
248 <     * timed pool with zero timeout succeeds when non-empty, else times out
248 >     * timed poll with zero timeout succeeds when non-empty, else times out
249       */
250      public void testTimedPoll0() throws InterruptedException {
251          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
# Line 335 | Line 257 | public class LinkedTransferQueueTest ext
257      }
258  
259      /**
260 <     * timed pool with nonzero timeout succeeds when non-empty, else times out
260 >     * timed poll with nonzero timeout succeeds when non-empty, else times out
261       */
262      public void testTimedPoll() throws InterruptedException {
263          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
264          for (int i = 0; i < SIZE; ++i) {
265 <            long t0 = System.nanoTime();
265 >            long startTime = System.nanoTime();
266              assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
267 <            long millisElapsed = (System.nanoTime() - t0)/(1024 * 1024);
346 <            assertTrue(millisElapsed < SMALL_DELAY_MS);
267 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
268          }
269 <        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
269 >        long startTime = System.nanoTime();
270 >        assertNull(q.poll(timeoutMillis(), MILLISECONDS));
271 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
272          checkEmpty(q);
273      }
274  
# Line 354 | Line 277 | public class LinkedTransferQueueTest ext
277       * returning timeout status
278       */
279      public void testInterruptedTimedPoll() throws InterruptedException {
280 <        final LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
281 <        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
282 <            void realRun() throws InterruptedException {
280 >        final BlockingQueue<Integer> q = populatedQueue(SIZE);
281 >        final CountDownLatch aboutToWait = new CountDownLatch(1);
282 >        Thread t = newStartedThread(new CheckedRunnable() {
283 >            public void realRun() throws InterruptedException {
284                  for (int i = 0; i < SIZE; ++i) {
285                      long t0 = System.nanoTime();
286 <                    threadAssertEquals(i, (int) q.poll(LONG_DELAY_MS,
287 <                                                       MILLISECONDS));
288 <                    long millisElapsed = (System.nanoTime() - t0)/(1024 * 1024);
289 <                    assertTrue(millisElapsed < SMALL_DELAY_MS);
286 >                    assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
287 >                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
288 >                }
289 >                long t0 = System.nanoTime();
290 >                aboutToWait.countDown();
291 >                try {
292 >                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
293 >                    shouldThrow();
294 >                } catch (InterruptedException success) {
295 >                    assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
296                  }
367                q.poll(LONG_DELAY_MS, MILLISECONDS);
297              }});
298 <        Thread.sleep(SMALL_DELAY_MS);
298 >
299 >        aboutToWait.await();
300 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
301          t.interrupt();
302 <        t.join();
302 >        awaitTermination(t, MEDIUM_DELAY_MS);
303          checkEmpty(q);
304      }
305  
306      /**
307 <     * timed poll before a delayed offer fails; after offer succeeds;
308 <     * on interruption throws
307 >     * timed poll after thread interrupted throws InterruptedException
308 >     * instead of returning timeout status
309       */
310 <    public void testTimedPollWithOffer() throws InterruptedException {
311 <        final LinkedTransferQueue q = new LinkedTransferQueue();
312 <        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
313 <            void realRun() throws InterruptedException {
314 <                threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
315 <                q.poll(LONG_DELAY_MS, MILLISECONDS);
316 <                q.poll(LONG_DELAY_MS, MILLISECONDS);
310 >    public void testTimedPollAfterInterrupt() throws InterruptedException {
311 >        final BlockingQueue<Integer> q = populatedQueue(SIZE);
312 >        Thread t = newStartedThread(new CheckedRunnable() {
313 >            public void realRun() throws InterruptedException {
314 >                Thread.currentThread().interrupt();
315 >                for (int i = 0; i < SIZE; ++i) {
316 >                    long t0 = System.nanoTime();
317 >                    assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
318 >                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
319 >                }
320 >                try {
321 >                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
322 >                    shouldThrow();
323 >                } catch (InterruptedException success) {}
324              }});
325 <        Thread.sleep(SMALL_DELAY_MS);
326 <        assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
327 <        t.interrupt();
390 <        t.join();
325 >
326 >        awaitTermination(t, MEDIUM_DELAY_MS);
327 >        checkEmpty(q);
328      }
329  
330      /**
# Line 417 | Line 354 | public class LinkedTransferQueueTest ext
354          try {
355              q.element();
356              shouldThrow();
357 <        } catch (NoSuchElementException success) {
421 <        }
357 >        } catch (NoSuchElementException success) {}
358          checkEmpty(q);
359      }
360  
# Line 433 | Line 369 | public class LinkedTransferQueueTest ext
369          try {
370              q.remove();
371              shouldThrow();
372 <        } catch (NoSuchElementException success) {
437 <        }
372 >        } catch (NoSuchElementException success) {}
373          checkEmpty(q);
374      }
375  
# Line 443 | Line 378 | public class LinkedTransferQueueTest ext
378       */
379      public void testRemoveElement() throws InterruptedException {
380          LinkedTransferQueue q = populatedQueue(SIZE);
381 <        for (int i = 1; i < SIZE; i += 2) {
381 >        for (int i = 1; i < SIZE; i+=2) {
382 >            assertTrue(q.contains(i));
383              assertTrue(q.remove(i));
384 +            assertFalse(q.contains(i));
385 +            assertTrue(q.contains(i-1));
386          }
387 <        for (int i = 0; i < SIZE; i += 2) {
387 >        for (int i = 0; i < SIZE; i+=2) {
388 >            assertTrue(q.contains(i));
389              assertTrue(q.remove(i));
390 <            assertFalse(q.remove(i + 1));
390 >            assertFalse(q.contains(i));
391 >            assertFalse(q.remove(i+1));
392 >            assertFalse(q.contains(i+1));
393          }
394          checkEmpty(q);
395      }
# Line 463 | Line 404 | public class LinkedTransferQueueTest ext
404          assertTrue(q.remove(one));
405          assertTrue(q.remove(two));
406          assertTrue(q.add(three));
407 <        assertTrue(q.take() == three);
407 >        assertSame(q.take(), three);
408      }
409  
410      /**
# Line 545 | Line 486 | public class LinkedTransferQueueTest ext
486      }
487  
488      /**
489 <     * toArray contains all elements
489 >     * toArray() contains all elements in FIFO order
490       */
491 <    public void testToArray() throws InterruptedException {
491 >    public void testToArray() {
492          LinkedTransferQueue q = populatedQueue(SIZE);
493          Object[] o = q.toArray();
494          for (int i = 0; i < o.length; i++) {
495 <            assertEquals(o[i], q.take());
495 >            assertSame(o[i], q.poll());
496          }
497      }
498  
499      /**
500 <     * toArray(a) contains all elements
500 >     * toArray(a) contains all elements in FIFO order
501       */
502 <    public void testToArray2() throws InterruptedException {
502 >    public void testToArray2() {
503          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
504          Integer[] ints = new Integer[SIZE];
505 <        ints = q.toArray(ints);
505 >        Integer[] array = q.toArray(ints);
506 >        assertSame(ints, array);
507          for (int i = 0; i < ints.length; i++) {
508 <            assertEquals(ints[i], q.take());
508 >            assertSame(ints[i], q.poll());
509          }
510      }
511  
512      /**
513 <     * toArray(null) throws NullPointerException
572 <     */
573 <    public void testToArray_BadArg() {
574 <        try {
575 <            LinkedTransferQueue q = populatedQueue(SIZE);
576 <            Object o[] = q.toArray(null);
577 <            shouldThrow();
578 <        } catch (NullPointerException success) {
579 <        }
580 <    }
581 <
582 <    /**
583 <     * toArray with incompatible array type throws CCE
513 >     * toArray(incompatible array type) throws ArrayStoreException
514       */
515      public void testToArray1_BadArg() {
516 +        LinkedTransferQueue q = populatedQueue(SIZE);
517          try {
518 <            LinkedTransferQueue q = populatedQueue(SIZE);
588 <            Object o[] = q.toArray(new String[10]);
518 >            q.toArray(new String[10]);
519              shouldThrow();
520 <        } catch (ArrayStoreException success) {
591 <        }
520 >        } catch (ArrayStoreException success) {}
521      }
522  
523      /**
# Line 618 | Line 547 | public class LinkedTransferQueueTest ext
547          it.remove();
548  
549          it = q.iterator();
550 <        assertEquals(it.next(), one);
551 <        assertEquals(it.next(), three);
550 >        assertSame(it.next(), one);
551 >        assertSame(it.next(), three);
552          assertFalse(it.hasNext());
553      }
554  
# Line 663 | Line 592 | public class LinkedTransferQueueTest ext
592          LinkedTransferQueue q = populatedQueue(SIZE);
593          String s = q.toString();
594          for (int i = 0; i < SIZE; ++i) {
595 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
595 >            assertTrue(s.contains(String.valueOf(i)));
596          }
597      }
598  
# Line 672 | Line 601 | public class LinkedTransferQueueTest ext
601       */
602      public void testOfferInExecutor() {
603          final LinkedTransferQueue q = new LinkedTransferQueue();
604 <        q.add(one);
676 <        q.add(two);
604 >        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
605          ExecutorService executor = Executors.newFixedThreadPool(2);
606  
607          executor.execute(new CheckedRunnable() {
608 <            void realRun() {
609 <                threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS,
610 <                                         MILLISECONDS));
608 >            public void realRun() throws InterruptedException {
609 >                threadsStarted.await();
610 >                assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS));
611              }});
612  
613          executor.execute(new CheckedRunnable() {
614 <            void realRun() throws InterruptedException {
615 <                Thread.sleep(SMALL_DELAY_MS);
616 <                threadAssertEquals(one, q.take());
614 >            public void realRun() throws InterruptedException {
615 >                threadsStarted.await();
616 >                assertSame(one, q.take());
617 >                checkEmpty(q);
618              }});
619  
620          joinPool(executor);
621      }
622  
623      /**
624 <     * poll retrieves elements across Executor threads
624 >     * timed poll retrieves elements across Executor threads
625       */
626      public void testPollInExecutor() {
627          final LinkedTransferQueue q = new LinkedTransferQueue();
628 +        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
629          ExecutorService executor = Executors.newFixedThreadPool(2);
630  
631          executor.execute(new CheckedRunnable() {
632 <            void realRun() throws InterruptedException {
633 <                threadAssertNull(q.poll());
634 <                threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS,
635 <                                                MILLISECONDS));
636 <                threadAssertTrue(q.isEmpty());
632 >            public void realRun() throws InterruptedException {
633 >                assertNull(q.poll());
634 >                threadsStarted.await();
635 >                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
636 >                checkEmpty(q);
637              }});
638  
639          executor.execute(new CheckedRunnable() {
640 <            void realRun() throws InterruptedException {
641 <                Thread.sleep(SMALL_DELAY_MS);
640 >            public void realRun() throws InterruptedException {
641 >                threadsStarted.await();
642                  q.put(one);
643              }});
644  
# Line 734 | Line 664 | public class LinkedTransferQueueTest ext
664          LinkedTransferQueue r = (LinkedTransferQueue) in.readObject();
665  
666          assertEquals(q.size(), r.size());
667 +        assertEquals(q.toString(), r.toString());
668 +        assertTrue(Arrays.equals(q.toArray(), r.toArray()));
669          while (!q.isEmpty()) {
670              assertEquals(q.remove(), r.remove());
671          }
672      }
673  
674      /**
743     * drainTo(null) throws NullPointerException
744     */
745    public void testDrainToNull() {
746        LinkedTransferQueue q = populatedQueue(SIZE);
747        try {
748            q.drainTo(null);
749            shouldThrow();
750        } catch (NullPointerException success) {
751        }
752    }
753
754    /**
755     * drainTo(this) throws IllegalArgumentException
756     */
757    public void testDrainToSelf() {
758        LinkedTransferQueue q = populatedQueue(SIZE);
759        try {
760            q.drainTo(q);
761            shouldThrow();
762        } catch (IllegalArgumentException success) {
763        }
764    }
765
766    /**
675       * drainTo(c) empties queue into another collection c
676       */
677      public void testDrainTo() {
# Line 790 | Line 698 | public class LinkedTransferQueueTest ext
698      }
699  
700      /**
701 <     * drainTo empties full queue, unblocking a waiting put.
701 >     * drainTo(c) empties full queue, unblocking a waiting put.
702       */
703      public void testDrainToWithActivePut() throws InterruptedException {
704          final LinkedTransferQueue q = populatedQueue(SIZE);
705          Thread t = newStartedThread(new CheckedRunnable() {
706 <            void realRun() {
706 >            public void realRun() {
707                  q.put(SIZE + 1);
708              }});
709          ArrayList l = new ArrayList();
# Line 804 | Line 712 | public class LinkedTransferQueueTest ext
712          for (int i = 0; i < SIZE; ++i) {
713              assertEquals(l.get(i), i);
714          }
715 <        t.join();
715 >        awaitTermination(t, MEDIUM_DELAY_MS);
716          assertTrue(q.size() + l.size() >= SIZE);
717      }
718  
719      /**
720 <     * drainTo(null, n) throws NullPointerException
813 <     */
814 <    public void testDrainToNullN() {
815 <        LinkedTransferQueue q = populatedQueue(SIZE);
816 <        try {
817 <            q.drainTo(null, SIZE);
818 <            shouldThrow();
819 <        } catch (NullPointerException success) {
820 <        }
821 <    }
822 <
823 <    /**
824 <     * drainTo(this, n) throws IllegalArgumentException
825 <     */
826 <    public void testDrainToSelfN() {
827 <        LinkedTransferQueue q = populatedQueue(SIZE);
828 <        try {
829 <            q.drainTo(q, SIZE);
830 <            shouldThrow();
831 <        } catch (IllegalArgumentException success) {
832 <        }
833 <    }
834 <
835 <    /**
836 <     * drainTo(c, n) empties first max {n, size} elements of queue into c
720 >     * drainTo(c, n) empties first min(n, size) elements of queue into c
721       */
722      public void testDrainToN() {
723          LinkedTransferQueue q = new LinkedTransferQueue();
# Line 855 | Line 739 | public class LinkedTransferQueueTest ext
739      }
740  
741      /**
742 <     * poll and take decrement the waiting consumer count
742 >     * timed poll() or take() increments the waiting consumer count;
743 >     * offer(e) decrements the waiting consumer count
744       */
745      public void testWaitingConsumer() throws InterruptedException {
746          final LinkedTransferQueue q = new LinkedTransferQueue();
747 <        final ConsumerObserver waiting = new ConsumerObserver();
747 >        assertEquals(q.getWaitingConsumerCount(), 0);
748 >        assertFalse(q.hasWaitingConsumer());
749 >        final CountDownLatch threadStarted = new CountDownLatch(1);
750  
751          Thread t = newStartedThread(new CheckedRunnable() {
752 <            void realRun() throws InterruptedException {
753 <                Thread.sleep(SMALL_DELAY_MS);
754 <                threadAssertTrue(q.hasWaitingConsumer());
755 <                waiting.setWaitingConsumer(q.getWaitingConsumerCount());
756 <                threadAssertTrue(q.offer(new Object()));
752 >            public void realRun() throws InterruptedException {
753 >                threadStarted.countDown();
754 >                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
755 >                assertEquals(q.getWaitingConsumerCount(), 0);
756 >                assertFalse(q.hasWaitingConsumer());
757              }});
758  
759 <        assertTrue(q.poll(LONG_DELAY_MS, MILLISECONDS) != null);
760 <        assertTrue(q.getWaitingConsumerCount()
761 <                   < waiting.getWaitingConsumers());
762 <        t.join();
759 >        threadStarted.await();
760 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
761 >        assertEquals(q.getWaitingConsumerCount(), 1);
762 >        assertTrue(q.hasWaitingConsumer());
763 >
764 >        assertTrue(q.offer(one));
765 >        assertEquals(q.getWaitingConsumerCount(), 0);
766 >        assertFalse(q.hasWaitingConsumer());
767 >
768 >        awaitTermination(t, MEDIUM_DELAY_MS);
769      }
770  
771      /**
# Line 883 | Line 776 | public class LinkedTransferQueueTest ext
776              LinkedTransferQueue q = new LinkedTransferQueue();
777              q.transfer(null);
778              shouldThrow();
779 <        } catch (NullPointerException ex) {
887 <        }
779 >        } catch (NullPointerException success) {}
780      }
781  
782      /**
# Line 894 | Line 786 | public class LinkedTransferQueueTest ext
786      public void testTransfer2() throws InterruptedException {
787          final LinkedTransferQueue<Integer> q
788              = new LinkedTransferQueue<Integer>();
789 +        final CountDownLatch threadStarted = new CountDownLatch(1);
790  
791          Thread t = newStartedThread(new CheckedRunnable() {
792 <            void realRun() throws InterruptedException {
793 <                q.transfer(SIZE);
794 <                threadAssertTrue(q.isEmpty());
792 >            public void realRun() throws InterruptedException {
793 >                threadStarted.countDown();
794 >                q.transfer(five);
795 >                checkEmpty(q);
796              }});
797  
798 <        Thread.sleep(SHORT_DELAY_MS);
798 >        threadStarted.await();
799 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
800          assertEquals(1, q.size());
801 <        assertEquals(SIZE, (int) q.poll());
802 <        assertTrue(q.isEmpty());
803 <        t.join();
801 >        assertSame(five, q.poll());
802 >        checkEmpty(q);
803 >        awaitTermination(t, MEDIUM_DELAY_MS);
804      }
805  
806      /**
# Line 916 | Line 811 | public class LinkedTransferQueueTest ext
811              = new LinkedTransferQueue<Integer>();
812  
813          Thread first = newStartedThread(new CheckedRunnable() {
814 <            void realRun() throws InterruptedException {
815 <                Integer i = SIZE + 1;
816 <                q.transfer(i);
817 <                threadAssertTrue(!q.contains(i));
923 <                threadAssertEquals(1, q.size());
814 >            public void realRun() throws InterruptedException {
815 >                q.transfer(four);
816 >                assertTrue(!q.contains(four));
817 >                assertEquals(1, q.size());
818              }});
819  
820          Thread interruptedThread = newStartedThread(
821              new CheckedInterruptedRunnable() {
822 <                void realRun() throws InterruptedException {
823 <                    while (q.size() == 0)
822 >                public void realRun() throws InterruptedException {
823 >                    while (q.isEmpty())
824                          Thread.yield();
825 <                    q.transfer(SIZE);
825 >                    q.transfer(five);
826                  }});
827  
828          while (q.size() < 2)
829              Thread.yield();
830          assertEquals(2, q.size());
831 <        assertEquals(SIZE + 1, (int) q.poll());
831 >        assertSame(four, q.poll());
832          first.join();
833          assertEquals(1, q.size());
834          interruptedThread.interrupt();
835          interruptedThread.join();
836 <        assertEquals(0, q.size());
943 <        assertTrue(q.isEmpty());
836 >        checkEmpty(q);
837      }
838  
839      /**
# Line 951 | Line 844 | public class LinkedTransferQueueTest ext
844          final LinkedTransferQueue q = new LinkedTransferQueue();
845  
846          Thread t = newStartedThread(new CheckedRunnable() {
847 <            void realRun() throws InterruptedException {
847 >            public void realRun() throws InterruptedException {
848                  q.transfer(four);
849 <                threadAssertFalse(q.contains(four));
850 <                threadAssertEquals(three, q.poll());
849 >                assertFalse(q.contains(four));
850 >                assertSame(three, q.poll());
851              }});
852  
853 <        Thread.sleep(SHORT_DELAY_MS);
853 >        while (q.isEmpty())
854 >            Thread.yield();
855 >        assertFalse(q.isEmpty());
856 >        assertEquals(1, q.size());
857          assertTrue(q.offer(three));
858 <        assertEquals(four, q.poll());
859 <        t.join();
858 >        assertSame(four, q.poll());
859 >        awaitTermination(t, MEDIUM_DELAY_MS);
860      }
861  
862      /**
# Line 972 | Line 868 | public class LinkedTransferQueueTest ext
868              = new LinkedTransferQueue<Integer>();
869  
870          Thread t = newStartedThread(new CheckedRunnable() {
871 <            void realRun() throws InterruptedException {
872 <                q.transfer(SIZE);
871 >            public void realRun() throws InterruptedException {
872 >                q.transfer(four);
873                  checkEmpty(q);
874              }});
875  
876 <        Thread.sleep(SHORT_DELAY_MS);
877 <        assertEquals(SIZE, (int) q.take());
876 >        while (q.isEmpty())
877 >            Thread.yield();
878 >        assertFalse(q.isEmpty());
879 >        assertEquals(1, q.size());
880 >        assertSame(four, q.take());
881          checkEmpty(q);
882 <        t.join();
882 >        awaitTermination(t, MEDIUM_DELAY_MS);
883      }
884  
885      /**
# Line 991 | Line 890 | public class LinkedTransferQueueTest ext
890              final LinkedTransferQueue q = new LinkedTransferQueue();
891              q.tryTransfer(null);
892              shouldThrow();
893 <        } catch (NullPointerException ex) {
995 <        }
893 >        } catch (NullPointerException success) {}
894      }
895  
896      /**
# Line 1015 | Line 913 | public class LinkedTransferQueueTest ext
913          final LinkedTransferQueue q = new LinkedTransferQueue();
914  
915          Thread t = newStartedThread(new CheckedRunnable() {
916 <            void realRun() {
916 >            public void realRun() {
917                  while (! q.hasWaitingConsumer())
918                      Thread.yield();
919 <                threadAssertTrue(q.hasWaitingConsumer());
920 <                threadAssertTrue(q.isEmpty());
921 <                threadAssertTrue(q.size() == 0);
1024 <                threadAssertTrue(q.tryTransfer(hotPotato));
919 >                assertTrue(q.hasWaitingConsumer());
920 >                checkEmpty(q);
921 >                assertTrue(q.tryTransfer(hotPotato));
922              }});
923  
924 <        assertTrue(q.poll(MEDIUM_DELAY_MS, MILLISECONDS) == hotPotato);
924 >        assertSame(hotPotato, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
925          checkEmpty(q);
926 <        t.join();
926 >        awaitTermination(t, MEDIUM_DELAY_MS);
927      }
928  
929      /**
# Line 1038 | Line 935 | public class LinkedTransferQueueTest ext
935          final LinkedTransferQueue q = new LinkedTransferQueue();
936  
937          Thread t = newStartedThread(new CheckedRunnable() {
938 <            void realRun() {
938 >            public void realRun() {
939                  while (! q.hasWaitingConsumer())
940                      Thread.yield();
941 <                threadAssertTrue(q.hasWaitingConsumer());
942 <                threadAssertTrue(q.isEmpty());
943 <                threadAssertTrue(q.size() == 0);
1047 <                threadAssertTrue(q.tryTransfer(hotPotato));
941 >                assertTrue(q.hasWaitingConsumer());
942 >                checkEmpty(q);
943 >                assertTrue(q.tryTransfer(hotPotato));
944              }});
945  
946 <        assertTrue(q.take() == hotPotato);
946 >        assertSame(q.take(), hotPotato);
947          checkEmpty(q);
948 <        t.join();
948 >        awaitTermination(t, MEDIUM_DELAY_MS);
949      }
950  
951      /**
952 <     * tryTransfer waits the amount given if interrupted, and
1057 <     * throws interrupted exception
952 >     * tryTransfer blocks interruptibly if no takers
953       */
954      public void testTryTransfer5() throws InterruptedException {
955          final LinkedTransferQueue q = new LinkedTransferQueue();
956 +        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
957 +        assertTrue(q.isEmpty());
958  
959 <        Thread toInterrupt = newStartedThread(new CheckedInterruptedRunnable() {
960 <            void realRun() throws InterruptedException {
961 <                q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS);
959 >        Thread t = newStartedThread(new CheckedRunnable() {
960 >            public void realRun() throws InterruptedException {
961 >                Thread.currentThread().interrupt();
962 >                try {
963 >                    q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS);
964 >                    shouldThrow();
965 >                } catch (InterruptedException success) {}
966 >                assertFalse(Thread.interrupted());
967 >
968 >                pleaseInterrupt.countDown();
969 >                try {
970 >                    q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS);
971 >                    shouldThrow();
972 >                } catch (InterruptedException success) {}
973 >                assertFalse(Thread.interrupted());
974              }});
975  
976 <        Thread.sleep(SMALL_DELAY_MS);
977 <        toInterrupt.interrupt();
978 <        toInterrupt.join();
976 >        await(pleaseInterrupt);
977 >        assertThreadStaysAlive(t);
978 >        t.interrupt();
979 >        awaitTermination(t);
980 >        checkEmpty(q);
981      }
982  
983      /**
984 <     * tryTransfer gives up after the timeout and return false
984 >     * tryTransfer gives up after the timeout and returns false
985       */
986      public void testTryTransfer6() throws InterruptedException {
987          final LinkedTransferQueue q = new LinkedTransferQueue();
988  
989          Thread t = newStartedThread(new CheckedRunnable() {
990 <            void realRun() throws InterruptedException {
991 <                threadAssertFalse
992 <                    (q.tryTransfer(new Object(),
993 <                                   SHORT_DELAY_MS, MILLISECONDS));
990 >            public void realRun() throws InterruptedException {
991 >                long t0 = System.nanoTime();
992 >                assertFalse(q.tryTransfer(new Object(),
993 >                                          timeoutMillis(), MILLISECONDS));
994 >                assertTrue(millisElapsedSince(t0) >= timeoutMillis());
995 >                checkEmpty(q);
996              }});
997  
998 <        Thread.sleep(SMALL_DELAY_MS);
998 >        awaitTermination(t);
999          checkEmpty(q);
1087        t.join();
1000      }
1001  
1002      /**
# Line 1096 | Line 1008 | public class LinkedTransferQueueTest ext
1008          assertTrue(q.offer(four));
1009  
1010          Thread t = newStartedThread(new CheckedRunnable() {
1011 <            void realRun() throws InterruptedException {
1012 <                threadAssertTrue(q.tryTransfer(five,
1013 <                                               MEDIUM_DELAY_MS, MILLISECONDS));
1102 <                threadAssertTrue(q.isEmpty());
1011 >            public void realRun() throws InterruptedException {
1012 >                assertTrue(q.tryTransfer(five, MEDIUM_DELAY_MS, MILLISECONDS));
1013 >                checkEmpty(q);
1014              }});
1015  
1016 <        Thread.sleep(SHORT_DELAY_MS);
1016 >        while (q.size() != 2)
1017 >            Thread.yield();
1018          assertEquals(2, q.size());
1019 <        assertEquals(four, q.poll());
1020 <        assertEquals(five, q.poll());
1019 >        assertSame(four, q.poll());
1020 >        assertSame(five, q.poll());
1021          checkEmpty(q);
1022 <        t.join();
1022 >        awaitTermination(t, MEDIUM_DELAY_MS);
1023      }
1024  
1025      /**
1026 <     * tryTransfer attempts to enqueue into the q and fails returning
1027 <     * false not enqueueing and the successive poll is null
1026 >     * tryTransfer attempts to enqueue into the queue and fails
1027 >     * returning false not enqueueing and the successive poll is null
1028       */
1029      public void testTryTransfer8() throws InterruptedException {
1030          final LinkedTransferQueue q = new LinkedTransferQueue();
1031          assertTrue(q.offer(four));
1032          assertEquals(1, q.size());
1033 <        assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS));
1033 >        long t0 = System.nanoTime();
1034 >        assertFalse(q.tryTransfer(five, timeoutMillis(), MILLISECONDS));
1035 >        assertTrue(millisElapsedSince(t0) >= timeoutMillis());
1036          assertEquals(1, q.size());
1037 <        assertEquals(four, q.poll());
1124 <        checkEmpty(q);
1037 >        assertSame(four, q.poll());
1038          assertNull(q.poll());
1039 +        checkEmpty(q);
1040      }
1041  
1042      private LinkedTransferQueue<Integer> populatedQueue(int n) {
1043          LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
1044 <        assertTrue(q.isEmpty());
1044 >        checkEmpty(q);
1045          for (int i = 0; i < n; i++) {
1046              assertEquals(i, q.size());
1047              assertTrue(q.offer(i));
# Line 1136 | Line 1050 | public class LinkedTransferQueueTest ext
1050          assertFalse(q.isEmpty());
1051          return q;
1052      }
1139
1140    private static class ConsumerObserver {
1141
1142        private int waitingConsumers;
1143
1144        private ConsumerObserver() {
1145        }
1146
1147        private void setWaitingConsumer(int i) {
1148            this.waitingConsumers = i;
1149        }
1150
1151        private int getWaitingConsumers() {
1152            return waitingConsumers;
1153        }
1154    }
1053   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines