use of org.apache.samza.operators.data.TestOutputMessageEnvelope in project samza by apache.
the class TestOperatorImpls method testJoinChain.
@Test
public void testJoinChain() throws IllegalAccessException, InvocationTargetException {
// test creation of join chain
StreamGraphImpl mockGraph = mock(StreamGraphImpl.class);
MessageStreamImpl<TestMessageEnvelope> input1 = TestMessageStreamImplUtil.getMessageStreamImpl(mockGraph);
MessageStreamImpl<TestMessageEnvelope> input2 = TestMessageStreamImplUtil.getMessageStreamImpl(mockGraph);
TaskContext mockContext = mock(TaskContext.class);
when(mockContext.getMetricsRegistry()).thenReturn(new MetricsRegistryMap());
Config mockConfig = mock(Config.class);
input1.join(input2, new JoinFunction<String, TestMessageEnvelope, TestMessageEnvelope, TestOutputMessageEnvelope>() {
@Override
public TestOutputMessageEnvelope apply(TestMessageEnvelope m1, TestMessageEnvelope m2) {
return new TestOutputMessageEnvelope(m1.getKey(), m1.getMessage().getValue().length() + m2.getMessage().getValue().length());
}
@Override
public String getFirstKey(TestMessageEnvelope message) {
return message.getKey();
}
@Override
public String getSecondKey(TestMessageEnvelope message) {
return message.getKey();
}
}, Duration.ofMinutes(1)).map(m -> m);
OperatorImplGraph opGraph = new OperatorImplGraph();
// now, we create chained operators from each input sources
RootOperatorImpl chain1 = (RootOperatorImpl) createOpsMethod.invoke(opGraph, input1, mockConfig, mockContext);
RootOperatorImpl chain2 = (RootOperatorImpl) createOpsMethod.invoke(opGraph, input2, mockConfig, mockContext);
// check that those two chains will merge at map operator
// first branch of the join
Set<OperatorImpl> subsSet = (Set<OperatorImpl>) nextOperatorsField.get(chain1);
assertEquals(subsSet.size(), 1);
OperatorImpl<TestMessageEnvelope, TestOutputMessageEnvelope> joinOp1 = subsSet.iterator().next();
Set<OperatorImpl> subsOps = (Set<OperatorImpl>) nextOperatorsField.get(joinOp1);
assertEquals(subsOps.size(), 1);
// the map operator consumes the common join output, where two branches merge
OperatorImpl mapImpl = subsOps.iterator().next();
// second branch of the join
subsSet = (Set<OperatorImpl>) nextOperatorsField.get(chain2);
assertEquals(subsSet.size(), 1);
OperatorImpl<TestMessageEnvelope, TestOutputMessageEnvelope> joinOp2 = subsSet.iterator().next();
assertNotSame(joinOp1, joinOp2);
subsOps = (Set<OperatorImpl>) nextOperatorsField.get(joinOp2);
assertEquals(subsOps.size(), 1);
// make sure that the map operator is the same
assertEquals(mapImpl, subsOps.iterator().next());
}
use of org.apache.samza.operators.data.TestOutputMessageEnvelope in project samza by apache.
the class TestSinkOperatorImpl method createSinkOperator.
private SinkOperatorImpl createSinkOperator(SinkFunction<TestOutputMessageEnvelope> sinkFn) {
SinkOperatorSpec<TestOutputMessageEnvelope> sinkOp = mock(SinkOperatorSpec.class);
when(sinkOp.getSinkFn()).thenReturn(sinkFn);
Config mockConfig = mock(Config.class);
TaskContext mockContext = mock(TaskContext.class);
return new SinkOperatorImpl<>(sinkOp, mockConfig, mockContext);
}
use of org.apache.samza.operators.data.TestOutputMessageEnvelope in project samza by apache.
the class TestStreamOperatorImpl method testSimpleOperator.
@Test
@SuppressWarnings("unchecked")
public void testSimpleOperator() {
StreamOperatorSpec<TestMessageEnvelope, TestOutputMessageEnvelope> mockOp = mock(StreamOperatorSpec.class);
FlatMapFunction<TestMessageEnvelope, TestOutputMessageEnvelope> txfmFn = mock(FlatMapFunction.class);
when(mockOp.getTransformFn()).thenReturn(txfmFn);
Config mockConfig = mock(Config.class);
TaskContext mockContext = mock(TaskContext.class);
StreamOperatorImpl<TestMessageEnvelope, TestOutputMessageEnvelope> opImpl = spy(new StreamOperatorImpl<>(mockOp, mockConfig, mockContext));
TestMessageEnvelope inMsg = mock(TestMessageEnvelope.class);
TestOutputMessageEnvelope outMsg = mock(TestOutputMessageEnvelope.class);
Collection<TestOutputMessageEnvelope> mockOutputs = new ArrayList() {
{
this.add(outMsg);
}
};
when(txfmFn.apply(inMsg)).thenReturn(mockOutputs);
MessageCollector mockCollector = mock(MessageCollector.class);
TaskCoordinator mockCoordinator = mock(TaskCoordinator.class);
Collection<TestOutputMessageEnvelope> results = opImpl.handleMessage(inMsg, mockCollector, mockCoordinator);
verify(txfmFn, times(1)).apply(inMsg);
assertEquals(results, mockOutputs);
}
use of org.apache.samza.operators.data.TestOutputMessageEnvelope in project samza by apache.
the class TestMessageStreamImpl method testJoin.
@Test
public void testJoin() {
StreamApplicationDescriptorImpl mockGraph = mock(StreamApplicationDescriptorImpl.class);
OperatorSpec leftInputOpSpec = mock(OperatorSpec.class);
MessageStreamImpl<TestMessageEnvelope> source1 = new MessageStreamImpl<>(mockGraph, leftInputOpSpec);
OperatorSpec rightInputOpSpec = mock(OperatorSpec.class);
MessageStreamImpl<TestMessageEnvelope> source2 = new MessageStreamImpl<>(mockGraph, rightInputOpSpec);
JoinFunction<String, TestMessageEnvelope, TestMessageEnvelope, TestOutputMessageEnvelope> mockJoinFn = mock(JoinFunction.class);
Duration joinTtl = Duration.ofMinutes(1);
source1.join(source2, mockJoinFn, mock(Serde.class), mock(Serde.class), mock(Serde.class), joinTtl, "j1");
ArgumentCaptor<OperatorSpec> leftRegisteredOpCaptor = ArgumentCaptor.forClass(OperatorSpec.class);
verify(leftInputOpSpec).registerNextOperatorSpec(leftRegisteredOpCaptor.capture());
OperatorSpec<?, TestMessageEnvelope> leftRegisteredOpSpec = leftRegisteredOpCaptor.getValue();
ArgumentCaptor<OperatorSpec> rightRegisteredOpCaptor = ArgumentCaptor.forClass(OperatorSpec.class);
verify(rightInputOpSpec).registerNextOperatorSpec(rightRegisteredOpCaptor.capture());
OperatorSpec<?, TestMessageEnvelope> rightRegisteredOpSpec = rightRegisteredOpCaptor.getValue();
assertEquals(leftRegisteredOpSpec, rightRegisteredOpSpec);
assertEquals(OpCode.JOIN, leftRegisteredOpSpec.getOpCode());
assertTrue(leftRegisteredOpSpec instanceof JoinOperatorSpec);
assertEquals(mockJoinFn, ((JoinOperatorSpec) leftRegisteredOpSpec).getJoinFn());
assertEquals(joinTtl.toMillis(), ((JoinOperatorSpec) leftRegisteredOpSpec).getTtlMs());
assertEquals(leftInputOpSpec, ((JoinOperatorSpec) leftRegisteredOpSpec).getLeftInputOpSpec());
assertEquals(rightInputOpSpec, ((JoinOperatorSpec) leftRegisteredOpSpec).getRightInputOpSpec());
}
use of org.apache.samza.operators.data.TestOutputMessageEnvelope in project samza by apache.
the class TestOperatorSpec method testJoinOperatorSpec.
@Test
public void testJoinOperatorSpec() {
InputOperatorSpec leftOpSpec = new InputOperatorSpec("test-input-1", new NoOpSerde<>(), new NoOpSerde<>(), null, false, "op0");
InputOperatorSpec rightOpSpec = new InputOperatorSpec("test-input-2", new NoOpSerde<>(), new NoOpSerde<>(), null, false, "op1");
Serde<Object> objSerde = new Serde<Object>() {
@Override
public Object fromBytes(byte[] bytes) {
return null;
}
@Override
public byte[] toBytes(Object object) {
return new byte[0];
}
};
JoinFunction<String, Object, Object, TestOutputMessageEnvelope> joinFn = new TestJoinFunction();
JoinOperatorSpec<String, Object, Object, TestOutputMessageEnvelope> joinOperatorSpec = new JoinOperatorSpec<>(leftOpSpec, rightOpSpec, joinFn, new StringSerde("UTF-8"), objSerde, objSerde, 50000, "op2");
JoinOperatorSpec<String, Object, Object, TestOutputMessageEnvelope> joinOpCopy = (JoinOperatorSpec<String, Object, Object, TestOutputMessageEnvelope>) OperatorSpecTestUtils.copyOpSpec(joinOperatorSpec);
assertNotEquals("Expected deserialized copy of operator spec should not be the same as the original operator spec", joinOperatorSpec, joinOpCopy);
assertTrue(joinOperatorSpec.isClone(joinOpCopy));
assertTrue(joinOpCopy.getLeftInputOpSpec().isClone(leftOpSpec));
assertTrue(joinOpCopy.getRightInputOpSpec().isClone(rightOpSpec));
}
Aggregations