use of org.apache.samza.task.TaskContext in project samza by apache.
the class TestOperatorImpls method testLinearChain.
@Test
public void testLinearChain() throws IllegalAccessException, InvocationTargetException {
// test creation of linear chain
StreamGraphImpl mockGraph = mock(StreamGraphImpl.class);
MessageStreamImpl<TestMessageEnvelope> testInput = TestMessageStreamImplUtil.getMessageStreamImpl(mockGraph);
TaskContext mockContext = mock(TaskContext.class);
when(mockContext.getMetricsRegistry()).thenReturn(new MetricsRegistryMap());
Config mockConfig = mock(Config.class);
testInput.map(m -> m).window(Windows.keyedSessionWindow(TestMessageEnvelope::getKey, Duration.ofMinutes(10)));
OperatorImplGraph opGraph = new OperatorImplGraph();
RootOperatorImpl operatorChain = (RootOperatorImpl) createOpsMethod.invoke(opGraph, testInput, mockConfig, mockContext);
Set<OperatorImpl> subsSet = (Set<OperatorImpl>) nextOperatorsField.get(operatorChain);
assertEquals(subsSet.size(), 1);
OperatorImpl<TestMessageEnvelope, TestMessageEnvelope> firstOpImpl = subsSet.iterator().next();
Set<OperatorImpl> subsOps = (Set<OperatorImpl>) nextOperatorsField.get(firstOpImpl);
assertEquals(subsOps.size(), 1);
OperatorImpl wndOpImpl = subsOps.iterator().next();
subsOps = (Set<OperatorImpl>) nextOperatorsField.get(wndOpImpl);
assertEquals(subsOps.size(), 0);
}
use of org.apache.samza.task.TaskContext in project samza by apache.
the class TestWindowOperator method testCancelationOfRepeatingNestedTriggers.
@Test
public void testCancelationOfRepeatingNestedTriggers() throws Exception {
StreamApplication sgb = new KeyedTumblingWindowStreamApplication(AccumulationMode.ACCUMULATING, Duration.ofSeconds(1), Triggers.repeat(Triggers.any(Triggers.count(2), Triggers.timeSinceFirstMessage(Duration.ofMillis(500)))));
List<WindowPane<Integer, Collection<IntegerEnvelope>>> windowPanes = new ArrayList<>();
MessageCollector messageCollector = envelope -> windowPanes.add((WindowPane<Integer, Collection<IntegerEnvelope>>) envelope.getMessage());
TestClock testClock = new TestClock();
StreamOperatorTask task = new StreamOperatorTask(sgb, runner, testClock);
task.init(config, taskContext);
task.process(new IntegerEnvelope(1), messageCollector, taskCoordinator);
task.process(new IntegerEnvelope(1), messageCollector, taskCoordinator);
//assert that the count trigger fired
Assert.assertEquals(windowPanes.size(), 1);
//advance the timer to enable the potential triggering of the inner timeSinceFirstMessage trigger
task.process(new IntegerEnvelope(1), messageCollector, taskCoordinator);
testClock.advanceTime(Duration.ofMillis(500));
//assert that the triggering of the count trigger cancelled the inner timeSinceFirstMessage trigger
task.window(messageCollector, taskCoordinator);
Assert.assertEquals(windowPanes.size(), 2);
task.process(new IntegerEnvelope(1), messageCollector, taskCoordinator);
task.process(new IntegerEnvelope(1), messageCollector, taskCoordinator);
Assert.assertEquals(windowPanes.size(), 3);
task.process(new IntegerEnvelope(1), messageCollector, taskCoordinator);
//advance timer by 500 more millis to enable the default trigger
testClock.advanceTime(Duration.ofMillis(500));
task.window(messageCollector, taskCoordinator);
//assert that the default trigger fired
Assert.assertEquals(windowPanes.size(), 4);
}
use of org.apache.samza.task.TaskContext in project samza by apache.
the class TestOperatorImpl method testOnTimerPropagatesResultsAndTimer.
@Test
public void testOnTimerPropagatesResultsAndTimer() {
TaskContext mockTaskContext = mock(TaskContext.class);
when(mockTaskContext.getMetricsRegistry()).thenReturn(new MetricsRegistryMap());
Object mockTestOpImplOutput = mock(Object.class);
OperatorImpl<Object, Object> opImpl = new TestOpImpl(mockTestOpImplOutput);
opImpl.init(mock(Config.class), mockTaskContext);
// register a couple of operators
OperatorImpl mockNextOpImpl1 = mock(OperatorImpl.class);
when(mockNextOpImpl1.getOperatorSpec()).thenReturn(new TestOpSpec());
when(mockNextOpImpl1.handleMessage(anyObject(), anyObject(), anyObject())).thenReturn(Collections.emptyList());
mockNextOpImpl1.init(mock(Config.class), mockTaskContext);
opImpl.registerNextOperator(mockNextOpImpl1);
OperatorImpl mockNextOpImpl2 = mock(OperatorImpl.class);
when(mockNextOpImpl2.getOperatorSpec()).thenReturn(new TestOpSpec());
when(mockNextOpImpl2.handleMessage(anyObject(), anyObject(), anyObject())).thenReturn(Collections.emptyList());
mockNextOpImpl2.init(mock(Config.class), mockTaskContext);
opImpl.registerNextOperator(mockNextOpImpl2);
// send a timer tick to this operator
MessageCollector mockCollector = mock(MessageCollector.class);
TaskCoordinator mockCoordinator = mock(TaskCoordinator.class);
opImpl.onTimer(mockCollector, mockCoordinator);
// verify that it propagates its handleTimer results to next operators
verify(mockNextOpImpl1, times(1)).handleMessage(mockTestOpImplOutput, mockCollector, mockCoordinator);
verify(mockNextOpImpl2, times(1)).handleMessage(mockTestOpImplOutput, mockCollector, mockCoordinator);
// verify that it propagates the timer tick to next operators
verify(mockNextOpImpl1, times(1)).handleTimer(mockCollector, mockCoordinator);
verify(mockNextOpImpl2, times(1)).handleTimer(mockCollector, mockCoordinator);
}
use of org.apache.samza.task.TaskContext in project samza by apache.
the class TestWindowOperator method testSessionWindowsAccumulatingMode.
@Test
public void testSessionWindowsAccumulatingMode() throws Exception {
StreamApplication sgb = new KeyedSessionWindowStreamApplication(AccumulationMode.DISCARDING, Duration.ofMillis(500));
TestClock testClock = new TestClock();
StreamOperatorTask task = new StreamOperatorTask(sgb, runner, testClock);
List<WindowPane<Integer, Collection<IntegerEnvelope>>> windowPanes = new ArrayList<>();
MessageCollector messageCollector = envelope -> windowPanes.add((WindowPane<Integer, Collection<IntegerEnvelope>>) envelope.getMessage());
task.init(config, taskContext);
task.process(new IntegerEnvelope(1), messageCollector, taskCoordinator);
task.process(new IntegerEnvelope(1), messageCollector, taskCoordinator);
testClock.advanceTime(Duration.ofSeconds(1));
task.process(new IntegerEnvelope(2), messageCollector, taskCoordinator);
task.process(new IntegerEnvelope(2), messageCollector, taskCoordinator);
task.process(new IntegerEnvelope(2), messageCollector, taskCoordinator);
task.process(new IntegerEnvelope(2), messageCollector, taskCoordinator);
testClock.advanceTime(Duration.ofSeconds(1));
task.window(messageCollector, taskCoordinator);
Assert.assertEquals(windowPanes.size(), 2);
Assert.assertEquals((windowPanes.get(0).getMessage()).size(), 2);
Assert.assertEquals(windowPanes.get(0).getKey().getKey(), new Integer(1));
Assert.assertEquals(windowPanes.get(1).getKey().getKey(), new Integer(2));
Assert.assertEquals((windowPanes.get(0).getMessage()).size(), 2);
Assert.assertEquals((windowPanes.get(1).getMessage()).size(), 4);
}
use of org.apache.samza.task.TaskContext in project samza by apache.
the class TestOperatorImpl method testOnMessagePropagatesResults.
@Test
public void testOnMessagePropagatesResults() {
TaskContext mockTaskContext = mock(TaskContext.class);
when(mockTaskContext.getMetricsRegistry()).thenReturn(new MetricsRegistryMap());
Object mockTestOpImplOutput = mock(Object.class);
OperatorImpl<Object, Object> opImpl = new TestOpImpl(mockTestOpImplOutput);
opImpl.init(mock(Config.class), mockTaskContext);
// register a couple of operators
OperatorImpl mockNextOpImpl1 = mock(OperatorImpl.class);
when(mockNextOpImpl1.getOperatorSpec()).thenReturn(new TestOpSpec());
when(mockNextOpImpl1.handleMessage(anyObject(), anyObject(), anyObject())).thenReturn(Collections.emptyList());
mockNextOpImpl1.init(mock(Config.class), mockTaskContext);
opImpl.registerNextOperator(mockNextOpImpl1);
OperatorImpl mockNextOpImpl2 = mock(OperatorImpl.class);
when(mockNextOpImpl2.getOperatorSpec()).thenReturn(new TestOpSpec());
when(mockNextOpImpl2.handleMessage(anyObject(), anyObject(), anyObject())).thenReturn(Collections.emptyList());
mockNextOpImpl2.init(mock(Config.class), mockTaskContext);
opImpl.registerNextOperator(mockNextOpImpl2);
// send a message to this operator
MessageCollector mockCollector = mock(MessageCollector.class);
TaskCoordinator mockCoordinator = mock(TaskCoordinator.class);
opImpl.onMessage(mock(Object.class), mockCollector, mockCoordinator);
// verify that it propagates its handleMessage results to next operators
verify(mockNextOpImpl1, times(1)).handleMessage(mockTestOpImplOutput, mockCollector, mockCoordinator);
verify(mockNextOpImpl2, times(1)).handleMessage(mockTestOpImplOutput, mockCollector, mockCoordinator);
}
Aggregations