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.62 by jsr166, Sun Jun 14 20:58:14 2015 UTC vs.
Revision 1.66 by jsr166, Sun Oct 18 04:48:32 2015 UTC

# Line 263 | Line 263 | public class LinkedTransferQueueTest ext
263       */
264      public void testTimedPoll() throws InterruptedException {
265          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
266        for (int i = 0; i < SIZE; ++i) {
267            long startTime = System.nanoTime();
268            assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
269            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
270        }
266          long startTime = System.nanoTime();
267 +        for (int i = 0; i < SIZE; ++i)
268 +            assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
269 +        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
270 +
271 +        startTime = System.nanoTime();
272          assertNull(q.poll(timeoutMillis(), MILLISECONDS));
273          assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
274          checkEmpty(q);
# Line 283 | 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 <                for (int i = 0; i < SIZE; ++i) {
287 <                    long t0 = System.nanoTime();
286 >                long startTime = System.nanoTime();
287 >                for (int i = 0; i < SIZE; ++i)
288                      assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
289                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
290                }
291                long t0 = System.nanoTime();
289                  aboutToWait.countDown();
290                  try {
291 <                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
291 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
292                      shouldThrow();
293 <                } catch (InterruptedException success) {
294 <                    assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
298 <                }
293 >                } catch (InterruptedException success) {}
294 >                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
295              }});
296  
297          aboutToWait.await();
298 <        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
298 >        waitForThreadToEnterWaitState(t);
299          t.interrupt();
300 <        awaitTermination(t, MEDIUM_DELAY_MS);
300 >        awaitTermination(t);
301          checkEmpty(q);
302      }
303  
# Line 313 | Line 309 | public class LinkedTransferQueueTest ext
309          final BlockingQueue<Integer> q = populatedQueue(SIZE);
310          Thread t = newStartedThread(new CheckedRunnable() {
311              public void realRun() throws InterruptedException {
312 +                long startTime = System.nanoTime();
313                  Thread.currentThread().interrupt();
314 <                for (int i = 0; i < SIZE; ++i) {
318 <                    long t0 = System.nanoTime();
314 >                for (int i = 0; i < SIZE; ++i)
315                      assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
320                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
321                }
316                  try {
317 <                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
317 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
318                      shouldThrow();
319                  } catch (InterruptedException success) {}
320 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
321              }});
322  
323 <        awaitTermination(t, MEDIUM_DELAY_MS);
323 >        awaitTermination(t);
324          checkEmpty(q);
325      }
326  
# Line 596 | Line 591 | public class LinkedTransferQueueTest ext
591      public void testOfferInExecutor() {
592          final LinkedTransferQueue q = new LinkedTransferQueue();
593          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
594 <        ExecutorService executor = Executors.newFixedThreadPool(2);
595 <
601 <        executor.execute(new CheckedRunnable() {
602 <            public void realRun() throws InterruptedException {
603 <                threadsStarted.await();
604 <                assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS));
605 <            }});
594 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
595 >        try (PoolCleaner cleaner = cleaner(executor)) {
596  
597 <        executor.execute(new CheckedRunnable() {
598 <            public void realRun() throws InterruptedException {
599 <                threadsStarted.await();
600 <                assertSame(one, q.take());
601 <                checkEmpty(q);
602 <            }});
597 >            executor.execute(new CheckedRunnable() {
598 >                public void realRun() throws InterruptedException {
599 >                    threadsStarted.await();
600 >                    long startTime = System.nanoTime();
601 >                    assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS));
602 >                    assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
603 >                }});
604  
605 <        joinPool(executor);
605 >            executor.execute(new CheckedRunnable() {
606 >                public void realRun() throws InterruptedException {
607 >                    threadsStarted.await();
608 >                    assertSame(one, q.take());
609 >                    checkEmpty(q);
610 >                }});
611 >        }
612      }
613  
614      /**
# Line 620 | Line 617 | public class LinkedTransferQueueTest ext
617      public void testPollInExecutor() {
618          final LinkedTransferQueue q = new LinkedTransferQueue();
619          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
620 <        ExecutorService executor = Executors.newFixedThreadPool(2);
620 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
621 >        try (PoolCleaner cleaner = cleaner(executor)) {
622  
623 <        executor.execute(new CheckedRunnable() {
624 <            public void realRun() throws InterruptedException {
625 <                assertNull(q.poll());
626 <                threadsStarted.await();
627 <                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
628 <                checkEmpty(q);
629 <            }});
630 <
631 <        executor.execute(new CheckedRunnable() {
634 <            public void realRun() throws InterruptedException {
635 <                threadsStarted.await();
636 <                q.put(one);
637 <            }});
623 >            executor.execute(new CheckedRunnable() {
624 >                public void realRun() throws InterruptedException {
625 >                    assertNull(q.poll());
626 >                    threadsStarted.await();
627 >                    long startTime = System.nanoTime();
628 >                    assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
629 >                    assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
630 >                    checkEmpty(q);
631 >                }});
632  
633 <        joinPool(executor);
633 >            executor.execute(new CheckedRunnable() {
634 >                public void realRun() throws InterruptedException {
635 >                    threadsStarted.await();
636 >                    q.put(one);
637 >                }});
638 >        }
639      }
640  
641      /**
# Line 697 | Line 696 | public class LinkedTransferQueueTest ext
696          assertTrue(l.size() >= SIZE);
697          for (int i = 0; i < SIZE; ++i)
698              assertEquals(i, l.get(i));
699 <        awaitTermination(t, MEDIUM_DELAY_MS);
699 >        awaitTermination(t);
700          assertTrue(q.size() + l.size() >= SIZE);
701      }
702  
# Line 734 | Line 733 | public class LinkedTransferQueueTest ext
733          Thread t = newStartedThread(new CheckedRunnable() {
734              public void realRun() throws InterruptedException {
735                  threadStarted.countDown();
736 +                long startTime = System.nanoTime();
737                  assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
738                  assertEquals(0, q.getWaitingConsumerCount());
739                  assertFalse(q.hasWaitingConsumer());
740 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
741              }});
742  
743          threadStarted.await();
744 <        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
744 >        waitForThreadToEnterWaitState(t);
745          assertEquals(1, q.getWaitingConsumerCount());
746          assertTrue(q.hasWaitingConsumer());
747  
# Line 748 | Line 749 | public class LinkedTransferQueueTest ext
749          assertEquals(0, q.getWaitingConsumerCount());
750          assertFalse(q.hasWaitingConsumer());
751  
752 <        awaitTermination(t, MEDIUM_DELAY_MS);
752 >        awaitTermination(t);
753      }
754  
755      /**
# Line 779 | Line 780 | public class LinkedTransferQueueTest ext
780              }});
781  
782          threadStarted.await();
783 <        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
783 >        waitForThreadToEnterWaitState(t);
784          assertEquals(1, q.size());
785          assertSame(five, q.poll());
786          checkEmpty(q);
787 <        awaitTermination(t, MEDIUM_DELAY_MS);
787 >        awaitTermination(t);
788      }
789  
790      /**
# Line 839 | Line 840 | public class LinkedTransferQueueTest ext
840          assertEquals(1, q.size());
841          assertTrue(q.offer(three));
842          assertSame(four, q.poll());
843 <        awaitTermination(t, MEDIUM_DELAY_MS);
843 >        awaitTermination(t);
844      }
845  
846      /**
# Line 862 | Line 863 | public class LinkedTransferQueueTest ext
863          assertEquals(1, q.size());
864          assertSame(four, q.take());
865          checkEmpty(q);
866 <        awaitTermination(t, MEDIUM_DELAY_MS);
866 >        awaitTermination(t);
867      }
868  
869      /**
# Line 904 | Line 905 | public class LinkedTransferQueueTest ext
905                  assertTrue(q.tryTransfer(hotPotato));
906              }});
907  
908 <        assertSame(hotPotato, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
908 >        long startTime = System.nanoTime();
909 >        assertSame(hotPotato, q.poll(LONG_DELAY_MS, MILLISECONDS));
910 >        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
911          checkEmpty(q);
912 <        awaitTermination(t, MEDIUM_DELAY_MS);
912 >        awaitTermination(t);
913      }
914  
915      /**
# Line 928 | Line 931 | public class LinkedTransferQueueTest ext
931  
932          assertSame(q.take(), hotPotato);
933          checkEmpty(q);
934 <        awaitTermination(t, MEDIUM_DELAY_MS);
934 >        awaitTermination(t);
935      }
936  
937      /**
# Line 941 | Line 944 | public class LinkedTransferQueueTest ext
944  
945          Thread t = newStartedThread(new CheckedRunnable() {
946              public void realRun() throws InterruptedException {
947 +                long startTime = System.nanoTime();
948                  Thread.currentThread().interrupt();
949                  try {
950                      q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS);
# Line 954 | Line 958 | public class LinkedTransferQueueTest ext
958                      shouldThrow();
959                  } catch (InterruptedException success) {}
960                  assertFalse(Thread.interrupted());
961 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
962              }});
963  
964          await(pleaseInterrupt);
# Line 971 | Line 976 | public class LinkedTransferQueueTest ext
976  
977          Thread t = newStartedThread(new CheckedRunnable() {
978              public void realRun() throws InterruptedException {
979 <                long t0 = System.nanoTime();
979 >                long startTime = System.nanoTime();
980                  assertFalse(q.tryTransfer(new Object(),
981                                            timeoutMillis(), MILLISECONDS));
982 <                assertTrue(millisElapsedSince(t0) >= timeoutMillis());
982 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
983                  checkEmpty(q);
984              }});
985  
# Line 992 | Line 997 | public class LinkedTransferQueueTest ext
997  
998          Thread t = newStartedThread(new CheckedRunnable() {
999              public void realRun() throws InterruptedException {
1000 <                assertTrue(q.tryTransfer(five, MEDIUM_DELAY_MS, MILLISECONDS));
1000 >                long startTime = System.nanoTime();
1001 >                assertTrue(q.tryTransfer(five, LONG_DELAY_MS, MILLISECONDS));
1002 >                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1003                  checkEmpty(q);
1004              }});
1005  
# Line 1002 | Line 1009 | public class LinkedTransferQueueTest ext
1009          assertSame(four, q.poll());
1010          assertSame(five, q.poll());
1011          checkEmpty(q);
1012 <        awaitTermination(t, MEDIUM_DELAY_MS);
1012 >        awaitTermination(t);
1013      }
1014  
1015      /**
# Line 1013 | Line 1020 | public class LinkedTransferQueueTest ext
1020          final LinkedTransferQueue q = new LinkedTransferQueue();
1021          assertTrue(q.offer(four));
1022          assertEquals(1, q.size());
1023 <        long t0 = System.nanoTime();
1023 >        long startTime = System.nanoTime();
1024          assertFalse(q.tryTransfer(five, timeoutMillis(), MILLISECONDS));
1025 <        assertTrue(millisElapsedSince(t0) >= timeoutMillis());
1025 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
1026          assertEquals(1, q.size());
1027          assertSame(four, q.poll());
1028          assertNull(q.poll());

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines