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.23 by jsr166, Sat Nov 21 21:00:34 2009 UTC vs.
Revision 1.46 by jsr166, Sat May 21 06:24:33 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 275 | Line 294 | public class ArrayBlockingQueueTest exte
294              q.put(null);
295              shouldThrow();
296          } catch (NullPointerException success) {}
297 <     }
297 >    }
298  
299      /**
300       * all elements successfully put are contained
# Line 296 | Line 315 | public class ArrayBlockingQueueTest exte
315      public void testBlockingPut() throws InterruptedException {
316          final ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
317          Thread t = new Thread(new CheckedRunnable() {
318 <            public void realRun() {
319 <                int added = 0;
318 >            public void realRun() throws InterruptedException {
319 >                for (int i = 0; i < SIZE; ++i)
320 >                    q.put(i);
321 >                assertEquals(SIZE, q.size());
322 >                assertEquals(0, q.remainingCapacity());
323                  try {
324 <                    for (int i = 0; i < SIZE; ++i) {
325 <                        q.put(new Integer(i));
326 <                        ++added;
327 <                    }
306 <                    q.put(new Integer(SIZE));
307 <                    threadShouldThrow();
308 <                } catch (InterruptedException success) {
309 <                    threadAssertEquals(added, SIZE);
310 <                }}});
324 >                    q.put(99);
325 >                    shouldThrow();
326 >                } catch (InterruptedException success) {}
327 >            }});
328  
329          t.start();
330 <        Thread.sleep(MEDIUM_DELAY_MS);
330 >        delay(SHORT_DELAY_MS);
331          t.interrupt();
332          t.join();
333 +        assertEquals(SIZE, q.size());
334 +        assertEquals(0, q.remainingCapacity());
335      }
336  
337      /**
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();
354 >        delay(SHORT_DELAY_MS);
355 >        assertEquals(q.remainingCapacity(), 0);
356 >        assertEquals(0, q.take());
357 >        delay(SHORT_DELAY_MS);
358          t.interrupt();
359          t.join();
360 +        assertEquals(q.remainingCapacity(), 0);
361      }
362  
363      /**
# Line 350 | Line 365 | public class ArrayBlockingQueueTest exte
365       */
366      public void testTimedOffer() throws InterruptedException {
367          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
368 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
368 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
369 >        Thread t = newStartedThread(new CheckedRunnable() {
370              public void realRun() throws InterruptedException {
371                  q.put(new Object());
372                  q.put(new Object());
373 <                threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, MILLISECONDS));
374 <                q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
375 <            }};
373 >                long startTime = System.nanoTime();
374 >                assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS));
375 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
376 >                pleaseInterrupt.countDown();
377 >                try {
378 >                    q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
379 >                    shouldThrow();
380 >                } catch (InterruptedException success) {}
381 >            }});
382  
383 <        t.start();
362 <        Thread.sleep(SHORT_DELAY_MS);
383 >        await(pleaseInterrupt);
384          t.interrupt();
385 <        t.join();
385 >        awaitTermination(t);
386      }
387  
388      /**
# Line 370 | Line 391 | public class ArrayBlockingQueueTest exte
391      public void testTake() throws InterruptedException {
392          ArrayBlockingQueue q = populatedQueue(SIZE);
393          for (int i = 0; i < SIZE; ++i) {
394 <            assertEquals(i, ((Integer)q.take()).intValue());
394 >            assertEquals(i, q.take());
395          }
396      }
397  
398      /**
378     * take blocks interruptibly when empty
379     */
380    public void testTakeFromEmpty() throws InterruptedException {
381        final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
382        Thread t = new ThreadShouldThrow(InterruptedException.class) {
383            public void realRun() throws InterruptedException {
384                q.take();
385            }};
386
387        t.start();
388        Thread.sleep(SHORT_DELAY_MS);
389        t.interrupt();
390        t.join();
391    }
392
393    /**
399       * Take removes existing elements until empty, then blocks interruptibly
400       */
401      public void testBlockingTake() throws InterruptedException {
402 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
402 >        final ArrayBlockingQueue q = populatedQueue(SIZE);
403 >        Thread t = new Thread(new CheckedRunnable() {
404              public void realRun() throws InterruptedException {
399                ArrayBlockingQueue q = populatedQueue(SIZE);
405                  for (int i = 0; i < SIZE; ++i) {
406 <                    threadAssertEquals(i, ((Integer)q.take()).intValue());
406 >                    assertEquals(i, q.take());
407                  }
408 <                q.take();
409 <            }};
408 >                try {
409 >                    q.take();
410 >                    shouldThrow();
411 >                } catch (InterruptedException success) {}
412 >            }});
413  
414          t.start();
415 <            Thread.sleep(SHORT_DELAY_MS);
416 <            t.interrupt();
417 <            t.join();
415 >        delay(SHORT_DELAY_MS);
416 >        t.interrupt();
417 >        t.join();
418      }
419  
420  
# Line 416 | Line 424 | public class ArrayBlockingQueueTest exte
424      public void testPoll() {
425          ArrayBlockingQueue q = populatedQueue(SIZE);
426          for (int i = 0; i < SIZE; ++i) {
427 <            assertEquals(i, ((Integer)q.poll()).intValue());
427 >            assertEquals(i, q.poll());
428          }
429          assertNull(q.poll());
430      }
431  
432      /**
433 <     * timed pool with zero timeout succeeds when non-empty, else times out
433 >     * timed poll with zero timeout succeeds when non-empty, else times out
434       */
435      public void testTimedPoll0() throws InterruptedException {
436          ArrayBlockingQueue q = populatedQueue(SIZE);
437          for (int i = 0; i < SIZE; ++i) {
438 <            assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue());
438 >            assertEquals(i, q.poll(0, MILLISECONDS));
439          }
440          assertNull(q.poll(0, MILLISECONDS));
441      }
442  
443      /**
444 <     * timed pool with nonzero timeout succeeds when non-empty, else times out
444 >     * timed poll with nonzero timeout succeeds when non-empty, else times out
445       */
446      public void testTimedPoll() throws InterruptedException {
447          ArrayBlockingQueue q = populatedQueue(SIZE);
448          for (int i = 0; i < SIZE; ++i) {
449 <            assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
449 >            assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
450          }
451          assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
452      }
# Line 448 | Line 456 | public class ArrayBlockingQueueTest exte
456       * returning timeout status
457       */
458      public void testInterruptedTimedPoll() throws InterruptedException {
459 <        Thread t = new Thread(new CheckedRunnable() {
459 >        final BlockingQueue<Integer> q = populatedQueue(SIZE);
460 >        final CountDownLatch aboutToWait = new CountDownLatch(1);
461 >        Thread t = newStartedThread(new CheckedRunnable() {
462              public void realRun() throws InterruptedException {
453                ArrayBlockingQueue q = populatedQueue(SIZE);
463                  for (int i = 0; i < SIZE; ++i) {
464 <                    assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
464 >                    long t0 = System.nanoTime();
465 >                    assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
466 >                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
467                  }
468 +                long t0 = System.nanoTime();
469 +                aboutToWait.countDown();
470                  try {
471 <                    q.poll(SMALL_DELAY_MS, MILLISECONDS);
459 <                    shouldThrow();
460 <                } catch (InterruptedException success) {}
461 <            }});
462 <
463 <        t.start();
464 <        Thread.sleep(SHORT_DELAY_MS);
465 <        t.interrupt();
466 <        t.join();
467 <    }
468 <
469 <    /**
470 <     *  timed poll before a delayed offer fails; after offer succeeds;
471 <     *  on interruption throws
472 <     */
473 <    public void testTimedPollWithOffer() throws InterruptedException {
474 <        final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
475 <        Thread t = new Thread(new CheckedRunnable() {
476 <            public void realRun() throws InterruptedException {
477 <                assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
478 <                assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
479 <                try {
480 <                    q.poll(LONG_DELAY_MS, MILLISECONDS);
471 >                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
472                      shouldThrow();
473 <                } catch (InterruptedException success) {}
473 >                } catch (InterruptedException success) {
474 >                    assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
475 >                }
476              }});
477  
478 <        t.start();
479 <        Thread.sleep(SMALL_DELAY_MS);
487 <        assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
478 >        aboutToWait.await();
479 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
480          t.interrupt();
481 <        t.join();
481 >        awaitTermination(t, MEDIUM_DELAY_MS);
482 >        checkEmpty(q);
483      }
484  
492
485      /**
486       * peek returns next element, or null if empty
487       */
488      public void testPeek() {
489          ArrayBlockingQueue q = populatedQueue(SIZE);
490          for (int i = 0; i < SIZE; ++i) {
491 <            assertEquals(i, ((Integer)q.peek()).intValue());
492 <            q.poll();
491 >            assertEquals(i, q.peek());
492 >            assertEquals(i, q.poll());
493              assertTrue(q.peek() == null ||
494 <                       i != ((Integer)q.peek()).intValue());
494 >                       !q.peek().equals(i));
495          }
496          assertNull(q.peek());
497      }
# Line 510 | Line 502 | public class ArrayBlockingQueueTest exte
502      public void testElement() {
503          ArrayBlockingQueue q = populatedQueue(SIZE);
504          for (int i = 0; i < SIZE; ++i) {
505 <            assertEquals(i, ((Integer)q.element()).intValue());
506 <            q.poll();
505 >            assertEquals(i, q.element());
506 >            assertEquals(i, q.poll());
507          }
508          try {
509              q.element();
# Line 525 | Line 517 | public class ArrayBlockingQueueTest exte
517      public void testRemove() {
518          ArrayBlockingQueue q = populatedQueue(SIZE);
519          for (int i = 0; i < SIZE; ++i) {
520 <            assertEquals(i, ((Integer)q.remove()).intValue());
520 >            assertEquals(i, q.remove());
521          }
522          try {
523              q.remove();
# Line 555 | Line 547 | public class ArrayBlockingQueueTest exte
547          ArrayBlockingQueue q = populatedQueue(SIZE);
548          for (int i = 0; i < SIZE; ++i) {
549              assertTrue(q.contains(new Integer(i)));
550 <            q.poll();
550 >            assertEquals(i, q.poll());
551              assertFalse(q.contains(new Integer(i)));
552          }
553      }
# Line 626 | Line 618 | public class ArrayBlockingQueueTest exte
618      }
619  
620      /**
621 <     *  toArray contains all elements
621 >     * toArray contains all elements in FIFO order
622       */
623 <    public void testToArray() throws InterruptedException {
623 >    public void testToArray() {
624          ArrayBlockingQueue q = populatedQueue(SIZE);
625          Object[] o = q.toArray();
626          for (int i = 0; i < o.length; i++)
627 <            assertEquals(o[i], q.take());
627 >            assertSame(o[i], q.poll());
628      }
629  
630      /**
631 <     * toArray(a) contains all elements
631 >     * toArray(a) contains all elements in FIFO order
632       */
633 <    public void testToArray2() throws InterruptedException {
634 <        ArrayBlockingQueue q = populatedQueue(SIZE);
633 >    public void testToArray2() {
634 >        ArrayBlockingQueue<Integer> q = populatedQueue(SIZE);
635          Integer[] ints = new Integer[SIZE];
636 <        ints = (Integer[])q.toArray(ints);
636 >        Integer[] array = q.toArray(ints);
637 >        assertSame(ints, array);
638          for (int i = 0; i < ints.length; i++)
639 <            assertEquals(ints[i], q.take());
639 >            assertSame(ints[i], q.poll());
640      }
641  
642      /**
643 <     * toArray(null) throws NPE
643 >     * toArray(null) throws NullPointerException
644       */
645 <    public void testToArray_BadArg() {
645 >    public void testToArray_NullArg() {
646 >        ArrayBlockingQueue q = populatedQueue(SIZE);
647          try {
648 <            ArrayBlockingQueue q = populatedQueue(SIZE);
655 <            Object o[] = q.toArray(null);
648 >            q.toArray(null);
649              shouldThrow();
650          } catch (NullPointerException success) {}
651      }
652  
653      /**
654 <     * toArray with incompatible array type throws CCE
654 >     * toArray(incompatible array type) throws ArrayStoreException
655       */
656      public void testToArray1_BadArg() {
657 +        ArrayBlockingQueue q = populatedQueue(SIZE);
658          try {
659 <            ArrayBlockingQueue q = populatedQueue(SIZE);
666 <            Object o[] = q.toArray(new String[10] );
659 >            q.toArray(new String[10]);
660              shouldThrow();
661          } catch (ArrayStoreException success) {}
662      }
# Line 683 | Line 676 | public class ArrayBlockingQueueTest exte
676      /**
677       * iterator.remove removes current element
678       */
679 <    public void testIteratorRemove () {
679 >    public void testIteratorRemove() {
680          final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
681          q.add(two);
682          q.add(one);
# Line 694 | Line 687 | public class ArrayBlockingQueueTest exte
687          it.remove();
688  
689          it = q.iterator();
690 <        assertEquals(it.next(), one);
691 <        assertEquals(it.next(), three);
690 >        assertSame(it.next(), one);
691 >        assertSame(it.next(), three);
692          assertFalse(it.hasNext());
693      }
694  
# Line 712 | Line 705 | public class ArrayBlockingQueueTest exte
705  
706          int k = 0;
707          for (Iterator it = q.iterator(); it.hasNext();) {
708 <            int i = ((Integer)(it.next())).intValue();
716 <            assertEquals(++k, i);
708 >            assertEquals(++k, it.next());
709          }
710          assertEquals(3, k);
711      }
# Line 721 | Line 713 | public class ArrayBlockingQueueTest exte
713      /**
714       * Modifications do not cause iterators to fail
715       */
716 <    public void testWeaklyConsistentIteration () {
716 >    public void testWeaklyConsistentIteration() {
717          final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
718          q.add(one);
719          q.add(two);
# Line 756 | Line 748 | public class ArrayBlockingQueueTest exte
748          ExecutorService executor = Executors.newFixedThreadPool(2);
749          executor.execute(new CheckedRunnable() {
750              public void realRun() throws InterruptedException {
751 <                threadAssertFalse(q.offer(three));
752 <                threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
753 <                threadAssertEquals(0, q.remainingCapacity());
751 >                assertFalse(q.offer(three));
752 >                assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
753 >                assertEquals(0, q.remainingCapacity());
754              }});
755  
756          executor.execute(new CheckedRunnable() {
757              public void realRun() throws InterruptedException {
758 <                Thread.sleep(SMALL_DELAY_MS);
759 <                threadAssertEquals(one, q.take());
758 >                delay(SMALL_DELAY_MS);
759 >                assertSame(one, q.take());
760              }});
761  
762          joinPool(executor);
771
763      }
764  
765      /**
# Line 779 | Line 770 | public class ArrayBlockingQueueTest exte
770          ExecutorService executor = Executors.newFixedThreadPool(2);
771          executor.execute(new CheckedRunnable() {
772              public void realRun() throws InterruptedException {
773 <                threadAssertNull(q.poll());
774 <                threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
775 <                threadAssertTrue(q.isEmpty());
773 >                assertNull(q.poll());
774 >                assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
775 >                assertTrue(q.isEmpty());
776              }});
777  
778          executor.execute(new CheckedRunnable() {
779              public void realRun() throws InterruptedException {
780 <                Thread.sleep(SMALL_DELAY_MS);
780 >                delay(SMALL_DELAY_MS);
781                  q.put(one);
782              }});
783  
# Line 901 | Line 892 | public class ArrayBlockingQueueTest exte
892      }
893  
894      /**
895 <     * drainTo(c, n) empties first max {n, size} elements of queue into c
895 >     * drainTo(c, n) empties first min(n, size) elements of queue into c
896       */
897      public void testDrainToN() {
898          ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE*2);
# Line 910 | Line 901 | public class ArrayBlockingQueueTest exte
901                  assertTrue(q.offer(new Integer(j)));
902              ArrayList l = new ArrayList();
903              q.drainTo(l, i);
904 <            int k = (i < SIZE)? i : SIZE;
904 >            int k = (i < SIZE) ? i : SIZE;
905              assertEquals(l.size(), k);
906              assertEquals(q.size(), SIZE-k);
907              for (int j = 0; j < k; ++j)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines