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.31 by jsr166, Tue Oct 19 00:43:49 2010 UTC

# 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 214 | Line 228 | public class LinkedBlockingQueueTest ext
228              shouldThrow();
229          } catch (NullPointerException success) {}
230      }
231 +
232      /**
233       * addAll of a collection with any null elements throws NPE after
234       * possibly adding some elements
# Line 228 | Line 243 | public class LinkedBlockingQueueTest ext
243              shouldThrow();
244          } catch (NullPointerException success) {}
245      }
246 +
247      /**
248       * addAll throws ISE if not enough room
249       */
# Line 241 | Line 257 | public class LinkedBlockingQueueTest ext
257              shouldThrow();
258          } catch (IllegalStateException success) {}
259      }
260 +
261      /**
262       * Queue contains all elements, in traversal order, of successful addAll
263       */
# Line 284 | Line 301 | public class LinkedBlockingQueueTest ext
301       * put blocks interruptibly if full
302       */
303      public void testBlockingPut() throws InterruptedException {
304 +        final LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
305          Thread t = new Thread(new CheckedRunnable() {
306 <            public void realRun() {
307 <                int added = 0;
306 >            public void realRun() throws InterruptedException {
307 >                for (int i = 0; i < SIZE; ++i)
308 >                    q.put(i);
309 >                assertEquals(SIZE, q.size());
310 >                assertEquals(0, q.remainingCapacity());
311                  try {
312 <                    LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
313 <                    for (int i = 0; i < SIZE; ++i) {
314 <                        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 <                }
312 >                    q.put(99);
313 >                    shouldThrow();
314 >                } catch (InterruptedException success) {}
315              }});
316  
317          t.start();
318          Thread.sleep(SHORT_DELAY_MS);
319          t.interrupt();
320          t.join();
321 +        assertEquals(SIZE, q.size());
322 +        assertEquals(0, q.remainingCapacity());
323      }
324  
325      /**
326       * put blocks waiting for take when full
327       */
328      public void testPutWithTake() throws InterruptedException {
329 +        final int capacity = 2;
330          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
331          Thread t = new Thread(new CheckedRunnable() {
332 <            public void realRun() {
333 <                int added = 0;
332 >            public void realRun() throws InterruptedException {
333 >                for (int i = 0; i < capacity + 1; i++)
334 >                    q.put(i);
335                  try {
336 <                    q.put(new Object());
337 <                    ++added;
338 <                    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 <                }
336 >                    q.put(99);
337 >                    shouldThrow();
338 >                } catch (InterruptedException success) {}
339              }});
340  
341          t.start();
342          Thread.sleep(SHORT_DELAY_MS);
343 <        q.take();
343 >        assertEquals(q.remainingCapacity(), 0);
344 >        assertEquals(0, q.take());
345 >        Thread.sleep(SHORT_DELAY_MS);
346          t.interrupt();
347          t.join();
348 +        assertEquals(q.remainingCapacity(), 0);
349      }
350  
351      /**
# Line 341 | Line 353 | public class LinkedBlockingQueueTest ext
353       */
354      public void testTimedOffer() throws InterruptedException {
355          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
356 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
356 >        Thread t = new Thread(new CheckedRunnable() {
357              public void realRun() throws InterruptedException {
358                  q.put(new Object());
359                  q.put(new Object());
360 <                threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS));
361 <                q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
362 <            }};
360 >                assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS));
361 >                try {
362 >                    q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
363 >                    shouldThrow();
364 >                } catch (InterruptedException success) {}
365 >            }});
366  
367          t.start();
368          Thread.sleep(SMALL_DELAY_MS);
# Line 361 | Line 376 | public class LinkedBlockingQueueTest ext
376      public void testTake() throws InterruptedException {
377          LinkedBlockingQueue q = populatedQueue(SIZE);
378          for (int i = 0; i < SIZE; ++i) {
379 <            assertEquals(i, ((Integer)q.take()).intValue());
379 >            assertEquals(i, q.take());
380          }
381      }
382  
# Line 385 | Line 400 | public class LinkedBlockingQueueTest ext
400       * Take removes existing elements until empty, then blocks interruptibly
401       */
402      public void testBlockingTake() throws InterruptedException {
403 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
403 >        final LinkedBlockingQueue q = populatedQueue(SIZE);
404 >        Thread t = new Thread(new CheckedRunnable() {
405              public void realRun() throws InterruptedException {
390                LinkedBlockingQueue q = populatedQueue(SIZE);
406                  for (int i = 0; i < SIZE; ++i) {
407 <                    assertEquals(i, ((Integer)q.take()).intValue());
407 >                    assertEquals(i, q.take());
408                  }
409 <                q.take();
410 <            }};
409 >                try {
410 >                    q.take();
411 >                    shouldThrow();
412 >                } catch (InterruptedException success) {}
413 >            }});
414  
415          t.start();
416          Thread.sleep(SHORT_DELAY_MS);
# Line 400 | Line 418 | public class LinkedBlockingQueueTest ext
418          t.join();
419      }
420  
403
421      /**
422       * poll succeeds unless empty
423       */
424      public void testPoll() {
425          LinkedBlockingQueue q = populatedQueue(SIZE);
426          for (int i = 0; i < SIZE; ++i) {
427 <            assertEquals(i, ((Integer)q.poll()).intValue());
427 >            assertEquals(i, q.poll());
428          }
429          assertNull(q.poll());
430      }
# Line 418 | Line 435 | public class LinkedBlockingQueueTest ext
435      public void testTimedPoll0() throws InterruptedException {
436          LinkedBlockingQueue q = populatedQueue(SIZE);
437          for (int i = 0; i < SIZE; ++i) {
438 <            assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue());
438 >            assertEquals(i, q.poll(0, MILLISECONDS));
439          }
440          assertNull(q.poll(0, MILLISECONDS));
441      }
# Line 429 | Line 446 | public class LinkedBlockingQueueTest ext
446      public void testTimedPoll() throws InterruptedException {
447          LinkedBlockingQueue q = populatedQueue(SIZE);
448          for (int i = 0; i < SIZE; ++i) {
449 <            assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
449 >            assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
450          }
451          assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
452      }
# Line 439 | Line 456 | public class LinkedBlockingQueueTest ext
456       * returning timeout status
457       */
458      public void testInterruptedTimedPoll() throws InterruptedException {
459 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
459 >        Thread t = new Thread(new CheckedRunnable() {
460              public void realRun() throws InterruptedException {
461                  LinkedBlockingQueue q = populatedQueue(SIZE);
462                  for (int i = 0; i < SIZE; ++i) {
463 <                    threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
463 >                    assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
464                  }
465 <                q.poll(SMALL_DELAY_MS, MILLISECONDS);
466 <            }};
465 >                try {
466 >                    q.poll(SMALL_DELAY_MS, MILLISECONDS);
467 >                    shouldThrow();
468 >                } catch (InterruptedException success) {}
469 >            }});
470  
471          t.start();
472          Thread.sleep(SHORT_DELAY_MS);
# Line 455 | Line 475 | public class LinkedBlockingQueueTest ext
475      }
476  
477      /**
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            }};
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
479       */
480      public void testPeek() {
481          LinkedBlockingQueue q = populatedQueue(SIZE);
482          for (int i = 0; i < SIZE; ++i) {
483 <            assertEquals(i, ((Integer)q.peek()).intValue());
484 <            q.poll();
483 >            assertEquals(i, q.peek());
484 >            assertEquals(i, q.poll());
485              assertTrue(q.peek() == null ||
486 <                       i != ((Integer)q.peek()).intValue());
486 >                       !q.peek().equals(i));
487          }
488          assertNull(q.peek());
489      }
# Line 494 | Line 494 | public class LinkedBlockingQueueTest ext
494      public void testElement() {
495          LinkedBlockingQueue q = populatedQueue(SIZE);
496          for (int i = 0; i < SIZE; ++i) {
497 <            assertEquals(i, ((Integer)q.element()).intValue());
498 <            q.poll();
497 >            assertEquals(i, q.element());
498 >            assertEquals(i, q.poll());
499          }
500          try {
501              q.element();
# Line 509 | Line 509 | public class LinkedBlockingQueueTest ext
509      public void testRemove() {
510          LinkedBlockingQueue q = populatedQueue(SIZE);
511          for (int i = 0; i < SIZE; ++i) {
512 <            assertEquals(i, ((Integer)q.remove()).intValue());
512 >            assertEquals(i, q.remove());
513          }
514          try {
515              q.remove();
# Line 647 | Line 647 | public class LinkedBlockingQueueTest ext
647       * toArray(null) throws NPE
648       */
649      public void testToArray_BadArg() {
650 +        LinkedBlockingQueue q = populatedQueue(SIZE);
651          try {
651            LinkedBlockingQueue q = populatedQueue(SIZE);
652              Object o[] = q.toArray(null);
653              shouldThrow();
654          } catch (NullPointerException success) {}
# Line 658 | Line 658 | public class LinkedBlockingQueueTest ext
658       * toArray with incompatible array type throws CCE
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 >            Object o[] = 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