Java Code Examples for org.apache.flink.streaming.util.TwoInputStreamOperatorTestHarness#initializeState()

The following examples show how to use org.apache.flink.streaming.util.TwoInputStreamOperatorTestHarness#initializeState() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CoBroadcastWithNonKeyedOperatorTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
private static <IN1, IN2, OUT> TwoInputStreamOperatorTestHarness<IN1, IN2, OUT> getInitializedTestHarness(
		final BroadcastProcessFunction<IN1, IN2, OUT> function,
		final int maxParallelism,
		final int numTasks,
		final int taskIdx,
		final OperatorSubtaskState initState,
		final MapStateDescriptor<?, ?>... descriptors) throws Exception {

	TwoInputStreamOperatorTestHarness<IN1, IN2, OUT> testHarness = new TwoInputStreamOperatorTestHarness<>(
			new CoBroadcastWithNonKeyedOperator<>(
					Preconditions.checkNotNull(function),
					Arrays.asList(descriptors)),
			maxParallelism, numTasks, taskIdx
	);
	testHarness.setup();
	testHarness.initializeState(initState);
	testHarness.open();

	return testHarness;
}
 
Example 2
Source File: CoBroadcastWithNonKeyedOperatorTest.java    From flink with Apache License 2.0 6 votes vote down vote up
private static <IN1, IN2, OUT> TwoInputStreamOperatorTestHarness<IN1, IN2, OUT> getInitializedTestHarness(
		final BroadcastProcessFunction<IN1, IN2, OUT> function,
		final int maxParallelism,
		final int numTasks,
		final int taskIdx,
		final OperatorSubtaskState initState,
		final MapStateDescriptor<?, ?>... descriptors) throws Exception {

	TwoInputStreamOperatorTestHarness<IN1, IN2, OUT> testHarness = new TwoInputStreamOperatorTestHarness<>(
			new CoBroadcastWithNonKeyedOperator<>(
					Preconditions.checkNotNull(function),
					Arrays.asList(descriptors)),
			maxParallelism, numTasks, taskIdx
	);
	testHarness.setup();
	testHarness.initializeState(initState);
	testHarness.open();

	return testHarness;
}
 
Example 3
Source File: CoBroadcastWithNonKeyedOperatorTest.java    From flink with Apache License 2.0 6 votes vote down vote up
private static <IN1, IN2, OUT> TwoInputStreamOperatorTestHarness<IN1, IN2, OUT> getInitializedTestHarness(
		final BroadcastProcessFunction<IN1, IN2, OUT> function,
		final int maxParallelism,
		final int numTasks,
		final int taskIdx,
		final OperatorSubtaskState initState,
		final MapStateDescriptor<?, ?>... descriptors) throws Exception {

	TwoInputStreamOperatorTestHarness<IN1, IN2, OUT> testHarness = new TwoInputStreamOperatorTestHarness<>(
			new CoBroadcastWithNonKeyedOperator<>(
					Preconditions.checkNotNull(function),
					Arrays.asList(descriptors)),
			maxParallelism, numTasks, taskIdx
	);
	testHarness.setup();
	testHarness.initializeState(initState);
	testHarness.open();

	return testHarness;
}
 
Example 4
Source File: CoBroadcastWithKeyedOperatorTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private static <KEY, IN1, IN2, OUT> TwoInputStreamOperatorTestHarness<IN1, IN2, OUT> getInitializedTestHarness(
		final TypeInformation<KEY> keyTypeInfo,
		final KeySelector<IN1, KEY> keyKeySelector,
		final KeyedBroadcastProcessFunction<KEY, IN1, IN2, OUT> function,
		final int maxParallelism,
		final int numTasks,
		final int taskIdx,
		final OperatorSubtaskState initState) throws Exception {

	final TwoInputStreamOperatorTestHarness<IN1, IN2, OUT>  testHarness =
			new KeyedTwoInputStreamOperatorTestHarness<>(
					new CoBroadcastWithKeyedOperator<>(
							Preconditions.checkNotNull(function),
							Collections.singletonList(STATE_DESCRIPTOR)),
					keyKeySelector,
					null,
					keyTypeInfo,
					maxParallelism,
					numTasks,
					taskIdx
			);

	testHarness.setup();
	testHarness.initializeState(initState);
	testHarness.open();

	return testHarness;
}
 
Example 5
Source File: CoBroadcastWithKeyedOperatorTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private static <KEY, IN1, IN2, OUT> TwoInputStreamOperatorTestHarness<IN1, IN2, OUT> getInitializedTestHarness(
		final TypeInformation<KEY> keyTypeInfo,
		final KeySelector<IN1, KEY> keyKeySelector,
		final KeyedBroadcastProcessFunction<KEY, IN1, IN2, OUT> function,
		final int maxParallelism,
		final int numTasks,
		final int taskIdx,
		final OperatorSubtaskState initState) throws Exception {

	final TwoInputStreamOperatorTestHarness<IN1, IN2, OUT>  testHarness =
			new KeyedTwoInputStreamOperatorTestHarness<>(
					new CoBroadcastWithKeyedOperator<>(
							Preconditions.checkNotNull(function),
							Collections.singletonList(STATE_DESCRIPTOR)),
					keyKeySelector,
					null,
					keyTypeInfo,
					maxParallelism,
					numTasks,
					taskIdx
			);

	testHarness.setup();
	testHarness.initializeState(initState);
	testHarness.open();

	return testHarness;
}
 
Example 6
Source File: CoBroadcastWithKeyedOperatorTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private static <KEY, IN1, IN2, OUT> TwoInputStreamOperatorTestHarness<IN1, IN2, OUT> getInitializedTestHarness(
		final TypeInformation<KEY> keyTypeInfo,
		final KeySelector<IN1, KEY> keyKeySelector,
		final KeyedBroadcastProcessFunction<KEY, IN1, IN2, OUT> function,
		final int maxParallelism,
		final int numTasks,
		final int taskIdx,
		final OperatorSubtaskState initState) throws Exception {

	final TwoInputStreamOperatorTestHarness<IN1, IN2, OUT>  testHarness =
			new KeyedTwoInputStreamOperatorTestHarness<>(
					new CoBroadcastWithKeyedOperator<>(
							Preconditions.checkNotNull(function),
							Collections.singletonList(STATE_DESCRIPTOR)),
					keyKeySelector,
					null,
					keyTypeInfo,
					maxParallelism,
					numTasks,
					taskIdx
			);

	testHarness.setup();
	testHarness.initializeState(initState);
	testHarness.open();

	return testHarness;
}
 
Example 7
Source File: KeyedCoProcessOperatorTest.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
@Test
public void testSnapshotAndRestore() throws Exception {

	KeyedCoProcessOperator<String, Integer, String, String> operator =
			new KeyedCoProcessOperator<>(new BothTriggeringProcessFunction());

	TwoInputStreamOperatorTestHarness<Integer, String, String> testHarness =
			new KeyedTwoInputStreamOperatorTestHarness<>(
					operator,
					new IntToStringKeySelector<>(),
					new IdentityKeySelector<String>(),
					BasicTypeInfo.STRING_TYPE_INFO);

	testHarness.setup();
	testHarness.open();

	testHarness.processElement1(new StreamRecord<>(5, 12L));
	testHarness.processElement2(new StreamRecord<>("5", 12L));

	// snapshot and restore from scratch
	OperatorSubtaskState snapshot = testHarness.snapshot(0, 0);

	testHarness.close();

	operator = new KeyedCoProcessOperator<>(new BothTriggeringProcessFunction());

	testHarness = new KeyedTwoInputStreamOperatorTestHarness<>(
			operator,
			new IntToStringKeySelector<>(),
			new IdentityKeySelector<String>(),
			BasicTypeInfo.STRING_TYPE_INFO);

	testHarness.setup();
	testHarness.initializeState(snapshot);
	testHarness.open();

	testHarness.setProcessingTime(5);
	testHarness.processWatermark1(new Watermark(6));
	testHarness.processWatermark2(new Watermark(6));

	ConcurrentLinkedQueue<Object> expectedOutput = new ConcurrentLinkedQueue<>();

	expectedOutput.add(new StreamRecord<>("PROC:1777"));
	expectedOutput.add(new StreamRecord<>("EVENT:1777", 6L));
	expectedOutput.add(new Watermark(6));

	TestHarnessUtil.assertOutputEquals("Output was not correct.", expectedOutput, testHarness.getOutput());

	testHarness.close();
}
 
Example 8
Source File: LegacyKeyedCoProcessOperatorTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test
public void testSnapshotAndRestore() throws Exception {

	LegacyKeyedCoProcessOperator<String, Integer, String, String> operator =
			new LegacyKeyedCoProcessOperator<>(new BothTriggeringProcessFunction());

	TwoInputStreamOperatorTestHarness<Integer, String, String> testHarness =
			new KeyedTwoInputStreamOperatorTestHarness<>(
					operator,
					new IntToStringKeySelector<>(),
					new IdentityKeySelector<String>(),
					BasicTypeInfo.STRING_TYPE_INFO);

	testHarness.setup();
	testHarness.open();

	testHarness.processElement1(new StreamRecord<>(5, 12L));
	testHarness.processElement2(new StreamRecord<>("5", 12L));

	// snapshot and restore from scratch
	OperatorSubtaskState snapshot = testHarness.snapshot(0, 0);

	testHarness.close();

	operator = new LegacyKeyedCoProcessOperator<>(new BothTriggeringProcessFunction());

	testHarness = new KeyedTwoInputStreamOperatorTestHarness<>(
			operator,
			new IntToStringKeySelector<>(),
			new IdentityKeySelector<String>(),
			BasicTypeInfo.STRING_TYPE_INFO);

	testHarness.setup();
	testHarness.initializeState(snapshot);
	testHarness.open();

	testHarness.setProcessingTime(5);
	testHarness.processWatermark1(new Watermark(6));
	testHarness.processWatermark2(new Watermark(6));

	ConcurrentLinkedQueue<Object> expectedOutput = new ConcurrentLinkedQueue<>();

	expectedOutput.add(new StreamRecord<>("PROC:1777"));
	expectedOutput.add(new StreamRecord<>("EVENT:1777", 6L));
	expectedOutput.add(new Watermark(6));

	TestHarnessUtil.assertOutputEquals("Output was not correct.", expectedOutput, testHarness.getOutput());

	testHarness.close();
}
 
Example 9
Source File: KeyedCoProcessOperatorTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test
public void testSnapshotAndRestore() throws Exception {

	KeyedCoProcessOperator<String, Integer, String, String> operator =
			new KeyedCoProcessOperator<>(new BothTriggeringProcessFunction());

	TwoInputStreamOperatorTestHarness<Integer, String, String> testHarness =
			new KeyedTwoInputStreamOperatorTestHarness<>(
					operator,
					new IntToStringKeySelector<>(),
					new IdentityKeySelector<String>(),
					BasicTypeInfo.STRING_TYPE_INFO);

	testHarness.setup();
	testHarness.open();

	testHarness.processElement1(new StreamRecord<>(5, 12L));
	testHarness.processElement2(new StreamRecord<>("5", 12L));

	// snapshot and restore from scratch
	OperatorSubtaskState snapshot = testHarness.snapshot(0, 0);

	testHarness.close();

	operator = new KeyedCoProcessOperator<>(new BothTriggeringProcessFunction());

	testHarness = new KeyedTwoInputStreamOperatorTestHarness<>(
			operator,
			new IntToStringKeySelector<>(),
			new IdentityKeySelector<String>(),
			BasicTypeInfo.STRING_TYPE_INFO);

	testHarness.setup();
	testHarness.initializeState(snapshot);
	testHarness.open();

	testHarness.setProcessingTime(5);
	testHarness.processWatermark1(new Watermark(6));
	testHarness.processWatermark2(new Watermark(6));

	ConcurrentLinkedQueue<Object> expectedOutput = new ConcurrentLinkedQueue<>();

	expectedOutput.add(new StreamRecord<>("PROC:1777"));
	expectedOutput.add(new StreamRecord<>("EVENT:1777", 6L));
	expectedOutput.add(new Watermark(6));

	TestHarnessUtil.assertOutputEquals("Output was not correct.", expectedOutput, testHarness.getOutput());

	testHarness.close();
}
 
Example 10
Source File: DoFnOperatorTest.java    From beam with Apache License 2.0 4 votes vote down vote up
void sideInputCheckpointing(
    TestHarnessFactory<
            TwoInputStreamOperatorTestHarness<
                WindowedValue<String>, RawUnionValue, WindowedValue<String>>>
        harnessFactory)
    throws Exception {

  TwoInputStreamOperatorTestHarness<WindowedValue<String>, RawUnionValue, WindowedValue<String>>
      testHarness = harnessFactory.create();

  testHarness.open();

  IntervalWindow firstWindow = new IntervalWindow(new Instant(0), new Instant(100));
  IntervalWindow secondWindow = new IntervalWindow(new Instant(0), new Instant(500));

  // push in some side inputs for both windows
  testHarness.processElement2(
      new StreamRecord<>(
          new RawUnionValue(
              1,
              valuesInWindow(
                  PCollectionViewTesting.materializeValuesFor(
                      view1.getPipeline().getOptions(), View.asIterable(), "hello", "ciao"),
                  new Instant(0),
                  firstWindow))));
  testHarness.processElement2(
      new StreamRecord<>(
          new RawUnionValue(
              2,
              valuesInWindow(
                  PCollectionViewTesting.materializeValuesFor(
                      view2.getPipeline().getOptions(), View.asIterable(), "foo", "bar"),
                  new Instant(0),
                  secondWindow))));

  // snapshot state, throw away the operator, then restore and verify that we still match
  // main-input elements to the side-inputs that we sent earlier
  OperatorSubtaskState snapshot = testHarness.snapshot(0, 0);

  testHarness = harnessFactory.create();

  testHarness.initializeState(snapshot);
  testHarness.open();

  // push in main-input elements
  WindowedValue<String> helloElement = valueInWindow("Hello", new Instant(0), firstWindow);
  WindowedValue<String> worldElement = valueInWindow("World", new Instant(1000), firstWindow);
  testHarness.processElement1(new StreamRecord<>(helloElement));
  testHarness.processElement1(new StreamRecord<>(worldElement));

  assertThat(
      stripStreamRecordFromWindowedValue(testHarness.getOutput()),
      contains(helloElement, worldElement));

  testHarness.close();
}
 
Example 11
Source File: DoFnOperatorTest.java    From beam with Apache License 2.0 4 votes vote down vote up
void pushbackDataCheckpointing(
    TestHarnessFactory<
            TwoInputStreamOperatorTestHarness<
                WindowedValue<String>, RawUnionValue, WindowedValue<String>>>
        harnessFactory)
    throws Exception {

  TwoInputStreamOperatorTestHarness<WindowedValue<String>, RawUnionValue, WindowedValue<String>>
      testHarness = harnessFactory.create();

  testHarness.open();

  IntervalWindow firstWindow = new IntervalWindow(new Instant(0), new Instant(100));
  IntervalWindow secondWindow = new IntervalWindow(new Instant(0), new Instant(500));

  // push in main-input elements
  WindowedValue<String> helloElement = valueInWindow("Hello", new Instant(0), firstWindow);
  WindowedValue<String> worldElement = valueInWindow("World", new Instant(1000), firstWindow);
  testHarness.processElement1(new StreamRecord<>(helloElement));
  testHarness.processElement1(new StreamRecord<>(worldElement));

  // snapshot state, throw away the operator, then restore and verify that we still match
  // main-input elements to the side-inputs that we sent earlier
  OperatorSubtaskState snapshot = testHarness.snapshot(0, 0);

  testHarness = harnessFactory.create();

  testHarness.initializeState(snapshot);
  testHarness.open();

  // push in some side inputs for both windows
  testHarness.processElement2(
      new StreamRecord<>(
          new RawUnionValue(
              1,
              valuesInWindow(
                  PCollectionViewTesting.materializeValuesFor(
                      view1.getPipeline().getOptions(), View.asIterable(), "hello", "ciao"),
                  new Instant(0),
                  firstWindow))));
  testHarness.processElement2(
      new StreamRecord<>(
          new RawUnionValue(
              2,
              valuesInWindow(
                  PCollectionViewTesting.materializeValuesFor(
                      view2.getPipeline().getOptions(), View.asIterable(), "foo", "bar"),
                  new Instant(0),
                  secondWindow))));

  assertThat(
      stripStreamRecordFromWindowedValue(testHarness.getOutput()),
      containsInAnyOrder(helloElement, worldElement));

  testHarness.close();
}
 
Example 12
Source File: LegacyKeyedCoProcessOperatorTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test
public void testSnapshotAndRestore() throws Exception {

	LegacyKeyedCoProcessOperator<String, Integer, String, String> operator =
			new LegacyKeyedCoProcessOperator<>(new BothTriggeringProcessFunction());

	TwoInputStreamOperatorTestHarness<Integer, String, String> testHarness =
			new KeyedTwoInputStreamOperatorTestHarness<>(
					operator,
					new IntToStringKeySelector<>(),
					new IdentityKeySelector<String>(),
					BasicTypeInfo.STRING_TYPE_INFO);

	testHarness.setup();
	testHarness.open();

	testHarness.processElement1(new StreamRecord<>(5, 12L));
	testHarness.processElement2(new StreamRecord<>("5", 12L));

	// snapshot and restore from scratch
	OperatorSubtaskState snapshot = testHarness.snapshot(0, 0);

	testHarness.close();

	operator = new LegacyKeyedCoProcessOperator<>(new BothTriggeringProcessFunction());

	testHarness = new KeyedTwoInputStreamOperatorTestHarness<>(
			operator,
			new IntToStringKeySelector<>(),
			new IdentityKeySelector<String>(),
			BasicTypeInfo.STRING_TYPE_INFO);

	testHarness.setup();
	testHarness.initializeState(snapshot);
	testHarness.open();

	testHarness.setProcessingTime(5);
	testHarness.processWatermark1(new Watermark(6));
	testHarness.processWatermark2(new Watermark(6));

	ConcurrentLinkedQueue<Object> expectedOutput = new ConcurrentLinkedQueue<>();

	expectedOutput.add(new StreamRecord<>("PROC:1777"));
	expectedOutput.add(new StreamRecord<>("EVENT:1777", 6L));
	expectedOutput.add(new Watermark(6));

	TestHarnessUtil.assertOutputEquals("Output was not correct.", expectedOutput, testHarness.getOutput());

	testHarness.close();
}
 
Example 13
Source File: KeyedCoProcessOperatorTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test
public void testSnapshotAndRestore() throws Exception {

	KeyedCoProcessOperator<String, Integer, String, String> operator =
			new KeyedCoProcessOperator<>(new BothTriggeringProcessFunction());

	TwoInputStreamOperatorTestHarness<Integer, String, String> testHarness =
			new KeyedTwoInputStreamOperatorTestHarness<>(
					operator,
					new IntToStringKeySelector<>(),
					new IdentityKeySelector<String>(),
					BasicTypeInfo.STRING_TYPE_INFO);

	testHarness.setup();
	testHarness.open();

	testHarness.processElement1(new StreamRecord<>(5, 12L));
	testHarness.processElement2(new StreamRecord<>("5", 12L));

	// snapshot and restore from scratch
	OperatorSubtaskState snapshot = testHarness.snapshot(0, 0);

	testHarness.close();

	operator = new KeyedCoProcessOperator<>(new BothTriggeringProcessFunction());

	testHarness = new KeyedTwoInputStreamOperatorTestHarness<>(
			operator,
			new IntToStringKeySelector<>(),
			new IdentityKeySelector<String>(),
			BasicTypeInfo.STRING_TYPE_INFO);

	testHarness.setup();
	testHarness.initializeState(snapshot);
	testHarness.open();

	testHarness.setProcessingTime(5);
	testHarness.processWatermark1(new Watermark(6));
	testHarness.processWatermark2(new Watermark(6));

	ConcurrentLinkedQueue<Object> expectedOutput = new ConcurrentLinkedQueue<>();

	expectedOutput.add(new StreamRecord<>("PROC:1777"));
	expectedOutput.add(new StreamRecord<>("EVENT:1777", 6L));
	expectedOutput.add(new Watermark(6));

	TestHarnessUtil.assertOutputEquals("Output was not correct.", expectedOutput, testHarness.getOutput());

	testHarness.close();
}