Java Code Examples for org.apache.flink.cep.utils.TestTimerService

The following examples show how to use org.apache.flink.cep.utils.TestTimerService. These examples are extracted from open source projects. 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 Project: Flink-CEPplus   Source File: NFAITCase.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testSharedBufferClearing() throws Exception {
	Pattern<Event, ?> pattern = Pattern.<Event>begin("start").followedBy("end");

	Event a = new Event(40, "a", 1.0);
	Event b = new Event(41, "b", 2.0);

	NFA<Event> nfa = compile(pattern, false);
	TestTimerService timerService = new TestTimerService();
	try (SharedBufferAccessor<Event> accessor = Mockito.spy(sharedBuffer.getAccessor())) {
		nfa.process(accessor, nfa.createInitialNFAState(), a, 1, AfterMatchSkipStrategy.noSkip(),
			timerService);
		nfa.process(accessor, nfa.createInitialNFAState(), b, 2, AfterMatchSkipStrategy.noSkip(),
			timerService);
		Mockito.verify(accessor, Mockito.never()).advanceTime(anyLong());
		nfa.advanceTime(accessor, nfa.createInitialNFAState(), 2);
		Mockito.verify(accessor, Mockito.times(1)).advanceTime(2);
	}
}
 
Example 2
Source Project: flink   Source File: NFAITCase.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testSharedBufferClearing() throws Exception {
	Pattern<Event, ?> pattern = Pattern.<Event>begin("start").followedBy("end");

	Event a = new Event(40, "a", 1.0);
	Event b = new Event(41, "b", 2.0);

	NFA<Event> nfa = compile(pattern, false);
	TestTimerService timerService = new TestTimerService();
	try (SharedBufferAccessor<Event> accessor = Mockito.spy(sharedBuffer.getAccessor())) {
		nfa.process(accessor, nfa.createInitialNFAState(), a, 1, AfterMatchSkipStrategy.noSkip(),
			timerService);
		nfa.process(accessor, nfa.createInitialNFAState(), b, 2, AfterMatchSkipStrategy.noSkip(),
			timerService);
		Mockito.verify(accessor, Mockito.never()).advanceTime(anyLong());
		nfa.advanceTime(accessor, nfa.createInitialNFAState(), 2);
		Mockito.verify(accessor, Mockito.times(1)).advanceTime(2);
	}
}
 
Example 3
Source Project: flink   Source File: NFAITCase.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testSharedBufferClearing() throws Exception {
	Pattern<Event, ?> pattern = Pattern.<Event>begin("start").followedBy("end");

	Event a = new Event(40, "a", 1.0);
	Event b = new Event(41, "b", 2.0);

	NFA<Event> nfa = compile(pattern, false);
	TestTimerService timerService = new TestTimerService();
	try (SharedBufferAccessor<Event> accessor = Mockito.spy(sharedBuffer.getAccessor())) {
		nfa.process(accessor, nfa.createInitialNFAState(), a, 1, AfterMatchSkipStrategy.noSkip(),
			timerService);
		nfa.process(accessor, nfa.createInitialNFAState(), b, 2, AfterMatchSkipStrategy.noSkip(),
			timerService);
		Mockito.verify(accessor, Mockito.never()).advanceTime(anyLong());
		nfa.advanceTime(accessor, nfa.createInitialNFAState(), 2);
		Mockito.verify(accessor, Mockito.times(1)).advanceTime(2);
	}
}
 
Example 4
@Test
public void testCurrentProcessingTime() throws Exception {
	final Event event1 = event().withId(1).build();
	final Event event2 = event().withId(2).build();

	final Pattern<Event, ?> pattern = Pattern.<Event>begin("start").where(new IterativeCondition<Event>() {
		@Override
		public boolean filter(Event value, Context<Event> ctx) throws Exception {
			return ctx.currentProcessingTime() == 3;
		}
	});

	final TestTimerService cepTimerService = new TestTimerService();
	final NFATestHarness testHarness = forPattern(pattern)
		.withTimerService(cepTimerService)
		.build();

	cepTimerService.setCurrentProcessingTime(1);
	final List<List<Event>> resultingPatterns1 = testHarness.feedRecord(new StreamRecord<>(event1, 7));
	cepTimerService.setCurrentProcessingTime(3);
	final List<List<Event>> resultingPatterns2 = testHarness.feedRecord(new StreamRecord<>(event2, 8));

	compareMaps(resultingPatterns1, Collections.emptyList());
	compareMaps(resultingPatterns2, Collections.singletonList(
		Collections.singletonList(event2)
	));
}
 
Example 5
@Test
public void testCurrentProcessingTime() throws Exception {
	final Event event1 = event().withId(1).build();
	final Event event2 = event().withId(2).build();

	final Pattern<Event, ?> pattern = Pattern.<Event>begin("start").where(new IterativeCondition<Event>() {
		@Override
		public boolean filter(Event value, Context<Event> ctx) throws Exception {
			return ctx.currentProcessingTime() == 3;
		}
	});

	final TestTimerService cepTimerService = new TestTimerService();
	final NFATestHarness testHarness = forPattern(pattern)
		.withTimerService(cepTimerService)
		.build();

	cepTimerService.setCurrentProcessingTime(1);
	final List<List<Event>> resultingPatterns1 = testHarness.feedRecord(new StreamRecord<>(event1, 7));
	cepTimerService.setCurrentProcessingTime(3);
	final List<List<Event>> resultingPatterns2 = testHarness.feedRecord(new StreamRecord<>(event2, 8));

	compareMaps(resultingPatterns1, Collections.emptyList());
	compareMaps(resultingPatterns2, Collections.singletonList(
		Collections.singletonList(event2)
	));
}
 
Example 6
@Test
public void testCurrentProcessingTime() throws Exception {
	final Event event1 = event().withId(1).build();
	final Event event2 = event().withId(2).build();

	final Pattern<Event, ?> pattern = Pattern.<Event>begin("start").where(new IterativeCondition<Event>() {
		@Override
		public boolean filter(Event value, Context<Event> ctx) throws Exception {
			return ctx.currentProcessingTime() == 3;
		}
	});

	final TestTimerService cepTimerService = new TestTimerService();
	final NFATestHarness testHarness = forPattern(pattern)
		.withTimerService(cepTimerService)
		.build();

	cepTimerService.setCurrentProcessingTime(1);
	final List<List<Event>> resultingPatterns1 = testHarness.feedRecord(new StreamRecord<>(event1, 7));
	cepTimerService.setCurrentProcessingTime(3);
	final List<List<Event>> resultingPatterns2 = testHarness.feedRecord(new StreamRecord<>(event2, 8));

	comparePatterns(resultingPatterns1, Collections.emptyList());
	comparePatterns(resultingPatterns2, Collections.singletonList(
		Collections.singletonList(event2)
	));
}