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.30 by jsr166, Thu Oct 28 22:20:47 2010 UTC vs.
Revision 1.31 by jsr166, Thu Oct 28 22:42:05 2010 UTC

# Line 40 | Line 40 | public class LinkedTransferQueueTest ext
40                              new Generic().testSuite());
41      }
42  
43 <    void checkEmpty(LinkedTransferQueue q) throws InterruptedException {
44 <        assertTrue(q.isEmpty());
45 <        assertEquals(0, q.size());
46 <        assertNull(q.peek());
47 <        assertNull(q.poll());
48 <        assertNull(q.poll(0, MILLISECONDS));
49 <        assertEquals(q.toString(), "[]");
50 <        assertTrue(Arrays.equals(q.toArray(), new Object[0]));
51 <        assertFalse(q.iterator().hasNext());
52 <        try {
53 <            q.element();
54 <            shouldThrow();
55 <        } catch (NoSuchElementException success) {}
43 >    void checkEmpty(BlockingQueue q) {
44          try {
45 <            q.iterator().next();
46 <            shouldThrow();
47 <        } catch (NoSuchElementException success) {}
48 <        try {
49 <            q.remove();
50 <            shouldThrow();
51 <        } catch (NoSuchElementException success) {}
45 >            assertTrue(q.isEmpty());
46 >            assertEquals(0, q.size());
47 >            assertNull(q.peek());
48 >            assertNull(q.poll());
49 >            assertNull(q.poll(0, MILLISECONDS));
50 >            assertEquals(q.toString(), "[]");
51 >            assertTrue(Arrays.equals(q.toArray(), new Object[0]));
52 >            assertFalse(q.iterator().hasNext());
53 >            try {
54 >                q.element();
55 >                shouldThrow();
56 >            } catch (NoSuchElementException success) {}
57 >            try {
58 >                q.iterator().next();
59 >                shouldThrow();
60 >            } catch (NoSuchElementException success) {}
61 >            try {
62 >                q.remove();
63 >                shouldThrow();
64 >            } catch (NoSuchElementException success) {}
65 >        } catch (InterruptedException ie) {
66 >            threadUnexpectedException(ie);
67 >        }
68      }
69  
70      /**
# Line 277 | Line 281 | public class LinkedTransferQueueTest ext
281       * take removes existing elements until empty, then blocks interruptibly
282       */
283      public void testBlockingTake() throws InterruptedException {
284 <        final LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
285 <        Thread t = new Thread(new CheckedRunnable() {
284 >        final BlockingQueue<Integer> q = populatedQueue(SIZE);
285 >        final CountDownLatch aboutToWait = new CountDownLatch(1);
286 >        Thread t = newStartedThread(new CheckedRunnable() {
287              public void realRun() throws InterruptedException {
288                  for (int i = 0; i < SIZE; ++i) {
289                      assertEquals(i, (int) q.take());
290                  }
291 +                aboutToWait.countDown();
292                  try {
293                      q.take();
294                      shouldThrow();
295                  } catch (InterruptedException success) {}
296              }});
297  
298 <        t.start();
299 <        Thread.sleep(SHORT_DELAY_MS);
298 >        aboutToWait.await();
299 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
300          t.interrupt();
301          awaitTermination(t, MEDIUM_DELAY_MS);
302          checkEmpty(q);
# Line 327 | Line 333 | public class LinkedTransferQueueTest ext
333          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
334          for (int i = 0; i < SIZE; ++i) {
335              long t0 = System.nanoTime();
336 <            assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
336 >            assertEquals(i, (int) q.poll(SMALL_DELAY_MS, MILLISECONDS));
337              assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
338          }
339 +        long t0 = System.nanoTime();
340          assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
341 +        assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
342          checkEmpty(q);
343      }
344  
# Line 339 | Line 347 | public class LinkedTransferQueueTest ext
347       * returning timeout status
348       */
349      public void testInterruptedTimedPoll() throws InterruptedException {
350 <        final LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
350 >        final BlockingQueue<Integer> q = populatedQueue(SIZE);
351 >        final CountDownLatch aboutToWait = new CountDownLatch(1);
352          Thread t = newStartedThread(new CheckedRunnable() {
353              public void realRun() throws InterruptedException {
354                  for (int i = 0; i < SIZE; ++i) {
355                      long t0 = System.nanoTime();
356                      assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
357 <                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
357 >                    assertTrue(millisElapsedSince(t0) < SHORT_DELAY_MS);
358                  }
359 +                aboutToWait.countDown();
360                  try {
361 <                    q.poll(LONG_DELAY_MS, MILLISECONDS);
361 >                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
362                      shouldThrow();
363                  } catch (InterruptedException success) {}
364              }});
365  
366 <        Thread.sleep(SMALL_DELAY_MS);
366 >        aboutToWait.await();
367 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
368          t.interrupt();
369          awaitTermination(t, MEDIUM_DELAY_MS);
370          checkEmpty(q);
371      }
372  
373      /**
374 +     * timed poll after thread interrupted throws InterruptedException
375 +     * instead of returning timeout status
376 +     */
377 +    public void testTimedPollAfterInterrupt() throws InterruptedException {
378 +        final BlockingQueue<Integer> q = populatedQueue(SIZE);
379 +        Thread t = newStartedThread(new CheckedRunnable() {
380 +            public void realRun() throws InterruptedException {
381 +                Thread.currentThread().interrupt();
382 +                for (int i = 0; i < SIZE; ++i) {
383 +                    long t0 = System.nanoTime();
384 +                    assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
385 +                    assertTrue(millisElapsedSince(t0) < SHORT_DELAY_MS);
386 +                }
387 +                try {
388 +                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
389 +                    shouldThrow();
390 +                } catch (InterruptedException success) {}
391 +            }});
392 +
393 +        awaitTermination(t, MEDIUM_DELAY_MS);
394 +        checkEmpty(q);
395 +    }
396 +
397 +    /**
398       * peek returns next element, or null if empty
399       */
400      public void testPeek() throws InterruptedException {
# Line 637 | Line 672 | public class LinkedTransferQueueTest ext
672       */
673      public void testOfferInExecutor() {
674          final LinkedTransferQueue q = new LinkedTransferQueue();
675 <        q.add(one);
641 <        q.add(two);
675 >        final CountDownLatch threadsStarted = new CountDownLatch(2);
676          ExecutorService executor = Executors.newFixedThreadPool(2);
677  
678          executor.execute(new CheckedRunnable() {
679 <            public void realRun() {
680 <                assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
679 >            public void realRun() throws InterruptedException {
680 >                threadsStarted.countDown();
681 >                threadsStarted.await();
682 >                assertTrue(q.offer(one, MEDIUM_DELAY_MS, MILLISECONDS));
683              }});
684  
685          executor.execute(new CheckedRunnable() {
686              public void realRun() throws InterruptedException {
687 <                Thread.sleep(SMALL_DELAY_MS);
687 >                threadsStarted.countDown();
688 >                threadsStarted.await();
689                  assertSame(one, q.take());
690 +                checkEmpty(q);
691              }});
692  
693          joinPool(executor);
# Line 660 | Line 698 | public class LinkedTransferQueueTest ext
698       */
699      public void testPollInExecutor() {
700          final LinkedTransferQueue q = new LinkedTransferQueue();
701 +        final CountDownLatch threadsStarted = new CountDownLatch(2);
702          ExecutorService executor = Executors.newFixedThreadPool(2);
703  
704          executor.execute(new CheckedRunnable() {
705              public void realRun() throws InterruptedException {
706                  assertNull(q.poll());
707 <                assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
708 <                assertTrue(q.isEmpty());
707 >                threadsStarted.countDown();
708 >                threadsStarted.await();
709 >                assertSame(one, q.poll(SMALL_DELAY_MS, MILLISECONDS));
710 >                checkEmpty(q);
711              }});
712  
713          executor.execute(new CheckedRunnable() {
714              public void realRun() throws InterruptedException {
715 <                Thread.sleep(SMALL_DELAY_MS);
715 >                threadsStarted.countDown();
716 >                threadsStarted.await();
717                  q.put(one);
718              }});
719  
# Line 821 | Line 863 | public class LinkedTransferQueueTest ext
863          final LinkedTransferQueue q = new LinkedTransferQueue();
864          assertEquals(q.getWaitingConsumerCount(), 0);
865          assertFalse(q.hasWaitingConsumer());
866 +        final CountDownLatch threadStarted = new CountDownLatch(1);
867  
868          Thread t = newStartedThread(new CheckedRunnable() {
869              public void realRun() throws InterruptedException {
870 <                Thread.sleep(SMALL_DELAY_MS);
871 <                assertTrue(q.hasWaitingConsumer());
829 <                assertEquals(q.getWaitingConsumerCount(), 1);
830 <                assertTrue(q.offer(one));
831 <                assertFalse(q.hasWaitingConsumer());
870 >                threadStarted.countDown();
871 >                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
872                  assertEquals(q.getWaitingConsumerCount(), 0);
873 +                assertFalse(q.hasWaitingConsumer());
874              }});
875  
876 <        assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
876 >        threadStarted.await();
877 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
878 >        assertEquals(q.getWaitingConsumerCount(), 1);
879 >        assertTrue(q.hasWaitingConsumer());
880 >
881 >        assertTrue(q.offer(one));
882          assertEquals(q.getWaitingConsumerCount(), 0);
883          assertFalse(q.hasWaitingConsumer());
884 +
885          awaitTermination(t, MEDIUM_DELAY_MS);
886      }
887  
# Line 856 | Line 903 | public class LinkedTransferQueueTest ext
903      public void testTransfer2() throws InterruptedException {
904          final LinkedTransferQueue<Integer> q
905              = new LinkedTransferQueue<Integer>();
906 +        final CountDownLatch threadStarted = new CountDownLatch(1);
907  
908          Thread t = newStartedThread(new CheckedRunnable() {
909              public void realRun() throws InterruptedException {
910 +                threadStarted.countDown();
911                  q.transfer(SIZE);
912 <                assertTrue(q.isEmpty());
912 >                checkEmpty(q);
913              }});
914  
915 <        Thread.sleep(SHORT_DELAY_MS);
915 >        threadStarted.await();
916 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
917 >        assertTrue(t.isAlive());
918          assertEquals(1, q.size());
919          assertEquals(SIZE, (int) q.poll());
920 <        assertTrue(q.isEmpty());
920 >        checkEmpty(q);
921          awaitTermination(t, MEDIUM_DELAY_MS);
922      }
923  
# Line 901 | Line 952 | public class LinkedTransferQueueTest ext
952          assertEquals(1, q.size());
953          interruptedThread.interrupt();
954          interruptedThread.join();
955 <        assertEquals(0, q.size());
905 <        assertTrue(q.isEmpty());
955 >        checkEmpty(q);
956      }
957  
958      /**
# Line 919 | Line 969 | public class LinkedTransferQueueTest ext
969                  assertSame(three, q.poll());
970              }});
971  
972 <        Thread.sleep(SHORT_DELAY_MS);
972 >        while (q.isEmpty())
973 >            Thread.yield();
974 >        assertFalse(q.isEmpty());
975 >        assertEquals(1, q.size());
976          assertTrue(q.offer(three));
977          assertSame(four, q.poll());
978          awaitTermination(t, MEDIUM_DELAY_MS);
# Line 935 | Line 988 | public class LinkedTransferQueueTest ext
988  
989          Thread t = newStartedThread(new CheckedRunnable() {
990              public void realRun() throws InterruptedException {
991 <                q.transfer(SIZE);
991 >                q.transfer(four);
992                  checkEmpty(q);
993              }});
994  
995 <        Thread.sleep(SHORT_DELAY_MS);
996 <        assertEquals(SIZE, (int) q.take());
995 >        while (q.isEmpty())
996 >            Thread.yield();
997 >        assertFalse(q.isEmpty());
998 >        assertEquals(1, q.size());
999 >        assertSame(four, q.take());
1000          checkEmpty(q);
1001          awaitTermination(t, MEDIUM_DELAY_MS);
1002      }
# Line 980 | Line 1036 | public class LinkedTransferQueueTest ext
1036                  while (! q.hasWaitingConsumer())
1037                      Thread.yield();
1038                  assertTrue(q.hasWaitingConsumer());
1039 <                assertTrue(q.isEmpty());
984 <                assertEquals(q.size(), 0);
1039 >                checkEmpty(q);
1040                  assertTrue(q.tryTransfer(hotPotato));
1041              }});
1042  
# Line 1003 | Line 1058 | public class LinkedTransferQueueTest ext
1058                  while (! q.hasWaitingConsumer())
1059                      Thread.yield();
1060                  assertTrue(q.hasWaitingConsumer());
1061 <                assertTrue(q.isEmpty());
1007 <                assertEquals(q.size(), 0);
1061 >                checkEmpty(q);
1062                  assertTrue(q.tryTransfer(hotPotato));
1063              }});
1064  
# Line 1019 | Line 1073 | public class LinkedTransferQueueTest ext
1073       */
1074      public void testTryTransfer5() throws InterruptedException {
1075          final LinkedTransferQueue q = new LinkedTransferQueue();
1076 +        final CountDownLatch threadStarted = new CountDownLatch(1);
1077  
1078 <        Thread toInterrupt = newStartedThread(new CheckedInterruptedRunnable() {
1078 >        Thread t = newStartedThread(new CheckedRunnable() {
1079              public void realRun() throws InterruptedException {
1080 <                q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS);
1080 >                long t0 = System.nanoTime();
1081 >                threadStarted.countDown();
1082 >                try {
1083 >                    q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS);
1084 >                    shouldThrow();
1085 >                } catch (InterruptedException success) {}
1086 >                assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
1087              }});
1088  
1089 <        Thread.sleep(SMALL_DELAY_MS);
1090 <        toInterrupt.interrupt();
1091 <        toInterrupt.join();
1089 >        threadStarted.await();
1090 >        Thread.sleep(SHORT_DELAY_MS);
1091 >        t.interrupt();
1092 >        awaitTermination(t, MEDIUM_DELAY_MS);
1093 >        checkEmpty(q);
1094      }
1095  
1096      /**
# Line 1060 | Line 1123 | public class LinkedTransferQueueTest ext
1123          Thread t = newStartedThread(new CheckedRunnable() {
1124              public void realRun() throws InterruptedException {
1125                  assertTrue(q.tryTransfer(five, MEDIUM_DELAY_MS, MILLISECONDS));
1126 <                assertTrue(q.isEmpty());
1126 >                checkEmpty(q);
1127              }});
1128  
1129          Thread.sleep(SHORT_DELAY_MS);
# Line 1088 | Line 1151 | public class LinkedTransferQueueTest ext
1151  
1152      private LinkedTransferQueue<Integer> populatedQueue(int n) {
1153          LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
1154 <        assertTrue(q.isEmpty());
1154 >        checkEmpty(q);
1155          for (int i = 0; i < n; i++) {
1156              assertEquals(i, q.size());
1157              assertTrue(q.offer(i));

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines