robertwb commented on code in PR #31498: URL: https://github.com/apache/beam/pull/31498#discussion_r1640085148
########## 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}") Review Comment: Consider whether https://testing.googleblog.com/2019/12/testing-on-toilet-tests-too-dry-make.html applies here, especially as there are only two methods being tested. (Similarly above.) ########## sdks/java/extensions/google-cloud-platform-core/src/test/java/org/apache/beam/sdk/extensions/gcp/util/channels/CountingChannelsTest.java: ########## @@ -0,0 +1,92 @@ +/* + * 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.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.nio.channels.Channel; +import java.nio.channels.SeekableByteChannel; +import java.util.function.Function; +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 CountingChannelsTest { + + private final Function<SeekableByteChannel, Channel> channelUnderTestProvider; + private Channel channelUnderTest; + private SeekableByteChannel delegate; + + public CountingChannelsTest( + Function<SeekableByteChannel, Channel> channelUnderTestProvider, + @SuppressWarnings("unused") Class<? extends Channel> testLabel) { + this.channelUnderTestProvider = channelUnderTestProvider; + } + + @Before + public void before() { + delegate = new SeekableInMemoryByteChannel(); + channelUnderTest = channelUnderTestProvider.apply(delegate); + } + + @Parameterized.Parameters(name = "{1}") + public static Iterable<Object[]> testParams() { + + ImmutableList.Builder<Object[]> paramBuilder = ImmutableList.builder(); + + Function<SeekableByteChannel, Channel> countingReadableByteChannelProvider = + delegate -> new CountingReadableByteChannel(delegate, __ -> {}); + + paramBuilder.add( + new Object[] {countingReadableByteChannelProvider, CountingReadableByteChannel.class}); + + Function<SeekableByteChannel, Channel> countingWritableByteChannelProvider = + delegate -> new CountingWritableByteChannel(delegate, __ -> {}); + paramBuilder.add( + new Object[] {countingWritableByteChannelProvider, CountingWritableByteChannel.class}); + + Function<SeekableByteChannel, Channel> countingSeekableByteChannelProvider = + delegate -> new CountingSeekableByteChannel(delegate, __ -> {}, __ -> {}); + paramBuilder.add( + new Object[] {countingSeekableByteChannelProvider, CountingSeekableByteChannel.class}); + + return paramBuilder.build(); + } + + @Test + public void testIsOpen() throws IOException { + Review Comment: Nit: extra newline? (And below.) ########## 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: IMHO, this test just uncovered a bug (that I didn't see before)--subtracting one is neither expected nor desirable. -- 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]
