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.19 by jsr166, Sat Nov 21 10:29:49 2009 UTC vs.
Revision 1.49 by jsr166, Tue May 31 16:16:23 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 Andrew Wright, Jeffrey Hayes,
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 +
25 +    public static class Fair extends BlockingQueueTest {
26 +        protected BlockingQueue emptyCollection() {
27 +            return new ArrayBlockingQueue(20, true);
28 +        }
29 +    }
30 +
31 +    public static class NonFair extends BlockingQueueTest {
32 +        protected BlockingQueue emptyCollection() {
33 +            return new ArrayBlockingQueue(20, false);
34 +        }
35 +    }
36 +
37      public static void main(String[] args) {
38 <        junit.textui.TestRunner.run (suite());
38 >        junit.textui.TestRunner.run(suite());
39      }
40 +
41      public static Test suite() {
42 <        return new TestSuite(ArrayBlockingQueueTest.class);
42 >        return newTestSuite(ArrayBlockingQueueTest.class,
43 >                            new Fair().testSuite(),
44 >                            new NonFair().testSuite());
45      }
46  
47      /**
48       * Create a queue of given size containing consecutive
49       * Integers 0 ... n.
50       */
51 <    private ArrayBlockingQueue populatedQueue(int n) {
52 <        ArrayBlockingQueue q = new ArrayBlockingQueue(n);
51 >    private ArrayBlockingQueue<Integer> populatedQueue(int n) {
52 >        ArrayBlockingQueue<Integer> q = new ArrayBlockingQueue<Integer>(n);
53          assertTrue(q.isEmpty());
54          for (int i = 0; i < n; i++)
55              assertTrue(q.offer(new Integer(i)));
# Line 48 | 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 58 | 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 67 | 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];
72 <            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints));
95 >            new ArrayBlockingQueue(SIZE, false, elements);
96              shouldThrow();
97          } catch (NullPointerException success) {}
98      }
# Line 78 | 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];
83 <            for (int i = 0; i < SIZE-1; ++i)
84 <                ints[i] = new Integer(i);
85 <            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 91 | 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];
96 <            for (int i = 0; i < SIZE; ++i)
97 <                ints[i] = new Integer(i);
98 <            ArrayBlockingQueue q = new ArrayBlockingQueue(1, false, Arrays.asList(ints));
123 >            new ArrayBlockingQueue(SIZE - 1, false, elements);
124              shouldThrow();
125          } catch (IllegalArgumentException success) {}
126      }
# Line 106 | 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 145 | Line 171 | public class ArrayBlockingQueueTest exte
171      }
172  
173      /**
148     *  offer(null) throws NPE
149     */
150    public void testOfferNull() {
151        try {
152            ArrayBlockingQueue q = new ArrayBlockingQueue(1);
153            q.offer(null);
154            shouldThrow();
155        } catch (NullPointerException success) {}
156    }
157
158    /**
159     *  add(null) throws NPE
160     */
161    public void testAddNull() {
162        try {
163            ArrayBlockingQueue q = new ArrayBlockingQueue(1);
164            q.add(null);
165            shouldThrow();
166        } catch (NullPointerException success) {}
167    }
168
169    /**
174       * Offer succeeds if not full; fails if full
175       */
176      public void testOffer() {
# Line 191 | Line 195 | public class ArrayBlockingQueueTest exte
195      }
196  
197      /**
194     *  addAll(null) throws NPE
195     */
196    public void testAddAll1() {
197        try {
198            ArrayBlockingQueue q = new ArrayBlockingQueue(1);
199            q.addAll(null);
200            shouldThrow();
201        } catch (NullPointerException success) {}
202    }
203
204    /**
198       * addAll(this) throws IAE
199       */
200      public void testAddAllSelf() {
# Line 212 | Line 205 | public class ArrayBlockingQueueTest exte
205          } catch (IllegalArgumentException success) {}
206      }
207  
215
216    /**
217     *  addAll of a collection with null elements throws NPE
218     */
219    public void testAddAll2() {
220        try {
221            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
222            Integer[] ints = new Integer[SIZE];
223            q.addAll(Arrays.asList(ints));
224            shouldThrow();
225        } catch (NullPointerException success) {}
226    }
208      /**
209       * addAll of a collection with any null elements throws NPE after
210       * possibly adding some elements
# Line 238 | Line 219 | public class ArrayBlockingQueueTest exte
219              shouldThrow();
220          } catch (NullPointerException success) {}
221      }
222 +
223      /**
224       * addAll throws ISE if not enough room
225       */
# Line 251 | Line 233 | public class ArrayBlockingQueueTest exte
233              shouldThrow();
234          } catch (IllegalStateException success) {}
235      }
236 +
237      /**
238       * Queue contains all elements, in traversal order, of successful addAll
239       */
# Line 267 | Line 250 | public class ArrayBlockingQueueTest exte
250      }
251  
252      /**
270     *  put(null) throws NPE
271     */
272    public void testPutNull() throws InterruptedException {
273        try {
274            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
275            q.put(null);
276            shouldThrow();
277        } catch (NullPointerException success) {}
278     }
279
280    /**
253       * all elements successfully put are contained
254       */
255      public void testPut() throws InterruptedException {
# Line 295 | 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() {
271 <            public void realRun() {
272 <                int added = 0;
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 <                    for (int i = 0; i < SIZE; ++i) {
281 <                        q.put(new Integer(i));
282 <                        ++added;
283 <                    }
306 <                    q.put(new Integer(SIZE));
307 <                    threadShouldThrow();
308 <                } catch (InterruptedException ie) {
309 <                    threadAssertEquals(added, SIZE);
310 <                }}});
280 >                    q.put(99);
281 >                    shouldThrow();
282 >                } catch (InterruptedException success) {}
283 >                assertFalse(Thread.interrupted());
284  
285 <        t.start();
286 <        Thread.sleep(MEDIUM_DELAY_MS);
285 >                pleaseInterrupt.countDown();
286 >                try {
287 >                    q.put(99);
288 >                    shouldThrow();
289 >                } catch (InterruptedException success) {}
290 >                assertFalse(Thread.interrupted());
291 >            }});
292 >
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 ArrayBlockingQueue q = new ArrayBlockingQueue(2);
306 <        Thread t = new Thread(new CheckedRunnable() {
307 <            public void realRun() {
308 <                int added = 0;
305 >        final int capacity = 2;
306 >        final ArrayBlockingQueue q = new ArrayBlockingQueue(capacity);
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; i++)
312 >                    q.put(i);
313 >                pleaseTake.countDown();
314 >                q.put(86);
315 >
316 >                pleaseInterrupt.countDown();
317                  try {
318 <                    q.put(new Object());
319 <                    ++added;
320 <                    q.put(new Object());
321 <                    ++added;
331 <                    q.put(new Object());
332 <                    ++added;
333 <                    q.put(new Object());
334 <                    ++added;
335 <                    threadShouldThrow();
336 <                } catch (InterruptedException e) {
337 <                    threadAssertTrue(added >= 2);
338 <                }
318 >                    q.put(99);
319 >                    shouldThrow();
320 >                } catch (InterruptedException success) {}
321 >                assertFalse(Thread.interrupted());
322              }});
323  
324 <        t.start();
325 <        Thread.sleep(SHORT_DELAY_MS);
326 <        q.take();
324 >        await(pleaseTake);
325 >        assertEquals(q.remainingCapacity(), 0);
326 >        assertEquals(0, q.take());
327 >
328 >        await(pleaseInterrupt);
329 >        assertThreadStaysAlive(t);
330          t.interrupt();
331 <        t.join();
331 >        awaitTermination(t);
332 >        assertEquals(q.remainingCapacity(), 0);
333      }
334  
335      /**
# Line 350 | Line 337 | public class ArrayBlockingQueueTest exte
337       */
338      public void testTimedOffer() throws InterruptedException {
339          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
340 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
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 <                threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, MILLISECONDS));
346 <                q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
347 <            }};
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(), 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 370 | Line 364 | public class ArrayBlockingQueueTest exte
364      public void testTake() throws InterruptedException {
365          ArrayBlockingQueue q = populatedQueue(SIZE);
366          for (int i = 0; i < SIZE; ++i) {
367 <            assertEquals(i, ((Integer)q.take()).intValue());
367 >            assertEquals(i, q.take());
368          }
369      }
370  
371      /**
378     * take blocks interruptibly when empty
379     */
380    public void testTakeFromEmpty() throws InterruptedException {
381        final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
382        Thread t = new ThreadShouldThrow(InterruptedException.class) {
383            public void realRun() throws InterruptedException {
384                q.take();
385            }};
386
387        t.start();
388        Thread.sleep(SHORT_DELAY_MS);
389        t.interrupt();
390        t.join();
391    }
392
393    /**
372       * Take removes existing elements until empty, then blocks interruptibly
373       */
374      public void testBlockingTake() throws InterruptedException {
375 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
375 >        final ArrayBlockingQueue q = populatedQueue(SIZE);
376 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
377 >        Thread t = newStartedThread(new CheckedRunnable() {
378              public void realRun() throws InterruptedException {
399                ArrayBlockingQueue q = populatedQueue(SIZE);
379                  for (int i = 0; i < SIZE; ++i) {
380 <                    threadAssertEquals(i, ((Integer)q.take()).intValue());
380 >                    assertEquals(i, q.take());
381                  }
403                q.take();
404            }};
382  
383 <        t.start();
384 <            Thread.sleep(SHORT_DELAY_MS);
385 <            t.interrupt();
386 <            t.join();
387 <    }
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 +        await(pleaseInterrupt);
399 +        assertThreadStaysAlive(t);
400 +        t.interrupt();
401 +        awaitTermination(t);
402 +    }
403  
404      /**
405       * poll succeeds unless empty
# Line 416 | Line 407 | public class ArrayBlockingQueueTest exte
407      public void testPoll() {
408          ArrayBlockingQueue q = populatedQueue(SIZE);
409          for (int i = 0; i < SIZE; ++i) {
410 <            assertEquals(i, ((Integer)q.poll()).intValue());
410 >            assertEquals(i, q.poll());
411          }
412          assertNull(q.poll());
413      }
414  
415      /**
416 <     * timed pool with zero timeout succeeds when non-empty, else times out
416 >     * timed poll with zero timeout succeeds when non-empty, else times out
417       */
418      public void testTimedPoll0() throws InterruptedException {
419          ArrayBlockingQueue q = populatedQueue(SIZE);
420          for (int i = 0; i < SIZE; ++i) {
421 <            assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue());
421 >            assertEquals(i, q.poll(0, MILLISECONDS));
422          }
423          assertNull(q.poll(0, MILLISECONDS));
424 +        checkEmpty(q);
425      }
426  
427      /**
428 <     * timed pool with nonzero timeout succeeds when non-empty, else times out
428 >     * timed poll with nonzero timeout succeeds when non-empty, else times out
429       */
430      public void testTimedPoll() throws InterruptedException {
431          ArrayBlockingQueue q = populatedQueue(SIZE);
432          for (int i = 0; i < SIZE; ++i) {
433 <            assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
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 448 | Line 445 | public class ArrayBlockingQueueTest exte
445       * returning timeout status
446       */
447      public void testInterruptedTimedPoll() throws InterruptedException {
448 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
448 >        final BlockingQueue<Integer> q = populatedQueue(SIZE);
449 >        final CountDownLatch aboutToWait = new CountDownLatch(1);
450 >        Thread t = newStartedThread(new CheckedRunnable() {
451              public void realRun() throws InterruptedException {
453                ArrayBlockingQueue q = populatedQueue(SIZE);
452                  for (int i = 0; i < SIZE; ++i) {
453 <                    threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
453 >                    long t0 = System.nanoTime();
454 >                    assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
455 >                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
456                  }
457 <                q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
458 <            }};
459 <
460 <        t.start();
461 <        Thread.sleep(SMALL_DELAY_MS);
462 <        t.interrupt();
463 <        t.join();
464 <    }
465 <
466 <    /**
467 <     *  timed poll before a delayed offer fails; after offer succeeds;
468 <     *  on interruption throws
469 <     */
470 <    public void testTimedPollWithOffer() throws InterruptedException {
471 <        final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
472 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
473 <            public void realRun() throws InterruptedException {
474 <                threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
475 <                q.poll(LONG_DELAY_MS, MILLISECONDS);
476 <                q.poll(LONG_DELAY_MS, MILLISECONDS);
477 <            }};
457 >                long t0 = System.nanoTime();
458 >                aboutToWait.countDown();
459 >                try {
460 >                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
461 >                    shouldThrow();
462 >                } catch (InterruptedException success) {
463 >                    assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
464 >                }
465 >            }});
466  
467 <        t.start();
468 <        Thread.sleep(SMALL_DELAY_MS);
481 <        assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
467 >        aboutToWait.await();
468 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
469          t.interrupt();
470 <        t.join();
470 >        awaitTermination(t, MEDIUM_DELAY_MS);
471 >        checkEmpty(q);
472      }
473  
486
474      /**
475       * peek returns next element, or null if empty
476       */
477      public void testPeek() {
478          ArrayBlockingQueue q = populatedQueue(SIZE);
479          for (int i = 0; i < SIZE; ++i) {
480 <            assertEquals(i, ((Integer)q.peek()).intValue());
481 <            q.poll();
480 >            assertEquals(i, q.peek());
481 >            assertEquals(i, q.poll());
482              assertTrue(q.peek() == null ||
483 <                       i != ((Integer)q.peek()).intValue());
483 >                       !q.peek().equals(i));
484          }
485          assertNull(q.peek());
486      }
# Line 504 | Line 491 | public class ArrayBlockingQueueTest exte
491      public void testElement() {
492          ArrayBlockingQueue q = populatedQueue(SIZE);
493          for (int i = 0; i < SIZE; ++i) {
494 <            assertEquals(i, ((Integer)q.element()).intValue());
495 <            q.poll();
494 >            assertEquals(i, q.element());
495 >            assertEquals(i, q.poll());
496          }
497          try {
498              q.element();
# Line 519 | Line 506 | public class ArrayBlockingQueueTest exte
506      public void testRemove() {
507          ArrayBlockingQueue q = populatedQueue(SIZE);
508          for (int i = 0; i < SIZE; ++i) {
509 <            assertEquals(i, ((Integer)q.remove()).intValue());
509 >            assertEquals(i, q.remove());
510          }
511          try {
512              q.remove();
# Line 549 | Line 536 | public class ArrayBlockingQueueTest exte
536          ArrayBlockingQueue q = populatedQueue(SIZE);
537          for (int i = 0; i < SIZE; ++i) {
538              assertTrue(q.contains(new Integer(i)));
539 <            q.poll();
539 >            assertEquals(i, q.poll());
540              assertFalse(q.contains(new Integer(i)));
541          }
542      }
# Line 620 | Line 607 | public class ArrayBlockingQueueTest exte
607      }
608  
609      /**
610 <     *  toArray contains all elements
610 >     * toArray contains all elements in FIFO order
611       */
612 <    public void testToArray() throws InterruptedException {
612 >    public void testToArray() {
613          ArrayBlockingQueue q = populatedQueue(SIZE);
614          Object[] o = q.toArray();
615          for (int i = 0; i < o.length; i++)
616 <            assertEquals(o[i], q.take());
616 >            assertSame(o[i], q.poll());
617      }
618  
619      /**
620 <     * toArray(a) contains all elements
620 >     * toArray(a) contains all elements in FIFO order
621       */
622 <    public void testToArray2() throws InterruptedException {
623 <        ArrayBlockingQueue q = populatedQueue(SIZE);
622 >    public void testToArray2() {
623 >        ArrayBlockingQueue<Integer> q = populatedQueue(SIZE);
624          Integer[] ints = new Integer[SIZE];
625 <        ints = (Integer[])q.toArray(ints);
625 >        Integer[] array = q.toArray(ints);
626 >        assertSame(ints, array);
627          for (int i = 0; i < ints.length; i++)
628 <            assertEquals(ints[i], q.take());
628 >            assertSame(ints[i], q.poll());
629      }
630  
631      /**
632 <     * toArray(null) throws NPE
645 <     */
646 <    public void testToArray_BadArg() {
647 <        try {
648 <            ArrayBlockingQueue q = populatedQueue(SIZE);
649 <            Object o[] = q.toArray(null);
650 <            shouldThrow();
651 <        } catch (NullPointerException success) {}
652 <    }
653 <
654 <    /**
655 <     * toArray with incompatible array type throws CCE
632 >     * toArray(incompatible array type) throws ArrayStoreException
633       */
634      public void testToArray1_BadArg() {
635 +        ArrayBlockingQueue q = populatedQueue(SIZE);
636          try {
637 <            ArrayBlockingQueue q = populatedQueue(SIZE);
660 <            Object o[] = q.toArray(new String[10] );
637 >            q.toArray(new String[10]);
638              shouldThrow();
639          } catch (ArrayStoreException success) {}
640      }
641  
665
642      /**
643       * iterator iterates through all elements
644       */
# Line 677 | Line 653 | public class ArrayBlockingQueueTest exte
653      /**
654       * iterator.remove removes current element
655       */
656 <    public void testIteratorRemove () {
656 >    public void testIteratorRemove() {
657          final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
658          q.add(two);
659          q.add(one);
# Line 688 | Line 664 | public class ArrayBlockingQueueTest exte
664          it.remove();
665  
666          it = q.iterator();
667 <        assertEquals(it.next(), one);
668 <        assertEquals(it.next(), three);
667 >        assertSame(it.next(), one);
668 >        assertSame(it.next(), three);
669          assertFalse(it.hasNext());
670      }
671  
# Line 706 | Line 682 | public class ArrayBlockingQueueTest exte
682  
683          int k = 0;
684          for (Iterator it = q.iterator(); it.hasNext();) {
685 <            int i = ((Integer)(it.next())).intValue();
710 <            assertEquals(++k, i);
685 >            assertEquals(++k, it.next());
686          }
687          assertEquals(3, k);
688      }
# Line 715 | Line 690 | public class ArrayBlockingQueueTest exte
690      /**
691       * Modifications do not cause iterators to fail
692       */
693 <    public void testWeaklyConsistentIteration () {
693 >    public void testWeaklyConsistentIteration() {
694          final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
695          q.add(one);
696          q.add(two);
# Line 727 | Line 702 | public class ArrayBlockingQueueTest exte
702          assertEquals(0, q.size());
703      }
704  
730
705      /**
706       * toString contains toStrings of elements
707       */
# Line 735 | 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  
742
716      /**
717       * offer transfers elements across Executor tasks
718       */
# Line 748 | 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 <                threadAssertFalse(q.offer(three));
728 <                threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
729 <                threadAssertEquals(0, q.remainingCapacity());
727 >                assertFalse(q.offer(three));
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);
736 <                threadAssertEquals(one, q.take());
735 >                threadsStarted.await();
736 >                assertEquals(0, q.remainingCapacity());
737 >                assertSame(one, q.take());
738              }});
739  
740          joinPool(executor);
765
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 <                threadAssertNull(q.poll());
753 <                threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
754 <                threadAssertTrue(q.isEmpty());
752 >                assertNull(q.poll());
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 791 | 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 <
796 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
797 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
798 <        out.writeObject(q);
799 <        out.close();
800 <
801 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
802 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
803 <        ArrayBlockingQueue r = (ArrayBlockingQueue)in.readObject();
804 <        assertEquals(q.size(), r.size());
805 <        while (!q.isEmpty())
806 <            assertEquals(q.remove(), r.remove());
807 <    }
771 >        Queue x = populatedQueue(SIZE);
772 >        Queue y = serialClone(x);
773  
774 <    /**
775 <     * drainTo(null) throws NPE
776 <     */
777 <    public void testDrainToNull() {
778 <        ArrayBlockingQueue q = populatedQueue(SIZE);
779 <        try {
780 <            q.drainTo(null);
781 <            shouldThrow();
782 <        } catch (NullPointerException success) {}
818 <    }
819 <
820 <    /**
821 <     * drainTo(this) throws IAE
822 <     */
823 <    public void testDrainToSelf() {
824 <        ArrayBlockingQueue q = populatedQueue(SIZE);
825 <        try {
826 <            q.drainTo(q);
827 <            shouldThrow();
828 <        } 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 873 | Line 827 | public class ArrayBlockingQueueTest exte
827      }
828  
829      /**
830 <     * drainTo(null, n) throws NPE
877 <     */
878 <    public void testDrainToNullN() {
879 <        ArrayBlockingQueue q = populatedQueue(SIZE);
880 <        try {
881 <            q.drainTo(null, 0);
882 <            shouldThrow();
883 <        } catch (NullPointerException success) {}
884 <    }
885 <
886 <    /**
887 <     * drainTo(this, n) throws IAE
888 <     */
889 <    public void testDrainToSelfN() {
890 <        ArrayBlockingQueue q = populatedQueue(SIZE);
891 <        try {
892 <            q.drainTo(q, 0);
893 <            shouldThrow();
894 <        } catch (IllegalArgumentException success) {}
895 <    }
896 <
897 <    /**
898 <     * drainTo(c, n) empties first max {n, size} elements of queue into c
830 >     * drainTo(c, n) empties first min(n, size) elements of queue into c
831       */
832      public void testDrainToN() {
833          ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE*2);
# Line 904 | Line 836 | public class ArrayBlockingQueueTest exte
836                  assertTrue(q.offer(new Integer(j)));
837              ArrayList l = new ArrayList();
838              q.drainTo(l, i);
839 <            int k = (i < SIZE)? i : SIZE;
839 >            int k = (i < SIZE) ? i : SIZE;
840              assertEquals(l.size(), k);
841              assertEquals(q.size(), SIZE-k);
842              for (int j = 0; j < k; ++j)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines