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.44 by jsr166, Tue Mar 15 19:47:06 2011 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   * Other contributors include John Vint
6   */
7  
# 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) {}
56        try {
57            q.iterator().next();
58            shouldThrow();
59        } catch (NoSuchElementException success) {}
60        try {
61            q.remove();
62            shouldThrow();
63        } catch (NoSuchElementException success) {}
64    }
65
43      /**
44       * Constructor builds new queue with size being zero and empty
45       * being true
# Line 277 | Line 254 | public class LinkedTransferQueueTest ext
254       * take removes existing elements until empty, then blocks interruptibly
255       */
256      public void testBlockingTake() throws InterruptedException {
257 <        final LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
258 <        Thread t = new Thread(new CheckedRunnable() {
257 >        final BlockingQueue<Integer> q = populatedQueue(SIZE);
258 >        final CountDownLatch aboutToWait = new CountDownLatch(1);
259 >        Thread t = newStartedThread(new CheckedRunnable() {
260              public void realRun() throws InterruptedException {
261                  for (int i = 0; i < SIZE; ++i) {
262                      assertEquals(i, (int) q.take());
263                  }
264 +                aboutToWait.countDown();
265                  try {
266                      q.take();
267                      shouldThrow();
268                  } catch (InterruptedException success) {}
269              }});
270  
271 <        t.start();
272 <        Thread.sleep(SHORT_DELAY_MS);
271 >        aboutToWait.await();
272 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
273          t.interrupt();
274          awaitTermination(t, MEDIUM_DELAY_MS);
275          checkEmpty(q);
# Line 327 | Line 306 | public class LinkedTransferQueueTest ext
306          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
307          for (int i = 0; i < SIZE; ++i) {
308              long t0 = System.nanoTime();
309 <            assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
309 >            assertEquals(i, (int) q.poll(SMALL_DELAY_MS, MILLISECONDS));
310              assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
311          }
312 +        long t0 = System.nanoTime();
313          assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
314 +        assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
315          checkEmpty(q);
316      }
317  
# Line 339 | Line 320 | public class LinkedTransferQueueTest ext
320       * returning timeout status
321       */
322      public void testInterruptedTimedPoll() throws InterruptedException {
323 <        final LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
323 >        final BlockingQueue<Integer> q = populatedQueue(SIZE);
324 >        final CountDownLatch aboutToWait = new CountDownLatch(1);
325          Thread t = newStartedThread(new CheckedRunnable() {
326              public void realRun() throws InterruptedException {
327                  for (int i = 0; i < SIZE; ++i) {
# Line 347 | Line 329 | public class LinkedTransferQueueTest ext
329                      assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
330                      assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
331                  }
332 +                long t0 = System.nanoTime();
333 +                aboutToWait.countDown();
334                  try {
335 <                    q.poll(LONG_DELAY_MS, MILLISECONDS);
335 >                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
336                      shouldThrow();
337 <                } catch (InterruptedException success) {}
337 >                } catch (InterruptedException success) {
338 >                    assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
339 >                }
340              }});
341  
342 <        Thread.sleep(SMALL_DELAY_MS);
342 >        aboutToWait.await();
343 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
344          t.interrupt();
345          awaitTermination(t, MEDIUM_DELAY_MS);
346          checkEmpty(q);
347      }
348  
349      /**
350 +     * timed poll after thread interrupted throws InterruptedException
351 +     * instead of returning timeout status
352 +     */
353 +    public void testTimedPollAfterInterrupt() throws InterruptedException {
354 +        final BlockingQueue<Integer> q = populatedQueue(SIZE);
355 +        Thread t = newStartedThread(new CheckedRunnable() {
356 +            public void realRun() throws InterruptedException {
357 +                Thread.currentThread().interrupt();
358 +                for (int i = 0; i < SIZE; ++i) {
359 +                    long t0 = System.nanoTime();
360 +                    assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
361 +                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
362 +                }
363 +                try {
364 +                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
365 +                    shouldThrow();
366 +                } catch (InterruptedException success) {}
367 +            }});
368 +
369 +        awaitTermination(t, MEDIUM_DELAY_MS);
370 +        checkEmpty(q);
371 +    }
372 +
373 +    /**
374       * peek returns next element, or null if empty
375       */
376      public void testPeek() throws InterruptedException {
# Line 410 | Line 421 | public class LinkedTransferQueueTest ext
421       */
422      public void testRemoveElement() throws InterruptedException {
423          LinkedTransferQueue q = populatedQueue(SIZE);
424 <        for (int i = 1; i < SIZE; i += 2) {
424 >        for (int i = 1; i < SIZE; i+=2) {
425 >            assertTrue(q.contains(i));
426              assertTrue(q.remove(i));
427 +            assertFalse(q.contains(i));
428 +            assertTrue(q.contains(i-1));
429          }
430 <        for (int i = 0; i < SIZE; i += 2) {
430 >        for (int i = 0; i < SIZE; i+=2) {
431 >            assertTrue(q.contains(i));
432              assertTrue(q.remove(i));
433 <            assertFalse(q.remove(i + 1));
433 >            assertFalse(q.contains(i));
434 >            assertFalse(q.remove(i+1));
435 >            assertFalse(q.contains(i+1));
436          }
437          checkEmpty(q);
438      }
# Line 512 | Line 529 | public class LinkedTransferQueueTest ext
529      }
530  
531      /**
532 <     * toArray() contains all elements
532 >     * toArray() contains all elements in FIFO order
533       */
534 <    public void testToArray() throws InterruptedException {
534 >    public void testToArray() {
535          LinkedTransferQueue q = populatedQueue(SIZE);
536          Object[] o = q.toArray();
537          for (int i = 0; i < o.length; i++) {
538 <            assertEquals(o[i], q.take());
538 >            assertSame(o[i], q.poll());
539          }
540      }
541  
542      /**
543 <     * toArray(a) contains all elements
543 >     * toArray(a) contains all elements in FIFO order
544       */
545 <    public void testToArray2() throws InterruptedException {
545 >    public void testToArray2() {
546          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
547          Integer[] ints = new Integer[SIZE];
548 <        ints = q.toArray(ints);
548 >        Integer[] array = q.toArray(ints);
549 >        assertSame(ints, array);
550          for (int i = 0; i < ints.length; i++) {
551 <            assertEquals(ints[i], q.take());
551 >            assertSame(ints[i], q.poll());
552          }
553      }
554  
555      /**
556       * toArray(null) throws NullPointerException
557       */
558 <    public void testToArray_BadArg() {
558 >    public void testToArray_NullArg() {
559          LinkedTransferQueue q = populatedQueue(SIZE);
560          try {
561 <            Object o[] = q.toArray(null);
561 >            q.toArray(null);
562              shouldThrow();
563          } catch (NullPointerException success) {}
564      }
565  
566      /**
567 <     * toArray(incompatible array type) throws CCE
567 >     * toArray(incompatible array type) throws ArrayStoreException
568       */
569      public void testToArray1_BadArg() {
570          LinkedTransferQueue q = populatedQueue(SIZE);
571          try {
572 <            Object o[] = q.toArray(new String[10]);
572 >            q.toArray(new String[10]);
573              shouldThrow();
574          } catch (ArrayStoreException success) {}
575      }
# Line 637 | Line 655 | public class LinkedTransferQueueTest ext
655       */
656      public void testOfferInExecutor() {
657          final LinkedTransferQueue q = new LinkedTransferQueue();
658 <        q.add(one);
641 <        q.add(two);
658 >        final CountDownLatch threadsStarted = new CountDownLatch(2);
659          ExecutorService executor = Executors.newFixedThreadPool(2);
660  
661          executor.execute(new CheckedRunnable() {
662 <            public void realRun() {
663 <                assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
662 >            public void realRun() throws InterruptedException {
663 >                threadsStarted.countDown();
664 >                threadsStarted.await();
665 >                assertTrue(q.offer(one, MEDIUM_DELAY_MS, MILLISECONDS));
666              }});
667  
668          executor.execute(new CheckedRunnable() {
669              public void realRun() throws InterruptedException {
670 <                Thread.sleep(SMALL_DELAY_MS);
670 >                threadsStarted.countDown();
671 >                threadsStarted.await();
672                  assertSame(one, q.take());
673 +                checkEmpty(q);
674              }});
675  
676          joinPool(executor);
# Line 660 | Line 681 | public class LinkedTransferQueueTest ext
681       */
682      public void testPollInExecutor() {
683          final LinkedTransferQueue q = new LinkedTransferQueue();
684 +        final CountDownLatch threadsStarted = new CountDownLatch(2);
685          ExecutorService executor = Executors.newFixedThreadPool(2);
686  
687          executor.execute(new CheckedRunnable() {
688              public void realRun() throws InterruptedException {
689                  assertNull(q.poll());
690 <                assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
691 <                assertTrue(q.isEmpty());
690 >                threadsStarted.countDown();
691 >                threadsStarted.await();
692 >                assertSame(one, q.poll(SMALL_DELAY_MS, MILLISECONDS));
693 >                checkEmpty(q);
694              }});
695  
696          executor.execute(new CheckedRunnable() {
697              public void realRun() throws InterruptedException {
698 <                Thread.sleep(SMALL_DELAY_MS);
698 >                threadsStarted.countDown();
699 >                threadsStarted.await();
700                  q.put(one);
701              }});
702  
# Line 697 | Line 722 | public class LinkedTransferQueueTest ext
722          LinkedTransferQueue r = (LinkedTransferQueue) in.readObject();
723  
724          assertEquals(q.size(), r.size());
725 +        assertEquals(q.toString(), r.toString());
726 +        assertTrue(Arrays.equals(q.toArray(), r.toArray()));
727          while (!q.isEmpty()) {
728              assertEquals(q.remove(), r.remove());
729          }
# Line 821 | Line 848 | public class LinkedTransferQueueTest ext
848          final LinkedTransferQueue q = new LinkedTransferQueue();
849          assertEquals(q.getWaitingConsumerCount(), 0);
850          assertFalse(q.hasWaitingConsumer());
851 +        final CountDownLatch threadStarted = new CountDownLatch(1);
852  
853          Thread t = newStartedThread(new CheckedRunnable() {
854              public void realRun() throws InterruptedException {
855 <                Thread.sleep(SMALL_DELAY_MS);
856 <                assertTrue(q.hasWaitingConsumer());
829 <                assertEquals(q.getWaitingConsumerCount(), 1);
830 <                assertTrue(q.offer(one));
831 <                assertFalse(q.hasWaitingConsumer());
855 >                threadStarted.countDown();
856 >                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
857                  assertEquals(q.getWaitingConsumerCount(), 0);
858 +                assertFalse(q.hasWaitingConsumer());
859              }});
860  
861 <        assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
861 >        threadStarted.await();
862 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
863 >        assertEquals(q.getWaitingConsumerCount(), 1);
864 >        assertTrue(q.hasWaitingConsumer());
865 >
866 >        assertTrue(q.offer(one));
867          assertEquals(q.getWaitingConsumerCount(), 0);
868          assertFalse(q.hasWaitingConsumer());
869 +
870          awaitTermination(t, MEDIUM_DELAY_MS);
871      }
872  
# Line 856 | Line 888 | public class LinkedTransferQueueTest ext
888      public void testTransfer2() throws InterruptedException {
889          final LinkedTransferQueue<Integer> q
890              = new LinkedTransferQueue<Integer>();
891 +        final CountDownLatch threadStarted = new CountDownLatch(1);
892  
893          Thread t = newStartedThread(new CheckedRunnable() {
894              public void realRun() throws InterruptedException {
895 <                q.transfer(SIZE);
896 <                assertTrue(q.isEmpty());
895 >                threadStarted.countDown();
896 >                q.transfer(five);
897 >                checkEmpty(q);
898              }});
899  
900 <        Thread.sleep(SHORT_DELAY_MS);
900 >        threadStarted.await();
901 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
902          assertEquals(1, q.size());
903 <        assertEquals(SIZE, (int) q.poll());
904 <        assertTrue(q.isEmpty());
903 >        assertSame(five, q.poll());
904 >        checkEmpty(q);
905          awaitTermination(t, MEDIUM_DELAY_MS);
906      }
907  
# Line 879 | Line 914 | public class LinkedTransferQueueTest ext
914  
915          Thread first = newStartedThread(new CheckedRunnable() {
916              public void realRun() throws InterruptedException {
917 <                Integer i = SIZE + 1;
918 <                q.transfer(i);
884 <                assertTrue(!q.contains(i));
917 >                q.transfer(four);
918 >                assertTrue(!q.contains(four));
919                  assertEquals(1, q.size());
920              }});
921  
922          Thread interruptedThread = newStartedThread(
923              new CheckedInterruptedRunnable() {
924                  public void realRun() throws InterruptedException {
925 <                    while (q.size() == 0)
925 >                    while (q.isEmpty())
926                          Thread.yield();
927 <                    q.transfer(SIZE);
927 >                    q.transfer(five);
928                  }});
929  
930          while (q.size() < 2)
931              Thread.yield();
932          assertEquals(2, q.size());
933 <        assertEquals(SIZE + 1, (int) q.poll());
933 >        assertSame(four, q.poll());
934          first.join();
935          assertEquals(1, q.size());
936          interruptedThread.interrupt();
937          interruptedThread.join();
938 <        assertEquals(0, q.size());
905 <        assertTrue(q.isEmpty());
938 >        checkEmpty(q);
939      }
940  
941      /**
# Line 919 | Line 952 | public class LinkedTransferQueueTest ext
952                  assertSame(three, q.poll());
953              }});
954  
955 <        Thread.sleep(SHORT_DELAY_MS);
955 >        while (q.isEmpty())
956 >            Thread.yield();
957 >        assertFalse(q.isEmpty());
958 >        assertEquals(1, q.size());
959          assertTrue(q.offer(three));
960          assertSame(four, q.poll());
961          awaitTermination(t, MEDIUM_DELAY_MS);
# Line 935 | Line 971 | public class LinkedTransferQueueTest ext
971  
972          Thread t = newStartedThread(new CheckedRunnable() {
973              public void realRun() throws InterruptedException {
974 <                q.transfer(SIZE);
974 >                q.transfer(four);
975                  checkEmpty(q);
976              }});
977  
978 <        Thread.sleep(SHORT_DELAY_MS);
979 <        assertEquals(SIZE, (int) q.take());
978 >        while (q.isEmpty())
979 >            Thread.yield();
980 >        assertFalse(q.isEmpty());
981 >        assertEquals(1, q.size());
982 >        assertSame(four, q.take());
983          checkEmpty(q);
984          awaitTermination(t, MEDIUM_DELAY_MS);
985      }
# Line 980 | Line 1019 | public class LinkedTransferQueueTest ext
1019                  while (! q.hasWaitingConsumer())
1020                      Thread.yield();
1021                  assertTrue(q.hasWaitingConsumer());
1022 <                assertTrue(q.isEmpty());
984 <                assertEquals(q.size(), 0);
1022 >                checkEmpty(q);
1023                  assertTrue(q.tryTransfer(hotPotato));
1024              }});
1025  
# Line 1003 | Line 1041 | public class LinkedTransferQueueTest ext
1041                  while (! q.hasWaitingConsumer())
1042                      Thread.yield();
1043                  assertTrue(q.hasWaitingConsumer());
1044 <                assertTrue(q.isEmpty());
1007 <                assertEquals(q.size(), 0);
1044 >                checkEmpty(q);
1045                  assertTrue(q.tryTransfer(hotPotato));
1046              }});
1047  
# Line 1014 | Line 1051 | public class LinkedTransferQueueTest ext
1051      }
1052  
1053      /**
1054 <     * tryTransfer waits the amount given if interrupted, and
1055 <     * throws interrupted exception
1054 >     * tryTransfer waits the amount given, and throws
1055 >     * InterruptedException when interrupted.
1056       */
1057      public void testTryTransfer5() throws InterruptedException {
1058          final LinkedTransferQueue q = new LinkedTransferQueue();
1059 +        final CountDownLatch threadStarted = new CountDownLatch(1);
1060 +        assertTrue(q.isEmpty());
1061  
1062 <        Thread toInterrupt = newStartedThread(new CheckedInterruptedRunnable() {
1062 >        Thread t = newStartedThread(new CheckedRunnable() {
1063              public void realRun() throws InterruptedException {
1064 <                q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS);
1064 >                long t0 = System.nanoTime();
1065 >                threadStarted.countDown();
1066 >                try {
1067 >                    q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS);
1068 >                    shouldThrow();
1069 >                } catch (InterruptedException success) {}
1070 >                assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
1071 >                assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
1072              }});
1073  
1074 <        Thread.sleep(SMALL_DELAY_MS);
1075 <        toInterrupt.interrupt();
1076 <        toInterrupt.join();
1074 >        threadStarted.await();
1075 >        while (q.isEmpty())
1076 >            Thread.yield();
1077 >        Thread.sleep(SHORT_DELAY_MS);
1078 >        t.interrupt();
1079 >        awaitTermination(t, MEDIUM_DELAY_MS);
1080 >        checkEmpty(q);
1081      }
1082  
1083      /**
1084 <     * tryTransfer gives up after the timeout and return false
1084 >     * tryTransfer gives up after the timeout and returns false
1085       */
1086      public void testTryTransfer6() throws InterruptedException {
1087          final LinkedTransferQueue q = new LinkedTransferQueue();
# Line 1042 | Line 1092 | public class LinkedTransferQueueTest ext
1092                  assertFalse(q.tryTransfer(new Object(),
1093                                            SHORT_DELAY_MS, MILLISECONDS));
1094                  assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
1095 +                checkEmpty(q);
1096              }});
1097  
1047        checkEmpty(q);
1098          awaitTermination(t, MEDIUM_DELAY_MS);
1099          checkEmpty(q);
1100      }
# Line 1060 | Line 1110 | public class LinkedTransferQueueTest ext
1110          Thread t = newStartedThread(new CheckedRunnable() {
1111              public void realRun() throws InterruptedException {
1112                  assertTrue(q.tryTransfer(five, MEDIUM_DELAY_MS, MILLISECONDS));
1113 <                assertTrue(q.isEmpty());
1113 >                checkEmpty(q);
1114              }});
1115  
1116 <        Thread.sleep(SHORT_DELAY_MS);
1116 >        while (q.size() != 2)
1117 >            Thread.yield();
1118          assertEquals(2, q.size());
1119          assertSame(four, q.poll());
1120          assertSame(five, q.poll());
# Line 1072 | Line 1123 | public class LinkedTransferQueueTest ext
1123      }
1124  
1125      /**
1126 <     * tryTransfer attempts to enqueue into the q and fails returning
1127 <     * false not enqueueing and the successive poll is null
1126 >     * tryTransfer attempts to enqueue into the queue and fails
1127 >     * returning false not enqueueing and the successive poll is null
1128       */
1129      public void testTryTransfer8() throws InterruptedException {
1130          final LinkedTransferQueue q = new LinkedTransferQueue();
1131          assertTrue(q.offer(four));
1132          assertEquals(1, q.size());
1133 +        long t0 = System.nanoTime();
1134          assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS));
1135 +        assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
1136          assertEquals(1, q.size());
1137          assertSame(four, q.poll());
1138          assertNull(q.poll());
# Line 1088 | Line 1141 | public class LinkedTransferQueueTest ext
1141  
1142      private LinkedTransferQueue<Integer> populatedQueue(int n) {
1143          LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
1144 <        assertTrue(q.isEmpty());
1144 >        checkEmpty(q);
1145          for (int i = 0; i < n; i++) {
1146              assertEquals(i, q.size());
1147              assertTrue(q.offer(i));

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines