Search in sources :

Example 11 with TestMessageEnvelope

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);
}
Also used : TestMessageEnvelope(org.apache.samza.operators.data.TestMessageEnvelope) SystemStream(org.apache.samza.system.SystemStream) MessageCollector(org.apache.samza.task.MessageCollector) ArrayList(java.util.ArrayList) TaskCoordinator(org.apache.samza.task.TaskCoordinator) OutgoingMessageEnvelope(org.apache.samza.system.OutgoingMessageEnvelope) MessageType(org.apache.samza.operators.data.MessageType) Test(org.junit.Test)

Example 12 with TestMessageEnvelope

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);
}
Also used : StreamOperatorSpec(org.apache.samza.operators.spec.StreamOperatorSpec) Assert.assertNotSame(org.junit.Assert.assertNotSame) ArrayList(java.util.ArrayList) OperatorSpec(org.apache.samza.operators.spec.OperatorSpec) PartialJoinOperatorSpec(org.apache.samza.operators.spec.PartialJoinOperatorSpec) SinkOperatorSpec(org.apache.samza.operators.spec.SinkOperatorSpec) MessageStreamImpl(org.apache.samza.operators.MessageStreamImpl) Duration(java.time.Duration) TestOutputMessageEnvelope(org.apache.samza.operators.data.TestOutputMessageEnvelope) Method(java.lang.reflect.Method) Before(org.junit.Before) TestMessageEnvelope(org.apache.samza.operators.data.TestMessageEnvelope) WindowType(org.apache.samza.operators.windows.internal.WindowType) TaskContext(org.apache.samza.task.TaskContext) Windows(org.apache.samza.operators.windows.Windows) Iterator(java.util.Iterator) WindowOperatorSpec(org.apache.samza.operators.spec.WindowOperatorSpec) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) JoinFunction(org.apache.samza.operators.functions.JoinFunction) Field(java.lang.reflect.Field) FlatMapFunction(org.apache.samza.operators.functions.FlatMapFunction) StreamGraphImpl(org.apache.samza.operators.StreamGraphImpl) InvocationTargetException(java.lang.reflect.InvocationTargetException) SinkFunction(org.apache.samza.operators.functions.SinkFunction) Config(org.apache.samza.config.Config) TestMessageStreamImplUtil(org.apache.samza.operators.TestMessageStreamImplUtil) WindowInternal(org.apache.samza.operators.windows.internal.WindowInternal) Assert.assertEquals(org.junit.Assert.assertEquals) MetricsRegistryMap(org.apache.samza.metrics.MetricsRegistryMap) Mockito.mock(org.mockito.Mockito.mock) TaskContext(org.apache.samza.task.TaskContext) Set(java.util.Set) Config(org.apache.samza.config.Config) ArrayList(java.util.ArrayList) TestMessageEnvelope(org.apache.samza.operators.data.TestMessageEnvelope) StreamGraphImpl(org.apache.samza.operators.StreamGraphImpl) MetricsRegistryMap(org.apache.samza.metrics.MetricsRegistryMap) Test(org.junit.Test)

Example 13 with TestMessageEnvelope

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);
}
Also used : TestMessageEnvelope(org.apache.samza.operators.data.TestMessageEnvelope) TaskContext(org.apache.samza.task.TaskContext) Config(org.apache.samza.config.Config) MetricsRegistryMap(org.apache.samza.metrics.MetricsRegistryMap) Test(org.junit.Test)

Example 14 with TestMessageEnvelope

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);
}
Also used : StreamOperatorSpec(org.apache.samza.operators.spec.StreamOperatorSpec) Assert.assertNotSame(org.junit.Assert.assertNotSame) ArrayList(java.util.ArrayList) OperatorSpec(org.apache.samza.operators.spec.OperatorSpec) PartialJoinOperatorSpec(org.apache.samza.operators.spec.PartialJoinOperatorSpec) SinkOperatorSpec(org.apache.samza.operators.spec.SinkOperatorSpec) MessageStreamImpl(org.apache.samza.operators.MessageStreamImpl) Duration(java.time.Duration) TestOutputMessageEnvelope(org.apache.samza.operators.data.TestOutputMessageEnvelope) Method(java.lang.reflect.Method) Before(org.junit.Before) TestMessageEnvelope(org.apache.samza.operators.data.TestMessageEnvelope) WindowType(org.apache.samza.operators.windows.internal.WindowType) TaskContext(org.apache.samza.task.TaskContext) Windows(org.apache.samza.operators.windows.Windows) Iterator(java.util.Iterator) WindowOperatorSpec(org.apache.samza.operators.spec.WindowOperatorSpec) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) JoinFunction(org.apache.samza.operators.functions.JoinFunction) Field(java.lang.reflect.Field) FlatMapFunction(org.apache.samza.operators.functions.FlatMapFunction) StreamGraphImpl(org.apache.samza.operators.StreamGraphImpl) InvocationTargetException(java.lang.reflect.InvocationTargetException) SinkFunction(org.apache.samza.operators.functions.SinkFunction) Config(org.apache.samza.config.Config) TestMessageStreamImplUtil(org.apache.samza.operators.TestMessageStreamImplUtil) WindowInternal(org.apache.samza.operators.windows.internal.WindowInternal) Assert.assertEquals(org.junit.Assert.assertEquals) MetricsRegistryMap(org.apache.samza.metrics.MetricsRegistryMap) Mockito.mock(org.mockito.Mockito.mock) TaskContext(org.apache.samza.task.TaskContext) WindowInternal(org.apache.samza.operators.windows.internal.WindowInternal) Config(org.apache.samza.config.Config) WindowOperatorSpec(org.apache.samza.operators.spec.WindowOperatorSpec) Field(java.lang.reflect.Field) TestMessageEnvelope(org.apache.samza.operators.data.TestMessageEnvelope) TestOutputMessageEnvelope(org.apache.samza.operators.data.TestOutputMessageEnvelope) Test(org.junit.Test)

Example 15 with TestMessageEnvelope

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);
}
Also used : StreamOperatorSpec(org.apache.samza.operators.spec.StreamOperatorSpec) Assert.assertNotSame(org.junit.Assert.assertNotSame) ArrayList(java.util.ArrayList) OperatorSpec(org.apache.samza.operators.spec.OperatorSpec) PartialJoinOperatorSpec(org.apache.samza.operators.spec.PartialJoinOperatorSpec) SinkOperatorSpec(org.apache.samza.operators.spec.SinkOperatorSpec) MessageStreamImpl(org.apache.samza.operators.MessageStreamImpl) Duration(java.time.Duration) TestOutputMessageEnvelope(org.apache.samza.operators.data.TestOutputMessageEnvelope) Method(java.lang.reflect.Method) Before(org.junit.Before) TestMessageEnvelope(org.apache.samza.operators.data.TestMessageEnvelope) WindowType(org.apache.samza.operators.windows.internal.WindowType) TaskContext(org.apache.samza.task.TaskContext) Windows(org.apache.samza.operators.windows.Windows) Iterator(java.util.Iterator) WindowOperatorSpec(org.apache.samza.operators.spec.WindowOperatorSpec) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) JoinFunction(org.apache.samza.operators.functions.JoinFunction) Field(java.lang.reflect.Field) FlatMapFunction(org.apache.samza.operators.functions.FlatMapFunction) StreamGraphImpl(org.apache.samza.operators.StreamGraphImpl) InvocationTargetException(java.lang.reflect.InvocationTargetException) SinkFunction(org.apache.samza.operators.functions.SinkFunction) Config(org.apache.samza.config.Config) TestMessageStreamImplUtil(org.apache.samza.operators.TestMessageStreamImplUtil) WindowInternal(org.apache.samza.operators.windows.internal.WindowInternal) Assert.assertEquals(org.junit.Assert.assertEquals) MetricsRegistryMap(org.apache.samza.metrics.MetricsRegistryMap) Mockito.mock(org.mockito.Mockito.mock) TaskContext(org.apache.samza.task.TaskContext) Set(java.util.Set) Config(org.apache.samza.config.Config) TestMessageEnvelope(org.apache.samza.operators.data.TestMessageEnvelope) StreamGraphImpl(org.apache.samza.operators.StreamGraphImpl) MetricsRegistryMap(org.apache.samza.metrics.MetricsRegistryMap) Test(org.junit.Test)

Aggregations

TestMessageEnvelope (org.apache.samza.operators.data.TestMessageEnvelope)24 Test (org.junit.Test)23 TestOutputMessageEnvelope (org.apache.samza.operators.data.TestOutputMessageEnvelope)16 ArrayList (java.util.ArrayList)13 Config (org.apache.samza.config.Config)12 MessageType (org.apache.samza.operators.data.MessageType)12 OperatorSpec (org.apache.samza.operators.spec.OperatorSpec)11 PartialJoinOperatorSpec (org.apache.samza.operators.spec.PartialJoinOperatorSpec)11 SinkOperatorSpec (org.apache.samza.operators.spec.SinkOperatorSpec)11 StreamOperatorSpec (org.apache.samza.operators.spec.StreamOperatorSpec)11 Assert.assertEquals (org.junit.Assert.assertEquals)11 Mockito.mock (org.mockito.Mockito.mock)11 Mockito.when (org.mockito.Mockito.when)11 StreamGraphImpl (org.apache.samza.operators.StreamGraphImpl)9 TestInputMessageEnvelope (org.apache.samza.operators.data.TestInputMessageEnvelope)9 FlatMapFunction (org.apache.samza.operators.functions.FlatMapFunction)9 TaskContext (org.apache.samza.task.TaskContext)9 Function (java.util.function.Function)8 SinkFunction (org.apache.samza.operators.functions.SinkFunction)8 List (java.util.List)7