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.18 by jsr166, Sat Nov 21 10:25:05 2009 UTC vs.
Revision 1.43 by jsr166, Sat May 21 06:24:33 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 214 | Line 229 | public class LinkedBlockingQueueTest ext
229              shouldThrow();
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 228 | Line 244 | public class LinkedBlockingQueueTest ext
244              shouldThrow();
245          } catch (NullPointerException success) {}
246      }
247 +
248      /**
249       * addAll throws ISE if not enough room
250       */
# Line 241 | Line 258 | public class LinkedBlockingQueueTest ext
258              shouldThrow();
259          } catch (IllegalStateException success) {}
260      }
261 +
262      /**
263       * Queue contains all elements, in traversal order, of successful addAll
264       */
# Line 259 | Line 277 | public class LinkedBlockingQueueTest ext
277      /**
278       * put(null) throws NPE
279       */
280 <     public void testPutNull() throws InterruptedException {
280 >    public void testPutNull() throws InterruptedException {
281          try {
282              LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
283              q.put(null);
284              shouldThrow();
285          } catch (NullPointerException success) {}
286 <     }
286 >    }
287  
288      /**
289       * all elements successfully put are contained
# Line 284 | Line 302 | public class LinkedBlockingQueueTest ext
302       * put blocks interruptibly if full
303       */
304      public void testBlockingPut() throws InterruptedException {
305 +        final LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
306          Thread t = new Thread(new CheckedRunnable() {
307 <            public void realRun() {
308 <                int added = 0;
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 <                    LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
314 <                    for (int i = 0; i < SIZE; ++i) {
315 <                        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 <                }
313 >                    q.put(99);
314 >                    shouldThrow();
315 >                } catch (InterruptedException success) {}
316              }});
317  
318          t.start();
319 <        Thread.sleep(SHORT_DELAY_MS);
319 >        delay(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() throws InterruptedException {
330 +        final int capacity = 2;
331          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
332          Thread t = new Thread(new CheckedRunnable() {
333 <            public void realRun() {
334 <                int added = 0;
333 >            public void realRun() throws InterruptedException {
334 >                for (int i = 0; i < capacity + 1; i++)
335 >                    q.put(i);
336                  try {
337 <                    q.put(new Object());
338 <                    ++added;
339 <                    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 <                }
337 >                    q.put(99);
338 >                    shouldThrow();
339 >                } catch (InterruptedException success) {}
340              }});
341  
342          t.start();
343 <        Thread.sleep(SHORT_DELAY_MS);
344 <        q.take();
343 >        delay(SHORT_DELAY_MS);
344 >        assertEquals(q.remainingCapacity(), 0);
345 >        assertEquals(0, q.take());
346 >        delay(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() throws InterruptedException {
355 >    public void testTimedOffer() {
356          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
357 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
357 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
358 >        Thread t = newStartedThread(new CheckedRunnable() {
359              public void realRun() throws InterruptedException {
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 <            }};
362 >                long startTime = System.nanoTime();
363 >                assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS));
364 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
365 >                pleaseInterrupt.countDown();
366 >                try {
367 >                    q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
368 >                    shouldThrow();
369 >                } catch (InterruptedException success) {}
370 >            }});
371  
372 <        t.start();
353 <        Thread.sleep(SMALL_DELAY_MS);
372 >        await(pleaseInterrupt);
373          t.interrupt();
374 <        t.join();
374 >        awaitTermination(t);
375      }
376  
377      /**
# Line 361 | Line 380 | public class LinkedBlockingQueueTest ext
380      public void testTake() throws InterruptedException {
381          LinkedBlockingQueue q = populatedQueue(SIZE);
382          for (int i = 0; i < SIZE; ++i) {
383 <            assertEquals(i, ((Integer)q.take()).intValue());
383 >            assertEquals(i, q.take());
384          }
385      }
386  
387      /**
369     * take blocks interruptibly when empty
370     */
371    public void testTakeFromEmpty() throws InterruptedException {
372        final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
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    /**
388       * Take removes existing elements until empty, then blocks interruptibly
389       */
390      public void testBlockingTake() throws InterruptedException {
391 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
391 >        final LinkedBlockingQueue q = populatedQueue(SIZE);
392 >        Thread t = new Thread(new CheckedRunnable() {
393              public void realRun() throws InterruptedException {
390                LinkedBlockingQueue q = populatedQueue(SIZE);
394                  for (int i = 0; i < SIZE; ++i) {
395 <                    assertEquals(i, ((Integer)q.take()).intValue());
395 >                    assertEquals(i, q.take());
396                  }
397 <                q.take();
398 <            }};
397 >                try {
398 >                    q.take();
399 >                    shouldThrow();
400 >                } catch (InterruptedException success) {}
401 >            }});
402  
403          t.start();
404 <        Thread.sleep(SHORT_DELAY_MS);
404 >        delay(SHORT_DELAY_MS);
405          t.interrupt();
406          t.join();
407      }
408  
403
409      /**
410       * poll succeeds unless empty
411       */
412      public void testPoll() {
413          LinkedBlockingQueue q = populatedQueue(SIZE);
414          for (int i = 0; i < SIZE; ++i) {
415 <            assertEquals(i, ((Integer)q.poll()).intValue());
415 >            assertEquals(i, q.poll());
416          }
417          assertNull(q.poll());
418      }
419  
420      /**
421 <     * timed pool with zero timeout succeeds when non-empty, else times out
421 >     * timed poll with zero timeout succeeds when non-empty, else times out
422       */
423      public void testTimedPoll0() throws InterruptedException {
424          LinkedBlockingQueue q = populatedQueue(SIZE);
425          for (int i = 0; i < SIZE; ++i) {
426 <            assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue());
426 >            assertEquals(i, q.poll(0, MILLISECONDS));
427          }
428          assertNull(q.poll(0, MILLISECONDS));
429      }
430  
431      /**
432 <     * timed pool with nonzero timeout succeeds when non-empty, else times out
432 >     * timed poll with nonzero timeout succeeds when non-empty, else times out
433       */
434      public void testTimedPoll() throws InterruptedException {
435          LinkedBlockingQueue q = populatedQueue(SIZE);
436          for (int i = 0; i < SIZE; ++i) {
437 <            assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
437 >            assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
438          }
439          assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
440      }
# Line 439 | Line 444 | public class LinkedBlockingQueueTest ext
444       * returning timeout status
445       */
446      public void testInterruptedTimedPoll() throws InterruptedException {
447 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
447 >        final BlockingQueue<Integer> q = populatedQueue(SIZE);
448 >        final CountDownLatch aboutToWait = new CountDownLatch(1);
449 >        Thread t = newStartedThread(new CheckedRunnable() {
450              public void realRun() throws InterruptedException {
444                LinkedBlockingQueue q = populatedQueue(SIZE);
451                  for (int i = 0; i < SIZE; ++i) {
452 <                    threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
452 >                    long t0 = System.nanoTime();
453 >                    assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
454 >                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
455                  }
456 <                q.poll(SMALL_DELAY_MS, MILLISECONDS);
457 <            }};
458 <
459 <        t.start();
460 <        Thread.sleep(SHORT_DELAY_MS);
461 <        t.interrupt();
462 <        t.join();
463 <    }
464 <
457 <    /**
458 <     *  timed poll before a delayed offer fails; after offer succeeds;
459 <     *  on interruption throws
460 <     */
461 <    public void testTimedPollWithOffer() throws InterruptedException {
462 <        final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
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 <            }};
456 >                long t0 = System.nanoTime();
457 >                aboutToWait.countDown();
458 >                try {
459 >                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
460 >                    shouldThrow();
461 >                } catch (InterruptedException success) {
462 >                    assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
463 >                }
464 >            }});
465  
466 <        t.start();
467 <        Thread.sleep(SMALL_DELAY_MS);
472 <        assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
466 >        aboutToWait.await();
467 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
468          t.interrupt();
469 <        t.join();
469 >        awaitTermination(t, MEDIUM_DELAY_MS);
470 >        checkEmpty(q);
471      }
472  
473      /**
# Line 480 | Line 476 | public class LinkedBlockingQueueTest ext
476      public void testPeek() {
477          LinkedBlockingQueue q = populatedQueue(SIZE);
478          for (int i = 0; i < SIZE; ++i) {
479 <            assertEquals(i, ((Integer)q.peek()).intValue());
480 <            q.poll();
479 >            assertEquals(i, q.peek());
480 >            assertEquals(i, q.poll());
481              assertTrue(q.peek() == null ||
482 <                       i != ((Integer)q.peek()).intValue());
482 >                       !q.peek().equals(i));
483          }
484          assertNull(q.peek());
485      }
# Line 494 | Line 490 | public class LinkedBlockingQueueTest ext
490      public void testElement() {
491          LinkedBlockingQueue q = populatedQueue(SIZE);
492          for (int i = 0; i < SIZE; ++i) {
493 <            assertEquals(i, ((Integer)q.element()).intValue());
494 <            q.poll();
493 >            assertEquals(i, q.element());
494 >            assertEquals(i, q.poll());
495          }
496          try {
497              q.element();
# Line 509 | Line 505 | public class LinkedBlockingQueueTest ext
505      public void testRemove() {
506          LinkedBlockingQueue q = populatedQueue(SIZE);
507          for (int i = 0; i < SIZE; ++i) {
508 <            assertEquals(i, ((Integer)q.remove()).intValue());
508 >            assertEquals(i, q.remove());
509          }
510          try {
511              q.remove();
# Line 523 | Line 519 | public class LinkedBlockingQueueTest ext
519      public void testRemoveElement() {
520          LinkedBlockingQueue q = populatedQueue(SIZE);
521          for (int i = 1; i < SIZE; i+=2) {
522 <            assertTrue(q.remove(new Integer(i)));
522 >            assertTrue(q.contains(i));
523 >            assertTrue(q.remove(i));
524 >            assertFalse(q.contains(i));
525 >            assertTrue(q.contains(i-1));
526          }
527          for (int i = 0; i < SIZE; i+=2) {
528 <            assertTrue(q.remove(new Integer(i)));
529 <            assertFalse(q.remove(new Integer(i+1)));
528 >            assertTrue(q.contains(i));
529 >            assertTrue(q.remove(i));
530 >            assertFalse(q.contains(i));
531 >            assertFalse(q.remove(i+1));
532 >            assertFalse(q.contains(i+1));
533          }
534          assertTrue(q.isEmpty());
535      }
# Line 623 | Line 625 | public class LinkedBlockingQueueTest ext
625      }
626  
627      /**
628 <     * toArray contains all elements
628 >     * toArray contains all elements in FIFO order
629       */
630 <    public void testToArray() throws InterruptedException {
630 >    public void testToArray() {
631          LinkedBlockingQueue q = populatedQueue(SIZE);
632          Object[] o = q.toArray();
633          for (int i = 0; i < o.length; i++)
634 <            assertEquals(o[i], q.take());
634 >            assertSame(o[i], q.poll());
635      }
636  
637      /**
638 <     * toArray(a) contains all elements
638 >     * toArray(a) contains all elements in FIFO order
639       */
640      public void testToArray2() throws InterruptedException {
641 <        LinkedBlockingQueue q = populatedQueue(SIZE);
641 >        LinkedBlockingQueue<Integer> q = populatedQueue(SIZE);
642          Integer[] ints = new Integer[SIZE];
643 <        ints = (Integer[])q.toArray(ints);
643 >        Integer[] array = q.toArray(ints);
644 >        assertSame(ints, array);
645          for (int i = 0; i < ints.length; i++)
646 <            assertEquals(ints[i], q.take());
646 >            assertSame(ints[i], q.poll());
647      }
648  
649      /**
650 <     * toArray(null) throws NPE
650 >     * toArray(null) throws NullPointerException
651       */
652 <    public void testToArray_BadArg() {
652 >    public void testToArray_NullArg() {
653 >        LinkedBlockingQueue q = populatedQueue(SIZE);
654          try {
655 <            LinkedBlockingQueue q = populatedQueue(SIZE);
652 <            Object o[] = q.toArray(null);
655 >            q.toArray(null);
656              shouldThrow();
657          } catch (NullPointerException success) {}
658      }
659  
660      /**
661 <     * toArray with incompatible array type throws CCE
661 >     * toArray(incompatible array type) throws ArrayStoreException
662       */
663      public void testToArray1_BadArg() {
664 +        LinkedBlockingQueue q = populatedQueue(SIZE);
665          try {
666 <            LinkedBlockingQueue q = populatedQueue(SIZE);
663 <            Object o[] = q.toArray(new String[10] );
666 >            q.toArray(new String[10]);
667              shouldThrow();
668          } catch (ArrayStoreException success) {}
669      }
# Line 680 | Line 683 | public class LinkedBlockingQueueTest ext
683      /**
684       * iterator.remove removes current element
685       */
686 <    public void testIteratorRemove () {
686 >    public void testIteratorRemove() {
687          final LinkedBlockingQueue q = new LinkedBlockingQueue(3);
688          q.add(two);
689          q.add(one);
# Line 691 | Line 694 | public class LinkedBlockingQueueTest ext
694          it.remove();
695  
696          it = q.iterator();
697 <        assertEquals(it.next(), one);
698 <        assertEquals(it.next(), three);
697 >        assertSame(it.next(), one);
698 >        assertSame(it.next(), three);
699          assertFalse(it.hasNext());
700      }
701  
# Line 708 | Line 711 | public class LinkedBlockingQueueTest ext
711          assertEquals(0, q.remainingCapacity());
712          int k = 0;
713          for (Iterator it = q.iterator(); it.hasNext();) {
714 <            int i = ((Integer)(it.next())).intValue();
712 <            assertEquals(++k, i);
714 >            assertEquals(++k, it.next());
715          }
716          assertEquals(3, k);
717      }
# Line 717 | Line 719 | public class LinkedBlockingQueueTest ext
719      /**
720       * Modifications do not cause iterators to fail
721       */
722 <    public void testWeaklyConsistentIteration () {
722 >    public void testWeaklyConsistentIteration() {
723          final LinkedBlockingQueue q = new LinkedBlockingQueue(3);
724          q.add(one);
725          q.add(two);
# Line 752 | Line 754 | public class LinkedBlockingQueueTest ext
754          ExecutorService executor = Executors.newFixedThreadPool(2);
755          executor.execute(new CheckedRunnable() {
756              public void realRun() throws InterruptedException {
757 <                threadAssertFalse(q.offer(three));
758 <                threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
759 <                threadAssertEquals(0, q.remainingCapacity());
757 >                assertFalse(q.offer(three));
758 >                assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
759 >                assertEquals(0, q.remainingCapacity());
760              }});
761  
762          executor.execute(new CheckedRunnable() {
763              public void realRun() throws InterruptedException {
764 <                Thread.sleep(SMALL_DELAY_MS);
765 <                threadAssertEquals(one, q.take());
764 >                delay(SMALL_DELAY_MS);
765 >                assertSame(one, q.take());
766              }});
767  
768          joinPool(executor);
# Line 774 | Line 776 | public class LinkedBlockingQueueTest ext
776          ExecutorService executor = Executors.newFixedThreadPool(2);
777          executor.execute(new CheckedRunnable() {
778              public void realRun() throws InterruptedException {
779 <                threadAssertNull(q.poll());
780 <                threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
781 <                threadAssertTrue(q.isEmpty());
779 >                assertNull(q.poll());
780 >                assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
781 >                assertTrue(q.isEmpty());
782              }});
783  
784          executor.execute(new CheckedRunnable() {
785              public void realRun() throws InterruptedException {
786 <                Thread.sleep(SMALL_DELAY_MS);
786 >                delay(SMALL_DELAY_MS);
787                  q.put(one);
788              }});
789  
# Line 896 | Line 898 | public class LinkedBlockingQueueTest ext
898      }
899  
900      /**
901 <     * drainTo(c, n) empties first max {n, size} elements of queue into c
901 >     * drainTo(c, n) empties first min(n, size) elements of queue into c
902       */
903      public void testDrainToN() {
904          LinkedBlockingQueue q = new LinkedBlockingQueue();
# Line 905 | Line 907 | public class LinkedBlockingQueueTest ext
907                  assertTrue(q.offer(new Integer(j)));
908              ArrayList l = new ArrayList();
909              q.drainTo(l, i);
910 <            int k = (i < SIZE)? i : SIZE;
910 >            int k = (i < SIZE) ? i : SIZE;
911              assertEquals(l.size(), k);
912              assertEquals(q.size(), SIZE-k);
913              for (int j = 0; j < k; ++j)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines