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.60 by jsr166, Fri May 15 18:21:19 2015 UTC vs.
Revision 1.65 by jsr166, Tue Oct 6 00:36:55 2015 UTC

# Line 24 | Line 24 | 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 37 | Line 44 | public class LinkedTransferQueueTest ext
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 78 | 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 275 | 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) {
279                    long t0 = System.nanoTime();
288                      assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
281                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
289                  }
283                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 307 | Line 313 | public class LinkedTransferQueueTest ext
313              public void realRun() throws InterruptedException {
314                  Thread.currentThread().interrupt();
315                  for (int i = 0; i < SIZE; ++i) {
316 <                    long t0 = System.nanoTime();
316 >                    long startTime = System.nanoTime();
317                      assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
318 <                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
318 >                    assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
319                  }
320                  try {
321                      q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
# Line 588 | 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);
598 <
593 <        executor.execute(new CheckedRunnable() {
594 <            public void realRun() throws InterruptedException {
595 <                threadsStarted.await();
596 <                assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS));
597 <            }});
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 <                assertSame(one, q.take());
604 <                checkEmpty(q);
604 <            }});
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 612 | 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);
622 <
617 <        executor.execute(new CheckedRunnable() {
618 <            public void realRun() throws InterruptedException {
619 <                assertNull(q.poll());
620 <                threadsStarted.await();
621 <                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
622 <                checkEmpty(q);
623 <            }});
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 <                threadsStarted.await();
627 <                q.put(one);
628 <            }});
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 963 | Line 969 | public class LinkedTransferQueueTest ext
969  
970          Thread t = newStartedThread(new CheckedRunnable() {
971              public void realRun() throws InterruptedException {
972 <                long t0 = System.nanoTime();
972 >                long startTime = System.nanoTime();
973                  assertFalse(q.tryTransfer(new Object(),
974                                            timeoutMillis(), MILLISECONDS));
975 <                assertTrue(millisElapsedSince(t0) >= timeoutMillis());
975 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
976                  checkEmpty(q);
977              }});
978  
# Line 1005 | Line 1011 | public class LinkedTransferQueueTest ext
1011          final LinkedTransferQueue q = new LinkedTransferQueue();
1012          assertTrue(q.offer(four));
1013          assertEquals(1, q.size());
1014 <        long t0 = System.nanoTime();
1014 >        long startTime = System.nanoTime();
1015          assertFalse(q.tryTransfer(five, timeoutMillis(), MILLISECONDS));
1016 <        assertTrue(millisElapsedSince(t0) >= timeoutMillis());
1016 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
1017          assertEquals(1, q.size());
1018          assertSame(four, q.poll());
1019          assertNull(q.poll());

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines