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.27 by jsr166, Wed Aug 25 01:44:48 2010 UTC

# Line 15 | Line 15 | import java.io.*;
15   public class LinkedBlockingQueueTest extends JSR166TestCase {
16  
17      public static void main(String[] args) {
18 <        junit.textui.TestRunner.run (suite());
18 >        junit.textui.TestRunner.run(suite());
19      }
20  
21      public static Test suite() {
# Line 214 | Line 214 | public class LinkedBlockingQueueTest ext
214              shouldThrow();
215          } catch (NullPointerException success) {}
216      }
217 +
218      /**
219       * addAll of a collection with any null elements throws NPE after
220       * possibly adding some elements
# Line 228 | Line 229 | public class LinkedBlockingQueueTest ext
229              shouldThrow();
230          } catch (NullPointerException success) {}
231      }
232 +
233      /**
234       * addAll throws ISE if not enough room
235       */
# Line 241 | Line 243 | public class LinkedBlockingQueueTest ext
243              shouldThrow();
244          } catch (IllegalStateException success) {}
245      }
246 +
247      /**
248       * Queue contains all elements, in traversal order, of successful addAll
249       */
# Line 284 | Line 287 | public class LinkedBlockingQueueTest ext
287       * put blocks interruptibly if full
288       */
289      public void testBlockingPut() throws InterruptedException {
290 +        final LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
291          Thread t = new Thread(new CheckedRunnable() {
292 <            public void realRun() {
293 <                int added = 0;
292 >            public void realRun() throws InterruptedException {
293 >                for (int i = 0; i < SIZE; ++i)
294 >                    q.put(i);
295 >                assertEquals(SIZE, q.size());
296 >                assertEquals(0, q.remainingCapacity());
297                  try {
298 <                    LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
299 <                    for (int i = 0; i < SIZE; ++i) {
300 <                        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 <                }
298 >                    q.put(99);
299 >                    shouldThrow();
300 >                } catch (InterruptedException success) {}
301              }});
302  
303          t.start();
304          Thread.sleep(SHORT_DELAY_MS);
305          t.interrupt();
306          t.join();
307 +        assertEquals(SIZE, q.size());
308 +        assertEquals(0, q.remainingCapacity());
309      }
310  
311      /**
312       * put blocks waiting for take when full
313       */
314      public void testPutWithTake() throws InterruptedException {
315 +        final int capacity = 2;
316          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
317          Thread t = new Thread(new CheckedRunnable() {
318 <            public void realRun() {
319 <                int added = 0;
318 >            public void realRun() throws InterruptedException {
319 >                for (int i = 0; i < capacity + 1; i++)
320 >                    q.put(i);
321                  try {
322 <                    q.put(new Object());
323 <                    ++added;
324 <                    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 <                }
322 >                    q.put(99);
323 >                    shouldThrow();
324 >                } catch (InterruptedException success) {}
325              }});
326  
327          t.start();
328          Thread.sleep(SHORT_DELAY_MS);
329 <        q.take();
329 >        assertEquals(q.remainingCapacity(), 0);
330 >        assertEquals(0, q.take());
331 >        Thread.sleep(SHORT_DELAY_MS);
332          t.interrupt();
333          t.join();
334 +        assertEquals(q.remainingCapacity(), 0);
335      }
336  
337      /**
# Line 341 | Line 339 | public class LinkedBlockingQueueTest ext
339       */
340      public void testTimedOffer() throws InterruptedException {
341          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
342 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
342 >        Thread t = new Thread(new CheckedRunnable() {
343              public void realRun() throws InterruptedException {
344                  q.put(new Object());
345                  q.put(new Object());
346 <                threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS));
347 <                q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
348 <            }};
346 >                assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS));
347 >                try {
348 >                    q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
349 >                    shouldThrow();
350 >                } catch (InterruptedException success) {}
351 >            }});
352  
353          t.start();
354          Thread.sleep(SMALL_DELAY_MS);
# Line 361 | Line 362 | public class LinkedBlockingQueueTest ext
362      public void testTake() throws InterruptedException {
363          LinkedBlockingQueue q = populatedQueue(SIZE);
364          for (int i = 0; i < SIZE; ++i) {
365 <            assertEquals(i, ((Integer)q.take()).intValue());
365 >            assertEquals(i, q.take());
366          }
367      }
368  
# Line 385 | Line 386 | public class LinkedBlockingQueueTest ext
386       * Take removes existing elements until empty, then blocks interruptibly
387       */
388      public void testBlockingTake() throws InterruptedException {
389 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
389 >        final LinkedBlockingQueue q = populatedQueue(SIZE);
390 >        Thread t = new Thread(new CheckedRunnable() {
391              public void realRun() throws InterruptedException {
390                LinkedBlockingQueue q = populatedQueue(SIZE);
392                  for (int i = 0; i < SIZE; ++i) {
393 <                    assertEquals(i, ((Integer)q.take()).intValue());
393 >                    assertEquals(i, q.take());
394                  }
395 <                q.take();
396 <            }};
395 >                try {
396 >                    q.take();
397 >                    shouldThrow();
398 >                } catch (InterruptedException success) {}
399 >            }});
400  
401          t.start();
402          Thread.sleep(SHORT_DELAY_MS);
# Line 400 | Line 404 | public class LinkedBlockingQueueTest ext
404          t.join();
405      }
406  
403
407      /**
408       * poll succeeds unless empty
409       */
410      public void testPoll() {
411          LinkedBlockingQueue q = populatedQueue(SIZE);
412          for (int i = 0; i < SIZE; ++i) {
413 <            assertEquals(i, ((Integer)q.poll()).intValue());
413 >            assertEquals(i, q.poll());
414          }
415          assertNull(q.poll());
416      }
# Line 418 | Line 421 | public class LinkedBlockingQueueTest ext
421      public void testTimedPoll0() throws InterruptedException {
422          LinkedBlockingQueue q = populatedQueue(SIZE);
423          for (int i = 0; i < SIZE; ++i) {
424 <            assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue());
424 >            assertEquals(i, q.poll(0, MILLISECONDS));
425          }
426          assertNull(q.poll(0, MILLISECONDS));
427      }
# Line 429 | Line 432 | public class LinkedBlockingQueueTest ext
432      public void testTimedPoll() throws InterruptedException {
433          LinkedBlockingQueue q = populatedQueue(SIZE);
434          for (int i = 0; i < SIZE; ++i) {
435 <            assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
435 >            assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
436          }
437          assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
438      }
# Line 439 | Line 442 | public class LinkedBlockingQueueTest ext
442       * returning timeout status
443       */
444      public void testInterruptedTimedPoll() throws InterruptedException {
445 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
445 >        Thread t = new Thread(new CheckedRunnable() {
446              public void realRun() throws InterruptedException {
447                  LinkedBlockingQueue q = populatedQueue(SIZE);
448                  for (int i = 0; i < SIZE; ++i) {
449 <                    threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
449 >                    assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
450                  }
451 <                q.poll(SMALL_DELAY_MS, MILLISECONDS);
452 <            }};
451 >                try {
452 >                    q.poll(SMALL_DELAY_MS, MILLISECONDS);
453 >                    shouldThrow();
454 >                } catch (InterruptedException success) {}
455 >            }});
456  
457          t.start();
458          Thread.sleep(SHORT_DELAY_MS);
# Line 460 | Line 466 | public class LinkedBlockingQueueTest ext
466       */
467      public void testTimedPollWithOffer() throws InterruptedException {
468          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
469 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
469 >        Thread t = new Thread(new CheckedRunnable() {
470              public void realRun() throws InterruptedException {
471 <                threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
472 <                q.poll(LONG_DELAY_MS, MILLISECONDS);
473 <                q.poll(LONG_DELAY_MS, MILLISECONDS);
474 <            }};
471 >                assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
472 >                assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
473 >                try {
474 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
475 >                    shouldThrow();
476 >                } catch (InterruptedException success) {}
477 >            }});
478  
479          t.start();
480          Thread.sleep(SMALL_DELAY_MS);
# Line 480 | Line 489 | public class LinkedBlockingQueueTest ext
489      public void testPeek() {
490          LinkedBlockingQueue q = populatedQueue(SIZE);
491          for (int i = 0; i < SIZE; ++i) {
492 <            assertEquals(i, ((Integer)q.peek()).intValue());
493 <            q.poll();
492 >            assertEquals(i, q.peek());
493 >            assertEquals(i, q.poll());
494              assertTrue(q.peek() == null ||
495 <                       i != ((Integer)q.peek()).intValue());
495 >                       !q.peek().equals(i));
496          }
497          assertNull(q.peek());
498      }
# Line 494 | Line 503 | public class LinkedBlockingQueueTest ext
503      public void testElement() {
504          LinkedBlockingQueue q = populatedQueue(SIZE);
505          for (int i = 0; i < SIZE; ++i) {
506 <            assertEquals(i, ((Integer)q.element()).intValue());
507 <            q.poll();
506 >            assertEquals(i, q.element());
507 >            assertEquals(i, q.poll());
508          }
509          try {
510              q.element();
# Line 509 | Line 518 | public class LinkedBlockingQueueTest ext
518      public void testRemove() {
519          LinkedBlockingQueue q = populatedQueue(SIZE);
520          for (int i = 0; i < SIZE; ++i) {
521 <            assertEquals(i, ((Integer)q.remove()).intValue());
521 >            assertEquals(i, q.remove());
522          }
523          try {
524              q.remove();
# Line 647 | Line 656 | public class LinkedBlockingQueueTest ext
656       * toArray(null) throws NPE
657       */
658      public void testToArray_BadArg() {
659 +        LinkedBlockingQueue q = populatedQueue(SIZE);
660          try {
651            LinkedBlockingQueue q = populatedQueue(SIZE);
661              Object o[] = q.toArray(null);
662              shouldThrow();
663          } catch (NullPointerException success) {}
# Line 658 | Line 667 | public class LinkedBlockingQueueTest ext
667       * toArray with incompatible array type throws CCE
668       */
669      public void testToArray1_BadArg() {
670 +        LinkedBlockingQueue q = populatedQueue(SIZE);
671          try {
672 <            LinkedBlockingQueue q = populatedQueue(SIZE);
663 <            Object o[] = q.toArray(new String[10] );
672 >            Object o[] = q.toArray(new String[10]);
673              shouldThrow();
674          } catch (ArrayStoreException success) {}
675      }
# Line 680 | Line 689 | public class LinkedBlockingQueueTest ext
689      /**
690       * iterator.remove removes current element
691       */
692 <    public void testIteratorRemove () {
692 >    public void testIteratorRemove() {
693          final LinkedBlockingQueue q = new LinkedBlockingQueue(3);
694          q.add(two);
695          q.add(one);
# Line 691 | Line 700 | public class LinkedBlockingQueueTest ext
700          it.remove();
701  
702          it = q.iterator();
703 <        assertEquals(it.next(), one);
704 <        assertEquals(it.next(), three);
703 >        assertSame(it.next(), one);
704 >        assertSame(it.next(), three);
705          assertFalse(it.hasNext());
706      }
707  
# Line 708 | Line 717 | public class LinkedBlockingQueueTest ext
717          assertEquals(0, q.remainingCapacity());
718          int k = 0;
719          for (Iterator it = q.iterator(); it.hasNext();) {
720 <            int i = ((Integer)(it.next())).intValue();
712 <            assertEquals(++k, i);
720 >            assertEquals(++k, it.next());
721          }
722          assertEquals(3, k);
723      }
# Line 717 | Line 725 | public class LinkedBlockingQueueTest ext
725      /**
726       * Modifications do not cause iterators to fail
727       */
728 <    public void testWeaklyConsistentIteration () {
728 >    public void testWeaklyConsistentIteration() {
729          final LinkedBlockingQueue q = new LinkedBlockingQueue(3);
730          q.add(one);
731          q.add(two);
# Line 752 | Line 760 | public class LinkedBlockingQueueTest ext
760          ExecutorService executor = Executors.newFixedThreadPool(2);
761          executor.execute(new CheckedRunnable() {
762              public void realRun() throws InterruptedException {
763 <                threadAssertFalse(q.offer(three));
764 <                threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
765 <                threadAssertEquals(0, q.remainingCapacity());
763 >                assertFalse(q.offer(three));
764 >                assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
765 >                assertEquals(0, q.remainingCapacity());
766              }});
767  
768          executor.execute(new CheckedRunnable() {
769              public void realRun() throws InterruptedException {
770                  Thread.sleep(SMALL_DELAY_MS);
771 <                threadAssertEquals(one, q.take());
771 >                assertSame(one, q.take());
772              }});
773  
774          joinPool(executor);
# Line 774 | Line 782 | public class LinkedBlockingQueueTest ext
782          ExecutorService executor = Executors.newFixedThreadPool(2);
783          executor.execute(new CheckedRunnable() {
784              public void realRun() throws InterruptedException {
785 <                threadAssertNull(q.poll());
786 <                threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
787 <                threadAssertTrue(q.isEmpty());
785 >                assertNull(q.poll());
786 >                assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
787 >                assertTrue(q.isEmpty());
788              }});
789  
790          executor.execute(new CheckedRunnable() {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines