--- jsr166/src/test/tck/Collection8Test.java 2016/02/22 19:43:54 1.2 +++ jsr166/src/test/tck/Collection8Test.java 2016/10/09 05:25:09 1.3 @@ -42,7 +42,7 @@ public class Collection8Test extends JSR /** * stream().forEach returns elements in the collection */ - public void testForEach() throws Throwable { + public void testStreamForEach() throws Throwable { final Collection c = impl.emptyCollection(); final AtomicLong count = new AtomicLong(0L); final Object x = impl.makeElement(1); @@ -69,7 +69,7 @@ public class Collection8Test extends JSR assertTrue(found.isEmpty()); } - public void testForEachConcurrentStressTest() throws Throwable { + public void testStreamForEachConcurrentStressTest() throws Throwable { if (!impl.isConcurrent()) return; final Collection c = impl.emptyCollection(); final long testDurationMillis = timeoutMillis(); @@ -86,6 +86,64 @@ public class Collection8Test extends JSR Runnable addRemove = () -> { threadsStarted.countDown(); while (!done.get()) { + assertTrue(c.add(elt)); + assertTrue(c.remove(elt)); + }}; + f1 = pool.submit(checkElt); + f2 = pool.submit(addRemove); + Thread.sleep(testDurationMillis); + } + assertNull(f1.get(0L, MILLISECONDS)); + assertNull(f2.get(0L, MILLISECONDS)); + } + + /** + * collection.forEach returns elements in the collection + */ + public void testForEach() throws Throwable { + final Collection c = impl.emptyCollection(); + final AtomicLong count = new AtomicLong(0L); + final Object x = impl.makeElement(1); + final Object y = impl.makeElement(2); + final ArrayList found = new ArrayList(); + Consumer spy = (o) -> { found.add(o); }; + c.forEach(spy); + assertTrue(found.isEmpty()); + + assertTrue(c.add(x)); + c.forEach(spy); + assertEquals(Collections.singletonList(x), found); + found.clear(); + + assertTrue(c.add(y)); + c.forEach(spy); + assertEquals(2, found.size()); + assertTrue(found.contains(x)); + assertTrue(found.contains(y)); + found.clear(); + + c.clear(); + c.forEach(spy); + assertTrue(found.isEmpty()); + } + + public void testForEachConcurrentStressTest() throws Throwable { + if (!impl.isConcurrent()) return; + 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 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)); }};