|
20 | 20 | import java.io.IOException; |
21 | 21 | import java.util.UUID; |
22 | 22 | import org.apache.beam.sdk.Pipeline; |
| 23 | +import org.apache.beam.sdk.Pipeline.PipelineExecutionException; |
23 | 24 | import org.apache.beam.sdk.PipelineResult; |
24 | 25 | import org.apache.beam.sdk.PipelineRunner; |
25 | 26 | import org.apache.beam.sdk.metrics.MetricResults; |
|
47 | 48 | * PAssert} that silently never runs is caught by {@code TestPipeline.verifyPAssertsSucceeded} |
48 | 49 | * comparing the success-counter metric against the number of assertions in the pipeline. |
49 | 50 | * |
| 51 | + * <p>Any other failed run is restated as a {@link PipelineExecutionException} carrying the |
| 52 | + * exception the user's code threw, which is the failure Beam's contract for {@link #run} — and the |
| 53 | + * tests that assert on a DoFn throwing — expect to see. |
| 54 | + * |
50 | 55 | * <p>Select it with {@code --runner=org.apache.beam.runners.kafka.streams.TestKafkaStreamsRunner} |
51 | 56 | * in {@code beamTestPipelineOptions}. |
52 | 57 | */ |
@@ -84,11 +89,31 @@ public PipelineResult run(Pipeline pipeline) { |
84 | 89 | throw (AssertionError) current; |
85 | 90 | } |
86 | 91 | } |
87 | | - throw t; |
| 92 | + throw pipelineExecutionExceptionFor(t); |
88 | 93 | } |
89 | 94 | return new TestKafkaStreamsPipelineResult(metrics); |
90 | 95 | } |
91 | 96 |
|
| 97 | + /** |
| 98 | + * Restates a failed run as the {@link PipelineExecutionException} Beam's contract for {@link |
| 99 | + * #run} expects, carrying the exception the user's code threw. |
| 100 | + * |
| 101 | + * <p>What surfaces from the run is the outermost layer of runner plumbing — a Kafka Streams |
| 102 | + * {@code StreamsException} naming the processor node that failed — wrapping several layers of |
| 103 | + * bundle- and Fn-API-level wrappers, with the user's exception at the bottom. Callers assert on |
| 104 | + * the user's failure, so the root cause is what {@link PipelineExecutionException} is given: |
| 105 | + * because {@code RuntimeException(Throwable)} derives its message from the cause, the user's |
| 106 | + * message ends up on the exception that {@link #run} throws. Other runners restate failures the |
| 107 | + * same way (e.g. {@code SparkPipelineResult}, {@code DirectRunner}). |
| 108 | + */ |
| 109 | + private static PipelineExecutionException pipelineExecutionExceptionFor(Throwable t) { |
| 110 | + Throwable rootCause = t; |
| 111 | + while (rootCause.getCause() != null && rootCause.getCause() != rootCause) { |
| 112 | + rootCause = rootCause.getCause(); |
| 113 | + } |
| 114 | + return new PipelineExecutionException(rootCause); |
| 115 | + } |
| 116 | + |
92 | 117 | /** Terminal result of a test run: state {@code DONE} with the harness-reported metrics. */ |
93 | 118 | private static final class TestKafkaStreamsPipelineResult implements PipelineResult { |
94 | 119 | private final MetricResults metrics; |
|
0 commit comments