ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ArrayBlockingQueueTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ArrayBlockingQueueTest.java (file contents):
Revision 1.26 by jsr166, Sun Nov 22 18:57:16 2009 UTC vs.
Revision 1.43 by jsr166, Tue Mar 15 19:47:06 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 Andrew Wright, Jeffrey Hayes,
6   * Pat Fisher, Mike Judd.
7   */
# Line 14 | Line 14 | import static java.util.concurrent.TimeU
14   import java.io.*;
15  
16   public class ArrayBlockingQueueTest extends JSR166TestCase {
17 +
18 +    public static class Fair extends BlockingQueueTest {
19 +        protected BlockingQueue emptyCollection() {
20 +            return new ArrayBlockingQueue(20, true);
21 +        }
22 +    }
23 +
24 +    public static class NonFair extends BlockingQueueTest {
25 +        protected BlockingQueue emptyCollection() {
26 +            return new ArrayBlockingQueue(20, false);
27 +        }
28 +    }
29 +
30      public static void main(String[] args) {
31 <        junit.textui.TestRunner.run (suite());
31 >        junit.textui.TestRunner.run(suite());
32      }
33 +
34      public static Test suite() {
35 <        return new TestSuite(ArrayBlockingQueueTest.class);
35 >        return newTestSuite(ArrayBlockingQueueTest.class,
36 >                            new Fair().testSuite(),
37 >                            new NonFair().testSuite());
38      }
39  
40      /**
41       * Create a queue of given size containing consecutive
42       * Integers 0 ... n.
43       */
44 <    private ArrayBlockingQueue populatedQueue(int n) {
45 <        ArrayBlockingQueue q = new ArrayBlockingQueue(n);
44 >    private ArrayBlockingQueue<Integer> populatedQueue(int n) {
45 >        ArrayBlockingQueue<Integer> q = new ArrayBlockingQueue<Integer>(n);
46          assertTrue(q.isEmpty());
47          for (int i = 0; i < n; i++)
48              assertTrue(q.offer(new Integer(i)));
# Line 145 | Line 161 | public class ArrayBlockingQueueTest exte
161      }
162  
163      /**
164 <     *  offer(null) throws NPE
164 >     * offer(null) throws NPE
165       */
166      public void testOfferNull() {
167          try {
# Line 156 | Line 172 | public class ArrayBlockingQueueTest exte
172      }
173  
174      /**
175 <     *  add(null) throws NPE
175 >     * add(null) throws NPE
176       */
177      public void testAddNull() {
178          try {
# Line 191 | Line 207 | public class ArrayBlockingQueueTest exte
207      }
208  
209      /**
210 <     *  addAll(null) throws NPE
210 >     * addAll(null) throws NPE
211       */
212      public void testAddAll1() {
213          try {
# Line 214 | Line 230 | public class ArrayBlockingQueueTest exte
230  
231  
232      /**
233 <     *  addAll of a collection with null elements throws NPE
233 >     * addAll of a collection with null elements throws NPE
234       */
235      public void testAddAll2() {
236          try {
# Line 224 | Line 240 | public class ArrayBlockingQueueTest exte
240              shouldThrow();
241          } catch (NullPointerException success) {}
242      }
243 +
244      /**
245       * addAll of a collection with any null elements throws NPE after
246       * possibly adding some elements
# Line 238 | Line 255 | public class ArrayBlockingQueueTest exte
255              shouldThrow();
256          } catch (NullPointerException success) {}
257      }
258 +
259      /**
260       * addAll throws ISE if not enough room
261       */
# Line 251 | Line 269 | public class ArrayBlockingQueueTest exte
269              shouldThrow();
270          } catch (IllegalStateException success) {}
271      }
272 +
273      /**
274       * Queue contains all elements, in traversal order, of successful addAll
275       */
# Line 267 | Line 286 | public class ArrayBlockingQueueTest exte
286      }
287  
288      /**
289 <     *  put(null) throws NPE
289 >     * put(null) throws NPE
290       */
291      public void testPutNull() throws InterruptedException {
292          try {
# Line 319 | Line 338 | public class ArrayBlockingQueueTest exte
338       * put blocks waiting for take when full
339       */
340      public void testPutWithTake() throws InterruptedException {
341 <        final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
341 >        final int capacity = 2;
342 >        final ArrayBlockingQueue q = new ArrayBlockingQueue(capacity);
343          Thread t = new Thread(new CheckedRunnable() {
344 <            public void realRun() {
345 <                int added = 0;
344 >            public void realRun() throws InterruptedException {
345 >                for (int i = 0; i < capacity + 1; i++)
346 >                    q.put(i);
347                  try {
348 <                    q.put(new Object());
349 <                    ++added;
350 <                    q.put(new Object());
330 <                    ++added;
331 <                    q.put(new Object());
332 <                    ++added;
333 <                    q.put(new Object());
334 <                    ++added;
335 <                    threadShouldThrow();
336 <                } catch (InterruptedException success) {
337 <                    threadAssertTrue(added >= 2);
338 <                }
348 >                    q.put(99);
349 >                    shouldThrow();
350 >                } catch (InterruptedException success) {}
351              }});
352  
353          t.start();
354          Thread.sleep(SHORT_DELAY_MS);
355 <        q.take();
355 >        assertEquals(q.remainingCapacity(), 0);
356 >        assertEquals(0, q.take());
357 >        Thread.sleep(SHORT_DELAY_MS);
358          t.interrupt();
359          t.join();
360 +        assertEquals(q.remainingCapacity(), 0);
361      }
362  
363      /**
# Line 378 | Line 393 | public class ArrayBlockingQueueTest exte
393      }
394  
395      /**
381     * take blocks interruptibly when empty
382     */
383    public void testTakeFromEmpty() throws InterruptedException {
384        final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
385        Thread t = new ThreadShouldThrow(InterruptedException.class) {
386            public void realRun() throws InterruptedException {
387                q.take();
388            }};
389
390        t.start();
391        Thread.sleep(SHORT_DELAY_MS);
392        t.interrupt();
393        t.join();
394    }
395
396    /**
396       * Take removes existing elements until empty, then blocks interruptibly
397       */
398      public void testBlockingTake() throws InterruptedException {
# Line 428 | Line 427 | public class ArrayBlockingQueueTest exte
427      }
428  
429      /**
430 <     * timed pool with zero timeout succeeds when non-empty, else times out
430 >     * timed poll with zero timeout succeeds when non-empty, else times out
431       */
432      public void testTimedPoll0() throws InterruptedException {
433          ArrayBlockingQueue q = populatedQueue(SIZE);
# Line 439 | Line 438 | public class ArrayBlockingQueueTest exte
438      }
439  
440      /**
441 <     * timed pool with nonzero timeout succeeds when non-empty, else times out
441 >     * timed poll with nonzero timeout succeeds when non-empty, else times out
442       */
443      public void testTimedPoll() throws InterruptedException {
444          ArrayBlockingQueue q = populatedQueue(SIZE);
# Line 454 | Line 453 | public class ArrayBlockingQueueTest exte
453       * returning timeout status
454       */
455      public void testInterruptedTimedPoll() throws InterruptedException {
456 <        Thread t = new Thread(new CheckedRunnable() {
456 >        final BlockingQueue<Integer> q = populatedQueue(SIZE);
457 >        final CountDownLatch aboutToWait = new CountDownLatch(1);
458 >        Thread t = newStartedThread(new CheckedRunnable() {
459              public void realRun() throws InterruptedException {
459                ArrayBlockingQueue q = populatedQueue(SIZE);
460                  for (int i = 0; i < SIZE; ++i) {
461 <                    assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));;
461 >                    long t0 = System.nanoTime();
462 >                    assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
463 >                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
464                  }
465 +                long t0 = System.nanoTime();
466 +                aboutToWait.countDown();
467                  try {
468 <                    q.poll(SMALL_DELAY_MS, MILLISECONDS);
465 <                    shouldThrow();
466 <                } catch (InterruptedException success) {}
467 <            }});
468 <
469 <        t.start();
470 <        Thread.sleep(SHORT_DELAY_MS);
471 <        t.interrupt();
472 <        t.join();
473 <    }
474 <
475 <    /**
476 <     *  timed poll before a delayed offer fails; after offer succeeds;
477 <     *  on interruption throws
478 <     */
479 <    public void testTimedPollWithOffer() throws InterruptedException {
480 <        final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
481 <        Thread t = new Thread(new CheckedRunnable() {
482 <            public void realRun() throws InterruptedException {
483 <                assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
484 <                assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
485 <                try {
486 <                    q.poll(LONG_DELAY_MS, MILLISECONDS);
468 >                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
469                      shouldThrow();
470 <                } catch (InterruptedException success) {}
470 >                } catch (InterruptedException success) {
471 >                    assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
472 >                }
473              }});
474  
475 <        t.start();
476 <        Thread.sleep(SMALL_DELAY_MS);
493 <        assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
475 >        aboutToWait.await();
476 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
477          t.interrupt();
478 <        t.join();
478 >        awaitTermination(t, MEDIUM_DELAY_MS);
479 >        checkEmpty(q);
480      }
481  
498
482      /**
483       * peek returns next element, or null if empty
484       */
# Line 632 | Line 615 | public class ArrayBlockingQueueTest exte
615      }
616  
617      /**
618 <     *  toArray contains all elements
618 >     * toArray contains all elements in FIFO order
619       */
620 <    public void testToArray() throws InterruptedException {
620 >    public void testToArray() {
621          ArrayBlockingQueue q = populatedQueue(SIZE);
622          Object[] o = q.toArray();
623          for (int i = 0; i < o.length; i++)
624 <            assertEquals(o[i], q.take());
624 >            assertSame(o[i], q.poll());
625      }
626  
627      /**
628 <     * toArray(a) contains all elements
628 >     * toArray(a) contains all elements in FIFO order
629       */
630 <    public void testToArray2() throws InterruptedException {
631 <        ArrayBlockingQueue q = populatedQueue(SIZE);
630 >    public void testToArray2() {
631 >        ArrayBlockingQueue<Integer> q = populatedQueue(SIZE);
632          Integer[] ints = new Integer[SIZE];
633 <        ints = (Integer[])q.toArray(ints);
633 >        Integer[] array = q.toArray(ints);
634 >        assertSame(ints, array);
635          for (int i = 0; i < ints.length; i++)
636 <            assertEquals(ints[i], q.take());
636 >            assertSame(ints[i], q.poll());
637      }
638  
639      /**
640 <     * toArray(null) throws NPE
640 >     * toArray(null) throws NullPointerException
641       */
642 <    public void testToArray_BadArg() {
642 >    public void testToArray_NullArg() {
643          ArrayBlockingQueue q = populatedQueue(SIZE);
644          try {
645 <            Object o[] = q.toArray(null);
645 >            q.toArray(null);
646              shouldThrow();
647          } catch (NullPointerException success) {}
648      }
649  
650      /**
651 <     * toArray with incompatible array type throws CCE
651 >     * toArray(incompatible array type) throws ArrayStoreException
652       */
653      public void testToArray1_BadArg() {
654          ArrayBlockingQueue q = populatedQueue(SIZE);
655          try {
656 <            Object o[] = q.toArray(new String[10]);
656 >            q.toArray(new String[10]);
657              shouldThrow();
658          } catch (ArrayStoreException success) {}
659      }
# Line 689 | Line 673 | public class ArrayBlockingQueueTest exte
673      /**
674       * iterator.remove removes current element
675       */
676 <    public void testIteratorRemove () {
676 >    public void testIteratorRemove() {
677          final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
678          q.add(two);
679          q.add(one);
# Line 700 | Line 684 | public class ArrayBlockingQueueTest exte
684          it.remove();
685  
686          it = q.iterator();
687 <        assertEquals(it.next(), one);
688 <        assertEquals(it.next(), three);
687 >        assertSame(it.next(), one);
688 >        assertSame(it.next(), three);
689          assertFalse(it.hasNext());
690      }
691  
# Line 726 | Line 710 | public class ArrayBlockingQueueTest exte
710      /**
711       * Modifications do not cause iterators to fail
712       */
713 <    public void testWeaklyConsistentIteration () {
713 >    public void testWeaklyConsistentIteration() {
714          final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
715          q.add(one);
716          q.add(two);
# Line 761 | Line 745 | public class ArrayBlockingQueueTest exte
745          ExecutorService executor = Executors.newFixedThreadPool(2);
746          executor.execute(new CheckedRunnable() {
747              public void realRun() throws InterruptedException {
748 <                threadAssertFalse(q.offer(three));
749 <                threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
750 <                threadAssertEquals(0, q.remainingCapacity());
748 >                assertFalse(q.offer(three));
749 >                assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
750 >                assertEquals(0, q.remainingCapacity());
751              }});
752  
753          executor.execute(new CheckedRunnable() {
754              public void realRun() throws InterruptedException {
755                  Thread.sleep(SMALL_DELAY_MS);
756 <                threadAssertEquals(one, q.take());
756 >                assertSame(one, q.take());
757              }});
758  
759          joinPool(executor);
776
760      }
761  
762      /**
# Line 784 | Line 767 | public class ArrayBlockingQueueTest exte
767          ExecutorService executor = Executors.newFixedThreadPool(2);
768          executor.execute(new CheckedRunnable() {
769              public void realRun() throws InterruptedException {
770 <                threadAssertNull(q.poll());
771 <                threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
772 <                threadAssertTrue(q.isEmpty());
770 >                assertNull(q.poll());
771 >                assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
772 >                assertTrue(q.isEmpty());
773              }});
774  
775          executor.execute(new CheckedRunnable() {
# Line 906 | Line 889 | public class ArrayBlockingQueueTest exte
889      }
890  
891      /**
892 <     * drainTo(c, n) empties first max {n, size} elements of queue into c
892 >     * drainTo(c, n) empties first min(n, size) elements of queue into c
893       */
894      public void testDrainToN() {
895          ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE*2);
# Line 915 | Line 898 | public class ArrayBlockingQueueTest exte
898                  assertTrue(q.offer(new Integer(j)));
899              ArrayList l = new ArrayList();
900              q.drainTo(l, i);
901 <            int k = (i < SIZE)? i : SIZE;
901 >            int k = (i < SIZE) ? i : SIZE;
902              assertEquals(l.size(), k);
903              assertEquals(q.size(), SIZE-k);
904              for (int j = 0; j < k; ++j)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines