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.9 by jsr166, Wed Aug 5 00:43:23 2009 UTC vs.
Revision 1.33 by jsr166, Fri Oct 29 06:22:12 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 48 | Line 84 | public class LinkedTransferQueueTest ext
84          try {
85              new LinkedTransferQueue(null);
86              shouldThrow();
87 <        } catch (NullPointerException success) {
52 <        }
87 >        } catch (NullPointerException success) {}
88      }
89  
90      /**
# Line 61 | Line 96 | public class LinkedTransferQueueTest ext
96              Integer[] ints = new Integer[SIZE];
97              new LinkedTransferQueue(Arrays.asList(ints));
98              shouldThrow();
99 <        } catch (NullPointerException success) {
65 <        }
99 >        } catch (NullPointerException success) {}
100      }
101  
102      /**
# Line 77 | Line 111 | public class LinkedTransferQueueTest ext
111              }
112              new LinkedTransferQueue(Arrays.asList(ints));
113              shouldThrow();
114 <        } catch (NullPointerException success) {
81 <        }
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 124 | Line 163 | public class LinkedTransferQueueTest ext
163              LinkedTransferQueue q = new LinkedTransferQueue();
164              q.offer(null);
165              shouldThrow();
166 <        } catch (NullPointerException success) {
128 <        }
166 >        } catch (NullPointerException success) {}
167      }
168  
169      /**
# Line 136 | Line 174 | public class LinkedTransferQueueTest ext
174              LinkedTransferQueue q = new LinkedTransferQueue();
175              q.add(null);
176              shouldThrow();
177 <        } catch (NullPointerException success) {
140 <        }
177 >        } catch (NullPointerException success) {}
178      }
179  
180      /**
# Line 148 | Line 185 | public class LinkedTransferQueueTest ext
185              LinkedTransferQueue q = new LinkedTransferQueue();
186              q.addAll(null);
187              shouldThrow();
188 <        } catch (NullPointerException success) {
152 <        }
188 >        } catch (NullPointerException success) {}
189      }
190  
191      /**
# Line 160 | Line 196 | public class LinkedTransferQueueTest ext
196              LinkedTransferQueue q = populatedQueue(SIZE);
197              q.addAll(q);
198              shouldThrow();
199 <        } catch (IllegalArgumentException success) {
164 <        }
199 >        } catch (IllegalArgumentException success) {}
200      }
201  
202      /**
# Line 173 | Line 208 | public class LinkedTransferQueueTest ext
208              Integer[] ints = new Integer[SIZE];
209              q.addAll(Arrays.asList(ints));
210              shouldThrow();
211 <        } catch (NullPointerException success) {
177 <        }
211 >        } catch (NullPointerException success) {}
212      }
213  
214      /**
# Line 190 | Line 224 | public class LinkedTransferQueueTest ext
224              }
225              q.addAll(Arrays.asList(ints));
226              shouldThrow();
227 <        } catch (NullPointerException success) {
194 <        }
227 >        } catch (NullPointerException success) {}
228      }
229  
230      /**
# Line 228 | 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          }
234        assertEquals(q.size(), SIZE);
268      }
269  
270      /**
# Line 245 | Line 278 | public class LinkedTransferQueueTest ext
278      }
279  
280      /**
281 <     * take blocks interruptibly when empty
249 <     */
250 <    public void testTakeFromEmpty() throws InterruptedException {
251 <        final LinkedTransferQueue q = new LinkedTransferQueue();
252 <        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
253 <            void realRun() throws InterruptedException {
254 <                q.take();
255 <            }});
256 <        Thread.sleep(SHORT_DELAY_MS);
257 <        t.interrupt();
258 <        t.join();
259 <    }
260 <
261 <    /**
262 <     * 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 295 | 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 313 | 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) < SHORT_DELAY_MS);
358                  }
359 <                q.poll(LONG_DELAY_MS, MILLISECONDS);
359 >                aboutToWait.countDown();
360 >                try {
361 >                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
362 >                    shouldThrow();
363 >                } catch (InterruptedException success) {}
364              }});
365 <        Thread.sleep(SMALL_DELAY_MS);
365 >
366 >        aboutToWait.await();
367 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
368          t.interrupt();
369 <        t.join();
370 <        assertEquals(q.size(), 0);
329 <        assertNull(q.poll());
369 >        awaitTermination(t, MEDIUM_DELAY_MS);
370 >        checkEmpty(q);
371      }
372  
373      /**
374 <     * timed poll before a delayed offer fails; after offer succeeds;
375 <     * on interruption throws
374 >     * timed poll after thread interrupted throws InterruptedException
375 >     * instead of returning timeout status
376       */
377 <    public void testTimedPollWithOffer() throws InterruptedException {
378 <        final LinkedTransferQueue q = new LinkedTransferQueue();
379 <        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
380 <            void realRun() throws InterruptedException {
381 <                threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
382 <                q.poll(LONG_DELAY_MS, MILLISECONDS);
383 <                q.poll(LONG_DELAY_MS, MILLISECONDS);
377 >    public void testTimedPollAfterInterrupt() throws InterruptedException {
378 >        final BlockingQueue<Integer> q = populatedQueue(SIZE);
379 >        Thread t = newStartedThread(new CheckedRunnable() {
380 >            public void realRun() throws InterruptedException {
381 >                Thread.currentThread().interrupt();
382 >                for (int i = 0; i < SIZE; ++i) {
383 >                    long t0 = System.nanoTime();
384 >                    assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
385 >                    assertTrue(millisElapsedSince(t0) < SHORT_DELAY_MS);
386 >                }
387 >                try {
388 >                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
389 >                    shouldThrow();
390 >                } catch (InterruptedException success) {}
391              }});
392 <        Thread.sleep(SMALL_DELAY_MS);
393 <        assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
394 <        t.interrupt();
347 <        t.join();
392 >
393 >        awaitTermination(t, MEDIUM_DELAY_MS);
394 >        checkEmpty(q);
395      }
396  
397      /**
398       * peek returns next element, or null if empty
399       */
400 <    public void testPeek() {
400 >    public void testPeek() throws InterruptedException {
401          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
402          for (int i = 0; i < SIZE; ++i) {
403              assertEquals(i, (int) q.peek());
# Line 359 | Line 406 | public class LinkedTransferQueueTest ext
406                         i != (int) q.peek());
407          }
408          assertNull(q.peek());
409 +        checkEmpty(q);
410      }
411  
412      /**
413       * element returns next element, or throws NoSuchElementException if empty
414       */
415 <    public void testElement() {
415 >    public void testElement() throws InterruptedException {
416          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
417          for (int i = 0; i < SIZE; ++i) {
418              assertEquals(i, (int) q.element());
# Line 373 | Line 421 | public class LinkedTransferQueueTest ext
421          try {
422              q.element();
423              shouldThrow();
424 <        } catch (NoSuchElementException success) {
425 <        }
424 >        } catch (NoSuchElementException success) {}
425 >        checkEmpty(q);
426      }
427  
428      /**
429       * remove removes next element, or throws NoSuchElementException if empty
430       */
431 <    public void testRemove() {
431 >    public void testRemove() throws InterruptedException {
432          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
433          for (int i = 0; i < SIZE; ++i) {
434              assertEquals(i, (int) q.remove());
# Line 388 | Line 436 | public class LinkedTransferQueueTest ext
436          try {
437              q.remove();
438              shouldThrow();
439 <        } catch (NoSuchElementException success) {
440 <        }
439 >        } catch (NoSuchElementException success) {}
440 >        checkEmpty(q);
441      }
442  
443      /**
444       * remove(x) removes x and returns true if present
445       */
446 <    public void testRemoveElement() {
446 >    public void testRemoveElement() throws InterruptedException {
447          LinkedTransferQueue q = populatedQueue(SIZE);
448          for (int i = 1; i < SIZE; i += 2) {
449              assertTrue(q.remove(i));
# Line 404 | Line 452 | public class LinkedTransferQueueTest ext
452              assertTrue(q.remove(i));
453              assertFalse(q.remove(i + 1));
454          }
455 <        assertTrue(q.isEmpty());
455 >        checkEmpty(q);
456      }
457  
458      /**
# Line 417 | Line 465 | public class LinkedTransferQueueTest ext
465          assertTrue(q.remove(one));
466          assertTrue(q.remove(two));
467          assertTrue(q.add(three));
468 <        assertTrue(q.take() != null);
468 >        assertSame(q.take(), three);
469      }
470  
471      /**
# Line 435 | Line 483 | public class LinkedTransferQueueTest ext
483      /**
484       * clear removes all elements
485       */
486 <    public void testClear() {
486 >    public void testClear() throws InterruptedException {
487          LinkedTransferQueue q = populatedQueue(SIZE);
488          q.clear();
489 <        assertTrue(q.isEmpty());
442 <        assertEquals(0, q.size());
489 >        checkEmpty(q);
490          assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
491          q.add(one);
492          assertFalse(q.isEmpty());
493 +        assertEquals(1, q.size());
494          assertTrue(q.contains(one));
495          q.clear();
496 <        assertTrue(q.isEmpty());
496 >        checkEmpty(q);
497      }
498  
499      /**
# Line 499 | Line 547 | public class LinkedTransferQueueTest ext
547      }
548  
549      /**
550 <     * toArray contains all elements
550 >     * toArray() contains all elements
551       */
552      public void testToArray() throws InterruptedException {
553          LinkedTransferQueue q = populatedQueue(SIZE);
# Line 525 | Line 573 | public class LinkedTransferQueueTest ext
573       * toArray(null) throws NullPointerException
574       */
575      public void testToArray_BadArg() {
576 +        LinkedTransferQueue q = populatedQueue(SIZE);
577          try {
529            LinkedTransferQueue q = populatedQueue(SIZE);
578              Object o[] = q.toArray(null);
579              shouldThrow();
580 <        } catch (NullPointerException success) {
533 <        }
580 >        } catch (NullPointerException success) {}
581      }
582  
583      /**
584 <     * toArray with incompatible array type throws CCE
584 >     * toArray(incompatible array type) throws CCE
585       */
586      public void testToArray1_BadArg() {
587 +        LinkedTransferQueue q = populatedQueue(SIZE);
588          try {
541            LinkedTransferQueue q = populatedQueue(SIZE);
589              Object o[] = q.toArray(new String[10]);
590              shouldThrow();
591 <        } catch (ArrayStoreException success) {
545 <        }
591 >        } catch (ArrayStoreException success) {}
592      }
593  
594      /**
# Line 551 | Line 597 | public class LinkedTransferQueueTest ext
597      public void testIterator() throws InterruptedException {
598          LinkedTransferQueue q = populatedQueue(SIZE);
599          Iterator it = q.iterator();
600 +        int i = 0;
601          while (it.hasNext()) {
602 <            assertEquals(it.next(), q.take());
602 >            assertEquals(it.next(), i++);
603          }
604 +        assertEquals(i, SIZE);
605      }
606  
607      /**
608 <     * iterator.remove removes current element
608 >     * iterator.remove() removes current element
609       */
610      public void testIteratorRemove() {
611          final LinkedTransferQueue q = new LinkedTransferQueue();
# Line 570 | Line 618 | public class LinkedTransferQueueTest ext
618          it.remove();
619  
620          it = q.iterator();
621 <        assertEquals(it.next(), one);
622 <        assertEquals(it.next(), three);
621 >        assertSame(it.next(), one);
622 >        assertSame(it.next(), three);
623          assertFalse(it.hasNext());
624      }
625  
# Line 624 | Line 672 | public class LinkedTransferQueueTest ext
672       */
673      public void testOfferInExecutor() {
674          final LinkedTransferQueue q = new LinkedTransferQueue();
675 <        q.add(one);
628 <        q.add(two);
675 >        final CountDownLatch threadsStarted = new CountDownLatch(2);
676          ExecutorService executor = Executors.newFixedThreadPool(2);
677  
678          executor.execute(new CheckedRunnable() {
679 <            void realRun() {
680 <                threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS,
681 <                                         MILLISECONDS));
679 >            public void realRun() throws InterruptedException {
680 >                threadsStarted.countDown();
681 >                threadsStarted.await();
682 >                assertTrue(q.offer(one, MEDIUM_DELAY_MS, MILLISECONDS));
683              }});
684  
685          executor.execute(new CheckedRunnable() {
686 <            void realRun() throws InterruptedException {
687 <                Thread.sleep(SMALL_DELAY_MS);
688 <                threadAssertEquals(one, q.take());
686 >            public void realRun() throws InterruptedException {
687 >                threadsStarted.countDown();
688 >                threadsStarted.await();
689 >                assertSame(one, q.take());
690 >                checkEmpty(q);
691              }});
692  
693          joinPool(executor);
694      }
695  
696      /**
697 <     * poll retrieves elements across Executor threads
697 >     * timed poll retrieves elements across Executor threads
698       */
699      public void testPollInExecutor() {
700          final LinkedTransferQueue q = new LinkedTransferQueue();
701 +        final CountDownLatch threadsStarted = new CountDownLatch(2);
702          ExecutorService executor = Executors.newFixedThreadPool(2);
703  
704          executor.execute(new CheckedRunnable() {
705 <            void realRun() throws InterruptedException {
706 <                threadAssertNull(q.poll());
707 <                threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS,
708 <                                                MILLISECONDS));
709 <                threadAssertTrue(q.isEmpty());
705 >            public void realRun() throws InterruptedException {
706 >                assertNull(q.poll());
707 >                threadsStarted.countDown();
708 >                threadsStarted.await();
709 >                assertSame(one, q.poll(SMALL_DELAY_MS, MILLISECONDS));
710 >                checkEmpty(q);
711              }});
712  
713          executor.execute(new CheckedRunnable() {
714 <            void realRun() throws InterruptedException {
715 <                Thread.sleep(SMALL_DELAY_MS);
714 >            public void realRun() throws InterruptedException {
715 >                threadsStarted.countDown();
716 >                threadsStarted.await();
717                  q.put(one);
718              }});
719  
# Line 686 | Line 739 | public class LinkedTransferQueueTest ext
739          LinkedTransferQueue r = (LinkedTransferQueue) in.readObject();
740  
741          assertEquals(q.size(), r.size());
742 +        assertEquals(q.toString(), r.toString());
743 +        assertTrue(Arrays.equals(q.toArray(), r.toArray()));
744          while (!q.isEmpty()) {
745              assertEquals(q.remove(), r.remove());
746          }
# Line 699 | Line 754 | public class LinkedTransferQueueTest ext
754          try {
755              q.drainTo(null);
756              shouldThrow();
757 <        } catch (NullPointerException success) {
703 <        }
757 >        } catch (NullPointerException success) {}
758      }
759  
760      /**
# Line 711 | Line 765 | public class LinkedTransferQueueTest ext
765          try {
766              q.drainTo(q);
767              shouldThrow();
768 <        } catch (IllegalArgumentException success) {
715 <        }
768 >        } catch (IllegalArgumentException success) {}
769      }
770  
771      /**
# Line 742 | Line 795 | public class LinkedTransferQueueTest ext
795      }
796  
797      /**
798 <     * drainTo empties full queue, unblocking a waiting put.
798 >     * drainTo(c) empties full queue, unblocking a waiting put.
799       */
800      public void testDrainToWithActivePut() throws InterruptedException {
801          final LinkedTransferQueue q = populatedQueue(SIZE);
802          Thread t = newStartedThread(new CheckedRunnable() {
803 <            void realRun() {
803 >            public void realRun() {
804                  q.put(SIZE + 1);
805              }});
806          ArrayList l = new ArrayList();
# Line 756 | Line 809 | public class LinkedTransferQueueTest ext
809          for (int i = 0; i < SIZE; ++i) {
810              assertEquals(l.get(i), i);
811          }
812 <        t.join();
812 >        awaitTermination(t, MEDIUM_DELAY_MS);
813          assertTrue(q.size() + l.size() >= SIZE);
814      }
815  
# Line 766 | Line 819 | public class LinkedTransferQueueTest ext
819      public void testDrainToNullN() {
820          LinkedTransferQueue q = populatedQueue(SIZE);
821          try {
822 <            q.drainTo(null, 0);
822 >            q.drainTo(null, SIZE);
823              shouldThrow();
824 <        } catch (NullPointerException success) {
772 <        }
824 >        } catch (NullPointerException success) {}
825      }
826  
827      /**
# Line 778 | Line 830 | public class LinkedTransferQueueTest ext
830      public void testDrainToSelfN() {
831          LinkedTransferQueue q = populatedQueue(SIZE);
832          try {
833 <            q.drainTo(q, 0);
833 >            q.drainTo(q, SIZE);
834              shouldThrow();
835 <        } catch (IllegalArgumentException success) {
784 <        }
835 >        } catch (IllegalArgumentException success) {}
836      }
837  
838      /**
839 <     * drainTo(c, n) empties first max {n, size} elements of queue into c
839 >     * drainTo(c, n) empties first min(n, size) elements of queue into c
840       */
841      public void testDrainToN() {
842          LinkedTransferQueue q = new LinkedTransferQueue();
# Line 807 | Line 858 | public class LinkedTransferQueueTest ext
858      }
859  
860      /**
861 <     * poll and take decrement the waiting consumer count
861 >     * timed poll() or take() increments the waiting consumer count;
862 >     * offer(e) decrements the waiting consumer count
863       */
864      public void testWaitingConsumer() throws InterruptedException {
865          final LinkedTransferQueue q = new LinkedTransferQueue();
866 <        final ConsumerObserver waiting = new ConsumerObserver();
866 >        assertEquals(q.getWaitingConsumerCount(), 0);
867 >        assertFalse(q.hasWaitingConsumer());
868 >        final CountDownLatch threadStarted = new CountDownLatch(1);
869  
870          Thread t = newStartedThread(new CheckedRunnable() {
871 <            void realRun() throws InterruptedException {
872 <                Thread.sleep(SMALL_DELAY_MS);
873 <                threadAssertTrue(q.hasWaitingConsumer());
874 <                waiting.setWaitingConsumer(q.getWaitingConsumerCount());
875 <                threadAssertTrue(q.offer(new Object()));
871 >            public void realRun() throws InterruptedException {
872 >                threadStarted.countDown();
873 >                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
874 >                assertEquals(q.getWaitingConsumerCount(), 0);
875 >                assertFalse(q.hasWaitingConsumer());
876              }});
877  
878 <        assertTrue(q.poll(LONG_DELAY_MS, MILLISECONDS) != null);
879 <        assertTrue(q.getWaitingConsumerCount()
880 <                   < waiting.getWaitingConsumers());
881 <        t.join();
878 >        threadStarted.await();
879 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
880 >        assertEquals(q.getWaitingConsumerCount(), 1);
881 >        assertTrue(q.hasWaitingConsumer());
882 >
883 >        assertTrue(q.offer(one));
884 >        assertEquals(q.getWaitingConsumerCount(), 0);
885 >        assertFalse(q.hasWaitingConsumer());
886 >
887 >        awaitTermination(t, MEDIUM_DELAY_MS);
888      }
889  
890      /**
# Line 835 | Line 895 | public class LinkedTransferQueueTest ext
895              LinkedTransferQueue q = new LinkedTransferQueue();
896              q.transfer(null);
897              shouldThrow();
898 <        } catch (NullPointerException ex) {
839 <        }
898 >        } catch (NullPointerException success) {}
899      }
900  
901      /**
# Line 846 | Line 905 | public class LinkedTransferQueueTest ext
905      public void testTransfer2() throws InterruptedException {
906          final LinkedTransferQueue<Integer> q
907              = new LinkedTransferQueue<Integer>();
908 +        final CountDownLatch threadStarted = new CountDownLatch(1);
909  
910          Thread t = newStartedThread(new CheckedRunnable() {
911 <            void realRun() throws InterruptedException {
911 >            public void realRun() throws InterruptedException {
912 >                threadStarted.countDown();
913                  q.transfer(SIZE);
914 <                threadAssertTrue(q.isEmpty());
914 >                checkEmpty(q);
915              }});
916  
917 <        Thread.sleep(SHORT_DELAY_MS);
917 >        threadStarted.await();
918 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
919 >        assertTrue(t.isAlive());
920          assertEquals(1, q.size());
921          assertEquals(SIZE, (int) q.poll());
922 <        assertTrue(q.isEmpty());
923 <        t.join();
922 >        checkEmpty(q);
923 >        awaitTermination(t, MEDIUM_DELAY_MS);
924      }
925  
926      /**
# Line 868 | Line 931 | public class LinkedTransferQueueTest ext
931              = new LinkedTransferQueue<Integer>();
932  
933          Thread first = newStartedThread(new CheckedRunnable() {
934 <            void realRun() throws InterruptedException {
935 <                Integer i = SIZE + 1;
936 <                q.transfer(i);
937 <                threadAssertTrue(!q.contains(i));
875 <                threadAssertEquals(1, q.size());
934 >            public void realRun() throws InterruptedException {
935 >                q.transfer(four);
936 >                assertTrue(!q.contains(four));
937 >                assertEquals(1, q.size());
938              }});
939  
940          Thread interruptedThread = newStartedThread(
941              new CheckedInterruptedRunnable() {
942 <                void realRun() throws InterruptedException {
943 <                    while (q.size() == 0)
942 >                public void realRun() throws InterruptedException {
943 >                    while (q.isEmpty())
944                          Thread.yield();
945 <                    q.transfer(SIZE);
945 >                    q.transfer(five);
946                  }});
947  
948          while (q.size() < 2)
949              Thread.yield();
950          assertEquals(2, q.size());
951 <        assertEquals(SIZE + 1, (int) q.poll());
951 >        assertSame(four, q.poll());
952          first.join();
953          assertEquals(1, q.size());
954          interruptedThread.interrupt();
955          interruptedThread.join();
956 <        assertEquals(0, q.size());
895 <        assertTrue(q.isEmpty());
956 >        checkEmpty(q);
957      }
958  
959      /**
# Line 903 | Line 964 | public class LinkedTransferQueueTest ext
964          final LinkedTransferQueue q = new LinkedTransferQueue();
965  
966          Thread t = newStartedThread(new CheckedRunnable() {
967 <            void realRun() throws InterruptedException {
967 >            public void realRun() throws InterruptedException {
968                  q.transfer(four);
969 <                threadAssertFalse(q.contains(four));
970 <                threadAssertEquals(three, q.poll());
969 >                assertFalse(q.contains(four));
970 >                assertSame(three, q.poll());
971              }});
972  
973 <        Thread.sleep(SHORT_DELAY_MS);
973 >        while (q.isEmpty())
974 >            Thread.yield();
975 >        assertFalse(q.isEmpty());
976 >        assertEquals(1, q.size());
977          assertTrue(q.offer(three));
978 <        assertEquals(four, q.poll());
979 <        t.join();
978 >        assertSame(four, q.poll());
979 >        awaitTermination(t, MEDIUM_DELAY_MS);
980      }
981  
982      /**
# Line 924 | Line 988 | public class LinkedTransferQueueTest ext
988              = new LinkedTransferQueue<Integer>();
989  
990          Thread t = newStartedThread(new CheckedRunnable() {
991 <            void realRun() throws InterruptedException {
992 <                q.transfer(SIZE);
993 <                threadAssertTrue(q.isEmpty());
991 >            public void realRun() throws InterruptedException {
992 >                q.transfer(four);
993 >                checkEmpty(q);
994              }});
995  
996 <        Thread.sleep(SHORT_DELAY_MS);
997 <        assertEquals(SIZE, (int) q.take());
998 <        assertTrue(q.isEmpty());
999 <        t.join();
996 >        while (q.isEmpty())
997 >            Thread.yield();
998 >        assertFalse(q.isEmpty());
999 >        assertEquals(1, q.size());
1000 >        assertSame(four, q.take());
1001 >        checkEmpty(q);
1002 >        awaitTermination(t, MEDIUM_DELAY_MS);
1003      }
1004  
1005      /**
# Line 943 | Line 1010 | public class LinkedTransferQueueTest ext
1010              final LinkedTransferQueue q = new LinkedTransferQueue();
1011              q.tryTransfer(null);
1012              shouldThrow();
1013 <        } catch (NullPointerException ex) {
947 <        }
1013 >        } catch (NullPointerException success) {}
1014      }
1015  
1016      /**
1017       * tryTransfer returns false and does not enqueue if there are no
1018       * consumers waiting to poll or take.
1019       */
1020 <    public void testTryTransfer2() {
1020 >    public void testTryTransfer2() throws InterruptedException {
1021          final LinkedTransferQueue q = new LinkedTransferQueue();
1022          assertFalse(q.tryTransfer(new Object()));
1023          assertFalse(q.hasWaitingConsumer());
1024 <        assertTrue(q.isEmpty());
959 <        assertEquals(0, q.size());
1024 >        checkEmpty(q);
1025      }
1026  
1027      /**
# Line 968 | Line 1033 | public class LinkedTransferQueueTest ext
1033          final LinkedTransferQueue q = new LinkedTransferQueue();
1034  
1035          Thread t = newStartedThread(new CheckedRunnable() {
1036 <            void realRun() {
1036 >            public void realRun() {
1037                  while (! q.hasWaitingConsumer())
1038                      Thread.yield();
1039 <                threadAssertTrue(q.hasWaitingConsumer());
1040 <                threadAssertTrue(q.isEmpty());
1041 <                threadAssertTrue(q.size() == 0);
977 <                threadAssertTrue(q.tryTransfer(hotPotato));
1039 >                assertTrue(q.hasWaitingConsumer());
1040 >                checkEmpty(q);
1041 >                assertTrue(q.tryTransfer(hotPotato));
1042              }});
1043  
1044 <        assertTrue(q.poll(MEDIUM_DELAY_MS, MILLISECONDS) == hotPotato);
1045 <        assertTrue(q.isEmpty());
1046 <        t.join();
1044 >        assertSame(hotPotato, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
1045 >        checkEmpty(q);
1046 >        awaitTermination(t, MEDIUM_DELAY_MS);
1047      }
1048  
1049      /**
# Line 991 | Line 1055 | public class LinkedTransferQueueTest ext
1055          final LinkedTransferQueue q = new LinkedTransferQueue();
1056  
1057          Thread t = newStartedThread(new CheckedRunnable() {
1058 <            void realRun() {
1058 >            public void realRun() {
1059                  while (! q.hasWaitingConsumer())
1060                      Thread.yield();
1061 <                threadAssertTrue(q.hasWaitingConsumer());
1062 <                threadAssertTrue(q.isEmpty());
1063 <                threadAssertTrue(q.size() == 0);
1000 <                threadAssertTrue(q.tryTransfer(hotPotato));
1061 >                assertTrue(q.hasWaitingConsumer());
1062 >                checkEmpty(q);
1063 >                assertTrue(q.tryTransfer(hotPotato));
1064              }});
1065  
1066 <        assertTrue(q.take() == hotPotato);
1067 <        assertTrue(q.isEmpty());
1068 <        t.join();
1066 >        assertSame(q.take(), hotPotato);
1067 >        checkEmpty(q);
1068 >        awaitTermination(t, MEDIUM_DELAY_MS);
1069      }
1070  
1071      /**
# Line 1011 | Line 1074 | public class LinkedTransferQueueTest ext
1074       */
1075      public void testTryTransfer5() throws InterruptedException {
1076          final LinkedTransferQueue q = new LinkedTransferQueue();
1077 +        final CountDownLatch threadStarted = new CountDownLatch(1);
1078  
1079 <        Thread toInterrupt = newStartedThread(new CheckedInterruptedRunnable() {
1080 <            void realRun() throws InterruptedException {
1081 <                q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS);
1079 >        Thread t = newStartedThread(new CheckedRunnable() {
1080 >            public void realRun() throws InterruptedException {
1081 >                long t0 = System.nanoTime();
1082 >                threadStarted.countDown();
1083 >                try {
1084 >                    q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS);
1085 >                    shouldThrow();
1086 >                } catch (InterruptedException success) {}
1087 >                assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
1088              }});
1089  
1090 <        Thread.sleep(SMALL_DELAY_MS);
1091 <        toInterrupt.interrupt();
1092 <        toInterrupt.join();
1090 >        threadStarted.await();
1091 >        Thread.sleep(SHORT_DELAY_MS);
1092 >        t.interrupt();
1093 >        awaitTermination(t, MEDIUM_DELAY_MS);
1094 >        checkEmpty(q);
1095      }
1096  
1097      /**
1098 <     * tryTransfer gives up after the timeout and return false
1098 >     * tryTransfer gives up after the timeout and returns false
1099       */
1100      public void testTryTransfer6() throws InterruptedException {
1101          final LinkedTransferQueue q = new LinkedTransferQueue();
1102  
1103          Thread t = newStartedThread(new CheckedRunnable() {
1104 <            void realRun() throws InterruptedException {
1105 <                threadAssertFalse
1106 <                    (q.tryTransfer(new Object(),
1107 <                                   SHORT_DELAY_MS, MILLISECONDS));
1104 >            public void realRun() throws InterruptedException {
1105 >                long t0 = System.nanoTime();
1106 >                assertFalse(q.tryTransfer(new Object(),
1107 >                                          SHORT_DELAY_MS, MILLISECONDS));
1108 >                assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
1109 >                checkEmpty(q);
1110              }});
1111  
1112 <        Thread.sleep(SMALL_DELAY_MS);
1113 <        assertTrue(q.isEmpty());
1040 <        t.join();
1112 >        awaitTermination(t, MEDIUM_DELAY_MS);
1113 >        checkEmpty(q);
1114      }
1115  
1116      /**
# Line 1049 | Line 1122 | public class LinkedTransferQueueTest ext
1122          assertTrue(q.offer(four));
1123  
1124          Thread t = newStartedThread(new CheckedRunnable() {
1125 <            void realRun() throws InterruptedException {
1126 <                threadAssertTrue(q.tryTransfer(five,
1127 <                                               MEDIUM_DELAY_MS, MILLISECONDS));
1055 <                threadAssertTrue(q.isEmpty());
1125 >            public void realRun() throws InterruptedException {
1126 >                assertTrue(q.tryTransfer(five, MEDIUM_DELAY_MS, MILLISECONDS));
1127 >                checkEmpty(q);
1128              }});
1129  
1130 <        Thread.sleep(SHORT_DELAY_MS);
1130 >        while (q.size() != 2)
1131 >            Thread.yield();
1132          assertEquals(2, q.size());
1133 <        assertEquals(four, q.poll());
1134 <        assertEquals(five, q.poll());
1135 <        assertTrue(q.isEmpty());
1136 <        t.join();
1133 >        assertSame(four, q.poll());
1134 >        assertSame(five, q.poll());
1135 >        checkEmpty(q);
1136 >        awaitTermination(t, MEDIUM_DELAY_MS);
1137      }
1138  
1139      /**
# Line 1071 | Line 1144 | public class LinkedTransferQueueTest ext
1144          final LinkedTransferQueue q = new LinkedTransferQueue();
1145          assertTrue(q.offer(four));
1146          assertEquals(1, q.size());
1147 +        long t0 = System.nanoTime();
1148          assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS));
1149 +        assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
1150          assertEquals(1, q.size());
1151 <        assertEquals(four, q.poll());
1077 <        threadAssertTrue(q.isEmpty());
1151 >        assertSame(four, q.poll());
1152          assertNull(q.poll());
1153 +        checkEmpty(q);
1154      }
1155  
1156      private LinkedTransferQueue<Integer> populatedQueue(int n) {
1157          LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
1158 <        assertTrue(q.isEmpty());
1158 >        checkEmpty(q);
1159          for (int i = 0; i < n; i++) {
1160              assertEquals(i, q.size());
1161              assertTrue(q.offer(i));
# Line 1089 | Line 1164 | public class LinkedTransferQueueTest ext
1164          assertFalse(q.isEmpty());
1165          return q;
1166      }
1092
1093    private static class ConsumerObserver {
1094
1095        private int waitingConsumers;
1096
1097        private ConsumerObserver() {
1098        }
1099
1100        private void setWaitingConsumer(int i) {
1101            this.waitingConsumers = i;
1102        }
1103
1104        private int getWaitingConsumers() {
1105            return waitingConsumers;
1106        }
1107    }
1167   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines