use of org.apache.samza.application.descriptors.StreamApplicationDescriptorImpl in project samza by apache.
the class TestMessageStreamImpl method testFlatMapWithRelaxedTypes.
@Test
public void testFlatMapWithRelaxedTypes() {
StreamApplicationDescriptorImpl mockGraph = mock(StreamApplicationDescriptorImpl.class);
OperatorSpec mockOpSpec = mock(OperatorSpec.class);
MessageStreamImpl<TestInputMessageEnvelope> inputStream = new MessageStreamImpl<>(mockGraph, mockOpSpec);
FlatMapFunction flatMapFunction = (FlatMapFunction<TestMessageEnvelope, TestOutputMessageEnvelope>) message -> Collections.emptyList();
// should compile since TestInputMessageEnvelope extends TestMessageEnvelope
inputStream.flatMap(flatMapFunction);
ArgumentCaptor<OperatorSpec> registeredOpCaptor = ArgumentCaptor.forClass(OperatorSpec.class);
verify(mockOpSpec).registerNextOperatorSpec(registeredOpCaptor.capture());
OperatorSpec<?, TestMessageEnvelope> registeredOpSpec = registeredOpCaptor.getValue();
assertTrue(registeredOpSpec instanceof StreamOperatorSpec);
assertNotNull(((StreamOperatorSpec) registeredOpSpec).getTransformFn());
assertEquals(OpCode.FLAT_MAP, registeredOpSpec.getOpCode());
}
use of org.apache.samza.application.descriptors.StreamApplicationDescriptorImpl in project samza by apache.
the class TestMessageStreamImpl method testMergeWithRelaxedTypes.
@Test
public void testMergeWithRelaxedTypes() {
StreamApplicationDescriptorImpl mockGraph = mock(StreamApplicationDescriptorImpl.class);
MessageStream<TestMessageEnvelope> inputStream = new MessageStreamImpl<>(mockGraph, mock(OperatorSpec.class));
// other streams have the same message type T as input stream message type M
Collection<MessageStream<TestMessageEnvelope>> otherStreams1 = ImmutableList.of(new MessageStreamImpl<>(mockGraph, mock(OperatorSpec.class)), new MessageStreamImpl<>(mockGraph, mock(OperatorSpec.class)));
// other streams have the same message type T that extends as input stream message type M
Collection<MessageStream<TestInputMessageEnvelope>> otherStreams2 = ImmutableList.of(new MessageStreamImpl<TestInputMessageEnvelope>(mockGraph, mock(OperatorSpec.class)), new MessageStreamImpl<TestInputMessageEnvelope>(mockGraph, mock(OperatorSpec.class)));
// other streams have a mix of message types such that T extends input stream message type M
Collection<MessageStream<TestMessageEnvelope>> otherStreams3 = ImmutableList.of(new MessageStreamImpl<TestMessageEnvelope>(mockGraph, mock(OperatorSpec.class)), // unchecked cast required for the next stream
(MessageStream) new MessageStreamImpl<TestInputMessageEnvelope>(mockGraph, mock(OperatorSpec.class)));
// not supported:
// other streams have a mix of message types such that T extends input stream message type M
Collection<MessageStream<? extends TestMessageEnvelope>> otherStreams4 = ImmutableList.of(new MessageStreamImpl<TestMessageEnvelope>(mockGraph, mock(OperatorSpec.class)), new MessageStreamImpl<TestInputMessageEnvelope>(mockGraph, mock(OperatorSpec.class)));
// check if all type combinations compile
inputStream.merge(otherStreams1);
inputStream.merge(otherStreams2);
inputStream.merge(otherStreams3);
inputStream.merge(otherStreams4);
}
use of org.apache.samza.application.descriptors.StreamApplicationDescriptorImpl in project samza by apache.
the class TestMessageStreamImpl method testSendTo.
@Test
public void testSendTo() {
StreamApplicationDescriptorImpl mockGraph = mock(StreamApplicationDescriptorImpl.class);
OperatorSpec mockOpSpec = mock(OperatorSpec.class);
MessageStreamImpl<TestMessageEnvelope> inputStream = new MessageStreamImpl<>(mockGraph, mockOpSpec);
OutputStreamImpl<TestMessageEnvelope> mockOutputStreamImpl = mock(OutputStreamImpl.class);
inputStream.sendTo(mockOutputStreamImpl);
ArgumentCaptor<OperatorSpec> registeredOpCaptor = ArgumentCaptor.forClass(OperatorSpec.class);
verify(mockOpSpec).registerNextOperatorSpec(registeredOpCaptor.capture());
OperatorSpec<?, TestMessageEnvelope> registeredOpSpec = registeredOpCaptor.getValue();
assertTrue(registeredOpSpec instanceof OutputOperatorSpec);
assertEquals(OpCode.SEND_TO, registeredOpSpec.getOpCode());
assertEquals(mockOutputStreamImpl, ((OutputOperatorSpec) registeredOpSpec).getOutputStream());
// same behavior as above so nothing new to assert. but ensures that this variant compiles.
MessageStreamImpl<KV<String, TestMessageEnvelope>> keyedInputStream = new MessageStreamImpl<>(mockGraph, mockOpSpec);
OutputStreamImpl<KV<String, TestMessageEnvelope>> mockKeyedOutputStreamImpl = mock(OutputStreamImpl.class);
keyedInputStream.sendTo(mockKeyedOutputStreamImpl);
// can't unit test it, but the following variants should not compile
// inputStream.sendTo(mockKeyedOutputStreamImpl);
// keyedInputStream.sendTo(mockOutputStreamImpl);
}
use of org.apache.samza.application.descriptors.StreamApplicationDescriptorImpl in project samza by apache.
the class TestMessageStreamImpl method testPartitionBy.
@Test
public void testPartitionBy() throws IOException {
StreamApplicationDescriptorImpl mockGraph = mock(StreamApplicationDescriptorImpl.class);
OperatorSpec mockOpSpec = mock(OperatorSpec.class);
String mockOpName = "mockName";
when(mockGraph.getNextOpId(anyObject(), anyObject())).thenReturn(mockOpName);
OutputStreamImpl mockOutputStreamImpl = mock(OutputStreamImpl.class);
KVSerde mockKVSerde = mock(KVSerde.class);
IntermediateMessageStreamImpl mockIntermediateStream = mock(IntermediateMessageStreamImpl.class);
when(mockGraph.getIntermediateStream(eq(mockOpName), eq(mockKVSerde), eq(false))).thenReturn(mockIntermediateStream);
when(mockIntermediateStream.getOutputStream()).thenReturn(mockOutputStreamImpl);
when(mockIntermediateStream.isKeyed()).thenReturn(true);
MessageStreamImpl<TestMessageEnvelope> inputStream = new MessageStreamImpl<>(mockGraph, mockOpSpec);
MapFunction mockKeyFunction = mock(MapFunction.class);
MapFunction mockValueFunction = mock(MapFunction.class);
inputStream.partitionBy(mockKeyFunction, mockValueFunction, mockKVSerde, "p1");
ArgumentCaptor<OperatorSpec> registeredOpCaptor = ArgumentCaptor.forClass(OperatorSpec.class);
verify(mockOpSpec).registerNextOperatorSpec(registeredOpCaptor.capture());
OperatorSpec<?, TestMessageEnvelope> registeredOpSpec = registeredOpCaptor.getValue();
assertTrue(registeredOpSpec instanceof PartitionByOperatorSpec);
assertEquals(OpCode.PARTITION_BY, registeredOpSpec.getOpCode());
assertEquals(mockOutputStreamImpl, ((PartitionByOperatorSpec) registeredOpSpec).getOutputStream());
assertEquals(mockKeyFunction, ((PartitionByOperatorSpec) registeredOpSpec).getKeyFunction());
assertEquals(mockValueFunction, ((PartitionByOperatorSpec) registeredOpSpec).getValueFunction());
}
use of org.apache.samza.application.descriptors.StreamApplicationDescriptorImpl in project samza by apache.
the class TestMessageStreamImpl method testWindowWithRelaxedTypes.
@Test
public void testWindowWithRelaxedTypes() throws Exception {
StreamApplicationDescriptorImpl mockGraph = mock(StreamApplicationDescriptorImpl.class);
OperatorSpec mockOpSpec = mock(OperatorSpec.class);
MessageStream<TestInputMessageEnvelope> inputStream = new MessageStreamImpl<>(mockGraph, mockOpSpec);
MapFunction<TestMessageEnvelope, String> keyExtractor = m -> m.getKey();
FoldLeftFunction<TestMessageEnvelope, Integer> aggregator = (m, c) -> c + 1;
SupplierFunction<Integer> initialValue = () -> 0;
// should compile since TestMessageEnvelope (input for functions) is base class of TestInputMessageEnvelope (M)
Window<TestInputMessageEnvelope, String, Integer> window = Windows.keyedTumblingWindow(keyExtractor, Duration.ofHours(1), initialValue, aggregator, null, mock(Serde.class));
MessageStream<WindowPane<String, Integer>> windowedStream = inputStream.window(window, "w1");
ArgumentCaptor<OperatorSpec> registeredOpCaptor = ArgumentCaptor.forClass(OperatorSpec.class);
verify(mockOpSpec).registerNextOperatorSpec(registeredOpCaptor.capture());
OperatorSpec<?, TestMessageEnvelope> registeredOpSpec = registeredOpCaptor.getValue();
assertTrue(registeredOpSpec instanceof WindowOperatorSpec);
assertEquals(OpCode.WINDOW, registeredOpSpec.getOpCode());
assertEquals(window, ((WindowOperatorSpec) registeredOpSpec).getWindow());
}
Aggregations