tilgalas commented on code in PR #31498: URL: https://github.com/apache/beam/pull/31498#discussion_r1640317968
########## sdks/java/extensions/google-cloud-platform-core/src/test/java/org/apache/beam/sdk/extensions/gcp/util/channels/CountingReadableByteChannelTest.java: ########## @@ -0,0 +1,108 @@ +/* + * 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 + * + * http://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.beam.sdk.extensions.gcp.util.channels; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.channels.ReadableByteChannel; +import java.nio.channels.SeekableByteChannel; +import java.nio.charset.StandardCharsets; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.BiFunction; +import java.util.function.Consumer; +import org.apache.beam.repackaged.core.org.apache.commons.compress.utils.SeekableInMemoryByteChannel; +import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.ImmutableList; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +@RunWith(Parameterized.class) +public class CountingReadableByteChannelTest { + + private ByteBuffer byteBuffer; + private byte[] testData; + private SeekableByteChannel delegate; + private final BiFunction<SeekableByteChannel, Consumer<Integer>, ? extends ReadableByteChannel> + channelUnderTestProvider; + + public CountingReadableByteChannelTest( + BiFunction<SeekableByteChannel, Consumer<Integer>, ReadableByteChannel> + channelUnderTestProvider, + @SuppressWarnings("unused") Class<? extends ReadableByteChannel> testLabel) { + this.channelUnderTestProvider = channelUnderTestProvider; + } + + @Parameterized.Parameters(name = "{1}") + public static Iterable<Object[]> testParams() { + ImmutableList.Builder<Object[]> builder = new ImmutableList.Builder<>(); + BiFunction<SeekableByteChannel, Consumer<Integer>, CountingReadableByteChannel> + countingReadableByteChannelProvider = CountingReadableByteChannel::new; + builder.add( + new Object[] {countingReadableByteChannelProvider, CountingReadableByteChannel.class}); + + BiFunction<SeekableByteChannel, Consumer<Integer>, CountingSeekableByteChannel> + countingSeekableByteChannelProvider = + (delegate, bytesReadConsumer) -> + new CountingSeekableByteChannel(delegate, bytesReadConsumer, __ -> {}); + builder.add( + new Object[] {countingSeekableByteChannelProvider, CountingSeekableByteChannel.class}); + + return builder.build(); + } + + @Before + public void before() { + testData = "This is some test data".getBytes(StandardCharsets.UTF_8); + byteBuffer = ByteBuffer.allocate(1024); + delegate = new SeekableInMemoryByteChannel(testData); + } + + @Test + public void testCounting() throws IOException { + AtomicInteger counter = new AtomicInteger(); + ReadableByteChannel countingChannel = + channelUnderTestProvider.apply(delegate, counter::addAndGet); + + while (countingChannel.read(byteBuffer) != -1) {} + + assertEquals( + testData.length - 1, + counter.get()); // the counter will subtract the final -1, that's expected Review Comment: It's not a bug, it's a matter of defining what exactly we are passing to the callback - we have two options here, either we just pass through everything the delegate reported (current approach), or we do some additional processing there (filter out the non-negative numbers for example), I opted for the current approach since adding extra logic would violate single responsibility principle (we're no longer simply calling the consumer, we're doing something additional on top of that as well) and also because it would lose some of the information, that from now on becomes unavailable to the callback (with current approach you can still implement another class that wraps the callback with a filter, the other way around wouldn't be possible) - I guess if you think of the type not as an Integer, but rather as a sum of, let's say, BytesRead and {EOF}, with the consumer simply accepting the objects of that type, it becomes clear that it's the consumer's responsibility to distinguish betwe en those two cases whenever dealing with the value of that type. Let me know if it doesn't convince you, I'll rewrite accordingly :) -- 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]
