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.7 by dl, Sat Dec 27 19:26:43 2003 UTC vs.
Revision 1.18 by jsr166, Sat Nov 21 10:25:05 2009 UTC

# Line 2 | Line 2
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4   * http://creativecommons.org/licenses/publicdomain
5 < * Other contributors include Andrew Wright, Jeffrey Hayes,
6 < * Pat Fisher, Mike Judd.
5 > * Other contributors include Andrew Wright, Jeffrey Hayes,
6 > * Pat Fisher, Mike Judd.
7   */
8  
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 <
40 >
41      /**
42       * A new queue has the indicated capacity, or Integer.MAX_VALUE if
43       * none given
# 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;
295 <                        }
296 <                        q.put(new Integer(SIZE));
297 <                        threadShouldThrow();
298 <                    } catch (InterruptedException ie){
299 <                        threadAssertEquals(added, SIZE);
300 <                    }  
301 <                }});
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 >                    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 <                }
354 <            });
355 <        
388 <        try {
389 <            t.start();
390 <            Thread.sleep(SMALL_DELAY_MS);
391 <            t.interrupt();
392 <            t.join();
393 <        } catch (Exception e){
394 <            unexpectedException();
395 <        }
344 >        Thread t = new ThreadShouldThrow(InterruptedException.class) {
345 >            public void realRun() throws InterruptedException {
346 >                q.put(new Object());
347 >                q.put(new Object());
348 >                threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS));
349 >                q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
350 >            }};
351 >
352 >        t.start();
353 >        Thread.sleep(SMALL_DELAY_MS);
354 >        t.interrupt();
355 >        t.join();
356      }
357  
358      /**
359       * take retrieves elements in FIFO order
360       */
361 <    public void testTake() {
362 <        try {
363 <            LinkedBlockingQueue q = populatedQueue(SIZE);
364 <            for (int i = 0; i < SIZE; ++i) {
365 <                assertEquals(i, ((Integer)q.take()).intValue());
406 <            }
407 <        } catch (InterruptedException e){
408 <            unexpectedException();
409 <        }  
361 >    public void testTake() throws InterruptedException {
362 >        LinkedBlockingQueue q = populatedQueue(SIZE);
363 >        for (int i = 0; i < SIZE; ++i) {
364 >            assertEquals(i, ((Integer)q.take()).intValue());
365 >        }
366      }
367  
368      /**
369       * take blocks interruptibly when empty
370       */
371 <    public void testTakeFromEmpty() {
371 >    public void testTakeFromEmpty() throws InterruptedException {
372          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
373 <        Thread t = new Thread(new Runnable() {
374 <                public void run() {
375 <                    try {
376 <                        q.take();
377 <                        threadShouldThrow();
378 <                    } catch (InterruptedException success){ }                
379 <                }
380 <            });
381 <        try {
426 <            t.start();
427 <            Thread.sleep(SHORT_DELAY_MS);
428 <            t.interrupt();
429 <            t.join();
430 <        } catch (Exception e){
431 <            unexpectedException();
432 <        }
373 >        Thread t = new ThreadShouldThrow(InterruptedException.class) {
374 >            public void realRun() throws InterruptedException {
375 >                q.take();
376 >            }};
377 >
378 >        t.start();
379 >        Thread.sleep(SHORT_DELAY_MS);
380 >        t.interrupt();
381 >        t.join();
382      }
383  
384      /**
385       * Take removes existing elements until empty, then blocks interruptibly
386       */
387 <    public void testBlockingTake() {
388 <        Thread t = new Thread(new Runnable() {
389 <                public void run() {
390 <                    try {
391 <                        LinkedBlockingQueue q = populatedQueue(SIZE);
392 <                        for (int i = 0; i < SIZE; ++i) {
393 <                            assertEquals(i, ((Integer)q.take()).intValue());
394 <                        }
395 <                        q.take();
396 <                        threadShouldThrow();
448 <                    } catch (InterruptedException success){
449 <                    }  
450 <                }});
387 >    public void testBlockingTake() throws InterruptedException {
388 >        Thread t = new ThreadShouldThrow(InterruptedException.class) {
389 >            public void realRun() throws InterruptedException {
390 >                LinkedBlockingQueue q = populatedQueue(SIZE);
391 >                for (int i = 0; i < SIZE; ++i) {
392 >                    assertEquals(i, ((Integer)q.take()).intValue());
393 >                }
394 >                q.take();
395 >            }};
396 >
397          t.start();
398 <        try {
399 <           Thread.sleep(SHORT_DELAY_MS);
400 <           t.interrupt();
455 <           t.join();
456 <        }
457 <        catch (InterruptedException ie) {
458 <            unexpectedException();
459 <        }
398 >        Thread.sleep(SHORT_DELAY_MS);
399 >        t.interrupt();
400 >        t.join();
401      }
402  
403  
# Line 468 | Line 409 | public class LinkedBlockingQueueTest ext
409          for (int i = 0; i < SIZE; ++i) {
410              assertEquals(i, ((Integer)q.poll()).intValue());
411          }
412 <        assertNull(q.poll());
412 >        assertNull(q.poll());
413      }
414  
415      /**
416       * timed pool with zero timeout succeeds when non-empty, else times out
417       */
418 <    public void testTimedPoll0() {
419 <        try {
420 <            LinkedBlockingQueue q = populatedQueue(SIZE);
421 <            for (int i = 0; i < SIZE; ++i) {
422 <                assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue());
423 <            }
483 <            assertNull(q.poll(0, TimeUnit.MILLISECONDS));
484 <        } catch (InterruptedException e){
485 <            unexpectedException();
486 <        }  
418 >    public void testTimedPoll0() throws InterruptedException {
419 >        LinkedBlockingQueue q = populatedQueue(SIZE);
420 >        for (int i = 0; i < SIZE; ++i) {
421 >            assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue());
422 >        }
423 >        assertNull(q.poll(0, MILLISECONDS));
424      }
425  
426      /**
427       * timed pool with nonzero timeout succeeds when non-empty, else times out
428       */
429 <    public void testTimedPoll() {
430 <        try {
431 <            LinkedBlockingQueue q = populatedQueue(SIZE);
432 <            for (int i = 0; i < SIZE; ++i) {
433 <                assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
434 <            }
498 <            assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
499 <        } catch (InterruptedException e){
500 <            unexpectedException();
501 <        }  
429 >    public void testTimedPoll() throws InterruptedException {
430 >        LinkedBlockingQueue q = populatedQueue(SIZE);
431 >        for (int i = 0; i < SIZE; ++i) {
432 >            assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
433 >        }
434 >        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
435      }
436  
437      /**
438       * Interrupted timed poll throws InterruptedException instead of
439       * returning timeout status
440       */
441 <    public void testInterruptedTimedPoll() {
442 <        Thread t = new Thread(new Runnable() {
443 <                public void run() {
444 <                    try {
445 <                        LinkedBlockingQueue q = populatedQueue(SIZE);
446 <                        for (int i = 0; i < SIZE; ++i) {
447 <                            threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
448 <                        }
449 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
450 <                    } catch (InterruptedException success){
518 <                    }  
519 <                }});
441 >    public void testInterruptedTimedPoll() throws InterruptedException {
442 >        Thread t = new ThreadShouldThrow(InterruptedException.class) {
443 >            public void realRun() throws InterruptedException {
444 >                LinkedBlockingQueue q = populatedQueue(SIZE);
445 >                for (int i = 0; i < SIZE; ++i) {
446 >                    threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
447 >                }
448 >                q.poll(SMALL_DELAY_MS, MILLISECONDS);
449 >            }};
450 >
451          t.start();
452 <        try {
453 <           Thread.sleep(SHORT_DELAY_MS);
454 <           t.interrupt();
524 <           t.join();
525 <        }
526 <        catch (InterruptedException ie) {
527 <            unexpectedException();
528 <        }
452 >        Thread.sleep(SHORT_DELAY_MS);
453 >        t.interrupt();
454 >        t.join();
455      }
456  
457      /**
458       *  timed poll before a delayed offer fails; after offer succeeds;
459       *  on interruption throws
460       */
461 <    public void testTimedPollWithOffer() {
461 >    public void testTimedPollWithOffer() throws InterruptedException {
462          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
463 <        Thread t = new Thread(new Runnable() {
464 <                public void run() {
465 <                    try {
466 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
467 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
468 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
469 <                        threadShouldThrow();
470 <                    } catch (InterruptedException success) { }                
471 <                }
472 <            });
473 <        try {
474 <            t.start();
475 <            Thread.sleep(SMALL_DELAY_MS);
550 <            assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
551 <            t.interrupt();
552 <            t.join();
553 <        } catch (Exception e){
554 <            unexpectedException();
555 <        }
556 <    }  
463 >        Thread t = new ThreadShouldThrow(InterruptedException.class) {
464 >            public void realRun() throws InterruptedException {
465 >                threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
466 >                q.poll(LONG_DELAY_MS, MILLISECONDS);
467 >                q.poll(LONG_DELAY_MS, MILLISECONDS);
468 >            }};
469 >
470 >        t.start();
471 >        Thread.sleep(SMALL_DELAY_MS);
472 >        assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
473 >        t.interrupt();
474 >        t.join();
475 >    }
476  
477      /**
478       * peek returns next element, or null if empty
# Line 566 | Line 485 | public class LinkedBlockingQueueTest ext
485              assertTrue(q.peek() == null ||
486                         i != ((Integer)q.peek()).intValue());
487          }
488 <        assertNull(q.peek());
488 >        assertNull(q.peek());
489      }
490  
491      /**
# Line 581 | Line 500 | public class LinkedBlockingQueueTest ext
500          try {
501              q.element();
502              shouldThrow();
503 <        }
585 <        catch (NoSuchElementException success) {}
503 >        } catch (NoSuchElementException success) {}
504      }
505  
506      /**
# Line 596 | Line 514 | public class LinkedBlockingQueueTest ext
514          try {
515              q.remove();
516              shouldThrow();
517 <        } catch (NoSuchElementException success){
600 <        }  
517 >        } catch (NoSuchElementException success) {}
518      }
519  
520      /**
# Line 614 | Line 531 | public class LinkedBlockingQueueTest ext
531          }
532          assertTrue(q.isEmpty());
533      }
534 <        
534 >
535 >    /**
536 >     * An add following remove(x) succeeds
537 >     */
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      /**
549       * contains(x) reports true when elements added but not yet removed
550       */
# Line 638 | Line 568 | public class LinkedBlockingQueueTest ext
568          assertEquals(SIZE, q.remainingCapacity());
569          q.add(one);
570          assertFalse(q.isEmpty());
571 +        assertTrue(q.contains(one));
572          q.clear();
573          assertTrue(q.isEmpty());
574      }
# Line 694 | 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();
631 <        try {
632 <        for(int i = 0; i < o.length; i++)
702 <            assertEquals(o[i], q.take());
703 <        } catch (InterruptedException e){
704 <            unexpectedException();
705 <        }    
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          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++)
717 <                assertEquals(ints[i], q.take());
718 <        } catch (InterruptedException e){
719 <            unexpectedException();
720 <        }    
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 {
650 >        try {
651              LinkedBlockingQueue q = populatedQueue(SIZE);
652 <            Object o[] = q.toArray(null);
653 <            shouldThrow();
654 <        } catch(NullPointerException success){}
652 >            Object o[] = q.toArray(null);
653 >            shouldThrow();
654 >        } catch (NullPointerException success) {}
655      }
656  
657      /**
658 <     * toArray with incompatable array type throws CCE
658 >     * toArray with incompatible array type throws CCE
659       */
660      public void testToArray1_BadArg() {
661 <        try {
661 >        try {
662              LinkedBlockingQueue q = populatedQueue(SIZE);
663 <            Object o[] = q.toArray(new String[10] );
664 <            shouldThrow();
665 <        } catch(ArrayStoreException  success){}
663 >            Object o[] = q.toArray(new String[10] );
664 >            shouldThrow();
665 >        } catch (ArrayStoreException success) {}
666      }
667  
668 <    
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()){
677 <                assertEquals(it.next(), q.take());
755 <            }
756 <        } catch (InterruptedException e){
757 <            unexpectedException();
758 <        }    
674 >        Iterator it = q.iterator();
675 >        while (it.hasNext()) {
676 >            assertEquals(it.next(), q.take());
677 >        }
678      }
679  
680      /**
# Line 770 | Line 689 | public class LinkedBlockingQueueTest ext
689          Iterator it = q.iterator();
690          it.next();
691          it.remove();
692 <        
692 >
693          it = q.iterator();
694          assertEquals(it.next(), one);
695          assertEquals(it.next(), three);
# Line 803 | Line 722 | public class LinkedBlockingQueueTest ext
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();
809 <                it.next();
810 <            }
811 <        }
812 <        catch (ConcurrentModificationException e) {
813 <            unexpectedException();
725 >        for (Iterator it = q.iterator(); it.hasNext();) {
726 >            q.remove();
727 >            it.next();
728          }
729          assertEquals(0, q.size());
730      }
# Line 825 | Line 739 | public class LinkedBlockingQueueTest ext
739          for (int i = 0; i < SIZE; ++i) {
740              assertTrue(s.indexOf(String.valueOf(i)) >= 0);
741          }
742 <    }        
742 >    }
743  
744  
745      /**
# Line 836 | Line 750 | public class LinkedBlockingQueueTest ext
750          q.add(one);
751          q.add(two);
752          ExecutorService executor = Executors.newFixedThreadPool(2);
753 <        executor.execute(new Runnable() {
754 <            public void run() {
753 >        executor.execute(new CheckedRunnable() {
754 >            public void realRun() throws InterruptedException {
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 <        });
756 >                threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
757 >                threadAssertEquals(0, q.remainingCapacity());
758 >            }});
759 >
760 >        executor.execute(new CheckedRunnable() {
761 >            public void realRun() throws InterruptedException {
762 >                Thread.sleep(SMALL_DELAY_MS);
763 >                threadAssertEquals(one, q.take());
764 >            }});
765  
852        executor.execute(new Runnable() {
853            public void run() {
854                try {
855                    Thread.sleep(SMALL_DELAY_MS);
856                    threadAssertEquals(one, q.take());
857                }
858                catch (InterruptedException e) {
859                    threadUnexpectedException();
860                }
861            }
862        });
863        
766          joinPool(executor);
767      }
768  
# Line 870 | Line 772 | public class LinkedBlockingQueueTest ext
772      public void testPollInExecutor() {
773          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
774          ExecutorService executor = Executors.newFixedThreadPool(2);
775 <        executor.execute(new Runnable() {
776 <            public void run() {
775 >        executor.execute(new CheckedRunnable() {
776 >            public void realRun() throws InterruptedException {
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 <        });
778 >                threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
779 >                threadAssertTrue(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  
886        executor.execute(new Runnable() {
887            public void run() {
888                try {
889                    Thread.sleep(SMALL_DELAY_MS);
890                    q.put(one);
891                }
892                catch (InterruptedException e) {
893                    threadUnexpectedException();
894                }
895            }
896        });
897        
788          joinPool(executor);
789      }
790  
791      /**
792       * A deserialized serialized queue has same elements in same order
793       */
794 <    public void testSerialization() {
794 >    public void testSerialization() throws Exception {
795          LinkedBlockingQueue 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 <            LinkedBlockingQueue r = (LinkedBlockingQueue)in.readObject();
806 <            assertEquals(q.size(), r.size());
807 <            while (!q.isEmpty())
918 <                assertEquals(q.remove(), r.remove());
919 <        } catch(Exception e){
920 <            unexpectedException();
921 <        }
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())
807 >            assertEquals(q.remove(), r.remove());
808      }
809  
810      /**
811       * drainTo(null) throws NPE
812 <     */
812 >     */
813      public void testDrainToNull() {
814          LinkedBlockingQueue q = populatedQueue(SIZE);
815          try {
816              q.drainTo(null);
817              shouldThrow();
818 <        } catch(NullPointerException success) {
933 <        }
818 >        } catch (NullPointerException success) {}
819      }
820  
821      /**
822       * drainTo(this) throws IAE
823 <     */
823 >     */
824      public void testDrainToSelf() {
825          LinkedBlockingQueue q = populatedQueue(SIZE);
826          try {
827              q.drainTo(q);
828              shouldThrow();
829 <        } catch(IllegalArgumentException success) {
945 <        }
829 >        } catch (IllegalArgumentException success) {}
830      }
831  
832      /**
833       * drainTo(c) empties queue into another collection c
834 <     */
834 >     */
835      public void testDrainTo() {
836          LinkedBlockingQueue q = populatedQueue(SIZE);
837          ArrayList l = new ArrayList();
838          q.drainTo(l);
839          assertEquals(q.size(), 0);
840          assertEquals(l.size(), SIZE);
841 <        for (int i = 0; i < SIZE; ++i)
841 >        for (int i = 0; i < SIZE; ++i)
842 >            assertEquals(l.get(i), new Integer(i));
843 >        q.add(zero);
844 >        q.add(one);
845 >        assertFalse(q.isEmpty());
846 >        assertTrue(q.contains(zero));
847 >        assertTrue(q.contains(one));
848 >        l.clear();
849 >        q.drainTo(l);
850 >        assertEquals(q.size(), 0);
851 >        assertEquals(l.size(), 2);
852 >        for (int i = 0; i < 2; ++i)
853              assertEquals(l.get(i), new Integer(i));
854      }
855  
856      /**
857       * drainTo empties full queue, unblocking a waiting put.
858 <     */
859 <    public void testDrainToWithActivePut() {
858 >     */
859 >    public void testDrainToWithActivePut() throws InterruptedException {
860          final LinkedBlockingQueue 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);
979 <            assertTrue(l.size() >= SIZE);
980 <            for (int i = 0; i < SIZE; ++i)
981 <                assertEquals(l.get(i), new Integer(i));
982 <            t.join();
983 <            assertTrue(q.size() + l.size() == SIZE+1);
984 <        } catch(Exception e){
985 <            unexpectedException();
986 <        }
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      /**
877       * drainTo(null, n) throws NPE
878 <     */
878 >     */
879      public void testDrainToNullN() {
880          LinkedBlockingQueue q = populatedQueue(SIZE);
881          try {
882              q.drainTo(null, 0);
883              shouldThrow();
884 <        } catch(NullPointerException success) {
998 <        }
884 >        } catch (NullPointerException success) {}
885      }
886  
887      /**
888       * drainTo(this, n) throws IAE
889 <     */
889 >     */
890      public void testDrainToSelfN() {
891          LinkedBlockingQueue q = populatedQueue(SIZE);
892          try {
893              q.drainTo(q, 0);
894              shouldThrow();
895 <        } catch(IllegalArgumentException success) {
1010 <        }
895 >        } catch (IllegalArgumentException success) {}
896      }
897  
898      /**
899       * drainTo(c, n) empties first max {n, size} elements of queue into c
900 <     */
900 >     */
901      public void testDrainToN() {
902 +        LinkedBlockingQueue q = new LinkedBlockingQueue();
903          for (int i = 0; i < SIZE + 2; ++i) {
904 <            LinkedBlockingQueue q = populatedQueue(SIZE);
904 >            for (int j = 0; j < SIZE; j++)
905 >                assertTrue(q.offer(new Integer(j)));
906              ArrayList l = new ArrayList();
907              q.drainTo(l, i);
908              int k = (i < SIZE)? i : SIZE;
1022            assertEquals(q.size(), SIZE-k);
909              assertEquals(l.size(), k);
910 <            for (int j = 0; j < k; ++j)
910 >            assertEquals(q.size(), SIZE-k);
911 >            for (int j = 0; j < k; ++j)
912                  assertEquals(l.get(j), new Integer(j));
913 +            while (q.poll() != null) ;
914          }
915      }
916  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines