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.21 by jsr166, Sat Nov 21 19:11:53 2009 UTC vs.
Revision 1.31 by dl, Wed Sep 29 12:33:48 2010 UTC

# Line 15 | Line 15 | import java.io.*;
15  
16   public class ArrayBlockingQueueTest extends JSR166TestCase {
17      public static void main(String[] args) {
18 <        junit.textui.TestRunner.run (suite());
18 >        junit.textui.TestRunner.run(suite());
19      }
20      public static Test suite() {
21          return new TestSuite(ArrayBlockingQueueTest.class);
# Line 224 | Line 224 | public class ArrayBlockingQueueTest exte
224              shouldThrow();
225          } catch (NullPointerException success) {}
226      }
227 +
228      /**
229       * addAll of a collection with any null elements throws NPE after
230       * possibly adding some elements
# Line 238 | Line 239 | public class ArrayBlockingQueueTest exte
239              shouldThrow();
240          } catch (NullPointerException success) {}
241      }
242 +
243      /**
244       * addAll throws ISE if not enough room
245       */
# Line 251 | Line 253 | public class ArrayBlockingQueueTest exte
253              shouldThrow();
254          } catch (IllegalStateException success) {}
255      }
256 +
257      /**
258       * Queue contains all elements, in traversal order, of successful addAll
259       */
# Line 296 | Line 299 | public class ArrayBlockingQueueTest exte
299      public void testBlockingPut() throws InterruptedException {
300          final ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
301          Thread t = new Thread(new CheckedRunnable() {
302 <            public void realRun() {
303 <                int added = 0;
302 >            public void realRun() throws InterruptedException {
303 >                for (int i = 0; i < SIZE; ++i)
304 >                    q.put(i);
305 >                assertEquals(SIZE, q.size());
306 >                assertEquals(0, q.remainingCapacity());
307                  try {
308 <                    for (int i = 0; i < SIZE; ++i) {
309 <                        q.put(new Integer(i));
310 <                        ++added;
311 <                    }
306 <                    q.put(new Integer(SIZE));
307 <                    threadShouldThrow();
308 <                } catch (InterruptedException success) {
309 <                    threadAssertEquals(added, SIZE);
310 <                }}});
308 >                    q.put(99);
309 >                    shouldThrow();
310 >                } catch (InterruptedException success) {}
311 >            }});
312  
313          t.start();
314 <        Thread.sleep(MEDIUM_DELAY_MS);
314 >        Thread.sleep(SHORT_DELAY_MS);
315          t.interrupt();
316          t.join();
317 +        assertEquals(SIZE, q.size());
318 +        assertEquals(0, q.remainingCapacity());
319      }
320  
321      /**
322       * put blocks waiting for take when full
323       */
324      public void testPutWithTake() throws InterruptedException {
325 <        final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
325 >        final int capacity = 2;
326 >        final ArrayBlockingQueue q = new ArrayBlockingQueue(capacity);
327          Thread t = new Thread(new CheckedRunnable() {
328 <            public void realRun() {
329 <                int added = 0;
328 >            public void realRun() throws InterruptedException {
329 >                for (int i = 0; i < capacity + 1; i++)
330 >                    q.put(i);
331                  try {
332 <                    q.put(new Object());
333 <                    ++added;
334 <                    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 <                }
332 >                    q.put(99);
333 >                    shouldThrow();
334 >                } catch (InterruptedException success) {}
335              }});
336  
337          t.start();
338          Thread.sleep(SHORT_DELAY_MS);
339 <        q.take();
339 >        assertEquals(q.remainingCapacity(), 0);
340 >        assertEquals(0, q.take());
341 >        Thread.sleep(SHORT_DELAY_MS);
342          t.interrupt();
343          t.join();
344 +        assertEquals(q.remainingCapacity(), 0);
345      }
346  
347      /**
# Line 350 | Line 349 | public class ArrayBlockingQueueTest exte
349       */
350      public void testTimedOffer() throws InterruptedException {
351          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
352 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
352 >        Thread t = new Thread(new CheckedRunnable() {
353              public void realRun() throws InterruptedException {
354                  q.put(new Object());
355                  q.put(new Object());
356 <                threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, MILLISECONDS));
357 <                q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
358 <            }};
356 >                assertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, MILLISECONDS));
357 >                try {
358 >                    q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
359 >                    shouldThrow();
360 >                } catch (InterruptedException success) {}
361 >            }});
362  
363          t.start();
364          Thread.sleep(SHORT_DELAY_MS);
# Line 370 | Line 372 | public class ArrayBlockingQueueTest exte
372      public void testTake() throws InterruptedException {
373          ArrayBlockingQueue q = populatedQueue(SIZE);
374          for (int i = 0; i < SIZE; ++i) {
375 <            assertEquals(i, ((Integer)q.take()).intValue());
375 >            assertEquals(i, q.take());
376          }
377      }
378  
# Line 394 | Line 396 | public class ArrayBlockingQueueTest exte
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 {
399                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 416 | 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      }
# Line 427 | Line 432 | public class ArrayBlockingQueueTest exte
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      }
# Line 438 | Line 443 | public class ArrayBlockingQueueTest exte
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 452 | Line 457 | public class ArrayBlockingQueueTest exte
457              public void realRun() throws InterruptedException {
458                  ArrayBlockingQueue q = populatedQueue(SIZE);
459                  for (int i = 0; i < SIZE; ++i) {
460 <                    assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
460 >                    assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));;
461                  }
462                  try {
463                      q.poll(SMALL_DELAY_MS, MILLISECONDS);
464 +                    shouldThrow();
465                  } catch (InterruptedException success) {}
466              }});
467  
# Line 471 | Line 477 | public class ArrayBlockingQueueTest exte
477       */
478      public void testTimedPollWithOffer() throws InterruptedException {
479          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
480 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
480 >        Thread t = new Thread(new CheckedRunnable() {
481              public void realRun() throws InterruptedException {
482 <                threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
483 <                q.poll(LONG_DELAY_MS, MILLISECONDS);
484 <                q.poll(LONG_DELAY_MS, MILLISECONDS);
485 <            }};
482 >                try {
483 >                    assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
484 >                    assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
485 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
486 >                    shouldThrow();
487 >                } catch (InterruptedException success) {}
488 >            }});
489  
490          t.start();
491          Thread.sleep(SMALL_DELAY_MS);
# Line 492 | Line 501 | public class ArrayBlockingQueueTest exte
501      public void testPeek() {
502          ArrayBlockingQueue q = populatedQueue(SIZE);
503          for (int i = 0; i < SIZE; ++i) {
504 <            assertEquals(i, ((Integer)q.peek()).intValue());
505 <            q.poll();
504 >            assertEquals(i, q.peek());
505 >            assertEquals(i, q.poll());
506              assertTrue(q.peek() == null ||
507 <                       i != ((Integer)q.peek()).intValue());
507 >                       !q.peek().equals(i));
508          }
509          assertNull(q.peek());
510      }
# Line 506 | Line 515 | public class ArrayBlockingQueueTest exte
515      public void testElement() {
516          ArrayBlockingQueue q = populatedQueue(SIZE);
517          for (int i = 0; i < SIZE; ++i) {
518 <            assertEquals(i, ((Integer)q.element()).intValue());
519 <            q.poll();
518 >            assertEquals(i, q.element());
519 >            assertEquals(i, q.poll());
520          }
521          try {
522              q.element();
# Line 521 | Line 530 | public class ArrayBlockingQueueTest exte
530      public void testRemove() {
531          ArrayBlockingQueue q = populatedQueue(SIZE);
532          for (int i = 0; i < SIZE; ++i) {
533 <            assertEquals(i, ((Integer)q.remove()).intValue());
533 >            assertEquals(i, q.remove());
534          }
535          try {
536              q.remove();
# Line 551 | Line 560 | public class ArrayBlockingQueueTest exte
560          ArrayBlockingQueue q = populatedQueue(SIZE);
561          for (int i = 0; i < SIZE; ++i) {
562              assertTrue(q.contains(new Integer(i)));
563 <            q.poll();
563 >            assertEquals(i, q.poll());
564              assertFalse(q.contains(new Integer(i)));
565          }
566      }
# Line 646 | Line 655 | public class ArrayBlockingQueueTest exte
655       * toArray(null) throws NPE
656       */
657      public void testToArray_BadArg() {
658 +        ArrayBlockingQueue q = populatedQueue(SIZE);
659          try {
650            ArrayBlockingQueue q = populatedQueue(SIZE);
660              Object o[] = q.toArray(null);
661              shouldThrow();
662          } catch (NullPointerException success) {}
# Line 657 | Line 666 | public class ArrayBlockingQueueTest exte
666       * toArray with incompatible array type throws CCE
667       */
668      public void testToArray1_BadArg() {
669 +        ArrayBlockingQueue q = populatedQueue(SIZE);
670          try {
671 <            ArrayBlockingQueue q = populatedQueue(SIZE);
662 <            Object o[] = q.toArray(new String[10] );
671 >            Object o[] = q.toArray(new String[10]);
672              shouldThrow();
673          } catch (ArrayStoreException success) {}
674      }
# Line 679 | Line 688 | public class ArrayBlockingQueueTest exte
688      /**
689       * iterator.remove removes current element
690       */
691 <    public void testIteratorRemove () {
691 >    public void testIteratorRemove() {
692          final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
693          q.add(two);
694          q.add(one);
# Line 690 | Line 699 | public class ArrayBlockingQueueTest exte
699          it.remove();
700  
701          it = q.iterator();
702 <        assertEquals(it.next(), one);
703 <        assertEquals(it.next(), three);
702 >        assertSame(it.next(), one);
703 >        assertSame(it.next(), three);
704          assertFalse(it.hasNext());
705      }
706  
# Line 708 | Line 717 | public class ArrayBlockingQueueTest exte
717  
718          int k = 0;
719          for (Iterator it = q.iterator(); it.hasNext();) {
720 <            int i = ((Integer)(it.next())).intValue();
712 <            assertEquals(++k, i);
720 >            assertEquals(++k, it.next());
721          }
722          assertEquals(3, k);
723      }
# Line 717 | Line 725 | public class ArrayBlockingQueueTest exte
725      /**
726       * Modifications do not cause iterators to fail
727       */
728 <    public void testWeaklyConsistentIteration () {
728 >    public void testWeaklyConsistentIteration() {
729          final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
730          q.add(one);
731          q.add(two);
# Line 752 | Line 760 | public class ArrayBlockingQueueTest exte
760          ExecutorService executor = Executors.newFixedThreadPool(2);
761          executor.execute(new CheckedRunnable() {
762              public void realRun() throws InterruptedException {
763 <                threadAssertFalse(q.offer(three));
764 <                threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
765 <                threadAssertEquals(0, q.remainingCapacity());
763 >                assertFalse(q.offer(three));
764 >                assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
765 >                assertEquals(0, q.remainingCapacity());
766              }});
767  
768          executor.execute(new CheckedRunnable() {
769              public void realRun() throws InterruptedException {
770                  Thread.sleep(SMALL_DELAY_MS);
771 <                threadAssertEquals(one, q.take());
771 >                assertSame(one, q.take());
772              }});
773  
774          joinPool(executor);
767
775      }
776  
777      /**
# Line 775 | Line 782 | public class ArrayBlockingQueueTest exte
782          ExecutorService executor = Executors.newFixedThreadPool(2);
783          executor.execute(new CheckedRunnable() {
784              public void realRun() throws InterruptedException {
785 <                threadAssertNull(q.poll());
786 <                threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
787 <                threadAssertTrue(q.isEmpty());
785 >                assertNull(q.poll());
786 >                assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
787 >                assertTrue(q.isEmpty());
788              }});
789  
790          executor.execute(new CheckedRunnable() {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines