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

The following examples show how to use org.apache.flink.streaming.util.TwoInputStreamOperatorTestHarness#open() . 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 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-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 3
Source File: KeyedCoProcessOperatorTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testTimestampAndWatermarkQuerying() throws Exception {

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

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

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

	testHarness.processWatermark1(new Watermark(17));
	testHarness.processWatermark2(new Watermark(17));
	testHarness.processElement1(new StreamRecord<>(5, 12L));

	testHarness.processWatermark1(new Watermark(42));
	testHarness.processWatermark2(new Watermark(42));
	testHarness.processElement2(new StreamRecord<>("6", 13L));

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

	expectedOutput.add(new Watermark(17L));
	expectedOutput.add(new StreamRecord<>("5WM:17 TS:12", 12L));
	expectedOutput.add(new Watermark(42L));
	expectedOutput.add(new StreamRecord<>("6WM:42 TS:13", 13L));

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

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

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

	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<>(17));
	testHarness.processElement2(new StreamRecord<>("18"));

	testHarness.setProcessingTime(5);
	testHarness.setProcessingTime(6);

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

	expectedOutput.add(new StreamRecord<>("INPUT1:17"));
	expectedOutput.add(new StreamRecord<>("INPUT2:18"));
	expectedOutput.add(new StreamRecord<>("1777"));
	expectedOutput.add(new StreamRecord<>("1777"));

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

	testHarness.close();
}
 
Example 5
Source File: CoStreamMapTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testCoMap() throws Exception {
	CoStreamMap<Double, Integer, String> operator = new CoStreamMap<Double, Integer, String>(new MyCoMap());

	TwoInputStreamOperatorTestHarness<Double, Integer, String> testHarness = new TwoInputStreamOperatorTestHarness<Double, Integer, String>(operator);

	long initialTime = 0L;
	ConcurrentLinkedQueue<Object> expectedOutput = new ConcurrentLinkedQueue<Object>();

	testHarness.open();

	testHarness.processElement1(new StreamRecord<Double>(1.1d, initialTime + 1));
	testHarness.processElement1(new StreamRecord<Double>(1.2d, initialTime + 2));
	testHarness.processElement1(new StreamRecord<Double>(1.3d, initialTime + 3));
	testHarness.processWatermark1(new Watermark(initialTime + 3));
	testHarness.processElement1(new StreamRecord<Double>(1.4d, initialTime + 4));
	testHarness.processElement1(new StreamRecord<Double>(1.5d, initialTime + 5));

	testHarness.processElement2(new StreamRecord<Integer>(1, initialTime + 1));
	testHarness.processElement2(new StreamRecord<Integer>(2, initialTime + 2));
	testHarness.processWatermark2(new Watermark(initialTime + 2));
	testHarness.processElement2(new StreamRecord<Integer>(3, initialTime + 3));
	testHarness.processElement2(new StreamRecord<Integer>(4, initialTime + 4));
	testHarness.processElement2(new StreamRecord<Integer>(5, initialTime + 5));

	expectedOutput.add(new StreamRecord<String>("1.1", initialTime + 1));
	expectedOutput.add(new StreamRecord<String>("1.2", initialTime + 2));
	expectedOutput.add(new StreamRecord<String>("1.3", initialTime + 3));
	expectedOutput.add(new StreamRecord<String>("1.4", initialTime + 4));
	expectedOutput.add(new StreamRecord<String>("1.5", initialTime + 5));

	expectedOutput.add(new StreamRecord<String>("1", initialTime + 1));
	expectedOutput.add(new StreamRecord<String>("2", initialTime + 2));
	expectedOutput.add(new Watermark(initialTime + 2));
	expectedOutput.add(new StreamRecord<String>("3", initialTime + 3));
	expectedOutput.add(new StreamRecord<String>("4", initialTime + 4));
	expectedOutput.add(new StreamRecord<String>("5", initialTime + 5));

	TestHarnessUtil.assertOutputEquals("Output was not correct.", expectedOutput, testHarness.getOutput());
}
 
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: CoStreamMapTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void testCoMap() throws Exception {
	CoStreamMap<Double, Integer, String> operator = new CoStreamMap<Double, Integer, String>(new MyCoMap());

	TwoInputStreamOperatorTestHarness<Double, Integer, String> testHarness = new TwoInputStreamOperatorTestHarness<Double, Integer, String>(operator);

	long initialTime = 0L;
	ConcurrentLinkedQueue<Object> expectedOutput = new ConcurrentLinkedQueue<Object>();

	testHarness.open();

	testHarness.processElement1(new StreamRecord<Double>(1.1d, initialTime + 1));
	testHarness.processElement1(new StreamRecord<Double>(1.2d, initialTime + 2));
	testHarness.processElement1(new StreamRecord<Double>(1.3d, initialTime + 3));
	testHarness.processWatermark1(new Watermark(initialTime + 3));
	testHarness.processElement1(new StreamRecord<Double>(1.4d, initialTime + 4));
	testHarness.processElement1(new StreamRecord<Double>(1.5d, initialTime + 5));

	testHarness.processElement2(new StreamRecord<Integer>(1, initialTime + 1));
	testHarness.processElement2(new StreamRecord<Integer>(2, initialTime + 2));
	testHarness.processWatermark2(new Watermark(initialTime + 2));
	testHarness.processElement2(new StreamRecord<Integer>(3, initialTime + 3));
	testHarness.processElement2(new StreamRecord<Integer>(4, initialTime + 4));
	testHarness.processElement2(new StreamRecord<Integer>(5, initialTime + 5));

	expectedOutput.add(new StreamRecord<String>("1.1", initialTime + 1));
	expectedOutput.add(new StreamRecord<String>("1.2", initialTime + 2));
	expectedOutput.add(new StreamRecord<String>("1.3", initialTime + 3));
	expectedOutput.add(new StreamRecord<String>("1.4", initialTime + 4));
	expectedOutput.add(new StreamRecord<String>("1.5", initialTime + 5));

	expectedOutput.add(new StreamRecord<String>("1", initialTime + 1));
	expectedOutput.add(new StreamRecord<String>("2", initialTime + 2));
	expectedOutput.add(new Watermark(initialTime + 2));
	expectedOutput.add(new StreamRecord<String>("3", initialTime + 3));
	expectedOutput.add(new StreamRecord<String>("4", initialTime + 4));
	expectedOutput.add(new StreamRecord<String>("5", initialTime + 5));

	TestHarnessUtil.assertOutputEquals("Output was not correct.", expectedOutput, testHarness.getOutput());
}
 
Example 8
Source File: KeyedCoProcessOperatorTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testEventTimeTimers() throws Exception {

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

	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<>(17, 42L));
	testHarness.processElement2(new StreamRecord<>("18", 42L));

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

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

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

	expectedOutput.add(new StreamRecord<>("INPUT1:17", 42L));
	expectedOutput.add(new StreamRecord<>("INPUT2:18", 42L));
	expectedOutput.add(new StreamRecord<>("17:1777", 5L));
	expectedOutput.add(new Watermark(5L));
	expectedOutput.add(new StreamRecord<>("18:1777", 6L));
	expectedOutput.add(new Watermark(6L));

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

	testHarness.close();
}
 
Example 9
Source File: KeyedCoProcessOperatorTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void testEventTimeTimers() throws Exception {

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

	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<>(17, 42L));
	testHarness.processElement2(new StreamRecord<>("18", 42L));

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

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

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

	expectedOutput.add(new StreamRecord<>("INPUT1:17", 42L));
	expectedOutput.add(new StreamRecord<>("INPUT2:18", 42L));
	expectedOutput.add(new StreamRecord<>("1777", 5L));
	expectedOutput.add(new Watermark(5L));
	expectedOutput.add(new StreamRecord<>("1777", 6L));
	expectedOutput.add(new Watermark(6L));

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

	testHarness.close();
}
 
Example 10
Source File: KeyedCoProcessOperatorTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void testTimestampAndProcessingTimeQuerying() throws Exception {

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

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

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

	testHarness.setProcessingTime(17);
	testHarness.processElement1(new StreamRecord<>(5));

	testHarness.setProcessingTime(42);
	testHarness.processElement2(new StreamRecord<>("6"));

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

	expectedOutput.add(new StreamRecord<>("5PT:17 TS:null"));
	expectedOutput.add(new StreamRecord<>("6PT:42 TS:null"));

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

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

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

	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<>(17, 42L));
	testHarness.processElement2(new StreamRecord<>("18", 42L));

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

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

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

	expectedOutput.add(new StreamRecord<>("INPUT1:17", 42L));
	expectedOutput.add(new StreamRecord<>("INPUT2:18", 42L));
	expectedOutput.add(new StreamRecord<>("1777", 5L));
	expectedOutput.add(new Watermark(5L));
	expectedOutput.add(new StreamRecord<>("1777", 6L));
	expectedOutput.add(new Watermark(6L));

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

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

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

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

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

	testHarness.processWatermark1(new Watermark(17));
	testHarness.processWatermark2(new Watermark(17));
	testHarness.processElement1(new StreamRecord<>(5, 12L));

	testHarness.processWatermark1(new Watermark(42));
	testHarness.processWatermark2(new Watermark(42));
	testHarness.processElement2(new StreamRecord<>("6", 13L));

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

	expectedOutput.add(new Watermark(17L));
	expectedOutput.add(new StreamRecord<>("5WM:17 TS:12", 12L));
	expectedOutput.add(new Watermark(42L));
	expectedOutput.add(new StreamRecord<>("6WM:42 TS:13", 13L));

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

	testHarness.close();
}
 
Example 13
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 14
Source File: CoStreamFlatMapTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test
public void testOpenClose() throws Exception {
	CoStreamFlatMap<String, Integer, String> operator = new CoStreamFlatMap<String, Integer, String>(new TestOpenCloseCoFlatMapFunction());

	TwoInputStreamOperatorTestHarness<String, Integer, String> testHarness = new TwoInputStreamOperatorTestHarness<String, Integer, String>(operator);

	long initialTime = 0L;

	testHarness.open();

	testHarness.processElement1(new StreamRecord<String>("Hello", initialTime));
	testHarness.processElement2(new StreamRecord<Integer>(42, initialTime));

	testHarness.close();

	Assert.assertTrue("RichFunction methods where not called.", TestOpenCloseCoFlatMapFunction.closeCalled);
	Assert.assertTrue("Output contains no elements.", testHarness.getOutput().size() > 0);
}
 
Example 15
Source File: LegacyKeyedCoProcessOperatorTest.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Verifies that we don't have leakage between different keys.
 */
@Test
public void testEventTimeTimerWithState() throws Exception {

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

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

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

	testHarness.processWatermark1(new Watermark(1));
	testHarness.processWatermark2(new Watermark(1));
	testHarness.processElement1(new StreamRecord<>(17, 0L)); // should set timer for 6
	testHarness.processElement1(new StreamRecord<>(13, 0L)); // should set timer for 6

	testHarness.processWatermark1(new Watermark(2));
	testHarness.processWatermark2(new Watermark(2));
	testHarness.processElement1(new StreamRecord<>(13, 1L)); // should delete timer
	testHarness.processElement2(new StreamRecord<>("42", 1L)); // should set timer for 7

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

	testHarness.processWatermark1(new Watermark(7));
	testHarness.processWatermark2(new Watermark(7));

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

	expectedOutput.add(new Watermark(1L));
	expectedOutput.add(new StreamRecord<>("INPUT1:17", 0L));
	expectedOutput.add(new StreamRecord<>("INPUT1:13", 0L));
	expectedOutput.add(new Watermark(2L));
	expectedOutput.add(new StreamRecord<>("INPUT2:42", 1L));
	expectedOutput.add(new StreamRecord<>("STATE:17", 6L));
	expectedOutput.add(new Watermark(6L));
	expectedOutput.add(new StreamRecord<>("STATE:42", 7L));
	expectedOutput.add(new Watermark(7L));

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

	testHarness.close();
}
 
Example 16
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 17
Source File: CoStreamFlatMapTest.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
@Test
public void testCoFlatMap() throws Exception {
	CoStreamFlatMap<String, Integer, String> operator = new CoStreamFlatMap<String, Integer, String>(new MyCoFlatMap());

	TwoInputStreamOperatorTestHarness<String, Integer, String> testHarness = new TwoInputStreamOperatorTestHarness<String, Integer, String>(operator);

	long initialTime = 0L;
	ConcurrentLinkedQueue<Object> expectedOutput = new ConcurrentLinkedQueue<Object>();

	testHarness.open();

	testHarness.processElement1(new StreamRecord<String>("abc", initialTime + 1));
	testHarness.processElement1(new StreamRecord<String>("def", initialTime + 2));
	testHarness.processWatermark1(new Watermark(initialTime + 2));
	testHarness.processElement1(new StreamRecord<String>("ghi", initialTime + 3));

	testHarness.processElement2(new StreamRecord<Integer>(1, initialTime + 1));
	testHarness.processElement2(new StreamRecord<Integer>(2, initialTime + 2));
	testHarness.processWatermark2(new Watermark(initialTime + 3));
	testHarness.processElement2(new StreamRecord<Integer>(3, initialTime + 3));
	testHarness.processElement2(new StreamRecord<Integer>(4, initialTime + 4));
	testHarness.processElement2(new StreamRecord<Integer>(5, initialTime + 5));

	expectedOutput.add(new StreamRecord<String>("a", initialTime + 1));
	expectedOutput.add(new StreamRecord<String>("b", initialTime + 1));
	expectedOutput.add(new StreamRecord<String>("c", initialTime + 1));
	expectedOutput.add(new StreamRecord<String>("d", initialTime + 2));
	expectedOutput.add(new StreamRecord<String>("e", initialTime + 2));
	expectedOutput.add(new StreamRecord<String>("f", initialTime + 2));
	expectedOutput.add(new StreamRecord<String>("g", initialTime + 3));
	expectedOutput.add(new StreamRecord<String>("h", initialTime + 3));
	expectedOutput.add(new StreamRecord<String>("i", initialTime + 3));

	expectedOutput.add(new StreamRecord<String>("1", initialTime + 1));
	expectedOutput.add(new StreamRecord<String>("2", initialTime + 2));
	expectedOutput.add(new Watermark(initialTime + 2));
	expectedOutput.add(new StreamRecord<String>("3", initialTime + 3));
	expectedOutput.add(new StreamRecord<String>("4", initialTime + 4));
	expectedOutput.add(new StreamRecord<String>("5", initialTime + 5));

	TestHarnessUtil.assertOutputEquals("Output was not correct.", expectedOutput, testHarness.getOutput());
}
 
Example 18
Source File: CoStreamFlatMapTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test
public void testCoFlatMap() throws Exception {
	CoStreamFlatMap<String, Integer, String> operator = new CoStreamFlatMap<String, Integer, String>(new MyCoFlatMap());

	TwoInputStreamOperatorTestHarness<String, Integer, String> testHarness = new TwoInputStreamOperatorTestHarness<String, Integer, String>(operator);

	long initialTime = 0L;
	ConcurrentLinkedQueue<Object> expectedOutput = new ConcurrentLinkedQueue<Object>();

	testHarness.open();

	testHarness.processElement1(new StreamRecord<String>("abc", initialTime + 1));
	testHarness.processElement1(new StreamRecord<String>("def", initialTime + 2));
	testHarness.processWatermark1(new Watermark(initialTime + 2));
	testHarness.processElement1(new StreamRecord<String>("ghi", initialTime + 3));

	testHarness.processElement2(new StreamRecord<Integer>(1, initialTime + 1));
	testHarness.processElement2(new StreamRecord<Integer>(2, initialTime + 2));
	testHarness.processWatermark2(new Watermark(initialTime + 3));
	testHarness.processElement2(new StreamRecord<Integer>(3, initialTime + 3));
	testHarness.processElement2(new StreamRecord<Integer>(4, initialTime + 4));
	testHarness.processElement2(new StreamRecord<Integer>(5, initialTime + 5));

	expectedOutput.add(new StreamRecord<String>("a", initialTime + 1));
	expectedOutput.add(new StreamRecord<String>("b", initialTime + 1));
	expectedOutput.add(new StreamRecord<String>("c", initialTime + 1));
	expectedOutput.add(new StreamRecord<String>("d", initialTime + 2));
	expectedOutput.add(new StreamRecord<String>("e", initialTime + 2));
	expectedOutput.add(new StreamRecord<String>("f", initialTime + 2));
	expectedOutput.add(new StreamRecord<String>("g", initialTime + 3));
	expectedOutput.add(new StreamRecord<String>("h", initialTime + 3));
	expectedOutput.add(new StreamRecord<String>("i", initialTime + 3));

	expectedOutput.add(new StreamRecord<String>("1", initialTime + 1));
	expectedOutput.add(new StreamRecord<String>("2", initialTime + 2));
	expectedOutput.add(new Watermark(initialTime + 2));
	expectedOutput.add(new StreamRecord<String>("3", initialTime + 3));
	expectedOutput.add(new StreamRecord<String>("4", initialTime + 4));
	expectedOutput.add(new StreamRecord<String>("5", initialTime + 5));

	TestHarnessUtil.assertOutputEquals("Output was not correct.", expectedOutput, testHarness.getOutput());
}
 
Example 19
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 20
Source File: CoProcessOperatorTest.java    From flink with Apache License 2.0 3 votes vote down vote up
@Test
public void testTimestampAndProcessingTimeQuerying() throws Exception {

	CoProcessOperator<Integer, String, String> operator =
			new CoProcessOperator<>(new ProcessingTimeQueryingProcessFunction());

	TwoInputStreamOperatorTestHarness<Integer, String, String> testHarness =
			new TwoInputStreamOperatorTestHarness<>(operator);

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

	testHarness.setProcessingTime(17);
	testHarness.processElement1(new StreamRecord<>(5));

	testHarness.setProcessingTime(42);
	testHarness.processElement2(new StreamRecord<>("6"));

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

	expectedOutput.add(new StreamRecord<>("5PT:17 TS:null"));
	expectedOutput.add(new StreamRecord<>("6PT:42 TS:null"));

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

	testHarness.close();
}