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.14 by jsr166, Mon Nov 16 05:30:07 2009 UTC vs.
Revision 1.32 by jsr166, Wed Oct 6 07:49:22 2010 UTC

# Line 10 | Line 10
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      /**
# Line 27 | Line 44 | public class ArrayBlockingQueueTest exte
44      private ArrayBlockingQueue populatedQueue(int n) {
45          ArrayBlockingQueue q = new ArrayBlockingQueue(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  
# 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 155 | Line 164 | public class ArrayBlockingQueueTest exte
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
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      /**
# Line 205 | Line 214 | public class ArrayBlockingQueueTest exte
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  
# 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
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 <        }
293 <        catch (NullPointerException success) {
294 <        }
295 <        catch (InterruptedException ie) {
296 <            unexpectedException();
297 <        }
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() {
315 >    public void testBlockingPut() throws InterruptedException {
316          final ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
317 <        Thread t = new Thread(new Runnable() {
318 <                public void run() {
319 <                    int added = 0;
320 <                    try {
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(MEDIUM_DELAY_MS);
334 <           t.interrupt();
341 <           t.join();
342 <        }
343 <        catch (InterruptedException ie) {
344 <            unexpectedException();
345 <        }
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 >        Thread.sleep(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 >        Thread.sleep(SHORT_DELAY_MS);
355 >        assertEquals(q.remainingCapacity(), 0);
356 >        assertEquals(0, q.take());
357 >        Thread.sleep(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);
402 <            t.interrupt();
403 <            t.join();
404 <        } catch (Exception e) {
405 <            unexpectedException();
406 <        }
368 >        Thread t = new Thread(new CheckedRunnable() {
369 >            public void realRun() throws InterruptedException {
370 >                q.put(new Object());
371 >                q.put(new Object());
372 >                assertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, MILLISECONDS));
373 >                try {
374 >                    q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
375 >                    shouldThrow();
376 >                } catch (InterruptedException success) {}
377 >            }});
378 >
379 >        t.start();
380 >        Thread.sleep(SHORT_DELAY_MS);
381 >        t.interrupt();
382 >        t.join();
383      }
384  
385      /**
386       * take retrieves elements in FIFO order
387       */
388 <    public void testTake() {
389 <        try {
390 <            ArrayBlockingQueue q = populatedQueue(SIZE);
391 <            for (int i = 0; i < SIZE; ++i) {
392 <                assertEquals(i, ((Integer)q.take()).intValue());
417 <            }
418 <        } catch (InterruptedException e) {
419 <            unexpectedException();
420 <        }
388 >    public void testTake() throws InterruptedException {
389 >        ArrayBlockingQueue q = populatedQueue(SIZE);
390 >        for (int i = 0; i < SIZE; ++i) {
391 >            assertEquals(i, q.take());
392 >        }
393      }
394  
395      /**
396       * take blocks interruptibly when empty
397       */
398 <    public void testTakeFromEmpty() {
398 >    public void testTakeFromEmpty() throws InterruptedException {
399          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
400 <        Thread t = new Thread(new Runnable() {
401 <                public void run() {
402 <                    try {
403 <                        q.take();
404 <                        threadShouldThrow();
405 <                    } catch (InterruptedException success) { }
406 <                }
407 <            });
408 <        try {
437 <            t.start();
438 <            Thread.sleep(SHORT_DELAY_MS);
439 <            t.interrupt();
440 <            t.join();
441 <        } catch (Exception e) {
442 <            unexpectedException();
443 <        }
400 >        Thread t = new ThreadShouldThrow(InterruptedException.class) {
401 >            public void realRun() throws InterruptedException {
402 >                q.take();
403 >            }};
404 >
405 >        t.start();
406 >        Thread.sleep(SHORT_DELAY_MS);
407 >        t.interrupt();
408 >        t.join();
409      }
410  
411      /**
412       * Take removes existing elements until empty, then blocks interruptibly
413       */
414 <    public void testBlockingTake() {
415 <        Thread t = new Thread(new Runnable() {
416 <                public void run() {
417 <                    try {
418 <                        ArrayBlockingQueue q = populatedQueue(SIZE);
419 <                        for (int i = 0; i < SIZE; ++i) {
420 <                            threadAssertEquals(i, ((Integer)q.take()).intValue());
421 <                        }
422 <                        q.take();
423 <                        threadShouldThrow();
424 <                    } catch (InterruptedException success) {
425 <                    }
426 <                }});
427 <        try {
428 <            t.start();
429 <            Thread.sleep(SHORT_DELAY_MS);
430 <            t.interrupt();
466 <            t.join();
467 <        }
468 <        catch (InterruptedException ie) {
469 <            unexpectedException();
470 <        }
414 >    public void testBlockingTake() throws InterruptedException {
415 >        final ArrayBlockingQueue q = populatedQueue(SIZE);
416 >        Thread t = new Thread(new CheckedRunnable() {
417 >            public void realRun() throws InterruptedException {
418 >                for (int i = 0; i < SIZE; ++i) {
419 >                    assertEquals(i, q.take());
420 >                }
421 >                try {
422 >                    q.take();
423 >                    shouldThrow();
424 >                } catch (InterruptedException success) {}
425 >            }});
426 >
427 >        t.start();
428 >        Thread.sleep(SHORT_DELAY_MS);
429 >        t.interrupt();
430 >        t.join();
431      }
432  
433  
# Line 477 | Line 437 | public class ArrayBlockingQueueTest exte
437      public void testPoll() {
438          ArrayBlockingQueue q = populatedQueue(SIZE);
439          for (int i = 0; i < SIZE; ++i) {
440 <            assertEquals(i, ((Integer)q.poll()).intValue());
440 >            assertEquals(i, q.poll());
441          }
442 <        assertNull(q.poll());
442 >        assertNull(q.poll());
443      }
444  
445      /**
446       * timed pool with zero timeout succeeds when non-empty, else times out
447       */
448 <    public void testTimedPoll0() {
449 <        try {
450 <            ArrayBlockingQueue q = populatedQueue(SIZE);
451 <            for (int i = 0; i < SIZE; ++i) {
452 <                assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue());
453 <            }
494 <            assertNull(q.poll(0, TimeUnit.MILLISECONDS));
495 <        } catch (InterruptedException e) {
496 <            unexpectedException();
497 <        }
448 >    public void testTimedPoll0() throws InterruptedException {
449 >        ArrayBlockingQueue q = populatedQueue(SIZE);
450 >        for (int i = 0; i < SIZE; ++i) {
451 >            assertEquals(i, q.poll(0, MILLISECONDS));
452 >        }
453 >        assertNull(q.poll(0, MILLISECONDS));
454      }
455  
456      /**
457       * timed pool with nonzero timeout succeeds when non-empty, else times out
458       */
459 <    public void testTimedPoll() {
460 <        try {
461 <            ArrayBlockingQueue q = populatedQueue(SIZE);
462 <            for (int i = 0; i < SIZE; ++i) {
463 <                assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
464 <            }
509 <            assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
510 <        } catch (InterruptedException e) {
511 <            unexpectedException();
512 <        }
459 >    public void testTimedPoll() throws InterruptedException {
460 >        ArrayBlockingQueue q = populatedQueue(SIZE);
461 >        for (int i = 0; i < SIZE; ++i) {
462 >            assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
463 >        }
464 >        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
465      }
466  
467      /**
468       * Interrupted timed poll throws InterruptedException instead of
469       * returning timeout status
470       */
471 <    public void testInterruptedTimedPoll() {
472 <        Thread t = new Thread(new Runnable() {
473 <                public void run() {
474 <                    try {
475 <                        ArrayBlockingQueue q = populatedQueue(SIZE);
476 <                        for (int i = 0; i < SIZE; ++i) {
525 <                            threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
526 <                        }
527 <                        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) { }
471 >    public void testInterruptedTimedPoll() throws InterruptedException {
472 >        Thread t = new Thread(new CheckedRunnable() {
473 >            public void realRun() throws InterruptedException {
474 >                ArrayBlockingQueue q = populatedQueue(SIZE);
475 >                for (int i = 0; i < SIZE; ++i) {
476 >                    assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));;
477                  }
478 <            });
479 <        try {
480 <            t.start();
481 <            Thread.sleep(SMALL_DELAY_MS);
482 <            assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
483 <            t.interrupt();
484 <            t.join();
485 <        } catch (Exception e) {
486 <            unexpectedException();
487 <        }
478 >                try {
479 >                    q.poll(SMALL_DELAY_MS, MILLISECONDS);
480 >                    shouldThrow();
481 >                } catch (InterruptedException success) {}
482 >            }});
483 >
484 >        t.start();
485 >        Thread.sleep(SHORT_DELAY_MS);
486 >        t.interrupt();
487 >        t.join();
488      }
489  
569
490      /**
491       * peek returns next element, or null if empty
492       */
493      public void testPeek() {
494          ArrayBlockingQueue q = populatedQueue(SIZE);
495          for (int i = 0; i < SIZE; ++i) {
496 <            assertEquals(i, ((Integer)q.peek()).intValue());
497 <            q.poll();
496 >            assertEquals(i, q.peek());
497 >            assertEquals(i, q.poll());
498              assertTrue(q.peek() == null ||
499 <                       i != ((Integer)q.peek()).intValue());
499 >                       !q.peek().equals(i));
500          }
501 <        assertNull(q.peek());
501 >        assertNull(q.peek());
502      }
503  
504      /**
# Line 587 | Line 507 | public class ArrayBlockingQueueTest exte
507      public void testElement() {
508          ArrayBlockingQueue q = populatedQueue(SIZE);
509          for (int i = 0; i < SIZE; ++i) {
510 <            assertEquals(i, ((Integer)q.element()).intValue());
511 <            q.poll();
510 >            assertEquals(i, q.element());
511 >            assertEquals(i, q.poll());
512          }
513          try {
514              q.element();
515              shouldThrow();
516 <        }
597 <        catch (NoSuchElementException success) {}
516 >        } catch (NoSuchElementException success) {}
517      }
518  
519      /**
# Line 603 | Line 522 | public class ArrayBlockingQueueTest exte
522      public void testRemove() {
523          ArrayBlockingQueue q = populatedQueue(SIZE);
524          for (int i = 0; i < SIZE; ++i) {
525 <            assertEquals(i, ((Integer)q.remove()).intValue());
525 >            assertEquals(i, q.remove());
526          }
527          try {
528              q.remove();
529              shouldThrow();
530 <        } catch (NoSuchElementException success) {
612 <        }
530 >        } catch (NoSuchElementException success) {}
531      }
532  
533      /**
# Line 634 | Line 552 | public class ArrayBlockingQueueTest exte
552          ArrayBlockingQueue q = populatedQueue(SIZE);
553          for (int i = 0; i < SIZE; ++i) {
554              assertTrue(q.contains(new Integer(i)));
555 <            q.poll();
555 >            assertEquals(i, q.poll());
556              assertFalse(q.contains(new Integer(i)));
557          }
558      }
# Line 707 | Line 625 | public class ArrayBlockingQueueTest exte
625      /**
626       *  toArray contains all elements
627       */
628 <    public void testToArray() {
628 >    public void testToArray() throws InterruptedException {
629          ArrayBlockingQueue q = populatedQueue(SIZE);
630 <        Object[] o = q.toArray();
631 <        try {
632 <        for (int i = 0; i < o.length; i++)
715 <            assertEquals(o[i], q.take());
716 <        } catch (InterruptedException e) {
717 <            unexpectedException();
718 <        }
630 >        Object[] o = q.toArray();
631 >        for (int i = 0; i < o.length; i++)
632 >            assertEquals(o[i], q.take());
633      }
634  
635      /**
636       * toArray(a) contains all elements
637       */
638 <    public void testToArray2() {
638 >    public void testToArray2() throws InterruptedException {
639          ArrayBlockingQueue q = populatedQueue(SIZE);
640 <        Integer[] ints = new Integer[SIZE];
641 <        ints = (Integer[])q.toArray(ints);
642 <        try {
643 <            for (int i = 0; i < ints.length; i++)
730 <                assertEquals(ints[i], q.take());
731 <        } catch (InterruptedException e) {
732 <            unexpectedException();
733 <        }
640 >        Integer[] ints = new Integer[SIZE];
641 >        ints = (Integer[])q.toArray(ints);
642 >        for (int i = 0; i < ints.length; i++)
643 >            assertEquals(ints[i], q.take());
644      }
645  
646      /**
647       * toArray(null) throws NPE
648       */
649      public void testToArray_BadArg() {
650 <        try {
651 <            ArrayBlockingQueue q = populatedQueue(SIZE);
652 <            Object o[] = q.toArray(null);
653 <            shouldThrow();
654 <        } catch (NullPointerException success) {}
650 >        ArrayBlockingQueue q = populatedQueue(SIZE);
651 >        try {
652 >            Object o[] = q.toArray(null);
653 >            shouldThrow();
654 >        } catch (NullPointerException success) {}
655      }
656  
657      /**
658       * toArray with incompatible array type throws CCE
659       */
660      public void testToArray1_BadArg() {
661 <        try {
662 <            ArrayBlockingQueue q = populatedQueue(SIZE);
663 <            Object o[] = q.toArray(new String[10] );
664 <            shouldThrow();
665 <        } catch (ArrayStoreException  success) {}
661 >        ArrayBlockingQueue q = populatedQueue(SIZE);
662 >        try {
663 >            Object o[] = q.toArray(new String[10]);
664 >            shouldThrow();
665 >        } catch (ArrayStoreException success) {}
666      }
667  
668  
669      /**
670       * iterator iterates through all elements
671       */
672 <    public void testIterator() {
672 >    public void testIterator() throws InterruptedException {
673          ArrayBlockingQueue q = populatedQueue(SIZE);
674 <        Iterator it = q.iterator();
675 <        try {
676 <            while (it.hasNext()) {
677 <                assertEquals(it.next(), q.take());
768 <            }
769 <        } catch (InterruptedException e) {
770 <            unexpectedException();
771 <        }
674 >        Iterator it = q.iterator();
675 >        while (it.hasNext()) {
676 >            assertEquals(it.next(), q.take());
677 >        }
678      }
679  
680      /**
681       * iterator.remove removes current element
682       */
683 <    public void testIteratorRemove () {
683 >    public void testIteratorRemove() {
684          final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
685          q.add(two);
686          q.add(one);
# Line 785 | Line 691 | public class ArrayBlockingQueueTest exte
691          it.remove();
692  
693          it = q.iterator();
694 <        assertEquals(it.next(), one);
695 <        assertEquals(it.next(), three);
694 >        assertSame(it.next(), one);
695 >        assertSame(it.next(), three);
696          assertFalse(it.hasNext());
697      }
698  
# Line 803 | Line 709 | public class ArrayBlockingQueueTest exte
709  
710          int k = 0;
711          for (Iterator it = q.iterator(); it.hasNext();) {
712 <            int i = ((Integer)(it.next())).intValue();
807 <            assertEquals(++k, i);
712 >            assertEquals(++k, it.next());
713          }
714          assertEquals(3, k);
715      }
# Line 812 | Line 717 | public class ArrayBlockingQueueTest exte
717      /**
718       * Modifications do not cause iterators to fail
719       */
720 <    public void testWeaklyConsistentIteration () {
720 >    public void testWeaklyConsistentIteration() {
721          final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
722          q.add(one);
723          q.add(two);
724          q.add(three);
725 <        try {
726 <            for (Iterator it = q.iterator(); it.hasNext();) {
727 <                q.remove();
823 <                it.next();
824 <            }
825 <        }
826 <        catch (ConcurrentModificationException e) {
827 <            unexpectedException();
725 >        for (Iterator it = q.iterator(); it.hasNext();) {
726 >            q.remove();
727 >            it.next();
728          }
729          assertEquals(0, q.size());
730      }
# Line 850 | Line 750 | public class ArrayBlockingQueueTest exte
750          q.add(one);
751          q.add(two);
752          ExecutorService executor = Executors.newFixedThreadPool(2);
753 <        executor.execute(new Runnable() {
754 <            public void run() {
755 <                threadAssertFalse(q.offer(three));
756 <                try {
757 <                    threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
758 <                    threadAssertEquals(0, q.remainingCapacity());
759 <                }
760 <                catch (InterruptedException e) {
761 <                    threadUnexpectedException();
762 <                }
763 <            }
764 <        });
865 <
866 <        executor.execute(new Runnable() {
867 <            public void run() {
868 <                try {
869 <                    Thread.sleep(SMALL_DELAY_MS);
870 <                    threadAssertEquals(one, q.take());
871 <                }
872 <                catch (InterruptedException e) {
873 <                    threadUnexpectedException();
874 <                }
875 <            }
876 <        });
753 >        executor.execute(new CheckedRunnable() {
754 >            public void realRun() throws InterruptedException {
755 >                assertFalse(q.offer(three));
756 >                assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
757 >                assertEquals(0, q.remainingCapacity());
758 >            }});
759 >
760 >        executor.execute(new CheckedRunnable() {
761 >            public void realRun() throws InterruptedException {
762 >                Thread.sleep(SMALL_DELAY_MS);
763 >                assertSame(one, q.take());
764 >            }});
765  
766          joinPool(executor);
879
767      }
768  
769      /**
# Line 885 | Line 772 | public class ArrayBlockingQueueTest exte
772      public void testPollInExecutor() {
773          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
774          ExecutorService executor = Executors.newFixedThreadPool(2);
775 <        executor.execute(new Runnable() {
776 <            public void run() {
777 <                threadAssertNull(q.poll());
778 <                try {
779 <                    threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
780 <                    threadAssertTrue(q.isEmpty());
781 <                }
782 <                catch (InterruptedException e) {
783 <                    threadUnexpectedException();
784 <                }
785 <            }
786 <        });
900 <
901 <        executor.execute(new Runnable() {
902 <            public void run() {
903 <                try {
904 <                    Thread.sleep(SMALL_DELAY_MS);
905 <                    q.put(one);
906 <                }
907 <                catch (InterruptedException e) {
908 <                    threadUnexpectedException();
909 <                }
910 <            }
911 <        });
775 >        executor.execute(new CheckedRunnable() {
776 >            public void realRun() throws InterruptedException {
777 >                assertNull(q.poll());
778 >                assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
779 >                assertTrue(q.isEmpty());
780 >            }});
781 >
782 >        executor.execute(new CheckedRunnable() {
783 >            public void realRun() throws InterruptedException {
784 >                Thread.sleep(SMALL_DELAY_MS);
785 >                q.put(one);
786 >            }});
787  
788          joinPool(executor);
789      }
# Line 916 | Line 791 | public class ArrayBlockingQueueTest exte
791      /**
792       * A deserialized serialized queue has same elements in same order
793       */
794 <    public void testSerialization() {
794 >    public void testSerialization() throws Exception {
795          ArrayBlockingQueue q = populatedQueue(SIZE);
796  
797 <        try {
798 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
799 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
800 <            out.writeObject(q);
801 <            out.close();
802 <
803 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
804 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
805 <            ArrayBlockingQueue r = (ArrayBlockingQueue)in.readObject();
806 <            assertEquals(q.size(), r.size());
807 <            while (!q.isEmpty())
933 <                assertEquals(q.remove(), r.remove());
934 <        } catch (Exception e) {
935 <            unexpectedException();
936 <        }
797 >        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
798 >        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
799 >        out.writeObject(q);
800 >        out.close();
801 >
802 >        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
803 >        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
804 >        ArrayBlockingQueue r = (ArrayBlockingQueue)in.readObject();
805 >        assertEquals(q.size(), r.size());
806 >        while (!q.isEmpty())
807 >            assertEquals(q.remove(), r.remove());
808      }
809  
810      /**
# Line 944 | Line 815 | public class ArrayBlockingQueueTest exte
815          try {
816              q.drainTo(null);
817              shouldThrow();
818 <        } catch (NullPointerException success) {
948 <        }
818 >        } catch (NullPointerException success) {}
819      }
820  
821      /**
# Line 956 | Line 826 | public class ArrayBlockingQueueTest exte
826          try {
827              q.drainTo(q);
828              shouldThrow();
829 <        } catch (IllegalArgumentException success) {
960 <        }
829 >        } catch (IllegalArgumentException success) {}
830      }
831  
832      /**
# Line 987 | Line 856 | public class ArrayBlockingQueueTest exte
856      /**
857       * drainTo empties full queue, unblocking a waiting put.
858       */
859 <    public void testDrainToWithActivePut() {
859 >    public void testDrainToWithActivePut() throws InterruptedException {
860          final ArrayBlockingQueue q = populatedQueue(SIZE);
861 <        Thread t = new Thread(new Runnable() {
862 <                public void run() {
863 <                    try {
864 <                        q.put(new Integer(SIZE+1));
865 <                    } catch (InterruptedException ie) {
866 <                        threadUnexpectedException();
867 <                    }
868 <                }
869 <            });
870 <        try {
871 <            t.start();
872 <            ArrayList l = new ArrayList();
873 <            q.drainTo(l);
1005 <            assertTrue(l.size() >= SIZE);
1006 <            for (int i = 0; i < SIZE; ++i)
1007 <                assertEquals(l.get(i), new Integer(i));
1008 <            t.join();
1009 <            assertTrue(q.size() + l.size() >= SIZE);
1010 <        } catch (Exception e) {
1011 <            unexpectedException();
1012 <        }
861 >        Thread t = new Thread(new CheckedRunnable() {
862 >            public void realRun() throws InterruptedException {
863 >                q.put(new Integer(SIZE+1));
864 >            }});
865 >
866 >        t.start();
867 >        ArrayList l = new ArrayList();
868 >        q.drainTo(l);
869 >        assertTrue(l.size() >= SIZE);
870 >        for (int i = 0; i < SIZE; ++i)
871 >            assertEquals(l.get(i), new Integer(i));
872 >        t.join();
873 >        assertTrue(q.size() + l.size() >= SIZE);
874      }
875  
876      /**
# Line 1020 | Line 881 | public class ArrayBlockingQueueTest exte
881          try {
882              q.drainTo(null, 0);
883              shouldThrow();
884 <        } catch (NullPointerException success) {
1024 <        }
884 >        } catch (NullPointerException success) {}
885      }
886  
887      /**
# Line 1032 | Line 892 | public class ArrayBlockingQueueTest exte
892          try {
893              q.drainTo(q, 0);
894              shouldThrow();
895 <        } catch (IllegalArgumentException success) {
1036 <        }
895 >        } catch (IllegalArgumentException success) {}
896      }
897  
898      /**
# Line 1055 | Line 914 | public class ArrayBlockingQueueTest exte
914          }
915      }
916  
1058
917   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines