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.65 by jsr166, Sat Apr 25 04:55:30 2015 UTC vs.
Revision 1.72 by jsr166, Sun Oct 16 20:44:18 2016 UTC

# Line 48 | Line 48 | public class ArrayBlockingQueueTest exte
48  
49      /**
50       * Returns a new queue of given size containing consecutive
51 <     * Integers 0 ... n.
51 >     * Integers 0 ... n - 1.
52       */
53      private ArrayBlockingQueue<Integer> populatedQueue(int n) {
54          ArrayBlockingQueue<Integer> q = new ArrayBlockingQueue<Integer>(n);
# Line 58 | Line 58 | public class ArrayBlockingQueueTest exte
58          assertFalse(q.isEmpty());
59          assertEquals(0, q.remainingCapacity());
60          assertEquals(n, q.size());
61 +        assertEquals((Integer) 0, q.peek());
62          return q;
63      }
64  
# Line 104 | Line 105 | public class ArrayBlockingQueueTest exte
105       */
106      public void testConstructor5() {
107          Integer[] ints = new Integer[SIZE];
108 <        for (int i = 0; i < SIZE-1; ++i)
108 >        for (int i = 0; i < SIZE - 1; ++i)
109              ints[i] = i;
110          Collection<Integer> elements = Arrays.asList(ints);
111          try {
# Line 166 | Line 167 | public class ArrayBlockingQueueTest exte
167              assertEquals(i, q.remove());
168          }
169          for (int i = 0; i < SIZE; ++i) {
170 <            assertEquals(SIZE-i, q.remainingCapacity());
170 >            assertEquals(SIZE - i, q.remainingCapacity());
171              assertEquals(SIZE, q.size() + q.remainingCapacity());
172              assertTrue(q.add(i));
173          }
# Line 214 | Line 215 | public class ArrayBlockingQueueTest exte
215      public void testAddAll3() {
216          ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
217          Integer[] ints = new Integer[SIZE];
218 <        for (int i = 0; i < SIZE-1; ++i)
218 >        for (int i = 0; i < SIZE - 1; ++i)
219              ints[i] = new Integer(i);
220          try {
221              q.addAll(Arrays.asList(ints));
# Line 451 | Line 452 | public class ArrayBlockingQueueTest exte
452          final CountDownLatch aboutToWait = new CountDownLatch(1);
453          Thread t = newStartedThread(new CheckedRunnable() {
454              public void realRun() throws InterruptedException {
455 +                long startTime = System.nanoTime();
456                  for (int i = 0; i < SIZE; ++i) {
455                    long t0 = System.nanoTime();
457                      assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
457                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
458                  }
459                long t0 = System.nanoTime();
459                  aboutToWait.countDown();
460                  try {
461 <                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
461 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
462                      shouldThrow();
463                  } catch (InterruptedException success) {
464 <                    assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
464 >                    assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
465                  }
466              }});
467  
468 <        aboutToWait.await();
469 <        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
468 >        await(aboutToWait);
469 >        waitForThreadToEnterWaitState(t);
470          t.interrupt();
471 <        awaitTermination(t, MEDIUM_DELAY_MS);
471 >        awaitTermination(t);
472          checkEmpty(q);
473      }
474  
# Line 572 | Line 571 | public class ArrayBlockingQueueTest exte
571                  assertTrue(changed);
572  
573              assertTrue(q.containsAll(p));
574 <            assertEquals(SIZE-i, q.size());
574 >            assertEquals(SIZE - i, q.size());
575              p.remove();
576          }
577      }
# Line 585 | Line 584 | public class ArrayBlockingQueueTest exte
584              ArrayBlockingQueue q = populatedQueue(SIZE);
585              ArrayBlockingQueue p = populatedQueue(i);
586              assertTrue(q.removeAll(p));
587 <            assertEquals(SIZE-i, q.size());
587 >            assertEquals(SIZE - i, q.size());
588              for (int j = 0; j < i; ++j) {
589                  Integer x = (Integer)(p.remove());
590                  assertFalse(q.contains(x));
# Line 619 | Line 618 | public class ArrayBlockingQueueTest exte
618              checkToArray(q);
619              assertEquals(i, q.poll());
620              checkToArray(q);
621 <            q.add(SIZE+i);
621 >            q.add(SIZE + i);
622          }
623          for (int i = 0; i < SIZE; i++) {
624              checkToArray(q);
625 <            assertEquals(SIZE+i, q.poll());
625 >            assertEquals(SIZE + i, q.poll());
626          }
627      }
628  
629      void checkToArray2(ArrayBlockingQueue q) {
630          int size = q.size();
631 <        Integer[] a1 = size == 0 ? null : new Integer[size-1];
631 >        Integer[] a1 = (size == 0) ? null : new Integer[size - 1];
632          Integer[] a2 = new Integer[size];
633 <        Integer[] a3 = new Integer[size+2];
633 >        Integer[] a3 = new Integer[size + 2];
634          if (size > 0) Arrays.fill(a1, 42);
635          Arrays.fill(a2, 42);
636          Arrays.fill(a3, 42);
637 <        Integer[] b1 = size == 0 ? null : (Integer[]) q.toArray(a1);
637 >        Integer[] b1 = (size == 0) ? null : (Integer[]) q.toArray(a1);
638          Integer[] b2 = (Integer[]) q.toArray(a2);
639          Integer[] b3 = (Integer[]) q.toArray(a3);
640          assertSame(a2, b2);
# Line 649 | Line 648 | public class ArrayBlockingQueueTest exte
648              assertSame(b3[i], x);
649          }
650          assertNull(a3[size]);
651 <        assertEquals(42, (int) a3[size+1]);
651 >        assertEquals(42, (int) a3[size + 1]);
652          if (size > 0) {
653              assertNotSame(a1, b1);
654              assertEquals(size, b1.length);
# Line 673 | Line 672 | public class ArrayBlockingQueueTest exte
672              checkToArray2(q);
673              assertEquals(i, q.poll());
674              checkToArray2(q);
675 <            q.add(SIZE+i);
675 >            q.add(SIZE + i);
676          }
677          for (int i = 0; i < SIZE; i++) {
678              checkToArray2(q);
679 <            assertEquals(SIZE+i, q.poll());
679 >            assertEquals(SIZE + i, q.poll());
680          }
681      }
682  
# Line 788 | Line 787 | public class ArrayBlockingQueueTest exte
787          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
788          q.add(one);
789          q.add(two);
791        ExecutorService executor = Executors.newFixedThreadPool(2);
790          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
791 <        executor.execute(new CheckedRunnable() {
792 <            public void realRun() throws InterruptedException {
793 <                assertFalse(q.offer(three));
794 <                threadsStarted.await();
795 <                assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
796 <                assertEquals(0, q.remainingCapacity());
797 <            }});
798 <
799 <        executor.execute(new CheckedRunnable() {
800 <            public void realRun() throws InterruptedException {
801 <                threadsStarted.await();
802 <                assertEquals(0, q.remainingCapacity());
803 <                assertSame(one, q.take());
804 <            }});
805 <
806 <        joinPool(executor);
791 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
792 >        try (PoolCleaner cleaner = cleaner(executor)) {
793 >            executor.execute(new CheckedRunnable() {
794 >                public void realRun() throws InterruptedException {
795 >                    assertFalse(q.offer(three));
796 >                    threadsStarted.await();
797 >                    assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
798 >                    assertEquals(0, q.remainingCapacity());
799 >                }});
800 >
801 >            executor.execute(new CheckedRunnable() {
802 >                public void realRun() throws InterruptedException {
803 >                    threadsStarted.await();
804 >                    assertEquals(0, q.remainingCapacity());
805 >                    assertSame(one, q.take());
806 >                }});
807 >        }
808      }
809  
810      /**
# Line 814 | Line 813 | public class ArrayBlockingQueueTest exte
813      public void testPollInExecutor() {
814          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
815          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
816 <        ExecutorService executor = Executors.newFixedThreadPool(2);
817 <        executor.execute(new CheckedRunnable() {
818 <            public void realRun() throws InterruptedException {
819 <                assertNull(q.poll());
820 <                threadsStarted.await();
821 <                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
822 <                checkEmpty(q);
823 <            }});
824 <
825 <        executor.execute(new CheckedRunnable() {
826 <            public void realRun() throws InterruptedException {
827 <                threadsStarted.await();
828 <                q.put(one);
829 <            }});
830 <
831 <        joinPool(executor);
816 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
817 >        try (PoolCleaner cleaner = cleaner(executor)) {
818 >            executor.execute(new CheckedRunnable() {
819 >                public void realRun() throws InterruptedException {
820 >                    assertNull(q.poll());
821 >                    threadsStarted.await();
822 >                    assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
823 >                    checkEmpty(q);
824 >                }});
825 >
826 >            executor.execute(new CheckedRunnable() {
827 >                public void realRun() throws InterruptedException {
828 >                    threadsStarted.await();
829 >                    q.put(one);
830 >                }});
831 >        }
832      }
833  
834      /**
# Line 881 | Line 880 | public class ArrayBlockingQueueTest exte
880          final ArrayBlockingQueue q = populatedQueue(SIZE);
881          Thread t = new Thread(new CheckedRunnable() {
882              public void realRun() throws InterruptedException {
883 <                q.put(new Integer(SIZE+1));
883 >                q.put(new Integer(SIZE + 1));
884              }});
885  
886          t.start();
# Line 898 | Line 897 | public class ArrayBlockingQueueTest exte
897       * drainTo(c, n) empties first min(n, size) elements of queue into c
898       */
899      public void testDrainToN() {
900 <        ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE*2);
900 >        ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE * 2);
901          for (int i = 0; i < SIZE + 2; ++i) {
902              for (int j = 0; j < SIZE; j++)
903                  assertTrue(q.offer(new Integer(j)));
# Line 906 | Line 905 | public class ArrayBlockingQueueTest exte
905              q.drainTo(l, i);
906              int k = (i < SIZE) ? i : SIZE;
907              assertEquals(k, l.size());
908 <            assertEquals(SIZE-k, q.size());
908 >            assertEquals(SIZE - k, q.size());
909              for (int j = 0; j < k; ++j)
910                  assertEquals(l.get(j), new Integer(j));
911              do {} while (q.poll() != null);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines