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.12 by jsr166, Mon Nov 2 20:28:31 2009 UTC vs.
Revision 1.21 by jsr166, Sat Nov 21 22:00:46 2009 UTC

# Line 9 | Line 9
9   import junit.framework.*;
10   import java.util.*;
11   import java.util.concurrent.*;
12 + import static java.util.concurrent.TimeUnit.MILLISECONDS;
13   import java.io.*;
14  
15   public class LinkedBlockingQueueTest extends JSR166TestCase {
16  
17      public static void main(String[] args) {
18 <        junit.textui.TestRunner.run (suite());
18 >        junit.textui.TestRunner.run (suite());
19      }
20  
21      public static Test suite() {
22 <        return new TestSuite(LinkedBlockingQueueTest.class);
22 >        return new TestSuite(LinkedBlockingQueueTest.class);
23      }
24  
25  
# Line 29 | Line 30 | public class LinkedBlockingQueueTest ext
30      private LinkedBlockingQueue populatedQueue(int n) {
31          LinkedBlockingQueue q = new LinkedBlockingQueue(n);
32          assertTrue(q.isEmpty());
33 <        for(int i = 0; i < n; i++)
34 <            assertTrue(q.offer(new Integer(i)));
33 >        for (int i = 0; i < n; i++)
34 >            assertTrue(q.offer(new Integer(i)));
35          assertFalse(q.isEmpty());
36          assertEquals(0, q.remainingCapacity());
37 <        assertEquals(n, q.size());
37 >        assertEquals(n, q.size());
38          return q;
39      }
40  
# Line 47 | Line 48 | public class LinkedBlockingQueueTest ext
48      }
49  
50      /**
51 <     * Constructor throws IAE if  capacity argument nonpositive
51 >     * Constructor throws IAE if capacity argument nonpositive
52       */
53      public void testConstructor2() {
54          try {
55              LinkedBlockingQueue q = new LinkedBlockingQueue(0);
56              shouldThrow();
57 <        }
57 <        catch (IllegalArgumentException success) {}
57 >        } catch (IllegalArgumentException success) {}
58      }
59  
60      /**
# Line 64 | Line 64 | public class LinkedBlockingQueueTest ext
64          try {
65              LinkedBlockingQueue q = new LinkedBlockingQueue(null);
66              shouldThrow();
67 <        }
68 <        catch (NullPointerException success) {}
67 >        } catch (NullPointerException success) {}
68      }
69  
70      /**
# Line 76 | Line 75 | public class LinkedBlockingQueueTest ext
75              Integer[] ints = new Integer[SIZE];
76              LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints));
77              shouldThrow();
78 <        }
80 <        catch (NullPointerException success) {}
78 >        } catch (NullPointerException success) {}
79      }
80  
81      /**
# Line 90 | Line 88 | public class LinkedBlockingQueueTest ext
88                  ints[i] = new Integer(i);
89              LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints));
90              shouldThrow();
91 <        }
94 <        catch (NullPointerException success) {}
91 >        } catch (NullPointerException success) {}
92      }
93  
94      /**
95       * Queue contains all elements of collection used to initialize
96       */
97      public void testConstructor6() {
98 <        try {
99 <            Integer[] ints = new Integer[SIZE];
100 <            for (int i = 0; i < SIZE; ++i)
101 <                ints[i] = new Integer(i);
102 <            LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints));
103 <            for (int i = 0; i < SIZE; ++i)
107 <                assertEquals(ints[i], q.poll());
108 <        }
109 <        finally {}
98 >        Integer[] ints = new Integer[SIZE];
99 >        for (int i = 0; i < SIZE; ++i)
100 >            ints[i] = new Integer(i);
101 >        LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints));
102 >        for (int i = 0; i < SIZE; ++i)
103 >            assertEquals(ints[i], q.poll());
104      }
105  
106      /**
# Line 145 | Line 139 | public class LinkedBlockingQueueTest ext
139       * offer(null) throws NPE
140       */
141      public void testOfferNull() {
142 <        try {
142 >        try {
143              LinkedBlockingQueue q = new LinkedBlockingQueue(1);
144              q.offer(null);
145              shouldThrow();
146 <        } catch (NullPointerException success) { }
146 >        } catch (NullPointerException success) {}
147      }
148  
149      /**
150       * add(null) throws NPE
151       */
152      public void testAddNull() {
153 <        try {
153 >        try {
154              LinkedBlockingQueue q = new LinkedBlockingQueue(1);
155              q.add(null);
156              shouldThrow();
157 <        } catch (NullPointerException success) { }
157 >        } catch (NullPointerException success) {}
158      }
159  
160      /**
# Line 176 | Line 170 | public class LinkedBlockingQueueTest ext
170       * add succeeds if not full; throws ISE if full
171       */
172      public void testAdd() {
173 <        try {
173 >        try {
174              LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
175              for (int i = 0; i < SIZE; ++i) {
176                  assertTrue(q.add(new Integer(i)));
177              }
178              assertEquals(0, q.remainingCapacity());
179              q.add(new Integer(SIZE));
180 <        } catch (IllegalStateException success){
181 <        }
180 >            shouldThrow();
181 >        } catch (IllegalStateException success) {}
182      }
183  
184      /**
# Line 195 | Line 189 | public class LinkedBlockingQueueTest ext
189              LinkedBlockingQueue q = new LinkedBlockingQueue(1);
190              q.addAll(null);
191              shouldThrow();
192 <        }
199 <        catch (NullPointerException success) {}
192 >        } catch (NullPointerException success) {}
193      }
194  
195      /**
# Line 207 | Line 200 | public class LinkedBlockingQueueTest ext
200              LinkedBlockingQueue q = populatedQueue(SIZE);
201              q.addAll(q);
202              shouldThrow();
203 <        }
211 <        catch (IllegalArgumentException success) {}
203 >        } catch (IllegalArgumentException success) {}
204      }
205  
206      /**
# Line 220 | Line 212 | public class LinkedBlockingQueueTest ext
212              Integer[] ints = new Integer[SIZE];
213              q.addAll(Arrays.asList(ints));
214              shouldThrow();
215 <        }
224 <        catch (NullPointerException success) {}
215 >        } catch (NullPointerException success) {}
216      }
217      /**
218       * addAll of a collection with any null elements throws NPE after
# Line 235 | Line 226 | public class LinkedBlockingQueueTest ext
226                  ints[i] = new Integer(i);
227              q.addAll(Arrays.asList(ints));
228              shouldThrow();
229 <        }
239 <        catch (NullPointerException success) {}
229 >        } catch (NullPointerException success) {}
230      }
231      /**
232       * addAll throws ISE if not enough room
# Line 249 | Line 239 | public class LinkedBlockingQueueTest ext
239                  ints[i] = new Integer(i);
240              q.addAll(Arrays.asList(ints));
241              shouldThrow();
242 <        }
253 <        catch (IllegalStateException success) {}
242 >        } catch (IllegalStateException success) {}
243      }
244      /**
245       * Queue contains all elements, in traversal order, of successful addAll
246       */
247      public void testAddAll5() {
248 <        try {
249 <            Integer[] empty = new Integer[0];
250 <            Integer[] ints = new Integer[SIZE];
251 <            for (int i = 0; i < SIZE; ++i)
252 <                ints[i] = new Integer(i);
253 <            LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
254 <            assertFalse(q.addAll(Arrays.asList(empty)));
255 <            assertTrue(q.addAll(Arrays.asList(ints)));
256 <            for (int i = 0; i < SIZE; ++i)
268 <                assertEquals(ints[i], q.poll());
269 <        }
270 <        finally {}
248 >        Integer[] empty = new Integer[0];
249 >        Integer[] ints = new Integer[SIZE];
250 >        for (int i = 0; i < SIZE; ++i)
251 >            ints[i] = new Integer(i);
252 >        LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
253 >        assertFalse(q.addAll(Arrays.asList(empty)));
254 >        assertTrue(q.addAll(Arrays.asList(ints)));
255 >        for (int i = 0; i < SIZE; ++i)
256 >            assertEquals(ints[i], q.poll());
257      }
258  
259      /**
260       * put(null) throws NPE
261       */
262 <     public void testPutNull() {
263 <        try {
262 >     public void testPutNull() throws InterruptedException {
263 >        try {
264              LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
265              q.put(null);
266              shouldThrow();
267 <        }
282 <        catch (NullPointerException success){
283 <        }
284 <        catch (InterruptedException ie) {
285 <            unexpectedException();
286 <        }
267 >        } catch (NullPointerException success) {}
268       }
269  
270      /**
271       * all elements successfully put are contained
272       */
273 <     public void testPut() {
274 <         try {
275 <             LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
276 <             for (int i = 0; i < SIZE; ++i) {
277 <                 Integer I = new Integer(i);
278 <                 q.put(I);
298 <                 assertTrue(q.contains(I));
299 <             }
300 <             assertEquals(0, q.remainingCapacity());
301 <         }
302 <        catch (InterruptedException ie) {
303 <            unexpectedException();
273 >    public void testPut() throws InterruptedException {
274 >        LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
275 >        for (int i = 0; i < SIZE; ++i) {
276 >            Integer I = new Integer(i);
277 >            q.put(I);
278 >            assertTrue(q.contains(I));
279          }
280 +        assertEquals(0, q.remainingCapacity());
281      }
282  
283      /**
284       * put blocks interruptibly if full
285       */
286 <    public void testBlockingPut() {
287 <        Thread t = new Thread(new Runnable() {
288 <                public void run() {
289 <                    int added = 0;
290 <                    try {
291 <                        LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
292 <                        for (int i = 0; i < SIZE; ++i) {
293 <                            q.put(new Integer(i));
294 <                            ++added;
319 <                        }
320 <                        q.put(new Integer(SIZE));
321 <                        threadShouldThrow();
322 <                    } catch (InterruptedException ie){
323 <                        threadAssertEquals(added, SIZE);
286 >    public void testBlockingPut() throws InterruptedException {
287 >        Thread t = new Thread(new CheckedRunnable() {
288 >            public void realRun() {
289 >                int added = 0;
290 >                try {
291 >                    LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
292 >                    for (int i = 0; i < SIZE; ++i) {
293 >                        q.put(new Integer(i));
294 >                        ++added;
295                      }
296 <                }});
296 >                    q.put(new Integer(SIZE));
297 >                    threadShouldThrow();
298 >                } catch (InterruptedException success) {
299 >                    threadAssertEquals(added, SIZE);
300 >                }
301 >            }});
302 >
303          t.start();
304 <        try {
305 <           Thread.sleep(SHORT_DELAY_MS);
306 <           t.interrupt();
330 <           t.join();
331 <        }
332 <        catch (InterruptedException ie) {
333 <            unexpectedException();
334 <        }
304 >        Thread.sleep(SHORT_DELAY_MS);
305 >        t.interrupt();
306 >        t.join();
307      }
308  
309      /**
310       * put blocks waiting for take when full
311       */
312 <    public void testPutWithTake() {
312 >    public void testPutWithTake() throws InterruptedException {
313          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
314 <        Thread t = new Thread(new Runnable() {
315 <                public void run() {
316 <                    int added = 0;
317 <                    try {
318 <                        q.put(new Object());
319 <                        ++added;
320 <                        q.put(new Object());
321 <                        ++added;
322 <                        q.put(new Object());
323 <                        ++added;
324 <                        q.put(new Object());
325 <                        ++added;
326 <                        threadShouldThrow();
327 <                    } catch (InterruptedException e){
328 <                        threadAssertTrue(added >= 2);
357 <                    }
314 >        Thread t = new Thread(new CheckedRunnable() {
315 >            public void realRun() {
316 >                int added = 0;
317 >                try {
318 >                    q.put(new Object());
319 >                    ++added;
320 >                    q.put(new Object());
321 >                    ++added;
322 >                    q.put(new Object());
323 >                    ++added;
324 >                    q.put(new Object());
325 >                    ++added;
326 >                    threadShouldThrow();
327 >                } catch (InterruptedException success) {
328 >                    threadAssertTrue(added >= 2);
329                  }
330 <            });
331 <        try {
332 <            t.start();
333 <            Thread.sleep(SHORT_DELAY_MS);
334 <            q.take();
335 <            t.interrupt();
336 <            t.join();
366 <        } catch (Exception e){
367 <            unexpectedException();
368 <        }
330 >            }});
331 >
332 >        t.start();
333 >        Thread.sleep(SHORT_DELAY_MS);
334 >        q.take();
335 >        t.interrupt();
336 >        t.join();
337      }
338  
339      /**
340       * timed offer times out if full and elements not taken
341       */
342 <    public void testTimedOffer() {
342 >    public void testTimedOffer() throws InterruptedException {
343          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
344 <        Thread t = new Thread(new Runnable() {
345 <                public void run() {
346 <                    try {
347 <                        q.put(new Object());
348 <                        q.put(new Object());
349 <                        threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
350 <                        q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
351 <                        threadShouldThrow();
352 <                    } catch (InterruptedException success){}
353 <                }
386 <            });
344 >        Thread t = new Thread(new CheckedRunnable() {
345 >            public void realRun() throws InterruptedException {
346 >                q.put(new Object());
347 >                q.put(new Object());
348 >                assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS));
349 >                try {
350 >                    q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
351 >                    shouldThrow();
352 >                } catch (InterruptedException success) {}
353 >            }});
354  
355 <        try {
356 <            t.start();
357 <            Thread.sleep(SMALL_DELAY_MS);
358 <            t.interrupt();
392 <            t.join();
393 <        } catch (Exception e){
394 <            unexpectedException();
395 <        }
355 >        t.start();
356 >        Thread.sleep(SMALL_DELAY_MS);
357 >        t.interrupt();
358 >        t.join();
359      }
360  
361      /**
362       * take retrieves elements in FIFO order
363       */
364 <    public void testTake() {
365 <        try {
366 <            LinkedBlockingQueue q = populatedQueue(SIZE);
367 <            for (int i = 0; i < SIZE; ++i) {
368 <                assertEquals(i, ((Integer)q.take()).intValue());
406 <            }
407 <        } catch (InterruptedException e){
408 <            unexpectedException();
409 <        }
364 >    public void testTake() throws InterruptedException {
365 >        LinkedBlockingQueue q = populatedQueue(SIZE);
366 >        for (int i = 0; i < SIZE; ++i) {
367 >            assertEquals(i, ((Integer)q.take()).intValue());
368 >        }
369      }
370  
371      /**
372       * take blocks interruptibly when empty
373       */
374 <    public void testTakeFromEmpty() {
374 >    public void testTakeFromEmpty() throws InterruptedException {
375          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
376 <        Thread t = new Thread(new Runnable() {
377 <                public void run() {
378 <                    try {
379 <                        q.take();
380 <                        threadShouldThrow();
381 <                    } catch (InterruptedException success){ }
382 <                }
383 <            });
384 <        try {
426 <            t.start();
427 <            Thread.sleep(SHORT_DELAY_MS);
428 <            t.interrupt();
429 <            t.join();
430 <        } catch (Exception e){
431 <            unexpectedException();
432 <        }
376 >        Thread t = new ThreadShouldThrow(InterruptedException.class) {
377 >            public void realRun() throws InterruptedException {
378 >                q.take();
379 >            }};
380 >
381 >        t.start();
382 >        Thread.sleep(SHORT_DELAY_MS);
383 >        t.interrupt();
384 >        t.join();
385      }
386  
387      /**
388       * Take removes existing elements until empty, then blocks interruptibly
389       */
390 <    public void testBlockingTake() {
391 <        Thread t = new Thread(new Runnable() {
392 <                public void run() {
393 <                    try {
394 <                        LinkedBlockingQueue q = populatedQueue(SIZE);
395 <                        for (int i = 0; i < SIZE; ++i) {
396 <                            assertEquals(i, ((Integer)q.take()).intValue());
397 <                        }
398 <                        q.take();
399 <                        threadShouldThrow();
448 <                    } catch (InterruptedException success){
449 <                    }
450 <                }});
390 >    public void testBlockingTake() throws InterruptedException {
391 >        Thread t = new ThreadShouldThrow(InterruptedException.class) {
392 >            public void realRun() throws InterruptedException {
393 >                LinkedBlockingQueue q = populatedQueue(SIZE);
394 >                for (int i = 0; i < SIZE; ++i) {
395 >                    assertEquals(i, ((Integer)q.take()).intValue());
396 >                }
397 >                q.take();
398 >            }};
399 >
400          t.start();
401 <        try {
402 <           Thread.sleep(SHORT_DELAY_MS);
403 <           t.interrupt();
455 <           t.join();
456 <        }
457 <        catch (InterruptedException ie) {
458 <            unexpectedException();
459 <        }
401 >        Thread.sleep(SHORT_DELAY_MS);
402 >        t.interrupt();
403 >        t.join();
404      }
405  
406  
# Line 468 | Line 412 | public class LinkedBlockingQueueTest ext
412          for (int i = 0; i < SIZE; ++i) {
413              assertEquals(i, ((Integer)q.poll()).intValue());
414          }
415 <        assertNull(q.poll());
415 >        assertNull(q.poll());
416      }
417  
418      /**
419       * timed pool with zero timeout succeeds when non-empty, else times out
420       */
421 <    public void testTimedPoll0() {
422 <        try {
423 <            LinkedBlockingQueue q = populatedQueue(SIZE);
424 <            for (int i = 0; i < SIZE; ++i) {
425 <                assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue());
426 <            }
483 <            assertNull(q.poll(0, TimeUnit.MILLISECONDS));
484 <        } catch (InterruptedException e){
485 <            unexpectedException();
486 <        }
421 >    public void testTimedPoll0() throws InterruptedException {
422 >        LinkedBlockingQueue q = populatedQueue(SIZE);
423 >        for (int i = 0; i < SIZE; ++i) {
424 >            assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue());
425 >        }
426 >        assertNull(q.poll(0, MILLISECONDS));
427      }
428  
429      /**
430       * timed pool with nonzero timeout succeeds when non-empty, else times out
431       */
432 <    public void testTimedPoll() {
433 <        try {
434 <            LinkedBlockingQueue q = populatedQueue(SIZE);
435 <            for (int i = 0; i < SIZE; ++i) {
436 <                assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
437 <            }
498 <            assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
499 <        } catch (InterruptedException e){
500 <            unexpectedException();
501 <        }
432 >    public void testTimedPoll() throws InterruptedException {
433 >        LinkedBlockingQueue q = populatedQueue(SIZE);
434 >        for (int i = 0; i < SIZE; ++i) {
435 >            assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
436 >        }
437 >        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
438      }
439  
440      /**
441       * Interrupted timed poll throws InterruptedException instead of
442       * returning timeout status
443       */
444 <    public void testInterruptedTimedPoll() {
445 <        Thread t = new Thread(new Runnable() {
446 <                public void run() {
447 <                    try {
448 <                        LinkedBlockingQueue q = populatedQueue(SIZE);
449 <                        for (int i = 0; i < SIZE; ++i) {
450 <                            threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
451 <                        }
452 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
453 <                    } catch (InterruptedException success){
454 <                    }
455 <                }});
444 >    public void testInterruptedTimedPoll() throws InterruptedException {
445 >        Thread t = new Thread(new CheckedRunnable() {
446 >            public void realRun() throws InterruptedException {
447 >                LinkedBlockingQueue q = populatedQueue(SIZE);
448 >                for (int i = 0; i < SIZE; ++i) {
449 >                    assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
450 >                }
451 >                try {
452 >                    q.poll(SMALL_DELAY_MS, MILLISECONDS);
453 >                    shouldThrow();
454 >                } catch (InterruptedException success) {}
455 >            }});
456 >
457          t.start();
458 <        try {
459 <           Thread.sleep(SHORT_DELAY_MS);
460 <           t.interrupt();
524 <           t.join();
525 <        }
526 <        catch (InterruptedException ie) {
527 <            unexpectedException();
528 <        }
458 >        Thread.sleep(SHORT_DELAY_MS);
459 >        t.interrupt();
460 >        t.join();
461      }
462  
463      /**
464       *  timed poll before a delayed offer fails; after offer succeeds;
465       *  on interruption throws
466       */
467 <    public void testTimedPollWithOffer() {
467 >    public void testTimedPollWithOffer() throws InterruptedException {
468          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
469 <        Thread t = new Thread(new Runnable() {
470 <                public void run() {
471 <                    try {
472 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
473 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
474 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
475 <                        threadShouldThrow();
476 <                    } catch (InterruptedException success) { }
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();
552 <            t.join();
553 <        } catch (Exception e){
554 <            unexpectedException();
555 <        }
469 >        Thread t = new Thread(new CheckedRunnable() {
470 >            public void realRun() throws InterruptedException {
471 >                assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
472 >                assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
473 >                try {
474 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
475 >                    shouldThrow();
476 >                } catch (InterruptedException success) {}
477 >            }});
478 >
479 >        t.start();
480 >        Thread.sleep(SMALL_DELAY_MS);
481 >        assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
482 >        t.interrupt();
483 >        t.join();
484      }
485  
486      /**
# Line 566 | Line 494 | public class LinkedBlockingQueueTest ext
494              assertTrue(q.peek() == null ||
495                         i != ((Integer)q.peek()).intValue());
496          }
497 <        assertNull(q.peek());
497 >        assertNull(q.peek());
498      }
499  
500      /**
# Line 581 | Line 509 | public class LinkedBlockingQueueTest ext
509          try {
510              q.element();
511              shouldThrow();
512 <        }
585 <        catch (NoSuchElementException success) {}
512 >        } catch (NoSuchElementException success) {}
513      }
514  
515      /**
# Line 596 | Line 523 | public class LinkedBlockingQueueTest ext
523          try {
524              q.remove();
525              shouldThrow();
526 <        } catch (NoSuchElementException success){
600 <        }
526 >        } catch (NoSuchElementException success) {}
527      }
528  
529      /**
# Line 618 | Line 544 | public class LinkedBlockingQueueTest ext
544      /**
545       * An add following remove(x) succeeds
546       */
547 <    public void testRemoveElementAndAdd() {
548 <        try {
549 <            LinkedBlockingQueue q = new LinkedBlockingQueue();
550 <            assertTrue(q.add(new Integer(1)));
551 <            assertTrue(q.add(new Integer(2)));
552 <            assertTrue(q.remove(new Integer(1)));
553 <            assertTrue(q.remove(new Integer(2)));
554 <            assertTrue(q.add(new Integer(3)));
629 <            assertTrue(q.take() != null);
630 <        } catch (Exception e){
631 <            unexpectedException();
632 <        }
547 >    public void testRemoveElementAndAdd() throws InterruptedException {
548 >        LinkedBlockingQueue q = new LinkedBlockingQueue();
549 >        assertTrue(q.add(new Integer(1)));
550 >        assertTrue(q.add(new Integer(2)));
551 >        assertTrue(q.remove(new Integer(1)));
552 >        assertTrue(q.remove(new Integer(2)));
553 >        assertTrue(q.add(new Integer(3)));
554 >        assertTrue(q.take() != null);
555      }
556  
557      /**
# Line 712 | Line 634 | public class LinkedBlockingQueueTest ext
634      /**
635       * toArray contains all elements
636       */
637 <    public void testToArray() {
637 >    public void testToArray() throws InterruptedException {
638          LinkedBlockingQueue q = populatedQueue(SIZE);
639 <        Object[] o = q.toArray();
640 <        try {
641 <        for(int i = 0; i < o.length; i++)
720 <            assertEquals(o[i], q.take());
721 <        } catch (InterruptedException e){
722 <            unexpectedException();
723 <        }
639 >        Object[] o = q.toArray();
640 >        for (int i = 0; i < o.length; i++)
641 >            assertEquals(o[i], q.take());
642      }
643  
644      /**
645       * toArray(a) contains all elements
646       */
647 <    public void testToArray2() {
647 >    public void testToArray2() throws InterruptedException {
648          LinkedBlockingQueue q = populatedQueue(SIZE);
649 <        Integer[] ints = new Integer[SIZE];
650 <        ints = (Integer[])q.toArray(ints);
651 <        try {
652 <            for(int i = 0; i < ints.length; i++)
735 <                assertEquals(ints[i], q.take());
736 <        } catch (InterruptedException e){
737 <            unexpectedException();
738 <        }
649 >        Integer[] ints = new Integer[SIZE];
650 >        ints = (Integer[])q.toArray(ints);
651 >        for (int i = 0; i < ints.length; i++)
652 >            assertEquals(ints[i], q.take());
653      }
654  
655      /**
656       * toArray(null) throws NPE
657       */
658      public void testToArray_BadArg() {
659 <        try {
659 >        try {
660              LinkedBlockingQueue q = populatedQueue(SIZE);
661 <            Object o[] = q.toArray(null);
662 <            shouldThrow();
663 <        } catch(NullPointerException success){}
661 >            Object o[] = q.toArray(null);
662 >            shouldThrow();
663 >        } catch (NullPointerException success) {}
664      }
665  
666      /**
667       * toArray with incompatible array type throws CCE
668       */
669      public void testToArray1_BadArg() {
670 <        try {
670 >        try {
671              LinkedBlockingQueue q = populatedQueue(SIZE);
672 <            Object o[] = q.toArray(new String[10] );
673 <            shouldThrow();
674 <        } catch(ArrayStoreException  success){}
672 >            Object o[] = q.toArray(new String[10] );
673 >            shouldThrow();
674 >        } catch (ArrayStoreException success) {}
675      }
676  
677  
678      /**
679       * iterator iterates through all elements
680       */
681 <    public void testIterator() {
681 >    public void testIterator() throws InterruptedException {
682          LinkedBlockingQueue q = populatedQueue(SIZE);
683 <        Iterator it = q.iterator();
684 <        try {
685 <            while(it.hasNext()){
686 <                assertEquals(it.next(), q.take());
773 <            }
774 <        } catch (InterruptedException e){
775 <            unexpectedException();
776 <        }
683 >        Iterator it = q.iterator();
684 >        while (it.hasNext()) {
685 >            assertEquals(it.next(), q.take());
686 >        }
687      }
688  
689      /**
# Line 821 | Line 731 | public class LinkedBlockingQueueTest ext
731          q.add(one);
732          q.add(two);
733          q.add(three);
734 <        try {
735 <            for (Iterator it = q.iterator(); it.hasNext();) {
736 <                q.remove();
827 <                it.next();
828 <            }
829 <        }
830 <        catch (ConcurrentModificationException e) {
831 <            unexpectedException();
734 >        for (Iterator it = q.iterator(); it.hasNext();) {
735 >            q.remove();
736 >            it.next();
737          }
738          assertEquals(0, q.size());
739      }
# Line 854 | Line 759 | public class LinkedBlockingQueueTest ext
759          q.add(one);
760          q.add(two);
761          ExecutorService executor = Executors.newFixedThreadPool(2);
762 <        executor.execute(new Runnable() {
763 <            public void run() {
762 >        executor.execute(new CheckedRunnable() {
763 >            public void realRun() throws InterruptedException {
764                  threadAssertFalse(q.offer(three));
765 <                try {
766 <                    threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
767 <                    threadAssertEquals(0, q.remainingCapacity());
768 <                }
769 <                catch (InterruptedException e) {
770 <                    threadUnexpectedException();
771 <                }
772 <            }
773 <        });
869 <
870 <        executor.execute(new Runnable() {
871 <            public void run() {
872 <                try {
873 <                    Thread.sleep(SMALL_DELAY_MS);
874 <                    threadAssertEquals(one, q.take());
875 <                }
876 <                catch (InterruptedException e) {
877 <                    threadUnexpectedException();
878 <                }
879 <            }
880 <        });
765 >                threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
766 >                threadAssertEquals(0, q.remainingCapacity());
767 >            }});
768 >
769 >        executor.execute(new CheckedRunnable() {
770 >            public void realRun() throws InterruptedException {
771 >                Thread.sleep(SMALL_DELAY_MS);
772 >                threadAssertEquals(one, q.take());
773 >            }});
774  
775          joinPool(executor);
776      }
# Line 888 | Line 781 | public class LinkedBlockingQueueTest ext
781      public void testPollInExecutor() {
782          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
783          ExecutorService executor = Executors.newFixedThreadPool(2);
784 <        executor.execute(new Runnable() {
785 <            public void run() {
784 >        executor.execute(new CheckedRunnable() {
785 >            public void realRun() throws InterruptedException {
786                  threadAssertNull(q.poll());
787 <                try {
788 <                    threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
789 <                    threadAssertTrue(q.isEmpty());
790 <                }
791 <                catch (InterruptedException e) {
792 <                    threadUnexpectedException();
793 <                }
794 <            }
795 <        });
903 <
904 <        executor.execute(new Runnable() {
905 <            public void run() {
906 <                try {
907 <                    Thread.sleep(SMALL_DELAY_MS);
908 <                    q.put(one);
909 <                }
910 <                catch (InterruptedException e) {
911 <                    threadUnexpectedException();
912 <                }
913 <            }
914 <        });
787 >                threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
788 >                threadAssertTrue(q.isEmpty());
789 >            }});
790 >
791 >        executor.execute(new CheckedRunnable() {
792 >            public void realRun() throws InterruptedException {
793 >                Thread.sleep(SMALL_DELAY_MS);
794 >                q.put(one);
795 >            }});
796  
797          joinPool(executor);
798      }
# Line 919 | Line 800 | public class LinkedBlockingQueueTest ext
800      /**
801       * A deserialized serialized queue has same elements in same order
802       */
803 <    public void testSerialization() {
803 >    public void testSerialization() throws Exception {
804          LinkedBlockingQueue q = populatedQueue(SIZE);
805  
806 <        try {
807 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
808 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
809 <            out.writeObject(q);
810 <            out.close();
811 <
812 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
813 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
814 <            LinkedBlockingQueue r = (LinkedBlockingQueue)in.readObject();
815 <            assertEquals(q.size(), r.size());
816 <            while (!q.isEmpty())
936 <                assertEquals(q.remove(), r.remove());
937 <        } catch(Exception e){
938 <            unexpectedException();
939 <        }
806 >        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
807 >        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
808 >        out.writeObject(q);
809 >        out.close();
810 >
811 >        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
812 >        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
813 >        LinkedBlockingQueue r = (LinkedBlockingQueue)in.readObject();
814 >        assertEquals(q.size(), r.size());
815 >        while (!q.isEmpty())
816 >            assertEquals(q.remove(), r.remove());
817      }
818  
819      /**
# Line 947 | Line 824 | public class LinkedBlockingQueueTest ext
824          try {
825              q.drainTo(null);
826              shouldThrow();
827 <        } catch(NullPointerException success) {
951 <        }
827 >        } catch (NullPointerException success) {}
828      }
829  
830      /**
# Line 959 | Line 835 | public class LinkedBlockingQueueTest ext
835          try {
836              q.drainTo(q);
837              shouldThrow();
838 <        } catch(IllegalArgumentException success) {
963 <        }
838 >        } catch (IllegalArgumentException success) {}
839      }
840  
841      /**
# Line 990 | Line 865 | public class LinkedBlockingQueueTest ext
865      /**
866       * drainTo empties full queue, unblocking a waiting put.
867       */
868 <    public void testDrainToWithActivePut() {
868 >    public void testDrainToWithActivePut() throws InterruptedException {
869          final LinkedBlockingQueue q = populatedQueue(SIZE);
870 <        Thread t = new Thread(new Runnable() {
871 <                public void run() {
872 <                    try {
873 <                        q.put(new Integer(SIZE+1));
874 <                    } catch (InterruptedException ie){
875 <                        threadUnexpectedException();
876 <                    }
877 <                }
878 <            });
879 <        try {
880 <            t.start();
881 <            ArrayList l = new ArrayList();
882 <            q.drainTo(l);
1008 <            assertTrue(l.size() >= SIZE);
1009 <            for (int i = 0; i < SIZE; ++i)
1010 <                assertEquals(l.get(i), new Integer(i));
1011 <            t.join();
1012 <            assertTrue(q.size() + l.size() >= SIZE);
1013 <        } catch(Exception e){
1014 <            unexpectedException();
1015 <        }
870 >        Thread t = new Thread(new CheckedRunnable() {
871 >            public void realRun() throws InterruptedException {
872 >                q.put(new Integer(SIZE+1));
873 >            }});
874 >
875 >        t.start();
876 >        ArrayList l = new ArrayList();
877 >        q.drainTo(l);
878 >        assertTrue(l.size() >= SIZE);
879 >        for (int i = 0; i < SIZE; ++i)
880 >            assertEquals(l.get(i), new Integer(i));
881 >        t.join();
882 >        assertTrue(q.size() + l.size() >= SIZE);
883      }
884  
885      /**
# Line 1023 | Line 890 | public class LinkedBlockingQueueTest ext
890          try {
891              q.drainTo(null, 0);
892              shouldThrow();
893 <        } catch(NullPointerException success) {
1027 <        }
893 >        } catch (NullPointerException success) {}
894      }
895  
896      /**
# Line 1035 | Line 901 | public class LinkedBlockingQueueTest ext
901          try {
902              q.drainTo(q, 0);
903              shouldThrow();
904 <        } catch(IllegalArgumentException success) {
1039 <        }
904 >        } catch (IllegalArgumentException success) {}
905      }
906  
907      /**
# Line 1045 | Line 910 | public class LinkedBlockingQueueTest ext
910      public void testDrainToN() {
911          LinkedBlockingQueue q = new LinkedBlockingQueue();
912          for (int i = 0; i < SIZE + 2; ++i) {
913 <            for(int j = 0; j < SIZE; j++)
913 >            for (int j = 0; j < SIZE; j++)
914                  assertTrue(q.offer(new Integer(j)));
915              ArrayList l = new ArrayList();
916              q.drainTo(l, i);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines