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.10 by jsr166, Thu Aug 6 03:55:39 2009 UTC vs.
Revision 1.42 by jsr166, Thu Nov 18 20:21:53 2010 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 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(BlockingQueue q) {
44 >        try {
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  
70      /**
# Line 49 | Line 84 | public class LinkedTransferQueueTest ext
84          try {
85              new LinkedTransferQueue(null);
86              shouldThrow();
87 <        } catch (NullPointerException success) {
53 <        }
87 >        } catch (NullPointerException success) {}
88      }
89  
90      /**
# Line 62 | Line 96 | public class LinkedTransferQueueTest ext
96              Integer[] ints = new Integer[SIZE];
97              new LinkedTransferQueue(Arrays.asList(ints));
98              shouldThrow();
99 <        } catch (NullPointerException success) {
66 <        }
99 >        } catch (NullPointerException success) {}
100      }
101  
102      /**
# Line 78 | Line 111 | public class LinkedTransferQueueTest ext
111              }
112              new LinkedTransferQueue(Arrays.asList(ints));
113              shouldThrow();
114 <        } catch (NullPointerException success) {
82 <        }
114 >        } catch (NullPointerException success) {}
115      }
116  
117      /**
118       * Queue contains all elements of the collection it is initialized by
119       */
120      public void testConstructor5() {
121 <        try {
122 <            Integer[] ints = new Integer[SIZE];
123 <            for (int i = 0; i < SIZE; ++i) {
124 <                ints[i] = i;
125 <            }
126 <            LinkedTransferQueue q
127 <                = new LinkedTransferQueue(Arrays.asList(ints));
128 <            for (int i = 0; i < SIZE; ++i) {
129 <                assertEquals(ints[i], q.poll());
130 <            }
131 <        } finally {
121 >        Integer[] ints = new Integer[SIZE];
122 >        for (int i = 0; i < SIZE; ++i) {
123 >            ints[i] = i;
124 >        }
125 >        List intList = Arrays.asList(ints);
126 >        LinkedTransferQueue q
127 >            = new LinkedTransferQueue(intList);
128 >        assertEquals(q.size(), intList.size());
129 >        assertEquals(q.toString(), intList.toString());
130 >        assertTrue(Arrays.equals(q.toArray(),
131 >                                     intList.toArray()));
132 >        assertTrue(Arrays.equals(q.toArray(new Object[0]),
133 >                                 intList.toArray(new Object[0])));
134 >        assertTrue(Arrays.equals(q.toArray(new Object[SIZE]),
135 >                                 intList.toArray(new Object[SIZE])));
136 >        for (int i = 0; i < SIZE; ++i) {
137 >            assertEquals(ints[i], q.poll());
138          }
139      }
140  
141      /**
142 <     * Remaining capacity never decrease nor increase on add or remove
142 >     * remainingCapacity() always returns Integer.MAX_VALUE
143       */
144      public void testRemainingCapacity() {
145          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
# Line 125 | Line 163 | public class LinkedTransferQueueTest ext
163              LinkedTransferQueue q = new LinkedTransferQueue();
164              q.offer(null);
165              shouldThrow();
166 <        } catch (NullPointerException success) {
129 <        }
166 >        } catch (NullPointerException success) {}
167      }
168  
169      /**
# Line 137 | Line 174 | public class LinkedTransferQueueTest ext
174              LinkedTransferQueue q = new LinkedTransferQueue();
175              q.add(null);
176              shouldThrow();
177 <        } catch (NullPointerException success) {
141 <        }
177 >        } catch (NullPointerException success) {}
178      }
179  
180      /**
# Line 149 | Line 185 | public class LinkedTransferQueueTest ext
185              LinkedTransferQueue q = new LinkedTransferQueue();
186              q.addAll(null);
187              shouldThrow();
188 <        } catch (NullPointerException success) {
153 <        }
188 >        } catch (NullPointerException success) {}
189      }
190  
191      /**
# Line 161 | Line 196 | public class LinkedTransferQueueTest ext
196              LinkedTransferQueue q = populatedQueue(SIZE);
197              q.addAll(q);
198              shouldThrow();
199 <        } catch (IllegalArgumentException success) {
165 <        }
199 >        } catch (IllegalArgumentException success) {}
200      }
201  
202      /**
# Line 174 | Line 208 | public class LinkedTransferQueueTest ext
208              Integer[] ints = new Integer[SIZE];
209              q.addAll(Arrays.asList(ints));
210              shouldThrow();
211 <        } catch (NullPointerException success) {
178 <        }
211 >        } catch (NullPointerException success) {}
212      }
213  
214      /**
# Line 191 | Line 224 | public class LinkedTransferQueueTest ext
224              }
225              q.addAll(Arrays.asList(ints));
226              shouldThrow();
227 <        } catch (NullPointerException success) {
195 <        }
227 >        } catch (NullPointerException success) {}
228      }
229  
230      /**
# Line 229 | Line 261 | public class LinkedTransferQueueTest ext
261      public void testPut() {
262          LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
263          for (int i = 0; i < SIZE; ++i) {
264 +            assertEquals(q.size(), i);
265              q.put(i);
266              assertTrue(q.contains(i));
267          }
235        assertEquals(q.size(), SIZE);
268      }
269  
270      /**
# Line 246 | Line 278 | public class LinkedTransferQueueTest ext
278      }
279  
280      /**
281 <     * take blocks interruptibly when empty
250 <     */
251 <    public void testTakeFromEmpty() throws InterruptedException {
252 <        final LinkedTransferQueue q = new LinkedTransferQueue();
253 <        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
254 <            void realRun() throws InterruptedException {
255 <                q.take();
256 <            }});
257 <        Thread.sleep(SHORT_DELAY_MS);
258 <        t.interrupt();
259 <        t.join();
260 <    }
261 <
262 <    /**
263 <     * 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 <        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
285 <            void realRun() throws InterruptedException {
286 <                LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
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(SHORT_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  
305      /**
306       * poll succeeds unless empty
307       */
308 <    public void testPoll() {
308 >    public void testPoll() throws InterruptedException {
309          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
310          for (int i = 0; i < SIZE; ++i) {
311              assertEquals(i, (int) q.poll());
312          }
313          assertNull(q.poll());
314 +        checkEmpty(q);
315      }
316  
317      /**
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 296 | Line 323 | public class LinkedTransferQueueTest ext
323              assertEquals(i, (int) q.poll(0, MILLISECONDS));
324          }
325          assertNull(q.poll(0, MILLISECONDS));
326 +        checkEmpty(q);
327      }
328  
329      /**
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 <            assertEquals(i, (int) q.poll(SHORT_DELAY_MS, MILLISECONDS));
335 >            long t0 = System.nanoTime();
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  
345      /**
# Line 314 | 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 <                    threadAssertEquals(i, (int) q.poll(LONG_DELAY_MS,
356 <                                                       MILLISECONDS));
355 >                    long t0 = System.nanoTime();
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                  }
324                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();
373 <        assertEquals(q.size(), 0);
330 <        assertNull(q.poll());
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();
348 <        t.join();
395 >
396 >        awaitTermination(t, MEDIUM_DELAY_MS);
397 >        checkEmpty(q);
398      }
399  
400      /**
401       * peek returns next element, or null if empty
402       */
403 <    public void testPeek() {
403 >    public void testPeek() throws InterruptedException {
404          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
405          for (int i = 0; i < SIZE; ++i) {
406              assertEquals(i, (int) q.peek());
# Line 360 | Line 409 | public class LinkedTransferQueueTest ext
409                         i != (int) q.peek());
410          }
411          assertNull(q.peek());
412 +        checkEmpty(q);
413      }
414  
415      /**
416       * element returns next element, or throws NoSuchElementException if empty
417       */
418 <    public void testElement() {
418 >    public void testElement() throws InterruptedException {
419          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
420          for (int i = 0; i < SIZE; ++i) {
421              assertEquals(i, (int) q.element());
# Line 374 | Line 424 | public class LinkedTransferQueueTest ext
424          try {
425              q.element();
426              shouldThrow();
427 <        } catch (NoSuchElementException success) {
428 <        }
427 >        } catch (NoSuchElementException success) {}
428 >        checkEmpty(q);
429      }
430  
431      /**
432       * remove removes next element, or throws NoSuchElementException if empty
433       */
434 <    public void testRemove() {
434 >    public void testRemove() throws InterruptedException {
435          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
436          for (int i = 0; i < SIZE; ++i) {
437              assertEquals(i, (int) q.remove());
# Line 389 | Line 439 | public class LinkedTransferQueueTest ext
439          try {
440              q.remove();
441              shouldThrow();
442 <        } catch (NoSuchElementException success) {
443 <        }
442 >        } catch (NoSuchElementException success) {}
443 >        checkEmpty(q);
444      }
445  
446      /**
447       * remove(x) removes x and returns true if present
448       */
449 <    public void testRemoveElement() {
449 >    public void testRemoveElement() throws InterruptedException {
450          LinkedTransferQueue q = populatedQueue(SIZE);
451 <        for (int i = 1; i < SIZE; i += 2) {
451 >        for (int i = 1; i < SIZE; i+=2) {
452 >            assertTrue(q.contains(i));
453              assertTrue(q.remove(i));
454 +            assertFalse(q.contains(i));
455 +            assertTrue(q.contains(i-1));
456          }
457 <        for (int i = 0; i < SIZE; i += 2) {
457 >        for (int i = 0; i < SIZE; i+=2) {
458 >            assertTrue(q.contains(i));
459              assertTrue(q.remove(i));
460 <            assertFalse(q.remove(i + 1));
460 >            assertFalse(q.contains(i));
461 >            assertFalse(q.remove(i+1));
462 >            assertFalse(q.contains(i+1));
463          }
464 <        assertTrue(q.isEmpty());
464 >        checkEmpty(q);
465      }
466  
467      /**
# Line 418 | Line 474 | public class LinkedTransferQueueTest ext
474          assertTrue(q.remove(one));
475          assertTrue(q.remove(two));
476          assertTrue(q.add(three));
477 <        assertTrue(q.take() != null);
477 >        assertSame(q.take(), three);
478      }
479  
480      /**
# Line 436 | Line 492 | public class LinkedTransferQueueTest ext
492      /**
493       * clear removes all elements
494       */
495 <    public void testClear() {
495 >    public void testClear() throws InterruptedException {
496          LinkedTransferQueue q = populatedQueue(SIZE);
497          q.clear();
498 <        assertTrue(q.isEmpty());
443 <        assertEquals(0, q.size());
498 >        checkEmpty(q);
499          assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
500          q.add(one);
501          assertFalse(q.isEmpty());
502 +        assertEquals(1, q.size());
503          assertTrue(q.contains(one));
504          q.clear();
505 <        assertTrue(q.isEmpty());
505 >        checkEmpty(q);
506      }
507  
508      /**
# Line 500 | Line 556 | public class LinkedTransferQueueTest ext
556      }
557  
558      /**
559 <     * toArray contains all elements
559 >     * toArray() contains all elements in FIFO order
560       */
561 <    public void testToArray() throws InterruptedException {
561 >    public void testToArray() {
562          LinkedTransferQueue q = populatedQueue(SIZE);
563          Object[] o = q.toArray();
564          for (int i = 0; i < o.length; i++) {
565 <            assertEquals(o[i], q.take());
565 >            assertSame(o[i], q.poll());
566          }
567      }
568  
569      /**
570 <     * toArray(a) contains all elements
570 >     * toArray(a) contains all elements in FIFO order
571       */
572 <    public void testToArray2() throws InterruptedException {
572 >    public void testToArray2() {
573          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
574          Integer[] ints = new Integer[SIZE];
575 <        ints = q.toArray(ints);
575 >        Integer[] array = q.toArray(ints);
576 >        assertSame(ints, array);
577          for (int i = 0; i < ints.length; i++) {
578 <            assertEquals(ints[i], q.take());
578 >            assertSame(ints[i], q.poll());
579          }
580      }
581  
582      /**
583       * toArray(null) throws NullPointerException
584       */
585 <    public void testToArray_BadArg() {
585 >    public void testToArray_NullArg() {
586 >        LinkedTransferQueue q = populatedQueue(SIZE);
587          try {
588 <            LinkedTransferQueue q = populatedQueue(SIZE);
531 <            Object o[] = q.toArray(null);
588 >            q.toArray(null);
589              shouldThrow();
590 <        } catch (NullPointerException success) {
534 <        }
590 >        } catch (NullPointerException success) {}
591      }
592  
593      /**
594 <     * toArray with incompatible array type throws CCE
594 >     * toArray(incompatible array type) throws ArrayStoreException
595       */
596      public void testToArray1_BadArg() {
597 +        LinkedTransferQueue q = populatedQueue(SIZE);
598          try {
599 <            LinkedTransferQueue q = populatedQueue(SIZE);
543 <            Object o[] = q.toArray(new String[10]);
599 >            q.toArray(new String[10]);
600              shouldThrow();
601 <        } catch (ArrayStoreException success) {
546 <        }
601 >        } catch (ArrayStoreException success) {}
602      }
603  
604      /**
# Line 560 | Line 615 | public class LinkedTransferQueueTest ext
615      }
616  
617      /**
618 <     * iterator.remove removes current element
618 >     * iterator.remove() removes current element
619       */
620      public void testIteratorRemove() {
621          final LinkedTransferQueue q = new LinkedTransferQueue();
# Line 573 | Line 628 | public class LinkedTransferQueueTest ext
628          it.remove();
629  
630          it = q.iterator();
631 <        assertEquals(it.next(), one);
632 <        assertEquals(it.next(), three);
631 >        assertSame(it.next(), one);
632 >        assertSame(it.next(), three);
633          assertFalse(it.hasNext());
634      }
635  
# Line 627 | Line 682 | public class LinkedTransferQueueTest ext
682       */
683      public void testOfferInExecutor() {
684          final LinkedTransferQueue q = new LinkedTransferQueue();
685 <        q.add(one);
631 <        q.add(two);
685 >        final CountDownLatch threadsStarted = new CountDownLatch(2);
686          ExecutorService executor = Executors.newFixedThreadPool(2);
687  
688          executor.execute(new CheckedRunnable() {
689 <            void realRun() {
690 <                threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS,
691 <                                         MILLISECONDS));
689 >            public void realRun() throws InterruptedException {
690 >                threadsStarted.countDown();
691 >                threadsStarted.await();
692 >                assertTrue(q.offer(one, MEDIUM_DELAY_MS, MILLISECONDS));
693              }});
694  
695          executor.execute(new CheckedRunnable() {
696 <            void realRun() throws InterruptedException {
697 <                Thread.sleep(SMALL_DELAY_MS);
698 <                threadAssertEquals(one, q.take());
696 >            public void realRun() throws InterruptedException {
697 >                threadsStarted.countDown();
698 >                threadsStarted.await();
699 >                assertSame(one, q.take());
700 >                checkEmpty(q);
701              }});
702  
703          joinPool(executor);
704      }
705  
706      /**
707 <     * poll retrieves elements across Executor threads
707 >     * timed poll retrieves elements across Executor threads
708       */
709      public void testPollInExecutor() {
710          final LinkedTransferQueue q = new LinkedTransferQueue();
711 +        final CountDownLatch threadsStarted = new CountDownLatch(2);
712          ExecutorService executor = Executors.newFixedThreadPool(2);
713  
714          executor.execute(new CheckedRunnable() {
715 <            void realRun() throws InterruptedException {
716 <                threadAssertNull(q.poll());
717 <                threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS,
718 <                                                MILLISECONDS));
719 <                threadAssertTrue(q.isEmpty());
715 >            public void realRun() throws InterruptedException {
716 >                assertNull(q.poll());
717 >                threadsStarted.countDown();
718 >                threadsStarted.await();
719 >                assertSame(one, q.poll(SMALL_DELAY_MS, MILLISECONDS));
720 >                checkEmpty(q);
721              }});
722  
723          executor.execute(new CheckedRunnable() {
724 <            void realRun() throws InterruptedException {
725 <                Thread.sleep(SMALL_DELAY_MS);
724 >            public void realRun() throws InterruptedException {
725 >                threadsStarted.countDown();
726 >                threadsStarted.await();
727                  q.put(one);
728              }});
729  
# Line 689 | Line 749 | public class LinkedTransferQueueTest ext
749          LinkedTransferQueue r = (LinkedTransferQueue) in.readObject();
750  
751          assertEquals(q.size(), r.size());
752 +        assertEquals(q.toString(), r.toString());
753 +        assertTrue(Arrays.equals(q.toArray(), r.toArray()));
754          while (!q.isEmpty()) {
755              assertEquals(q.remove(), r.remove());
756          }
# Line 702 | Line 764 | public class LinkedTransferQueueTest ext
764          try {
765              q.drainTo(null);
766              shouldThrow();
767 <        } catch (NullPointerException success) {
706 <        }
767 >        } catch (NullPointerException success) {}
768      }
769  
770      /**
# Line 714 | Line 775 | public class LinkedTransferQueueTest ext
775          try {
776              q.drainTo(q);
777              shouldThrow();
778 <        } catch (IllegalArgumentException success) {
718 <        }
778 >        } catch (IllegalArgumentException success) {}
779      }
780  
781      /**
# Line 745 | Line 805 | public class LinkedTransferQueueTest ext
805      }
806  
807      /**
808 <     * drainTo empties full queue, unblocking a waiting put.
808 >     * drainTo(c) empties full queue, unblocking a waiting put.
809       */
810      public void testDrainToWithActivePut() throws InterruptedException {
811          final LinkedTransferQueue q = populatedQueue(SIZE);
812          Thread t = newStartedThread(new CheckedRunnable() {
813 <            void realRun() {
813 >            public void realRun() {
814                  q.put(SIZE + 1);
815              }});
816          ArrayList l = new ArrayList();
# Line 759 | Line 819 | public class LinkedTransferQueueTest ext
819          for (int i = 0; i < SIZE; ++i) {
820              assertEquals(l.get(i), i);
821          }
822 <        t.join();
822 >        awaitTermination(t, MEDIUM_DELAY_MS);
823          assertTrue(q.size() + l.size() >= SIZE);
824      }
825  
# Line 769 | Line 829 | public class LinkedTransferQueueTest ext
829      public void testDrainToNullN() {
830          LinkedTransferQueue q = populatedQueue(SIZE);
831          try {
832 <            q.drainTo(null, 0);
832 >            q.drainTo(null, SIZE);
833              shouldThrow();
834 <        } catch (NullPointerException success) {
775 <        }
834 >        } catch (NullPointerException success) {}
835      }
836  
837      /**
# Line 781 | Line 840 | public class LinkedTransferQueueTest ext
840      public void testDrainToSelfN() {
841          LinkedTransferQueue q = populatedQueue(SIZE);
842          try {
843 <            q.drainTo(q, 0);
843 >            q.drainTo(q, SIZE);
844              shouldThrow();
845 <        } catch (IllegalArgumentException success) {
787 <        }
845 >        } catch (IllegalArgumentException success) {}
846      }
847  
848      /**
849 <     * drainTo(c, n) empties first max {n, size} elements of queue into c
849 >     * drainTo(c, n) empties first min(n, size) elements of queue into c
850       */
851      public void testDrainToN() {
852          LinkedTransferQueue q = new LinkedTransferQueue();
# Line 810 | Line 868 | public class LinkedTransferQueueTest ext
868      }
869  
870      /**
871 <     * poll and take decrement the waiting consumer count
871 >     * timed poll() or take() increments the waiting consumer count;
872 >     * offer(e) decrements the waiting consumer count
873       */
874      public void testWaitingConsumer() throws InterruptedException {
875          final LinkedTransferQueue q = new LinkedTransferQueue();
876 <        final ConsumerObserver waiting = new ConsumerObserver();
876 >        assertEquals(q.getWaitingConsumerCount(), 0);
877 >        assertFalse(q.hasWaitingConsumer());
878 >        final CountDownLatch threadStarted = new CountDownLatch(1);
879  
880          Thread t = newStartedThread(new CheckedRunnable() {
881 <            void realRun() throws InterruptedException {
882 <                Thread.sleep(SMALL_DELAY_MS);
883 <                threadAssertTrue(q.hasWaitingConsumer());
884 <                waiting.setWaitingConsumer(q.getWaitingConsumerCount());
885 <                threadAssertTrue(q.offer(new Object()));
881 >            public void realRun() throws InterruptedException {
882 >                threadStarted.countDown();
883 >                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
884 >                assertEquals(q.getWaitingConsumerCount(), 0);
885 >                assertFalse(q.hasWaitingConsumer());
886              }});
887  
888 <        assertTrue(q.poll(LONG_DELAY_MS, MILLISECONDS) != null);
889 <        assertTrue(q.getWaitingConsumerCount()
890 <                   < waiting.getWaitingConsumers());
891 <        t.join();
888 >        threadStarted.await();
889 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
890 >        assertEquals(q.getWaitingConsumerCount(), 1);
891 >        assertTrue(q.hasWaitingConsumer());
892 >
893 >        assertTrue(q.offer(one));
894 >        assertEquals(q.getWaitingConsumerCount(), 0);
895 >        assertFalse(q.hasWaitingConsumer());
896 >
897 >        awaitTermination(t, MEDIUM_DELAY_MS);
898      }
899  
900      /**
# Line 838 | Line 905 | public class LinkedTransferQueueTest ext
905              LinkedTransferQueue q = new LinkedTransferQueue();
906              q.transfer(null);
907              shouldThrow();
908 <        } catch (NullPointerException ex) {
842 <        }
908 >        } catch (NullPointerException success) {}
909      }
910  
911      /**
# Line 849 | Line 915 | public class LinkedTransferQueueTest ext
915      public void testTransfer2() throws InterruptedException {
916          final LinkedTransferQueue<Integer> q
917              = new LinkedTransferQueue<Integer>();
918 +        final CountDownLatch threadStarted = new CountDownLatch(1);
919  
920          Thread t = newStartedThread(new CheckedRunnable() {
921 <            void realRun() throws InterruptedException {
922 <                q.transfer(SIZE);
923 <                threadAssertTrue(q.isEmpty());
921 >            public void realRun() throws InterruptedException {
922 >                threadStarted.countDown();
923 >                q.transfer(five);
924 >                checkEmpty(q);
925              }});
926  
927 <        Thread.sleep(SHORT_DELAY_MS);
927 >        threadStarted.await();
928 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
929          assertEquals(1, q.size());
930 <        assertEquals(SIZE, (int) q.poll());
931 <        assertTrue(q.isEmpty());
932 <        t.join();
930 >        assertSame(five, q.poll());
931 >        checkEmpty(q);
932 >        awaitTermination(t, MEDIUM_DELAY_MS);
933      }
934  
935      /**
# Line 871 | Line 940 | public class LinkedTransferQueueTest ext
940              = new LinkedTransferQueue<Integer>();
941  
942          Thread first = newStartedThread(new CheckedRunnable() {
943 <            void realRun() throws InterruptedException {
944 <                Integer i = SIZE + 1;
945 <                q.transfer(i);
946 <                threadAssertTrue(!q.contains(i));
878 <                threadAssertEquals(1, q.size());
943 >            public void realRun() throws InterruptedException {
944 >                q.transfer(four);
945 >                assertTrue(!q.contains(four));
946 >                assertEquals(1, q.size());
947              }});
948  
949          Thread interruptedThread = newStartedThread(
950              new CheckedInterruptedRunnable() {
951 <                void realRun() throws InterruptedException {
952 <                    while (q.size() == 0)
951 >                public void realRun() throws InterruptedException {
952 >                    while (q.isEmpty())
953                          Thread.yield();
954 <                    q.transfer(SIZE);
954 >                    q.transfer(five);
955                  }});
956  
957          while (q.size() < 2)
958              Thread.yield();
959          assertEquals(2, q.size());
960 <        assertEquals(SIZE + 1, (int) q.poll());
960 >        assertSame(four, q.poll());
961          first.join();
962          assertEquals(1, q.size());
963          interruptedThread.interrupt();
964          interruptedThread.join();
965 <        assertEquals(0, q.size());
898 <        assertTrue(q.isEmpty());
965 >        checkEmpty(q);
966      }
967  
968      /**
# Line 906 | Line 973 | public class LinkedTransferQueueTest ext
973          final LinkedTransferQueue q = new LinkedTransferQueue();
974  
975          Thread t = newStartedThread(new CheckedRunnable() {
976 <            void realRun() throws InterruptedException {
976 >            public void realRun() throws InterruptedException {
977                  q.transfer(four);
978 <                threadAssertFalse(q.contains(four));
979 <                threadAssertEquals(three, q.poll());
978 >                assertFalse(q.contains(four));
979 >                assertSame(three, q.poll());
980              }});
981  
982 <        Thread.sleep(SHORT_DELAY_MS);
982 >        while (q.isEmpty())
983 >            Thread.yield();
984 >        assertFalse(q.isEmpty());
985 >        assertEquals(1, q.size());
986          assertTrue(q.offer(three));
987 <        assertEquals(four, q.poll());
988 <        t.join();
987 >        assertSame(four, q.poll());
988 >        awaitTermination(t, MEDIUM_DELAY_MS);
989      }
990  
991      /**
# Line 927 | Line 997 | public class LinkedTransferQueueTest ext
997              = new LinkedTransferQueue<Integer>();
998  
999          Thread t = newStartedThread(new CheckedRunnable() {
1000 <            void realRun() throws InterruptedException {
1001 <                q.transfer(SIZE);
1002 <                threadAssertTrue(q.isEmpty());
1000 >            public void realRun() throws InterruptedException {
1001 >                q.transfer(four);
1002 >                checkEmpty(q);
1003              }});
1004  
1005 <        Thread.sleep(SHORT_DELAY_MS);
1006 <        assertEquals(SIZE, (int) q.take());
1007 <        assertTrue(q.isEmpty());
1008 <        t.join();
1005 >        while (q.isEmpty())
1006 >            Thread.yield();
1007 >        assertFalse(q.isEmpty());
1008 >        assertEquals(1, q.size());
1009 >        assertSame(four, q.take());
1010 >        checkEmpty(q);
1011 >        awaitTermination(t, MEDIUM_DELAY_MS);
1012      }
1013  
1014      /**
# Line 946 | Line 1019 | public class LinkedTransferQueueTest ext
1019              final LinkedTransferQueue q = new LinkedTransferQueue();
1020              q.tryTransfer(null);
1021              shouldThrow();
1022 <        } catch (NullPointerException ex) {
950 <        }
1022 >        } catch (NullPointerException success) {}
1023      }
1024  
1025      /**
1026       * tryTransfer returns false and does not enqueue if there are no
1027       * consumers waiting to poll or take.
1028       */
1029 <    public void testTryTransfer2() {
1029 >    public void testTryTransfer2() throws InterruptedException {
1030          final LinkedTransferQueue q = new LinkedTransferQueue();
1031          assertFalse(q.tryTransfer(new Object()));
1032          assertFalse(q.hasWaitingConsumer());
1033 <        assertTrue(q.isEmpty());
962 <        assertEquals(0, q.size());
1033 >        checkEmpty(q);
1034      }
1035  
1036      /**
# Line 971 | Line 1042 | public class LinkedTransferQueueTest ext
1042          final LinkedTransferQueue q = new LinkedTransferQueue();
1043  
1044          Thread t = newStartedThread(new CheckedRunnable() {
1045 <            void realRun() {
1045 >            public void realRun() {
1046                  while (! q.hasWaitingConsumer())
1047                      Thread.yield();
1048 <                threadAssertTrue(q.hasWaitingConsumer());
1049 <                threadAssertTrue(q.isEmpty());
1050 <                threadAssertTrue(q.size() == 0);
980 <                threadAssertTrue(q.tryTransfer(hotPotato));
1048 >                assertTrue(q.hasWaitingConsumer());
1049 >                checkEmpty(q);
1050 >                assertTrue(q.tryTransfer(hotPotato));
1051              }});
1052  
1053 <        assertTrue(q.poll(MEDIUM_DELAY_MS, MILLISECONDS) == hotPotato);
1054 <        assertTrue(q.isEmpty());
1055 <        t.join();
1053 >        assertSame(hotPotato, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
1054 >        checkEmpty(q);
1055 >        awaitTermination(t, MEDIUM_DELAY_MS);
1056      }
1057  
1058      /**
# Line 994 | Line 1064 | public class LinkedTransferQueueTest ext
1064          final LinkedTransferQueue q = new LinkedTransferQueue();
1065  
1066          Thread t = newStartedThread(new CheckedRunnable() {
1067 <            void realRun() {
1067 >            public void realRun() {
1068                  while (! q.hasWaitingConsumer())
1069                      Thread.yield();
1070 <                threadAssertTrue(q.hasWaitingConsumer());
1071 <                threadAssertTrue(q.isEmpty());
1072 <                threadAssertTrue(q.size() == 0);
1003 <                threadAssertTrue(q.tryTransfer(hotPotato));
1070 >                assertTrue(q.hasWaitingConsumer());
1071 >                checkEmpty(q);
1072 >                assertTrue(q.tryTransfer(hotPotato));
1073              }});
1074  
1075 <        assertTrue(q.take() == hotPotato);
1076 <        assertTrue(q.isEmpty());
1077 <        t.join();
1075 >        assertSame(q.take(), hotPotato);
1076 >        checkEmpty(q);
1077 >        awaitTermination(t, MEDIUM_DELAY_MS);
1078      }
1079  
1080      /**
1081 <     * tryTransfer waits the amount given if interrupted, and
1082 <     * throws interrupted exception
1081 >     * tryTransfer waits the amount given, and throws
1082 >     * InterruptedException when interrupted.
1083       */
1084      public void testTryTransfer5() throws InterruptedException {
1085          final LinkedTransferQueue q = new LinkedTransferQueue();
1086 +        final CountDownLatch threadStarted = new CountDownLatch(1);
1087 +        assertTrue(q.isEmpty());
1088  
1089 <        Thread toInterrupt = newStartedThread(new CheckedInterruptedRunnable() {
1090 <            void realRun() throws InterruptedException {
1091 <                q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS);
1089 >        Thread t = newStartedThread(new CheckedRunnable() {
1090 >            public void realRun() throws InterruptedException {
1091 >                long t0 = System.nanoTime();
1092 >                threadStarted.countDown();
1093 >                try {
1094 >                    q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS);
1095 >                    shouldThrow();
1096 >                } catch (InterruptedException success) {}
1097 >                assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
1098 >                assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
1099              }});
1100  
1101 <        Thread.sleep(SMALL_DELAY_MS);
1102 <        toInterrupt.interrupt();
1103 <        toInterrupt.join();
1101 >        threadStarted.await();
1102 >        while (q.isEmpty())
1103 >            Thread.yield();
1104 >        Thread.sleep(SHORT_DELAY_MS);
1105 >        t.interrupt();
1106 >        awaitTermination(t, MEDIUM_DELAY_MS);
1107 >        checkEmpty(q);
1108      }
1109  
1110      /**
1111 <     * tryTransfer gives up after the timeout and return false
1111 >     * tryTransfer gives up after the timeout and returns false
1112       */
1113      public void testTryTransfer6() throws InterruptedException {
1114          final LinkedTransferQueue q = new LinkedTransferQueue();
1115  
1116          Thread t = newStartedThread(new CheckedRunnable() {
1117 <            void realRun() throws InterruptedException {
1118 <                threadAssertFalse
1119 <                    (q.tryTransfer(new Object(),
1120 <                                   SHORT_DELAY_MS, MILLISECONDS));
1117 >            public void realRun() throws InterruptedException {
1118 >                long t0 = System.nanoTime();
1119 >                assertFalse(q.tryTransfer(new Object(),
1120 >                                          SHORT_DELAY_MS, MILLISECONDS));
1121 >                assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
1122 >                checkEmpty(q);
1123              }});
1124  
1125 <        Thread.sleep(SMALL_DELAY_MS);
1126 <        assertTrue(q.isEmpty());
1043 <        t.join();
1125 >        awaitTermination(t, MEDIUM_DELAY_MS);
1126 >        checkEmpty(q);
1127      }
1128  
1129      /**
# Line 1052 | Line 1135 | public class LinkedTransferQueueTest ext
1135          assertTrue(q.offer(four));
1136  
1137          Thread t = newStartedThread(new CheckedRunnable() {
1138 <            void realRun() throws InterruptedException {
1139 <                threadAssertTrue(q.tryTransfer(five,
1140 <                                               MEDIUM_DELAY_MS, MILLISECONDS));
1058 <                threadAssertTrue(q.isEmpty());
1138 >            public void realRun() throws InterruptedException {
1139 >                assertTrue(q.tryTransfer(five, MEDIUM_DELAY_MS, MILLISECONDS));
1140 >                checkEmpty(q);
1141              }});
1142  
1143 <        Thread.sleep(SHORT_DELAY_MS);
1143 >        while (q.size() != 2)
1144 >            Thread.yield();
1145          assertEquals(2, q.size());
1146 <        assertEquals(four, q.poll());
1147 <        assertEquals(five, q.poll());
1148 <        assertTrue(q.isEmpty());
1149 <        t.join();
1146 >        assertSame(four, q.poll());
1147 >        assertSame(five, q.poll());
1148 >        checkEmpty(q);
1149 >        awaitTermination(t, MEDIUM_DELAY_MS);
1150      }
1151  
1152      /**
1153 <     * tryTransfer attempts to enqueue into the q and fails returning
1154 <     * false not enqueueing and the successive poll is null
1153 >     * tryTransfer attempts to enqueue into the queue and fails
1154 >     * returning false not enqueueing and the successive poll is null
1155       */
1156      public void testTryTransfer8() throws InterruptedException {
1157          final LinkedTransferQueue q = new LinkedTransferQueue();
1158          assertTrue(q.offer(four));
1159          assertEquals(1, q.size());
1160 +        long t0 = System.nanoTime();
1161          assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS));
1162 +        assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
1163          assertEquals(1, q.size());
1164 <        assertEquals(four, q.poll());
1080 <        threadAssertTrue(q.isEmpty());
1164 >        assertSame(four, q.poll());
1165          assertNull(q.poll());
1166 +        checkEmpty(q);
1167      }
1168  
1169      private LinkedTransferQueue<Integer> populatedQueue(int n) {
1170          LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
1171 <        assertTrue(q.isEmpty());
1171 >        checkEmpty(q);
1172          for (int i = 0; i < n; i++) {
1173              assertEquals(i, q.size());
1174              assertTrue(q.offer(i));
# Line 1092 | Line 1177 | public class LinkedTransferQueueTest ext
1177          assertFalse(q.isEmpty());
1178          return q;
1179      }
1095
1096    private static class ConsumerObserver {
1097
1098        private int waitingConsumers;
1099
1100        private ConsumerObserver() {
1101        }
1102
1103        private void setWaitingConsumer(int i) {
1104            this.waitingConsumers = i;
1105        }
1106
1107        private int getWaitingConsumers() {
1108            return waitingConsumers;
1109        }
1110    }
1180   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines