use of org.apache.samza.operators.data.TestMessageEnvelope 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.operators.data.TestMessageEnvelope in project samza by apache.
the class TestOperatorImpls method testBroadcastChain.
@Test
public void testBroadcastChain() throws IllegalAccessException, InvocationTargetException {
// test creation of broadcast 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.filter(m -> m.getMessage().getEventTime() > 123456L).flatMap(m -> new ArrayList() {
{
this.add(m);
this.add(m);
}
});
testInput.filter(m -> m.getMessage().getEventTime() < 123456L).map(m -> m);
OperatorImplGraph opGraph = new OperatorImplGraph();
RootOperatorImpl operatorChain = (RootOperatorImpl) createOpsMethod.invoke(opGraph, testInput, mockConfig, mockContext);
Set<OperatorImpl> subsSet = (Set<OperatorImpl>) nextOperatorsField.get(operatorChain);
assertEquals(subsSet.size(), 2);
Iterator<OperatorImpl> iter = subsSet.iterator();
// check the first branch w/ flatMap
OperatorImpl<TestMessageEnvelope, TestMessageEnvelope> opImpl = iter.next();
Set<OperatorImpl> subsOps = (Set<OperatorImpl>) nextOperatorsField.get(opImpl);
assertEquals(subsOps.size(), 1);
OperatorImpl flatMapImpl = subsOps.iterator().next();
subsOps = (Set<OperatorImpl>) nextOperatorsField.get(flatMapImpl);
assertEquals(subsOps.size(), 0);
// check the second branch w/ map
opImpl = iter.next();
subsOps = (Set<OperatorImpl>) nextOperatorsField.get(opImpl);
assertEquals(subsOps.size(), 1);
OperatorImpl mapImpl = subsOps.iterator().next();
subsOps = (Set<OperatorImpl>) nextOperatorsField.get(mapImpl);
assertEquals(subsOps.size(), 0);
}
use of org.apache.samza.operators.data.TestMessageEnvelope in project samza by apache.
the class TestOperatorImpls method testEmptyChain.
@Test
public void testEmptyChain() throws InvocationTargetException, IllegalAccessException {
// test creation of empty chain
MessageStreamImpl<TestMessageEnvelope> testStream = mock(MessageStreamImpl.class);
TaskContext mockContext = mock(TaskContext.class);
when(mockContext.getMetricsRegistry()).thenReturn(new MetricsRegistryMap());
Config mockConfig = mock(Config.class);
OperatorImplGraph opGraph = new OperatorImplGraph();
RootOperatorImpl operatorChain = (RootOperatorImpl) createOpsMethod.invoke(opGraph, testStream, mockConfig, mockContext);
assertTrue(operatorChain != null);
}
use of org.apache.samza.operators.data.TestMessageEnvelope in project samza by apache.
the class TestOperatorImpls method testCreateOperator.
@Test
public void testCreateOperator() throws NoSuchFieldException, IllegalAccessException, InvocationTargetException {
// get window operator
WindowOperatorSpec mockWnd = mock(WindowOperatorSpec.class);
WindowInternal<TestMessageEnvelope, String, Integer> windowInternal = new WindowInternal<>(null, null, null, null, null, WindowType.TUMBLING);
when(mockWnd.getWindow()).thenReturn(windowInternal);
Config mockConfig = mock(Config.class);
TaskContext mockContext = mock(TaskContext.class);
OperatorImplGraph opGraph = new OperatorImplGraph();
OperatorImpl<TestMessageEnvelope, ?> opImpl = (OperatorImpl<TestMessageEnvelope, ?>) createOpMethod.invoke(opGraph, mockWnd, mockConfig, mockContext);
assertTrue(opImpl instanceof WindowOperatorImpl);
Field wndInternalField = WindowOperatorImpl.class.getDeclaredField("window");
wndInternalField.setAccessible(true);
WindowInternal wndInternal = (WindowInternal) wndInternalField.get(opImpl);
assertEquals(wndInternal, windowInternal);
// get simple operator
StreamOperatorSpec<TestMessageEnvelope, TestOutputMessageEnvelope> mockSimpleOp = mock(StreamOperatorSpec.class);
FlatMapFunction<TestMessageEnvelope, TestOutputMessageEnvelope> mockTxfmFn = mock(FlatMapFunction.class);
when(mockSimpleOp.getTransformFn()).thenReturn(mockTxfmFn);
opImpl = (OperatorImpl<TestMessageEnvelope, ?>) createOpMethod.invoke(opGraph, mockSimpleOp, mockConfig, mockContext);
assertTrue(opImpl instanceof StreamOperatorImpl);
Field txfmFnField = StreamOperatorImpl.class.getDeclaredField("transformFn");
txfmFnField.setAccessible(true);
assertEquals(mockTxfmFn, txfmFnField.get(opImpl));
// get sink operator
SinkFunction<TestMessageEnvelope> sinkFn = (m, mc, tc) -> {
};
SinkOperatorSpec<TestMessageEnvelope> sinkOp = mock(SinkOperatorSpec.class);
when(sinkOp.getSinkFn()).thenReturn(sinkFn);
opImpl = (OperatorImpl<TestMessageEnvelope, ?>) createOpMethod.invoke(opGraph, sinkOp, mockConfig, mockContext);
assertTrue(opImpl instanceof SinkOperatorImpl);
Field sinkFnField = SinkOperatorImpl.class.getDeclaredField("sinkFn");
sinkFnField.setAccessible(true);
assertEquals(sinkFn, sinkFnField.get(opImpl));
// get join operator
PartialJoinOperatorSpec<String, TestMessageEnvelope, TestMessageEnvelope, TestOutputMessageEnvelope> joinOp = mock(PartialJoinOperatorSpec.class);
opImpl = (OperatorImpl<TestMessageEnvelope, ?>) createOpMethod.invoke(opGraph, joinOp, mockConfig, mockContext);
assertTrue(opImpl instanceof PartialJoinOperatorImpl);
}
use of org.apache.samza.operators.data.TestMessageEnvelope 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);
}
Aggregations