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.41 by jsr166, Thu Apr 14 22:55:08 2011 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   * Other contributors include Andrew Wright, Jeffrey Hayes,
6   * Pat Fisher, Mike Judd.
7   */
# 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 27 | Line 41 | public class LinkedBlockingQueueTest ext
41       * Create a queue of given size containing consecutive
42       * Integers 0 ... n.
43       */
44 <    private LinkedBlockingQueue populatedQueue(int n) {
45 <        LinkedBlockingQueue q = new LinkedBlockingQueue(n);
44 >    private LinkedBlockingQueue<Integer> populatedQueue(int n) {
45 >        LinkedBlockingQueue<Integer> q =
46 >            new LinkedBlockingQueue<Integer>(n);
47          assertTrue(q.isEmpty());
48          for (int i = 0; i < n; i++)
49              assertTrue(q.offer(new Integer(i)));
# Line 48 | Line 63 | public class LinkedBlockingQueueTest ext
63      }
64  
65      /**
66 <     * Constructor throws IAE if  capacity argument nonpositive
66 >     * Constructor throws IAE if capacity argument nonpositive
67       */
68      public void testConstructor2() {
69          try {
70              LinkedBlockingQueue q = new LinkedBlockingQueue(0);
71              shouldThrow();
72 <        }
58 <        catch (IllegalArgumentException success) {}
72 >        } catch (IllegalArgumentException success) {}
73      }
74  
75      /**
# Line 65 | Line 79 | public class LinkedBlockingQueueTest ext
79          try {
80              LinkedBlockingQueue q = new LinkedBlockingQueue(null);
81              shouldThrow();
82 <        }
69 <        catch (NullPointerException success) {}
82 >        } catch (NullPointerException success) {}
83      }
84  
85      /**
# Line 77 | Line 90 | public class LinkedBlockingQueueTest ext
90              Integer[] ints = new Integer[SIZE];
91              LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints));
92              shouldThrow();
93 <        }
81 <        catch (NullPointerException success) {}
93 >        } catch (NullPointerException success) {}
94      }
95  
96      /**
# Line 91 | Line 103 | public class LinkedBlockingQueueTest ext
103                  ints[i] = new Integer(i);
104              LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints));
105              shouldThrow();
106 <        }
95 <        catch (NullPointerException success) {}
106 >        } catch (NullPointerException success) {}
107      }
108  
109      /**
110       * Queue contains all elements of collection used to initialize
111       */
112      public void testConstructor6() {
113 <        try {
114 <            Integer[] ints = new Integer[SIZE];
115 <            for (int i = 0; i < SIZE; ++i)
116 <                ints[i] = new Integer(i);
117 <            LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints));
118 <            for (int i = 0; i < SIZE; ++i)
108 <                assertEquals(ints[i], q.poll());
109 <        }
110 <        finally {}
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)
118 >            assertEquals(ints[i], q.poll());
119      }
120  
121      /**
# Line 150 | Line 158 | public class LinkedBlockingQueueTest ext
158              LinkedBlockingQueue q = new LinkedBlockingQueue(1);
159              q.offer(null);
160              shouldThrow();
161 <        } catch (NullPointerException success) { }
161 >        } catch (NullPointerException success) {}
162      }
163  
164      /**
# Line 161 | Line 169 | public class LinkedBlockingQueueTest ext
169              LinkedBlockingQueue q = new LinkedBlockingQueue(1);
170              q.add(null);
171              shouldThrow();
172 <        } catch (NullPointerException success) { }
172 >        } catch (NullPointerException success) {}
173      }
174  
175      /**
# Line 184 | Line 192 | public class LinkedBlockingQueueTest ext
192              }
193              assertEquals(0, q.remainingCapacity());
194              q.add(new Integer(SIZE));
195 <        } catch (IllegalStateException success) {
196 <        }
195 >            shouldThrow();
196 >        } catch (IllegalStateException success) {}
197      }
198  
199      /**
# Line 196 | Line 204 | public class LinkedBlockingQueueTest ext
204              LinkedBlockingQueue q = new LinkedBlockingQueue(1);
205              q.addAll(null);
206              shouldThrow();
207 <        }
200 <        catch (NullPointerException success) {}
207 >        } catch (NullPointerException success) {}
208      }
209  
210      /**
# Line 208 | Line 215 | public class LinkedBlockingQueueTest ext
215              LinkedBlockingQueue q = populatedQueue(SIZE);
216              q.addAll(q);
217              shouldThrow();
218 <        }
212 <        catch (IllegalArgumentException success) {}
218 >        } catch (IllegalArgumentException success) {}
219      }
220  
221      /**
# Line 221 | Line 227 | public class LinkedBlockingQueueTest ext
227              Integer[] ints = new Integer[SIZE];
228              q.addAll(Arrays.asList(ints));
229              shouldThrow();
230 <        }
225 <        catch (NullPointerException success) {}
230 >        } catch (NullPointerException success) {}
231      }
232 +
233      /**
234       * addAll of a collection with any null elements throws NPE after
235       * possibly adding some elements
# Line 236 | Line 242 | public class LinkedBlockingQueueTest ext
242                  ints[i] = new Integer(i);
243              q.addAll(Arrays.asList(ints));
244              shouldThrow();
245 <        }
240 <        catch (NullPointerException success) {}
245 >        } catch (NullPointerException success) {}
246      }
247 +
248      /**
249       * addAll throws ISE if not enough room
250       */
# Line 250 | Line 256 | public class LinkedBlockingQueueTest ext
256                  ints[i] = new Integer(i);
257              q.addAll(Arrays.asList(ints));
258              shouldThrow();
259 <        }
254 <        catch (IllegalStateException success) {}
259 >        } catch (IllegalStateException success) {}
260      }
261 +
262      /**
263       * Queue contains all elements, in traversal order, of successful addAll
264       */
265      public void testAddAll5() {
266 <        try {
267 <            Integer[] empty = new Integer[0];
268 <            Integer[] ints = new Integer[SIZE];
269 <            for (int i = 0; i < SIZE; ++i)
270 <                ints[i] = new Integer(i);
271 <            LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
272 <            assertFalse(q.addAll(Arrays.asList(empty)));
273 <            assertTrue(q.addAll(Arrays.asList(ints)));
274 <            for (int i = 0; i < SIZE; ++i)
269 <                assertEquals(ints[i], q.poll());
270 <        }
271 <        finally {}
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)
274 >            assertEquals(ints[i], q.poll());
275      }
276  
277      /**
278       * put(null) throws NPE
279       */
280 <     public void testPutNull() {
280 >    public void testPutNull() throws InterruptedException {
281          try {
282              LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
283              q.put(null);
284              shouldThrow();
285 <        }
286 <        catch (NullPointerException success) {
284 <        }
285 <        catch (InterruptedException ie) {
286 <            unexpectedException();
287 <        }
288 <     }
285 >        } catch (NullPointerException success) {}
286 >    }
287  
288      /**
289       * all elements successfully put are contained
290       */
291 <     public void testPut() {
292 <         try {
293 <             LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
294 <             for (int i = 0; i < SIZE; ++i) {
295 <                 Integer I = new Integer(i);
296 <                 q.put(I);
299 <                 assertTrue(q.contains(I));
300 <             }
301 <             assertEquals(0, q.remainingCapacity());
302 <         }
303 <        catch (InterruptedException ie) {
304 <            unexpectedException();
291 >    public void testPut() throws InterruptedException {
292 >        LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
293 >        for (int i = 0; i < SIZE; ++i) {
294 >            Integer I = new Integer(i);
295 >            q.put(I);
296 >            assertTrue(q.contains(I));
297          }
298 +        assertEquals(0, q.remainingCapacity());
299      }
300  
301      /**
302       * put blocks interruptibly if full
303       */
304 <    public void testBlockingPut() {
305 <        Thread t = new Thread(new Runnable() {
306 <                public void run() {
307 <                    int added = 0;
308 <                    try {
309 <                        LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
310 <                        for (int i = 0; i < SIZE; ++i) {
311 <                            q.put(new Integer(i));
312 <                            ++added;
313 <                        }
314 <                        q.put(new Integer(SIZE));
315 <                        threadShouldThrow();
316 <                    } catch (InterruptedException ie) {
317 <                        threadAssertEquals(added, SIZE);
325 <                    }
326 <                }});
304 >    public void testBlockingPut() throws InterruptedException {
305 >        final LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
306 >        Thread t = new Thread(new CheckedRunnable() {
307 >            public void realRun() throws InterruptedException {
308 >                for (int i = 0; i < SIZE; ++i)
309 >                    q.put(i);
310 >                assertEquals(SIZE, q.size());
311 >                assertEquals(0, q.remainingCapacity());
312 >                try {
313 >                    q.put(99);
314 >                    shouldThrow();
315 >                } catch (InterruptedException success) {}
316 >            }});
317 >
318          t.start();
319 <        try {
320 <           Thread.sleep(SHORT_DELAY_MS);
321 <           t.interrupt();
322 <           t.join();
323 <        }
333 <        catch (InterruptedException ie) {
334 <            unexpectedException();
335 <        }
319 >        Thread.sleep(SHORT_DELAY_MS);
320 >        t.interrupt();
321 >        t.join();
322 >        assertEquals(SIZE, q.size());
323 >        assertEquals(0, q.remainingCapacity());
324      }
325  
326      /**
327       * put blocks waiting for take when full
328       */
329 <    public void testPutWithTake() {
329 >    public void testPutWithTake() throws InterruptedException {
330 >        final int capacity = 2;
331          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
332 <        Thread t = new Thread(new Runnable() {
333 <                public void run() {
334 <                    int added = 0;
335 <                    try {
336 <                        q.put(new Object());
337 <                        ++added;
338 <                        q.put(new Object());
339 <                        ++added;
340 <                        q.put(new Object());
341 <                        ++added;
342 <                        q.put(new Object());
343 <                        ++added;
344 <                        threadShouldThrow();
345 <                    } catch (InterruptedException e) {
346 <                        threadAssertTrue(added >= 2);
347 <                    }
348 <                }
349 <            });
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 <        }
332 >        Thread t = new Thread(new CheckedRunnable() {
333 >            public void realRun() throws InterruptedException {
334 >                for (int i = 0; i < capacity + 1; i++)
335 >                    q.put(i);
336 >                try {
337 >                    q.put(99);
338 >                    shouldThrow();
339 >                } catch (InterruptedException success) {}
340 >            }});
341 >
342 >        t.start();
343 >        Thread.sleep(SHORT_DELAY_MS);
344 >        assertEquals(q.remainingCapacity(), 0);
345 >        assertEquals(0, q.take());
346 >        Thread.sleep(SHORT_DELAY_MS);
347 >        t.interrupt();
348 >        t.join();
349 >        assertEquals(q.remainingCapacity(), 0);
350      }
351  
352      /**
353       * timed offer times out if full and elements not taken
354       */
355 <    public void testTimedOffer() {
355 >    public void testTimedOffer() throws InterruptedException {
356          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
357 <        Thread t = new Thread(new Runnable() {
358 <                public void run() {
359 <                    try {
360 <                        q.put(new Object());
361 <                        q.put(new Object());
362 <                        threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS));
363 <                        q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
364 <                        threadShouldThrow();
365 <                    } catch (InterruptedException success) {}
366 <                }
387 <            });
357 >        Thread t = new Thread(new CheckedRunnable() {
358 >            public void realRun() throws InterruptedException {
359 >                q.put(new Object());
360 >                q.put(new Object());
361 >                assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS));
362 >                try {
363 >                    q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
364 >                    shouldThrow();
365 >                } catch (InterruptedException success) {}
366 >            }});
367  
368 <        try {
369 <            t.start();
370 <            Thread.sleep(SMALL_DELAY_MS);
371 <            t.interrupt();
393 <            t.join();
394 <        } catch (Exception e) {
395 <            unexpectedException();
396 <        }
368 >        t.start();
369 >        Thread.sleep(SMALL_DELAY_MS);
370 >        t.interrupt();
371 >        t.join();
372      }
373  
374      /**
375       * take retrieves elements in FIFO order
376       */
377 <    public void testTake() {
378 <        try {
379 <            LinkedBlockingQueue q = populatedQueue(SIZE);
380 <            for (int i = 0; i < SIZE; ++i) {
406 <                assertEquals(i, ((Integer)q.take()).intValue());
407 <            }
408 <        } catch (InterruptedException e) {
409 <            unexpectedException();
377 >    public void testTake() throws InterruptedException {
378 >        LinkedBlockingQueue q = populatedQueue(SIZE);
379 >        for (int i = 0; i < SIZE; ++i) {
380 >            assertEquals(i, q.take());
381          }
382      }
383  
384      /**
385 <     * take blocks interruptibly when empty
385 >     * Take removes existing elements until empty, then blocks interruptibly
386       */
387 <    public void testTakeFromEmpty() {
388 <        final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
389 <        Thread t = new Thread(new Runnable() {
390 <                public void run() {
391 <                    try {
392 <                        q.take();
422 <                        threadShouldThrow();
423 <                    } catch (InterruptedException success) { }
387 >    public void testBlockingTake() throws InterruptedException {
388 >        final LinkedBlockingQueue q = populatedQueue(SIZE);
389 >        Thread t = new Thread(new CheckedRunnable() {
390 >            public void realRun() throws InterruptedException {
391 >                for (int i = 0; i < SIZE; ++i) {
392 >                    assertEquals(i, q.take());
393                  }
394 <            });
395 <        try {
396 <            t.start();
397 <            Thread.sleep(SHORT_DELAY_MS);
398 <            t.interrupt();
430 <            t.join();
431 <        } catch (Exception e) {
432 <            unexpectedException();
433 <        }
434 <    }
394 >                try {
395 >                    q.take();
396 >                    shouldThrow();
397 >                } catch (InterruptedException success) {}
398 >            }});
399  
436    /**
437     * Take removes existing elements until empty, then blocks interruptibly
438     */
439    public void testBlockingTake() {
440        Thread t = new Thread(new Runnable() {
441                public void run() {
442                    try {
443                        LinkedBlockingQueue q = populatedQueue(SIZE);
444                        for (int i = 0; i < SIZE; ++i) {
445                            assertEquals(i, ((Integer)q.take()).intValue());
446                        }
447                        q.take();
448                        threadShouldThrow();
449                    } catch (InterruptedException success) {
450                    }
451                }});
400          t.start();
401 <        try {
402 <           Thread.sleep(SHORT_DELAY_MS);
403 <           t.interrupt();
456 <           t.join();
457 <        }
458 <        catch (InterruptedException ie) {
459 <            unexpectedException();
460 <        }
401 >        Thread.sleep(SHORT_DELAY_MS);
402 >        t.interrupt();
403 >        t.join();
404      }
405  
463
406      /**
407       * poll succeeds unless empty
408       */
409      public void testPoll() {
410          LinkedBlockingQueue q = populatedQueue(SIZE);
411          for (int i = 0; i < SIZE; ++i) {
412 <            assertEquals(i, ((Integer)q.poll()).intValue());
412 >            assertEquals(i, q.poll());
413          }
414          assertNull(q.poll());
415      }
416  
417      /**
418 <     * timed pool with zero timeout succeeds when non-empty, else times out
418 >     * timed poll with zero timeout succeeds when non-empty, else times out
419       */
420 <    public void testTimedPoll0() {
421 <        try {
422 <            LinkedBlockingQueue q = populatedQueue(SIZE);
423 <            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();
420 >    public void testTimedPoll0() throws InterruptedException {
421 >        LinkedBlockingQueue q = populatedQueue(SIZE);
422 >        for (int i = 0; i < SIZE; ++i) {
423 >            assertEquals(i, q.poll(0, MILLISECONDS));
424          }
425 +        assertNull(q.poll(0, MILLISECONDS));
426      }
427  
428      /**
429 <     * timed pool with nonzero timeout succeeds when non-empty, else times out
429 >     * timed poll with nonzero timeout succeeds when non-empty, else times out
430       */
431 <    public void testTimedPoll() {
432 <        try {
433 <            LinkedBlockingQueue q = populatedQueue(SIZE);
434 <            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();
431 >    public void testTimedPoll() throws InterruptedException {
432 >        LinkedBlockingQueue q = populatedQueue(SIZE);
433 >        for (int i = 0; i < SIZE; ++i) {
434 >            assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
435          }
436 +        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
437      }
438  
439      /**
440       * Interrupted timed poll throws InterruptedException instead of
441       * returning timeout status
442       */
443 <    public void testInterruptedTimedPoll() {
444 <        Thread t = new Thread(new Runnable() {
445 <                public void run() {
446 <                    try {
447 <                        LinkedBlockingQueue q = populatedQueue(SIZE);
448 <                        for (int i = 0; i < SIZE; ++i) {
449 <                            threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
450 <                        }
451 <                        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) { }
443 >    public void testInterruptedTimedPoll() throws InterruptedException {
444 >        final BlockingQueue<Integer> q = populatedQueue(SIZE);
445 >        final CountDownLatch aboutToWait = new CountDownLatch(1);
446 >        Thread t = newStartedThread(new CheckedRunnable() {
447 >            public void realRun() throws InterruptedException {
448 >                for (int i = 0; i < SIZE; ++i) {
449 >                    long t0 = System.nanoTime();
450 >                    assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
451 >                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
452                  }
453 <            });
454 <        try {
455 <            t.start();
456 <            Thread.sleep(SMALL_DELAY_MS);
457 <            assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
458 <            t.interrupt();
459 <            t.join();
460 <        } catch (Exception e) {
461 <            unexpectedException();
462 <        }
453 >                long t0 = System.nanoTime();
454 >                aboutToWait.countDown();
455 >                try {
456 >                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
457 >                    shouldThrow();
458 >                } catch (InterruptedException success) {
459 >                    assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
460 >                }
461 >            }});
462 >
463 >        aboutToWait.await();
464 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
465 >        t.interrupt();
466 >        awaitTermination(t, MEDIUM_DELAY_MS);
467 >        checkEmpty(q);
468      }
469  
470      /**
# Line 562 | Line 473 | public class LinkedBlockingQueueTest ext
473      public void testPeek() {
474          LinkedBlockingQueue q = populatedQueue(SIZE);
475          for (int i = 0; i < SIZE; ++i) {
476 <            assertEquals(i, ((Integer)q.peek()).intValue());
477 <            q.poll();
476 >            assertEquals(i, q.peek());
477 >            assertEquals(i, q.poll());
478              assertTrue(q.peek() == null ||
479 <                       i != ((Integer)q.peek()).intValue());
479 >                       !q.peek().equals(i));
480          }
481          assertNull(q.peek());
482      }
# Line 576 | Line 487 | public class LinkedBlockingQueueTest ext
487      public void testElement() {
488          LinkedBlockingQueue q = populatedQueue(SIZE);
489          for (int i = 0; i < SIZE; ++i) {
490 <            assertEquals(i, ((Integer)q.element()).intValue());
491 <            q.poll();
490 >            assertEquals(i, q.element());
491 >            assertEquals(i, q.poll());
492          }
493          try {
494              q.element();
495              shouldThrow();
496 <        }
586 <        catch (NoSuchElementException success) {}
496 >        } catch (NoSuchElementException success) {}
497      }
498  
499      /**
# Line 592 | Line 502 | public class LinkedBlockingQueueTest ext
502      public void testRemove() {
503          LinkedBlockingQueue q = populatedQueue(SIZE);
504          for (int i = 0; i < SIZE; ++i) {
505 <            assertEquals(i, ((Integer)q.remove()).intValue());
505 >            assertEquals(i, q.remove());
506          }
507          try {
508              q.remove();
509              shouldThrow();
510 <        } catch (NoSuchElementException success) {
601 <        }
510 >        } catch (NoSuchElementException success) {}
511      }
512  
513      /**
# Line 607 | Line 516 | public class LinkedBlockingQueueTest ext
516      public void testRemoveElement() {
517          LinkedBlockingQueue q = populatedQueue(SIZE);
518          for (int i = 1; i < SIZE; i+=2) {
519 <            assertTrue(q.remove(new Integer(i)));
519 >            assertTrue(q.contains(i));
520 >            assertTrue(q.remove(i));
521 >            assertFalse(q.contains(i));
522 >            assertTrue(q.contains(i-1));
523          }
524          for (int i = 0; i < SIZE; i+=2) {
525 <            assertTrue(q.remove(new Integer(i)));
526 <            assertFalse(q.remove(new Integer(i+1)));
525 >            assertTrue(q.contains(i));
526 >            assertTrue(q.remove(i));
527 >            assertFalse(q.contains(i));
528 >            assertFalse(q.remove(i+1));
529 >            assertFalse(q.contains(i+1));
530          }
531          assertTrue(q.isEmpty());
532      }
# Line 619 | Line 534 | public class LinkedBlockingQueueTest ext
534      /**
535       * An add following remove(x) succeeds
536       */
537 <    public void testRemoveElementAndAdd() {
538 <        try {
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)));
630 <            assertTrue(q.take() != null);
631 <        } catch (Exception e) {
632 <            unexpectedException();
633 <        }
537 >    public void testRemoveElementAndAdd() throws InterruptedException {
538 >        LinkedBlockingQueue q = new LinkedBlockingQueue();
539 >        assertTrue(q.add(new Integer(1)));
540 >        assertTrue(q.add(new Integer(2)));
541 >        assertTrue(q.remove(new Integer(1)));
542 >        assertTrue(q.remove(new Integer(2)));
543 >        assertTrue(q.add(new Integer(3)));
544 >        assertTrue(q.take() != null);
545      }
546  
547      /**
# Line 711 | Line 622 | public class LinkedBlockingQueueTest ext
622      }
623  
624      /**
625 <     * toArray contains all elements
625 >     * toArray contains all elements in FIFO order
626       */
627      public void testToArray() {
628          LinkedBlockingQueue q = populatedQueue(SIZE);
629          Object[] o = q.toArray();
719        try {
630          for (int i = 0; i < o.length; i++)
631 <            assertEquals(o[i], q.take());
722 <        } catch (InterruptedException e) {
723 <            unexpectedException();
724 <        }
631 >            assertSame(o[i], q.poll());
632      }
633  
634      /**
635 <     * toArray(a) contains all elements
635 >     * toArray(a) contains all elements in FIFO order
636       */
637 <    public void testToArray2() {
638 <        LinkedBlockingQueue q = populatedQueue(SIZE);
637 >    public void testToArray2() throws InterruptedException {
638 >        LinkedBlockingQueue<Integer> q = populatedQueue(SIZE);
639          Integer[] ints = new Integer[SIZE];
640 <        ints = (Integer[])q.toArray(ints);
641 <        try {
642 <            for (int i = 0; i < ints.length; i++)
643 <                assertEquals(ints[i], q.take());
737 <        } catch (InterruptedException e) {
738 <            unexpectedException();
739 <        }
640 >        Integer[] array = q.toArray(ints);
641 >        assertSame(ints, array);
642 >        for (int i = 0; i < ints.length; i++)
643 >            assertSame(ints[i], q.poll());
644      }
645  
646      /**
647 <     * toArray(null) throws NPE
647 >     * toArray(null) throws NullPointerException
648       */
649 <    public void testToArray_BadArg() {
649 >    public void testToArray_NullArg() {
650 >        LinkedBlockingQueue q = populatedQueue(SIZE);
651          try {
652 <            LinkedBlockingQueue q = populatedQueue(SIZE);
748 <            Object o[] = q.toArray(null);
652 >            q.toArray(null);
653              shouldThrow();
654          } catch (NullPointerException success) {}
655      }
656  
657      /**
658 <     * toArray with incompatible array type throws CCE
658 >     * toArray(incompatible array type) throws ArrayStoreException
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 >            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