damccorm commented on code in PR #30123: URL: https://github.com/apache/beam/pull/30123#discussion_r1481919319
########## sdks/java/io/rrio/src/test/java/org/apache/beam/io/requestresponse/ThrottleTest.java: ########## @@ -0,0 +1,354 @@ +/* + * 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.io.requestresponse; + +import static org.apache.beam.io.requestresponse.Throttle.INPUT_ELEMENTS_COUNTER_NAME; +import static org.apache.beam.io.requestresponse.Throttle.OUTPUT_ELEMENTS_COUNTER_NAME; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.greaterThan; +import static org.hamcrest.Matchers.lessThan; +import static org.hamcrest.Matchers.notNullValue; + +import com.google.protobuf.ByteString; +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.stream.Collectors; +import java.util.stream.Stream; +import java.util.stream.StreamSupport; +import org.apache.beam.sdk.PipelineResult; +import org.apache.beam.sdk.coders.VarIntCoder; +import org.apache.beam.sdk.metrics.MetricNameFilter; +import org.apache.beam.sdk.metrics.MetricQueryResults; +import org.apache.beam.sdk.metrics.MetricResult; +import org.apache.beam.sdk.metrics.MetricResults; +import org.apache.beam.sdk.metrics.MetricsFilter; +import org.apache.beam.sdk.testing.PAssert; +import org.apache.beam.sdk.testing.TestPipeline; +import org.apache.beam.sdk.transforms.Create; +import org.apache.beam.sdk.transforms.DoFn; +import org.apache.beam.sdk.transforms.MapElements; +import org.apache.beam.sdk.transforms.ParDo; +import org.apache.beam.sdk.transforms.PeriodicImpulse; +import org.apache.beam.sdk.transforms.SerializableFunction; +import org.apache.beam.sdk.transforms.windowing.DefaultTrigger; +import org.apache.beam.sdk.transforms.windowing.GlobalWindows; +import org.apache.beam.sdk.transforms.windowing.Sessions; +import org.apache.beam.sdk.transforms.windowing.Window; +import org.apache.beam.sdk.values.PCollection; +import org.apache.beam.sdk.values.TypeDescriptor; +import org.apache.beam.testinfra.mockapis.echo.v1.Echo; +import org.hamcrest.Matcher; +import org.joda.time.Duration; +import org.joda.time.Instant; +import org.joda.time.ReadableDuration; +import org.junit.Rule; +import org.junit.Test; + +/** Tests for {@link Throttle}. */ +public class ThrottleTest { + @Rule public TestPipeline pipeline = TestPipeline.create(); + + /** + * Tests whether a pulse of elements totaled less than the maximum rate are just emitted + * immediately without throttling. + */ + @Test + public void givenElementSizeNotExceedsRate_thenEmitsAllImmediately() { + Rate rate = Rate.of(10, Duration.standardSeconds(1L)); + long expectedMillis = rate.getInterval().getMillis(); + long toleratedError = (long) (0.05 * (double) expectedMillis); + // tolerate 5% error. + Duration expectedInterval = Duration.millis(expectedMillis + toleratedError); + List<Integer> items = Stream.iterate(0, i -> i + 1).limit(3).collect(Collectors.toList()); + PCollection<Integer> throttled = pipeline.apply(Create.of(items)).apply(transformOf(rate)); + + PAssert.that(throttled).containsInAnyOrder(items); + PAssert.that(timestampsOf(throttled)) + .satisfies( + itr -> { + List<Instant> timestamps = + StreamSupport.stream(itr.spliterator(), true) + .sorted() + .collect(Collectors.toList()); + assertTimestampIntervalsMatch(timestamps, lessThan(expectedInterval)); + return null; + }); + + pipeline.run(); + } + + /** Tests whether a pulse of elements totaled greater than the maximum rate are throttled. */ + @Test + public void givenElementSizeExceedsRate_thenEmitsAtRate() { + Rate rate = Rate.of(1, Duration.standardSeconds(1L)); + long expectedMillis = rate.getInterval().getMillis(); + // tolerate 5% error. + long toleratedError = (long) (0.05 * (double) expectedMillis); + Duration expectedInterval = Duration.millis(expectedMillis - toleratedError); + List<Integer> items = Stream.iterate(0, i -> i + 1).limit(3).collect(Collectors.toList()); + PCollection<Integer> throttled = pipeline.apply(Create.of(items)).apply(transformOf(rate)); + + PAssert.that(throttled).containsInAnyOrder(items); + PAssert.that(timestampsOf(throttled)) + .satisfies( + itr -> { + List<Instant> timestamps = + StreamSupport.stream(itr.spliterator(), true) + .sorted() + .collect(Collectors.toList()); + assertTimestampIntervalsMatch(timestamps, greaterThan(expectedInterval)); + return null; + }); + + pipeline.run(); + } + + @Test + public void givenLargerElementSize_noDataLost() { + Rate rate = Rate.of(1_000, Duration.standardSeconds(1L)); + List<Integer> items = Stream.iterate(0, i -> i + 1).limit(3_000).collect(Collectors.toList()); + PCollection<Integer> throttled = pipeline.apply(Create.of(items)).apply(transformOf(rate)); + + PAssert.that(throttled).containsInAnyOrder(items); + + pipeline.run(); + } + + /** Tests withMetricsCollected that Counters populate appropriately. */ + @Test + public void givenCollectMetricsTrue_thenPopulatesMetrics() { + long size = 300; + Rate rate = Rate.of(100, Duration.standardSeconds(1L)); + + List<Integer> list = Stream.iterate(0, i -> i + 1).limit(size).collect(Collectors.toList()); + + pipeline.apply(Create.of(list)).apply(transformOf(rate).withMetricsCollected()); + + PipelineResult pipelineResult = pipeline.run(); + pipelineResult.waitUntilFinish(); + MetricResults results = pipelineResult.metrics(); + + assertThat(getCount(results, INPUT_ELEMENTS_COUNTER_NAME), equalTo(size)); + assertThat(getCount(results, OUTPUT_ELEMENTS_COUNTER_NAME), equalTo(size)); + } + + /** Tests that an upstream GlobalWindows gets reapplied to the resulting PCollection. */ + @Test + public void givenUpstreamGlobalWindows_thenReassignedToGlobalWindow() { + PCollection<Integer> throttled = + pipeline + .apply(Create.of(1, 2, 3)) + .apply(Window.into(new GlobalWindows())) + .apply(transformOf(Rate.of(1, Duration.standardSeconds(1L)))); + + assertThat(throttled.getWindowingStrategy().getWindowFn(), equalTo(new GlobalWindows())); + + pipeline.run(); + } Review Comment: @damondouglas it looks like this use case still isn't tested, would you mind covering it? If it is and I just missed it, please point me to the test case -- 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]
