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.15 by jsr166, Sat Nov 21 17:38:05 2009 UTC vs.
Revision 1.27 by jsr166, Thu Oct 28 17:22:13 2010 UTC

# Line 18 | Line 18 | import java.util.List;
18   import java.util.NoSuchElementException;
19   import java.util.concurrent.*;
20   import static java.util.concurrent.TimeUnit.MILLISECONDS;
21 + import static java.util.concurrent.TimeUnit.NANOSECONDS;
22   import junit.framework.Test;
23   import junit.framework.TestSuite;
24  
25   @SuppressWarnings({"unchecked", "rawtypes"})
26   public class LinkedTransferQueueTest extends JSR166TestCase {
27  
28 +    public static class Generic extends BlockingQueueTest {
29 +        protected BlockingQueue emptyCollection() {
30 +            return new LinkedTransferQueue();
31 +        }
32 +    }
33 +
34      public static void main(String[] args) {
35          junit.textui.TestRunner.run(suite());
36      }
37  
38      public static Test suite() {
39 <        return new TestSuite(LinkedTransferQueueTest.class);
39 >        return newTestSuite(LinkedTransferQueueTest.class,
40 >                            new Generic().testSuite());
41      }
42  
43      void checkEmpty(LinkedTransferQueue q) throws InterruptedException {
# Line 44 | Line 52 | public class LinkedTransferQueueTest ext
52          try {
53              q.element();
54              shouldThrow();
55 <        } catch (NoSuchElementException success) {
48 <        }
55 >        } catch (NoSuchElementException success) {}
56          try {
57              q.iterator().next();
58              shouldThrow();
59 <        } catch (NoSuchElementException success) {
53 <        }
59 >        } catch (NoSuchElementException success) {}
60          try {
61              q.remove();
62              shouldThrow();
63 <        } catch (NoSuchElementException success) {
58 <        }
63 >        } catch (NoSuchElementException success) {}
64      }
65  
66      /**
# Line 75 | Line 80 | public class LinkedTransferQueueTest ext
80          try {
81              new LinkedTransferQueue(null);
82              shouldThrow();
83 <        } catch (NullPointerException success) {
79 <        }
83 >        } catch (NullPointerException success) {}
84      }
85  
86      /**
# Line 88 | Line 92 | public class LinkedTransferQueueTest ext
92              Integer[] ints = new Integer[SIZE];
93              new LinkedTransferQueue(Arrays.asList(ints));
94              shouldThrow();
95 <        } catch (NullPointerException success) {
92 <        }
95 >        } catch (NullPointerException success) {}
96      }
97  
98      /**
# Line 104 | Line 107 | public class LinkedTransferQueueTest ext
107              }
108              new LinkedTransferQueue(Arrays.asList(ints));
109              shouldThrow();
110 <        } catch (NullPointerException success) {
108 <        }
110 >        } catch (NullPointerException success) {}
111      }
112  
113      /**
# Line 157 | Line 159 | public class LinkedTransferQueueTest ext
159              LinkedTransferQueue q = new LinkedTransferQueue();
160              q.offer(null);
161              shouldThrow();
162 <        } catch (NullPointerException success) {
161 <        }
162 >        } catch (NullPointerException success) {}
163      }
164  
165      /**
# Line 169 | Line 170 | public class LinkedTransferQueueTest ext
170              LinkedTransferQueue q = new LinkedTransferQueue();
171              q.add(null);
172              shouldThrow();
173 <        } catch (NullPointerException success) {
173 <        }
173 >        } catch (NullPointerException success) {}
174      }
175  
176      /**
# Line 181 | Line 181 | public class LinkedTransferQueueTest ext
181              LinkedTransferQueue q = new LinkedTransferQueue();
182              q.addAll(null);
183              shouldThrow();
184 <        } catch (NullPointerException success) {
185 <        }
184 >        } catch (NullPointerException success) {}
185      }
186  
187      /**
# Line 193 | Line 192 | public class LinkedTransferQueueTest ext
192              LinkedTransferQueue q = populatedQueue(SIZE);
193              q.addAll(q);
194              shouldThrow();
195 <        } catch (IllegalArgumentException success) {
197 <        }
195 >        } catch (IllegalArgumentException success) {}
196      }
197  
198      /**
# Line 206 | Line 204 | public class LinkedTransferQueueTest ext
204              Integer[] ints = new Integer[SIZE];
205              q.addAll(Arrays.asList(ints));
206              shouldThrow();
207 <        } catch (NullPointerException success) {
210 <        }
207 >        } catch (NullPointerException success) {}
208      }
209  
210      /**
# Line 223 | Line 220 | public class LinkedTransferQueueTest ext
220              }
221              q.addAll(Arrays.asList(ints));
222              shouldThrow();
223 <        } catch (NullPointerException success) {
227 <        }
223 >        } catch (NullPointerException success) {}
224      }
225  
226      /**
# Line 283 | Line 279 | public class LinkedTransferQueueTest ext
279      public void testTakeFromEmpty() throws InterruptedException {
280          final LinkedTransferQueue q = new LinkedTransferQueue();
281          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
282 <            void realRun() throws InterruptedException {
282 >            public void realRun() throws InterruptedException {
283                  q.take();
284              }});
285          Thread.sleep(SHORT_DELAY_MS);
# Line 296 | Line 292 | public class LinkedTransferQueueTest ext
292       */
293      public void testBlockingTake() throws InterruptedException {
294          final LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
295 <        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
296 <            void realRun() throws InterruptedException {
295 >        Thread t = new Thread(new CheckedRunnable() {
296 >            public void realRun() throws InterruptedException {
297                  for (int i = 0; i < SIZE; ++i) {
298 <                    threadAssertEquals(i, (int) q.take());
298 >                    assertEquals(i, (int) q.take());
299                  }
300 <                q.take();
300 >                try {
301 >                    q.take();
302 >                    shouldThrow();
303 >                } catch (InterruptedException success) {}
304              }});
305 <        Thread.sleep(SMALL_DELAY_MS);
305 >
306 >        t.start();
307 >        Thread.sleep(SHORT_DELAY_MS);
308          t.interrupt();
309          t.join();
310          checkEmpty(q);
# Line 354 | Line 355 | public class LinkedTransferQueueTest ext
355       */
356      public void testInterruptedTimedPoll() throws InterruptedException {
357          final LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
358 <        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
359 <            void realRun() throws InterruptedException {
358 >        Thread t = newStartedThread(new CheckedRunnable() {
359 >            public void realRun() throws InterruptedException {
360                  for (int i = 0; i < SIZE; ++i) {
361                      long t0 = System.nanoTime();
362 <                    threadAssertEquals(i, (int) q.poll(LONG_DELAY_MS,
362 <                                                       MILLISECONDS));
362 >                    assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
363                      long millisElapsed = (System.nanoTime() - t0)/(1024 * 1024);
364                      assertTrue(millisElapsed < SMALL_DELAY_MS);
365                  }
366 <                q.poll(LONG_DELAY_MS, MILLISECONDS);
366 >                try {
367 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
368 >                    shouldThrow();
369 >                } catch (InterruptedException success) {}
370              }});
368        Thread.sleep(SMALL_DELAY_MS);
369        t.interrupt();
370        t.join();
371        checkEmpty(q);
372    }
371  
374    /**
375     * timed poll before a delayed offer fails; after offer succeeds;
376     * on interruption throws
377     */
378    public void testTimedPollWithOffer() throws InterruptedException {
379        final LinkedTransferQueue q = new LinkedTransferQueue();
380        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
381            void realRun() throws InterruptedException {
382                threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
383                q.poll(LONG_DELAY_MS, MILLISECONDS);
384                q.poll(LONG_DELAY_MS, MILLISECONDS);
385            }});
372          Thread.sleep(SMALL_DELAY_MS);
387        assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
373          t.interrupt();
374          t.join();
375 +        checkEmpty(q);
376      }
377  
378      /**
# Line 416 | Line 402 | public class LinkedTransferQueueTest ext
402          try {
403              q.element();
404              shouldThrow();
405 <        } catch (NoSuchElementException success) {
420 <        }
405 >        } catch (NoSuchElementException success) {}
406          checkEmpty(q);
407      }
408  
# Line 432 | Line 417 | public class LinkedTransferQueueTest ext
417          try {
418              q.remove();
419              shouldThrow();
420 <        } catch (NoSuchElementException success) {
436 <        }
420 >        } catch (NoSuchElementException success) {}
421          checkEmpty(q);
422      }
423  
# Line 462 | Line 446 | public class LinkedTransferQueueTest ext
446          assertTrue(q.remove(one));
447          assertTrue(q.remove(two));
448          assertTrue(q.add(three));
449 <        assertTrue(q.take() == three);
449 >        assertSame(q.take(), three);
450      }
451  
452      /**
# Line 570 | Line 554 | public class LinkedTransferQueueTest ext
554       * toArray(null) throws NullPointerException
555       */
556      public void testToArray_BadArg() {
557 +        LinkedTransferQueue q = populatedQueue(SIZE);
558          try {
574            LinkedTransferQueue q = populatedQueue(SIZE);
559              Object o[] = q.toArray(null);
560              shouldThrow();
561 <        } catch (NullPointerException success) {
578 <        }
561 >        } catch (NullPointerException success) {}
562      }
563  
564      /**
565       * toArray(incompatible array type) throws CCE
566       */
567      public void testToArray1_BadArg() {
568 +        LinkedTransferQueue q = populatedQueue(SIZE);
569          try {
586            LinkedTransferQueue q = populatedQueue(SIZE);
570              Object o[] = q.toArray(new String[10]);
571              shouldThrow();
572 <        } catch (ArrayStoreException success) {
590 <        }
572 >        } catch (ArrayStoreException success) {}
573      }
574  
575      /**
# Line 617 | Line 599 | public class LinkedTransferQueueTest ext
599          it.remove();
600  
601          it = q.iterator();
602 <        assertEquals(it.next(), one);
603 <        assertEquals(it.next(), three);
602 >        assertSame(it.next(), one);
603 >        assertSame(it.next(), three);
604          assertFalse(it.hasNext());
605      }
606  
# Line 676 | Line 658 | public class LinkedTransferQueueTest ext
658          ExecutorService executor = Executors.newFixedThreadPool(2);
659  
660          executor.execute(new CheckedRunnable() {
661 <            void realRun() {
662 <                threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS,
681 <                                         MILLISECONDS));
661 >            public void realRun() {
662 >                assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
663              }});
664  
665          executor.execute(new CheckedRunnable() {
666 <            void realRun() throws InterruptedException {
666 >            public void realRun() throws InterruptedException {
667                  Thread.sleep(SMALL_DELAY_MS);
668 <                threadAssertEquals(one, q.take());
668 >                assertSame(one, q.take());
669              }});
670  
671          joinPool(executor);
# Line 698 | Line 679 | public class LinkedTransferQueueTest ext
679          ExecutorService executor = Executors.newFixedThreadPool(2);
680  
681          executor.execute(new CheckedRunnable() {
682 <            void realRun() throws InterruptedException {
683 <                threadAssertNull(q.poll());
684 <                threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS,
685 <                                                MILLISECONDS));
705 <                threadAssertTrue(q.isEmpty());
682 >            public void realRun() throws InterruptedException {
683 >                assertNull(q.poll());
684 >                assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
685 >                assertTrue(q.isEmpty());
686              }});
687  
688          executor.execute(new CheckedRunnable() {
689 <            void realRun() throws InterruptedException {
689 >            public void realRun() throws InterruptedException {
690                  Thread.sleep(SMALL_DELAY_MS);
691                  q.put(one);
692              }});
# Line 746 | Line 726 | public class LinkedTransferQueueTest ext
726          try {
727              q.drainTo(null);
728              shouldThrow();
729 <        } catch (NullPointerException success) {
750 <        }
729 >        } catch (NullPointerException success) {}
730      }
731  
732      /**
# Line 758 | Line 737 | public class LinkedTransferQueueTest ext
737          try {
738              q.drainTo(q);
739              shouldThrow();
740 <        } catch (IllegalArgumentException success) {
762 <        }
740 >        } catch (IllegalArgumentException success) {}
741      }
742  
743      /**
# Line 794 | Line 772 | public class LinkedTransferQueueTest ext
772      public void testDrainToWithActivePut() throws InterruptedException {
773          final LinkedTransferQueue q = populatedQueue(SIZE);
774          Thread t = newStartedThread(new CheckedRunnable() {
775 <            void realRun() {
775 >            public void realRun() {
776                  q.put(SIZE + 1);
777              }});
778          ArrayList l = new ArrayList();
# Line 815 | Line 793 | public class LinkedTransferQueueTest ext
793          try {
794              q.drainTo(null, SIZE);
795              shouldThrow();
796 <        } catch (NullPointerException success) {
819 <        }
796 >        } catch (NullPointerException success) {}
797      }
798  
799      /**
# Line 827 | Line 804 | public class LinkedTransferQueueTest ext
804          try {
805              q.drainTo(q, SIZE);
806              shouldThrow();
807 <        } catch (IllegalArgumentException success) {
831 <        }
807 >        } catch (IllegalArgumentException success) {}
808      }
809  
810      /**
811 <     * drainTo(c, n) empties first max {n, size} elements of queue into c
811 >     * drainTo(c, n) empties first min(n, size) elements of queue into c
812       */
813      public void testDrainToN() {
814          LinkedTransferQueue q = new LinkedTransferQueue();
# Line 863 | Line 839 | public class LinkedTransferQueueTest ext
839          assertFalse(q.hasWaitingConsumer());
840  
841          Thread t = newStartedThread(new CheckedRunnable() {
842 <            void realRun() throws InterruptedException {
842 >            public void realRun() throws InterruptedException {
843                  Thread.sleep(SMALL_DELAY_MS);
844 <                threadAssertTrue(q.hasWaitingConsumer());
845 <                threadAssertEquals(q.getWaitingConsumerCount(), 1);
846 <                threadAssertTrue(q.offer(new Object()));
847 <                threadAssertFalse(q.hasWaitingConsumer());
848 <                threadAssertEquals(q.getWaitingConsumerCount(), 0);
844 >                assertTrue(q.hasWaitingConsumer());
845 >                assertEquals(q.getWaitingConsumerCount(), 1);
846 >                assertTrue(q.offer(one));
847 >                assertFalse(q.hasWaitingConsumer());
848 >                assertEquals(q.getWaitingConsumerCount(), 0);
849              }});
850  
851 <        assertTrue(q.poll(LONG_DELAY_MS, MILLISECONDS) != null);
851 >        assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
852          assertEquals(q.getWaitingConsumerCount(), 0);
853          assertFalse(q.hasWaitingConsumer());
854          t.join();
# Line 898 | Line 874 | public class LinkedTransferQueueTest ext
874              = new LinkedTransferQueue<Integer>();
875  
876          Thread t = newStartedThread(new CheckedRunnable() {
877 <            void realRun() throws InterruptedException {
877 >            public void realRun() throws InterruptedException {
878                  q.transfer(SIZE);
879 <                threadAssertTrue(q.isEmpty());
879 >                assertTrue(q.isEmpty());
880              }});
881  
882          Thread.sleep(SHORT_DELAY_MS);
# Line 918 | Line 894 | public class LinkedTransferQueueTest ext
894              = new LinkedTransferQueue<Integer>();
895  
896          Thread first = newStartedThread(new CheckedRunnable() {
897 <            void realRun() throws InterruptedException {
897 >            public void realRun() throws InterruptedException {
898                  Integer i = SIZE + 1;
899                  q.transfer(i);
900 <                threadAssertTrue(!q.contains(i));
901 <                threadAssertEquals(1, q.size());
900 >                assertTrue(!q.contains(i));
901 >                assertEquals(1, q.size());
902              }});
903  
904          Thread interruptedThread = newStartedThread(
905              new CheckedInterruptedRunnable() {
906 <                void realRun() throws InterruptedException {
906 >                public void realRun() throws InterruptedException {
907                      while (q.size() == 0)
908                          Thread.yield();
909                      q.transfer(SIZE);
# Line 953 | Line 929 | public class LinkedTransferQueueTest ext
929          final LinkedTransferQueue q = new LinkedTransferQueue();
930  
931          Thread t = newStartedThread(new CheckedRunnable() {
932 <            void realRun() throws InterruptedException {
932 >            public void realRun() throws InterruptedException {
933                  q.transfer(four);
934 <                threadAssertFalse(q.contains(four));
935 <                threadAssertEquals(three, q.poll());
934 >                assertFalse(q.contains(four));
935 >                assertSame(three, q.poll());
936              }});
937  
938          Thread.sleep(SHORT_DELAY_MS);
939          assertTrue(q.offer(three));
940 <        assertEquals(four, q.poll());
940 >        assertSame(four, q.poll());
941          t.join();
942      }
943  
# Line 974 | Line 950 | public class LinkedTransferQueueTest ext
950              = new LinkedTransferQueue<Integer>();
951  
952          Thread t = newStartedThread(new CheckedRunnable() {
953 <            void realRun() throws InterruptedException {
953 >            public void realRun() throws InterruptedException {
954                  q.transfer(SIZE);
955                  checkEmpty(q);
956              }});
# Line 1016 | Line 992 | public class LinkedTransferQueueTest ext
992          final LinkedTransferQueue q = new LinkedTransferQueue();
993  
994          Thread t = newStartedThread(new CheckedRunnable() {
995 <            void realRun() {
995 >            public void realRun() {
996                  while (! q.hasWaitingConsumer())
997                      Thread.yield();
998 <                threadAssertTrue(q.hasWaitingConsumer());
999 <                threadAssertTrue(q.isEmpty());
1000 <                threadAssertTrue(q.size() == 0);
1001 <                threadAssertTrue(q.tryTransfer(hotPotato));
998 >                assertTrue(q.hasWaitingConsumer());
999 >                assertTrue(q.isEmpty());
1000 >                assertEquals(q.size(), 0);
1001 >                assertTrue(q.tryTransfer(hotPotato));
1002              }});
1003  
1004 <        assertTrue(q.poll(MEDIUM_DELAY_MS, MILLISECONDS) == hotPotato);
1004 >        assertSame(hotPotato, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
1005          checkEmpty(q);
1006          t.join();
1007      }
# Line 1039 | Line 1015 | public class LinkedTransferQueueTest ext
1015          final LinkedTransferQueue q = new LinkedTransferQueue();
1016  
1017          Thread t = newStartedThread(new CheckedRunnable() {
1018 <            void realRun() {
1018 >            public void realRun() {
1019                  while (! q.hasWaitingConsumer())
1020                      Thread.yield();
1021 <                threadAssertTrue(q.hasWaitingConsumer());
1022 <                threadAssertTrue(q.isEmpty());
1023 <                threadAssertTrue(q.size() == 0);
1024 <                threadAssertTrue(q.tryTransfer(hotPotato));
1021 >                assertTrue(q.hasWaitingConsumer());
1022 >                assertTrue(q.isEmpty());
1023 >                assertEquals(q.size(), 0);
1024 >                assertTrue(q.tryTransfer(hotPotato));
1025              }});
1026  
1027 <        assertTrue(q.take() == hotPotato);
1027 >        assertSame(q.take(), hotPotato);
1028          checkEmpty(q);
1029          t.join();
1030      }
# Line 1061 | Line 1037 | public class LinkedTransferQueueTest ext
1037          final LinkedTransferQueue q = new LinkedTransferQueue();
1038  
1039          Thread toInterrupt = newStartedThread(new CheckedInterruptedRunnable() {
1040 <            void realRun() throws InterruptedException {
1040 >            public void realRun() throws InterruptedException {
1041                  q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS);
1042              }});
1043  
# Line 1077 | Line 1053 | public class LinkedTransferQueueTest ext
1053          final LinkedTransferQueue q = new LinkedTransferQueue();
1054  
1055          Thread t = newStartedThread(new CheckedRunnable() {
1056 <            void realRun() throws InterruptedException {
1057 <                threadAssertFalse
1058 <                    (q.tryTransfer(new Object(),
1059 <                                   SHORT_DELAY_MS, MILLISECONDS));
1056 >            public void realRun() throws InterruptedException {
1057 >                long t0 = System.nanoTime();
1058 >                assertFalse(q.tryTransfer(new Object(),
1059 >                                          SHORT_DELAY_MS, MILLISECONDS));
1060 >                long elapsed = NANOSECONDS.toMillis(System.nanoTime() - t0);
1061 >                assertTrue(elapsed >= SHORT_DELAY_MS);
1062              }});
1063  
1086        Thread.sleep(SMALL_DELAY_MS);
1064          checkEmpty(q);
1065 <        t.join();
1065 >        awaitTermination(t, MEDIUM_DELAY_MS);
1066 >        checkEmpty(q);
1067      }
1068  
1069      /**
# Line 1097 | Line 1075 | public class LinkedTransferQueueTest ext
1075          assertTrue(q.offer(four));
1076  
1077          Thread t = newStartedThread(new CheckedRunnable() {
1078 <            void realRun() throws InterruptedException {
1079 <                threadAssertTrue(q.tryTransfer(five,
1080 <                                               MEDIUM_DELAY_MS, MILLISECONDS));
1103 <                threadAssertTrue(q.isEmpty());
1078 >            public void realRun() throws InterruptedException {
1079 >                assertTrue(q.tryTransfer(five, MEDIUM_DELAY_MS, MILLISECONDS));
1080 >                assertTrue(q.isEmpty());
1081              }});
1082  
1083          Thread.sleep(SHORT_DELAY_MS);
1084          assertEquals(2, q.size());
1085 <        assertEquals(four, q.poll());
1086 <        assertEquals(five, q.poll());
1085 >        assertSame(four, q.poll());
1086 >        assertSame(five, q.poll());
1087          checkEmpty(q);
1088          t.join();
1089      }
# Line 1121 | Line 1098 | public class LinkedTransferQueueTest ext
1098          assertEquals(1, q.size());
1099          assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS));
1100          assertEquals(1, q.size());
1101 <        assertEquals(four, q.poll());
1101 >        assertSame(four, q.poll());
1102          assertNull(q.poll());
1103          checkEmpty(q);
1104      }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines