Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
*/
package org.apache.beam.runners.flink;

import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;

Expand All @@ -31,6 +30,7 @@
import org.apache.beam.model.jobmanagement.v1.JobApi.JobState.Enum;
import org.apache.beam.model.pipeline.v1.RunnerApi;
import org.apache.beam.runners.core.construction.Environments;
import org.apache.beam.runners.core.construction.JavaReadViaImpulse;
import org.apache.beam.runners.core.construction.PipelineTranslation;
import org.apache.beam.sdk.Pipeline;
import org.apache.beam.sdk.coders.StringUtf8Coder;
Expand All @@ -45,11 +45,13 @@
import org.apache.beam.sdk.state.TimerSpecs;
import org.apache.beam.sdk.state.ValueState;
import org.apache.beam.sdk.testing.CrashingRunner;
import org.apache.beam.sdk.testing.PAssert;
import org.apache.beam.sdk.transforms.DoFn;
import org.apache.beam.sdk.transforms.Impulse;
import org.apache.beam.sdk.transforms.ParDo;
import org.apache.beam.sdk.transforms.windowing.BoundedWindow;
import org.apache.beam.sdk.values.KV;
import org.apache.beam.sdk.values.PCollection;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -74,11 +76,8 @@ public static Object[] testModes() {

private transient ListeningExecutorService flinkJobExecutor;

private static List<KV<String, Integer>> results = new ArrayList<>();

@Before
public void setup() {
results.clear();
flinkJobExecutor = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool());
}

Expand All @@ -87,7 +86,7 @@ public void tearDown() {
flinkJobExecutor.shutdown();
}

@Test(timeout = 60_000)
@Test(timeout = 120_000)
public void testTimerExecution() throws Exception {
PipelineOptions options = PipelineOptionsFactory.create();
options.setRunner(CrashingRunner.class);
Expand Down Expand Up @@ -116,6 +115,7 @@ public void testTimerExecution() throws Exception {
expectedOutput.add(KV.of(key.toString(), i + offset));
}
}

Collections.shuffle(input);

DoFn<byte[], KV<String, Integer>> inputFn =
Expand Down Expand Up @@ -157,20 +157,13 @@ public void onTimer(
}
};

DoFn<KV<String, Integer>, Void> collectResults =
new DoFn<KV<String, Integer>, Void>() {
@ProcessElement
public void processElement(ProcessContext context) {
results.add(context.element());
}
};

final Pipeline pipeline = Pipeline.create(options);
pipeline
.apply(Impulse.create())
.apply(ParDo.of(inputFn))
.apply(ParDo.of(testFn))
.apply(ParDo.of(collectResults));
PCollection<KV<String, Integer>> output =
pipeline.apply(Impulse.create()).apply(ParDo.of(inputFn)).apply(ParDo.of(testFn));
PAssert.that(output).containsInAnyOrder(expectedOutput);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 to use PAssert to verify result of a Pipeline.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

// This is line below required to convert the PAssert's read to an impulse, which is expected
// by the GreedyPipelineFuser.
pipeline.replaceAll(Collections.singletonList(JavaReadViaImpulse.boundedOverride()));

RunnerApi.Pipeline pipelineProto = PipelineTranslation.toProto(pipeline);

Expand All @@ -185,11 +178,9 @@ public void processElement(ProcessContext context) {
Collections.emptyList());

jobInvocation.start();
long timeout = System.currentTimeMillis() + 60 * 1000;
while (jobInvocation.getState() != Enum.DONE && System.currentTimeMillis() < timeout) {
while (jobInvocation.getState() != Enum.DONE) {
Thread.sleep(1000);
}
assertThat(jobInvocation.getState(), is(Enum.DONE));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can also remove this line if we remove the timeout because the while loop will only return when it is done.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

assertThat(results, containsInAnyOrder(expectedOutput.toArray()));
}
}