tilgalas commented on code in PR #31498: URL: https://github.com/apache/beam/pull/31498#discussion_r1640320544
########## 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: another approach would be to admit two types of the events that the users of the class might be interested in (bytes read, eof encountered) and allow them to pass two types of callbacks (both optional) that would be notified of them -- 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]
