Java Code Examples for org.apache.flink.streaming.util.KeyedOneInputStreamOperatorTestHarness#processElement()

The following examples show how to use org.apache.flink.streaming.util.KeyedOneInputStreamOperatorTestHarness#processElement() . 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: AbstractStreamOperatorTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Test
public void testStateDoesNotInterfere() throws Exception {
	TestOperator testOperator = new TestOperator();

	KeyedOneInputStreamOperatorTestHarness<Integer, Tuple2<Integer, String>, String> testHarness =
			new KeyedOneInputStreamOperatorTestHarness<>(testOperator, new TestKeySelector(), BasicTypeInfo.INT_TYPE_INFO);

	testHarness.open();

	testHarness.processElement(new Tuple2<>(0, "SET_STATE:HELLO"), 0);
	testHarness.processElement(new Tuple2<>(1, "SET_STATE:CIAO"), 0);

	testHarness.processElement(new Tuple2<>(1, "EMIT_STATE"), 0);
	testHarness.processElement(new Tuple2<>(0, "EMIT_STATE"), 0);

	assertThat(
			extractResult(testHarness),
			contains("ON_ELEMENT:1:CIAO", "ON_ELEMENT:0:HELLO"));
}
 
Example 2
Source File: RocksIncrementalCheckpointRescalingTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private void validHarnessResult(
	KeyedOneInputStreamOperatorTestHarness<?, String, ?> harness,
	Integer expectedValue,
	String... records) throws Exception {
	for (String record : records) {
		harness.processElement(new StreamRecord<>(record, 1));
		StreamRecord<Integer> outputRecord = (StreamRecord<Integer>) harness.getOutput().poll();
		Assert.assertNotNull(outputRecord);
		Assert.assertEquals(expectedValue, outputRecord.getValue());
	}
}
 
Example 3
Source File: AllWindowTranslationTest.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Ensure that we get some output from the given operator when pushing in an element and
 * setting watermark and processing time to {@code Long.MAX_VALUE}.
 */
private static <K, IN, OUT> void processElementAndEnsureOutput(
		OneInputStreamOperator<IN, OUT> operator,
		KeySelector<IN, K> keySelector,
		TypeInformation<K> keyType,
		IN element) throws Exception {

	KeyedOneInputStreamOperatorTestHarness<K, IN, OUT> testHarness =
			new KeyedOneInputStreamOperatorTestHarness<>(
					operator,
					keySelector,
					keyType);

	testHarness.open();

	testHarness.setProcessingTime(0);
	testHarness.processWatermark(Long.MIN_VALUE);

	testHarness.processElement(new StreamRecord<>(element, 0));

	// provoke any processing-time/event-time triggers
	testHarness.setProcessingTime(Long.MAX_VALUE);
	testHarness.processWatermark(Long.MAX_VALUE);

	// we at least get the two watermarks and should also see an output element
	assertTrue(testHarness.getOutput().size() >= 3);

	testHarness.close();
}
 
Example 4
Source File: WindowOperatorContractTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private void testNoGarbageCollectionTimerForGlobalWindow(TimeDomainAdaptor timeAdaptor) throws Exception {

		WindowAssigner<Integer, GlobalWindow> mockAssigner = mockGlobalWindowAssigner();
		timeAdaptor.setIsEventTime(mockAssigner);
		Trigger<Integer, GlobalWindow> mockTrigger = mockTrigger();
		InternalWindowFunction<Iterable<Integer>, Void, Integer, GlobalWindow> mockWindowFunction = mockWindowFunction();

		// this needs to be true for the test to succeed
		assertEquals(Long.MAX_VALUE, GlobalWindow.get().maxTimestamp());

		KeyedOneInputStreamOperatorTestHarness<Integer, Integer, Void> testHarness =
				createWindowOperator(mockAssigner, mockTrigger, 0L, mockWindowFunction);

		testHarness.open();

		assertEquals(0, testHarness.getOutput().size());
		assertEquals(0, testHarness.numKeyedStateEntries());

		testHarness.processElement(new StreamRecord<>(0, 0L));

		// just the window contents
		assertEquals(1, testHarness.numKeyedStateEntries());

		// verify we have no timers for either time domain
		assertEquals(0, testHarness.numEventTimeTimers());
		assertEquals(0, testHarness.numProcessingTimeTimers());
	}
 
Example 5
Source File: RocksIncrementalCheckpointRescalingTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private void validHarnessResult(
	KeyedOneInputStreamOperatorTestHarness<?, String, ?> harness,
	Integer expectedValue,
	String... records) throws Exception {
	for (String record : records) {
		harness.processElement(new StreamRecord<>(record, 1));
		StreamRecord<Integer> outputRecord = (StreamRecord<Integer>) harness.getOutput().poll();
		Assert.assertNotNull(outputRecord);
		Assert.assertEquals(expectedValue, outputRecord.getValue());
	}
}
 
Example 6
Source File: WindowOperatorContractTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private void testMergingWindowSetClearedAtGarbageCollection(TimeDomainAdaptor timeAdaptor) throws Exception {
	WindowAssigner<Integer, TimeWindow> mockAssigner = mockMergingAssigner();
	timeAdaptor.setIsEventTime(mockAssigner);
	Trigger<Integer, TimeWindow> mockTrigger = mockTrigger();
	InternalWindowFunction<Iterable<Integer>, Void, Integer, TimeWindow> mockWindowFunction = mockWindowFunction();

	KeyedOneInputStreamOperatorTestHarness<Integer, Integer, Void> testHarness =
			createWindowOperator(mockAssigner, mockTrigger, 20L, mockWindowFunction);

	testHarness.open();

	when(mockAssigner.assignWindows(anyInt(), anyLong(), anyAssignerContext()))
			.thenReturn(Arrays.asList(new TimeWindow(0, 20)));

	assertEquals(0, testHarness.getOutput().size());
	assertEquals(0, testHarness.numKeyedStateEntries());

	testHarness.processElement(new StreamRecord<>(0, 0L));

	assertEquals(2, testHarness.numKeyedStateEntries()); // window contents plus merging window set
	assertEquals(1, timeAdaptor.numTimers(testHarness)); // gc timers

	timeAdaptor.advanceTime(testHarness, 19 + 20); // 19 is maxTime of the window

	assertEquals(0, testHarness.numKeyedStateEntries());
	assertEquals(0, timeAdaptor.numTimers(testHarness));
}
 
Example 7
Source File: WindowOperatorContractTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testProcessingElementsWithinAllowedLateness() throws Exception {
	WindowAssigner<Integer, TimeWindow> mockAssigner = mockTimeWindowAssigner();
	Trigger<Integer, TimeWindow> mockTrigger = mockTrigger();
	InternalWindowFunction<Iterable<Integer>, Void, Integer, TimeWindow> mockWindowFunction = mockWindowFunction();

	KeyedOneInputStreamOperatorTestHarness<Integer, Integer, Void> testHarness =
			createWindowOperator(mockAssigner, mockTrigger, 20L, mockWindowFunction);

	testHarness.open();

	when(mockAssigner.assignWindows(anyInt(), anyLong(), anyAssignerContext()))
			.thenReturn(Arrays.asList(new TimeWindow(0, 2)));

	assertEquals(0, testHarness.getOutput().size());
	assertEquals(0, testHarness.numKeyedStateEntries());

	shouldFireOnElement(mockTrigger);

	// 20 is just at the limit, window.maxTime() is 1 and allowed lateness is 20
	testHarness.processWatermark(new Watermark(20));

	testHarness.processElement(new StreamRecord<>(0, 0L));

	verify(mockWindowFunction, times(1)).process(eq(0), eq(new TimeWindow(0, 2)), anyInternalWindowContext(), intIterable(0), WindowOperatorContractTest.<Void>anyCollector());

	// clear is only called at cleanup time/GC time
	verify(mockTrigger, never()).clear(anyTimeWindow(), anyTriggerContext());

	// FIRE should not purge contents
	assertEquals(1, testHarness.numKeyedStateEntries()); // window contents plus trigger state
	assertEquals(1, testHarness.numEventTimeTimers()); // just the GC timer
}
 
Example 8
Source File: WindowOperatorContractTest.java    From flink with Apache License 2.0 4 votes vote down vote up
private void testEmittingFromWindowFunction(TimeDomainAdaptor timeAdaptor) throws Exception {

		WindowAssigner<Integer, TimeWindow> mockAssigner = mockTimeWindowAssigner();
		Trigger<Integer, TimeWindow> mockTrigger = mockTrigger();
		InternalWindowFunction<Iterable<Integer>, String, Integer, TimeWindow> mockWindowFunction = mockWindowFunction();

		KeyedOneInputStreamOperatorTestHarness<Integer, Integer, String> testHarness =
				createWindowOperator(mockAssigner, mockTrigger, 0L, mockWindowFunction);

		testHarness.open();

		when(mockAssigner.assignWindows(anyInt(), anyLong(), anyAssignerContext()))
				.thenReturn(Collections.singletonList(new TimeWindow(0, 2)));

		doAnswer(new Answer<Void>() {
			@Override
			public Void answer(InvocationOnMock invocation) throws Exception {
				@SuppressWarnings("unchecked")
				Collector<String> out = invocation.getArgument(4);
				out.collect("Hallo");
				out.collect("Ciao");
				return null;
			}
		}).when(mockWindowFunction).process(eq(0), eq(new TimeWindow(0, 2)), anyInternalWindowContext(), intIterable(0), WindowOperatorContractTest.<String>anyCollector());

		timeAdaptor.shouldRegisterTimerOnElement(mockTrigger, 1);

		testHarness.processElement(new StreamRecord<>(0, 0L));

		verify(mockWindowFunction, never()).process(anyInt(), anyTimeWindow(), anyInternalWindowContext(), anyIntIterable(), WindowOperatorContractTest.<String>anyCollector());
		assertTrue(testHarness.extractOutputStreamRecords().isEmpty());

		timeAdaptor.shouldFireOnTime(mockTrigger);

		timeAdaptor.advanceTime(testHarness, 1L);

		verify(mockWindowFunction, times(1)).process(eq(0), eq(new TimeWindow(0, 2)), anyInternalWindowContext(), intIterable(0), WindowOperatorContractTest.<String>anyCollector());

		assertThat(testHarness.extractOutputStreamRecords(),
				contains(streamRecord("Hallo", 1L), streamRecord("Ciao", 1L)));
	}
 
Example 9
Source File: WindowOperatorContractTest.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
private void testTriggerTimerAndGarbageCollectionTimerCoincide(final TimeDomainAdaptor timeAdaptor) throws Exception {
	WindowAssigner<Integer, TimeWindow> mockAssigner = mockTimeWindowAssigner();
	timeAdaptor.setIsEventTime(mockAssigner);
	Trigger<Integer, TimeWindow> mockTrigger = mockTrigger();
	InternalWindowFunction<Iterable<Integer>, Void, Integer, TimeWindow> mockWindowFunction = mockWindowFunction();

	KeyedOneInputStreamOperatorTestHarness<Integer, Integer, Void> testHarness =
			createWindowOperator(mockAssigner, mockTrigger, 0L, mockWindowFunction);

	testHarness.open();

	when(mockAssigner.assignWindows(anyInt(), anyLong(), anyAssignerContext()))
			.thenReturn(Arrays.asList(new TimeWindow(0, 20)));

	assertEquals(0, testHarness.getOutput().size());
	assertEquals(0, testHarness.numKeyedStateEntries());

	doAnswer(new Answer<TriggerResult>() {
		@Override
		public TriggerResult answer(InvocationOnMock invocation) throws Exception {
			Trigger.TriggerContext context = (Trigger.TriggerContext) invocation.getArguments()[3];
			// 19 is maxTime of window
			timeAdaptor.registerTimer(context, 19L);
			return TriggerResult.CONTINUE;
		}
	}).when(mockTrigger).onElement(Matchers.<Integer>anyObject(), anyLong(), anyTimeWindow(), anyTriggerContext());

	testHarness.processElement(new StreamRecord<>(0, 0L));

	// just the window contents
	assertEquals(1, testHarness.numKeyedStateEntries());

	assertEquals(1, timeAdaptor.numTimers(testHarness));
	assertEquals(0, timeAdaptor.numTimersOtherDomain(testHarness));

	verify(mockTrigger, never()).clear(anyTimeWindow(), anyTriggerContext());

	timeAdaptor.advanceTime(testHarness, 19); // 19 is maxTime of the window

	verify(mockTrigger, times(1)).clear(anyTimeWindow(), anyTriggerContext());
	timeAdaptor.verifyTriggerCallback(mockTrigger, times(1), null, null);

	assertEquals(0, timeAdaptor.numTimers(testHarness));
	assertEquals(0, timeAdaptor.numTimersOtherDomain(testHarness));
}
 
Example 10
Source File: WindowOperatorContractTest.java    From flink with Apache License 2.0 4 votes vote down vote up
private void testOnTimePurge(final TimeDomainAdaptor timeAdaptor) throws Exception {

		WindowAssigner<Integer, TimeWindow> mockAssigner = mockTimeWindowAssigner();
		timeAdaptor.setIsEventTime(mockAssigner);
		Trigger<Integer, TimeWindow> mockTrigger = mockTrigger();
		InternalWindowFunction<Iterable<Integer>, Void, Integer, TimeWindow> mockWindowFunction = mockWindowFunction();

		KeyedOneInputStreamOperatorTestHarness<Integer, Integer, Void> testHarness =
				createWindowOperator(mockAssigner, mockTrigger, 0L, mockWindowFunction);

		testHarness.open();

		timeAdaptor.advanceTime(testHarness, Long.MIN_VALUE);

		when(mockAssigner.assignWindows(anyInt(), anyLong(), anyAssignerContext()))
				.thenReturn(Arrays.asList(new TimeWindow(2, 4), new TimeWindow(4, 6)));

		assertEquals(0, testHarness.extractOutputStreamRecords().size());
		assertEquals(0, testHarness.numKeyedStateEntries());

		doAnswer(new Answer<TriggerResult>() {
			@Override
			public TriggerResult answer(InvocationOnMock invocation) throws Exception {
				Trigger.TriggerContext context = (Trigger.TriggerContext) invocation.getArguments()[3];
				// don't interfere with cleanup timers
				timeAdaptor.registerTimer(context, 1L);
				context.getPartitionedState(valueStateDescriptor).update("hello");
				return TriggerResult.CONTINUE;
			}
		}).when(mockTrigger).onElement(Matchers.<Integer>anyObject(), anyLong(), anyTimeWindow(), anyTriggerContext());

		timeAdaptor.shouldPurgeOnTime(mockTrigger);

		testHarness.processElement(new StreamRecord<>(0, 0L));

		assertEquals(4, testHarness.numKeyedStateEntries()); // window-contents and trigger state for two windows
		assertEquals(4, timeAdaptor.numTimers(testHarness)); // timers/gc timers for two windows

		timeAdaptor.advanceTime(testHarness, 1L);

		// clear is only called at cleanup time/GC time
		verify(mockTrigger, never()).clear(anyTimeWindow(), anyTriggerContext());

		// PURGE should purge contents
		assertEquals(2, testHarness.numKeyedStateEntries()); // trigger state will stick around
		assertEquals(2, timeAdaptor.numTimers(testHarness)); // gc timers are still there

		// still no output
		assertEquals(0, testHarness.extractOutputStreamRecords().size());
	}
 
Example 11
Source File: WindowOperatorContractTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test
public void testOnElementFire() throws Exception {

	WindowAssigner<Integer, TimeWindow> mockAssigner = mockTimeWindowAssigner();
	Trigger<Integer, TimeWindow> mockTrigger = mockTrigger();
	InternalWindowFunction<Iterable<Integer>, Void, Integer, TimeWindow> mockWindowFunction = mockWindowFunction();

	KeyedOneInputStreamOperatorTestHarness<Integer, Integer, Void> testHarness =
			createWindowOperator(mockAssigner, mockTrigger, 0L, mockWindowFunction);

	testHarness.open();

	when(mockAssigner.assignWindows(anyInt(), anyLong(), anyAssignerContext()))
			.thenReturn(Arrays.asList(new TimeWindow(2, 4), new TimeWindow(0, 2)));

	assertEquals(0, testHarness.getOutput().size());
	assertEquals(0, testHarness.numKeyedStateEntries());

	doAnswer(new Answer<TriggerResult>() {
		@Override
		public TriggerResult answer(InvocationOnMock invocation) throws Exception {
			TimeWindow window = (TimeWindow) invocation.getArguments()[2];
			Trigger.TriggerContext context = (Trigger.TriggerContext) invocation.getArguments()[3];
			context.registerEventTimeTimer(window.getEnd());
			context.getPartitionedState(valueStateDescriptor).update("hello");
			return TriggerResult.FIRE;
		}
	}).when(mockTrigger).onElement(Matchers.<Integer>anyObject(), anyLong(), anyTimeWindow(), anyTriggerContext());

	testHarness.processElement(new StreamRecord<>(0, 0L));

	verify(mockWindowFunction, times(2)).process(eq(0), anyTimeWindow(), anyInternalWindowContext(), anyIntIterable(), WindowOperatorContractTest.<Void>anyCollector());
	verify(mockWindowFunction, times(1)).process(eq(0), eq(new TimeWindow(0, 2)), anyInternalWindowContext(), intIterable(0), WindowOperatorContractTest.<Void>anyCollector());
	verify(mockWindowFunction, times(1)).process(eq(0), eq(new TimeWindow(2, 4)), anyInternalWindowContext(), intIterable(0), WindowOperatorContractTest.<Void>anyCollector());

	// clear is only called at cleanup time/GC time
	verify(mockTrigger, never()).clear(anyTimeWindow(), anyTriggerContext());

	// FIRE should not purge contents
	assertEquals(4, testHarness.numKeyedStateEntries()); // window contents plus trigger state
	assertEquals(4, testHarness.numEventTimeTimers()); // window timers/gc timers
}
 
Example 12
Source File: CEPOperatorTest.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Tests that the internal time of a CEP operator advances only given watermarks. See FLINK-5033
 */
@Test
public void testKeyedAdvancingTimeWithoutElements() throws Exception {
	final Event startEvent = new Event(42, "start", 1.0);
	final long watermarkTimestamp1 = 5L;
	final long watermarkTimestamp2 = 13L;

	final Map<String, List<Event>> expectedSequence = new HashMap<>(2);
	expectedSequence.put("start", Collections.<Event>singletonList(startEvent));

	final OutputTag<Tuple2<Map<String, List<Event>>, Long>> timedOut =
		new OutputTag<Tuple2<Map<String, List<Event>>, Long>>("timedOut") {};
	final KeyedOneInputStreamOperatorTestHarness<Integer, Event, Map<String, List<Event>>> harness =
		new KeyedOneInputStreamOperatorTestHarness<>(
			new CepOperator<>(
				Event.createTypeSerializer(),
				false,
				new NFAFactory(true),
				null,
				null,
				new TimedOutProcessFunction(timedOut),
				null), new KeySelector<Event, Integer>() {
			private static final long serialVersionUID = 7219185117566268366L;

			@Override
			public Integer getKey(Event value) throws Exception {
				return value.getId();
			}
		}, BasicTypeInfo.INT_TYPE_INFO);

	try {
		String rocksDbPath = tempFolder.newFolder().getAbsolutePath();
		RocksDBStateBackend rocksDBStateBackend = new RocksDBStateBackend(new MemoryStateBackend());
		rocksDBStateBackend.setDbStoragePath(rocksDbPath);

		harness.setStateBackend(rocksDBStateBackend);
		harness.setup(
			new KryoSerializer<>(
				(Class<Map<String, List<Event>>>) (Object) Map.class,
				new ExecutionConfig()));
		harness.open();

		harness.processElement(new StreamRecord<>(startEvent, 3L));
		harness.processWatermark(new Watermark(watermarkTimestamp1));
		harness.processWatermark(new Watermark(watermarkTimestamp2));

		Queue<Object> result = harness.getOutput();
		Queue<StreamRecord<Tuple2<Map<String, List<Event>>, Long>>> sideOutput = harness.getSideOutput(timedOut);

		assertEquals(2L, result.size());
		assertEquals(1L, sideOutput.size());

		Object watermark1 = result.poll();

		assertTrue(watermark1 instanceof Watermark);

		assertEquals(watermarkTimestamp1, ((Watermark) watermark1).getTimestamp());

		Tuple2<Map<String, List<Event>>, Long> leftResult = sideOutput.poll().getValue();

		assertEquals(watermarkTimestamp2, (long) leftResult.f1);
		assertEquals(expectedSequence, leftResult.f0);

		Object watermark2 = result.poll();

		assertTrue(watermark2 instanceof Watermark);

		assertEquals(watermarkTimestamp2, ((Watermark) watermark2).getTimestamp());
	} finally {
		harness.close();
	}
}
 
Example 13
Source File: WindowOperatorContractTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test
public void testOnElementFire() throws Exception {

	WindowAssigner<Integer, TimeWindow> mockAssigner = mockTimeWindowAssigner();
	Trigger<Integer, TimeWindow> mockTrigger = mockTrigger();
	InternalWindowFunction<Iterable<Integer>, Void, Integer, TimeWindow> mockWindowFunction = mockWindowFunction();

	KeyedOneInputStreamOperatorTestHarness<Integer, Integer, Void> testHarness =
			createWindowOperator(mockAssigner, mockTrigger, 0L, mockWindowFunction);

	testHarness.open();

	when(mockAssigner.assignWindows(anyInt(), anyLong(), anyAssignerContext()))
			.thenReturn(Arrays.asList(new TimeWindow(2, 4), new TimeWindow(0, 2)));

	assertEquals(0, testHarness.getOutput().size());
	assertEquals(0, testHarness.numKeyedStateEntries());

	doAnswer(new Answer<TriggerResult>() {
		@Override
		public TriggerResult answer(InvocationOnMock invocation) throws Exception {
			TimeWindow window = (TimeWindow) invocation.getArguments()[2];
			Trigger.TriggerContext context = (Trigger.TriggerContext) invocation.getArguments()[3];
			context.registerEventTimeTimer(window.getEnd());
			context.getPartitionedState(valueStateDescriptor).update("hello");
			return TriggerResult.FIRE;
		}
	}).when(mockTrigger).onElement(Matchers.<Integer>anyObject(), anyLong(), anyTimeWindow(), anyTriggerContext());

	testHarness.processElement(new StreamRecord<>(0, 0L));

	verify(mockWindowFunction, times(2)).process(eq(0), anyTimeWindow(), anyInternalWindowContext(), anyIntIterable(), WindowOperatorContractTest.<Void>anyCollector());
	verify(mockWindowFunction, times(1)).process(eq(0), eq(new TimeWindow(0, 2)), anyInternalWindowContext(), intIterable(0), WindowOperatorContractTest.<Void>anyCollector());
	verify(mockWindowFunction, times(1)).process(eq(0), eq(new TimeWindow(2, 4)), anyInternalWindowContext(), intIterable(0), WindowOperatorContractTest.<Void>anyCollector());

	// clear is only called at cleanup time/GC time
	verify(mockTrigger, never()).clear(anyTimeWindow(), anyTriggerContext());

	// FIRE should not purge contents
	assertEquals(4, testHarness.numKeyedStateEntries()); // window contents plus trigger state
	assertEquals(4, testHarness.numEventTimeTimers()); // window timers/gc timers
}
 
Example 14
Source File: WindowOperatorContractTest.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
private void testOnTimeFire(final TimeDomainAdaptor timeAdaptor) throws Exception {

		WindowAssigner<Integer, TimeWindow> mockAssigner = mockTimeWindowAssigner();
		timeAdaptor.setIsEventTime(mockAssigner);
		Trigger<Integer, TimeWindow> mockTrigger = mockTrigger();
		InternalWindowFunction<Iterable<Integer>, Void, Integer, TimeWindow> mockWindowFunction = mockWindowFunction();

		KeyedOneInputStreamOperatorTestHarness<Integer, Integer, Void> testHarness =
				createWindowOperator(mockAssigner, mockTrigger, 0L, mockWindowFunction);

		testHarness.open();

		timeAdaptor.advanceTime(testHarness, Long.MIN_VALUE);

		when(mockAssigner.assignWindows(anyInt(), anyLong(), anyAssignerContext()))
				.thenReturn(Arrays.asList(new TimeWindow(2, 4), new TimeWindow(0, 2)));

		assertEquals(0, testHarness.extractOutputStreamRecords().size());
		assertEquals(0, testHarness.numKeyedStateEntries());

		doAnswer(new Answer<TriggerResult>() {
			@Override
			public TriggerResult answer(InvocationOnMock invocation) throws Exception {
				Trigger.TriggerContext context = (Trigger.TriggerContext) invocation.getArguments()[3];
				// don't interfere with cleanup timers
				timeAdaptor.registerTimer(context, 0L);
				context.getPartitionedState(valueStateDescriptor).update("hello");
				return TriggerResult.CONTINUE;
			}
		}).when(mockTrigger).onElement(Matchers.<Integer>anyObject(), anyLong(), anyTimeWindow(), anyTriggerContext());

		timeAdaptor.shouldFireOnTime(mockTrigger);

		testHarness.processElement(new StreamRecord<>(0, 0L));

		assertEquals(4, testHarness.numKeyedStateEntries()); // window-contents and trigger state for two windows
		assertEquals(4, timeAdaptor.numTimers(testHarness)); // timers/gc timers for two windows

		timeAdaptor.advanceTime(testHarness, 0L);

		verify(mockWindowFunction, times(2)).process(eq(0), anyTimeWindow(), anyInternalWindowContext(), anyIntIterable(), WindowOperatorContractTest.<Void>anyCollector());
		verify(mockWindowFunction, times(1)).process(eq(0), eq(new TimeWindow(0, 2)), anyInternalWindowContext(), intIterable(0), WindowOperatorContractTest.<Void>anyCollector());
		verify(mockWindowFunction, times(1)).process(eq(0), eq(new TimeWindow(2, 4)), anyInternalWindowContext(), intIterable(0), WindowOperatorContractTest.<Void>anyCollector());

		// clear is only called at cleanup time/GC time
		verify(mockTrigger, never()).clear(anyTimeWindow(), anyTriggerContext());

		// FIRE should not purge contents
		assertEquals(4, testHarness.numKeyedStateEntries());
		assertEquals(2, timeAdaptor.numTimers(testHarness)); // only gc timers left
	}
 
Example 15
Source File: WindowOperatorContractTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test
public void testOnElementContinue() throws Exception {

	WindowAssigner<Integer, TimeWindow> mockAssigner = mockTimeWindowAssigner();
	Trigger<Integer, TimeWindow> mockTrigger = mockTrigger();
	InternalWindowFunction<Iterable<Integer>, Void, Integer, TimeWindow> mockWindowFunction = mockWindowFunction();

	KeyedOneInputStreamOperatorTestHarness<Integer, Integer, Void> testHarness =
			createWindowOperator(mockAssigner, mockTrigger, 0L, mockWindowFunction);

	testHarness.open();

	when(mockAssigner.assignWindows(anyInt(), anyLong(), anyAssignerContext()))
			.thenReturn(Arrays.asList(new TimeWindow(2, 4), new TimeWindow(0, 2)));

	assertEquals(0, testHarness.getOutput().size());
	assertEquals(0, testHarness.numKeyedStateEntries());

	doAnswer(new Answer<TriggerResult>() {
		@Override
		public TriggerResult answer(InvocationOnMock invocation) throws Exception {
			TimeWindow window = (TimeWindow) invocation.getArguments()[2];
			Trigger.TriggerContext context = (Trigger.TriggerContext) invocation.getArguments()[3];
			context.registerEventTimeTimer(window.getEnd());
			context.getPartitionedState(valueStateDescriptor).update("hello");
			return TriggerResult.CONTINUE;
		}
	}).when(mockTrigger).onElement(Matchers.<Integer>anyObject(), anyLong(), anyTimeWindow(), anyTriggerContext());

	testHarness.processElement(new StreamRecord<>(0, 0L));

	// clear is only called at cleanup time/GC time
	verify(mockTrigger, never()).clear(anyTimeWindow(), anyTriggerContext());

	// CONTINUE should not purge contents
	assertEquals(4, testHarness.numKeyedStateEntries()); // window contents plus trigger state
	assertEquals(4, testHarness.numEventTimeTimers()); // window timers/gc timers

	// there should be no firing
	assertEquals(0, testHarness.getOutput().size());
}
 
Example 16
Source File: WindowOperatorContractTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test
public void testOnElementPurge() throws Exception {

	WindowAssigner<Integer, TimeWindow> mockAssigner = mockTimeWindowAssigner();
	Trigger<Integer, TimeWindow> mockTrigger = mockTrigger();
	InternalWindowFunction<Iterable<Integer>, Void, Integer, TimeWindow> mockWindowFunction = mockWindowFunction();

	KeyedOneInputStreamOperatorTestHarness<Integer, Integer, Void> testHarness =
			createWindowOperator(mockAssigner, mockTrigger, 0L, mockWindowFunction);

	testHarness.open();

	when(mockAssigner.assignWindows(anyInt(), anyLong(), anyAssignerContext()))
			.thenReturn(Arrays.asList(new TimeWindow(2, 4), new TimeWindow(0, 2)));

	assertEquals(0, testHarness.getOutput().size());
	assertEquals(0, testHarness.numKeyedStateEntries());

	doAnswer(new Answer<TriggerResult>() {
		@Override
		public TriggerResult answer(InvocationOnMock invocation) throws Exception {
			Trigger.TriggerContext context = (Trigger.TriggerContext) invocation.getArguments()[3];
			context.registerEventTimeTimer(0L);
			context.getPartitionedState(valueStateDescriptor).update("hello");
			return TriggerResult.PURGE;
		}
	}).when(mockTrigger).onElement(Matchers.<Integer>anyObject(), anyLong(), anyTimeWindow(), anyTriggerContext());

	testHarness.processElement(new StreamRecord<>(0, 0L));

	// clear is only called at cleanup time/GC time
	verify(mockTrigger, never()).clear(anyTimeWindow(), anyTriggerContext());

	// PURGE should purge contents
	assertEquals(2, testHarness.numKeyedStateEntries()); // trigger state will stick around until GC time

	// timers will stick around
	assertEquals(4, testHarness.numEventTimeTimers()); // trigger timer and GC timer

	// no output
	assertEquals(0, testHarness.getOutput().size());
}
 
Example 17
Source File: WindowOperatorContractTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test
public void testOnElementPurge() throws Exception {

	WindowAssigner<Integer, TimeWindow> mockAssigner = mockTimeWindowAssigner();
	Trigger<Integer, TimeWindow> mockTrigger = mockTrigger();
	InternalWindowFunction<Iterable<Integer>, Void, Integer, TimeWindow> mockWindowFunction = mockWindowFunction();

	KeyedOneInputStreamOperatorTestHarness<Integer, Integer, Void> testHarness =
			createWindowOperator(mockAssigner, mockTrigger, 0L, mockWindowFunction);

	testHarness.open();

	when(mockAssigner.assignWindows(anyInt(), anyLong(), anyAssignerContext()))
			.thenReturn(Arrays.asList(new TimeWindow(2, 4), new TimeWindow(0, 2)));

	assertEquals(0, testHarness.getOutput().size());
	assertEquals(0, testHarness.numKeyedStateEntries());

	doAnswer(new Answer<TriggerResult>() {
		@Override
		public TriggerResult answer(InvocationOnMock invocation) throws Exception {
			Trigger.TriggerContext context = (Trigger.TriggerContext) invocation.getArguments()[3];
			context.registerEventTimeTimer(0L);
			context.getPartitionedState(valueStateDescriptor).update("hello");
			return TriggerResult.PURGE;
		}
	}).when(mockTrigger).onElement(Matchers.<Integer>anyObject(), anyLong(), anyTimeWindow(), anyTriggerContext());

	testHarness.processElement(new StreamRecord<>(0, 0L));

	// clear is only called at cleanup time/GC time
	verify(mockTrigger, never()).clear(anyTimeWindow(), anyTriggerContext());

	// PURGE should purge contents
	assertEquals(2, testHarness.numKeyedStateEntries()); // trigger state will stick around until GC time

	// timers will stick around
	assertEquals(4, testHarness.numEventTimeTimers()); // trigger timer and GC timer

	// no output
	assertEquals(0, testHarness.getOutput().size());
}
 
Example 18
Source File: WindowOperatorContractTest.java    From flink with Apache License 2.0 3 votes vote down vote up
private void testTimerCreationAndDeletion(TimeDomainAdaptor timeAdaptor) throws Exception {

		WindowAssigner<Integer, TimeWindow> mockAssigner = mockTimeWindowAssigner();
		timeAdaptor.setIsEventTime(mockAssigner);

		Trigger<Integer, TimeWindow> mockTrigger = mockTrigger();
		InternalWindowFunction<Iterable<Integer>, Void, Integer, TimeWindow> mockWindowFunction = mockWindowFunction();

		KeyedOneInputStreamOperatorTestHarness<Integer, Integer, Void> testHarness =
				createWindowOperator(mockAssigner, mockTrigger, 0L, mockWindowFunction);

		testHarness.open();

		when(mockAssigner.assignWindows(anyInt(), anyLong(), anyAssignerContext()))
				.thenReturn(Arrays.asList(new TimeWindow(0, 2)));

		assertEquals(0, timeAdaptor.numTimers(testHarness));

		timeAdaptor.shouldRegisterTimerOnElement(mockTrigger, 17);
		testHarness.processElement(new StreamRecord<>(0, 0L));

		assertEquals(2, timeAdaptor.numTimers(testHarness)); // +1 because of the GC timer of the window

		timeAdaptor.shouldRegisterTimerOnElement(mockTrigger, 42);
		testHarness.processElement(new StreamRecord<>(0, 0L));

		assertEquals(3, timeAdaptor.numTimers(testHarness)); // +1 because of the GC timer of the window

		timeAdaptor.shouldDeleteTimerOnElement(mockTrigger, 42);
		testHarness.processElement(new StreamRecord<>(0, 0L));

		timeAdaptor.shouldDeleteTimerOnElement(mockTrigger, 17);
		testHarness.processElement(new StreamRecord<>(0, 0L));

		assertEquals(1, timeAdaptor.numTimers(testHarness)); // +1 because of the GC timer of the window
	}
 
Example 19
Source File: WindowOperatorContractTest.java    From flink with Apache License 2.0 3 votes vote down vote up
private void testDeletedTimerDoesNotFire(TimeDomainAdaptor timeAdaptor) throws Exception {

		WindowAssigner<Integer, TimeWindow> mockAssigner = mockTimeWindowAssigner();
		timeAdaptor.setIsEventTime(mockAssigner);
		Trigger<Integer, TimeWindow> mockTrigger = mockTrigger();
		InternalWindowFunction<Iterable<Integer>, Void, Integer, TimeWindow> mockWindowFunction = mockWindowFunction();

		KeyedOneInputStreamOperatorTestHarness<Integer, Integer, Void> testHarness =
				createWindowOperator(mockAssigner, mockTrigger, 0L, mockWindowFunction);

		testHarness.open();

		when(mockAssigner.assignWindows(anyInt(), anyLong(), anyAssignerContext()))
				.thenReturn(Arrays.asList(new TimeWindow(0, 100)));

		assertEquals(0, timeAdaptor.numTimers(testHarness));

		timeAdaptor.shouldRegisterTimerOnElement(mockTrigger, 1);
		testHarness.processElement(new StreamRecord<>(0, 0L));

		assertEquals(2, timeAdaptor.numTimers(testHarness)); // +1 for the GC timer

		timeAdaptor.shouldDeleteTimerOnElement(mockTrigger, 1);
		testHarness.processElement(new StreamRecord<>(0, 0L));

		assertEquals(1, timeAdaptor.numTimers(testHarness)); // +1 for the GC timer

		timeAdaptor.shouldRegisterTimerOnElement(mockTrigger, 2);
		testHarness.processElement(new StreamRecord<>(0, 0L));

		assertEquals(2, timeAdaptor.numTimers(testHarness)); // +1 for the GC timer

		timeAdaptor.advanceTime(testHarness, 50L);

		timeAdaptor.verifyTriggerCallback(mockTrigger, times(0), 1L, null);
		timeAdaptor.verifyTriggerCallback(mockTrigger, times(1), 2L, new TimeWindow(0, 100));
		timeAdaptor.verifyTriggerCallback(mockTrigger, times(1), null, null);

		assertEquals(1, timeAdaptor.numTimers(testHarness)); // +1 for the GC timer
	}
 
Example 20
Source File: WindowOperatorContractTest.java    From flink with Apache License 2.0 2 votes vote down vote up
@Test
public void testWindowsDontInterfere() throws Exception {

	WindowAssigner<Integer, TimeWindow> mockAssigner = mockTimeWindowAssigner();
	Trigger<Integer, TimeWindow> mockTrigger = mockTrigger();
	InternalWindowFunction<Iterable<Integer>, Void, Integer, TimeWindow> mockWindowFunction = mockWindowFunction();

	KeyedOneInputStreamOperatorTestHarness<Integer, Integer, Void> testHarness =
			createWindowOperator(mockAssigner, mockTrigger, 0L, mockWindowFunction);

	testHarness.open();

	when(mockAssigner.assignWindows(anyInt(), anyLong(), anyAssignerContext()))
			.thenReturn(Collections.singletonList(new TimeWindow(0, 2)));

	testHarness.processElement(new StreamRecord<>(0, 0L));

	when(mockAssigner.assignWindows(anyInt(), anyLong(), anyAssignerContext()))
			.thenReturn(Collections.singletonList(new TimeWindow(0, 1)));

	testHarness.processElement(new StreamRecord<>(1, 0L));

	// no output so far
	assertTrue(testHarness.extractOutputStreamRecords().isEmpty());

	// state for two windows
	assertEquals(2, testHarness.numKeyedStateEntries());
	assertEquals(2, testHarness.numEventTimeTimers());

	// now we fire
	shouldFireOnElement(mockTrigger);

	when(mockAssigner.assignWindows(anyInt(), anyLong(), anyAssignerContext()))
			.thenReturn(Collections.singletonList(new TimeWindow(0, 1)));

	testHarness.processElement(new StreamRecord<>(1, 0L));

	when(mockAssigner.assignWindows(anyInt(), anyLong(), anyAssignerContext()))
			.thenReturn(Collections.singletonList(new TimeWindow(0, 2)));

	testHarness.processElement(new StreamRecord<>(0, 0L));

	verify(mockWindowFunction, times(2)).process(anyInt(), anyTimeWindow(), anyInternalWindowContext(), anyIntIterable(), WindowOperatorContractTest.<Void>anyCollector());
	verify(mockWindowFunction, times(1)).process(eq(0), eq(new TimeWindow(0, 2)), anyInternalWindowContext(), intIterable(0, 0), WindowOperatorContractTest.<Void>anyCollector());
	verify(mockWindowFunction, times(1)).process(eq(1), eq(new TimeWindow(0, 1)), anyInternalWindowContext(), intIterable(1, 1), WindowOperatorContractTest.<Void>anyCollector());
}