From ffbac041d6b82b9c316436c54512cd00c3bd5a99 Mon Sep 17 00:00:00 2001 From: Harshil Valecha Date: Wed, 11 Nov 2026 14:59:20 +0530 Subject: [PATCH] Port BatchViewOverridesTest off DoFnTester These DoFns number their output by position within a bundle, and the tests assert those positions, so they need one bundle with a known input order. TestPipeline does not guarantee that, so the tests now run each fn in-process through DoFnRunners.simpleRunner via a small processBundle helper, the same approach as the DatastoreV1Test port. The helper collects outputs per tag, for the map-like fn's side outputs, and unwraps UserCodeException so the two exception tests keep expecting the fn's own IllegalStateException. Fixes #18693 --- .../dataflow/BatchViewOverridesTest.java | 215 ++++++++++++------ 1 file changed, 144 insertions(+), 71 deletions(-) diff --git a/runners/google-cloud-dataflow-java/src/test/java/org/apache/beam/runners/dataflow/BatchViewOverridesTest.java b/runners/google-cloud-dataflow-java/src/test/java/org/apache/beam/runners/dataflow/BatchViewOverridesTest.java index 1f160ffdf3af..a33f90092b7a 100644 --- a/runners/google-cloud-dataflow-java/src/test/java/org/apache/beam/runners/dataflow/BatchViewOverridesTest.java +++ b/runners/google-cloud-dataflow-java/src/test/java/org/apache/beam/runners/dataflow/BatchViewOverridesTest.java @@ -23,8 +23,14 @@ import static org.hamcrest.Matchers.containsInAnyOrder; import static org.junit.Assert.assertEquals; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; import java.util.List; import java.util.Map; +import org.apache.beam.runners.core.DoFnRunner; +import org.apache.beam.runners.core.DoFnRunners; +import org.apache.beam.runners.core.NullSideInputReader; import org.apache.beam.runners.dataflow.BatchViewOverrides.TransformedMap; import org.apache.beam.runners.dataflow.internal.IsmFormat; import org.apache.beam.runners.dataflow.internal.IsmFormat.IsmRecord; @@ -33,16 +39,22 @@ import org.apache.beam.sdk.coders.BigEndianLongCoder; import org.apache.beam.sdk.coders.Coder; import org.apache.beam.sdk.coders.VarLongCoder; -import org.apache.beam.sdk.transforms.DoFnTester; +import org.apache.beam.sdk.options.PipelineOptionsFactory; +import org.apache.beam.sdk.transforms.DoFn; +import org.apache.beam.sdk.transforms.DoFnSchemaInformation; import org.apache.beam.sdk.transforms.windowing.GlobalWindow; import org.apache.beam.sdk.transforms.windowing.IntervalWindow; import org.apache.beam.sdk.transforms.windowing.PaneInfo; import org.apache.beam.sdk.util.CoderUtils; +import org.apache.beam.sdk.util.UserCodeException; +import org.apache.beam.sdk.util.WindowedValueMultiReceiver; import org.apache.beam.sdk.values.KV; import org.apache.beam.sdk.values.TupleTag; import org.apache.beam.sdk.values.WindowedValue; import org.apache.beam.sdk.values.WindowedValues; import org.apache.beam.sdk.values.WindowedValues.FullWindowedValueCoder; +import org.apache.beam.sdk.values.WindowingStrategy; +import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Throwables; import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.ImmutableList; import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.ImmutableMap; import org.joda.time.Instant; @@ -59,16 +71,16 @@ public class BatchViewOverridesTest { @Test public void testBatchViewAsSingletonToIsmRecord() throws Exception { - DoFnTester< + DoFn< KV>>>, IsmRecord>> - doFnTester = - DoFnTester.of( - new BatchViewOverrides.BatchViewAsSingleton.IsmRecordForSingularValuePerWindowDoFn< - String, GlobalWindow>(GlobalWindow.Coder.INSTANCE)); + fn = + new BatchViewOverrides.BatchViewAsSingleton.IsmRecordForSingularValuePerWindowDoFn< + String, GlobalWindow>(GlobalWindow.Coder.INSTANCE); assertThat( - doFnTester.processBundle( + processBundle( + fn, ImmutableList.of( KV.of( 0, ImmutableList.of(KV.of(GlobalWindow.INSTANCE, valueInGlobalWindow("a")))))), @@ -78,17 +90,17 @@ public void testBatchViewAsSingletonToIsmRecord() throws Exception { @Test public void testBatchViewAsSingletonToIsmRecordWithMultipleValuesThrowsException() throws Exception { - DoFnTester< + DoFn< KV>>>, IsmRecord>> - doFnTester = - DoFnTester.of( - new BatchViewOverrides.BatchViewAsSingleton.IsmRecordForSingularValuePerWindowDoFn< - String, GlobalWindow>(GlobalWindow.Coder.INSTANCE)); + fn = + new BatchViewOverrides.BatchViewAsSingleton.IsmRecordForSingularValuePerWindowDoFn< + String, GlobalWindow>(GlobalWindow.Coder.INSTANCE); thrown.expect(IllegalStateException.class); thrown.expectMessage("found for singleton within window"); - doFnTester.processBundle( + processBundle( + fn, ImmutableList.of( KV.of( 0, @@ -99,13 +111,12 @@ public void testBatchViewAsSingletonToIsmRecordWithMultipleValuesThrowsException @Test public void testBatchViewAsListToIsmRecordForGlobalWindow() throws Exception { - DoFnTester>> doFnTester = - DoFnTester.of( - new BatchViewOverrides.BatchViewAsList.ToIsmRecordForGlobalWindowDoFn()); + DoFn>> fn = + new BatchViewOverrides.BatchViewAsList.ToIsmRecordForGlobalWindowDoFn(); // The order of the output elements is important relative to processing order assertThat( - doFnTester.processBundle(ImmutableList.of("a", "b", "c")), + processBundle(fn, ImmutableList.of("a", "b", "c")), contains( IsmRecord.of(ImmutableList.of(GlobalWindow.INSTANCE, 0L), valueInGlobalWindow("a")), IsmRecord.of(ImmutableList.of(GlobalWindow.INSTANCE, 1L), valueInGlobalWindow("b")), @@ -114,13 +125,12 @@ public void testBatchViewAsListToIsmRecordForGlobalWindow() throws Exception { @Test public void testBatchViewAsListToIsmRecordForNonGlobalWindow() throws Exception { - DoFnTester< + DoFn< KV>>>, IsmRecord>> - doFnTester = - DoFnTester.of( - new BatchViewOverrides.BatchViewAsList.ToIsmRecordForNonGlobalWindowDoFn< - Long, IntervalWindow>(IntervalWindow.getCoder())); + fn = + new BatchViewOverrides.BatchViewAsList.ToIsmRecordForNonGlobalWindowDoFn< + Long, IntervalWindow>(IntervalWindow.getCoder()); IntervalWindow windowA = new IntervalWindow(new Instant(0), new Instant(10)); IntervalWindow windowB = new IntervalWindow(new Instant(10), new Instant(20)); @@ -159,7 +169,7 @@ public void testBatchViewAsListToIsmRecordForNonGlobalWindow() throws Exception // The order of the output elements is important relative to processing order assertThat( - doFnTester.processBundle(inputElements), + processBundle(fn, inputElements), contains( IsmRecord.of( ImmutableList.of(windowA, 0L), @@ -197,18 +207,17 @@ public void testToIsmRecordForMapLikeDoFn() throws Exception { MetadataKeyCoder.of(keyCoder), IntervalWindow.getCoder(), BigEndianLongCoder.of()), FullWindowedValueCoder.of(VarLongCoder.of(), windowCoder)); - DoFnTester< + DoFn< KV, WindowedValue>>>, IsmRecord>> - doFnTester = - DoFnTester.of( - new BatchViewOverrides.BatchViewAsMultimap.ToIsmRecordForMapLikeDoFn<>( - outputForSizeTag, - outputForEntrySetTag, - windowCoder, - keyCoder, - ismCoder, - false /* unique keys */)); + fn = + new BatchViewOverrides.BatchViewAsMultimap.ToIsmRecordForMapLikeDoFn<>( + outputForSizeTag, + outputForEntrySetTag, + windowCoder, + keyCoder, + ismCoder, + false /* unique keys */); IntervalWindow windowA = new IntervalWindow(new Instant(0), new Instant(10)); IntervalWindow windowB = new IntervalWindow(new Instant(10), new Instant(20)); @@ -255,9 +264,17 @@ public void testToIsmRecordForMapLikeDoFn() throws Exception { WindowedValues.of( 330L, new Instant(21), windowC, PaneInfo.NO_FIRING))))); + TupleTag>> mainOutputTag = new TupleTag<>(); + BundleOutputs outputs = + processBundle( + fn, + inputElements, + mainOutputTag, + ImmutableList.of(outputForSizeTag, outputForEntrySetTag)); + // The order of the output elements is important relative to processing order assertThat( - doFnTester.processBundle(inputElements), + outputs.get(mainOutputTag), contains( IsmRecord.of( ImmutableList.of(1L, windowA, 0L), @@ -280,7 +297,7 @@ public void testToIsmRecordForMapLikeDoFn() throws Exception { // Verify the number of unique keys per window. assertThat( - doFnTester.takeOutputElements(outputForSizeTag), + outputs.get(outputForSizeTag), contains( KV.of( ismCoder.hash(ImmutableList.of(IsmFormat.getMetadataKey(), windowA)), @@ -294,7 +311,7 @@ public void testToIsmRecordForMapLikeDoFn() throws Exception { // Verify the output for the unique keys. assertThat( - doFnTester.takeOutputElements(outputForEntrySetTag), + outputs.get(outputForEntrySetTag), contains( KV.of( ismCoder.hash(ImmutableList.of(IsmFormat.getMetadataKey(), windowA)), @@ -329,18 +346,17 @@ public void testToIsmRecordForMapLikeDoFnWithoutUniqueKeysThrowsException() thro MetadataKeyCoder.of(keyCoder), IntervalWindow.getCoder(), BigEndianLongCoder.of()), FullWindowedValueCoder.of(VarLongCoder.of(), windowCoder)); - DoFnTester< + DoFn< KV, WindowedValue>>>, IsmRecord>> - doFnTester = - DoFnTester.of( - new BatchViewOverrides.BatchViewAsMultimap.ToIsmRecordForMapLikeDoFn<>( - outputForSizeTag, - outputForEntrySetTag, - windowCoder, - keyCoder, - ismCoder, - true /* unique keys */)); + fn = + new BatchViewOverrides.BatchViewAsMultimap.ToIsmRecordForMapLikeDoFn<>( + outputForSizeTag, + outputForEntrySetTag, + windowCoder, + keyCoder, + ismCoder, + true /* unique keys */); IntervalWindow windowA = new IntervalWindow(new Instant(0), new Instant(10)); @@ -363,7 +379,7 @@ public void testToIsmRecordForMapLikeDoFnWithoutUniqueKeysThrowsException() thro thrown.expect(IllegalStateException.class); thrown.expectMessage("Unique keys are expected but found key"); - doFnTester.processBundle(inputElements); + processBundle(fn, inputElements); } @Test @@ -380,11 +396,9 @@ public void testToIsmMetadataRecordForSizeDoFn() throws Exception { MetadataKeyCoder.of(keyCoder), IntervalWindow.getCoder(), BigEndianLongCoder.of()), FullWindowedValueCoder.of(VarLongCoder.of(), windowCoder)); - DoFnTester>>, IsmRecord>> - doFnTester = - DoFnTester.of( - new BatchViewOverrides.BatchViewAsMultimap.ToIsmMetadataRecordForSizeDoFn< - Long, Long, IntervalWindow>(windowCoder)); + DoFn>>, IsmRecord>> fn = + new BatchViewOverrides.BatchViewAsMultimap.ToIsmMetadataRecordForSizeDoFn< + Long, Long, IntervalWindow>(windowCoder); IntervalWindow windowA = new IntervalWindow(new Instant(0), new Instant(10)); IntervalWindow windowB = new IntervalWindow(new Instant(10), new Instant(20)); @@ -402,7 +416,7 @@ public void testToIsmMetadataRecordForSizeDoFn() throws Exception { // The order of the output elements is important relative to processing order assertThat( - doFnTester.processBundle(inputElements), + processBundle(fn, inputElements), contains( IsmRecord.>meta( ImmutableList.of(IsmFormat.getMetadataKey(), windowA, 0L), @@ -429,11 +443,9 @@ public void testToIsmMetadataRecordForKeyDoFn() throws Exception { MetadataKeyCoder.of(keyCoder), IntervalWindow.getCoder(), BigEndianLongCoder.of()), FullWindowedValueCoder.of(VarLongCoder.of(), windowCoder)); - DoFnTester>>, IsmRecord>> - doFnTester = - DoFnTester.of( - new BatchViewOverrides.BatchViewAsMultimap.ToIsmMetadataRecordForKeyDoFn< - Long, Long, IntervalWindow>(keyCoder, windowCoder)); + DoFn>>, IsmRecord>> fn = + new BatchViewOverrides.BatchViewAsMultimap.ToIsmMetadataRecordForKeyDoFn< + Long, Long, IntervalWindow>(keyCoder, windowCoder); IntervalWindow windowA = new IntervalWindow(new Instant(0), new Instant(10)); IntervalWindow windowB = new IntervalWindow(new Instant(10), new Instant(20)); @@ -456,7 +468,7 @@ public void testToIsmMetadataRecordForKeyDoFn() throws Exception { // The order of the output elements is important relative to processing order assertThat( - doFnTester.processBundle(inputElements), + processBundle(fn, inputElements), contains( IsmRecord.>meta( ImmutableList.of(IsmFormat.getMetadataKey(), windowA, 1L), @@ -476,13 +488,12 @@ public void testToIsmMetadataRecordForKeyDoFn() throws Exception { public void testToMapDoFn() throws Exception { Coder windowCoder = IntervalWindow.getCoder(); - DoFnTester< + DoFn< KV>>>>, IsmRecord, Long>>>> - doFnTester = - DoFnTester.of( - new BatchViewOverrides.BatchViewAsMap.ToMapDoFn( - windowCoder)); + fn = + new BatchViewOverrides.BatchViewAsMap.ToMapDoFn( + windowCoder); IntervalWindow windowA = new IntervalWindow(new Instant(0), new Instant(10)); IntervalWindow windowB = new IntervalWindow(new Instant(10), new Instant(20)); @@ -528,7 +539,7 @@ public void testToMapDoFn() throws Exception { // The order of the output elements is important relative to processing order List, Long>>>> output = - doFnTester.processBundle(inputElements); + processBundle(fn, inputElements); assertEquals(3, output.size()); Map outputMap; @@ -549,14 +560,13 @@ public void testToMapDoFn() throws Exception { public void testToMultimapDoFn() throws Exception { Coder windowCoder = IntervalWindow.getCoder(); - DoFnTester< + DoFn< KV>>>>, IsmRecord< WindowedValue>, Iterable>>>> - doFnTester = - DoFnTester.of( - new BatchViewOverrides.BatchViewAsMultimap.ToMultimapDoFn< - Long, Long, IntervalWindow>(windowCoder)); + fn = + new BatchViewOverrides.BatchViewAsMultimap.ToMultimapDoFn( + windowCoder); IntervalWindow windowA = new IntervalWindow(new Instant(0), new Instant(10)); IntervalWindow windowB = new IntervalWindow(new Instant(10), new Instant(20)); @@ -614,7 +624,7 @@ public void testToMultimapDoFn() throws Exception { List< IsmRecord< WindowedValue>, Iterable>>>> - output = doFnTester.processBundle(inputElements); + output = processBundle(fn, inputElements); assertEquals(3, output.size()); Map> outputMap; @@ -632,4 +642,67 @@ public void testToMultimapDoFn() throws Exception { assertEquals(1, outputMap.size()); assertThat(outputMap.get(4L), containsInAnyOrder(41L)); } + + /** Outputs of one bundle, by tag, in the order the fn emitted them. */ + private static class BundleOutputs { + private final Map, List> byTag = new HashMap<>(); + + @SuppressWarnings("unchecked") + List get(TupleTag tag) { + return (List) byTag.getOrDefault(tag, Collections.emptyList()); + } + } + + private static List processBundle( + DoFn fn, Iterable inputs) { + TupleTag mainOutputTag = new TupleTag<>(); + return processBundle(fn, inputs, mainOutputTag, Collections.emptyList()).get(mainOutputTag); + } + + /** + * Runs {@code fn} over {@code inputs} in a single bundle, in-process, in the global window. These + * fns number their output by position within the bundle, so the tests need one bundle with a + * known input order, which a {@code TestPipeline} does not guarantee. + */ + private static BundleOutputs processBundle( + DoFn fn, + Iterable inputs, + TupleTag mainOutputTag, + List> additionalOutputTags) { + BundleOutputs outputs = new BundleOutputs(); + DoFnRunner runner = + DoFnRunners.simpleRunner( + PipelineOptionsFactory.create(), + fn, + NullSideInputReader.empty(), + new WindowedValueMultiReceiver() { + @Override + public void output(TupleTag tag, WindowedValue output) { + outputs + .byTag + .computeIfAbsent(tag, unused -> new ArrayList<>()) + .add(output.getValue()); + } + }, + mainOutputTag, + additionalOutputTags, + null, + null, + Collections.emptyMap(), + WindowingStrategy.globalDefault(), + DoFnSchemaInformation.create(), + Collections.emptyMap()); + try { + runner.startBundle(); + for (InputT input : inputs) { + runner.processElement(WindowedValues.valueInGlobalWindow(input)); + } + runner.finishBundle(); + } catch (UserCodeException e) { + // Surface the fn's own exception, as the tests expect it rather than the runner's wrapper. + Throwables.throwIfUnchecked(e.getCause()); + throw e; + } + return outputs; + } }