gemini-code-assist[bot] commented on code in PR #39362:
URL: https://github.com/apache/beam/pull/39362#discussion_r3601067119


##########
runners/kafka-streams/src/test/java/org/apache/beam/runners/kafka/streams/TestKafkaStreamsRunner.java:
##########
@@ -0,0 +1,126 @@
+/*
+ * 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.kafka.streams;
+
+import java.io.IOException;
+import java.util.UUID;
+import org.apache.beam.sdk.Pipeline;
+import org.apache.beam.sdk.PipelineResult;
+import org.apache.beam.sdk.PipelineRunner;
+import org.apache.beam.sdk.metrics.MetricResults;
+import org.apache.beam.sdk.options.PipelineOptions;
+import org.apache.beam.sdk.options.PortablePipelineOptions;
+import org.apache.beam.sdk.util.construction.Environments;
+import org.checkerframework.checker.nullness.qual.Nullable;
+import org.joda.time.Duration;
+
+/**
+ * A {@link PipelineRunner} for tests, so {@code TestPipeline}-based suites — 
including Beam's
+ * {@code @ValidatesRunner} tests — can run against the Kafka Streams runner 
without a broker.
+ *
+ * <p>{@link #run} translates the pipeline and drives it to quiescence through 
a {@code
+ * TopologyTestDriver} (via {@link KafkaStreamsTestRunner}), with the SDK 
harness in-process
+ * (EMBEDDED environment). The returned {@link PipelineResult} is terminal 
({@code DONE}) and
+ * exposes the metrics the harness reported — which is what {@code
+ * TestPipeline.verifyPAssertsSucceeded} uses to check that every {@code 
PAssert} in the pipeline
+ * actually ran and succeeded.
+ *
+ * <p>A {@code PAssert} failure inside a DoFn fails the pipeline run — over 
the Fn API the DoFn's
+ * error travels as an error string (carrying the assertion text), not as a 
Java {@link
+ * AssertionError} object. {@link #run} additionally unwraps and rethrows an 
{@link AssertionError}
+ * when one is present in the exception chain (e.g. assertions thrown 
runner-side). A {@code
+ * PAssert} that silently never runs is caught by {@code 
TestPipeline.verifyPAssertsSucceeded}
+ * comparing the success-counter metric against the number of assertions in 
the pipeline.
+ *
+ * <p>Select it with {@code 
--runner=org.apache.beam.runners.kafka.streams.TestKafkaStreamsRunner}
+ * in {@code beamTestPipelineOptions}.
+ */
+public final class TestKafkaStreamsRunner extends 
PipelineRunner<PipelineResult> {
+
+  private TestKafkaStreamsRunner() {}
+
+  /** Called reflectively by {@link PipelineRunner#fromOptions}. */
+  public static TestKafkaStreamsRunner fromOptions(PipelineOptions options) {
+    return new TestKafkaStreamsRunner();
+  }
+
+  @Override
+  public PipelineResult run(Pipeline pipeline) {
+    // Tests supply generic options; fill in what the Kafka Streams 
translation needs. The
+    // pipeline's own options are the ones the translation reads.
+    KafkaStreamsPipelineOptions options =
+        pipeline.getOptions().as(KafkaStreamsPipelineOptions.class);
+    if (options.getApplicationId() == null) {
+      options.setApplicationId("ks-validates-runner-" + UUID.randomUUID());
+    }

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   It is safer to check if the `applicationId` is either `null` or empty. If an 
empty string is provided as the application ID, it will bypass the `null` check 
but will subsequently fail in Kafka Streams, which requires a non-empty 
application ID.
   
   ```suggestion
       if (options.getApplicationId() == null || 
options.getApplicationId().isEmpty()) {
         options.setApplicationId("ks-validates-runner-" + UUID.randomUUID());
       }
   ```



##########
runners/kafka-streams/src/test/java/org/apache/beam/runners/kafka/streams/TestKafkaStreamsRunnerTest.java:
##########
@@ -0,0 +1,80 @@
+/*
+ * 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.kafka.streams;
+
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.assertThrows;
+
+import org.apache.beam.sdk.options.PipelineOptions;
+import org.apache.beam.sdk.options.PipelineOptionsFactory;
+import org.apache.beam.sdk.testing.PAssert;
+import org.apache.beam.sdk.testing.TestPipeline;
+import org.apache.beam.sdk.transforms.Count;
+import org.apache.beam.sdk.transforms.Create;
+import org.apache.beam.sdk.values.KV;
+import 
org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Throwables;
+import org.junit.Rule;
+import org.junit.Test;
+
+/**
+ * End-to-end tests for {@link TestKafkaStreamsRunner}: a {@link TestPipeline} 
with {@link PAssert}
+ * runs against the Kafka Streams runner, and {@code 
TestPipeline.verifyPAssertsSucceeded} confirms
+ * through the runner's metrics that every assertion actually executed.
+ *
+ * <p>This is the full {@code @ValidatesRunner} mechanism exercised in-module: 
PAssert's {@code
+ * containsInAnyOrder} materializes the actual PCollection through the {@code 
GBK + Flatten}
+ * bootstrap path (no side inputs), the assertion DoFn runs in the EMBEDDED 
harness, its success
+ * counter travels back through the runner's MetricResults, and a failing 
assertion surfaces as an
+ * {@link AssertionError}.
+ */
+public class TestKafkaStreamsRunnerTest {
+
+  private static PipelineOptions options() {
+    PipelineOptions options = PipelineOptionsFactory.create();
+    options.setRunner(TestKafkaStreamsRunner.class);
+    return options;
+  }
+
+  @Rule public final transient TestPipeline pipeline = 
TestPipeline.fromOptions(options());
+
+  @Test
+  public void pAssertContainsInAnyOrderSucceeds() {
+    PAssert.that(pipeline.apply("create", Create.of(1, 2, 
3))).containsInAnyOrder(3, 2, 1);
+    pipeline.run();
+  }
+
+  @Test
+  public void pAssertOnGroupedResultSucceeds() {
+    PAssert.that(
+            pipeline.apply("create", Create.of("a", "b", "a")).apply("count", 
Count.perElement()))
+        .containsInAnyOrder(KV.of("a", 2L), KV.of("b", 1L));
+    pipeline.run();
+  }
+
+  @Test
+  public void failingPAssertFailsTheRun() {
+    // Over the Fn API a DoFn failure travels as an error string, not a Java 
AssertionError object,
+    // so the guarantee for a portable runner is that a failing PAssert fails 
the pipeline run (and
+    // its message carries the assertion text). A dropped assertion — one that 
never runs — would
+    // instead be caught by TestPipeline.verifyPAssertsSucceeded via the 
runner's metrics.
+    PAssert.that(pipeline.apply("create", Create.of(1, 2, 
3))).containsInAnyOrder(1, 2, 4);
+    RuntimeException thrown = assertThrows(RuntimeException.class, 
pipeline::run);
+    assertThat(Throwables.getStackTraceAsString(thrown), 
containsString("AssertionError"));

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   Using `assertThrows(RuntimeException.class, ...)` can cause the test to fail 
if the runner's unwrapping logic successfully extracts and rethrows an 
`AssertionError` (since `AssertionError` extends `Error` and is not a subclass 
of `RuntimeException`). To make the test robust and correctly handle both 
wrapped `RuntimeException` and unwrapped `AssertionError`, assert that a 
`Throwable` is thrown instead.
   
   ```suggestion
       PAssert.that(pipeline.apply("create", Create.of(1, 2, 
3))).containsInAnyOrder(1, 2, 4);
       Throwable thrown = assertThrows(Throwable.class, pipeline::run);
   ```



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