use of org.apache.samza.task.TaskCoordinator in project samza by apache.
the class TestOperatorImpl method testOnMessageUpdatesMetrics.
@Test
public void testOnMessageUpdatesMetrics() {
TaskContext mockTaskContext = mock(TaskContext.class);
MetricsRegistry mockMetricsRegistry = mock(MetricsRegistry.class);
when(mockTaskContext.getMetricsRegistry()).thenReturn(mockMetricsRegistry);
Counter mockCounter = mock(Counter.class);
Timer mockTimer = mock(Timer.class);
when(mockMetricsRegistry.newCounter(anyString(), anyString())).thenReturn(mockCounter);
when(mockMetricsRegistry.newTimer(anyString(), anyString())).thenReturn(mockTimer);
Object mockTestOpImplOutput = mock(Object.class);
OperatorImpl<Object, Object> opImpl = new TestOpImpl(mockTestOpImplOutput);
opImpl.init(mock(Config.class), mockTaskContext);
// 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 updates message count and timer metrics
verify(mockCounter, times(1)).inc();
verify(mockTimer, times(1)).update(anyLong());
}
use of org.apache.samza.task.TaskCoordinator in project samza by apache.
the class TestOperatorSpecs method testCreateSinkOperator.
@Test
public void testCreateSinkOperator() {
SystemStream testStream = new SystemStream("test-sys", "test-stream");
SinkFunction<TestMessageEnvelope> sinkFn = (TestMessageEnvelope message, MessageCollector messageCollector, TaskCoordinator taskCoordinator) -> {
messageCollector.send(new OutgoingMessageEnvelope(testStream, message.getKey(), message.getMessage()));
};
SinkOperatorSpec<TestMessageEnvelope> sinkOp = OperatorSpecs.createSinkOperatorSpec(sinkFn, 1);
assertEquals(sinkOp.getSinkFn(), sinkFn);
TestMessageEnvelope mockInput = mock(TestMessageEnvelope.class);
when(mockInput.getKey()).thenReturn("my-test-msg-key");
MessageType mockMsgBody = mock(MessageType.class);
when(mockInput.getMessage()).thenReturn(mockMsgBody);
final List<OutgoingMessageEnvelope> outputMsgs = new ArrayList<>();
MessageCollector mockCollector = mock(MessageCollector.class);
doAnswer(invocation -> {
outputMsgs.add((OutgoingMessageEnvelope) invocation.getArguments()[0]);
return null;
}).when(mockCollector).send(any());
sinkOp.getSinkFn().apply(mockInput, mockCollector, null);
assertEquals(1, outputMsgs.size());
assertEquals(outputMsgs.get(0).getKey(), "my-test-msg-key");
assertEquals(outputMsgs.get(0).getMessage(), mockMsgBody);
assertEquals(sinkOp.getOpCode(), OperatorSpec.OpCode.SINK);
assertEquals(sinkOp.getNextStream(), null);
}
use of org.apache.samza.task.TaskCoordinator in project samza by apache.
the class TestWindowOperator method testTumblingWindowsDiscardingMode.
@Test
public void testTumblingWindowsDiscardingMode() throws Exception {
StreamApplication sgb = new KeyedTumblingWindowStreamApplication(AccumulationMode.DISCARDING, Duration.ofSeconds(1), Triggers.repeat(Triggers.count(2)));
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());
integers.forEach(n -> task.process(new IntegerEnvelope(n), messageCollector, taskCoordinator));
testClock.advanceTime(Duration.ofSeconds(1));
task.window(messageCollector, taskCoordinator);
Assert.assertEquals(windowPanes.size(), 5);
Assert.assertEquals(windowPanes.get(0).getKey().getKey(), new Integer(1));
Assert.assertEquals((windowPanes.get(0).getMessage()).size(), 2);
Assert.assertEquals(windowPanes.get(1).getKey().getKey(), new Integer(2));
Assert.assertEquals((windowPanes.get(1).getMessage()).size(), 2);
Assert.assertEquals(windowPanes.get(2).getKey().getKey(), new Integer(1));
Assert.assertEquals((windowPanes.get(2).getMessage()).size(), 2);
Assert.assertEquals(windowPanes.get(3).getKey().getKey(), new Integer(2));
Assert.assertEquals((windowPanes.get(3).getMessage()).size(), 2);
Assert.assertEquals(windowPanes.get(4).getKey().getKey(), new Integer(3));
Assert.assertEquals((windowPanes.get(4).getMessage()).size(), 1);
}
use of org.apache.samza.task.TaskCoordinator in project samza by apache.
the class TestWindowOperator method testSessionWindowsDiscardingMode.
@Test
public void testSessionWindowsDiscardingMode() throws Exception {
StreamApplication sgb = new KeyedSessionWindowStreamApplication(AccumulationMode.DISCARDING, Duration.ofMillis(500));
TestClock testClock = new TestClock();
List<WindowPane<Integer, Collection<IntegerEnvelope>>> windowPanes = new ArrayList<>();
StreamOperatorTask task = new StreamOperatorTask(sgb, runner, testClock);
task.init(config, taskContext);
MessageCollector messageCollector = envelope -> windowPanes.add((WindowPane<Integer, Collection<IntegerEnvelope>>) envelope.getMessage());
task.process(new IntegerEnvelope(1), messageCollector, taskCoordinator);
task.process(new IntegerEnvelope(1), messageCollector, taskCoordinator);
testClock.advanceTime(Duration.ofSeconds(1));
task.window(messageCollector, taskCoordinator);
Assert.assertEquals(windowPanes.size(), 1);
Assert.assertEquals(windowPanes.get(0).getKey().getPaneId(), "1");
Assert.assertEquals(windowPanes.get(0).getKey().getKey(), new Integer(1));
task.process(new IntegerEnvelope(2), messageCollector, taskCoordinator);
task.process(new IntegerEnvelope(2), messageCollector, taskCoordinator);
task.process(new IntegerEnvelope(3), messageCollector, taskCoordinator);
task.process(new IntegerEnvelope(3), messageCollector, taskCoordinator);
testClock.advanceTime(Duration.ofSeconds(1));
task.window(messageCollector, taskCoordinator);
Assert.assertEquals(windowPanes.size(), 3);
Assert.assertEquals(windowPanes.get(0).getKey().getPaneId(), "1");
Assert.assertEquals(windowPanes.get(1).getKey().getPaneId(), "1001");
Assert.assertEquals(windowPanes.get(2).getKey().getPaneId(), "1001");
Assert.assertEquals((windowPanes.get(0).getMessage()).size(), 2);
Assert.assertEquals((windowPanes.get(1).getMessage()).size(), 2);
Assert.assertEquals((windowPanes.get(2).getMessage()).size(), 2);
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(), 4);
Assert.assertEquals(windowPanes.get(3).getKey().getKey(), new Integer(2));
Assert.assertEquals(windowPanes.get(3).getKey().getPaneId(), "2001");
Assert.assertEquals((windowPanes.get(3).getMessage()).size(), 2);
}
use of org.apache.samza.task.TaskCoordinator 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);
}
Aggregations