ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/LinkedBlockingQueueTest.java
(Generate patch)

Comparing jsr166/src/test/tck/LinkedBlockingQueueTest.java (file contents):
Revision 1.43 by jsr166, Sat May 21 06:24:33 2011 UTC vs.
Revision 1.48 by jsr166, Sat Nov 26 05:19:17 2011 UTC

# Line 7 | Line 7
7   */
8  
9   import junit.framework.*;
10 < import java.util.*;
11 < import java.util.concurrent.*;
10 > import java.util.Arrays;
11 > import java.util.ArrayList;
12 > import java.util.Collection;
13 > import java.util.Iterator;
14 > import java.util.NoSuchElementException;
15 > import java.util.Queue;
16 > import java.util.concurrent.BlockingQueue;
17 > import java.util.concurrent.CountDownLatch;
18 > import java.util.concurrent.LinkedBlockingQueue;
19 > import java.util.concurrent.Executors;
20 > import java.util.concurrent.ExecutorService;
21   import static java.util.concurrent.TimeUnit.MILLISECONDS;
13 import java.io.*;
22  
23   public class LinkedBlockingQueueTest extends JSR166TestCase {
24  
# Line 22 | Line 30 | public class LinkedBlockingQueueTest ext
30  
31      public static class Bounded extends BlockingQueueTest {
32          protected BlockingQueue emptyCollection() {
33 <            return new LinkedBlockingQueue(20);
33 >            return new LinkedBlockingQueue(SIZE);
34          }
35      }
36  
# Line 36 | Line 44 | public class LinkedBlockingQueueTest ext
44                              new Bounded().testSuite());
45      }
46  
39
47      /**
48       * Create a queue of given size containing consecutive
49       * Integers 0 ... n.
# Line 63 | Line 70 | public class LinkedBlockingQueueTest ext
70      }
71  
72      /**
73 <     * Constructor throws IAE if capacity argument nonpositive
73 >     * Constructor throws IllegalArgumentException if capacity argument nonpositive
74       */
75      public void testConstructor2() {
76          try {
77 <            LinkedBlockingQueue q = new LinkedBlockingQueue(0);
77 >            new LinkedBlockingQueue(0);
78              shouldThrow();
79          } catch (IllegalArgumentException success) {}
80      }
81  
82      /**
83 <     * Initializing from null Collection throws NPE
83 >     * Initializing from null Collection throws NullPointerException
84       */
85      public void testConstructor3() {
86          try {
87 <            LinkedBlockingQueue q = new LinkedBlockingQueue(null);
87 >            new LinkedBlockingQueue(null);
88              shouldThrow();
89          } catch (NullPointerException success) {}
90      }
91  
92      /**
93 <     * Initializing from Collection of null elements throws NPE
93 >     * Initializing from Collection of null elements throws NullPointerException
94       */
95      public void testConstructor4() {
96 +        Collection<Integer> elements = Arrays.asList(new Integer[SIZE]);
97          try {
98 <            Integer[] ints = new Integer[SIZE];
91 <            LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints));
98 >            new LinkedBlockingQueue(elements);
99              shouldThrow();
100          } catch (NullPointerException success) {}
101      }
102  
103      /**
104 <     * Initializing from Collection with some null elements throws NPE
104 >     * Initializing from Collection with some null elements throws
105 >     * NullPointerException
106       */
107      public void testConstructor5() {
108 +        Integer[] ints = new Integer[SIZE];
109 +        for (int i = 0; i < SIZE-1; ++i)
110 +            ints[i] = new Integer(i);
111 +        Collection<Integer> elements = Arrays.asList(ints);
112          try {
113 <            Integer[] ints = new Integer[SIZE];
102 <            for (int i = 0; i < SIZE-1; ++i)
103 <                ints[i] = new Integer(i);
104 <            LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints));
113 >            new LinkedBlockingQueue(elements);
114              shouldThrow();
115          } catch (NullPointerException success) {}
116      }
# Line 151 | Line 160 | public class LinkedBlockingQueueTest ext
160      }
161  
162      /**
154     * offer(null) throws NPE
155     */
156    public void testOfferNull() {
157        try {
158            LinkedBlockingQueue q = new LinkedBlockingQueue(1);
159            q.offer(null);
160            shouldThrow();
161        } catch (NullPointerException success) {}
162    }
163
164    /**
165     * add(null) throws NPE
166     */
167    public void testAddNull() {
168        try {
169            LinkedBlockingQueue q = new LinkedBlockingQueue(1);
170            q.add(null);
171            shouldThrow();
172        } catch (NullPointerException success) {}
173    }
174
175    /**
163       * Offer succeeds if not full; fails if full
164       */
165      public void testOffer() {
# Line 182 | Line 169 | public class LinkedBlockingQueueTest ext
169      }
170  
171      /**
172 <     * add succeeds if not full; throws ISE if full
172 >     * add succeeds if not full; throws IllegalStateException if full
173       */
174      public void testAdd() {
175 +        LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
176 +        for (int i = 0; i < SIZE; ++i)
177 +            assertTrue(q.add(new Integer(i)));
178 +        assertEquals(0, q.remainingCapacity());
179          try {
189            LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
190            for (int i = 0; i < SIZE; ++i) {
191                assertTrue(q.add(new Integer(i)));
192            }
193            assertEquals(0, q.remainingCapacity());
180              q.add(new Integer(SIZE));
181              shouldThrow();
182          } catch (IllegalStateException success) {}
183      }
184  
185      /**
186 <     * addAll(null) throws NPE
201 <     */
202 <    public void testAddAll1() {
203 <        try {
204 <            LinkedBlockingQueue q = new LinkedBlockingQueue(1);
205 <            q.addAll(null);
206 <            shouldThrow();
207 <        } catch (NullPointerException success) {}
208 <    }
209 <
210 <    /**
211 <     * addAll(this) throws IAE
186 >     * addAll(this) throws IllegalArgumentException
187       */
188      public void testAddAllSelf() {
189 +        LinkedBlockingQueue q = populatedQueue(SIZE);
190          try {
215            LinkedBlockingQueue q = populatedQueue(SIZE);
191              q.addAll(q);
192              shouldThrow();
193          } catch (IllegalArgumentException success) {}
194      }
195  
196      /**
222     * addAll of a collection with null elements throws NPE
223     */
224    public void testAddAll2() {
225        try {
226            LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
227            Integer[] ints = new Integer[SIZE];
228            q.addAll(Arrays.asList(ints));
229            shouldThrow();
230        } catch (NullPointerException success) {}
231    }
232
233    /**
197       * addAll of a collection with any null elements throws NPE after
198       * possibly adding some elements
199       */
200      public void testAddAll3() {
201 +        LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
202 +        Integer[] ints = new Integer[SIZE];
203 +        for (int i = 0; i < SIZE-1; ++i)
204 +            ints[i] = new Integer(i);
205 +        Collection<Integer> elements = Arrays.asList(ints);
206          try {
207 <            LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
240 <            Integer[] ints = new Integer[SIZE];
241 <            for (int i = 0; i < SIZE-1; ++i)
242 <                ints[i] = new Integer(i);
243 <            q.addAll(Arrays.asList(ints));
207 >            q.addAll(elements);
208              shouldThrow();
209          } catch (NullPointerException success) {}
210      }
211  
212      /**
213 <     * addAll throws ISE if not enough room
213 >     * addAll throws IllegalStateException if not enough room
214       */
215      public void testAddAll4() {
216 +        LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE - 1);
217 +        Integer[] ints = new Integer[SIZE];
218 +        for (int i = 0; i < SIZE; ++i)
219 +            ints[i] = new Integer(i);
220 +        Collection<Integer> elements = Arrays.asList(ints);
221          try {
222 <            LinkedBlockingQueue q = new LinkedBlockingQueue(1);
254 <            Integer[] ints = new Integer[SIZE];
255 <            for (int i = 0; i < SIZE; ++i)
256 <                ints[i] = new Integer(i);
257 <            q.addAll(Arrays.asList(ints));
222 >            q.addAll(elements);
223              shouldThrow();
224          } catch (IllegalStateException success) {}
225      }
# Line 275 | Line 240 | public class LinkedBlockingQueueTest ext
240      }
241  
242      /**
278     * put(null) throws NPE
279     */
280    public void testPutNull() throws InterruptedException {
281        try {
282            LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
283            q.put(null);
284            shouldThrow();
285        } catch (NullPointerException success) {}
286    }
287
288    /**
243       * all elements successfully put are contained
244       */
245      public void testPut() throws InterruptedException {
# Line 303 | Line 257 | public class LinkedBlockingQueueTest ext
257       */
258      public void testBlockingPut() throws InterruptedException {
259          final LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
260 <        Thread t = new Thread(new CheckedRunnable() {
260 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
261 >        Thread t = newStartedThread(new CheckedRunnable() {
262              public void realRun() throws InterruptedException {
263                  for (int i = 0; i < SIZE; ++i)
264                      q.put(i);
265                  assertEquals(SIZE, q.size());
266                  assertEquals(0, q.remainingCapacity());
267 +
268 +                Thread.currentThread().interrupt();
269 +                try {
270 +                    q.put(99);
271 +                    shouldThrow();
272 +                } catch (InterruptedException success) {}
273 +                assertFalse(Thread.interrupted());
274 +
275 +                pleaseInterrupt.countDown();
276                  try {
277                      q.put(99);
278                      shouldThrow();
279                  } catch (InterruptedException success) {}
280 +                assertFalse(Thread.interrupted());
281              }});
282  
283 <        t.start();
284 <        delay(SHORT_DELAY_MS);
283 >        await(pleaseInterrupt);
284 >        assertThreadStaysAlive(t);
285          t.interrupt();
286 <        t.join();
286 >        awaitTermination(t);
287          assertEquals(SIZE, q.size());
288          assertEquals(0, q.remainingCapacity());
289      }
290  
291      /**
292 <     * put blocks waiting for take when full
292 >     * put blocks interruptibly waiting for take when full
293       */
294      public void testPutWithTake() throws InterruptedException {
295          final int capacity = 2;
296          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
297 <        Thread t = new Thread(new CheckedRunnable() {
297 >        final CountDownLatch pleaseTake = new CountDownLatch(1);
298 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
299 >        Thread t = newStartedThread(new CheckedRunnable() {
300              public void realRun() throws InterruptedException {
301 <                for (int i = 0; i < capacity + 1; i++)
301 >                for (int i = 0; i < capacity; i++)
302                      q.put(i);
303 +                pleaseTake.countDown();
304 +                q.put(86);
305 +
306 +                pleaseInterrupt.countDown();
307                  try {
308                      q.put(99);
309                      shouldThrow();
310                  } catch (InterruptedException success) {}
311 +                assertFalse(Thread.interrupted());
312              }});
313  
314 <        t.start();
315 <        delay(SHORT_DELAY_MS);
344 <        assertEquals(q.remainingCapacity(), 0);
314 >        await(pleaseTake);
315 >        assertEquals(0, q.remainingCapacity());
316          assertEquals(0, q.take());
317 <        delay(SHORT_DELAY_MS);
317 >
318 >        await(pleaseInterrupt);
319 >        assertThreadStaysAlive(t);
320          t.interrupt();
321 <        t.join();
322 <        assertEquals(q.remainingCapacity(), 0);
321 >        awaitTermination(t);
322 >        assertEquals(0, q.remainingCapacity());
323      }
324  
325      /**
# Line 370 | Line 343 | public class LinkedBlockingQueueTest ext
343              }});
344  
345          await(pleaseInterrupt);
346 +        assertThreadStaysAlive(t);
347          t.interrupt();
348          awaitTermination(t);
349      }
# Line 388 | Line 362 | public class LinkedBlockingQueueTest ext
362       * Take removes existing elements until empty, then blocks interruptibly
363       */
364      public void testBlockingTake() throws InterruptedException {
365 <        final LinkedBlockingQueue q = populatedQueue(SIZE);
366 <        Thread t = new Thread(new CheckedRunnable() {
365 >        final BlockingQueue q = populatedQueue(SIZE);
366 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
367 >        Thread t = newStartedThread(new CheckedRunnable() {
368              public void realRun() throws InterruptedException {
369                  for (int i = 0; i < SIZE; ++i) {
370                      assertEquals(i, q.take());
371                  }
372 +
373 +                Thread.currentThread().interrupt();
374                  try {
375                      q.take();
376                      shouldThrow();
377                  } catch (InterruptedException success) {}
378 +                assertFalse(Thread.interrupted());
379 +
380 +                pleaseInterrupt.countDown();
381 +                try {
382 +                    q.take();
383 +                    shouldThrow();
384 +                } catch (InterruptedException success) {}
385 +                assertFalse(Thread.interrupted());
386              }});
387  
388 <        t.start();
389 <        delay(SHORT_DELAY_MS);
388 >        await(pleaseInterrupt);
389 >        assertThreadStaysAlive(t);
390          t.interrupt();
391 <        t.join();
391 >        awaitTermination(t);
392      }
393  
394      /**
# Line 432 | Line 417 | public class LinkedBlockingQueueTest ext
417       * timed poll with nonzero timeout succeeds when non-empty, else times out
418       */
419      public void testTimedPoll() throws InterruptedException {
420 <        LinkedBlockingQueue q = populatedQueue(SIZE);
420 >        LinkedBlockingQueue<Integer> q = populatedQueue(SIZE);
421          for (int i = 0; i < SIZE; ++i) {
422 <            assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
423 <        }
424 <        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
422 >            long startTime = System.nanoTime();
423 >            assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
424 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
425 >        }
426 >        long startTime = System.nanoTime();
427 >        assertNull(q.poll(timeoutMillis(), MILLISECONDS));
428 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
429 >        checkEmpty(q);
430      }
431  
432      /**
# Line 514 | Line 504 | public class LinkedBlockingQueueTest ext
504      }
505  
506      /**
517     * remove(x) removes x and returns true if present
518     */
519    public void testRemoveElement() {
520        LinkedBlockingQueue q = populatedQueue(SIZE);
521        for (int i = 1; i < SIZE; i+=2) {
522            assertTrue(q.contains(i));
523            assertTrue(q.remove(i));
524            assertFalse(q.contains(i));
525            assertTrue(q.contains(i-1));
526        }
527        for (int i = 0; i < SIZE; i+=2) {
528            assertTrue(q.contains(i));
529            assertTrue(q.remove(i));
530            assertFalse(q.contains(i));
531            assertFalse(q.remove(i+1));
532            assertFalse(q.contains(i+1));
533        }
534        assertTrue(q.isEmpty());
535    }
536
537    /**
507       * An add following remove(x) succeeds
508       */
509      public void testRemoveElementAndAdd() throws InterruptedException {
# Line 647 | Line 616 | public class LinkedBlockingQueueTest ext
616      }
617  
618      /**
650     * toArray(null) throws NullPointerException
651     */
652    public void testToArray_NullArg() {
653        LinkedBlockingQueue q = populatedQueue(SIZE);
654        try {
655            q.toArray(null);
656            shouldThrow();
657        } catch (NullPointerException success) {}
658    }
659
660    /**
619       * toArray(incompatible array type) throws ArrayStoreException
620       */
621      public void testToArray1_BadArg() {
# Line 668 | Line 626 | public class LinkedBlockingQueueTest ext
626          } catch (ArrayStoreException success) {}
627      }
628  
671
629      /**
630       * iterator iterates through all elements
631       */
# Line 699 | Line 656 | public class LinkedBlockingQueueTest ext
656          assertFalse(it.hasNext());
657      }
658  
702
659      /**
660       * iterator ordering is FIFO
661       */
# Line 731 | Line 687 | public class LinkedBlockingQueueTest ext
687          assertEquals(0, q.size());
688      }
689  
734
690      /**
691       * toString contains toStrings of elements
692       */
# Line 739 | Line 694 | public class LinkedBlockingQueueTest ext
694          LinkedBlockingQueue q = populatedQueue(SIZE);
695          String s = q.toString();
696          for (int i = 0; i < SIZE; ++i) {
697 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
697 >            assertTrue(s.contains(String.valueOf(i)));
698          }
699      }
700  
746
701      /**
702       * offer transfers elements across Executor tasks
703       */
# Line 752 | Line 706 | public class LinkedBlockingQueueTest ext
706          q.add(one);
707          q.add(two);
708          ExecutorService executor = Executors.newFixedThreadPool(2);
709 +        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
710          executor.execute(new CheckedRunnable() {
711              public void realRun() throws InterruptedException {
712                  assertFalse(q.offer(three));
713 <                assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
713 >                threadsStarted.await();
714 >                assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
715                  assertEquals(0, q.remainingCapacity());
716              }});
717  
718          executor.execute(new CheckedRunnable() {
719              public void realRun() throws InterruptedException {
720 <                delay(SMALL_DELAY_MS);
720 >                threadsStarted.await();
721                  assertSame(one, q.take());
722              }});
723  
# Line 769 | Line 725 | public class LinkedBlockingQueueTest ext
725      }
726  
727      /**
728 <     * poll retrieves elements across Executor threads
728 >     * timed poll retrieves elements across Executor threads
729       */
730      public void testPollInExecutor() {
731          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
732 +        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
733          ExecutorService executor = Executors.newFixedThreadPool(2);
734          executor.execute(new CheckedRunnable() {
735              public void realRun() throws InterruptedException {
736                  assertNull(q.poll());
737 <                assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
738 <                assertTrue(q.isEmpty());
737 >                threadsStarted.await();
738 >                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
739 >                checkEmpty(q);
740              }});
741  
742          executor.execute(new CheckedRunnable() {
743              public void realRun() throws InterruptedException {
744 <                delay(SMALL_DELAY_MS);
744 >                threadsStarted.await();
745                  q.put(one);
746              }});
747  
# Line 794 | Line 752 | public class LinkedBlockingQueueTest ext
752       * A deserialized serialized queue has same elements in same order
753       */
754      public void testSerialization() throws Exception {
755 <        LinkedBlockingQueue q = populatedQueue(SIZE);
756 <
799 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
800 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
801 <        out.writeObject(q);
802 <        out.close();
755 >        Queue x = populatedQueue(SIZE);
756 >        Queue y = serialClone(x);
757  
758 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
759 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
760 <        LinkedBlockingQueue r = (LinkedBlockingQueue)in.readObject();
761 <        assertEquals(q.size(), r.size());
762 <        while (!q.isEmpty())
763 <            assertEquals(q.remove(), r.remove());
764 <    }
765 <
766 <    /**
813 <     * drainTo(null) throws NPE
814 <     */
815 <    public void testDrainToNull() {
816 <        LinkedBlockingQueue q = populatedQueue(SIZE);
817 <        try {
818 <            q.drainTo(null);
819 <            shouldThrow();
820 <        } catch (NullPointerException success) {}
821 <    }
822 <
823 <    /**
824 <     * drainTo(this) throws IAE
825 <     */
826 <    public void testDrainToSelf() {
827 <        LinkedBlockingQueue q = populatedQueue(SIZE);
828 <        try {
829 <            q.drainTo(q);
830 <            shouldThrow();
831 <        } catch (IllegalArgumentException success) {}
758 >        assertTrue(x != y);
759 >        assertEquals(x.size(), y.size());
760 >        assertEquals(x.toString(), y.toString());
761 >        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
762 >        while (!x.isEmpty()) {
763 >            assertFalse(y.isEmpty());
764 >            assertEquals(x.remove(), y.remove());
765 >        }
766 >        assertTrue(y.isEmpty());
767      }
768  
769      /**
# Line 838 | Line 773 | public class LinkedBlockingQueueTest ext
773          LinkedBlockingQueue q = populatedQueue(SIZE);
774          ArrayList l = new ArrayList();
775          q.drainTo(l);
776 <        assertEquals(q.size(), 0);
777 <        assertEquals(l.size(), SIZE);
776 >        assertEquals(0, q.size());
777 >        assertEquals(SIZE, l.size());
778          for (int i = 0; i < SIZE; ++i)
779              assertEquals(l.get(i), new Integer(i));
780          q.add(zero);
# Line 849 | Line 784 | public class LinkedBlockingQueueTest ext
784          assertTrue(q.contains(one));
785          l.clear();
786          q.drainTo(l);
787 <        assertEquals(q.size(), 0);
788 <        assertEquals(l.size(), 2);
787 >        assertEquals(0, q.size());
788 >        assertEquals(2, l.size());
789          for (int i = 0; i < 2; ++i)
790              assertEquals(l.get(i), new Integer(i));
791      }
# Line 876 | Line 811 | public class LinkedBlockingQueueTest ext
811      }
812  
813      /**
879     * drainTo(null, n) throws NPE
880     */
881    public void testDrainToNullN() {
882        LinkedBlockingQueue q = populatedQueue(SIZE);
883        try {
884            q.drainTo(null, 0);
885            shouldThrow();
886        } catch (NullPointerException success) {}
887    }
888
889    /**
890     * drainTo(this, n) throws IAE
891     */
892    public void testDrainToSelfN() {
893        LinkedBlockingQueue q = populatedQueue(SIZE);
894        try {
895            q.drainTo(q, 0);
896            shouldThrow();
897        } catch (IllegalArgumentException success) {}
898    }
899
900    /**
814       * drainTo(c, n) empties first min(n, size) elements of queue into c
815       */
816      public void testDrainToN() {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines