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.37 by jsr166, Wed Nov 3 16:46:34 2010 UTC vs.
Revision 1.47 by jsr166, Mon May 30 22:43:20 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 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 40 | Line 45 | public class LinkedTransferQueueTest ext
45                              new Generic().testSuite());
46      }
47  
43    void checkEmpty(BlockingQueue q) {
44        try {
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
48      /**
49       * Constructor builds new queue with size being zero and empty
50       * being true
# Line 92 | 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];
97 <            new LinkedTransferQueue(Arrays.asList(ints));
75 >            new LinkedTransferQueue(elements);
76              shouldThrow();
77          } catch (NullPointerException success) {}
78      }
# Line 104 | 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];
109 <            for (int i = 0; i < SIZE - 1; ++i) {
110 <                ints[i] = i;
111 <            }
112 <            new LinkedTransferQueue(Arrays.asList(ints));
90 >            new LinkedTransferQueue(elements);
91              shouldThrow();
92          } catch (NullPointerException success) {}
93      }
# Line 156 | Line 134 | public class LinkedTransferQueueTest ext
134      }
135  
136      /**
159     * offer(null) throws NullPointerException
160     */
161    public void testOfferNull() {
162        try {
163            LinkedTransferQueue q = new LinkedTransferQueue();
164            q.offer(null);
165            shouldThrow();
166        } catch (NullPointerException success) {}
167    }
168
169    /**
170     * add(null) throws NullPointerException
171     */
172    public void testAddNull() {
173        try {
174            LinkedTransferQueue q = new LinkedTransferQueue();
175            q.add(null);
176            shouldThrow();
177        } catch (NullPointerException success) {}
178    }
179
180    /**
181     * addAll(null) throws NullPointerException
182     */
183    public void testAddAll1() {
184        try {
185            LinkedTransferQueue q = new LinkedTransferQueue();
186            q.addAll(null);
187            shouldThrow();
188        } catch (NullPointerException success) {}
189    }
190
191    /**
137       * addAll(this) throws IllegalArgumentException
138       */
139      public void testAddAllSelf() {
# Line 200 | Line 145 | public class LinkedTransferQueueTest ext
145      }
146  
147      /**
203     * addAll of a collection with null elements throws NullPointerException
204     */
205    public void testAddAll2() {
206        try {
207            LinkedTransferQueue q = new LinkedTransferQueue();
208            Integer[] ints = new Integer[SIZE];
209            q.addAll(Arrays.asList(ints));
210            shouldThrow();
211        } catch (NullPointerException success) {}
212    }
213
214    /**
148       * addAll of a collection with any null elements throws
149       * NullPointerException after possibly adding some elements
150       */
# Line 245 | Line 178 | public class LinkedTransferQueueTest ext
178      }
179  
180      /**
248     * put(null) throws NullPointerException
249     */
250    public void testPutNull() throws InterruptedException {
251        try {
252            LinkedTransferQueue q = new LinkedTransferQueue();
253            q.put(null);
254            shouldThrow();
255        } catch (NullPointerException success) {}
256    }
257
258    /**
181       * all elements successfully put are contained
182       */
183      public void testPut() {
# Line 281 | 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);
302 <        checkEmpty(q);
232 >        awaitTermination(t);
233      }
234  
235      /**
# Line 332 | 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 448 | Line 378 | public class LinkedTransferQueueTest ext
378       */
379      public void testRemoveElement() throws InterruptedException {
380          LinkedTransferQueue q = populatedQueue(SIZE);
381 <        for (int i = 1; i < SIZE; i += 2) {
381 >        for (int i = 1; i < SIZE; i+=2) {
382 >            assertTrue(q.contains(i));
383              assertTrue(q.remove(i));
384 +            assertFalse(q.contains(i));
385 +            assertTrue(q.contains(i-1));
386          }
387 <        for (int i = 0; i < SIZE; i += 2) {
387 >        for (int i = 0; i < SIZE; i+=2) {
388 >            assertTrue(q.contains(i));
389              assertTrue(q.remove(i));
390 <            assertFalse(q.remove(i + 1));
390 >            assertFalse(q.contains(i));
391 >            assertFalse(q.remove(i+1));
392 >            assertFalse(q.contains(i+1));
393          }
394          checkEmpty(q);
395      }
# Line 550 | Line 486 | public class LinkedTransferQueueTest ext
486      }
487  
488      /**
489 <     * toArray() contains all elements
489 >     * toArray() contains all elements in FIFO order
490       */
491 <    public void testToArray() throws InterruptedException {
491 >    public void testToArray() {
492          LinkedTransferQueue q = populatedQueue(SIZE);
493          Object[] o = q.toArray();
494          for (int i = 0; i < o.length; i++) {
495 <            assertEquals(o[i], q.take());
495 >            assertSame(o[i], q.poll());
496          }
497      }
498  
499      /**
500 <     * toArray(a) contains all elements
500 >     * toArray(a) contains all elements in FIFO order
501       */
502 <    public void testToArray2() throws InterruptedException {
502 >    public void testToArray2() {
503          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
504          Integer[] ints = new Integer[SIZE];
505 <        ints = q.toArray(ints);
505 >        Integer[] array = q.toArray(ints);
506 >        assertSame(ints, array);
507          for (int i = 0; i < ints.length; i++) {
508 <            assertEquals(ints[i], q.take());
508 >            assertSame(ints[i], q.poll());
509          }
510      }
511  
512      /**
576     * toArray(null) throws NullPointerException
577     */
578    public void testToArray_NullArg() {
579        LinkedTransferQueue q = populatedQueue(SIZE);
580        try {
581            q.toArray(null);
582            shouldThrow();
583        } catch (NullPointerException success) {}
584    }
585
586    /**
513       * toArray(incompatible array type) throws ArrayStoreException
514       */
515      public void testToArray1_BadArg() {
# Line 666 | 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 675 | 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 {
683                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 {
690                threadsStarted.countDown();
615                  threadsStarted.await();
616                  assertSame(one, q.take());
617                  checkEmpty(q);
# Line 701 | 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());
710                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 {
718                threadsStarted.countDown();
641                  threadsStarted.await();
642                  q.put(one);
643              }});
# Line 750 | Line 672 | public class LinkedTransferQueueTest ext
672      }
673  
674      /**
753     * drainTo(null) throws NullPointerException
754     */
755    public void testDrainToNull() {
756        LinkedTransferQueue q = populatedQueue(SIZE);
757        try {
758            q.drainTo(null);
759            shouldThrow();
760        } catch (NullPointerException success) {}
761    }
762
763    /**
764     * drainTo(this) throws IllegalArgumentException
765     */
766    public void testDrainToSelf() {
767        LinkedTransferQueue q = populatedQueue(SIZE);
768        try {
769            q.drainTo(q);
770            shouldThrow();
771        } catch (IllegalArgumentException success) {}
772    }
773
774    /**
675       * drainTo(c) empties queue into another collection c
676       */
677      public void testDrainTo() {
# Line 817 | Line 717 | public class LinkedTransferQueueTest ext
717      }
718  
719      /**
820     * drainTo(null, n) throws NullPointerException
821     */
822    public void testDrainToNullN() {
823        LinkedTransferQueue q = populatedQueue(SIZE);
824        try {
825            q.drainTo(null, SIZE);
826            shouldThrow();
827        } catch (NullPointerException success) {}
828    }
829
830    /**
831     * drainTo(this, n) throws IllegalArgumentException
832     */
833    public void testDrainToSelfN() {
834        LinkedTransferQueue q = populatedQueue(SIZE);
835        try {
836            q.drainTo(q, SIZE);
837            shouldThrow();
838        } catch (IllegalArgumentException success) {}
839    }
840
841    /**
720       * drainTo(c, n) empties first min(n, size) elements of queue into c
721       */
722      public void testDrainToN() {
# Line 1071 | Line 949 | public class LinkedTransferQueueTest ext
949      }
950  
951      /**
952 <     * tryTransfer waits the amount given if interrupted, and
1075 <     * throws interrupted exception
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();
1084 <                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);
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 <        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 1106 | 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 1139 | Line 1023 | public class LinkedTransferQueueTest ext
1023      }
1024  
1025      /**
1026 <     * tryTransfer attempts to enqueue into the q and fails returning
1027 <     * false not enqueueing and the successive poll is null
1026 >     * tryTransfer attempts to enqueue into the queue and fails
1027 >     * returning false not enqueueing and the successive poll is null
1028       */
1029      public void testTryTransfer8() throws InterruptedException {
1030          final LinkedTransferQueue q = new LinkedTransferQueue();
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