rishabhdaim commented on code in PR #755: URL: https://github.com/apache/commons-collections/pull/755#discussion_r4539389826
########## src/test/java/org/apache/commons/collections4/iterators/IteratorChainCompatibilityTest.java: ########## @@ -0,0 +1,405 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.collections4.iterators; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.time.Duration; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import java.util.NoSuchElementException; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.stream.Stream; + +import org.apache.commons.collections4.IteratorUtils; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.CsvSource; +import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.params.provider.ValueSource; + +/** + * Tests the live child-chain and subclass contracts that eager flattening broke in COLLECTIONS-722. + */ +class IteratorChainCompatibilityTest { + + private enum Construction { + ADD, SINGLE, PAIR, ARRAY, COLLECTION, UTILS_PAIR, UTILS_ARRAY, UTILS_COLLECTION + } + + private static Stream<Arguments> additionCases() { + final List<Arguments> arguments = new ArrayList<>(); + for (final Construction construction : Construction.values()) { + for (final boolean unmodifiable : new boolean[] { false, true }) { + for (final boolean initiallyEmpty : new boolean[] { false, true }) { + arguments.add(Arguments.of(construction, unmodifiable, initiallyEmpty)); + } + } + } + return arguments.stream(); + } + + private static Iterator<Integer> construct(final Construction construction, final Iterator<Integer> child) { + final Iterator<Integer> tail = Collections.singletonList(9).iterator(); + switch (construction) { + case ADD: + final IteratorChain<Integer> added = new IteratorChain<>(); + added.addIterator(child); + added.addIterator(tail); + return added; + case SINGLE: + final IteratorChain<Integer> single = new IteratorChain<>(child); + single.addIterator(tail); + return single; + case PAIR: + return new IteratorChain<>(child, tail); + case ARRAY: + return new IteratorChain<>(Collections.emptyIterator(), child, tail); + case COLLECTION: + return new IteratorChain<>(Arrays.asList(child, tail)); + case UTILS_PAIR: + return IteratorUtils.chainedIterator(child, tail); + case UTILS_ARRAY: + return IteratorUtils.chainedIterator(Collections.emptyIterator(), child, tail); + case UTILS_COLLECTION: + return IteratorUtils.chainedIterator(Arrays.asList(child, tail)); + default: + throw new AssertionError(construction); + } + } + + private static <E> Iterator<E> wrap(final Iterator<E> iterator, final boolean unmodifiable) { + return unmodifiable ? UnmodifiableIterator.unmodifiableIterator(iterator) : iterator; + } + + @ParameterizedTest + @ValueSource(booleans = { false, true }) + void testAdditionsAfterParentStartsBeforeChildIsReached(final boolean unmodifiable) { + final IteratorChain<Integer> source = new IteratorChain<>(); + final IteratorChain<Integer> destination = new IteratorChain<>(Arrays.asList(0, 1).iterator(), wrap(source, unmodifiable)); + assertTrue(destination.hasNext()); + assertFalse(source.isLocked()); + source.addIterator(Collections.singletonList(2).iterator()); + assertEquals(0, destination.next()); + source.addIterator(Collections.singletonList(3).iterator()); + assertEquals(1, destination.next()); + assertFalse(source.isLocked()); + assertTrue(destination.hasNext()); + assertTrue(source.isLocked()); + assertThrows(UnsupportedOperationException.class, () -> source.addIterator(Collections.singletonList(4).iterator())); + assertEquals(Arrays.asList(2, 3), IteratorUtils.toList(destination)); + } + + @ParameterizedTest + @MethodSource("additionCases") + void testAdditionsAfterWrapping(final Construction construction, final boolean unmodifiable, final boolean initiallyEmpty) { + final IteratorChain<Integer> source = new IteratorChain<>(); + if (!initiallyEmpty) { + source.addIterator(Collections.singletonList(1).iterator()); + } + final Iterator<Integer> destination = construct(construction, wrap(source, unmodifiable)); + assertFalse(source.isLocked()); + source.addIterator(Collections.emptyIterator()); + source.addIterator(Collections.singletonList(2).iterator()); + assertEquals(initiallyEmpty ? Arrays.asList(2, 9) : Arrays.asList(1, 2, 9), IteratorUtils.toList(destination)); + assertTrue(source.isLocked()); + assertFalse(source.hasNext()); + assertThrows(UnsupportedOperationException.class, () -> source.addIterator(Collections.emptyIterator())); + } + + @ParameterizedTest + @ValueSource(booleans = { false, true }) + void testDeepLiveChains(final boolean unmodifiable) { + assertTimeoutPreemptively(Duration.ofSeconds(10), () -> { + final List<Integer> values = new ArrayList<>(Arrays.asList(1, 2)); + final AtomicInteger hasNextCalls = new AtomicInteger(); + final Iterator<Integer> delegate = values.iterator(); + final Iterator<Integer> leaf = new Iterator<Integer>() { + + @Override + public boolean hasNext() { + hasNextCalls.incrementAndGet(); + return delegate.hasNext(); + } + + @Override + public Integer next() { + return delegate.next(); + } + + @Override + public void remove() { + delegate.remove(); + } + }; + final IteratorChain<Integer> source = new IteratorChain<>(leaf); + final List<IteratorChain<Integer>> chains = new ArrayList<>(); + IteratorChain<Integer> destination = source; + for (int i = 0; i < 20000; i++) { Review Comment: The deep-chain test consumes only three elements. Add depth × element-count measurements using the existing JMH benchmark profile rather than increasing timeout-based assertions. Suggested benchmark parameters: ```java @Param({"0", "10", "100"}) public int depth; @Param({"1000", "10000"}) public int elementCount; @Param({"false", "true"}) public boolean unmodifiable; ``` Construct a fresh chain outside the measured traversal, consume results through JMH’s `Blackhole`, and compare the same matrix on base and head. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
