--- jsr166/src/test/tck/Collection8Test.java 2016/11/06 05:45:09 1.16 +++ jsr166/src/test/tck/Collection8Test.java 2016/11/15 23:08:30 1.27 @@ -9,6 +9,7 @@ import static java.util.concurrent.TimeU import static java.util.concurrent.TimeUnit.MILLISECONDS; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Deque; @@ -24,12 +25,14 @@ import java.util.concurrent.CountDownLat import java.util.concurrent.Executors; import java.util.concurrent.ExecutorService; import java.util.concurrent.Future; +import java.util.concurrent.Phaser; import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicReference; import java.util.function.Consumer; import java.util.function.Predicate; +import java.util.stream.Collectors; import junit.framework.Test; @@ -60,12 +63,23 @@ public class Collection8Test extends JSR } /** Checks properties of empty collections. */ - public void testEmptyMeansEmpty() throws InterruptedException { + public void testEmptyMeansEmpty() throws Throwable { Collection c = impl.emptyCollection(); emptyMeansEmpty(c); - if (c instanceof java.io.Serializable) - emptyMeansEmpty(serialClone(c)); + if (c instanceof java.io.Serializable) { + try { + emptyMeansEmpty(serialClonePossiblyFailing(c)); + } catch (java.io.NotSerializableException ex) { + // excusable when we have a serializable wrapper around + // a non-serializable collection, as can happen with: + // Vector.subList() => wrapped AbstractList$RandomAccessSubList + if (testImplementationDetails + && (! c.getClass().getName().matches( + "java.util.Collections.*"))) + throw ex; + } + } Collection clone = cloneableClone(c); if (clone != null) @@ -97,7 +111,7 @@ public class Collection8Test extends JSR assertSame(3, a[2]); } assertIteratorExhausted(c.iterator()); - Consumer alwaysThrows = (e) -> { throw new AssertionError(); }; + Consumer alwaysThrows = e -> { throw new AssertionError(); }; c.forEach(alwaysThrows); c.iterator().forEachRemaining(alwaysThrows); c.spliterator().forEachRemaining(alwaysThrows); @@ -240,14 +254,24 @@ public class Collection8Test extends JSR public void testRemoveIf() { Collection c = impl.emptyCollection(); + boolean ordered = + c.spliterator().hasCharacteristics(Spliterator.ORDERED); ThreadLocalRandom rnd = ThreadLocalRandom.current(); int n = rnd.nextInt(6); for (int i = 0; i < n; i++) c.add(impl.makeElement(i)); AtomicReference threwAt = new AtomicReference(null); - ArrayList survivors = new ArrayList(c); + List orig = rnd.nextBoolean() + ? new ArrayList(c) + : Arrays.asList(c.toArray()); + + // Merely creating an iterator can change ArrayBlockingQueue behavior + Iterator it = rnd.nextBoolean() ? c.iterator() : null; + + ArrayList survivors = new ArrayList(); ArrayList accepts = new ArrayList(); ArrayList rejects = new ArrayList(); - Predicate randomPredicate = (e) -> { + + Predicate randomPredicate = e -> { assertNull(threwAt.get()); switch (rnd.nextInt(3)) { case 0: accepts.add(e); return true; @@ -257,30 +281,61 @@ public class Collection8Test extends JSR } }; try { - assertFalse(survivors.contains(null)); try { boolean modified = c.removeIf(randomPredicate); - if (!modified) { - assertNull(threwAt.get()); - assertEquals(n, rejects.size()); - assertEquals(0, accepts.size()); + assertNull(threwAt.get()); + assertEquals(modified, accepts.size() > 0); + assertEquals(modified, rejects.size() != n); + assertEquals(accepts.size() + rejects.size(), n); + if (ordered) { + assertEquals(rejects, + Arrays.asList(c.toArray())); + } else { + assertEquals(new HashSet(rejects), + new HashSet(Arrays.asList(c.toArray()))); } - } catch (ArithmeticException ok) {} - survivors.removeAll(accepts); - assertEquals(n - accepts.size(), c.size()); + } catch (ArithmeticException ok) { + assertNotNull(threwAt.get()); + assertTrue(c.contains(threwAt.get())); + } + if (it != null && impl.isConcurrent()) + // check for weakly consistent iterator + while (it.hasNext()) assertTrue(orig.contains(it.next())); + switch (rnd.nextInt(4)) { + case 0: survivors.addAll(c); break; + case 1: survivors.addAll(Arrays.asList(c.toArray())); break; + case 2: c.forEach(e -> survivors.add(e)); break; + case 3: for (Object e : c) survivors.add(e); break; + } + assertTrue(orig.containsAll(accepts)); + assertTrue(orig.containsAll(rejects)); + assertTrue(orig.containsAll(survivors)); + assertTrue(orig.containsAll(c)); + assertTrue(c.containsAll(rejects)); assertTrue(c.containsAll(survivors)); assertTrue(survivors.containsAll(rejects)); - for (Object x : accepts) assertFalse(c.contains(x)); - if (threwAt.get() == null) - assertEquals(accepts.size() + rejects.size(), n); + if (threwAt.get() == null) { + assertEquals(n - accepts.size(), c.size()); + for (Object x : accepts) assertFalse(c.contains(x)); + } else { + // Two acceptable behaviors: entire removeIf call is one + // transaction, or each element processed is one transaction. + assertTrue(n == c.size() || n == c.size() + accepts.size()); + int k = 0; + for (Object x : accepts) if (c.contains(x)) k++; + assertTrue(k == accepts.size() || k == 0); + } } catch (Throwable ex) { System.err.println(impl.klazz()); - System.err.printf("c=%s%n", c); + // c is at risk of corruption if we got here, so be lenient + try { System.err.printf("c=%s%n", c); } + catch (Throwable t) { t.printStackTrace(); } System.err.printf("n=%d%n", n); + System.err.printf("orig=%s%n", orig); System.err.printf("accepts=%s%n", accepts); System.err.printf("rejects=%s%n", rejects); System.err.printf("survivors=%s%n", survivors); - System.err.printf("threw=%s%n", threwAt.get()); + System.err.printf("threwAt=%s%n", threwAt.get()); throw ex; } } @@ -342,12 +397,12 @@ public class Collection8Test extends JSR /** * Calling Iterator#remove() after Iterator#forEachRemaining - * should remove last element + * should (maybe) remove last element */ public void testRemoveAfterForEachRemaining() { Collection c = impl.emptyCollection(); ThreadLocalRandom rnd = ThreadLocalRandom.current(); - { + testCollection: { int n = 3 + rnd.nextInt(2); for (int i = 0; i < n; i++) c.add(impl.makeElement(i)); Iterator it = c.iterator(); @@ -355,12 +410,21 @@ public class Collection8Test extends JSR assertEquals(impl.makeElement(0), it.next()); assertTrue(it.hasNext()); assertEquals(impl.makeElement(1), it.next()); - it.forEachRemaining((e) -> {}); - it.remove(); - assertEquals(n - 1, c.size()); - for (int i = 0; i < n - 1; i++) - assertTrue(c.contains(impl.makeElement(i))); - assertFalse(c.contains(impl.makeElement(n - 1))); + it.forEachRemaining(e -> assertTrue(c.contains(e))); + if (testImplementationDetails) { + if (c instanceof java.util.concurrent.ArrayBlockingQueue) { + assertIteratorExhausted(it); + } else { + try { it.remove(); } + catch (UnsupportedOperationException ok) { + break testCollection; + } + assertEquals(n - 1, c.size()); + for (int i = 0; i < n - 1; i++) + assertTrue(c.contains(impl.makeElement(i))); + assertFalse(c.contains(impl.makeElement(n - 1))); + } + } } if (c instanceof Deque) { Deque d = (Deque) impl.emptyCollection(); @@ -371,12 +435,14 @@ public class Collection8Test extends JSR assertEquals(impl.makeElement(n - 1), it.next()); assertTrue(it.hasNext()); assertEquals(impl.makeElement(n - 2), it.next()); - it.forEachRemaining((e) -> {}); - it.remove(); - assertEquals(n - 1, d.size()); - for (int i = 1; i < n; i++) - assertTrue(d.contains(impl.makeElement(i))); - assertFalse(d.contains(impl.makeElement(0))); + it.forEachRemaining(e -> assertTrue(c.contains(e))); + if (testImplementationDetails) { + it.remove(); + assertEquals(n - 1, d.size()); + for (int i = 1; i < n; i++) + assertTrue(d.contains(impl.makeElement(i))); + assertFalse(d.contains(impl.makeElement(0))); + } } } @@ -389,7 +455,7 @@ public class Collection8Test extends JSR final Object x = impl.makeElement(1); final Object y = impl.makeElement(2); final ArrayList found = new ArrayList(); - Consumer spy = (o) -> { found.add(o); }; + Consumer spy = o -> found.add(o); c.stream().forEach(spy); assertTrue(found.isEmpty()); @@ -423,7 +489,7 @@ public class Collection8Test extends JSR Runnable checkElt = () -> { threadsStarted.countDown(); while (!done.get()) - c.stream().forEach((x) -> { assertSame(x, elt); }); }; + c.stream().forEach(x -> assertSame(x, elt)); }; Runnable addRemove = () -> { threadsStarted.countDown(); while (!done.get()) { @@ -447,7 +513,7 @@ public class Collection8Test extends JSR final Object x = impl.makeElement(1); final Object y = impl.makeElement(2); final ArrayList found = new ArrayList(); - Consumer spy = (o) -> { found.add(o); }; + Consumer spy = o -> found.add(o); c.forEach(spy); assertTrue(found.isEmpty()); @@ -468,32 +534,68 @@ public class Collection8Test extends JSR assertTrue(found.isEmpty()); } - public void testForEachConcurrentStressTest() throws Throwable { + /** + * Motley crew of threads concurrently randomly hammer the collection. + */ + public void testDetectRaces() throws Throwable { if (!impl.isConcurrent()) return; + final ThreadLocalRandom rnd = ThreadLocalRandom.current(); final Collection c = impl.emptyCollection(); final long testDurationMillis = timeoutMillis(); final AtomicBoolean done = new AtomicBoolean(false); - final Object elt = impl.makeElement(1); - final Future f1, f2; + final Object one = impl.makeElement(1); + final Object two = impl.makeElement(2); + final List> futures; + final Phaser threadsStarted = new Phaser(1); // register this thread + final Runnable[] frobbers = { + () -> c.forEach(x -> assertTrue(x == one || x == two)), + () -> c.stream().forEach(x -> assertTrue(x == one || x == two)), + () -> c.spliterator().trySplit(), + () -> { + Spliterator s = c.spliterator(); + s.tryAdvance(x -> assertTrue(x == one || x == two)); + s.trySplit(); + }, + () -> { + Spliterator s = c.spliterator(); + do {} while (s.tryAdvance(x -> assertTrue(x == one || x == two))); + }, + () -> { + for (Object x : c) assertTrue(x == one || x == two); + }, + () -> { + assertTrue(c.add(one)); + assertTrue(c.contains(one)); + assertTrue(c.remove(one)); + assertFalse(c.contains(one)); + }, + () -> { + assertTrue(c.add(two)); + assertTrue(c.contains(two)); + assertTrue(c.remove(two)); + assertFalse(c.contains(two)); + }, + }; + final List tasks = + Arrays.stream(frobbers) + .filter(task -> rnd.nextBoolean()) // random subset + .map(task -> (Runnable) () -> { + threadsStarted.arriveAndAwaitAdvance(); + while (!done.get()) + task.run(); + }) + .collect(Collectors.toList()); final ExecutorService pool = Executors.newCachedThreadPool(); try (PoolCleaner cleaner = cleaner(pool, done)) { - final CountDownLatch threadsStarted = new CountDownLatch(2); - Runnable checkElt = () -> { - threadsStarted.countDown(); - while (!done.get()) - c.forEach((x) -> { assertSame(x, elt); }); }; - Runnable addRemove = () -> { - threadsStarted.countDown(); - while (!done.get()) { - assertTrue(c.add(elt)); - assertTrue(c.remove(elt)); - }}; - f1 = pool.submit(checkElt); - f2 = pool.submit(addRemove); + threadsStarted.bulkRegister(tasks.size()); + futures = tasks.stream() + .map(task -> pool.submit(task)) + .collect(Collectors.toList()); + threadsStarted.arriveAndDeregister(); Thread.sleep(testDurationMillis); } - assertNull(f1.get(0L, MILLISECONDS)); - assertNull(f2.get(0L, MILLISECONDS)); + for (Future future : futures) + assertNull(future.get(0L, MILLISECONDS)); } // public void testCollection8DebugFail() {