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.22 by jsr166, Sun Nov 13 02:10:10 2016 UTC vs.
Revision 1.43 by jsr166, Thu Dec 15 01:21:22 2016 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 21 | Line 22 | import java.util.Queue;
22   import java.util.Spliterator;
23   import java.util.concurrent.BlockingDeque;
24   import java.util.concurrent.BlockingQueue;
25 + import java.util.concurrent.ConcurrentLinkedQueue;
26   import java.util.concurrent.CountDownLatch;
27   import java.util.concurrent.Executors;
28   import java.util.concurrent.ExecutorService;
29   import java.util.concurrent.Future;
30 + import java.util.concurrent.Phaser;
31   import java.util.concurrent.ThreadLocalRandom;
32   import java.util.concurrent.atomic.AtomicBoolean;
33   import java.util.concurrent.atomic.AtomicLong;
34   import java.util.concurrent.atomic.AtomicReference;
35   import java.util.function.Consumer;
36   import java.util.function.Predicate;
37 + import java.util.stream.Collectors;
38  
39   import junit.framework.Test;
40  
# Line 61 | Line 65 | public class Collection8Test extends JSR
65      }
66  
67      /** Checks properties of empty collections. */
68 <    public void testEmptyMeansEmpty() throws InterruptedException {
68 >    public void testEmptyMeansEmpty() throws Throwable {
69          Collection c = impl.emptyCollection();
70          emptyMeansEmpty(c);
71  
72 <        if (c instanceof java.io.Serializable)
73 <            emptyMeansEmpty(serialClone(c));
72 >        if (c instanceof java.io.Serializable) {
73 >            try {
74 >                emptyMeansEmpty(serialClonePossiblyFailing(c));
75 >            } catch (java.io.NotSerializableException ex) {
76 >                // excusable when we have a serializable wrapper around
77 >                // a non-serializable collection, as can happen with:
78 >                // Vector.subList() => wrapped AbstractList$RandomAccessSubList
79 >                if (testImplementationDetails
80 >                    && (! c.getClass().getName().matches(
81 >                                "java.util.Collections.*")))
82 >                    throw ex;
83 >            }
84 >        }
85  
86          Collection clone = cloneableClone(c);
87          if (clone != null)
# Line 291 | Line 306 | public class Collection8Test extends JSR
306              switch (rnd.nextInt(4)) {
307              case 0: survivors.addAll(c); break;
308              case 1: survivors.addAll(Arrays.asList(c.toArray())); break;
309 <            case 2: c.forEach(e -> survivors.add(e)); break;
309 >            case 2: c.forEach(survivors::add); break;
310              case 3: for (Object e : c) survivors.add(e); break;
311              }
312              assertTrue(orig.containsAll(accepts));
# Line 314 | Line 329 | public class Collection8Test extends JSR
329              }
330          } catch (Throwable ex) {
331              System.err.println(impl.klazz());
332 <            System.err.printf("c=%s%n", c);
332 >            // c is at risk of corruption if we got here, so be lenient
333 >            try { System.err.printf("c=%s%n", c); }
334 >            catch (Throwable t) { t.printStackTrace(); }
335              System.err.printf("n=%d%n", n);
336              System.err.printf("orig=%s%n", orig);
337              System.err.printf("accepts=%s%n", accepts);
# Line 326 | Line 343 | public class Collection8Test extends JSR
343      }
344  
345      /**
346 +     * All elements removed in the middle of CONCURRENT traversal.
347 +     */
348 +    public void testElementRemovalDuringTraversal() {
349 +        Collection c = impl.emptyCollection();
350 +        ThreadLocalRandom rnd = ThreadLocalRandom.current();
351 +        int n = rnd.nextInt(6);
352 +        ArrayList copy = new ArrayList();
353 +        for (int i = 0; i < n; i++) {
354 +            Object x = impl.makeElement(i);
355 +            copy.add(x);
356 +            c.add(x);
357 +        }
358 +        ArrayList iterated = new ArrayList();
359 +        ArrayList spliterated = new ArrayList();
360 +        Spliterator s = c.spliterator();
361 +        Iterator it = c.iterator();
362 +        for (int i = rnd.nextInt(n + 1); --i >= 0; ) {
363 +            assertTrue(s.tryAdvance(spliterated::add));
364 +            if (rnd.nextBoolean()) assertTrue(it.hasNext());
365 +            iterated.add(it.next());
366 +        }
367 +        Consumer alwaysThrows = e -> { throw new AssertionError(); };
368 +        if (s.hasCharacteristics(Spliterator.CONCURRENT)) {
369 +            c.clear();          // TODO: many more removal methods
370 +            if (testImplementationDetails
371 +                && !(c instanceof java.util.concurrent.ArrayBlockingQueue)) {
372 +                if (rnd.nextBoolean())
373 +                    assertFalse(s.tryAdvance(alwaysThrows));
374 +                else
375 +                    s.forEachRemaining(alwaysThrows);
376 +            }
377 +            if (it.hasNext()) iterated.add(it.next());
378 +            if (rnd.nextBoolean()) assertIteratorExhausted(it);
379 +        }
380 +        assertTrue(copy.containsAll(iterated));
381 +        assertTrue(copy.containsAll(spliterated));
382 +    }
383 +
384 +    /**
385 +     * Some elements randomly disappear in the middle of traversal.
386 +     */
387 +    public void testRandomElementRemovalDuringTraversal() {
388 +        Collection c = impl.emptyCollection();
389 +        ThreadLocalRandom rnd = ThreadLocalRandom.current();
390 +        int n = rnd.nextInt(6);
391 +        ArrayList copy = new ArrayList();
392 +        for (int i = 0; i < n; i++) {
393 +            Object x = impl.makeElement(i);
394 +            copy.add(x);
395 +            c.add(x);
396 +        }
397 +        ArrayList iterated = new ArrayList();
398 +        ArrayList spliterated = new ArrayList();
399 +        ArrayList removed = new ArrayList();
400 +        Spliterator s = c.spliterator();
401 +        Iterator it = c.iterator();
402 +        if (! (s.hasCharacteristics(Spliterator.CONCURRENT) ||
403 +               s.hasCharacteristics(Spliterator.IMMUTABLE)))
404 +            return;
405 +        for (int i = rnd.nextInt(n + 1); --i >= 0; ) {
406 +            assertTrue(s.tryAdvance(e -> {}));
407 +            if (rnd.nextBoolean()) assertTrue(it.hasNext());
408 +            it.next();
409 +        }
410 +        Consumer alwaysThrows = e -> { throw new AssertionError(); };
411 +        // TODO: many more removal methods
412 +        if (rnd.nextBoolean()) {
413 +            for (Iterator z = c.iterator(); z.hasNext(); ) {
414 +                Object e = z.next();
415 +                if (rnd.nextBoolean()) {
416 +                    try {
417 +                        z.remove();
418 +                    } catch (UnsupportedOperationException ok) { return; }
419 +                    removed.add(e);
420 +                }
421 +            }
422 +        } else {
423 +            Predicate randomlyRemove = e -> {
424 +                if (rnd.nextBoolean()) { removed.add(e); return true; }
425 +                else return false;
426 +            };
427 +            c.removeIf(randomlyRemove);
428 +        }
429 +        s.forEachRemaining(spliterated::add);
430 +        while (it.hasNext())
431 +            iterated.add(it.next());
432 +        assertTrue(copy.containsAll(iterated));
433 +        assertTrue(copy.containsAll(spliterated));
434 +        assertTrue(copy.containsAll(removed));
435 +        if (s.hasCharacteristics(Spliterator.CONCURRENT)) {
436 +            ArrayList iteratedAndRemoved = new ArrayList(iterated);
437 +            ArrayList spliteratedAndRemoved = new ArrayList(spliterated);
438 +            iteratedAndRemoved.retainAll(removed);
439 +            spliteratedAndRemoved.retainAll(removed);
440 +            assertTrue(iteratedAndRemoved.size() <= 1);
441 +            assertTrue(spliteratedAndRemoved.size() <= 1);
442 +            if (testImplementationDetails
443 +                && !(c instanceof java.util.concurrent.ArrayBlockingQueue))
444 +                assertTrue(spliteratedAndRemoved.isEmpty());
445 +        }
446 +    }
447 +
448 +    /**
449       * Various ways of traversing a collection yield same elements
450       */
451 <    public void testIteratorEquivalence() {
451 >    public void testTraversalEquivalence() {
452          Collection c = impl.emptyCollection();
453          ThreadLocalRandom rnd = ThreadLocalRandom.current();
454          int n = rnd.nextInt(6);
# Line 337 | Line 457 | public class Collection8Test extends JSR
457          ArrayList iteratedForEachRemaining = new ArrayList();
458          ArrayList tryAdvanced = new ArrayList();
459          ArrayList spliterated = new ArrayList();
460 +        ArrayList splitonced = new ArrayList();
461          ArrayList forEached = new ArrayList();
462 +        ArrayList streamForEached = new ArrayList();
463 +        ConcurrentLinkedQueue parallelStreamForEached = new ConcurrentLinkedQueue();
464          ArrayList removeIfed = new ArrayList();
465          for (Object x : c) iterated.add(x);
466 <        c.iterator().forEachRemaining(e -> iteratedForEachRemaining.add(e));
466 >        c.iterator().forEachRemaining(iteratedForEachRemaining::add);
467          for (Spliterator s = c.spliterator();
468 <             s.tryAdvance(e -> tryAdvanced.add(e)); ) {}
469 <        c.spliterator().forEachRemaining(e -> spliterated.add(e));
470 <        c.forEach(e -> forEached.add(e));
468 >             s.tryAdvance(tryAdvanced::add); ) {}
469 >        c.spliterator().forEachRemaining(spliterated::add);
470 >        {                       // trySplit returns "strict prefix"
471 >            Spliterator s1 = c.spliterator(), s2 = s1.trySplit();
472 >            if (s2 != null) s2.forEachRemaining(splitonced::add);
473 >            s1.forEachRemaining(splitonced::add);
474 >        }
475 >        c.forEach(forEached::add);
476 >        c.stream().forEach(streamForEached::add);
477 >        c.parallelStream().forEach(parallelStreamForEached::add);
478          c.removeIf(e -> { removeIfed.add(e); return false; });
479          boolean ordered =
480              c.spliterator().hasCharacteristics(Spliterator.ORDERED);
481          if (c instanceof List || c instanceof Deque)
482              assertTrue(ordered);
483 +        HashSet cset = new HashSet(c);
484 +        assertEquals(cset, new HashSet(parallelStreamForEached));
485          if (ordered) {
486              assertEquals(iterated, iteratedForEachRemaining);
487              assertEquals(iterated, tryAdvanced);
488              assertEquals(iterated, spliterated);
489 +            assertEquals(iterated, splitonced);
490              assertEquals(iterated, forEached);
491 +            assertEquals(iterated, streamForEached);
492              assertEquals(iterated, removeIfed);
493          } else {
360            HashSet cset = new HashSet(c);
494              assertEquals(cset, new HashSet(iterated));
495              assertEquals(cset, new HashSet(iteratedForEachRemaining));
496              assertEquals(cset, new HashSet(tryAdvanced));
497              assertEquals(cset, new HashSet(spliterated));
498 +            assertEquals(cset, new HashSet(splitonced));
499              assertEquals(cset, new HashSet(forEached));
500 +            assertEquals(cset, new HashSet(streamForEached));
501              assertEquals(cset, new HashSet(removeIfed));
502          }
503          if (c instanceof Deque) {
# Line 381 | 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 +        } catch (UnsupportedOperationException ex) {
545 +            r1 = UnsupportedOperationException.class;
546 +        }
547 +        try {
548 +            it2.forEachRemaining(iteratedForEachRemaining::add);
549 +            r2 = iteratedForEachRemaining;
550 +        } catch (ConcurrentModificationException ex) {
551 +            r2 = ConcurrentModificationException.class;
552 +        } catch (UnsupportedOperationException ex) {
553 +            r2 = UnsupportedOperationException.class;
554 +        }
555 +        assertEquals(r1, r2);
556 +    }
557 +
558 +    /**
559       * Calling Iterator#remove() after Iterator#forEachRemaining
560       * should (maybe) remove last element
561       */
562      public void testRemoveAfterForEachRemaining() {
563          Collection c = impl.emptyCollection();
564          ThreadLocalRandom rnd = ThreadLocalRandom.current();
565 <        {
565 >        testCollection: {
566              int n = 3 + rnd.nextInt(2);
567              for (int i = 0; i < n; i++) c.add(impl.makeElement(i));
568              Iterator it = c.iterator();
# Line 400 | Line 575 | public class Collection8Test extends JSR
575                  if (c instanceof java.util.concurrent.ArrayBlockingQueue) {
576                      assertIteratorExhausted(it);
577                  } else {
578 <                    it.remove();
578 >                    try { it.remove(); }
579 >                    catch (UnsupportedOperationException ok) {
580 >                        break testCollection;
581 >                    }
582                      assertEquals(n - 1, c.size());
583                      for (int i = 0; i < n - 1; i++)
584                          assertTrue(c.contains(impl.makeElement(i)));
# Line 516 | Line 694 | public class Collection8Test extends JSR
694          assertTrue(found.isEmpty());
695      }
696  
697 <    public void testForEachConcurrentStressTest() throws Throwable {
697 >    /** TODO: promote to a common utility */
698 >    static <T> T chooseOne(T ... ts) {
699 >        return ts[ThreadLocalRandom.current().nextInt(ts.length)];
700 >    }
701 >
702 >    /** TODO: more random adders and removers */
703 >    static <E> Runnable adderRemover(Collection<E> c, E e) {
704 >        return chooseOne(
705 >            () -> {
706 >                assertTrue(c.add(e));
707 >                assertTrue(c.contains(e));
708 >                assertTrue(c.remove(e));
709 >                assertFalse(c.contains(e));
710 >            },
711 >            () -> {
712 >                assertTrue(c.add(e));
713 >                assertTrue(c.contains(e));
714 >                assertTrue(c.removeIf(x -> x == e));
715 >                assertFalse(c.contains(e));
716 >            },
717 >            () -> {
718 >                assertTrue(c.add(e));
719 >                assertTrue(c.contains(e));
720 >                for (Iterator it = c.iterator();; )
721 >                    if (it.next() == e) {
722 >                        try { it.remove(); }
723 >                        catch (UnsupportedOperationException ok) {
724 >                            c.remove(e);
725 >                        }
726 >                        break;
727 >                    }
728 >                assertFalse(c.contains(e));
729 >            });
730 >    }
731 >
732 >    /**
733 >     * Motley crew of threads concurrently randomly hammer the collection.
734 >     */
735 >    public void testDetectRaces() throws Throwable {
736          if (!impl.isConcurrent()) return;
737 +        final ThreadLocalRandom rnd = ThreadLocalRandom.current();
738          final Collection c = impl.emptyCollection();
739 <        final long testDurationMillis = timeoutMillis();
739 >        final long testDurationMillis
740 >            = expensiveTests ? LONG_DELAY_MS : timeoutMillis();
741          final AtomicBoolean done = new AtomicBoolean(false);
742 <        final Object elt = impl.makeElement(1);
743 <        final Future<?> f1, f2;
742 >        final Object one = impl.makeElement(1);
743 >        final Object two = impl.makeElement(2);
744 >        final Consumer checkSanity = x -> assertTrue(x == one || x == two);
745 >        final Consumer<Object[]> checkArraySanity = array -> {
746 >            // assertTrue(array.length <= 2); // duplicates are permitted
747 >            for (Object x : array) assertTrue(x == one || x == two);
748 >        };
749 >        final Object[] emptyArray =
750 >            (Object[]) java.lang.reflect.Array.newInstance(one.getClass(), 0);
751 >        final List<Future<?>> futures;
752 >        final Phaser threadsStarted = new Phaser(1); // register this thread
753 >        final Runnable[] frobbers = {
754 >            () -> c.forEach(checkSanity),
755 >            () -> c.stream().forEach(checkSanity),
756 >            () -> c.parallelStream().forEach(checkSanity),
757 >            () -> c.spliterator().trySplit(),
758 >            () -> {
759 >                Spliterator s = c.spliterator();
760 >                s.tryAdvance(checkSanity);
761 >                s.trySplit();
762 >            },
763 >            () -> {
764 >                Spliterator s = c.spliterator();
765 >                do {} while (s.tryAdvance(checkSanity));
766 >            },
767 >            () -> { for (Object x : c) checkSanity.accept(x); },
768 >            () -> checkArraySanity.accept(c.toArray()),
769 >            () -> checkArraySanity.accept(c.toArray(emptyArray)),
770 >            () -> {
771 >                Object[] a = new Object[5];
772 >                Object three = impl.makeElement(3);
773 >                Arrays.fill(a, 0, a.length, three);
774 >                Object[] x = c.toArray(a);
775 >                if (x == a)
776 >                    for (int i = 0; i < a.length && a[i] != null; i++)
777 >                        checkSanity.accept(a[i]);
778 >                    // A careful reading of the spec does not support:
779 >                    // for (i++; i < a.length; i++) assertSame(three, a[i]);
780 >                else
781 >                    checkArraySanity.accept(x);
782 >                },
783 >            adderRemover(c, one),
784 >            adderRemover(c, two),
785 >        };
786 >        final List<Runnable> tasks =
787 >            Arrays.stream(frobbers)
788 >            .filter(task -> rnd.nextBoolean()) // random subset
789 >            .map(task -> (Runnable) () -> {
790 >                     threadsStarted.arriveAndAwaitAdvance();
791 >                     while (!done.get())
792 >                         task.run();
793 >                 })
794 >            .collect(Collectors.toList());
795          final ExecutorService pool = Executors.newCachedThreadPool();
796          try (PoolCleaner cleaner = cleaner(pool, done)) {
797 <            final CountDownLatch threadsStarted = new CountDownLatch(2);
798 <            Runnable checkElt = () -> {
799 <                threadsStarted.countDown();
800 <                while (!done.get())
801 <                    c.forEach(x -> assertSame(x, elt)); };
533 <            Runnable addRemove = () -> {
534 <                threadsStarted.countDown();
535 <                while (!done.get()) {
536 <                    assertTrue(c.add(elt));
537 <                    assertTrue(c.remove(elt));
538 <                }};
539 <            f1 = pool.submit(checkElt);
540 <            f2 = pool.submit(addRemove);
797 >            threadsStarted.bulkRegister(tasks.size());
798 >            futures = tasks.stream()
799 >                .map(pool::submit)
800 >                .collect(Collectors.toList());
801 >            threadsStarted.arriveAndDeregister();
802              Thread.sleep(testDurationMillis);
803          }
804 <        assertNull(f1.get(0L, MILLISECONDS));
805 <        assertNull(f2.get(0L, MILLISECONDS));
804 >        for (Future future : futures)
805 >            assertNull(future.get(0L, MILLISECONDS));
806 >    }
807 >
808 >    /**
809 >     * Spliterators are either IMMUTABLE or truly late-binding or, if
810 >     * concurrent, use the same "late-binding style" of returning
811 >     * elements added between creation and first use.
812 >     */
813 >    public void testLateBindingStyle() {
814 >        if (!testImplementationDetails) return;
815 >        if (impl.klazz() == ArrayList.class) return; // for jdk8
816 >        // Immutable (snapshot) spliterators are exempt
817 >        if (impl.emptyCollection().spliterator()
818 >            .hasCharacteristics(Spliterator.IMMUTABLE))
819 >            return;
820 >        final Object one = impl.makeElement(1);
821 >        {
822 >            final Collection c = impl.emptyCollection();
823 >            final Spliterator split = c.spliterator();
824 >            c.add(one);
825 >            assertTrue(split.tryAdvance(e -> { assertSame(e, one); }));
826 >            assertFalse(split.tryAdvance(e -> { throw new AssertionError(); }));
827 >            assertTrue(c.contains(one));
828 >        }
829 >        {
830 >            final AtomicLong count = new AtomicLong(0);
831 >            final Collection c = impl.emptyCollection();
832 >            final Spliterator split = c.spliterator();
833 >            c.add(one);
834 >            split.forEachRemaining(
835 >                e -> { assertSame(e, one); count.getAndIncrement(); });
836 >            assertEquals(1L, count.get());
837 >            assertFalse(split.tryAdvance(e -> { throw new AssertionError(); }));
838 >            assertTrue(c.contains(one));
839 >        }
840 >    }
841 >
842 >    /**
843 >     * Spliterator.getComparator throws IllegalStateException iff the
844 >     * spliterator does not report SORTED.
845 >     */
846 >    public void testGetComparator_IllegalStateException() {
847 >        Collection c = impl.emptyCollection();
848 >        Spliterator s = c.spliterator();
849 >        boolean reportsSorted = s.hasCharacteristics(Spliterator.SORTED);
850 >        try {
851 >            s.getComparator();
852 >            assertTrue(reportsSorted);
853 >        } catch (IllegalStateException ex) {
854 >            assertFalse(reportsSorted);
855 >        }
856      }
857  
858   //     public void testCollection8DebugFail() {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines