On Mon, Nov 21, 2016 at 1:06 PM, Paul Sandoz <paul.san...@oracle.com> wrote:
> > See the SpliteratorLateBindingFailFastTest. That could be refactored out > as it’s conflating two concepts and is too collection/map focused. > Here's my own attempt, for the pending jsr166 integration: --- src/test/tck/Collection8Test.java 22 Nov 2016 01:08:14 -0000 1.29 +++ src/test/tck/Collection8Test.java 23 Nov 2016 00:20:43 -0000 @@ -606,6 +606,39 @@ assertNull(future.get(0L, MILLISECONDS)); } + /** + * Spliterators are either IMMUTABLE or truly late-binding or, if + * concurrent, use the same "late-binding style" of returning + * elements added between creation and first use. + */ + public void testLateBinding() throws Throwable { + if (!testImplementationDetails) return; + // Immutable (snapshot) spliterators are exempt + if (impl.emptyCollection().spliterator() + .hasCharacteristics(Spliterator.IMMUTABLE)) + return; + final Object one = impl.makeElement(1); + { + final Collection c = impl.emptyCollection(); + final Spliterator split = c.spliterator(); + c.add(one); + assertTrue(split.tryAdvance(e -> { assertSame(e, one); })); + assertFalse(split.tryAdvance(e -> { throw new AssertionError(); })); + assertTrue(c.contains(one)); + } + { + final AtomicLong count = new AtomicLong(0); + final Collection c = impl.emptyCollection(); + final Spliterator split = c.spliterator(); + c.add(one); + split.forEachRemaining( + e -> { assertSame(e, one); count.getAndIncrement(); }); + assertEquals(1L, count.get()); + assertFalse(split.tryAdvance(e -> { throw new AssertionError(); })); + assertTrue(c.contains(one)); + } + } +