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.18 by jsr166, Sat Nov 21 10:25:05 2009 UTC vs.
Revision 1.38 by jsr166, Wed Nov 3 07:54:52 2010 UTC

# 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      /**
# Line 104 | Line 120 | public class ArrayBlockingQueueTest exte
120       * Queue contains all elements of collection used to initialize
121       */
122      public void testConstructor7() {
123 <        try {
124 <            Integer[] ints = new Integer[SIZE];
125 <            for (int i = 0; i < SIZE; ++i)
126 <                ints[i] = new Integer(i);
127 <            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, true, Arrays.asList(ints));
128 <            for (int i = 0; i < SIZE; ++i)
113 <                assertEquals(ints[i], q.poll());
114 <        }
115 <        finally {}
123 >        Integer[] ints = new Integer[SIZE];
124 >        for (int i = 0; i < SIZE; ++i)
125 >            ints[i] = new Integer(i);
126 >        ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, true, Arrays.asList(ints));
127 >        for (int i = 0; i < SIZE; ++i)
128 >            assertEquals(ints[i], q.poll());
129      }
130  
131      /**
# Line 148 | 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 159 | 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 194 | 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 217 | 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 227 | 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 241 | 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 254 | 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 270 | 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 299 | 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 <                    }
309 <                    q.put(new Integer(SIZE));
310 <                    threadShouldThrow();
311 <                } catch (InterruptedException ie) {
312 <                    threadAssertEquals(added, SIZE);
313 <                }}});
324 >                    q.put(99);
325 >                    shouldThrow();
326 >                } catch (InterruptedException success) {}
327 >            }});
328  
329          t.start();
330 <        Thread.sleep(MEDIUM_DELAY_MS);
330 >        Thread.sleep(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());
333 <                    ++added;
334 <                    q.put(new Object());
335 <                    ++added;
336 <                    q.put(new Object());
337 <                    ++added;
338 <                    threadShouldThrow();
339 <                } catch (InterruptedException e) {
340 <                    threadAssertTrue(added >= 2);
341 <                }
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 353 | 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 >        Thread t = new Thread(new CheckedRunnable() {
369              public void realRun() throws InterruptedException {
370                  q.put(new Object());
371                  q.put(new Object());
372 <                threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, MILLISECONDS));
373 <                q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
374 <            }};
372 >                assertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, MILLISECONDS));
373 >                try {
374 >                    q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
375 >                    shouldThrow();
376 >                } catch (InterruptedException success) {}
377 >            }});
378  
379          t.start();
380          Thread.sleep(SHORT_DELAY_MS);
# Line 373 | Line 388 | public class ArrayBlockingQueueTest exte
388      public void testTake() throws InterruptedException {
389          ArrayBlockingQueue q = populatedQueue(SIZE);
390          for (int i = 0; i < SIZE; ++i) {
391 <            assertEquals(i, ((Integer)q.take()).intValue());
391 >            assertEquals(i, q.take());
392          }
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 {
399 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
399 >        final ArrayBlockingQueue q = populatedQueue(SIZE);
400 >        Thread t = new Thread(new CheckedRunnable() {
401              public void realRun() throws InterruptedException {
402                ArrayBlockingQueue q = populatedQueue(SIZE);
402                  for (int i = 0; i < SIZE; ++i) {
403 <                    threadAssertEquals(i, ((Integer)q.take()).intValue());
403 >                    assertEquals(i, q.take());
404                  }
405 <                q.take();
406 <            }};
405 >                try {
406 >                    q.take();
407 >                    shouldThrow();
408 >                } catch (InterruptedException success) {}
409 >            }});
410  
411          t.start();
412 <            Thread.sleep(SHORT_DELAY_MS);
413 <            t.interrupt();
414 <            t.join();
412 >        Thread.sleep(SHORT_DELAY_MS);
413 >        t.interrupt();
414 >        t.join();
415      }
416  
417  
# Line 419 | Line 421 | public class ArrayBlockingQueueTest exte
421      public void testPoll() {
422          ArrayBlockingQueue q = populatedQueue(SIZE);
423          for (int i = 0; i < SIZE; ++i) {
424 <            assertEquals(i, ((Integer)q.poll()).intValue());
424 >            assertEquals(i, q.poll());
425          }
426          assertNull(q.poll());
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);
434          for (int i = 0; i < SIZE; ++i) {
435 <            assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue());
435 >            assertEquals(i, q.poll(0, MILLISECONDS));
436          }
437          assertNull(q.poll(0, MILLISECONDS));
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);
445          for (int i = 0; i < SIZE; ++i) {
446 <            assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
446 >            assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
447          }
448          assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
449      }
# Line 451 | Line 453 | public class ArrayBlockingQueueTest exte
453       * returning timeout status
454       */
455      public void testInterruptedTimedPoll() throws InterruptedException {
456 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
456 >        Thread t = new Thread(new CheckedRunnable() {
457              public void realRun() throws InterruptedException {
458                  ArrayBlockingQueue q = populatedQueue(SIZE);
459                  for (int i = 0; i < SIZE; ++i) {
460 <                    threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
460 >                    assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));;
461                  }
462 <                q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
463 <            }};
464 <
465 <        t.start();
466 <        Thread.sleep(SMALL_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 ThreadShouldThrow(InterruptedException.class) {
476 <            public void realRun() throws InterruptedException {
477 <                threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
478 <                q.poll(LONG_DELAY_MS, MILLISECONDS);
479 <                q.poll(LONG_DELAY_MS, MILLISECONDS);
480 <            }};
462 >                try {
463 >                    q.poll(SMALL_DELAY_MS, MILLISECONDS);
464 >                    shouldThrow();
465 >                } catch (InterruptedException success) {}
466 >            }});
467  
468          t.start();
469 <        Thread.sleep(SMALL_DELAY_MS);
484 <        assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
469 >        Thread.sleep(SHORT_DELAY_MS);
470          t.interrupt();
471          t.join();
472      }
473  
489
474      /**
475       * peek returns next element, or null if empty
476       */
477      public void testPeek() {
478          ArrayBlockingQueue q = populatedQueue(SIZE);
479          for (int i = 0; i < SIZE; ++i) {
480 <            assertEquals(i, ((Integer)q.peek()).intValue());
481 <            q.poll();
480 >            assertEquals(i, q.peek());
481 >            assertEquals(i, q.poll());
482              assertTrue(q.peek() == null ||
483 <                       i != ((Integer)q.peek()).intValue());
483 >                       !q.peek().equals(i));
484          }
485          assertNull(q.peek());
486      }
# Line 507 | Line 491 | public class ArrayBlockingQueueTest exte
491      public void testElement() {
492          ArrayBlockingQueue q = populatedQueue(SIZE);
493          for (int i = 0; i < SIZE; ++i) {
494 <            assertEquals(i, ((Integer)q.element()).intValue());
495 <            q.poll();
494 >            assertEquals(i, q.element());
495 >            assertEquals(i, q.poll());
496          }
497          try {
498              q.element();
# Line 522 | Line 506 | public class ArrayBlockingQueueTest exte
506      public void testRemove() {
507          ArrayBlockingQueue q = populatedQueue(SIZE);
508          for (int i = 0; i < SIZE; ++i) {
509 <            assertEquals(i, ((Integer)q.remove()).intValue());
509 >            assertEquals(i, q.remove());
510          }
511          try {
512              q.remove();
# Line 552 | Line 536 | public class ArrayBlockingQueueTest exte
536          ArrayBlockingQueue q = populatedQueue(SIZE);
537          for (int i = 0; i < SIZE; ++i) {
538              assertTrue(q.contains(new Integer(i)));
539 <            q.poll();
539 >            assertEquals(i, q.poll());
540              assertFalse(q.contains(new Integer(i)));
541          }
542      }
# Line 623 | Line 607 | public class ArrayBlockingQueueTest exte
607      }
608  
609      /**
610 <     *  toArray contains all elements
610 >     * toArray contains all elements
611       */
612      public void testToArray() throws InterruptedException {
613          ArrayBlockingQueue q = populatedQueue(SIZE);
# Line 647 | Line 631 | public class ArrayBlockingQueueTest exte
631       * toArray(null) throws NPE
632       */
633      public void testToArray_BadArg() {
634 +        ArrayBlockingQueue q = populatedQueue(SIZE);
635          try {
651            ArrayBlockingQueue q = populatedQueue(SIZE);
636              Object o[] = q.toArray(null);
637              shouldThrow();
638          } catch (NullPointerException success) {}
639      }
640  
641      /**
642 <     * toArray with incompatible array type throws CCE
642 >     * toArray(incompatible array type) throws ArrayStoreException
643       */
644      public void testToArray1_BadArg() {
645 +        ArrayBlockingQueue q = populatedQueue(SIZE);
646          try {
647 <            ArrayBlockingQueue q = populatedQueue(SIZE);
663 <            Object o[] = q.toArray(new String[10] );
647 >            q.toArray(new String[10]);
648              shouldThrow();
649          } catch (ArrayStoreException success) {}
650      }
# Line 680 | Line 664 | public class ArrayBlockingQueueTest exte
664      /**
665       * iterator.remove removes current element
666       */
667 <    public void testIteratorRemove () {
667 >    public void testIteratorRemove() {
668          final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
669          q.add(two);
670          q.add(one);
# Line 691 | Line 675 | public class ArrayBlockingQueueTest exte
675          it.remove();
676  
677          it = q.iterator();
678 <        assertEquals(it.next(), one);
679 <        assertEquals(it.next(), three);
678 >        assertSame(it.next(), one);
679 >        assertSame(it.next(), three);
680          assertFalse(it.hasNext());
681      }
682  
# Line 709 | Line 693 | public class ArrayBlockingQueueTest exte
693  
694          int k = 0;
695          for (Iterator it = q.iterator(); it.hasNext();) {
696 <            int i = ((Integer)(it.next())).intValue();
713 <            assertEquals(++k, i);
696 >            assertEquals(++k, it.next());
697          }
698          assertEquals(3, k);
699      }
# Line 718 | Line 701 | public class ArrayBlockingQueueTest exte
701      /**
702       * Modifications do not cause iterators to fail
703       */
704 <    public void testWeaklyConsistentIteration () {
704 >    public void testWeaklyConsistentIteration() {
705          final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
706          q.add(one);
707          q.add(two);
# Line 753 | Line 736 | public class ArrayBlockingQueueTest exte
736          ExecutorService executor = Executors.newFixedThreadPool(2);
737          executor.execute(new CheckedRunnable() {
738              public void realRun() throws InterruptedException {
739 <                threadAssertFalse(q.offer(three));
740 <                threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
741 <                threadAssertEquals(0, q.remainingCapacity());
739 >                assertFalse(q.offer(three));
740 >                assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
741 >                assertEquals(0, q.remainingCapacity());
742              }});
743  
744          executor.execute(new CheckedRunnable() {
745              public void realRun() throws InterruptedException {
746                  Thread.sleep(SMALL_DELAY_MS);
747 <                threadAssertEquals(one, q.take());
747 >                assertSame(one, q.take());
748              }});
749  
750          joinPool(executor);
768
751      }
752  
753      /**
# Line 776 | Line 758 | public class ArrayBlockingQueueTest exte
758          ExecutorService executor = Executors.newFixedThreadPool(2);
759          executor.execute(new CheckedRunnable() {
760              public void realRun() throws InterruptedException {
761 <                threadAssertNull(q.poll());
762 <                threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
763 <                threadAssertTrue(q.isEmpty());
761 >                assertNull(q.poll());
762 >                assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
763 >                assertTrue(q.isEmpty());
764              }});
765  
766          executor.execute(new CheckedRunnable() {
# Line 898 | Line 880 | public class ArrayBlockingQueueTest exte
880      }
881  
882      /**
883 <     * drainTo(c, n) empties first max {n, size} elements of queue into c
883 >     * drainTo(c, n) empties first min(n, size) elements of queue into c
884       */
885      public void testDrainToN() {
886          ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE*2);
# Line 907 | Line 889 | public class ArrayBlockingQueueTest exte
889                  assertTrue(q.offer(new Integer(j)));
890              ArrayList l = new ArrayList();
891              q.drainTo(l, i);
892 <            int k = (i < SIZE)? i : SIZE;
892 >            int k = (i < SIZE) ? i : SIZE;
893              assertEquals(l.size(), k);
894              assertEquals(q.size(), SIZE-k);
895              for (int j = 0; j < k; ++j)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines