use of org.apache.samza.task.MessageCollector in project samza by apache.
the class TestWindowOperator method testNonKeyedTumblingWindowsDiscardingMode.
@Test
public void testNonKeyedTumblingWindowsDiscardingMode() throws Exception {
StreamApplication sgb = new TumblingWindowStreamApplication(AccumulationMode.DISCARDING, Duration.ofSeconds(1), Triggers.repeat(Triggers.count(1000)));
List<WindowPane<Integer, Collection<IntegerEnvelope>>> windowPanes = new ArrayList<>();
TestClock testClock = new TestClock();
StreamOperatorTask task = new StreamOperatorTask(sgb, runner, testClock);
task.init(config, taskContext);
MessageCollector messageCollector = envelope -> windowPanes.add((WindowPane<Integer, Collection<IntegerEnvelope>>) envelope.getMessage());
Assert.assertEquals(windowPanes.size(), 0);
integers.forEach(n -> task.process(new IntegerEnvelope(n), messageCollector, taskCoordinator));
Assert.assertEquals(windowPanes.size(), 0);
testClock.advanceTime(Duration.ofSeconds(1));
Assert.assertEquals(windowPanes.size(), 0);
task.window(messageCollector, taskCoordinator);
Assert.assertEquals(windowPanes.size(), 1);
Assert.assertEquals((windowPanes.get(0).getMessage()).size(), 9);
}
use of org.apache.samza.task.MessageCollector in project samza by apache.
the class TestSinkOperatorImpl method testSinkOperatorSinkFunction.
@Test
public void testSinkOperatorSinkFunction() {
SinkFunction<TestOutputMessageEnvelope> sinkFn = mock(SinkFunction.class);
SinkOperatorImpl<TestOutputMessageEnvelope> sinkImpl = createSinkOperator(sinkFn);
TestOutputMessageEnvelope mockMsg = mock(TestOutputMessageEnvelope.class);
MessageCollector mockCollector = mock(MessageCollector.class);
TaskCoordinator mockCoordinator = mock(TaskCoordinator.class);
sinkImpl.handleMessage(mockMsg, mockCollector, mockCoordinator);
verify(sinkFn, times(1)).apply(mockMsg, mockCollector, mockCoordinator);
}
use of org.apache.samza.task.MessageCollector in project samza by apache.
the class TestSinkOperatorImpl method testSinkOperatorClose.
@Test
public void testSinkOperatorClose() {
TestOutputMessageEnvelope mockMsg = mock(TestOutputMessageEnvelope.class);
MessageCollector mockCollector = mock(MessageCollector.class);
TaskCoordinator mockCoordinator = mock(TaskCoordinator.class);
SinkFunction<TestOutputMessageEnvelope> sinkFn = mock(SinkFunction.class);
SinkOperatorImpl<TestOutputMessageEnvelope> sinkImpl = createSinkOperator(sinkFn);
sinkImpl.handleMessage(mockMsg, mockCollector, mockCoordinator);
verify(sinkFn, times(1)).apply(mockMsg, mockCollector, mockCoordinator);
// ensure that close is not called yet
verify(sinkFn, times(0)).close();
sinkImpl.handleClose();
// ensure that close is called once from handleClose()
verify(sinkFn, times(1)).close();
}
use of org.apache.samza.task.MessageCollector 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.MessageCollector 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);
}
Aggregations