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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines