lukecwik commented on a change in pull request #16439:
URL: https://github.com/apache/beam/pull/16439#discussion_r799898892



##########
File path: 
sdks/java/fn-execution/src/test/java/org/apache/beam/sdk/fn/data/BeamFnDataOutboundAggregatorTest.java
##########
@@ -0,0 +1,385 @@
+/*
+ * 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.fn.data;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.empty;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+import com.google.common.collect.Iterables;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.function.Consumer;
+import org.apache.beam.model.fnexecution.v1.BeamFnApi;
+import org.apache.beam.model.fnexecution.v1.BeamFnApi.Elements;
+import org.apache.beam.sdk.coders.ByteArrayCoder;
+import org.apache.beam.sdk.coders.Coder;
+import org.apache.beam.sdk.coders.LengthPrefixCoder;
+import org.apache.beam.sdk.fn.data.BeamFnDataOutboundAggregator.Receiver;
+import org.apache.beam.sdk.fn.test.TestStreams;
+import org.apache.beam.sdk.options.ExperimentalOptions;
+import org.apache.beam.sdk.options.PipelineOptions;
+import org.apache.beam.sdk.options.PipelineOptionsFactory;
+import org.apache.beam.vendor.grpc.v1p43p2.com.google.protobuf.ByteString;
+import org.hamcrest.Matchers;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+
+/** Tests for {@link BeamFnDataOutboundAggregator}. */
+@RunWith(Parameterized.class)
+public class BeamFnDataOutboundAggregatorTest {
+
+  private static final LogicalEndpoint DATA_OUTPUT_LOCATION = 
LogicalEndpoint.data("777L", "555L");
+  private static final LogicalEndpoint TIMER_OUTPUT_LOCATION =
+      LogicalEndpoint.timer("999L", "333L", "111L");
+  private static final Coder<byte[]> CODER = 
LengthPrefixCoder.of(ByteArrayCoder.of());
+
+  @Parameters
+  public static Collection<LogicalEndpoint> data() {
+    return Arrays.asList(DATA_OUTPUT_LOCATION, TIMER_OUTPUT_LOCATION);
+  }
+
+  private final LogicalEndpoint endpoint;
+
+  public BeamFnDataOutboundAggregatorTest(LogicalEndpoint endpoint) {
+    this.endpoint = endpoint;
+  }
+
+  @Test
+  public void testWithDefaultBuffer() throws Exception {
+    final List<Elements> values = new ArrayList<>();
+    final AtomicBoolean onCompletedWasCalled = new AtomicBoolean();
+    BeamFnDataOutboundAggregator aggregator =
+        new BeamFnDataOutboundAggregator(
+            PipelineOptionsFactory.create(),
+            endpoint::getInstructionId,
+            TestStreams.<Elements>withOnNext(values::add)
+                .withOnCompleted(() -> onCompletedWasCalled.set(true))
+                .build());
+
+    // Test that nothing is emitted till the default buffer size is surpassed.
+    FnDataReceiver<byte[]> dataReceiver = registerOutputLocation(aggregator, 
endpoint, CODER);
+    aggregator.startFlushThread();
+    dataReceiver.accept(new 
byte[BeamFnDataOutboundAggregator.DEFAULT_BUFFER_LIMIT_BYTES - 50]);
+    assertThat(values, empty());
+
+    // Test that when we cross the buffer, we emit.
+    dataReceiver.accept(new byte[50]);
+    assertEquals(
+        messageWithData(
+            new byte[BeamFnDataOutboundAggregator.DEFAULT_BUFFER_LIMIT_BYTES - 
50], new byte[50]),
+        values.get(0));
+
+    // Test that nothing is emitted till the default buffer size is surpassed 
after a reset
+    dataReceiver.accept(new 
byte[BeamFnDataOutboundAggregator.DEFAULT_BUFFER_LIMIT_BYTES - 50]);
+    assertEquals(1, values.size());
+
+    // Test that when we cross the buffer, we emit.
+    dataReceiver.accept(new byte[50]);
+    assertEquals(
+        messageWithData(
+            new byte[BeamFnDataOutboundAggregator.DEFAULT_BUFFER_LIMIT_BYTES - 
50], new byte[50]),
+        values.get(1));
+
+    // Test that when we close with an empty buffer we only have one end of 
stream
+    aggregator.sendBufferedDataAndFinishOutboundStreams();
+    assertEquals(endMessage(), values.get(2));
+
+    // Test that we can close twice.
+    aggregator.sendBufferedDataAndFinishOutboundStreams();
+    assertEquals(endMessage(), values.get(2));
+  }
+
+  @Test
+  public void testConfiguredBufferLimit() throws Exception {
+    List<BeamFnApi.Elements> values = new ArrayList<>();
+    AtomicBoolean onCompletedWasCalled = new AtomicBoolean();
+    PipelineOptions options = PipelineOptionsFactory.create();
+    options
+        .as(ExperimentalOptions.class)
+        .setExperiments(Arrays.asList("data_buffer_size_limit=100"));
+    BeamFnDataOutboundAggregator aggregator =
+        new BeamFnDataOutboundAggregator(
+            options,
+            endpoint::getInstructionId,
+            TestStreams.<Elements>withOnNext(values::add)
+                .withOnCompleted(() -> onCompletedWasCalled.set(true))
+                .build());
+    // Test that nothing is emitted till the default buffer size is surpassed.
+    FnDataReceiver<byte[]> dataReceiver = registerOutputLocation(aggregator, 
endpoint, CODER);
+    aggregator.startFlushThread();
+    dataReceiver.accept(new byte[51]);
+    assertThat(values, empty());
+
+    // Test that when we cross the buffer, we emit.
+    dataReceiver.accept(new byte[49]);
+    assertEquals(messageWithData(new byte[51], new byte[49]), values.get(0));
+    Receiver<?> receiver;
+    if (endpoint.isTimer()) {
+      receiver = 
Iterables.getOnlyElement(aggregator.outputTimersReceivers.values());
+    } else {
+      receiver = 
Iterables.getOnlyElement(aggregator.outputDataReceivers.values());
+    }
+    assertEquals(0L, receiver.getOutput().size());
+    assertEquals(102L, receiver.getByteCount());
+    assertEquals(2L, receiver.getElementCount());
+
+    // Test that when we close we empty the value, and then send the stream 
terminator as part
+    // of the same message
+    dataReceiver.accept(new byte[1]);
+    aggregator.sendBufferedDataAndFinishOutboundStreams();
+    // Test that receiver stats have been reset after 
sendBufferedDataAndFinishOutboundStreams.
+    assertEquals(0L, receiver.getByteCount());

Review comment:
       ```suggestion
       assertEquals(0L, receiver.getOutput().size());
       assertEquals(0L, receiver.getByteCount());
   ```

##########
File path: 
sdks/java/fn-execution/src/test/java/org/apache/beam/sdk/fn/data/BeamFnDataOutboundAggregatorTest.java
##########
@@ -0,0 +1,385 @@
+/*
+ * 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.fn.data;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.empty;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+import com.google.common.collect.Iterables;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.function.Consumer;
+import org.apache.beam.model.fnexecution.v1.BeamFnApi;
+import org.apache.beam.model.fnexecution.v1.BeamFnApi.Elements;
+import org.apache.beam.sdk.coders.ByteArrayCoder;
+import org.apache.beam.sdk.coders.Coder;
+import org.apache.beam.sdk.coders.LengthPrefixCoder;
+import org.apache.beam.sdk.fn.data.BeamFnDataOutboundAggregator.Receiver;
+import org.apache.beam.sdk.fn.test.TestStreams;
+import org.apache.beam.sdk.options.ExperimentalOptions;
+import org.apache.beam.sdk.options.PipelineOptions;
+import org.apache.beam.sdk.options.PipelineOptionsFactory;
+import org.apache.beam.vendor.grpc.v1p43p2.com.google.protobuf.ByteString;
+import org.hamcrest.Matchers;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+
+/** Tests for {@link BeamFnDataOutboundAggregator}. */
+@RunWith(Parameterized.class)
+public class BeamFnDataOutboundAggregatorTest {
+
+  private static final LogicalEndpoint DATA_OUTPUT_LOCATION = 
LogicalEndpoint.data("777L", "555L");
+  private static final LogicalEndpoint TIMER_OUTPUT_LOCATION =
+      LogicalEndpoint.timer("999L", "333L", "111L");
+  private static final Coder<byte[]> CODER = 
LengthPrefixCoder.of(ByteArrayCoder.of());
+
+  @Parameters
+  public static Collection<LogicalEndpoint> data() {
+    return Arrays.asList(DATA_OUTPUT_LOCATION, TIMER_OUTPUT_LOCATION);
+  }
+
+  private final LogicalEndpoint endpoint;
+
+  public BeamFnDataOutboundAggregatorTest(LogicalEndpoint endpoint) {
+    this.endpoint = endpoint;
+  }
+
+  @Test
+  public void testWithDefaultBuffer() throws Exception {
+    final List<Elements> values = new ArrayList<>();
+    final AtomicBoolean onCompletedWasCalled = new AtomicBoolean();
+    BeamFnDataOutboundAggregator aggregator =
+        new BeamFnDataOutboundAggregator(
+            PipelineOptionsFactory.create(),
+            endpoint::getInstructionId,
+            TestStreams.<Elements>withOnNext(values::add)
+                .withOnCompleted(() -> onCompletedWasCalled.set(true))
+                .build());
+
+    // Test that nothing is emitted till the default buffer size is surpassed.
+    FnDataReceiver<byte[]> dataReceiver = registerOutputLocation(aggregator, 
endpoint, CODER);
+    aggregator.startFlushThread();
+    dataReceiver.accept(new 
byte[BeamFnDataOutboundAggregator.DEFAULT_BUFFER_LIMIT_BYTES - 50]);
+    assertThat(values, empty());
+
+    // Test that when we cross the buffer, we emit.
+    dataReceiver.accept(new byte[50]);
+    assertEquals(
+        messageWithData(
+            new byte[BeamFnDataOutboundAggregator.DEFAULT_BUFFER_LIMIT_BYTES - 
50], new byte[50]),
+        values.get(0));
+
+    // Test that nothing is emitted till the default buffer size is surpassed 
after a reset
+    dataReceiver.accept(new 
byte[BeamFnDataOutboundAggregator.DEFAULT_BUFFER_LIMIT_BYTES - 50]);
+    assertEquals(1, values.size());
+
+    // Test that when we cross the buffer, we emit.
+    dataReceiver.accept(new byte[50]);
+    assertEquals(
+        messageWithData(
+            new byte[BeamFnDataOutboundAggregator.DEFAULT_BUFFER_LIMIT_BYTES - 
50], new byte[50]),
+        values.get(1));
+
+    // Test that when we close with an empty buffer we only have one end of 
stream
+    aggregator.sendBufferedDataAndFinishOutboundStreams();
+    assertEquals(endMessage(), values.get(2));
+
+    // Test that we can close twice.
+    aggregator.sendBufferedDataAndFinishOutboundStreams();
+    assertEquals(endMessage(), values.get(2));
+  }
+
+  @Test
+  public void testConfiguredBufferLimit() throws Exception {
+    List<BeamFnApi.Elements> values = new ArrayList<>();
+    AtomicBoolean onCompletedWasCalled = new AtomicBoolean();
+    PipelineOptions options = PipelineOptionsFactory.create();
+    options
+        .as(ExperimentalOptions.class)
+        .setExperiments(Arrays.asList("data_buffer_size_limit=100"));
+    BeamFnDataOutboundAggregator aggregator =
+        new BeamFnDataOutboundAggregator(
+            options,
+            endpoint::getInstructionId,
+            TestStreams.<Elements>withOnNext(values::add)
+                .withOnCompleted(() -> onCompletedWasCalled.set(true))
+                .build());
+    // Test that nothing is emitted till the default buffer size is surpassed.
+    FnDataReceiver<byte[]> dataReceiver = registerOutputLocation(aggregator, 
endpoint, CODER);
+    aggregator.startFlushThread();
+    dataReceiver.accept(new byte[51]);
+    assertThat(values, empty());
+
+    // Test that when we cross the buffer, we emit.
+    dataReceiver.accept(new byte[49]);
+    assertEquals(messageWithData(new byte[51], new byte[49]), values.get(0));
+    Receiver<?> receiver;
+    if (endpoint.isTimer()) {
+      receiver = 
Iterables.getOnlyElement(aggregator.outputTimersReceivers.values());
+    } else {
+      receiver = 
Iterables.getOnlyElement(aggregator.outputDataReceivers.values());
+    }

Review comment:
       Since it is the data receiver why would you need the if?




-- 
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]


Reply via email to