tkaymak commented on code in PR #40090:
URL: https://github.com/apache/beam/pull/40090#discussion_r3987111529


##########
runners/spark/4/src/test/java/org/apache/beam/runners/spark/structuredstreaming/translation/streaming/StreamingTestUtils.java:
##########
@@ -0,0 +1,154 @@
+/*
+ * 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.runners.spark.structuredstreaming.translation.streaming;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.UUID;
+import java.util.concurrent.ConcurrentHashMap;
+import 
org.apache.beam.runners.spark.structuredstreaming.SparkStructuredStreamingPipelineOptions;
+import 
org.apache.beam.runners.spark.structuredstreaming.SparkStructuredStreamingRunner;
+import org.apache.beam.sdk.Pipeline;
+import org.apache.beam.sdk.PipelineResult;
+import org.apache.beam.sdk.options.PipelineOptionsFactory;
+import org.apache.beam.sdk.transforms.DoFn;
+import 
org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Preconditions;
+import org.joda.time.Duration;
+import org.junit.rules.TemporaryFolder;
+
+/**
+ * Shared test utilities for the Spark 4 streaming translators.
+ *
+ * <p>Collectors are static so they work in local mode only.
+ *
+ * <p>The {@link #run} helper bounds each query at five minutes and cancels on 
expiry.
+ *
+ * <p>Tests poll with deadlines instead of {@code @Test(timeout)}, JUnit runs 
a timed test in a
+ * separate thread group and Spark's static thread pools inherit it.
+ */
+public final class StreamingTestUtils {
+
+  private StreamingTestUtils() {}
+
+  /** Driver side, per collector id accumulation of every element a {@link 
CollectDoFn} saw. */
+  private static final Map<String, List<Object>> COLLECTORS = new 
ConcurrentHashMap<>();
+
+  /**
+   * Appends every element to a static collector named {@code collectorId}, 
then passes it through.
+   * Safe to use concurrently. Works in Spark local mode only.
+   */
+  public static final class CollectDoFn<T> extends DoFn<T, T> {
+    private final String collectorId;
+
+    public CollectDoFn(String collectorId) {
+      this.collectorId = Preconditions.checkNotNull(collectorId);
+    }
+
+    @ProcessElement
+    public void processElement(@Element T element, OutputReceiver<T> out) {
+      append(collectorId, element);
+      out.output(element);
+    }
+  }
+
+  private static void append(String collectorId, Object value) {
+    COLLECTORS
+        .computeIfAbsent(collectorId, unused -> 
Collections.synchronizedList(new ArrayList<>()))
+        .add(value);
+  }
+
+  /** Returns a snapshot of everything collected so far under {@code 
collectorId}. */
+  @SuppressWarnings("unchecked")
+  public static <T> List<T> getCollected(String collectorId) {

Review Comment:
   Done, collected() returns one Set snapshot and the callers use it directly.



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