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

Comparing jsr166/src/test/tck/LinkedBlockingQueueTest.java (file contents):
Revision 1.16 by jsr166, Sat Nov 21 02:33:20 2009 UTC vs.
Revision 1.31 by jsr166, Tue Oct 19 00:43:49 2010 UTC

# Line 14 | Line 14 | import java.io.*;
14  
15   public class LinkedBlockingQueueTest extends JSR166TestCase {
16  
17 +    public static class Unbounded extends BlockingQueueTest {
18 +        protected BlockingQueue emptyCollection() {
19 +            return new LinkedBlockingQueue();
20 +        }
21 +    }
22 +
23 +    public static class Bounded extends BlockingQueueTest {
24 +        protected BlockingQueue emptyCollection() {
25 +            return new LinkedBlockingQueue(20);
26 +        }
27 +    }
28 +
29      public static void main(String[] args) {
30 <        junit.textui.TestRunner.run (suite());
30 >        junit.textui.TestRunner.run(suite());
31      }
32  
33      public static Test suite() {
34 <        return new TestSuite(LinkedBlockingQueueTest.class);
34 >        return newTestSuite(LinkedBlockingQueueTest.class,
35 >                            new Unbounded().testSuite(),
36 >                            new Bounded().testSuite());
37      }
38  
39  
# Line 48 | Line 62 | public class LinkedBlockingQueueTest ext
62      }
63  
64      /**
65 <     * Constructor throws IAE if  capacity argument nonpositive
65 >     * Constructor throws IAE if capacity argument nonpositive
66       */
67      public void testConstructor2() {
68          try {
69              LinkedBlockingQueue q = new LinkedBlockingQueue(0);
70              shouldThrow();
71 <        }
58 <        catch (IllegalArgumentException success) {}
71 >        } catch (IllegalArgumentException success) {}
72      }
73  
74      /**
# Line 65 | Line 78 | public class LinkedBlockingQueueTest ext
78          try {
79              LinkedBlockingQueue q = new LinkedBlockingQueue(null);
80              shouldThrow();
81 <        }
69 <        catch (NullPointerException success) {}
81 >        } catch (NullPointerException success) {}
82      }
83  
84      /**
# Line 77 | Line 89 | public class LinkedBlockingQueueTest ext
89              Integer[] ints = new Integer[SIZE];
90              LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints));
91              shouldThrow();
92 <        }
81 <        catch (NullPointerException success) {}
92 >        } catch (NullPointerException success) {}
93      }
94  
95      /**
# Line 91 | Line 102 | public class LinkedBlockingQueueTest ext
102                  ints[i] = new Integer(i);
103              LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints));
104              shouldThrow();
105 <        }
95 <        catch (NullPointerException success) {}
105 >        } catch (NullPointerException success) {}
106      }
107  
108      /**
109       * Queue contains all elements of collection used to initialize
110       */
111      public void testConstructor6() {
112 <        try {
113 <            Integer[] ints = new Integer[SIZE];
114 <            for (int i = 0; i < SIZE; ++i)
115 <                ints[i] = new Integer(i);
116 <            LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints));
117 <            for (int i = 0; i < SIZE; ++i)
108 <                assertEquals(ints[i], q.poll());
109 <        }
110 <        finally {}
112 >        Integer[] ints = new Integer[SIZE];
113 >        for (int i = 0; i < SIZE; ++i)
114 >            ints[i] = new Integer(i);
115 >        LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints));
116 >        for (int i = 0; i < SIZE; ++i)
117 >            assertEquals(ints[i], q.poll());
118      }
119  
120      /**
# Line 150 | Line 157 | public class LinkedBlockingQueueTest ext
157              LinkedBlockingQueue q = new LinkedBlockingQueue(1);
158              q.offer(null);
159              shouldThrow();
160 <        } catch (NullPointerException success) { }
160 >        } catch (NullPointerException success) {}
161      }
162  
163      /**
# Line 161 | Line 168 | public class LinkedBlockingQueueTest ext
168              LinkedBlockingQueue q = new LinkedBlockingQueue(1);
169              q.add(null);
170              shouldThrow();
171 <        } catch (NullPointerException success) { }
171 >        } catch (NullPointerException success) {}
172      }
173  
174      /**
# Line 184 | Line 191 | public class LinkedBlockingQueueTest ext
191              }
192              assertEquals(0, q.remainingCapacity());
193              q.add(new Integer(SIZE));
194 <        } catch (IllegalStateException success) {
195 <        }
194 >            shouldThrow();
195 >        } catch (IllegalStateException success) {}
196      }
197  
198      /**
# Line 196 | Line 203 | public class LinkedBlockingQueueTest ext
203              LinkedBlockingQueue q = new LinkedBlockingQueue(1);
204              q.addAll(null);
205              shouldThrow();
206 <        }
200 <        catch (NullPointerException success) {}
206 >        } catch (NullPointerException success) {}
207      }
208  
209      /**
# Line 208 | Line 214 | public class LinkedBlockingQueueTest ext
214              LinkedBlockingQueue q = populatedQueue(SIZE);
215              q.addAll(q);
216              shouldThrow();
217 <        }
212 <        catch (IllegalArgumentException success) {}
217 >        } catch (IllegalArgumentException success) {}
218      }
219  
220      /**
# Line 221 | Line 226 | public class LinkedBlockingQueueTest ext
226              Integer[] ints = new Integer[SIZE];
227              q.addAll(Arrays.asList(ints));
228              shouldThrow();
229 <        }
225 <        catch (NullPointerException success) {}
229 >        } catch (NullPointerException success) {}
230      }
231 +
232      /**
233       * addAll of a collection with any null elements throws NPE after
234       * possibly adding some elements
# Line 236 | Line 241 | public class LinkedBlockingQueueTest ext
241                  ints[i] = new Integer(i);
242              q.addAll(Arrays.asList(ints));
243              shouldThrow();
244 <        }
240 <        catch (NullPointerException success) {}
244 >        } catch (NullPointerException success) {}
245      }
246 +
247      /**
248       * addAll throws ISE if not enough room
249       */
# Line 250 | Line 255 | public class LinkedBlockingQueueTest ext
255                  ints[i] = new Integer(i);
256              q.addAll(Arrays.asList(ints));
257              shouldThrow();
258 <        }
254 <        catch (IllegalStateException success) {}
258 >        } catch (IllegalStateException success) {}
259      }
260 +
261      /**
262       * Queue contains all elements, in traversal order, of successful addAll
263       */
264      public void testAddAll5() {
265 <        try {
266 <            Integer[] empty = new Integer[0];
267 <            Integer[] ints = new Integer[SIZE];
268 <            for (int i = 0; i < SIZE; ++i)
269 <                ints[i] = new Integer(i);
270 <            LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
271 <            assertFalse(q.addAll(Arrays.asList(empty)));
272 <            assertTrue(q.addAll(Arrays.asList(ints)));
273 <            for (int i = 0; i < SIZE; ++i)
269 <                assertEquals(ints[i], q.poll());
270 <        }
271 <        finally {}
265 >        Integer[] empty = new Integer[0];
266 >        Integer[] ints = new Integer[SIZE];
267 >        for (int i = 0; i < SIZE; ++i)
268 >            ints[i] = new Integer(i);
269 >        LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
270 >        assertFalse(q.addAll(Arrays.asList(empty)));
271 >        assertTrue(q.addAll(Arrays.asList(ints)));
272 >        for (int i = 0; i < SIZE; ++i)
273 >            assertEquals(ints[i], q.poll());
274      }
275  
276      /**
277       * put(null) throws NPE
278       */
279 <     public void testPutNull() {
279 >     public void testPutNull() throws InterruptedException {
280          try {
281              LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
282              q.put(null);
283              shouldThrow();
284 <        }
283 <        catch (NullPointerException success) {
284 <        }
285 <        catch (InterruptedException ie) {
286 <            unexpectedException();
287 <        }
284 >        } catch (NullPointerException success) {}
285       }
286  
287      /**
288       * all elements successfully put are contained
289       */
290 <     public void testPut() {
291 <         try {
292 <             LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
293 <             for (int i = 0; i < SIZE; ++i) {
294 <                 Integer I = new Integer(i);
295 <                 q.put(I);
299 <                 assertTrue(q.contains(I));
300 <             }
301 <             assertEquals(0, q.remainingCapacity());
302 <         }
303 <        catch (InterruptedException ie) {
304 <            unexpectedException();
290 >    public void testPut() throws InterruptedException {
291 >        LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
292 >        for (int i = 0; i < SIZE; ++i) {
293 >            Integer I = new Integer(i);
294 >            q.put(I);
295 >            assertTrue(q.contains(I));
296          }
297 +        assertEquals(0, q.remainingCapacity());
298      }
299  
300      /**
301       * put blocks interruptibly if full
302       */
303 <    public void testBlockingPut() {
304 <        Thread t = new Thread(new Runnable() {
305 <                public void run() {
306 <                    int added = 0;
307 <                    try {
308 <                        LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
309 <                        for (int i = 0; i < SIZE; ++i) {
310 <                            q.put(new Integer(i));
311 <                            ++added;
312 <                        }
313 <                        q.put(new Integer(SIZE));
314 <                        threadShouldThrow();
315 <                    } catch (InterruptedException ie) {
316 <                        threadAssertEquals(added, SIZE);
325 <                    }
326 <                }});
303 >    public void testBlockingPut() throws InterruptedException {
304 >        final LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
305 >        Thread t = new Thread(new CheckedRunnable() {
306 >            public void realRun() throws InterruptedException {
307 >                for (int i = 0; i < SIZE; ++i)
308 >                    q.put(i);
309 >                assertEquals(SIZE, q.size());
310 >                assertEquals(0, q.remainingCapacity());
311 >                try {
312 >                    q.put(99);
313 >                    shouldThrow();
314 >                } catch (InterruptedException success) {}
315 >            }});
316 >
317          t.start();
318 <        try {
319 <           Thread.sleep(SHORT_DELAY_MS);
320 <           t.interrupt();
321 <           t.join();
322 <        }
333 <        catch (InterruptedException ie) {
334 <            unexpectedException();
335 <        }
318 >        Thread.sleep(SHORT_DELAY_MS);
319 >        t.interrupt();
320 >        t.join();
321 >        assertEquals(SIZE, q.size());
322 >        assertEquals(0, q.remainingCapacity());
323      }
324  
325      /**
326       * put blocks waiting for take when full
327       */
328 <    public void testPutWithTake() {
328 >    public void testPutWithTake() throws InterruptedException {
329 >        final int capacity = 2;
330          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
331 <        Thread t = new Thread(new Runnable() {
332 <                public void run() {
333 <                    int added = 0;
334 <                    try {
335 <                        q.put(new Object());
336 <                        ++added;
337 <                        q.put(new Object());
338 <                        ++added;
339 <                        q.put(new Object());
340 <                        ++added;
341 <                        q.put(new Object());
342 <                        ++added;
343 <                        threadShouldThrow();
344 <                    } catch (InterruptedException e) {
345 <                        threadAssertTrue(added >= 2);
346 <                    }
347 <                }
348 <            });
361 <        try {
362 <            t.start();
363 <            Thread.sleep(SHORT_DELAY_MS);
364 <            q.take();
365 <            t.interrupt();
366 <            t.join();
367 <        } catch (Exception e) {
368 <            unexpectedException();
369 <        }
331 >        Thread t = new Thread(new CheckedRunnable() {
332 >            public void realRun() throws InterruptedException {
333 >                for (int i = 0; i < capacity + 1; i++)
334 >                    q.put(i);
335 >                try {
336 >                    q.put(99);
337 >                    shouldThrow();
338 >                } catch (InterruptedException success) {}
339 >            }});
340 >
341 >        t.start();
342 >        Thread.sleep(SHORT_DELAY_MS);
343 >        assertEquals(q.remainingCapacity(), 0);
344 >        assertEquals(0, q.take());
345 >        Thread.sleep(SHORT_DELAY_MS);
346 >        t.interrupt();
347 >        t.join();
348 >        assertEquals(q.remainingCapacity(), 0);
349      }
350  
351      /**
352       * timed offer times out if full and elements not taken
353       */
354 <    public void testTimedOffer() {
354 >    public void testTimedOffer() throws InterruptedException {
355          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
356 <        Thread t = new Thread(new Runnable() {
357 <                public void run() {
358 <                    try {
359 <                        q.put(new Object());
360 <                        q.put(new Object());
361 <                        threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS));
362 <                        q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
363 <                        threadShouldThrow();
364 <                    } catch (InterruptedException success) {}
365 <                }
387 <            });
356 >        Thread t = new Thread(new CheckedRunnable() {
357 >            public void realRun() throws InterruptedException {
358 >                q.put(new Object());
359 >                q.put(new Object());
360 >                assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS));
361 >                try {
362 >                    q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
363 >                    shouldThrow();
364 >                } catch (InterruptedException success) {}
365 >            }});
366  
367 <        try {
368 <            t.start();
369 <            Thread.sleep(SMALL_DELAY_MS);
370 <            t.interrupt();
393 <            t.join();
394 <        } catch (Exception e) {
395 <            unexpectedException();
396 <        }
367 >        t.start();
368 >        Thread.sleep(SMALL_DELAY_MS);
369 >        t.interrupt();
370 >        t.join();
371      }
372  
373      /**
374       * take retrieves elements in FIFO order
375       */
376 <    public void testTake() {
377 <        try {
378 <            LinkedBlockingQueue q = populatedQueue(SIZE);
379 <            for (int i = 0; i < SIZE; ++i) {
406 <                assertEquals(i, ((Integer)q.take()).intValue());
407 <            }
408 <        } catch (InterruptedException e) {
409 <            unexpectedException();
376 >    public void testTake() throws InterruptedException {
377 >        LinkedBlockingQueue q = populatedQueue(SIZE);
378 >        for (int i = 0; i < SIZE; ++i) {
379 >            assertEquals(i, q.take());
380          }
381      }
382  
383      /**
384       * take blocks interruptibly when empty
385       */
386 <    public void testTakeFromEmpty() {
386 >    public void testTakeFromEmpty() throws InterruptedException {
387          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
388 <        Thread t = new Thread(new Runnable() {
389 <                public void run() {
390 <                    try {
391 <                        q.take();
392 <                        threadShouldThrow();
393 <                    } catch (InterruptedException success) { }
394 <                }
395 <            });
396 <        try {
427 <            t.start();
428 <            Thread.sleep(SHORT_DELAY_MS);
429 <            t.interrupt();
430 <            t.join();
431 <        } catch (Exception e) {
432 <            unexpectedException();
433 <        }
388 >        Thread t = new ThreadShouldThrow(InterruptedException.class) {
389 >            public void realRun() throws InterruptedException {
390 >                q.take();
391 >            }};
392 >
393 >        t.start();
394 >        Thread.sleep(SHORT_DELAY_MS);
395 >        t.interrupt();
396 >        t.join();
397      }
398  
399      /**
400       * Take removes existing elements until empty, then blocks interruptibly
401       */
402 <    public void testBlockingTake() {
403 <        Thread t = new Thread(new Runnable() {
404 <                public void run() {
405 <                    try {
406 <                        LinkedBlockingQueue q = populatedQueue(SIZE);
407 <                        for (int i = 0; i < SIZE; ++i) {
408 <                            assertEquals(i, ((Integer)q.take()).intValue());
409 <                        }
410 <                        q.take();
411 <                        threadShouldThrow();
412 <                    } catch (InterruptedException success) {
413 <                    }
414 <                }});
402 >    public void testBlockingTake() throws InterruptedException {
403 >        final LinkedBlockingQueue q = populatedQueue(SIZE);
404 >        Thread t = new Thread(new CheckedRunnable() {
405 >            public void realRun() throws InterruptedException {
406 >                for (int i = 0; i < SIZE; ++i) {
407 >                    assertEquals(i, q.take());
408 >                }
409 >                try {
410 >                    q.take();
411 >                    shouldThrow();
412 >                } catch (InterruptedException success) {}
413 >            }});
414 >
415          t.start();
416 <        try {
417 <           Thread.sleep(SHORT_DELAY_MS);
418 <           t.interrupt();
456 <           t.join();
457 <        }
458 <        catch (InterruptedException ie) {
459 <            unexpectedException();
460 <        }
416 >        Thread.sleep(SHORT_DELAY_MS);
417 >        t.interrupt();
418 >        t.join();
419      }
420  
463
421      /**
422       * poll succeeds unless empty
423       */
424      public void testPoll() {
425          LinkedBlockingQueue 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());
430      }
# Line 475 | Line 432 | public class LinkedBlockingQueueTest ext
432      /**
433       * timed pool with zero timeout succeeds when non-empty, else times out
434       */
435 <    public void testTimedPoll0() {
436 <        try {
437 <            LinkedBlockingQueue q = populatedQueue(SIZE);
438 <            for (int i = 0; i < SIZE; ++i) {
482 <                assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue());
483 <            }
484 <            assertNull(q.poll(0, MILLISECONDS));
485 <        } catch (InterruptedException e) {
486 <            unexpectedException();
435 >    public void testTimedPoll0() throws InterruptedException {
436 >        LinkedBlockingQueue 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
445       */
446 <    public void testTimedPoll() {
447 <        try {
448 <            LinkedBlockingQueue q = populatedQueue(SIZE);
449 <            for (int i = 0; i < SIZE; ++i) {
497 <                assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
498 <            }
499 <            assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
500 <        } catch (InterruptedException e) {
501 <            unexpectedException();
446 >    public void testTimedPoll() throws InterruptedException {
447 >        LinkedBlockingQueue 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 <                        LinkedBlockingQueue q = populatedQueue(SIZE);
463 <                        for (int i = 0; i < SIZE; ++i) {
515 <                            threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
516 <                        }
517 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
518 <                    } catch (InterruptedException success) {
519 <                    }
520 <                }});
521 <        t.start();
522 <        try {
523 <           Thread.sleep(SHORT_DELAY_MS);
524 <           t.interrupt();
525 <           t.join();
526 <        }
527 <        catch (InterruptedException ie) {
528 <            unexpectedException();
529 <        }
530 <    }
531 <
532 <    /**
533 <     *  timed poll before a delayed offer fails; after offer succeeds;
534 <     *  on interruption throws
535 <     */
536 <    public void testTimedPollWithOffer() {
537 <        final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
538 <        Thread t = new Thread(new Runnable() {
539 <                public void run() {
540 <                    try {
541 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
542 <                        q.poll(LONG_DELAY_MS, MILLISECONDS);
543 <                        q.poll(LONG_DELAY_MS, MILLISECONDS);
544 <                        threadShouldThrow();
545 <                    } catch (InterruptedException success) { }
458 >    public void testInterruptedTimedPoll() throws InterruptedException {
459 >        Thread t = new Thread(new CheckedRunnable() {
460 >            public void realRun() throws InterruptedException {
461 >                LinkedBlockingQueue q = populatedQueue(SIZE);
462 >                for (int i = 0; i < SIZE; ++i) {
463 >                    assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
464                  }
465 <            });
466 <        try {
467 <            t.start();
468 <            Thread.sleep(SMALL_DELAY_MS);
469 <            assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
470 <            t.interrupt();
471 <            t.join();
472 <        } catch (Exception e) {
473 <            unexpectedException();
474 <        }
465 >                try {
466 >                    q.poll(SMALL_DELAY_MS, MILLISECONDS);
467 >                    shouldThrow();
468 >                } catch (InterruptedException success) {}
469 >            }});
470 >
471 >        t.start();
472 >        Thread.sleep(SHORT_DELAY_MS);
473 >        t.interrupt();
474 >        t.join();
475      }
476  
477      /**
# Line 562 | Line 480 | public class LinkedBlockingQueueTest ext
480      public void testPeek() {
481          LinkedBlockingQueue q = populatedQueue(SIZE);
482          for (int i = 0; i < SIZE; ++i) {
483 <            assertEquals(i, ((Integer)q.peek()).intValue());
484 <            q.poll();
483 >            assertEquals(i, q.peek());
484 >            assertEquals(i, q.poll());
485              assertTrue(q.peek() == null ||
486 <                       i != ((Integer)q.peek()).intValue());
486 >                       !q.peek().equals(i));
487          }
488          assertNull(q.peek());
489      }
# Line 576 | Line 494 | public class LinkedBlockingQueueTest ext
494      public void testElement() {
495          LinkedBlockingQueue q = populatedQueue(SIZE);
496          for (int i = 0; i < SIZE; ++i) {
497 <            assertEquals(i, ((Integer)q.element()).intValue());
498 <            q.poll();
497 >            assertEquals(i, q.element());
498 >            assertEquals(i, q.poll());
499          }
500          try {
501              q.element();
502              shouldThrow();
503 <        }
586 <        catch (NoSuchElementException success) {}
503 >        } catch (NoSuchElementException success) {}
504      }
505  
506      /**
# Line 592 | Line 509 | public class LinkedBlockingQueueTest ext
509      public void testRemove() {
510          LinkedBlockingQueue q = populatedQueue(SIZE);
511          for (int i = 0; i < SIZE; ++i) {
512 <            assertEquals(i, ((Integer)q.remove()).intValue());
512 >            assertEquals(i, q.remove());
513          }
514          try {
515              q.remove();
516              shouldThrow();
517 <        } catch (NoSuchElementException success) {
601 <        }
517 >        } catch (NoSuchElementException success) {}
518      }
519  
520      /**
# Line 619 | Line 535 | public class LinkedBlockingQueueTest ext
535      /**
536       * An add following remove(x) succeeds
537       */
538 <    public void testRemoveElementAndAdd() {
539 <        try {
540 <            LinkedBlockingQueue q = new LinkedBlockingQueue();
541 <            assertTrue(q.add(new Integer(1)));
542 <            assertTrue(q.add(new Integer(2)));
543 <            assertTrue(q.remove(new Integer(1)));
544 <            assertTrue(q.remove(new Integer(2)));
545 <            assertTrue(q.add(new Integer(3)));
630 <            assertTrue(q.take() != null);
631 <        } catch (Exception e) {
632 <            unexpectedException();
633 <        }
538 >    public void testRemoveElementAndAdd() throws InterruptedException {
539 >        LinkedBlockingQueue q = new LinkedBlockingQueue();
540 >        assertTrue(q.add(new Integer(1)));
541 >        assertTrue(q.add(new Integer(2)));
542 >        assertTrue(q.remove(new Integer(1)));
543 >        assertTrue(q.remove(new Integer(2)));
544 >        assertTrue(q.add(new Integer(3)));
545 >        assertTrue(q.take() != null);
546      }
547  
548      /**
# Line 713 | Line 625 | public class LinkedBlockingQueueTest ext
625      /**
626       * toArray contains all elements
627       */
628 <    public void testToArray() {
628 >    public void testToArray() throws InterruptedException {
629          LinkedBlockingQueue q = populatedQueue(SIZE);
630          Object[] o = q.toArray();
719        try {
631          for (int i = 0; i < o.length; i++)
632              assertEquals(o[i], q.take());
722        } catch (InterruptedException e) {
723            unexpectedException();
724        }
633      }
634  
635      /**
636       * toArray(a) contains all elements
637       */
638 <    public void testToArray2() {
638 >    public void testToArray2() throws InterruptedException {
639          LinkedBlockingQueue 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++)
736 <                assertEquals(ints[i], q.take());
737 <        } catch (InterruptedException e) {
738 <            unexpectedException();
739 <        }
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 +        LinkedBlockingQueue q = populatedQueue(SIZE);
651          try {
747            LinkedBlockingQueue q = populatedQueue(SIZE);
652              Object o[] = q.toArray(null);
653              shouldThrow();
654          } catch (NullPointerException success) {}
# Line 754 | Line 658 | public class LinkedBlockingQueueTest ext
658       * toArray with incompatible array type throws CCE
659       */
660      public void testToArray1_BadArg() {
661 +        LinkedBlockingQueue q = populatedQueue(SIZE);
662          try {
663 <            LinkedBlockingQueue q = populatedQueue(SIZE);
759 <            Object o[] = q.toArray(new String[10] );
663 >            Object o[] = q.toArray(new String[10]);
664              shouldThrow();
665 <        } catch (ArrayStoreException  success) {}
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          LinkedBlockingQueue q = populatedQueue(SIZE);
674          Iterator it = q.iterator();
675 <        try {
676 <            while (it.hasNext()) {
773 <                assertEquals(it.next(), q.take());
774 <            }
775 <        } catch (InterruptedException e) {
776 <            unexpectedException();
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 LinkedBlockingQueue q = new LinkedBlockingQueue(3);
685          q.add(two);
686          q.add(one);
# Line 791 | Line 691 | public class LinkedBlockingQueueTest ext
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 808 | Line 708 | public class LinkedBlockingQueueTest ext
708          assertEquals(0, q.remainingCapacity());
709          int k = 0;
710          for (Iterator it = q.iterator(); it.hasNext();) {
711 <            int i = ((Integer)(it.next())).intValue();
812 <            assertEquals(++k, i);
711 >            assertEquals(++k, it.next());
712          }
713          assertEquals(3, k);
714      }
# Line 817 | Line 716 | public class LinkedBlockingQueueTest ext
716      /**
717       * Modifications do not cause iterators to fail
718       */
719 <    public void testWeaklyConsistentIteration () {
719 >    public void testWeaklyConsistentIteration() {
720          final LinkedBlockingQueue q = new LinkedBlockingQueue(3);
721          q.add(one);
722          q.add(two);
723          q.add(three);
724 <        try {
725 <            for (Iterator it = q.iterator(); it.hasNext();) {
726 <                q.remove();
828 <                it.next();
829 <            }
830 <        }
831 <        catch (ConcurrentModificationException e) {
832 <            unexpectedException();
724 >        for (Iterator it = q.iterator(); it.hasNext();) {
725 >            q.remove();
726 >            it.next();
727          }
728          assertEquals(0, q.size());
729      }
# Line 855 | Line 749 | public class LinkedBlockingQueueTest ext
749          q.add(one);
750          q.add(two);
751          ExecutorService executor = Executors.newFixedThreadPool(2);
752 <        executor.execute(new Runnable() {
753 <            public void run() {
754 <                threadAssertFalse(q.offer(three));
755 <                try {
756 <                    threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
757 <                    threadAssertEquals(0, q.remainingCapacity());
758 <                }
759 <                catch (InterruptedException e) {
760 <                    threadUnexpectedException();
761 <                }
762 <            }
763 <        });
870 <
871 <        executor.execute(new Runnable() {
872 <            public void run() {
873 <                try {
874 <                    Thread.sleep(SMALL_DELAY_MS);
875 <                    threadAssertEquals(one, q.take());
876 <                }
877 <                catch (InterruptedException e) {
878 <                    threadUnexpectedException();
879 <                }
880 <            }
881 <        });
752 >        executor.execute(new CheckedRunnable() {
753 >            public void realRun() throws InterruptedException {
754 >                assertFalse(q.offer(three));
755 >                assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
756 >                assertEquals(0, q.remainingCapacity());
757 >            }});
758 >
759 >        executor.execute(new CheckedRunnable() {
760 >            public void realRun() throws InterruptedException {
761 >                Thread.sleep(SMALL_DELAY_MS);
762 >                assertSame(one, q.take());
763 >            }});
764  
765          joinPool(executor);
766      }
# Line 889 | Line 771 | public class LinkedBlockingQueueTest ext
771      public void testPollInExecutor() {
772          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
773          ExecutorService executor = Executors.newFixedThreadPool(2);
774 <        executor.execute(new Runnable() {
775 <            public void run() {
776 <                threadAssertNull(q.poll());
777 <                try {
778 <                    threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
779 <                    threadAssertTrue(q.isEmpty());
780 <                }
781 <                catch (InterruptedException e) {
782 <                    threadUnexpectedException();
783 <                }
784 <            }
785 <        });
904 <
905 <        executor.execute(new Runnable() {
906 <            public void run() {
907 <                try {
908 <                    Thread.sleep(SMALL_DELAY_MS);
909 <                    q.put(one);
910 <                }
911 <                catch (InterruptedException e) {
912 <                    threadUnexpectedException();
913 <                }
914 <            }
915 <        });
774 >        executor.execute(new CheckedRunnable() {
775 >            public void realRun() throws InterruptedException {
776 >                assertNull(q.poll());
777 >                assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
778 >                assertTrue(q.isEmpty());
779 >            }});
780 >
781 >        executor.execute(new CheckedRunnable() {
782 >            public void realRun() throws InterruptedException {
783 >                Thread.sleep(SMALL_DELAY_MS);
784 >                q.put(one);
785 >            }});
786  
787          joinPool(executor);
788      }
# Line 920 | Line 790 | public class LinkedBlockingQueueTest ext
790      /**
791       * A deserialized serialized queue has same elements in same order
792       */
793 <    public void testSerialization() {
793 >    public void testSerialization() throws Exception {
794          LinkedBlockingQueue q = populatedQueue(SIZE);
795  
796 <        try {
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 <            LinkedBlockingQueue r = (LinkedBlockingQueue)in.readObject();
805 <            assertEquals(q.size(), r.size());
806 <            while (!q.isEmpty())
937 <                assertEquals(q.remove(), r.remove());
938 <        } catch (Exception e) {
939 <            unexpectedException();
940 <        }
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 >        LinkedBlockingQueue r = (LinkedBlockingQueue)in.readObject();
804 >        assertEquals(q.size(), r.size());
805 >        while (!q.isEmpty())
806 >            assertEquals(q.remove(), r.remove());
807      }
808  
809      /**
# Line 948 | Line 814 | public class LinkedBlockingQueueTest ext
814          try {
815              q.drainTo(null);
816              shouldThrow();
817 <        } catch (NullPointerException success) {
952 <        }
817 >        } catch (NullPointerException success) {}
818      }
819  
820      /**
# Line 960 | Line 825 | public class LinkedBlockingQueueTest ext
825          try {
826              q.drainTo(q);
827              shouldThrow();
828 <        } catch (IllegalArgumentException success) {
964 <        }
828 >        } catch (IllegalArgumentException success) {}
829      }
830  
831      /**
# Line 991 | Line 855 | public class LinkedBlockingQueueTest ext
855      /**
856       * drainTo empties full queue, unblocking a waiting put.
857       */
858 <    public void testDrainToWithActivePut() {
858 >    public void testDrainToWithActivePut() throws InterruptedException {
859          final LinkedBlockingQueue q = populatedQueue(SIZE);
860 <        Thread t = new Thread(new Runnable() {
861 <                public void run() {
862 <                    try {
863 <                        q.put(new Integer(SIZE+1));
864 <                    } catch (InterruptedException ie) {
865 <                        threadUnexpectedException();
866 <                    }
867 <                }
868 <            });
869 <        try {
870 <            t.start();
871 <            ArrayList l = new ArrayList();
872 <            q.drainTo(l);
1009 <            assertTrue(l.size() >= SIZE);
1010 <            for (int i = 0; i < SIZE; ++i)
1011 <                assertEquals(l.get(i), new Integer(i));
1012 <            t.join();
1013 <            assertTrue(q.size() + l.size() >= SIZE);
1014 <        } catch (Exception e) {
1015 <            unexpectedException();
1016 <        }
860 >        Thread t = new Thread(new CheckedRunnable() {
861 >            public void realRun() throws InterruptedException {
862 >                q.put(new Integer(SIZE+1));
863 >            }});
864 >
865 >        t.start();
866 >        ArrayList l = new ArrayList();
867 >        q.drainTo(l);
868 >        assertTrue(l.size() >= SIZE);
869 >        for (int i = 0; i < SIZE; ++i)
870 >            assertEquals(l.get(i), new Integer(i));
871 >        t.join();
872 >        assertTrue(q.size() + l.size() >= SIZE);
873      }
874  
875      /**
# Line 1024 | Line 880 | public class LinkedBlockingQueueTest ext
880          try {
881              q.drainTo(null, 0);
882              shouldThrow();
883 <        } catch (NullPointerException success) {
1028 <        }
883 >        } catch (NullPointerException success) {}
884      }
885  
886      /**
# Line 1036 | Line 891 | public class LinkedBlockingQueueTest ext
891          try {
892              q.drainTo(q, 0);
893              shouldThrow();
894 <        } catch (IllegalArgumentException success) {
1040 <        }
894 >        } catch (IllegalArgumentException success) {}
895      }
896  
897      /**
898 <     * drainTo(c, n) empties first max {n, size} elements of queue into c
898 >     * drainTo(c, n) empties first min(n, size) elements of queue into c
899       */
900      public void testDrainToN() {
901          LinkedBlockingQueue q = new LinkedBlockingQueue();
# Line 1050 | Line 904 | public class LinkedBlockingQueueTest ext
904                  assertTrue(q.offer(new Integer(j)));
905              ArrayList l = new ArrayList();
906              q.drainTo(l, i);
907 <            int k = (i < SIZE)? i : SIZE;
907 >            int k = (i < SIZE) ? i : SIZE;
908              assertEquals(l.size(), k);
909              assertEquals(q.size(), SIZE-k);
910              for (int j = 0; j < k; ++j)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines