ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/Collection8Test.java
(Generate patch)

Comparing jsr166/src/test/tck/Collection8Test.java (file contents):
Revision 1.41 by jsr166, Mon Dec 12 03:50:15 2016 UTC vs.
Revision 1.46 by jsr166, Mon May 29 19:15:02 2017 UTC

# Line 12 | Line 12 | import java.util.ArrayList;
12   import java.util.Arrays;
13   import java.util.Collection;
14   import java.util.Collections;
15 + import java.util.ConcurrentModificationException;
16   import java.util.Deque;
17   import java.util.HashSet;
18   import java.util.Iterator;
# Line 139 | Line 140 | public class Collection8Test extends JSR
140          }
141          if (c instanceof BlockingQueue) {
142              BlockingQueue q = (BlockingQueue) c;
143 <            assertNull(q.poll(0L, MILLISECONDS));
143 >            assertNull(q.poll(randomExpiredTimeout(), randomTimeUnit()));
144          }
145          if (c instanceof BlockingDeque) {
146              BlockingDeque q = (BlockingDeque) c;
147 <            assertNull(q.pollFirst(0L, MILLISECONDS));
148 <            assertNull(q.pollLast(0L, MILLISECONDS));
147 >            assertNull(q.pollFirst(randomExpiredTimeout(), randomTimeUnit()));
148 >            assertNull(q.pollLast(randomExpiredTimeout(), randomTimeUnit()));
149          }
150      }
151  
# Line 515 | Line 516 | public class Collection8Test extends JSR
516      }
517  
518      /**
519 +     * Iterator.forEachRemaining has same behavior as Iterator's
520 +     * default implementation.
521 +     */
522 +    public void testForEachRemainingConsistentWithDefaultImplementation() {
523 +        Collection c = impl.emptyCollection();
524 +        if (!testImplementationDetails
525 +            || c.getClass() == java.util.LinkedList.class)
526 +            return;
527 +        ThreadLocalRandom rnd = ThreadLocalRandom.current();
528 +        int n = 1 + rnd.nextInt(3);
529 +        for (int i = 0; i < n; i++) c.add(impl.makeElement(i));
530 +        ArrayList iterated = new ArrayList();
531 +        ArrayList iteratedForEachRemaining = new ArrayList();
532 +        Iterator it1 = c.iterator();
533 +        Iterator it2 = c.iterator();
534 +        assertTrue(it1.hasNext());
535 +        assertTrue(it2.hasNext());
536 +        c.clear();
537 +        Object r1, r2;
538 +        try {
539 +            while (it1.hasNext()) iterated.add(it1.next());
540 +            r1 = iterated;
541 +        } catch (ConcurrentModificationException ex) {
542 +            r1 = ConcurrentModificationException.class;
543 +            assertFalse(impl.isConcurrent());
544 +        }
545 +        try {
546 +            it2.forEachRemaining(iteratedForEachRemaining::add);
547 +            r2 = iteratedForEachRemaining;
548 +        } catch (ConcurrentModificationException ex) {
549 +            r2 = ConcurrentModificationException.class;
550 +            assertFalse(impl.isConcurrent());
551 +        }
552 +        assertEquals(r1, r2);
553 +    }
554 +
555 +    /**
556       * Calling Iterator#remove() after Iterator#forEachRemaining
557       * should (maybe) remove last element
558       */
# Line 689 | Line 727 | public class Collection8Test extends JSR
727      }
728  
729      /**
730 +     * Concurrent Spliterators, once exhausted, stay exhausted.
731 +     */
732 +    public void testStickySpliteratorExhaustion() throws Throwable {
733 +        if (!impl.isConcurrent()) return;
734 +        if (!testImplementationDetails) return;
735 +        final ThreadLocalRandom rnd = ThreadLocalRandom.current();
736 +        final Consumer alwaysThrows = e -> { throw new AssertionError(); };
737 +        final Collection c = impl.emptyCollection();
738 +        final Spliterator s = c.spliterator();
739 +        if (rnd.nextBoolean()) {
740 +            assertFalse(s.tryAdvance(alwaysThrows));
741 +        } else {
742 +            s.forEachRemaining(alwaysThrows);
743 +        }
744 +        final Object one = impl.makeElement(1);
745 +        // Spliterator should not notice added element
746 +        c.add(one);
747 +        if (rnd.nextBoolean()) {
748 +            assertFalse(s.tryAdvance(alwaysThrows));
749 +        } else {
750 +            s.forEachRemaining(alwaysThrows);
751 +        }
752 +    }
753 +
754 +    /**
755       * Motley crew of threads concurrently randomly hammer the collection.
756       */
757      public void testDetectRaces() throws Throwable {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines