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.62 by jsr166, Sun Feb 22 04:34:44 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 <        junit.textui.TestRunner.run(suite());
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 104 | Line 137 | public class ArrayBlockingQueueTest exte
137       */
138      public void testConstructor5() {
139          Integer[] ints = new Integer[SIZE];
140 <        for (int i = 0; i < SIZE-1; ++i)
140 >        for (int i = 0; i < SIZE - 1; ++i)
141              ints[i] = i;
142          Collection<Integer> elements = Arrays.asList(ints);
143          try {
# Line 159 | Line 192 | public class ArrayBlockingQueueTest exte
192       * remainingCapacity decreases on add, increases on remove
193       */
194      public void testRemainingCapacity() {
195 <        ArrayBlockingQueue q = populatedQueue(SIZE);
195 >        BlockingQueue q = populatedQueue(SIZE);
196          for (int i = 0; i < SIZE; ++i) {
197              assertEquals(i, q.remainingCapacity());
198 <            assertEquals(SIZE-i, q.size());
199 <            q.remove();
198 >            assertEquals(SIZE, q.size() + q.remainingCapacity());
199 >            assertEquals(i, q.remove());
200          }
201          for (int i = 0; i < SIZE; ++i) {
202 <            assertEquals(SIZE-i, q.remainingCapacity());
203 <            assertEquals(i, q.size());
204 <            q.add(new Integer(i));
202 >            assertEquals(SIZE - i, q.remainingCapacity());
203 >            assertEquals(SIZE, q.size() + q.remainingCapacity());
204 >            assertTrue(q.add(i));
205          }
206      }
207  
# Line 185 | Line 218 | public class ArrayBlockingQueueTest exte
218       * add succeeds if not full; throws ISE if full
219       */
220      public void testAdd() {
221 +        ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
222 +        for (int i = 0; i < SIZE; ++i) {
223 +            assertTrue(q.add(new Integer(i)));
224 +        }
225 +        assertEquals(0, q.remainingCapacity());
226          try {
189            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
190            for (int i = 0; i < SIZE; ++i) {
191                assertTrue(q.add(new Integer(i)));
192            }
193            assertEquals(0, q.remainingCapacity());
227              q.add(new Integer(SIZE));
228              shouldThrow();
229          } catch (IllegalStateException success) {}
# Line 200 | Line 233 | public class ArrayBlockingQueueTest exte
233       * addAll(this) throws IAE
234       */
235      public void testAddAllSelf() {
236 +        ArrayBlockingQueue q = populatedQueue(SIZE);
237          try {
204            ArrayBlockingQueue q = populatedQueue(SIZE);
238              q.addAll(q);
239              shouldThrow();
240          } catch (IllegalArgumentException success) {}
# Line 212 | Line 245 | public class ArrayBlockingQueueTest exte
245       * possibly adding some elements
246       */
247      public void testAddAll3() {
248 +        ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
249 +        Integer[] ints = new Integer[SIZE];
250 +        for (int i = 0; i < SIZE - 1; ++i)
251 +            ints[i] = new Integer(i);
252          try {
216            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
217            Integer[] ints = new Integer[SIZE];
218            for (int i = 0; i < SIZE-1; ++i)
219                ints[i] = new Integer(i);
253              q.addAll(Arrays.asList(ints));
254              shouldThrow();
255          } catch (NullPointerException success) {}
# Line 226 | Line 259 | public class ArrayBlockingQueueTest exte
259       * addAll throws ISE if not enough room
260       */
261      public void testAddAll4() {
262 +        ArrayBlockingQueue q = new ArrayBlockingQueue(1);
263 +        Integer[] ints = new Integer[SIZE];
264 +        for (int i = 0; i < SIZE; ++i)
265 +            ints[i] = new Integer(i);
266          try {
230            ArrayBlockingQueue q = new ArrayBlockingQueue(1);
231            Integer[] ints = new Integer[SIZE];
232            for (int i = 0; i < SIZE; ++i)
233                ints[i] = new Integer(i);
267              q.addAll(Arrays.asList(ints));
268              shouldThrow();
269          } catch (IllegalStateException success) {}
# 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 572 | Line 603 | public class ArrayBlockingQueueTest exte
603                  assertTrue(changed);
604  
605              assertTrue(q.containsAll(p));
606 <            assertEquals(SIZE-i, q.size());
606 >            assertEquals(SIZE - i, q.size());
607              p.remove();
608          }
609      }
# Line 585 | Line 616 | public class ArrayBlockingQueueTest exte
616              ArrayBlockingQueue q = populatedQueue(SIZE);
617              ArrayBlockingQueue p = populatedQueue(i);
618              assertTrue(q.removeAll(p));
619 <            assertEquals(SIZE-i, q.size());
619 >            assertEquals(SIZE - i, q.size());
620              for (int j = 0; j < i; ++j) {
621                  Integer x = (Integer)(p.remove());
622                  assertFalse(q.contains(x));
# 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      /**
# Line 788 | Line 791 | public class ArrayBlockingQueueTest exte
791          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
792          q.add(one);
793          q.add(two);
791        ExecutorService executor = Executors.newFixedThreadPool(2);
794          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
795 <        executor.execute(new CheckedRunnable() {
796 <            public void realRun() throws InterruptedException {
797 <                assertFalse(q.offer(three));
798 <                threadsStarted.await();
799 <                assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
800 <                assertEquals(0, q.remainingCapacity());
801 <            }});
802 <
803 <        executor.execute(new CheckedRunnable() {
804 <            public void realRun() throws InterruptedException {
805 <                threadsStarted.await();
806 <                assertEquals(0, q.remainingCapacity());
807 <                assertSame(one, q.take());
808 <            }});
809 <
810 <        joinPool(executor);
795 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
796 >        try (PoolCleaner cleaner = cleaner(executor)) {
797 >            executor.execute(new CheckedRunnable() {
798 >                public void realRun() throws InterruptedException {
799 >                    assertFalse(q.offer(three));
800 >                    threadsStarted.await();
801 >                    assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
802 >                    assertEquals(0, q.remainingCapacity());
803 >                }});
804 >
805 >            executor.execute(new CheckedRunnable() {
806 >                public void realRun() throws InterruptedException {
807 >                    threadsStarted.await();
808 >                    assertEquals(0, q.remainingCapacity());
809 >                    assertSame(one, q.take());
810 >                }});
811 >        }
812      }
813  
814      /**
# Line 814 | Line 817 | public class ArrayBlockingQueueTest exte
817      public void testPollInExecutor() {
818          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
819          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
820 <        ExecutorService executor = Executors.newFixedThreadPool(2);
821 <        executor.execute(new CheckedRunnable() {
822 <            public void realRun() throws InterruptedException {
823 <                assertNull(q.poll());
824 <                threadsStarted.await();
825 <                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
826 <                checkEmpty(q);
827 <            }});
828 <
829 <        executor.execute(new CheckedRunnable() {
830 <            public void realRun() throws InterruptedException {
831 <                threadsStarted.await();
832 <                q.put(one);
833 <            }});
834 <
835 <        joinPool(executor);
820 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
821 >        try (PoolCleaner cleaner = cleaner(executor)) {
822 >            executor.execute(new CheckedRunnable() {
823 >                public void realRun() throws InterruptedException {
824 >                    assertNull(q.poll());
825 >                    threadsStarted.await();
826 >                    assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
827 >                    checkEmpty(q);
828 >                }});
829 >
830 >            executor.execute(new CheckedRunnable() {
831 >                public void realRun() throws InterruptedException {
832 >                    threadsStarted.await();
833 >                    q.put(one);
834 >                }});
835 >        }
836      }
837  
838      /**
# Line 881 | Line 884 | public class ArrayBlockingQueueTest exte
884          final ArrayBlockingQueue q = populatedQueue(SIZE);
885          Thread t = new Thread(new CheckedRunnable() {
886              public void realRun() throws InterruptedException {
887 <                q.put(new Integer(SIZE+1));
887 >                q.put(new Integer(SIZE + 1));
888              }});
889  
890          t.start();
# Line 898 | Line 901 | public class ArrayBlockingQueueTest exte
901       * drainTo(c, n) empties first min(n, size) elements of queue into c
902       */
903      public void testDrainToN() {
904 <        ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE*2);
904 >        ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE * 2);
905          for (int i = 0; i < SIZE + 2; ++i) {
906              for (int j = 0; j < SIZE; j++)
907                  assertTrue(q.offer(new Integer(j)));
# Line 906 | Line 909 | public class ArrayBlockingQueueTest exte
909              q.drainTo(l, i);
910              int k = (i < SIZE) ? i : SIZE;
911              assertEquals(k, l.size());
912 <            assertEquals(SIZE-k, q.size());
912 >            assertEquals(SIZE - k, q.size());
913              for (int j = 0; j < k; ++j)
914                  assertEquals(l.get(j), new Integer(j));
915              do {} while (q.poll() != null);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines