Search in sources :

Example 1 with TestClock

use of org.apache.samza.testUtils.TestClock in project samza by apache.

the class TestJoinOperator method joinRemovesExpiredMessagesReverse.

@Test
public void joinRemovesExpiredMessagesReverse() throws Exception {
    TestClock testClock = new TestClock();
    StreamOperatorTask sot = createStreamOperatorTask(testClock, new TestJoinStreamApplication(new TestJoinFunction()));
    List<Integer> output = new ArrayList<>();
    MessageCollector messageCollector = envelope -> output.add((Integer) envelope.getMessage());
    // push messages to second stream
    numbers.forEach(n -> sot.process(new SecondStreamIME(n, n), messageCollector, taskCoordinator));
    // 1 minute after ttl
    testClock.advanceTime(JOIN_TTL.plus(Duration.ofMinutes(1)));
    // should expire second stream messages
    sot.window(messageCollector, taskCoordinator);
    // push messages to first stream with same key
    numbers.forEach(n -> sot.process(new FirstStreamIME(n, n), messageCollector, taskCoordinator));
    assertTrue(output.isEmpty());
}
Also used : TestClock(org.apache.samza.testUtils.TestClock) StreamOperatorTask(org.apache.samza.task.StreamOperatorTask) SystemStreamPartition(org.apache.samza.system.SystemStreamPartition) ArrayList(java.util.ArrayList) MessageCollector(org.apache.samza.task.MessageCollector) SystemStream(org.apache.samza.system.SystemStream) Duration(java.time.Duration) ApplicationRunner(org.apache.samza.runtime.ApplicationRunner) TaskContext(org.apache.samza.task.TaskContext) ImmutableSet(com.google.common.collect.ImmutableSet) IncomingMessageEnvelope(org.apache.samza.system.IncomingMessageEnvelope) Partition(org.apache.samza.Partition) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) StreamSpec(org.apache.samza.system.StreamSpec) Clock(org.apache.samza.util.Clock) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) JoinFunction(org.apache.samza.operators.functions.JoinFunction) TaskCoordinator(org.apache.samza.task.TaskCoordinator) List(java.util.List) SystemClock(org.apache.samza.util.SystemClock) TestClock(org.apache.samza.testUtils.TestClock) OutgoingMessageEnvelope(org.apache.samza.system.OutgoingMessageEnvelope) Config(org.apache.samza.config.Config) StreamApplication(org.apache.samza.application.StreamApplication) Assert.assertEquals(org.junit.Assert.assertEquals) MetricsRegistryMap(org.apache.samza.metrics.MetricsRegistryMap) Mockito.mock(org.mockito.Mockito.mock) StreamOperatorTask(org.apache.samza.task.StreamOperatorTask) MessageCollector(org.apache.samza.task.MessageCollector) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 2 with TestClock

use of org.apache.samza.testUtils.TestClock in project samza by apache.

the class TestJoinOperator method joinRemovesExpiredMessages.

@Test
public void joinRemovesExpiredMessages() throws Exception {
    TestClock testClock = new TestClock();
    StreamOperatorTask sot = createStreamOperatorTask(testClock, new TestJoinStreamApplication(new TestJoinFunction()));
    List<Integer> output = new ArrayList<>();
    MessageCollector messageCollector = envelope -> output.add((Integer) envelope.getMessage());
    // push messages to first stream
    numbers.forEach(n -> sot.process(new FirstStreamIME(n, n), messageCollector, taskCoordinator));
    // 1 minute after ttl
    testClock.advanceTime(JOIN_TTL.plus(Duration.ofMinutes(1)));
    // should expire first stream messages
    sot.window(messageCollector, taskCoordinator);
    // push messages to second stream with same key
    numbers.forEach(n -> sot.process(new SecondStreamIME(n, n), messageCollector, taskCoordinator));
    assertTrue(output.isEmpty());
}
Also used : TestClock(org.apache.samza.testUtils.TestClock) StreamOperatorTask(org.apache.samza.task.StreamOperatorTask) SystemStreamPartition(org.apache.samza.system.SystemStreamPartition) ArrayList(java.util.ArrayList) MessageCollector(org.apache.samza.task.MessageCollector) SystemStream(org.apache.samza.system.SystemStream) Duration(java.time.Duration) ApplicationRunner(org.apache.samza.runtime.ApplicationRunner) TaskContext(org.apache.samza.task.TaskContext) ImmutableSet(com.google.common.collect.ImmutableSet) IncomingMessageEnvelope(org.apache.samza.system.IncomingMessageEnvelope) Partition(org.apache.samza.Partition) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) StreamSpec(org.apache.samza.system.StreamSpec) Clock(org.apache.samza.util.Clock) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) JoinFunction(org.apache.samza.operators.functions.JoinFunction) TaskCoordinator(org.apache.samza.task.TaskCoordinator) List(java.util.List) SystemClock(org.apache.samza.util.SystemClock) TestClock(org.apache.samza.testUtils.TestClock) OutgoingMessageEnvelope(org.apache.samza.system.OutgoingMessageEnvelope) Config(org.apache.samza.config.Config) StreamApplication(org.apache.samza.application.StreamApplication) Assert.assertEquals(org.junit.Assert.assertEquals) MetricsRegistryMap(org.apache.samza.metrics.MetricsRegistryMap) Mockito.mock(org.mockito.Mockito.mock) StreamOperatorTask(org.apache.samza.task.StreamOperatorTask) MessageCollector(org.apache.samza.task.MessageCollector) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 3 with TestClock

use of org.apache.samza.testUtils.TestClock in project samza by apache.

the class TestWindowOperator method testTumblingWindowsAccumulatingMode.

@Test
public void testTumblingWindowsAccumulatingMode() throws Exception {
    StreamApplication sgb = new KeyedTumblingWindowStreamApplication(AccumulationMode.ACCUMULATING, 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(), 7);
    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(), 4);
    Assert.assertEquals(windowPanes.get(3).getKey().getKey(), new Integer(2));
    Assert.assertEquals((windowPanes.get(3).getMessage()).size(), 4);
}
Also used : TestClock(org.apache.samza.testUtils.TestClock) StreamOperatorTask(org.apache.samza.task.StreamOperatorTask) SystemStreamPartition(org.apache.samza.system.SystemStreamPartition) Function(java.util.function.Function) WindowPane(org.apache.samza.operators.windows.WindowPane) ArrayList(java.util.ArrayList) Trigger(org.apache.samza.operators.triggers.Trigger) ImmutableList(com.google.common.collect.ImmutableList) AccumulationMode(org.apache.samza.operators.windows.AccumulationMode) MessageCollector(org.apache.samza.task.MessageCollector) FiringType(org.apache.samza.operators.triggers.FiringType) SystemStream(org.apache.samza.system.SystemStream) Duration(java.time.Duration) Before(org.junit.Before) ApplicationRunner(org.apache.samza.runtime.ApplicationRunner) TaskContext(org.apache.samza.task.TaskContext) ImmutableSet(com.google.common.collect.ImmutableSet) Windows(org.apache.samza.operators.windows.Windows) IncomingMessageEnvelope(org.apache.samza.system.IncomingMessageEnvelope) Collection(java.util.Collection) Partition(org.apache.samza.Partition) StreamSpec(org.apache.samza.system.StreamSpec) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) TaskCoordinator(org.apache.samza.task.TaskCoordinator) Triggers(org.apache.samza.operators.triggers.Triggers) List(java.util.List) TestClock(org.apache.samza.testUtils.TestClock) Assert(junit.framework.Assert) OutgoingMessageEnvelope(org.apache.samza.system.OutgoingMessageEnvelope) Config(org.apache.samza.config.Config) StreamApplication(org.apache.samza.application.StreamApplication) MetricsRegistryMap(org.apache.samza.metrics.MetricsRegistryMap) Mockito.mock(org.mockito.Mockito.mock) StreamApplication(org.apache.samza.application.StreamApplication) StreamOperatorTask(org.apache.samza.task.StreamOperatorTask) MessageCollector(org.apache.samza.task.MessageCollector) ArrayList(java.util.ArrayList) Collection(java.util.Collection) WindowPane(org.apache.samza.operators.windows.WindowPane) Test(org.junit.Test)

Example 4 with TestClock

use of org.apache.samza.testUtils.TestClock 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);
}
Also used : TestClock(org.apache.samza.testUtils.TestClock) StreamOperatorTask(org.apache.samza.task.StreamOperatorTask) SystemStreamPartition(org.apache.samza.system.SystemStreamPartition) Function(java.util.function.Function) WindowPane(org.apache.samza.operators.windows.WindowPane) ArrayList(java.util.ArrayList) Trigger(org.apache.samza.operators.triggers.Trigger) ImmutableList(com.google.common.collect.ImmutableList) AccumulationMode(org.apache.samza.operators.windows.AccumulationMode) MessageCollector(org.apache.samza.task.MessageCollector) FiringType(org.apache.samza.operators.triggers.FiringType) SystemStream(org.apache.samza.system.SystemStream) Duration(java.time.Duration) Before(org.junit.Before) ApplicationRunner(org.apache.samza.runtime.ApplicationRunner) TaskContext(org.apache.samza.task.TaskContext) ImmutableSet(com.google.common.collect.ImmutableSet) Windows(org.apache.samza.operators.windows.Windows) IncomingMessageEnvelope(org.apache.samza.system.IncomingMessageEnvelope) Collection(java.util.Collection) Partition(org.apache.samza.Partition) StreamSpec(org.apache.samza.system.StreamSpec) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) TaskCoordinator(org.apache.samza.task.TaskCoordinator) Triggers(org.apache.samza.operators.triggers.Triggers) List(java.util.List) TestClock(org.apache.samza.testUtils.TestClock) Assert(junit.framework.Assert) OutgoingMessageEnvelope(org.apache.samza.system.OutgoingMessageEnvelope) Config(org.apache.samza.config.Config) StreamApplication(org.apache.samza.application.StreamApplication) MetricsRegistryMap(org.apache.samza.metrics.MetricsRegistryMap) Mockito.mock(org.mockito.Mockito.mock) StreamApplication(org.apache.samza.application.StreamApplication) StreamOperatorTask(org.apache.samza.task.StreamOperatorTask) MessageCollector(org.apache.samza.task.MessageCollector) ArrayList(java.util.ArrayList) Collection(java.util.Collection) WindowPane(org.apache.samza.operators.windows.WindowPane) Test(org.junit.Test)

Example 5 with TestClock

use of org.apache.samza.testUtils.TestClock 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);
}
Also used : TestClock(org.apache.samza.testUtils.TestClock) StreamOperatorTask(org.apache.samza.task.StreamOperatorTask) SystemStreamPartition(org.apache.samza.system.SystemStreamPartition) Function(java.util.function.Function) WindowPane(org.apache.samza.operators.windows.WindowPane) ArrayList(java.util.ArrayList) Trigger(org.apache.samza.operators.triggers.Trigger) ImmutableList(com.google.common.collect.ImmutableList) AccumulationMode(org.apache.samza.operators.windows.AccumulationMode) MessageCollector(org.apache.samza.task.MessageCollector) FiringType(org.apache.samza.operators.triggers.FiringType) SystemStream(org.apache.samza.system.SystemStream) Duration(java.time.Duration) Before(org.junit.Before) ApplicationRunner(org.apache.samza.runtime.ApplicationRunner) TaskContext(org.apache.samza.task.TaskContext) ImmutableSet(com.google.common.collect.ImmutableSet) Windows(org.apache.samza.operators.windows.Windows) IncomingMessageEnvelope(org.apache.samza.system.IncomingMessageEnvelope) Collection(java.util.Collection) Partition(org.apache.samza.Partition) StreamSpec(org.apache.samza.system.StreamSpec) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) TaskCoordinator(org.apache.samza.task.TaskCoordinator) Triggers(org.apache.samza.operators.triggers.Triggers) List(java.util.List) TestClock(org.apache.samza.testUtils.TestClock) Assert(junit.framework.Assert) OutgoingMessageEnvelope(org.apache.samza.system.OutgoingMessageEnvelope) Config(org.apache.samza.config.Config) StreamApplication(org.apache.samza.application.StreamApplication) MetricsRegistryMap(org.apache.samza.metrics.MetricsRegistryMap) Mockito.mock(org.mockito.Mockito.mock) StreamApplication(org.apache.samza.application.StreamApplication) StreamOperatorTask(org.apache.samza.task.StreamOperatorTask) MessageCollector(org.apache.samza.task.MessageCollector) ArrayList(java.util.ArrayList) Collection(java.util.Collection) WindowPane(org.apache.samza.operators.windows.WindowPane) Test(org.junit.Test)

Aggregations

ImmutableSet (com.google.common.collect.ImmutableSet)10 Duration (java.time.Duration)10 ArrayList (java.util.ArrayList)10 List (java.util.List)10 Partition (org.apache.samza.Partition)10 StreamApplication (org.apache.samza.application.StreamApplication)10 Config (org.apache.samza.config.Config)10 MetricsRegistryMap (org.apache.samza.metrics.MetricsRegistryMap)10 ApplicationRunner (org.apache.samza.runtime.ApplicationRunner)10 IncomingMessageEnvelope (org.apache.samza.system.IncomingMessageEnvelope)10 OutgoingMessageEnvelope (org.apache.samza.system.OutgoingMessageEnvelope)10 StreamSpec (org.apache.samza.system.StreamSpec)10 SystemStream (org.apache.samza.system.SystemStream)10 SystemStreamPartition (org.apache.samza.system.SystemStreamPartition)10 MessageCollector (org.apache.samza.task.MessageCollector)10 StreamOperatorTask (org.apache.samza.task.StreamOperatorTask)10 TaskContext (org.apache.samza.task.TaskContext)10 TaskCoordinator (org.apache.samza.task.TaskCoordinator)10 TestClock (org.apache.samza.testUtils.TestClock)10 Test (org.junit.Test)10