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.40 by jsr166, Tue Mar 15 19:47:06 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 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);
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();
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      /**
# Line 341 | Line 354 | public class LinkedBlockingQueueTest ext
354       */
355      public void testTimedOffer() throws InterruptedException {
356          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
357 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
357 >        Thread t = new Thread(new CheckedRunnable() {
358              public void realRun() throws InterruptedException {
359                  q.put(new Object());
360                  q.put(new Object());
361 <                threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS));
362 <                q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
363 <            }};
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          t.start();
369          Thread.sleep(SMALL_DELAY_MS);
# Line 361 | Line 377 | public class LinkedBlockingQueueTest ext
377      public void testTake() throws InterruptedException {
378          LinkedBlockingQueue q = populatedQueue(SIZE);
379          for (int i = 0; i < SIZE; ++i) {
380 <            assertEquals(i, ((Integer)q.take()).intValue());
380 >            assertEquals(i, q.take());
381          }
382      }
383  
384      /**
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    /**
385       * Take removes existing elements until empty, then blocks interruptibly
386       */
387      public void testBlockingTake() throws InterruptedException {
388 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
388 >        final LinkedBlockingQueue q = populatedQueue(SIZE);
389 >        Thread t = new Thread(new CheckedRunnable() {
390              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());
392 >                    assertEquals(i, q.take());
393                  }
394 <                q.take();
395 <            }};
394 >                try {
395 >                    q.take();
396 >                    shouldThrow();
397 >                } catch (InterruptedException success) {}
398 >            }});
399  
400          t.start();
401          Thread.sleep(SHORT_DELAY_MS);
# Line 400 | Line 403 | public class LinkedBlockingQueueTest ext
403          t.join();
404      }
405  
403
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() throws InterruptedException {
421          LinkedBlockingQueue q = populatedQueue(SIZE);
422          for (int i = 0; i < SIZE; ++i) {
423 <            assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue());
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() throws InterruptedException {
432          LinkedBlockingQueue q = populatedQueue(SIZE);
433          for (int i = 0; i < SIZE; ++i) {
434 <            assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
434 >            assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
435          }
436          assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
437      }
# Line 439 | Line 441 | public class LinkedBlockingQueueTest ext
441       * returning timeout status
442       */
443      public void testInterruptedTimedPoll() throws InterruptedException {
444 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
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 {
444                LinkedBlockingQueue q = populatedQueue(SIZE);
448                  for (int i = 0; i < SIZE; ++i) {
449 <                    threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
449 >                    long t0 = System.nanoTime();
450 >                    assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
451 >                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
452                  }
453 <                q.poll(SMALL_DELAY_MS, MILLISECONDS);
454 <            }};
455 <
456 <        t.start();
457 <        Thread.sleep(SHORT_DELAY_MS);
458 <        t.interrupt();
459 <        t.join();
460 <    }
461 <
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 <            }};
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 <        t.start();
464 <        Thread.sleep(SMALL_DELAY_MS);
472 <        assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
463 >        aboutToWait.await();
464 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
465          t.interrupt();
466 <        t.join();
466 >        awaitTermination(t, MEDIUM_DELAY_MS);
467 >        checkEmpty(q);
468      }
469  
470      /**
# Line 480 | 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 494 | 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();
# Line 509 | 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();
# Line 523 | 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 623 | 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() throws InterruptedException {
627 >    public void testToArray() {
628          LinkedBlockingQueue q = populatedQueue(SIZE);
629          Object[] o = q.toArray();
630          for (int i = 0; i < o.length; i++)
631 <            assertEquals(o[i], q.take());
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() throws InterruptedException {
638 <        LinkedBlockingQueue q = populatedQueue(SIZE);
638 >        LinkedBlockingQueue<Integer> q = populatedQueue(SIZE);
639          Integer[] ints = new Integer[SIZE];
640 <        ints = (Integer[])q.toArray(ints);
640 >        Integer[] array = q.toArray(ints);
641 >        assertSame(ints, array);
642          for (int i = 0; i < ints.length; i++)
643 <            assertEquals(ints[i], q.take());
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);
652 <            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);
663 <            Object o[] = q.toArray(new String[10] );
663 >            q.toArray(new String[10]);
664              shouldThrow();
665          } catch (ArrayStoreException success) {}
666      }
# Line 680 | Line 680 | public class LinkedBlockingQueueTest ext
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 691 | 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 708 | 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();
712 <            assertEquals(++k, i);
711 >            assertEquals(++k, it.next());
712          }
713          assertEquals(3, k);
714      }
# Line 717 | 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);
# Line 752 | Line 751 | public class LinkedBlockingQueueTest ext
751          ExecutorService executor = Executors.newFixedThreadPool(2);
752          executor.execute(new CheckedRunnable() {
753              public void realRun() throws InterruptedException {
754 <                threadAssertFalse(q.offer(three));
755 <                threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
756 <                threadAssertEquals(0, q.remainingCapacity());
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 <                threadAssertEquals(one, q.take());
762 >                assertSame(one, q.take());
763              }});
764  
765          joinPool(executor);
# Line 774 | Line 773 | public class LinkedBlockingQueueTest ext
773          ExecutorService executor = Executors.newFixedThreadPool(2);
774          executor.execute(new CheckedRunnable() {
775              public void realRun() throws InterruptedException {
776 <                threadAssertNull(q.poll());
777 <                threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
778 <                threadAssertTrue(q.isEmpty());
776 >                assertNull(q.poll());
777 >                assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
778 >                assertTrue(q.isEmpty());
779              }});
780  
781          executor.execute(new CheckedRunnable() {
# Line 896 | Line 895 | public class LinkedBlockingQueueTest ext
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 905 | 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