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.44 by jsr166, Tue Mar 15 19:47:06 2011 UTC vs.
Revision 1.47 by jsr166, Mon May 30 22:43:20 2011 UTC

# Line 13 | Line 13 | import java.io.ObjectInputStream;
13   import java.io.ObjectOutputStream;
14   import java.util.ArrayList;
15   import java.util.Arrays;
16 + import java.util.Collection;
17   import java.util.Iterator;
18   import java.util.List;
19   import java.util.NoSuchElementException;
20 < import java.util.concurrent.*;
20 > import java.util.concurrent.BlockingQueue;
21 > import java.util.concurrent.CountDownLatch;
22 > import java.util.concurrent.Executors;
23 > import java.util.concurrent.ExecutorService;
24 > import java.util.concurrent.LinkedTransferQueue;
25   import static java.util.concurrent.TimeUnit.MILLISECONDS;
26   import static java.util.concurrent.TimeUnit.NANOSECONDS;
27   import junit.framework.Test;
# Line 65 | Line 70 | public class LinkedTransferQueueTest ext
70       * NullPointerException
71       */
72      public void testConstructor3() {
73 +        Collection<Integer> elements = Arrays.asList(new Integer[SIZE]);
74          try {
75 <            Integer[] ints = new Integer[SIZE];
70 <            new LinkedTransferQueue(Arrays.asList(ints));
75 >            new LinkedTransferQueue(elements);
76              shouldThrow();
77          } catch (NullPointerException success) {}
78      }
# Line 77 | Line 82 | public class LinkedTransferQueueTest ext
82       * throws NullPointerException
83       */
84      public void testConstructor4() {
85 +        Integer[] ints = new Integer[SIZE];
86 +        for (int i = 0; i < SIZE-1; ++i)
87 +            ints[i] = i;
88 +        Collection<Integer> elements = Arrays.asList(ints);
89          try {
90 <            Integer[] ints = new Integer[SIZE];
82 <            for (int i = 0; i < SIZE - 1; ++i) {
83 <                ints[i] = i;
84 <            }
85 <            new LinkedTransferQueue(Arrays.asList(ints));
90 >            new LinkedTransferQueue(elements);
91              shouldThrow();
92          } catch (NullPointerException success) {}
93      }
# Line 129 | Line 134 | public class LinkedTransferQueueTest ext
134      }
135  
136      /**
132     * offer(null) throws NullPointerException
133     */
134    public void testOfferNull() {
135        try {
136            LinkedTransferQueue q = new LinkedTransferQueue();
137            q.offer(null);
138            shouldThrow();
139        } catch (NullPointerException success) {}
140    }
141
142    /**
143     * add(null) throws NullPointerException
144     */
145    public void testAddNull() {
146        try {
147            LinkedTransferQueue q = new LinkedTransferQueue();
148            q.add(null);
149            shouldThrow();
150        } catch (NullPointerException success) {}
151    }
152
153    /**
154     * addAll(null) throws NullPointerException
155     */
156    public void testAddAll1() {
157        try {
158            LinkedTransferQueue q = new LinkedTransferQueue();
159            q.addAll(null);
160            shouldThrow();
161        } catch (NullPointerException success) {}
162    }
163
164    /**
137       * addAll(this) throws IllegalArgumentException
138       */
139      public void testAddAllSelf() {
# Line 173 | Line 145 | public class LinkedTransferQueueTest ext
145      }
146  
147      /**
176     * addAll of a collection with null elements throws NullPointerException
177     */
178    public void testAddAll2() {
179        try {
180            LinkedTransferQueue q = new LinkedTransferQueue();
181            Integer[] ints = new Integer[SIZE];
182            q.addAll(Arrays.asList(ints));
183            shouldThrow();
184        } catch (NullPointerException success) {}
185    }
186
187    /**
148       * addAll of a collection with any null elements throws
149       * NullPointerException after possibly adding some elements
150       */
# Line 218 | Line 178 | public class LinkedTransferQueueTest ext
178      }
179  
180      /**
221     * put(null) throws NullPointerException
222     */
223    public void testPutNull() throws InterruptedException {
224        try {
225            LinkedTransferQueue q = new LinkedTransferQueue();
226            q.put(null);
227            shouldThrow();
228        } catch (NullPointerException success) {}
229    }
230
231    /**
181       * all elements successfully put are contained
182       */
183      public void testPut() {
# Line 254 | Line 203 | public class LinkedTransferQueueTest ext
203       * take removes existing elements until empty, then blocks interruptibly
204       */
205      public void testBlockingTake() throws InterruptedException {
206 <        final BlockingQueue<Integer> q = populatedQueue(SIZE);
207 <        final CountDownLatch aboutToWait = new CountDownLatch(1);
206 >        final BlockingQueue q = populatedQueue(SIZE);
207 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
208          Thread t = newStartedThread(new CheckedRunnable() {
209              public void realRun() throws InterruptedException {
210                  for (int i = 0; i < SIZE; ++i) {
211 <                    assertEquals(i, (int) q.take());
211 >                    assertEquals(i, q.take());
212                  }
213 <                aboutToWait.countDown();
213 >
214 >                Thread.currentThread().interrupt();
215                  try {
216                      q.take();
217                      shouldThrow();
218                  } catch (InterruptedException success) {}
219 +                assertFalse(Thread.interrupted());
220 +
221 +                pleaseInterrupt.countDown();
222 +                try {
223 +                    q.take();
224 +                    shouldThrow();
225 +                } catch (InterruptedException success) {}
226 +                assertFalse(Thread.interrupted());
227              }});
228  
229 <        aboutToWait.await();
230 <        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
229 >        await(pleaseInterrupt);
230 >        assertThreadStaysAlive(t);
231          t.interrupt();
232 <        awaitTermination(t, MEDIUM_DELAY_MS);
275 <        checkEmpty(q);
232 >        awaitTermination(t);
233      }
234  
235      /**
# Line 305 | Line 262 | public class LinkedTransferQueueTest ext
262      public void testTimedPoll() throws InterruptedException {
263          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
264          for (int i = 0; i < SIZE; ++i) {
265 <            long t0 = System.nanoTime();
266 <            assertEquals(i, (int) q.poll(SMALL_DELAY_MS, MILLISECONDS));
267 <            assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
268 <        }
269 <        long t0 = System.nanoTime();
270 <        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
271 <        assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
265 >            long startTime = System.nanoTime();
266 >            assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
267 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
268 >        }
269 >        long startTime = System.nanoTime();
270 >        assertNull(q.poll(timeoutMillis(), MILLISECONDS));
271 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
272          checkEmpty(q);
273      }
274  
# Line 553 | Line 510 | public class LinkedTransferQueueTest ext
510      }
511  
512      /**
556     * toArray(null) throws NullPointerException
557     */
558    public void testToArray_NullArg() {
559        LinkedTransferQueue q = populatedQueue(SIZE);
560        try {
561            q.toArray(null);
562            shouldThrow();
563        } catch (NullPointerException success) {}
564    }
565
566    /**
513       * toArray(incompatible array type) throws ArrayStoreException
514       */
515      public void testToArray1_BadArg() {
# Line 646 | Line 592 | public class LinkedTransferQueueTest ext
592          LinkedTransferQueue q = populatedQueue(SIZE);
593          String s = q.toString();
594          for (int i = 0; i < SIZE; ++i) {
595 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
595 >            assertTrue(s.contains(String.valueOf(i)));
596          }
597      }
598  
# Line 655 | Line 601 | public class LinkedTransferQueueTest ext
601       */
602      public void testOfferInExecutor() {
603          final LinkedTransferQueue q = new LinkedTransferQueue();
604 <        final CountDownLatch threadsStarted = new CountDownLatch(2);
604 >        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
605          ExecutorService executor = Executors.newFixedThreadPool(2);
606  
607          executor.execute(new CheckedRunnable() {
608              public void realRun() throws InterruptedException {
663                threadsStarted.countDown();
609                  threadsStarted.await();
610 <                assertTrue(q.offer(one, MEDIUM_DELAY_MS, MILLISECONDS));
610 >                assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS));
611              }});
612  
613          executor.execute(new CheckedRunnable() {
614              public void realRun() throws InterruptedException {
670                threadsStarted.countDown();
615                  threadsStarted.await();
616                  assertSame(one, q.take());
617                  checkEmpty(q);
# Line 681 | Line 625 | public class LinkedTransferQueueTest ext
625       */
626      public void testPollInExecutor() {
627          final LinkedTransferQueue q = new LinkedTransferQueue();
628 <        final CountDownLatch threadsStarted = new CountDownLatch(2);
628 >        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
629          ExecutorService executor = Executors.newFixedThreadPool(2);
630  
631          executor.execute(new CheckedRunnable() {
632              public void realRun() throws InterruptedException {
633                  assertNull(q.poll());
690                threadsStarted.countDown();
634                  threadsStarted.await();
635 <                assertSame(one, q.poll(SMALL_DELAY_MS, MILLISECONDS));
635 >                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
636                  checkEmpty(q);
637              }});
638  
639          executor.execute(new CheckedRunnable() {
640              public void realRun() throws InterruptedException {
698                threadsStarted.countDown();
641                  threadsStarted.await();
642                  q.put(one);
643              }});
# Line 730 | Line 672 | public class LinkedTransferQueueTest ext
672      }
673  
674      /**
733     * drainTo(null) throws NullPointerException
734     */
735    public void testDrainToNull() {
736        LinkedTransferQueue q = populatedQueue(SIZE);
737        try {
738            q.drainTo(null);
739            shouldThrow();
740        } catch (NullPointerException success) {}
741    }
742
743    /**
744     * drainTo(this) throws IllegalArgumentException
745     */
746    public void testDrainToSelf() {
747        LinkedTransferQueue q = populatedQueue(SIZE);
748        try {
749            q.drainTo(q);
750            shouldThrow();
751        } catch (IllegalArgumentException success) {}
752    }
753
754    /**
675       * drainTo(c) empties queue into another collection c
676       */
677      public void testDrainTo() {
# Line 797 | Line 717 | public class LinkedTransferQueueTest ext
717      }
718  
719      /**
800     * drainTo(null, n) throws NullPointerException
801     */
802    public void testDrainToNullN() {
803        LinkedTransferQueue q = populatedQueue(SIZE);
804        try {
805            q.drainTo(null, SIZE);
806            shouldThrow();
807        } catch (NullPointerException success) {}
808    }
809
810    /**
811     * drainTo(this, n) throws IllegalArgumentException
812     */
813    public void testDrainToSelfN() {
814        LinkedTransferQueue q = populatedQueue(SIZE);
815        try {
816            q.drainTo(q, SIZE);
817            shouldThrow();
818        } catch (IllegalArgumentException success) {}
819    }
820
821    /**
720       * drainTo(c, n) empties first min(n, size) elements of queue into c
721       */
722      public void testDrainToN() {
# Line 1051 | Line 949 | public class LinkedTransferQueueTest ext
949      }
950  
951      /**
952 <     * tryTransfer waits the amount given, and throws
1055 <     * InterruptedException when interrupted.
952 >     * tryTransfer blocks interruptibly if no takers
953       */
954      public void testTryTransfer5() throws InterruptedException {
955          final LinkedTransferQueue q = new LinkedTransferQueue();
956 <        final CountDownLatch threadStarted = new CountDownLatch(1);
956 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
957          assertTrue(q.isEmpty());
958  
959          Thread t = newStartedThread(new CheckedRunnable() {
960              public void realRun() throws InterruptedException {
961 <                long t0 = System.nanoTime();
1065 <                threadStarted.countDown();
961 >                Thread.currentThread().interrupt();
962                  try {
963                      q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS);
964                      shouldThrow();
965                  } catch (InterruptedException success) {}
966 <                assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
967 <                assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
966 >                assertFalse(Thread.interrupted());
967 >
968 >                pleaseInterrupt.countDown();
969 >                try {
970 >                    q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS);
971 >                    shouldThrow();
972 >                } catch (InterruptedException success) {}
973 >                assertFalse(Thread.interrupted());
974              }});
975  
976 <        threadStarted.await();
977 <        while (q.isEmpty())
1076 <            Thread.yield();
1077 <        Thread.sleep(SHORT_DELAY_MS);
976 >        await(pleaseInterrupt);
977 >        assertThreadStaysAlive(t);
978          t.interrupt();
979 <        awaitTermination(t, MEDIUM_DELAY_MS);
979 >        awaitTermination(t);
980          checkEmpty(q);
981      }
982  
# Line 1090 | Line 990 | public class LinkedTransferQueueTest ext
990              public void realRun() throws InterruptedException {
991                  long t0 = System.nanoTime();
992                  assertFalse(q.tryTransfer(new Object(),
993 <                                          SHORT_DELAY_MS, MILLISECONDS));
994 <                assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
993 >                                          timeoutMillis(), MILLISECONDS));
994 >                assertTrue(millisElapsedSince(t0) >= timeoutMillis());
995                  checkEmpty(q);
996              }});
997  
998 <        awaitTermination(t, MEDIUM_DELAY_MS);
998 >        awaitTermination(t);
999          checkEmpty(q);
1000      }
1001  
# Line 1131 | Line 1031 | public class LinkedTransferQueueTest ext
1031          assertTrue(q.offer(four));
1032          assertEquals(1, q.size());
1033          long t0 = System.nanoTime();
1034 <        assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS));
1035 <        assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
1034 >        assertFalse(q.tryTransfer(five, timeoutMillis(), MILLISECONDS));
1035 >        assertTrue(millisElapsedSince(t0) >= timeoutMillis());
1036          assertEquals(1, q.size());
1037          assertSame(four, q.poll());
1038          assertNull(q.poll());

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines