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.8 by dl, Mon Dec 29 19:05:40 2003 UTC vs.
Revision 1.46 by jsr166, Sat May 21 06:24:33 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
5 < * Other contributors include Andrew Wright, Jeffrey Hayes,
6 < * Pat Fisher, Mike Judd.
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5 > * Other contributors include Andrew Wright, Jeffrey Hayes,
6 > * Pat Fisher, Mike Judd.
7   */
8  
9  
10   import junit.framework.*;
11   import java.util.*;
12   import java.util.concurrent.*;
13 + import static java.util.concurrent.TimeUnit.MILLISECONDS;
14   import java.io.*;
15  
16   public class ArrayBlockingQueueTest extends JSR166TestCase {
17 +
18 +    public static class Fair extends BlockingQueueTest {
19 +        protected BlockingQueue emptyCollection() {
20 +            return new ArrayBlockingQueue(20, true);
21 +        }
22 +    }
23 +
24 +    public static class NonFair extends BlockingQueueTest {
25 +        protected BlockingQueue emptyCollection() {
26 +            return new ArrayBlockingQueue(20, false);
27 +        }
28 +    }
29 +
30      public static void main(String[] args) {
31 <        junit.textui.TestRunner.run (suite());  
31 >        junit.textui.TestRunner.run(suite());
32      }
33 +
34      public static Test suite() {
35 <        return new TestSuite(ArrayBlockingQueueTest.class);
35 >        return newTestSuite(ArrayBlockingQueueTest.class,
36 >                            new Fair().testSuite(),
37 >                            new NonFair().testSuite());
38      }
39  
40      /**
41       * Create a queue of given size containing consecutive
42       * Integers 0 ... n.
43       */
44 <    private ArrayBlockingQueue populatedQueue(int n) {
45 <        ArrayBlockingQueue q = new ArrayBlockingQueue(n);
44 >    private ArrayBlockingQueue<Integer> populatedQueue(int n) {
45 >        ArrayBlockingQueue<Integer> q = new ArrayBlockingQueue<Integer>(n);
46          assertTrue(q.isEmpty());
47 <        for(int i = 0; i < n; i++)
48 <            assertTrue(q.offer(new Integer(i)));
47 >        for (int i = 0; i < n; i++)
48 >            assertTrue(q.offer(new Integer(i)));
49          assertFalse(q.isEmpty());
50          assertEquals(0, q.remainingCapacity());
51 <        assertEquals(n, q.size());
51 >        assertEquals(n, q.size());
52          return q;
53      }
54 <
54 >
55      /**
56       * A new queue has the indicated capacity
57       */
# Line 43 | Line 60 | public class ArrayBlockingQueueTest exte
60      }
61  
62      /**
63 <     * Constructor throws IAE if  capacity argument nonpositive
63 >     * Constructor throws IAE if capacity argument nonpositive
64       */
65      public void testConstructor2() {
66          try {
67              ArrayBlockingQueue q = new ArrayBlockingQueue(0);
68              shouldThrow();
69 <        }
53 <        catch (IllegalArgumentException success) {}
69 >        } catch (IllegalArgumentException success) {}
70      }
71  
72      /**
# Line 60 | Line 76 | public class ArrayBlockingQueueTest exte
76          try {
77              ArrayBlockingQueue q = new ArrayBlockingQueue(1, true, null);
78              shouldThrow();
79 <        }
64 <        catch (NullPointerException success) {}
79 >        } catch (NullPointerException success) {}
80      }
81  
82      /**
# Line 72 | Line 87 | public class ArrayBlockingQueueTest exte
87              Integer[] ints = new Integer[SIZE];
88              ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints));
89              shouldThrow();
90 <        }
76 <        catch (NullPointerException success) {}
90 >        } catch (NullPointerException success) {}
91      }
92  
93      /**
# Line 86 | Line 100 | public class ArrayBlockingQueueTest exte
100                  ints[i] = new Integer(i);
101              ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints));
102              shouldThrow();
103 <        }
90 <        catch (NullPointerException success) {}
103 >        } catch (NullPointerException success) {}
104      }
105  
106      /**
# Line 100 | Line 113 | public class ArrayBlockingQueueTest exte
113                  ints[i] = new Integer(i);
114              ArrayBlockingQueue q = new ArrayBlockingQueue(1, false, Arrays.asList(ints));
115              shouldThrow();
116 <        }
104 <        catch (IllegalArgumentException success) {}
116 >        } catch (IllegalArgumentException success) {}
117      }
118  
119      /**
120       * Queue contains all elements of collection used to initialize
121       */
122      public void testConstructor7() {
123 <        try {
124 <            Integer[] ints = new Integer[SIZE];
125 <            for (int i = 0; i < SIZE; ++i)
126 <                ints[i] = new Integer(i);
127 <            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, true, Arrays.asList(ints));
128 <            for (int i = 0; i < SIZE; ++i)
117 <                assertEquals(ints[i], q.poll());
118 <        }
119 <        finally {}
123 >        Integer[] ints = new Integer[SIZE];
124 >        for (int i = 0; i < SIZE; ++i)
125 >            ints[i] = new Integer(i);
126 >        ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, true, Arrays.asList(ints));
127 >        for (int i = 0; i < SIZE; ++i)
128 >            assertEquals(ints[i], q.poll());
129      }
130  
131      /**
# Line 152 | Line 161 | public class ArrayBlockingQueueTest exte
161      }
162  
163      /**
164 <     *  offer(null) throws NPE
164 >     * offer(null) throws NPE
165       */
166      public void testOfferNull() {
167 <        try {
167 >        try {
168              ArrayBlockingQueue q = new ArrayBlockingQueue(1);
169              q.offer(null);
170              shouldThrow();
171 <        } catch (NullPointerException success) { }  
171 >        } catch (NullPointerException success) {}
172      }
173  
174      /**
175 <     *  add(null) throws NPE
175 >     * add(null) throws NPE
176       */
177      public void testAddNull() {
178 <        try {
178 >        try {
179              ArrayBlockingQueue q = new ArrayBlockingQueue(1);
180              q.add(null);
181              shouldThrow();
182 <        } catch (NullPointerException success) { }  
182 >        } catch (NullPointerException success) {}
183      }
184  
185      /**
# Line 186 | Line 195 | public class ArrayBlockingQueueTest exte
195       * add succeeds if not full; throws ISE if full
196       */
197      public void testAdd() {
198 <        try {
198 >        try {
199              ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
200              for (int i = 0; i < SIZE; ++i) {
201                  assertTrue(q.add(new Integer(i)));
202              }
203              assertEquals(0, q.remainingCapacity());
204              q.add(new Integer(SIZE));
205 <        } catch (IllegalStateException success){
206 <        }  
205 >            shouldThrow();
206 >        } catch (IllegalStateException success) {}
207      }
208  
209      /**
210 <     *  addAll(null) throws NPE
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 <        }
209 <        catch (NullPointerException success) {}
217 >        } catch (NullPointerException success) {}
218      }
219  
220      /**
# Line 217 | Line 225 | public class ArrayBlockingQueueTest exte
225              ArrayBlockingQueue q = populatedQueue(SIZE);
226              q.addAll(q);
227              shouldThrow();
228 <        }
221 <        catch (IllegalArgumentException success) {}
228 >        } catch (IllegalArgumentException success) {}
229      }
230  
231  
232      /**
233 <     *  addAll of a collection with null elements throws NPE
233 >     * addAll of a collection with null elements throws NPE
234       */
235      public void testAddAll2() {
236          try {
# Line 231 | Line 238 | public class ArrayBlockingQueueTest exte
238              Integer[] ints = new Integer[SIZE];
239              q.addAll(Arrays.asList(ints));
240              shouldThrow();
241 <        }
235 <        catch (NullPointerException success) {}
241 >        } catch (NullPointerException success) {}
242      }
243 +
244      /**
245       * addAll of a collection with any null elements throws NPE after
246       * possibly adding some elements
# Line 246 | Line 253 | public class ArrayBlockingQueueTest exte
253                  ints[i] = new Integer(i);
254              q.addAll(Arrays.asList(ints));
255              shouldThrow();
256 <        }
250 <        catch (NullPointerException success) {}
256 >        } catch (NullPointerException success) {}
257      }
258 +
259      /**
260       * addAll throws ISE if not enough room
261       */
# Line 260 | Line 267 | public class ArrayBlockingQueueTest exte
267                  ints[i] = new Integer(i);
268              q.addAll(Arrays.asList(ints));
269              shouldThrow();
270 <        }
264 <        catch (IllegalStateException success) {}
270 >        } catch (IllegalStateException success) {}
271      }
272 +
273      /**
274       * Queue contains all elements, in traversal order, of successful addAll
275       */
276      public void testAddAll5() {
277 <        try {
278 <            Integer[] empty = new Integer[0];
279 <            Integer[] ints = new Integer[SIZE];
280 <            for (int i = 0; i < SIZE; ++i)
281 <                ints[i] = new Integer(i);
282 <            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
283 <            assertFalse(q.addAll(Arrays.asList(empty)));
284 <            assertTrue(q.addAll(Arrays.asList(ints)));
285 <            for (int i = 0; i < SIZE; ++i)
279 <                assertEquals(ints[i], q.poll());
280 <        }
281 <        finally {}
277 >        Integer[] empty = new Integer[0];
278 >        Integer[] ints = new Integer[SIZE];
279 >        for (int i = 0; i < SIZE; ++i)
280 >            ints[i] = new Integer(i);
281 >        ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
282 >        assertFalse(q.addAll(Arrays.asList(empty)));
283 >        assertTrue(q.addAll(Arrays.asList(ints)));
284 >        for (int i = 0; i < SIZE; ++i)
285 >            assertEquals(ints[i], q.poll());
286      }
287  
288      /**
289 <     *  put(null) throws NPE
289 >     * put(null) throws NPE
290       */
291 <     public void testPutNull() {
292 <        try {
291 >    public void testPutNull() throws InterruptedException {
292 >        try {
293              ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
294              q.put(null);
295              shouldThrow();
296 <        }
297 <        catch (NullPointerException success){
294 <        }  
295 <        catch (InterruptedException ie) {
296 <            unexpectedException();
297 <        }
298 <     }
296 >        } catch (NullPointerException success) {}
297 >    }
298  
299      /**
300       * all elements successfully put are contained
301       */
302 <     public void testPut() {
303 <         try {
304 <             ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
305 <             for (int i = 0; i < SIZE; ++i) {
306 <                 Integer I = new Integer(i);
307 <                 q.put(I);
309 <                 assertTrue(q.contains(I));
310 <             }
311 <             assertEquals(0, q.remainingCapacity());
312 <         }
313 <        catch (InterruptedException ie) {
314 <            unexpectedException();
302 >    public void testPut() throws InterruptedException {
303 >        ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
304 >        for (int i = 0; i < SIZE; ++i) {
305 >            Integer I = new Integer(i);
306 >            q.put(I);
307 >            assertTrue(q.contains(I));
308          }
309 +        assertEquals(0, q.remainingCapacity());
310      }
311  
312      /**
313       * put blocks interruptibly if full
314       */
315 <    public void testBlockingPut() {
316 <        Thread t = new Thread(new Runnable() {
317 <                public void run() {
318 <                    int added = 0;
319 <                    try {
320 <                        ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
321 <                        for (int i = 0; i < SIZE; ++i) {
322 <                            q.put(new Integer(i));
323 <                            ++added;
324 <                        }
325 <                        q.put(new Integer(SIZE));
326 <                        threadShouldThrow();
327 <                    } catch (InterruptedException ie){
328 <                        threadAssertEquals(added, SIZE);
329 <                    }  
330 <                }});
331 <        try {
332 <            t.start();
333 <           Thread.sleep(SHORT_DELAY_MS);
334 <           t.interrupt();
341 <           t.join();
342 <        }
343 <        catch (InterruptedException ie) {
344 <            unexpectedException();
345 <        }
315 >    public void testBlockingPut() throws InterruptedException {
316 >        final ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
317 >        Thread t = new Thread(new CheckedRunnable() {
318 >            public void realRun() throws InterruptedException {
319 >                for (int i = 0; i < SIZE; ++i)
320 >                    q.put(i);
321 >                assertEquals(SIZE, q.size());
322 >                assertEquals(0, q.remainingCapacity());
323 >                try {
324 >                    q.put(99);
325 >                    shouldThrow();
326 >                } catch (InterruptedException success) {}
327 >            }});
328 >
329 >        t.start();
330 >        delay(SHORT_DELAY_MS);
331 >        t.interrupt();
332 >        t.join();
333 >        assertEquals(SIZE, q.size());
334 >        assertEquals(0, q.remainingCapacity());
335      }
336  
337      /**
338       * put blocks waiting for take when full
339       */
340 <    public void testPutWithTake() {
341 <        final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
342 <        Thread t = new Thread(new Runnable() {
343 <                public void run() {
344 <                    int added = 0;
345 <                    try {
346 <                        q.put(new Object());
347 <                        ++added;
348 <                        q.put(new Object());
349 <                        ++added;
350 <                        q.put(new Object());
351 <                        ++added;
352 <                        q.put(new Object());
353 <                        ++added;
354 <                        threadShouldThrow();
355 <                    } catch (InterruptedException e){
356 <                        threadAssertTrue(added >= 2);
357 <                    }
358 <                }
359 <            });
360 <        try {
372 <            t.start();
373 <            Thread.sleep(SHORT_DELAY_MS);
374 <            q.take();
375 <            t.interrupt();
376 <            t.join();
377 <        } catch (Exception e){
378 <            unexpectedException();
379 <        }
340 >    public void testPutWithTake() throws InterruptedException {
341 >        final int capacity = 2;
342 >        final ArrayBlockingQueue q = new ArrayBlockingQueue(capacity);
343 >        Thread t = new Thread(new CheckedRunnable() {
344 >            public void realRun() throws InterruptedException {
345 >                for (int i = 0; i < capacity + 1; i++)
346 >                    q.put(i);
347 >                try {
348 >                    q.put(99);
349 >                    shouldThrow();
350 >                } catch (InterruptedException success) {}
351 >            }});
352 >
353 >        t.start();
354 >        delay(SHORT_DELAY_MS);
355 >        assertEquals(q.remainingCapacity(), 0);
356 >        assertEquals(0, q.take());
357 >        delay(SHORT_DELAY_MS);
358 >        t.interrupt();
359 >        t.join();
360 >        assertEquals(q.remainingCapacity(), 0);
361      }
362  
363      /**
364       * timed offer times out if full and elements not taken
365       */
366 <    public void testTimedOffer() {
366 >    public void testTimedOffer() throws InterruptedException {
367          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
368 <        Thread t = new Thread(new Runnable() {
369 <                public void run() {
370 <                    try {
371 <                        q.put(new Object());
372 <                        q.put(new Object());
373 <                        threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS));
374 <                        q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
375 <                        threadShouldThrow();
376 <                    } catch (InterruptedException success){}
377 <                }
378 <            });
379 <        
380 <        try {
381 <            t.start();
382 <            Thread.sleep(SHORT_DELAY_MS);
383 <            t.interrupt();
384 <            t.join();
385 <        } catch (Exception e){
405 <            unexpectedException();
406 <        }
368 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
369 >        Thread t = newStartedThread(new CheckedRunnable() {
370 >            public void realRun() throws InterruptedException {
371 >                q.put(new Object());
372 >                q.put(new Object());
373 >                long startTime = System.nanoTime();
374 >                assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS));
375 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
376 >                pleaseInterrupt.countDown();
377 >                try {
378 >                    q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
379 >                    shouldThrow();
380 >                } catch (InterruptedException success) {}
381 >            }});
382 >
383 >        await(pleaseInterrupt);
384 >        t.interrupt();
385 >        awaitTermination(t);
386      }
387  
388      /**
389       * take retrieves elements in FIFO order
390       */
391 <    public void testTake() {
392 <        try {
393 <            ArrayBlockingQueue q = populatedQueue(SIZE);
394 <            for (int i = 0; i < SIZE; ++i) {
416 <                assertEquals(i, ((Integer)q.take()).intValue());
417 <            }
418 <        } catch (InterruptedException e){
419 <            unexpectedException();
420 <        }  
421 <    }
422 <
423 <    /**
424 <     * take blocks interruptibly when empty
425 <     */
426 <    public void testTakeFromEmpty() {
427 <        final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
428 <        Thread t = new Thread(new Runnable() {
429 <                public void run() {
430 <                    try {
431 <                        q.take();
432 <                        threadShouldThrow();
433 <                    } catch (InterruptedException success){ }                
434 <                }
435 <            });
436 <        try {
437 <            t.start();
438 <            Thread.sleep(SHORT_DELAY_MS);
439 <            t.interrupt();
440 <            t.join();
441 <        } catch (Exception e){
442 <            unexpectedException();
391 >    public void testTake() throws InterruptedException {
392 >        ArrayBlockingQueue q = populatedQueue(SIZE);
393 >        for (int i = 0; i < SIZE; ++i) {
394 >            assertEquals(i, q.take());
395          }
396      }
397  
398      /**
399       * Take removes existing elements until empty, then blocks interruptibly
400       */
401 <    public void testBlockingTake() {
402 <        Thread t = new Thread(new Runnable() {
403 <                public void run() {
404 <                    try {
405 <                        ArrayBlockingQueue q = populatedQueue(SIZE);
406 <                        for (int i = 0; i < SIZE; ++i) {
407 <                            threadAssertEquals(i, ((Integer)q.take()).intValue());
408 <                        }
409 <                        q.take();
410 <                        threadShouldThrow();
411 <                    } catch (InterruptedException success){
412 <                    }  
413 <                }});
414 <        try {
415 <            t.start();
416 <            Thread.sleep(SHORT_DELAY_MS);
417 <            t.interrupt();
466 <            t.join();
467 <        }
468 <        catch (InterruptedException ie) {
469 <            unexpectedException();
470 <        }
401 >    public void testBlockingTake() throws InterruptedException {
402 >        final ArrayBlockingQueue q = populatedQueue(SIZE);
403 >        Thread t = new Thread(new CheckedRunnable() {
404 >            public void realRun() throws InterruptedException {
405 >                for (int i = 0; i < SIZE; ++i) {
406 >                    assertEquals(i, q.take());
407 >                }
408 >                try {
409 >                    q.take();
410 >                    shouldThrow();
411 >                } catch (InterruptedException success) {}
412 >            }});
413 >
414 >        t.start();
415 >        delay(SHORT_DELAY_MS);
416 >        t.interrupt();
417 >        t.join();
418      }
419  
420  
# Line 477 | Line 424 | public class ArrayBlockingQueueTest exte
424      public void testPoll() {
425          ArrayBlockingQueue q = populatedQueue(SIZE);
426          for (int i = 0; i < SIZE; ++i) {
427 <            assertEquals(i, ((Integer)q.poll()).intValue());
427 >            assertEquals(i, q.poll());
428          }
429 <        assertNull(q.poll());
429 >        assertNull(q.poll());
430      }
431  
432      /**
433 <     * timed pool with zero timeout succeeds when non-empty, else times out
433 >     * timed poll with zero timeout succeeds when non-empty, else times out
434       */
435 <    public void testTimedPoll0() {
436 <        try {
437 <            ArrayBlockingQueue q = populatedQueue(SIZE);
438 <            for (int i = 0; i < SIZE; ++i) {
439 <                assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue());
440 <            }
494 <            assertNull(q.poll(0, TimeUnit.MILLISECONDS));
495 <        } catch (InterruptedException e){
496 <            unexpectedException();
497 <        }  
435 >    public void testTimedPoll0() throws InterruptedException {
436 >        ArrayBlockingQueue q = populatedQueue(SIZE);
437 >        for (int i = 0; i < SIZE; ++i) {
438 >            assertEquals(i, q.poll(0, MILLISECONDS));
439 >        }
440 >        assertNull(q.poll(0, MILLISECONDS));
441      }
442  
443      /**
444 <     * timed pool with nonzero timeout succeeds when non-empty, else times out
444 >     * timed poll with nonzero timeout succeeds when non-empty, else times out
445       */
446 <    public void testTimedPoll() {
447 <        try {
448 <            ArrayBlockingQueue q = populatedQueue(SIZE);
449 <            for (int i = 0; i < SIZE; ++i) {
450 <                assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
451 <            }
509 <            assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
510 <        } catch (InterruptedException e){
511 <            unexpectedException();
512 <        }  
446 >    public void testTimedPoll() throws InterruptedException {
447 >        ArrayBlockingQueue q = populatedQueue(SIZE);
448 >        for (int i = 0; i < SIZE; ++i) {
449 >            assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
450 >        }
451 >        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
452      }
453  
454      /**
455       * Interrupted timed poll throws InterruptedException instead of
456       * returning timeout status
457       */
458 <    public void testInterruptedTimedPoll() {
459 <        Thread t = new Thread(new Runnable() {
460 <                public void run() {
461 <                    try {
462 <                        ArrayBlockingQueue q = populatedQueue(SIZE);
463 <                        for (int i = 0; i < SIZE; ++i) {
464 <                            threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
465 <                        }
466 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
528 <                    } catch (InterruptedException success){
529 <                    }  
530 <                }});
531 <        try {
532 <            t.start();
533 <            Thread.sleep(SHORT_DELAY_MS);
534 <            t.interrupt();
535 <            t.join();
536 <        }
537 <        catch (InterruptedException ie) {
538 <            unexpectedException();
539 <        }
540 <    }
541 <
542 <    /**
543 <     *  timed poll before a delayed offer fails; after offer succeeds;
544 <     *  on interruption throws
545 <     */
546 <    public void testTimedPollWithOffer() {
547 <        final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
548 <        Thread t = new Thread(new Runnable() {
549 <                public void run() {
550 <                    try {
551 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
552 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
553 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
554 <                        threadShouldThrow();
555 <                    } catch (InterruptedException success) { }                
458 >    public void testInterruptedTimedPoll() throws InterruptedException {
459 >        final BlockingQueue<Integer> q = populatedQueue(SIZE);
460 >        final CountDownLatch aboutToWait = new CountDownLatch(1);
461 >        Thread t = newStartedThread(new CheckedRunnable() {
462 >            public void realRun() throws InterruptedException {
463 >                for (int i = 0; i < SIZE; ++i) {
464 >                    long t0 = System.nanoTime();
465 >                    assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
466 >                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
467                  }
468 <            });
469 <        try {
470 <            t.start();
471 <            Thread.sleep(SMALL_DELAY_MS);
472 <            assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
473 <            t.interrupt();
474 <            t.join();
475 <        } catch (Exception e){
476 <            unexpectedException();
477 <        }
478 <    }  
479 <
468 >                long t0 = System.nanoTime();
469 >                aboutToWait.countDown();
470 >                try {
471 >                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
472 >                    shouldThrow();
473 >                } catch (InterruptedException success) {
474 >                    assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
475 >                }
476 >            }});
477 >
478 >        aboutToWait.await();
479 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
480 >        t.interrupt();
481 >        awaitTermination(t, MEDIUM_DELAY_MS);
482 >        checkEmpty(q);
483 >    }
484  
485      /**
486       * peek returns next element, or null if empty
# Line 573 | Line 488 | public class ArrayBlockingQueueTest exte
488      public void testPeek() {
489          ArrayBlockingQueue q = populatedQueue(SIZE);
490          for (int i = 0; i < SIZE; ++i) {
491 <            assertEquals(i, ((Integer)q.peek()).intValue());
492 <            q.poll();
491 >            assertEquals(i, q.peek());
492 >            assertEquals(i, q.poll());
493              assertTrue(q.peek() == null ||
494 <                       i != ((Integer)q.peek()).intValue());
494 >                       !q.peek().equals(i));
495          }
496 <        assertNull(q.peek());
496 >        assertNull(q.peek());
497      }
498  
499      /**
# Line 587 | Line 502 | public class ArrayBlockingQueueTest exte
502      public void testElement() {
503          ArrayBlockingQueue q = populatedQueue(SIZE);
504          for (int i = 0; i < SIZE; ++i) {
505 <            assertEquals(i, ((Integer)q.element()).intValue());
506 <            q.poll();
505 >            assertEquals(i, q.element());
506 >            assertEquals(i, q.poll());
507          }
508          try {
509              q.element();
510              shouldThrow();
511 <        }
597 <        catch (NoSuchElementException success) {}
511 >        } catch (NoSuchElementException success) {}
512      }
513  
514      /**
# Line 603 | Line 517 | public class ArrayBlockingQueueTest exte
517      public void testRemove() {
518          ArrayBlockingQueue q = populatedQueue(SIZE);
519          for (int i = 0; i < SIZE; ++i) {
520 <            assertEquals(i, ((Integer)q.remove()).intValue());
520 >            assertEquals(i, q.remove());
521          }
522          try {
523              q.remove();
524              shouldThrow();
525 <        } catch (NoSuchElementException success){
612 <        }  
525 >        } catch (NoSuchElementException success) {}
526      }
527  
528      /**
# Line 626 | Line 539 | public class ArrayBlockingQueueTest exte
539          }
540          assertTrue(q.isEmpty());
541      }
542 <        
542 >
543      /**
544       * contains(x) reports true when elements added but not yet removed
545       */
# Line 634 | Line 547 | public class ArrayBlockingQueueTest exte
547          ArrayBlockingQueue q = populatedQueue(SIZE);
548          for (int i = 0; i < SIZE; ++i) {
549              assertTrue(q.contains(new Integer(i)));
550 <            q.poll();
550 >            assertEquals(i, q.poll());
551              assertFalse(q.contains(new Integer(i)));
552          }
553      }
# Line 650 | Line 563 | public class ArrayBlockingQueueTest exte
563          assertEquals(SIZE, q.remainingCapacity());
564          q.add(one);
565          assertFalse(q.isEmpty());
566 +        assertTrue(q.contains(one));
567          q.clear();
568          assertTrue(q.isEmpty());
569      }
# Line 704 | Line 618 | public class ArrayBlockingQueueTest exte
618      }
619  
620      /**
621 <     *  toArray contains all elements
621 >     * toArray contains all elements in FIFO order
622       */
623      public void testToArray() {
624          ArrayBlockingQueue q = populatedQueue(SIZE);
625 <        Object[] o = q.toArray();
626 <        try {
627 <        for(int i = 0; i < o.length; i++)
714 <            assertEquals(o[i], q.take());
715 <        } catch (InterruptedException e){
716 <            unexpectedException();
717 <        }    
625 >        Object[] o = q.toArray();
626 >        for (int i = 0; i < o.length; i++)
627 >            assertSame(o[i], q.poll());
628      }
629  
630      /**
631 <     * toArray(a) contains all elements
631 >     * toArray(a) contains all elements in FIFO order
632       */
633      public void testToArray2() {
634 <        ArrayBlockingQueue q = populatedQueue(SIZE);
635 <        Integer[] ints = new Integer[SIZE];
636 <        ints = (Integer[])q.toArray(ints);
637 <        try {
638 <            for(int i = 0; i < ints.length; i++)
639 <                assertEquals(ints[i], q.take());
730 <        } catch (InterruptedException e){
731 <            unexpectedException();
732 <        }    
634 >        ArrayBlockingQueue<Integer> q = populatedQueue(SIZE);
635 >        Integer[] ints = new Integer[SIZE];
636 >        Integer[] array = q.toArray(ints);
637 >        assertSame(ints, array);
638 >        for (int i = 0; i < ints.length; i++)
639 >            assertSame(ints[i], q.poll());
640      }
641  
642      /**
643 <     * toArray(null) throws NPE
643 >     * toArray(null) throws NullPointerException
644       */
645 <    public void testToArray_BadArg() {
646 <        try {
647 <            ArrayBlockingQueue q = populatedQueue(SIZE);
648 <            Object o[] = q.toArray(null);
649 <            shouldThrow();
650 <        } catch(NullPointerException success){}
645 >    public void testToArray_NullArg() {
646 >        ArrayBlockingQueue q = populatedQueue(SIZE);
647 >        try {
648 >            q.toArray(null);
649 >            shouldThrow();
650 >        } catch (NullPointerException success) {}
651      }
652  
653      /**
654 <     * toArray with incompatible array type throws CCE
654 >     * toArray(incompatible array type) throws ArrayStoreException
655       */
656      public void testToArray1_BadArg() {
657 <        try {
658 <            ArrayBlockingQueue q = populatedQueue(SIZE);
659 <            Object o[] = q.toArray(new String[10] );
660 <            shouldThrow();
661 <        } catch(ArrayStoreException  success){}
657 >        ArrayBlockingQueue q = populatedQueue(SIZE);
658 >        try {
659 >            q.toArray(new String[10]);
660 >            shouldThrow();
661 >        } catch (ArrayStoreException success) {}
662      }
663  
664 <    
664 >
665      /**
666       * iterator iterates through all elements
667       */
668 <    public void testIterator() {
668 >    public void testIterator() throws InterruptedException {
669          ArrayBlockingQueue q = populatedQueue(SIZE);
670 <        Iterator it = q.iterator();
671 <        try {
672 <            while(it.hasNext()){
673 <                assertEquals(it.next(), q.take());
767 <            }
768 <        } catch (InterruptedException e){
769 <            unexpectedException();
770 <        }    
670 >        Iterator it = q.iterator();
671 >        while (it.hasNext()) {
672 >            assertEquals(it.next(), q.take());
673 >        }
674      }
675  
676      /**
677       * iterator.remove removes current element
678       */
679 <    public void testIteratorRemove () {
679 >    public void testIteratorRemove() {
680          final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
681          q.add(two);
682          q.add(one);
# Line 782 | Line 685 | public class ArrayBlockingQueueTest exte
685          Iterator it = q.iterator();
686          it.next();
687          it.remove();
688 <        
688 >
689          it = q.iterator();
690 <        assertEquals(it.next(), one);
691 <        assertEquals(it.next(), three);
690 >        assertSame(it.next(), one);
691 >        assertSame(it.next(), three);
692          assertFalse(it.hasNext());
693      }
694  
# Line 802 | Line 705 | public class ArrayBlockingQueueTest exte
705  
706          int k = 0;
707          for (Iterator it = q.iterator(); it.hasNext();) {
708 <            int i = ((Integer)(it.next())).intValue();
806 <            assertEquals(++k, i);
708 >            assertEquals(++k, it.next());
709          }
710          assertEquals(3, k);
711      }
# Line 811 | Line 713 | public class ArrayBlockingQueueTest exte
713      /**
714       * Modifications do not cause iterators to fail
715       */
716 <    public void testWeaklyConsistentIteration () {
716 >    public void testWeaklyConsistentIteration() {
717          final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
718          q.add(one);
719          q.add(two);
720          q.add(three);
721 <        try {
722 <            for (Iterator it = q.iterator(); it.hasNext();) {
723 <                q.remove();
822 <                it.next();
823 <            }
824 <        }
825 <        catch (ConcurrentModificationException e) {
826 <            unexpectedException();
721 >        for (Iterator it = q.iterator(); it.hasNext();) {
722 >            q.remove();
723 >            it.next();
724          }
725          assertEquals(0, q.size());
726      }
# Line 838 | Line 735 | public class ArrayBlockingQueueTest exte
735          for (int i = 0; i < SIZE; ++i) {
736              assertTrue(s.indexOf(String.valueOf(i)) >= 0);
737          }
738 <    }        
738 >    }
739  
740  
741      /**
# Line 849 | Line 746 | public class ArrayBlockingQueueTest exte
746          q.add(one);
747          q.add(two);
748          ExecutorService executor = Executors.newFixedThreadPool(2);
749 <        executor.execute(new Runnable() {
750 <            public void run() {
751 <                threadAssertFalse(q.offer(three));
752 <                try {
753 <                    threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
754 <                    threadAssertEquals(0, q.remainingCapacity());
755 <                }
756 <                catch (InterruptedException e) {
757 <                    threadUnexpectedException();
758 <                }
759 <            }
760 <        });
749 >        executor.execute(new CheckedRunnable() {
750 >            public void realRun() throws InterruptedException {
751 >                assertFalse(q.offer(three));
752 >                assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
753 >                assertEquals(0, q.remainingCapacity());
754 >            }});
755 >
756 >        executor.execute(new CheckedRunnable() {
757 >            public void realRun() throws InterruptedException {
758 >                delay(SMALL_DELAY_MS);
759 >                assertSame(one, q.take());
760 >            }});
761  
865        executor.execute(new Runnable() {
866            public void run() {
867                try {
868                    Thread.sleep(SMALL_DELAY_MS);
869                    threadAssertEquals(one, q.take());
870                }
871                catch (InterruptedException e) {
872                    threadUnexpectedException();
873                }
874            }
875        });
876        
762          joinPool(executor);
878
763      }
764  
765      /**
# Line 884 | Line 768 | public class ArrayBlockingQueueTest exte
768      public void testPollInExecutor() {
769          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
770          ExecutorService executor = Executors.newFixedThreadPool(2);
771 <        executor.execute(new Runnable() {
772 <            public void run() {
773 <                threadAssertNull(q.poll());
774 <                try {
775 <                    threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
776 <                    threadAssertTrue(q.isEmpty());
777 <                }
778 <                catch (InterruptedException e) {
779 <                    threadUnexpectedException();
780 <                }
781 <            }
782 <        });
771 >        executor.execute(new CheckedRunnable() {
772 >            public void realRun() throws InterruptedException {
773 >                assertNull(q.poll());
774 >                assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
775 >                assertTrue(q.isEmpty());
776 >            }});
777 >
778 >        executor.execute(new CheckedRunnable() {
779 >            public void realRun() throws InterruptedException {
780 >                delay(SMALL_DELAY_MS);
781 >                q.put(one);
782 >            }});
783  
900        executor.execute(new Runnable() {
901            public void run() {
902                try {
903                    Thread.sleep(SMALL_DELAY_MS);
904                    q.put(one);
905                }
906                catch (InterruptedException e) {
907                    threadUnexpectedException();
908                }
909            }
910        });
911        
784          joinPool(executor);
785      }
786  
787      /**
788       * A deserialized serialized queue has same elements in same order
789       */
790 <    public void testSerialization() {
790 >    public void testSerialization() throws Exception {
791          ArrayBlockingQueue q = populatedQueue(SIZE);
792  
793 <        try {
794 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
795 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
796 <            out.writeObject(q);
797 <            out.close();
798 <
799 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
800 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
801 <            ArrayBlockingQueue r = (ArrayBlockingQueue)in.readObject();
802 <            assertEquals(q.size(), r.size());
803 <            while (!q.isEmpty())
932 <                assertEquals(q.remove(), r.remove());
933 <        } catch(Exception e){
934 <            unexpectedException();
935 <        }
793 >        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
794 >        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
795 >        out.writeObject(q);
796 >        out.close();
797 >
798 >        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
799 >        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
800 >        ArrayBlockingQueue r = (ArrayBlockingQueue)in.readObject();
801 >        assertEquals(q.size(), r.size());
802 >        while (!q.isEmpty())
803 >            assertEquals(q.remove(), r.remove());
804      }
805  
806      /**
807       * drainTo(null) throws NPE
808 <     */
808 >     */
809      public void testDrainToNull() {
810          ArrayBlockingQueue q = populatedQueue(SIZE);
811          try {
812              q.drainTo(null);
813              shouldThrow();
814 <        } catch(NullPointerException success) {
947 <        }
814 >        } catch (NullPointerException success) {}
815      }
816  
817      /**
818       * drainTo(this) throws IAE
819 <     */
819 >     */
820      public void testDrainToSelf() {
821          ArrayBlockingQueue q = populatedQueue(SIZE);
822          try {
823              q.drainTo(q);
824              shouldThrow();
825 <        } catch(IllegalArgumentException success) {
959 <        }
825 >        } catch (IllegalArgumentException success) {}
826      }
827  
828      /**
829       * drainTo(c) empties queue into another collection c
830 <     */
830 >     */
831      public void testDrainTo() {
832          ArrayBlockingQueue q = populatedQueue(SIZE);
833          ArrayList l = new ArrayList();
834          q.drainTo(l);
835          assertEquals(q.size(), 0);
836          assertEquals(l.size(), SIZE);
837 <        for (int i = 0; i < SIZE; ++i)
837 >        for (int i = 0; i < SIZE; ++i)
838 >            assertEquals(l.get(i), new Integer(i));
839 >        q.add(zero);
840 >        q.add(one);
841 >        assertFalse(q.isEmpty());
842 >        assertTrue(q.contains(zero));
843 >        assertTrue(q.contains(one));
844 >        l.clear();
845 >        q.drainTo(l);
846 >        assertEquals(q.size(), 0);
847 >        assertEquals(l.size(), 2);
848 >        for (int i = 0; i < 2; ++i)
849              assertEquals(l.get(i), new Integer(i));
850      }
851  
852      /**
853       * drainTo empties full queue, unblocking a waiting put.
854 <     */
855 <    public void testDrainToWithActivePut() {
854 >     */
855 >    public void testDrainToWithActivePut() throws InterruptedException {
856          final ArrayBlockingQueue q = populatedQueue(SIZE);
857 <        Thread t = new Thread(new Runnable() {
858 <                public void run() {
859 <                    try {
860 <                        q.put(new Integer(SIZE+1));
861 <                    } catch (InterruptedException ie){
862 <                        threadUnexpectedException();
863 <                    }
864 <                }
865 <            });
866 <        try {
867 <            t.start();
868 <            ArrayList l = new ArrayList();
869 <            q.drainTo(l);
993 <            assertTrue(l.size() >= SIZE);
994 <            for (int i = 0; i < SIZE; ++i)
995 <                assertEquals(l.get(i), new Integer(i));
996 <            t.join();
997 <            assertTrue(q.size() + l.size() == SIZE+1);
998 <        } catch(Exception e){
999 <            unexpectedException();
1000 <        }
857 >        Thread t = new Thread(new CheckedRunnable() {
858 >            public void realRun() throws InterruptedException {
859 >                q.put(new Integer(SIZE+1));
860 >            }});
861 >
862 >        t.start();
863 >        ArrayList l = new ArrayList();
864 >        q.drainTo(l);
865 >        assertTrue(l.size() >= SIZE);
866 >        for (int i = 0; i < SIZE; ++i)
867 >            assertEquals(l.get(i), new Integer(i));
868 >        t.join();
869 >        assertTrue(q.size() + l.size() >= SIZE);
870      }
871  
872      /**
873       * drainTo(null, n) throws NPE
874 <     */
874 >     */
875      public void testDrainToNullN() {
876          ArrayBlockingQueue q = populatedQueue(SIZE);
877          try {
878              q.drainTo(null, 0);
879              shouldThrow();
880 <        } catch(NullPointerException success) {
1012 <        }
880 >        } catch (NullPointerException success) {}
881      }
882  
883      /**
884       * drainTo(this, n) throws IAE
885 <     */
885 >     */
886      public void testDrainToSelfN() {
887          ArrayBlockingQueue q = populatedQueue(SIZE);
888          try {
889              q.drainTo(q, 0);
890              shouldThrow();
891 <        } catch(IllegalArgumentException success) {
1024 <        }
891 >        } catch (IllegalArgumentException success) {}
892      }
893  
894      /**
895 <     * drainTo(c, n) empties first max {n, size} elements of queue into c
896 <     */
895 >     * drainTo(c, n) empties first min(n, size) elements of queue into c
896 >     */
897      public void testDrainToN() {
898 +        ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE*2);
899          for (int i = 0; i < SIZE + 2; ++i) {
900 <            ArrayBlockingQueue q = populatedQueue(SIZE);
900 >            for (int j = 0; j < SIZE; j++)
901 >                assertTrue(q.offer(new Integer(j)));
902              ArrayList l = new ArrayList();
903              q.drainTo(l, i);
904 <            int k = (i < SIZE)? i : SIZE;
1036 <            assertEquals(q.size(), SIZE-k);
904 >            int k = (i < SIZE) ? i : SIZE;
905              assertEquals(l.size(), k);
906 <            for (int j = 0; j < k; ++j)
906 >            assertEquals(q.size(), SIZE-k);
907 >            for (int j = 0; j < k; ++j)
908                  assertEquals(l.get(j), new Integer(j));
909 +            while (q.poll() != null) ;
910          }
911      }
912  
1043
913   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines