Java Code Examples for org.apache.flink.streaming.util.KeyedOneInputStreamOperatorTestHarness#open()
The following examples show how to use
org.apache.flink.streaming.util.KeyedOneInputStreamOperatorTestHarness#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: WindowOperatorContractTest.java From Flink-CEPplus with Apache License 2.0 | 5 votes |
@Test public void testLateWindowDropping() 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); // window.maxTime() == 1 plus 20L of allowed lateness testHarness.processWatermark(new Watermark(21)); testHarness.processElement(new StreamRecord<>(0, 0L)); // there should be nothing assertEquals(0, testHarness.numKeyedStateEntries()); assertEquals(0, testHarness.numEventTimeTimers()); assertEquals(0, testHarness.numProcessingTimeTimers()); // there should be two elements now assertEquals(0, testHarness.extractOutputStreamRecords().size()); }
Example 2
Source File: AllWindowTranslationTest.java From Flink-CEPplus with Apache License 2.0 | 5 votes |
/** * 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 3
Source File: WindowOperatorContractTest.java From Flink-CEPplus with Apache License 2.0 | 5 votes |
@Test public void testNoEventTimeGarbageCollectionTimerForLongMax() 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, Long.MAX_VALUE - 10))); assertEquals(0, testHarness.getOutput().size()); assertEquals(0, testHarness.numKeyedStateEntries()); testHarness.processElement(new StreamRecord<>(0, 0L)); // just the window contents assertEquals(1, testHarness.numKeyedStateEntries()); // no GC timer assertEquals(0, testHarness.numEventTimeTimers()); assertEquals(0, testHarness.numProcessingTimeTimers()); }
Example 4
Source File: WindowOperatorContractTest.java From flink with Apache License 2.0 | 5 votes |
@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 5
Source File: WindowOperatorContractTest.java From flink with Apache License 2.0 | 4 votes |
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 6
Source File: WindowDoFnOperatorTest.java From beam with Apache License 2.0 | 4 votes |
@Test public void testTimerCleanupOfPendingTimerList() throws Exception { // test harness WindowDoFnOperator<Long, Long, Long> windowDoFnOperator = getWindowDoFnOperator(); KeyedOneInputStreamOperatorTestHarness< ByteBuffer, WindowedValue<KeyedWorkItem<Long, Long>>, WindowedValue<KV<Long, Long>>> testHarness = createTestHarness(windowDoFnOperator); testHarness.open(); DoFnOperator<KeyedWorkItem<Long, Long>, KV<Long, Long>>.FlinkTimerInternals timerInternals = windowDoFnOperator.timerInternals; // process elements IntervalWindow window = new IntervalWindow(new Instant(0), Duration.millis(100)); IntervalWindow window2 = new IntervalWindow(new Instant(100), Duration.millis(100)); testHarness.processWatermark(0L); // Use two different keys to check for correct watermark hold calculation testHarness.processElement( Item.builder().key(1L).timestamp(1L).value(100L).window(window).build().toStreamRecord()); testHarness.processElement( Item.builder() .key(2L) .timestamp(150L) .value(150L) .window(window2) .build() .toStreamRecord()); testHarness.processWatermark(1); // Note that the following is 1 because the state is key-partitioned assertThat(Iterables.size(timerInternals.pendingTimersById.keys()), is(1)); assertThat(testHarness.numKeyedStateEntries(), is(6)); assertThat(windowDoFnOperator.getCurrentOutputWatermark(), is(1L)); assertThat(timerInternals.getMinOutputTimestampMs(), is(Long.MAX_VALUE)); // close window testHarness.processWatermark(100L); // Note that the following is zero because we only the first key is active assertThat(Iterables.size(timerInternals.pendingTimersById.keys()), is(0)); assertThat(testHarness.numKeyedStateEntries(), is(3)); assertThat(windowDoFnOperator.getCurrentOutputWatermark(), is(100L)); assertThat(timerInternals.getMinOutputTimestampMs(), is(Long.MAX_VALUE)); testHarness.processWatermark(200L); // All the state has been cleaned up assertThat(testHarness.numKeyedStateEntries(), is(0)); assertThat( stripStreamRecordFromWindowedValue(testHarness.getOutput()), containsInAnyOrder( WindowedValue.of( KV.of(1L, 100L), new Instant(99), window, PaneInfo.createPane(true, true, ON_TIME)), WindowedValue.of( KV.of(2L, 150L), new Instant(199), window2, PaneInfo.createPane(true, true, ON_TIME)))); // cleanup testHarness.close(); }
Example 7
Source File: RegularWindowOperatorContractTest.java From Flink-CEPplus with Apache License 2.0 | 4 votes |
@Test public void testReducingWindow() throws Exception { WindowAssigner<Integer, TimeWindow> mockAssigner = mockTimeWindowAssigner(); Trigger<Integer, TimeWindow> mockTrigger = mockTrigger(); InternalWindowFunction<Integer, Void, Integer, TimeWindow> mockWindowFunction = mockWindowFunction(); ReducingStateDescriptor<Integer> intReduceSumDescriptor = new ReducingStateDescriptor<>( "int-reduce", new ReduceFunction<Integer>() { private static final long serialVersionUID = 1L; @Override public Integer reduce(Integer a, Integer b) throws Exception { return a + b; } }, IntSerializer.INSTANCE); final ValueStateDescriptor<String> valueStateDescriptor = new ValueStateDescriptor<>("string-state", StringSerializer.INSTANCE); KeyedOneInputStreamOperatorTestHarness<Integer, Integer, Void> testHarness = createWindowOperator(mockAssigner, mockTrigger, 0L, intReduceSumDescriptor, 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()); // insert two elements without firing testHarness.processElement(new StreamRecord<>(1, 0L)); testHarness.processElement(new StreamRecord<>(1, 0L)); 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<>(1, 0L)); verify(mockWindowFunction, times(2)).process(eq(1), anyTimeWindow(), anyInternalWindowContext(), anyInt(), WindowOperatorContractTest.<Void>anyCollector()); verify(mockWindowFunction, times(1)).process(eq(1), eq(new TimeWindow(0, 2)), anyInternalWindowContext(), eq(3), WindowOperatorContractTest.<Void>anyCollector()); verify(mockWindowFunction, times(1)).process(eq(1), eq(new TimeWindow(2, 4)), anyInternalWindowContext(), eq(3), 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 8
Source File: WindowOperatorContractTest.java From flink with Apache License 2.0 | 4 votes |
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 9
Source File: WindowOperatorContractTest.java From Flink-CEPplus with Apache License 2.0 | 4 votes |
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 10
Source File: WindowOperatorContractTest.java From Flink-CEPplus with Apache License 2.0 | 4 votes |
@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 11
Source File: WindowOperatorContractTest.java From flink with Apache License 2.0 | 4 votes |
/** * Verify that we neither invoke the trigger nor the window function if a timer * for an empty merging window fires. */ private void testNoTimerFiringForPurgedMergingWindow(final TimeDomainAdaptor timeAdaptor) throws Exception { MergingWindowAssigner<Integer, TimeWindow> mockAssigner = mockMergingAssigner(); timeAdaptor.setIsEventTime(mockAssigner); Trigger<Integer, TimeWindow> mockTrigger = mockTrigger(); @SuppressWarnings("unchecked") InternalWindowFunction<Iterable<Integer>, List<Integer>, Integer, TimeWindow> mockWindowFunction = mock(InternalWindowFunction.class); KeyedOneInputStreamOperatorTestHarness<Integer, Integer, List<Integer>> 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))); 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); return TriggerResult.PURGE; } }).when(mockTrigger).onElement(Matchers.<Integer>anyObject(), anyLong(), anyTimeWindow(), anyTriggerContext()); testHarness.processElement(new StreamRecord<>(0, 0L)); assertEquals(1, testHarness.numKeyedStateEntries()); // just the merging window set assertEquals(2, timeAdaptor.numTimers(testHarness)); // timer and gc timer timeAdaptor.advanceTime(testHarness, 0L); // trigger is not called if there is no more window (timer is silently ignored) timeAdaptor.verifyTriggerCallback(mockTrigger, times(1), null, null); verify(mockWindowFunction, never()) .process(anyInt(), anyTimeWindow(), anyInternalWindowContext(), anyIntIterable(), WindowOperatorContractTest.<List<Integer>>anyCollector()); assertEquals(1, timeAdaptor.numTimers(testHarness)); // only gc timers left }
Example 12
Source File: RegularWindowOperatorContractTest.java From flink with Apache License 2.0 | 4 votes |
@Test public void testFoldingWindow() throws Exception { WindowAssigner<Integer, TimeWindow> mockAssigner = mockTimeWindowAssigner(); Trigger<Integer, TimeWindow> mockTrigger = mockTrigger(); InternalWindowFunction<Integer, Void, Integer, TimeWindow> mockWindowFunction = mockWindowFunction(); FoldingStateDescriptor<Integer, Integer> intFoldSumDescriptor = new FoldingStateDescriptor<>( "int-fold", 0, new FoldFunction<Integer, Integer>() { private static final long serialVersionUID = 1L; @Override public Integer fold(Integer accumulator, Integer value) throws Exception { return accumulator + value; } }, IntSerializer.INSTANCE); final ValueStateDescriptor<String> valueStateDescriptor = new ValueStateDescriptor<>("string-state", StringSerializer.INSTANCE); KeyedOneInputStreamOperatorTestHarness<Integer, Integer, Void> testHarness = createWindowOperator(mockAssigner, mockTrigger, 0L, intFoldSumDescriptor, 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()); // insert two elements without firing testHarness.processElement(new StreamRecord<>(1, 0L)); testHarness.processElement(new StreamRecord<>(1, 0L)); 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<>(1, 0L)); verify(mockWindowFunction, times(2)).process(eq(1), anyTimeWindow(), anyInternalWindowContext(), anyInt(), WindowOperatorContractTest.<Void>anyCollector()); verify(mockWindowFunction, times(1)).process(eq(1), eq(new TimeWindow(0, 2)), anyInternalWindowContext(), eq(3), WindowOperatorContractTest.<Void>anyCollector()); verify(mockWindowFunction, times(1)).process(eq(1), eq(new TimeWindow(2, 4)), anyInternalWindowContext(), eq(3), 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 13
Source File: AbstractStreamOperatorTest.java From Flink-CEPplus with Apache License 2.0 | 4 votes |
/** * Verify that a low-level timer is set for processing-time timers in case of restore. */ @Test public void testEnsureProcessingTimeTimerRegisteredOnRestore() 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.setProcessingTime(0L); testHarness.processElement(new Tuple2<>(1, "SET_PROC_TIME_TIMER:20"), 0); testHarness.processElement(new Tuple2<>(0, "SET_STATE:HELLO"), 0); testHarness.processElement(new Tuple2<>(1, "SET_STATE:CIAO"), 0); testHarness.processElement(new Tuple2<>(0, "SET_PROC_TIME_TIMER:10"), 0); OperatorSubtaskState snapshot = testHarness.snapshot(0, 0); TestOperator testOperator1 = new TestOperator(); KeyedOneInputStreamOperatorTestHarness<Integer, Tuple2<Integer, String>, String> testHarness1 = new KeyedOneInputStreamOperatorTestHarness<>( testOperator1, new TestKeySelector(), BasicTypeInfo.INT_TYPE_INFO); testHarness1.setProcessingTime(0L); testHarness1.setup(); testHarness1.initializeState(snapshot); testHarness1.open(); testHarness1.setProcessingTime(10L); assertThat( extractResult(testHarness1), contains("ON_PROC_TIME:HELLO")); testHarness1.setProcessingTime(20L); assertThat( extractResult(testHarness1), contains("ON_PROC_TIME:CIAO")); }
Example 14
Source File: WindowOperatorContractTest.java From flink with Apache License 2.0 | 4 votes |
@Test public void testOnElementFireAndPurge() 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_AND_PURGE; } }).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_AND_PURGE should purge contents assertEquals(2, testHarness.numKeyedStateEntries()); // trigger state will stick around until GC time // timers will stick around assertEquals(4, testHarness.numEventTimeTimers()); }
Example 15
Source File: WindowOperatorContractTest.java From flink with Apache License 2.0 | 4 votes |
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 16
Source File: WindowOperatorContractTest.java From flink with Apache License 2.0 | 3 votes |
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 17
Source File: AbstractStreamOperatorTest.java From Flink-CEPplus with Apache License 2.0 | 3 votes |
/** * Verify that firing processing-time timers see the state of the key that was active * when the timer was set. */ @Test public void testProcessingTimeTimersDontInterfere() 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.setProcessingTime(0L); testHarness.processElement(new Tuple2<>(1, "SET_PROC_TIME_TIMER:20"), 0); testHarness.processElement(new Tuple2<>(0, "SET_STATE:HELLO"), 0); testHarness.processElement(new Tuple2<>(1, "SET_STATE:CIAO"), 0); testHarness.processElement(new Tuple2<>(0, "SET_PROC_TIME_TIMER:10"), 0); testHarness.setProcessingTime(10L); assertThat( extractResult(testHarness), contains("ON_PROC_TIME:HELLO")); testHarness.setProcessingTime(20L); assertThat( extractResult(testHarness), contains("ON_PROC_TIME:CIAO")); }
Example 18
Source File: WindowOperatorContractTest.java From Flink-CEPplus with Apache License 2.0 | 3 votes |
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 19
Source File: WindowOperatorContractTest.java From flink with Apache License 2.0 | 3 votes |
private void testOnTimePurgeDoesNotCleanupMergingSet(TimeDomainAdaptor timeAdaptor) throws Exception { MergingWindowAssigner<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, 0L, mockWindowFunction); testHarness.open(); when(mockAssigner.assignWindows(anyInt(), anyLong(), anyAssignerContext())) .thenReturn(Arrays.asList(new TimeWindow(0, 4))); assertEquals(0, testHarness.getOutput().size()); assertEquals(0, testHarness.numKeyedStateEntries()); timeAdaptor.shouldRegisterTimerOnElement(mockTrigger, 1L); testHarness.processElement(new StreamRecord<>(0, 0L)); timeAdaptor.shouldPurgeOnTime(mockTrigger); assertEquals(2, testHarness.numKeyedStateEntries()); // the merging window set + window contents assertEquals(2, timeAdaptor.numTimers(testHarness)); // one cleanup timer + timer assertEquals(0, testHarness.getOutput().size()); timeAdaptor.advanceTime(testHarness, 1L); assertEquals(1, testHarness.numKeyedStateEntries()); // the merging window set assertEquals(1, timeAdaptor.numTimers(testHarness)); // one cleanup timer assertEquals(0, testHarness.extractOutputStreamRecords().size()); }
Example 20
Source File: WindowOperatorContractTest.java From Flink-CEPplus with Apache License 2.0 | 2 votes |
@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()); }