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.68 by jsr166, Sun Oct 4 18:49:02 2015 UTC vs.
Revision 1.76 by jsr166, Sun Nov 6 02:40:38 2016 UTC

# Line 19 | Line 19 | import java.util.concurrent.BlockingQueu
19   import java.util.concurrent.CountDownLatch;
20   import java.util.concurrent.Executors;
21   import java.util.concurrent.ExecutorService;
22 + import java.util.concurrent.ThreadLocalRandom;
23  
24   import junit.framework.Test;
25  
26   public class ArrayBlockingQueueTest extends JSR166TestCase {
27  
28 +    public static void main(String[] args) {
29 +        main(suite(), args);
30 +    }
31 +
32 +    public static Test suite() {
33 +        class Implementation implements CollectionImplementation {
34 +            public Class<?> klazz() { return ArrayBlockingQueue.class; }
35 +            public Collection emptyCollection() {
36 +                boolean fair = ThreadLocalRandom.current().nextBoolean();
37 +                return populatedQueue(0, SIZE, 2 * SIZE, fair);
38 +            }
39 +            public Object makeElement(int i) { return i; }
40 +            public boolean isConcurrent() { return true; }
41 +            public boolean permitsNulls() { return false; }
42 +        }
43 +
44 +        return newTestSuite(
45 +            ArrayBlockingQueueTest.class,
46 +            new Fair().testSuite(),
47 +            new NonFair().testSuite(),
48 +            CollectionTest.testSuite(new Implementation()));
49 +    }
50 +
51      public static class Fair extends BlockingQueueTest {
52          protected BlockingQueue emptyCollection() {
53 <            return new ArrayBlockingQueue(SIZE, true);
53 >            return populatedQueue(0, SIZE, 2 * SIZE, true);
54          }
55      }
56  
57      public static class NonFair extends BlockingQueueTest {
58          protected BlockingQueue emptyCollection() {
59 <            return new ArrayBlockingQueue(SIZE, false);
59 >            return populatedQueue(0, SIZE, 2 * SIZE, false);
60          }
61      }
62  
63 <    public static void main(String[] args) {
64 <        main(suite(), args);
65 <    }
66 <
67 <    public static Test suite() {
68 <        return newTestSuite(ArrayBlockingQueueTest.class,
45 <                            new Fair().testSuite(),
46 <                            new NonFair().testSuite());
63 >    /**
64 >     * Returns a new queue of given size containing consecutive
65 >     * Integers 0 ... n - 1.
66 >     */
67 >    static ArrayBlockingQueue<Integer> populatedQueue(int n) {
68 >        return populatedQueue(n, n, n, false);
69      }
70  
71      /**
72       * Returns a new queue of given size containing consecutive
73 <     * Integers 0 ... n.
73 >     * Integers 0 ... n - 1, with given capacity range and fairness.
74       */
75 <    private ArrayBlockingQueue<Integer> populatedQueue(int n) {
76 <        ArrayBlockingQueue<Integer> q = new ArrayBlockingQueue<Integer>(n);
75 >    static ArrayBlockingQueue<Integer> populatedQueue(
76 >        int size, int minCapacity, int maxCapacity, boolean fair) {
77 >        ThreadLocalRandom rnd = ThreadLocalRandom.current();
78 >        int capacity = rnd.nextInt(minCapacity, maxCapacity + 1);
79 >        ArrayBlockingQueue<Integer> q = new ArrayBlockingQueue<>(capacity);
80          assertTrue(q.isEmpty());
81 <        for (int i = 0; i < n; i++)
82 <            assertTrue(q.offer(new Integer(i)));
83 <        assertFalse(q.isEmpty());
84 <        assertEquals(0, q.remainingCapacity());
85 <        assertEquals(n, q.size());
81 >        // shuffle circular array elements so they wrap
82 >        {
83 >            int n = rnd.nextInt(capacity);
84 >            for (int i = 0; i < n; i++) q.add(42);
85 >            for (int i = 0; i < n; i++) q.remove();
86 >        }
87 >        for (int i = 0; i < size; i++)
88 >            assertTrue(q.offer((Integer) i));
89 >        assertEquals(size == 0, q.isEmpty());
90 >        assertEquals(capacity - size, q.remainingCapacity());
91 >        assertEquals(size, q.size());
92 >        if (size > 0)
93 >            assertEquals((Integer) 0, q.peek());
94          return q;
95      }
96  
# Line 451 | Line 484 | public class ArrayBlockingQueueTest exte
484          final CountDownLatch aboutToWait = new CountDownLatch(1);
485          Thread t = newStartedThread(new CheckedRunnable() {
486              public void realRun() throws InterruptedException {
487 +                long startTime = System.nanoTime();
488                  for (int i = 0; i < SIZE; ++i) {
455                    long t0 = System.nanoTime();
489                      assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
457                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
490                  }
459                long t0 = System.nanoTime();
491                  aboutToWait.countDown();
492                  try {
493 <                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
493 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
494                      shouldThrow();
495                  } catch (InterruptedException success) {
496 <                    assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
496 >                    assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
497                  }
498              }});
499  
500 <        aboutToWait.await();
501 <        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
500 >        await(aboutToWait);
501 >        waitForThreadToEnterWaitState(t);
502          t.interrupt();
503 <        awaitTermination(t, MEDIUM_DELAY_MS);
503 >        awaitTermination(t);
504          checkEmpty(q);
505      }
506  
# Line 593 | Line 624 | public class ArrayBlockingQueueTest exte
624          }
625      }
626  
627 <    void checkToArray(ArrayBlockingQueue q) {
627 >    void checkToArray(ArrayBlockingQueue<Integer> q) {
628          int size = q.size();
629 <        Object[] o = q.toArray();
630 <        assertEquals(size, o.length);
629 >        Object[] a1 = q.toArray();
630 >        assertEquals(size, a1.length);
631 >        Integer[] a2 = q.toArray(new Integer[0]);
632 >        assertEquals(size, a2.length);
633 >        Integer[] a3 = q.toArray(new Integer[Math.max(0, size - 1)]);
634 >        assertEquals(size, a3.length);
635 >        Integer[] a4 = new Integer[size];
636 >        assertSame(a4, q.toArray(a4));
637 >        Integer[] a5 = new Integer[size + 1];
638 >        Arrays.fill(a5, 42);
639 >        assertSame(a5, q.toArray(a5));
640 >        Integer[] a6 = new Integer[size + 2];
641 >        Arrays.fill(a6, 42);
642 >        assertSame(a6, q.toArray(a6));
643 >        Object[][] as = { a1, a2, a3, a4, a5, a6 };
644 >        for (Object[] a : as) {
645 >            if (a.length > size) assertNull(a[size]);
646 >            if (a.length > size + 1) assertEquals(42, a[size + 1]);
647 >        }
648          Iterator it = q.iterator();
649 +        Integer s = q.peek();
650          for (int i = 0; i < size; i++) {
651              Integer x = (Integer) it.next();
652 <            assertEquals((Integer)o[0] + i, (int) x);
653 <            assertSame(o[i], x);
652 >            assertEquals(s + i, (int) x);
653 >            for (Object[] a : as)
654 >                assertSame(a1[i], x);
655          }
656      }
657  
658      /**
659 <     * toArray() contains all elements in FIFO order
659 >     * toArray() and toArray(a) contain all elements in FIFO order
660       */
661      public void testToArray() {
662 <        ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
663 <        for (int i = 0; i < SIZE; i++) {
662 >        final ThreadLocalRandom rnd = ThreadLocalRandom.current();
663 >        final int size = rnd.nextInt(6);
664 >        final int capacity = Math.max(1, size + rnd.nextInt(size + 1));
665 >        ArrayBlockingQueue<Integer> q = new ArrayBlockingQueue<>(capacity);
666 >        for (int i = 0; i < size; i++) {
667              checkToArray(q);
668              q.add(i);
669          }
670          // Provoke wraparound
671 <        for (int i = 0; i < SIZE; i++) {
671 >        int added = size * 2;
672 >        for (int i = 0; i < added; i++) {
673              checkToArray(q);
674 <            assertEquals(i, q.poll());
675 <            checkToArray(q);
622 <            q.add(SIZE + i);
623 <        }
624 <        for (int i = 0; i < SIZE; i++) {
625 <            checkToArray(q);
626 <            assertEquals(SIZE + i, q.poll());
674 >            assertEquals((Integer) i, q.poll());
675 >            q.add(size + i);
676          }
628    }
629
630    void checkToArray2(ArrayBlockingQueue q) {
631        int size = q.size();
632        Integer[] a1 = (size == 0) ? null : new Integer[size - 1];
633        Integer[] a2 = new Integer[size];
634        Integer[] a3 = new Integer[size + 2];
635        if (size > 0) Arrays.fill(a1, 42);
636        Arrays.fill(a2, 42);
637        Arrays.fill(a3, 42);
638        Integer[] b1 = (size == 0) ? null : (Integer[]) q.toArray(a1);
639        Integer[] b2 = (Integer[]) q.toArray(a2);
640        Integer[] b3 = (Integer[]) q.toArray(a3);
641        assertSame(a2, b2);
642        assertSame(a3, b3);
643        Iterator it = q.iterator();
677          for (int i = 0; i < size; i++) {
678 <            Integer x = (Integer) it.next();
679 <            assertSame(b1[i], x);
647 <            assertEquals(b1[0] + i, (int) x);
648 <            assertSame(b2[i], x);
649 <            assertSame(b3[i], x);
650 <        }
651 <        assertNull(a3[size]);
652 <        assertEquals(42, (int) a3[size + 1]);
653 <        if (size > 0) {
654 <            assertNotSame(a1, b1);
655 <            assertEquals(size, b1.length);
656 <            for (int i = 0; i < a1.length; i++) {
657 <                assertEquals(42, (int) a1[i]);
658 <            }
659 <        }
660 <    }
661 <
662 <    /**
663 <     * toArray(a) contains all elements in FIFO order
664 <     */
665 <    public void testToArray2() {
666 <        ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
667 <        for (int i = 0; i < SIZE; i++) {
668 <            checkToArray2(q);
669 <            q.add(i);
670 <        }
671 <        // Provoke wraparound
672 <        for (int i = 0; i < SIZE; i++) {
673 <            checkToArray2(q);
674 <            assertEquals(i, q.poll());
675 <            checkToArray2(q);
676 <            q.add(SIZE + i);
677 <        }
678 <        for (int i = 0; i < SIZE; i++) {
679 <            checkToArray2(q);
680 <            assertEquals(SIZE + i, q.poll());
678 >            checkToArray(q);
679 >            assertEquals((Integer) (added + i), q.poll());
680          }
681      }
682  
683      /**
684       * toArray(incompatible array type) throws ArrayStoreException
685       */
686 <    public void testToArray1_BadArg() {
686 >    public void testToArray_incompatibleArrayType() {
687          ArrayBlockingQueue q = populatedQueue(SIZE);
688          try {
689              q.toArray(new String[10]);
690              shouldThrow();
691          } catch (ArrayStoreException success) {}
692 +        try {
693 +            q.toArray(new String[0]);
694 +            shouldThrow();
695 +        } catch (ArrayStoreException success) {}
696      }
697  
698      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines