robertwb commented on code in PR #31498: URL: https://github.com/apache/beam/pull/31498#discussion_r1640427758
########## 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: True, that is a valid interpretation, but I would say quite a surprising one. When I see `CountingReadableByteChannel` and `Consumer<Integer> bytesReadConsumer` I don't interpret these as "number of bytes read minus number of EOF calls" or "sum of the read(...) return values" but as just "number of bytes read." As such I think it's totally valid for CountingReadableByteChannel to interpret the results and only invoke the callback for bytes read. If we cared about the number of times read was called after EOF was reached, the clean way would be to add another callback. (Java overloads there return value 'cause there's no way to efficiently return union types, and 'cause that's the way C did it for the same reason...) But I don't think that's worth its weight--we don't care about this value (and if, for some reason, we do later, we can always add it then.) So let's filter out the negative (or, indeed, non-positive) values and have the callback just receive the number of bytes read. -- 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: github-unsubscr...@beam.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org