ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/LinkedTransferQueueTest.java
(Generate patch)

Comparing jsr166/src/test/tck/LinkedTransferQueueTest.java (file contents):
Revision 1.54 by jsr166, Wed Dec 31 16:44:02 2014 UTC vs.
Revision 1.64 by jsr166, Tue Oct 6 00:03:55 2015 UTC

# Line 5 | Line 5
5   * Other contributors include John Vint
6   */
7  
8 < import junit.framework.*;
9 < import java.util.Arrays;
8 > import static java.util.concurrent.TimeUnit.MILLISECONDS;
9 >
10   import java.util.ArrayList;
11 + import java.util.Arrays;
12   import java.util.Collection;
13   import java.util.Iterator;
14   import java.util.List;
# Line 18 | Line 19 | import java.util.concurrent.CountDownLat
19   import java.util.concurrent.Executors;
20   import java.util.concurrent.ExecutorService;
21   import java.util.concurrent.LinkedTransferQueue;
22 < import static java.util.concurrent.TimeUnit.MILLISECONDS;
22 >
23 > import junit.framework.Test;
24  
25   @SuppressWarnings({"unchecked", "rawtypes"})
26   public class LinkedTransferQueueTest extends JSR166TestCase {
27 +    static class Implementation implements CollectionImplementation {
28 +        public Class<?> klazz() { return LinkedTransferQueue.class; }
29 +        public Collection emptyCollection() { return new LinkedTransferQueue(); }
30 +        public Object makeElement(int i) { return i; }
31 +        public boolean isConcurrent() { return true; }
32 +        public boolean permitsNulls() { return false; }
33 +    }
34  
35      public static class Generic extends BlockingQueueTest {
36          protected BlockingQueue emptyCollection() {
# Line 30 | Line 39 | public class LinkedTransferQueueTest ext
39      }
40  
41      public static void main(String[] args) {
42 <        junit.textui.TestRunner.run(suite());
42 >        main(suite(), args);
43      }
44  
45      public static Test suite() {
46          return newTestSuite(LinkedTransferQueueTest.class,
47 <                            new Generic().testSuite());
47 >                            new Generic().testSuite(),
48 >                            CollectionTest.testSuite(new Implementation()));
49      }
50  
51      /**
# Line 76 | Line 86 | public class LinkedTransferQueueTest ext
86       */
87      public void testConstructor4() {
88          Integer[] ints = new Integer[SIZE];
89 <        for (int i = 0; i < SIZE-1; ++i)
89 >        for (int i = 0; i < SIZE - 1; ++i)
90              ints[i] = i;
91          Collection<Integer> elements = Arrays.asList(ints);
92          try {
# Line 113 | Line 123 | public class LinkedTransferQueueTest ext
123       * remainingCapacity() always returns Integer.MAX_VALUE
124       */
125      public void testRemainingCapacity() {
126 <        LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
126 >        BlockingQueue q = populatedQueue(SIZE);
127          for (int i = 0; i < SIZE; ++i) {
128              assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
129              assertEquals(SIZE - i, q.size());
130 <            q.remove();
130 >            assertEquals(i, q.remove());
131          }
132          for (int i = 0; i < SIZE; ++i) {
133              assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
134              assertEquals(i, q.size());
135 <            q.add(i);
135 >            assertTrue(q.add(i));
136          }
137      }
138  
# Line 130 | Line 140 | public class LinkedTransferQueueTest ext
140       * addAll(this) throws IllegalArgumentException
141       */
142      public void testAddAllSelf() {
143 +        LinkedTransferQueue q = populatedQueue(SIZE);
144          try {
134            LinkedTransferQueue q = populatedQueue(SIZE);
145              q.addAll(q);
146              shouldThrow();
147          } catch (IllegalArgumentException success) {}
# Line 142 | Line 152 | public class LinkedTransferQueueTest ext
152       * NullPointerException after possibly adding some elements
153       */
154      public void testAddAll3() {
155 +        LinkedTransferQueue q = new LinkedTransferQueue();
156 +        Integer[] ints = new Integer[SIZE];
157 +        for (int i = 0; i < SIZE - 1; ++i)
158 +            ints[i] = i;
159          try {
146            LinkedTransferQueue q = new LinkedTransferQueue();
147            Integer[] ints = new Integer[SIZE];
148            for (int i = 0; i < SIZE - 1; ++i) {
149                ints[i] = i;
150            }
160              q.addAll(Arrays.asList(ints));
161              shouldThrow();
162          } catch (NullPointerException success) {}
# Line 274 | Line 283 | public class LinkedTransferQueueTest ext
283          final CountDownLatch aboutToWait = new CountDownLatch(1);
284          Thread t = newStartedThread(new CheckedRunnable() {
285              public void realRun() throws InterruptedException {
286 +                long startTime = System.nanoTime();
287                  for (int i = 0; i < SIZE; ++i) {
278                    long t0 = System.nanoTime();
288                      assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
280                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
289                  }
282                long t0 = System.nanoTime();
290                  aboutToWait.countDown();
291                  try {
292 <                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
292 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
293                      shouldThrow();
294                  } catch (InterruptedException success) {
295 <                    assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
295 >                    assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
296                  }
297              }});
298  
299          aboutToWait.await();
300 <        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
300 >        waitForThreadToEnterWaitState(t, LONG_DELAY_MS);
301          t.interrupt();
302 <        awaitTermination(t, MEDIUM_DELAY_MS);
302 >        awaitTermination(t);
303          checkEmpty(q);
304      }
305  
# Line 498 | Line 505 | public class LinkedTransferQueueTest ext
505      public void testIterator() throws InterruptedException {
506          LinkedTransferQueue q = populatedQueue(SIZE);
507          Iterator it = q.iterator();
508 <        int i = 0;
509 <        while (it.hasNext()) {
510 <            assertEquals(it.next(), i++);
511 <        }
508 >        int i;
509 >        for (i = 0; it.hasNext(); i++)
510 >            assertTrue(q.contains(it.next()));
511 >        assertEquals(i, SIZE);
512 >        assertIteratorExhausted(it);
513 >
514 >        it = q.iterator();
515 >        for (i = 0; it.hasNext(); i++)
516 >            assertEquals(it.next(), q.take());
517          assertEquals(i, SIZE);
518 +        assertIteratorExhausted(it);
519 +    }
520 +
521 +    /**
522 +     * iterator of empty collection has no elements
523 +     */
524 +    public void testEmptyIterator() {
525 +        assertIteratorExhausted(new LinkedTransferQueue().iterator());
526      }
527  
528      /**
# Line 574 | Line 594 | public class LinkedTransferQueueTest ext
594      public void testOfferInExecutor() {
595          final LinkedTransferQueue q = new LinkedTransferQueue();
596          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
597 <        ExecutorService executor = Executors.newFixedThreadPool(2);
597 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
598 >        try (PoolCleaner cleaner = cleaner(executor)) {
599  
600 <        executor.execute(new CheckedRunnable() {
601 <            public void realRun() throws InterruptedException {
602 <                threadsStarted.await();
603 <                assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS));
604 <            }});
584 <
585 <        executor.execute(new CheckedRunnable() {
586 <            public void realRun() throws InterruptedException {
587 <                threadsStarted.await();
588 <                assertSame(one, q.take());
589 <                checkEmpty(q);
590 <            }});
600 >            executor.execute(new CheckedRunnable() {
601 >                public void realRun() throws InterruptedException {
602 >                    threadsStarted.await();
603 >                    assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS));
604 >                }});
605  
606 <        joinPool(executor);
606 >            executor.execute(new CheckedRunnable() {
607 >                public void realRun() throws InterruptedException {
608 >                    threadsStarted.await();
609 >                    assertSame(one, q.take());
610 >                    checkEmpty(q);
611 >                }});
612 >        }
613      }
614  
615      /**
# Line 598 | Line 618 | public class LinkedTransferQueueTest ext
618      public void testPollInExecutor() {
619          final LinkedTransferQueue q = new LinkedTransferQueue();
620          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
621 <        ExecutorService executor = Executors.newFixedThreadPool(2);
621 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
622 >        try (PoolCleaner cleaner = cleaner(executor)) {
623  
624 <        executor.execute(new CheckedRunnable() {
625 <            public void realRun() throws InterruptedException {
626 <                assertNull(q.poll());
627 <                threadsStarted.await();
628 <                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
629 <                checkEmpty(q);
630 <            }});
610 <
611 <        executor.execute(new CheckedRunnable() {
612 <            public void realRun() throws InterruptedException {
613 <                threadsStarted.await();
614 <                q.put(one);
615 <            }});
624 >            executor.execute(new CheckedRunnable() {
625 >                public void realRun() throws InterruptedException {
626 >                    assertNull(q.poll());
627 >                    threadsStarted.await();
628 >                    assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
629 >                    checkEmpty(q);
630 >                }});
631  
632 <        joinPool(executor);
632 >            executor.execute(new CheckedRunnable() {
633 >                public void realRun() throws InterruptedException {
634 >                    threadsStarted.await();
635 >                    q.put(one);
636 >                }});
637 >        }
638      }
639  
640      /**
# Line 695 | Line 715 | public class LinkedTransferQueueTest ext
715              assertEquals(SIZE - k, q.size());
716              for (int j = 0; j < k; ++j)
717                  assertEquals(j, l.get(j));
718 <            while (q.poll() != null)
699 <                ;
718 >            do {} while (q.poll() != null);
719          }
720      }
721  
# Line 848 | Line 867 | public class LinkedTransferQueueTest ext
867       * tryTransfer(null) throws NullPointerException
868       */
869      public void testTryTransfer1() {
870 +        final LinkedTransferQueue q = new LinkedTransferQueue();
871          try {
852            final LinkedTransferQueue q = new LinkedTransferQueue();
872              q.tryTransfer(null);
873              shouldThrow();
874          } catch (NullPointerException success) {}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines