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

Comparing jsr166/src/test/tck/ArrayBlockingQueueTest.java (file contents):
Revision 1.43 by jsr166, Tue Mar 15 19:47:06 2011 UTC vs.
Revision 1.49 by jsr166, Tue May 31 16:16:23 2011 UTC

# Line 6 | Line 6
6   * Pat Fisher, Mike Judd.
7   */
8  
9
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.ArrayBlockingQueue;
17 > import java.util.concurrent.BlockingQueue;
18 > import java.util.concurrent.CountDownLatch;
19 > import java.util.concurrent.Executors;
20 > import java.util.concurrent.ExecutorService;
21   import static java.util.concurrent.TimeUnit.MILLISECONDS;
14 import java.io.*;
22  
23   public class ArrayBlockingQueueTest extends JSR166TestCase {
24  
# Line 64 | Line 71 | public class ArrayBlockingQueueTest exte
71       */
72      public void testConstructor2() {
73          try {
74 <            ArrayBlockingQueue q = new ArrayBlockingQueue(0);
74 >            new ArrayBlockingQueue(0);
75              shouldThrow();
76          } catch (IllegalArgumentException success) {}
77      }
# Line 74 | Line 81 | public class ArrayBlockingQueueTest exte
81       */
82      public void testConstructor3() {
83          try {
84 <            ArrayBlockingQueue q = new ArrayBlockingQueue(1, true, null);
84 >            new ArrayBlockingQueue(1, true, null);
85              shouldThrow();
86          } catch (NullPointerException success) {}
87      }
# Line 83 | Line 90 | public class ArrayBlockingQueueTest exte
90       * Initializing from Collection of null elements throws NPE
91       */
92      public void testConstructor4() {
93 +        Collection<Integer> elements = Arrays.asList(new Integer[SIZE]);
94          try {
95 <            Integer[] ints = new Integer[SIZE];
88 <            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints));
95 >            new ArrayBlockingQueue(SIZE, false, elements);
96              shouldThrow();
97          } catch (NullPointerException success) {}
98      }
# Line 94 | Line 101 | public class ArrayBlockingQueueTest exte
101       * Initializing from Collection with some null elements throws NPE
102       */
103      public void testConstructor5() {
104 +        Integer[] ints = new Integer[SIZE];
105 +        for (int i = 0; i < SIZE-1; ++i)
106 +            ints[i] = i;
107 +        Collection<Integer> elements = Arrays.asList(ints);
108          try {
109 <            Integer[] ints = new Integer[SIZE];
99 <            for (int i = 0; i < SIZE-1; ++i)
100 <                ints[i] = new Integer(i);
101 <            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints));
109 >            new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints));
110              shouldThrow();
111          } catch (NullPointerException success) {}
112      }
# Line 107 | Line 115 | public class ArrayBlockingQueueTest exte
115       * Initializing from too large collection throws IAE
116       */
117      public void testConstructor6() {
118 +        Integer[] ints = new Integer[SIZE];
119 +        for (int i = 0; i < SIZE; ++i)
120 +            ints[i] = i;
121 +        Collection<Integer> elements = Arrays.asList(ints);
122          try {
123 <            Integer[] ints = new Integer[SIZE];
112 <            for (int i = 0; i < SIZE; ++i)
113 <                ints[i] = new Integer(i);
114 <            ArrayBlockingQueue q = new ArrayBlockingQueue(1, false, Arrays.asList(ints));
123 >            new ArrayBlockingQueue(SIZE - 1, false, elements);
124              shouldThrow();
125          } catch (IllegalArgumentException success) {}
126      }
# Line 122 | Line 131 | public class ArrayBlockingQueueTest exte
131      public void testConstructor7() {
132          Integer[] ints = new Integer[SIZE];
133          for (int i = 0; i < SIZE; ++i)
134 <            ints[i] = new Integer(i);
135 <        ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, true, Arrays.asList(ints));
134 >            ints[i] = i;
135 >        Collection<Integer> elements = Arrays.asList(ints);
136 >        ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, true, elements);
137          for (int i = 0; i < SIZE; ++i)
138              assertEquals(ints[i], q.poll());
139      }
# Line 161 | Line 171 | public class ArrayBlockingQueueTest exte
171      }
172  
173      /**
164     * offer(null) throws NPE
165     */
166    public void testOfferNull() {
167        try {
168            ArrayBlockingQueue q = new ArrayBlockingQueue(1);
169            q.offer(null);
170            shouldThrow();
171        } catch (NullPointerException success) {}
172    }
173
174    /**
175     * add(null) throws NPE
176     */
177    public void testAddNull() {
178        try {
179            ArrayBlockingQueue q = new ArrayBlockingQueue(1);
180            q.add(null);
181            shouldThrow();
182        } catch (NullPointerException success) {}
183    }
184
185    /**
174       * Offer succeeds if not full; fails if full
175       */
176      public void testOffer() {
# Line 207 | Line 195 | public class ArrayBlockingQueueTest exte
195      }
196  
197      /**
210     * addAll(null) throws NPE
211     */
212    public void testAddAll1() {
213        try {
214            ArrayBlockingQueue q = new ArrayBlockingQueue(1);
215            q.addAll(null);
216            shouldThrow();
217        } catch (NullPointerException success) {}
218    }
219
220    /**
198       * addAll(this) throws IAE
199       */
200      public void testAddAllSelf() {
# Line 228 | Line 205 | public class ArrayBlockingQueueTest exte
205          } catch (IllegalArgumentException success) {}
206      }
207  
231
232    /**
233     * addAll of a collection with null elements throws NPE
234     */
235    public void testAddAll2() {
236        try {
237            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
238            Integer[] ints = new Integer[SIZE];
239            q.addAll(Arrays.asList(ints));
240            shouldThrow();
241        } catch (NullPointerException success) {}
242    }
243
208      /**
209       * addAll of a collection with any null elements throws NPE after
210       * possibly adding some elements
# Line 286 | Line 250 | public class ArrayBlockingQueueTest exte
250      }
251  
252      /**
289     * put(null) throws NPE
290     */
291    public void testPutNull() throws InterruptedException {
292        try {
293            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
294            q.put(null);
295            shouldThrow();
296        } catch (NullPointerException success) {}
297     }
298
299    /**
253       * all elements successfully put are contained
254       */
255      public void testPut() throws InterruptedException {
# Line 314 | Line 267 | public class ArrayBlockingQueueTest exte
267       */
268      public void testBlockingPut() throws InterruptedException {
269          final ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
270 <        Thread t = new Thread(new CheckedRunnable() {
270 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
271 >        Thread t = newStartedThread(new CheckedRunnable() {
272              public void realRun() throws InterruptedException {
273                  for (int i = 0; i < SIZE; ++i)
274                      q.put(i);
275                  assertEquals(SIZE, q.size());
276                  assertEquals(0, q.remainingCapacity());
277 +
278 +                Thread.currentThread().interrupt();
279                  try {
280                      q.put(99);
281                      shouldThrow();
282                  } catch (InterruptedException success) {}
283 +                assertFalse(Thread.interrupted());
284 +
285 +                pleaseInterrupt.countDown();
286 +                try {
287 +                    q.put(99);
288 +                    shouldThrow();
289 +                } catch (InterruptedException success) {}
290 +                assertFalse(Thread.interrupted());
291              }});
292  
293 <        t.start();
294 <        Thread.sleep(SHORT_DELAY_MS);
293 >        await(pleaseInterrupt);
294 >        assertThreadStaysAlive(t);
295          t.interrupt();
296 <        t.join();
296 >        awaitTermination(t);
297          assertEquals(SIZE, q.size());
298          assertEquals(0, q.remainingCapacity());
299      }
300  
301      /**
302 <     * put blocks waiting for take when full
302 >     * put blocks interruptibly waiting for take when full
303       */
304      public void testPutWithTake() throws InterruptedException {
305          final int capacity = 2;
306          final ArrayBlockingQueue q = new ArrayBlockingQueue(capacity);
307 <        Thread t = new Thread(new CheckedRunnable() {
307 >        final CountDownLatch pleaseTake = new CountDownLatch(1);
308 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
309 >        Thread t = newStartedThread(new CheckedRunnable() {
310              public void realRun() throws InterruptedException {
311 <                for (int i = 0; i < capacity + 1; i++)
311 >                for (int i = 0; i < capacity; i++)
312                      q.put(i);
313 +                pleaseTake.countDown();
314 +                q.put(86);
315 +
316 +                pleaseInterrupt.countDown();
317                  try {
318                      q.put(99);
319                      shouldThrow();
320                  } catch (InterruptedException success) {}
321 +                assertFalse(Thread.interrupted());
322              }});
323  
324 <        t.start();
354 <        Thread.sleep(SHORT_DELAY_MS);
324 >        await(pleaseTake);
325          assertEquals(q.remainingCapacity(), 0);
326          assertEquals(0, q.take());
327 <        Thread.sleep(SHORT_DELAY_MS);
327 >
328 >        await(pleaseInterrupt);
329 >        assertThreadStaysAlive(t);
330          t.interrupt();
331 <        t.join();
331 >        awaitTermination(t);
332          assertEquals(q.remainingCapacity(), 0);
333      }
334  
# Line 365 | Line 337 | public class ArrayBlockingQueueTest exte
337       */
338      public void testTimedOffer() throws InterruptedException {
339          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
340 <        Thread t = new Thread(new CheckedRunnable() {
340 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
341 >        Thread t = newStartedThread(new CheckedRunnable() {
342              public void realRun() throws InterruptedException {
343                  q.put(new Object());
344                  q.put(new Object());
345 <                assertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, MILLISECONDS));
345 >                long startTime = System.nanoTime();
346 >                assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS));
347 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
348 >                pleaseInterrupt.countDown();
349                  try {
350 <                    q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
350 >                    q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
351                      shouldThrow();
352                  } catch (InterruptedException success) {}
353              }});
354  
355 <        t.start();
356 <        Thread.sleep(SHORT_DELAY_MS);
355 >        await(pleaseInterrupt);
356 >        assertThreadStaysAlive(t);
357          t.interrupt();
358 <        t.join();
358 >        awaitTermination(t);
359      }
360  
361      /**
# Line 397 | Line 373 | public class ArrayBlockingQueueTest exte
373       */
374      public void testBlockingTake() throws InterruptedException {
375          final ArrayBlockingQueue q = populatedQueue(SIZE);
376 <        Thread t = new Thread(new CheckedRunnable() {
376 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
377 >        Thread t = newStartedThread(new CheckedRunnable() {
378              public void realRun() throws InterruptedException {
379                  for (int i = 0; i < SIZE; ++i) {
380                      assertEquals(i, q.take());
381                  }
382 +
383 +                Thread.currentThread().interrupt();
384                  try {
385                      q.take();
386                      shouldThrow();
387                  } catch (InterruptedException success) {}
388 +                assertFalse(Thread.interrupted());
389 +
390 +                pleaseInterrupt.countDown();
391 +                try {
392 +                    q.take();
393 +                    shouldThrow();
394 +                } catch (InterruptedException success) {}
395 +                assertFalse(Thread.interrupted());
396              }});
397  
398 <        t.start();
399 <        Thread.sleep(SHORT_DELAY_MS);
398 >        await(pleaseInterrupt);
399 >        assertThreadStaysAlive(t);
400          t.interrupt();
401 <        t.join();
401 >        awaitTermination(t);
402      }
403  
417
404      /**
405       * poll succeeds unless empty
406       */
# Line 435 | Line 421 | public class ArrayBlockingQueueTest exte
421              assertEquals(i, q.poll(0, MILLISECONDS));
422          }
423          assertNull(q.poll(0, MILLISECONDS));
424 +        checkEmpty(q);
425      }
426  
427      /**
# Line 443 | Line 430 | public class ArrayBlockingQueueTest exte
430      public void testTimedPoll() throws InterruptedException {
431          ArrayBlockingQueue q = populatedQueue(SIZE);
432          for (int i = 0; i < SIZE; ++i) {
433 <            assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
434 <        }
435 <        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
433 >            long startTime = System.nanoTime();
434 >            assertEquals(i, q.poll(LONG_DELAY_MS, MILLISECONDS));
435 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
436 >        }
437 >        long startTime = System.nanoTime();
438 >        assertNull(q.poll(timeoutMillis(), MILLISECONDS));
439 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
440 >        checkEmpty(q);
441      }
442  
443      /**
# Line 637 | Line 629 | public class ArrayBlockingQueueTest exte
629      }
630  
631      /**
640     * toArray(null) throws NullPointerException
641     */
642    public void testToArray_NullArg() {
643        ArrayBlockingQueue q = populatedQueue(SIZE);
644        try {
645            q.toArray(null);
646            shouldThrow();
647        } catch (NullPointerException success) {}
648    }
649
650    /**
632       * toArray(incompatible array type) throws ArrayStoreException
633       */
634      public void testToArray1_BadArg() {
# Line 658 | Line 639 | public class ArrayBlockingQueueTest exte
639          } catch (ArrayStoreException success) {}
640      }
641  
661
642      /**
643       * iterator iterates through all elements
644       */
# Line 722 | Line 702 | public class ArrayBlockingQueueTest exte
702          assertEquals(0, q.size());
703      }
704  
725
705      /**
706       * toString contains toStrings of elements
707       */
# Line 730 | Line 709 | public class ArrayBlockingQueueTest exte
709          ArrayBlockingQueue q = populatedQueue(SIZE);
710          String s = q.toString();
711          for (int i = 0; i < SIZE; ++i) {
712 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
712 >            assertTrue(s.contains(String.valueOf(i)));
713          }
714      }
715  
737
716      /**
717       * offer transfers elements across Executor tasks
718       */
# Line 743 | Line 721 | public class ArrayBlockingQueueTest exte
721          q.add(one);
722          q.add(two);
723          ExecutorService executor = Executors.newFixedThreadPool(2);
724 +        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
725          executor.execute(new CheckedRunnable() {
726              public void realRun() throws InterruptedException {
727                  assertFalse(q.offer(three));
728 <                assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
728 >                threadsStarted.await();
729 >                assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
730                  assertEquals(0, q.remainingCapacity());
731              }});
732  
733          executor.execute(new CheckedRunnable() {
734              public void realRun() throws InterruptedException {
735 <                Thread.sleep(SMALL_DELAY_MS);
735 >                threadsStarted.await();
736 >                assertEquals(0, q.remainingCapacity());
737                  assertSame(one, q.take());
738              }});
739  
# Line 760 | Line 741 | public class ArrayBlockingQueueTest exte
741      }
742  
743      /**
744 <     * poll retrieves elements across Executor threads
744 >     * timed poll retrieves elements across Executor threads
745       */
746      public void testPollInExecutor() {
747          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
748 +        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
749          ExecutorService executor = Executors.newFixedThreadPool(2);
750          executor.execute(new CheckedRunnable() {
751              public void realRun() throws InterruptedException {
752                  assertNull(q.poll());
753 <                assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
754 <                assertTrue(q.isEmpty());
753 >                threadsStarted.await();
754 >                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
755 >                checkEmpty(q);
756              }});
757  
758          executor.execute(new CheckedRunnable() {
759              public void realRun() throws InterruptedException {
760 <                Thread.sleep(SMALL_DELAY_MS);
760 >                threadsStarted.await();
761                  q.put(one);
762              }});
763  
# Line 785 | Line 768 | public class ArrayBlockingQueueTest exte
768       * A deserialized serialized queue has same elements in same order
769       */
770      public void testSerialization() throws Exception {
771 <        ArrayBlockingQueue q = populatedQueue(SIZE);
772 <
790 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
791 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
792 <        out.writeObject(q);
793 <        out.close();
771 >        Queue x = populatedQueue(SIZE);
772 >        Queue y = serialClone(x);
773  
774 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
775 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
776 <        ArrayBlockingQueue r = (ArrayBlockingQueue)in.readObject();
777 <        assertEquals(q.size(), r.size());
778 <        while (!q.isEmpty())
779 <            assertEquals(q.remove(), r.remove());
780 <    }
781 <
782 <    /**
804 <     * drainTo(null) throws NPE
805 <     */
806 <    public void testDrainToNull() {
807 <        ArrayBlockingQueue q = populatedQueue(SIZE);
808 <        try {
809 <            q.drainTo(null);
810 <            shouldThrow();
811 <        } catch (NullPointerException success) {}
812 <    }
813 <
814 <    /**
815 <     * drainTo(this) throws IAE
816 <     */
817 <    public void testDrainToSelf() {
818 <        ArrayBlockingQueue q = populatedQueue(SIZE);
819 <        try {
820 <            q.drainTo(q);
821 <            shouldThrow();
822 <        } catch (IllegalArgumentException success) {}
774 >        assertTrue(x != y);
775 >        assertEquals(x.size(), y.size());
776 >        assertEquals(x.toString(), y.toString());
777 >        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
778 >        while (!x.isEmpty()) {
779 >            assertFalse(y.isEmpty());
780 >            assertEquals(x.remove(), y.remove());
781 >        }
782 >        assertTrue(y.isEmpty());
783      }
784  
785      /**
# Line 867 | Line 827 | public class ArrayBlockingQueueTest exte
827      }
828  
829      /**
870     * drainTo(null, n) throws NPE
871     */
872    public void testDrainToNullN() {
873        ArrayBlockingQueue q = populatedQueue(SIZE);
874        try {
875            q.drainTo(null, 0);
876            shouldThrow();
877        } catch (NullPointerException success) {}
878    }
879
880    /**
881     * drainTo(this, n) throws IAE
882     */
883    public void testDrainToSelfN() {
884        ArrayBlockingQueue q = populatedQueue(SIZE);
885        try {
886            q.drainTo(q, 0);
887            shouldThrow();
888        } catch (IllegalArgumentException success) {}
889    }
890
891    /**
830       * drainTo(c, n) empties first min(n, size) elements of queue into c
831       */
832      public void testDrainToN() {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines