Search in sources :

Example 21 with OutputBufferEmitter

use of edu.snu.mist.core.utils.OutputBufferEmitter in project mist by snuspl.

the class ApplyStatefulOperatorTest method testApplyStatefulOperatorSetState.

/**
 * Test setting state of the ApplyStatefulOperator.
 */
@Test
public void testApplyStatefulOperatorSetState() throws InterruptedException {
    // Generate a new state and set it to a new ApplyStatefulOperator.
    final ApplyStatefulFunction applyStatefulFunction = new FindMaxIntFunction();
    final int expectedApplyStatefulFunctionState = 5;
    final Map<String, Object> loadStateMap = new HashMap<>();
    loadStateMap.put("applyStatefulFunctionState", expectedApplyStatefulFunctionState);
    final ApplyStatefulOperator<Integer, Integer> applyStatefulOperator = new ApplyStatefulOperator<>(applyStatefulFunction);
    applyStatefulOperator.setState(loadStateMap);
    // Get the current ApplyStatefulOperator's state.
    final Map<String, Object> operatorState = applyStatefulOperator.getStateSnapshot();
    final int applyStatefulFunctionState = (Integer) operatorState.get("applyStatefulFunctionState");
    // Compare the original and the set operator.
    Assert.assertEquals(expectedApplyStatefulFunctionState, applyStatefulFunctionState);
    // Test if the operator can properly process data.
    final List<MistEvent> result = new LinkedList<>();
    applyStatefulOperator.setOutputEmitter(new OutputBufferEmitter(result));
    final MistDataEvent data10 = new MistDataEvent(10, 0L);
    final MistDataEvent data20 = new MistDataEvent(20, 1L);
    final MistDataEvent data15 = new MistDataEvent(15, 2L);
    applyStatefulOperator.processLeftData(data10);
    Assert.assertEquals(1, result.size());
    Assert.assertEquals(10, ((MistDataEvent) result.get(0)).getValue());
    applyStatefulOperator.processLeftData(data20);
    Assert.assertEquals(2, result.size());
    Assert.assertEquals(20, ((MistDataEvent) result.get(1)).getValue());
    applyStatefulOperator.processLeftData(data15);
    Assert.assertEquals(3, result.size());
    Assert.assertEquals(20, ((MistDataEvent) result.get(2)).getValue());
}
Also used : ApplyStatefulFunction(edu.snu.mist.common.functions.ApplyStatefulFunction) HashMap(java.util.HashMap) MistEvent(edu.snu.mist.core.MistEvent) LinkedList(java.util.LinkedList) OutputBufferEmitter(edu.snu.mist.core.utils.OutputBufferEmitter) MistDataEvent(edu.snu.mist.core.MistDataEvent) FindMaxIntFunction(edu.snu.mist.core.utils.FindMaxIntFunction) Test(org.junit.Test)

Example 22 with OutputBufferEmitter

use of edu.snu.mist.core.utils.OutputBufferEmitter in project mist by snuspl.

the class ApplyStatefulOperatorTest method testApplyStatefulOperatorGetState.

/**
 * Test getting state of the ApplyStatefulOperator.
 */
@Test
public void testApplyStatefulOperatorGetState() throws InterruptedException {
    // Generate the current ApplyStatefulOperator.
    final ApplyStatefulFunction applyStatefulFunction = new FindMaxIntFunction();
    final ApplyStatefulOperator<Integer, Integer> applyStatefulOperator = new ApplyStatefulOperator<>(applyStatefulFunction);
    final MistDataEvent data10 = new MistDataEvent(10, 0L);
    final MistDataEvent data20 = new MistDataEvent(20, 1L);
    final List<MistEvent> result = new LinkedList<>();
    applyStatefulOperator.setOutputEmitter(new OutputBufferEmitter(result));
    applyStatefulOperator.processLeftData(data10);
    applyStatefulOperator.processLeftData(data20);
    // Generate the expected ApplyStatefulOperator's state.
    final int expectedApplyStatefulOperatorState = 20;
    // Get the current ApplyStatefulOperator's state.
    final Map<String, Object> operatorState = applyStatefulOperator.getStateSnapshot();
    final int applyStatefulOperatorState = (int) operatorState.get("applyStatefulFunctionState");
    // Compare the expected and original operator's state.
    Assert.assertEquals(expectedApplyStatefulOperatorState, applyStatefulOperatorState);
}
Also used : ApplyStatefulFunction(edu.snu.mist.common.functions.ApplyStatefulFunction) MistEvent(edu.snu.mist.core.MistEvent) LinkedList(java.util.LinkedList) OutputBufferEmitter(edu.snu.mist.core.utils.OutputBufferEmitter) MistDataEvent(edu.snu.mist.core.MistDataEvent) FindMaxIntFunction(edu.snu.mist.core.utils.FindMaxIntFunction) Test(org.junit.Test)

Example 23 with OutputBufferEmitter

use of edu.snu.mist.core.utils.OutputBufferEmitter in project mist by snuspl.

the class FixedSizeWindowOperatorTest method testSlidingTimeWindowOperator.

/**
 * Test TimeWindowOperator creating sliding window.
 * It receives some continuous data stream and groups them as a collection.
 */
@SuppressWarnings("unchecked")
@Test
public void testSlidingTimeWindowOperator() throws InterruptedException {
    final int windowSize = 500;
    final int emissionInterval = 250;
    final TimeWindowOperator<Integer> timeWindowOperator = new TimeWindowOperator<>(windowSize, emissionInterval);
    final List<MistEvent> result = new LinkedList<>();
    timeWindowOperator.setOutputEmitter(new OutputBufferEmitter(result));
    // (1000)Window1-----(1249):
    // (1000)Window2----------------------(1499):
    // (1250)Window3----------------(1749):
    // (1500)Window4----------------------(1999):
    // (1750)Window5-----------------------------: (will not be emitted)
    // d1--------------------------d2-----------d3-w1-------------w2-----------------w3:
    // expected results:
    // d1 in Window1
    // d1, d2 in Window2
    // d2, d3, w1 in Window3
    // d3, w2 in Window4
    timeWindowOperator.processLeftData(d1);
    Assert.assertEquals(0, result.size());
    timeWindowOperator.processLeftData(d2);
    Assert.assertEquals(1, result.size());
    final Collection<Integer> expectedResult1 = new LinkedList<>();
    expectedResult1.add(1);
    OperatorTestUtils.checkWindowData(result.get(0), expectedResult1, d1.getTimestamp(), emissionInterval, d1.getTimestamp());
    timeWindowOperator.processLeftData(d3);
    timeWindowOperator.processLeftWatermark(w1);
    Assert.assertEquals(2, result.size());
    final Collection<Integer> expectedResult2 = new LinkedList<>();
    expectedResult2.add(1);
    expectedResult2.add(2);
    OperatorTestUtils.checkWindowData(result.get(1), expectedResult2, d1.getTimestamp(), windowSize, d2.getTimestamp());
    timeWindowOperator.processLeftWatermark(w2);
    Assert.assertEquals(4, result.size());
    final Collection<Integer> expectedResult3 = new LinkedList<>();
    expectedResult3.add(2);
    expectedResult3.add(3);
    OperatorTestUtils.checkWindowData(result.get(2), expectedResult3, d1.getTimestamp() + emissionInterval, windowSize, d3.getTimestamp());
    Assert.assertEquals(w1, result.get(3));
    timeWindowOperator.processLeftWatermark(w3);
    Assert.assertEquals(6, result.size());
    final Collection<Integer> expectedResult4 = new LinkedList<>();
    expectedResult4.add(3);
    OperatorTestUtils.checkWindowData(result.get(4), expectedResult4, d1.getTimestamp() + 2 * emissionInterval, windowSize, w2.getTimestamp());
    Assert.assertEquals(w2, result.get(5));
}
Also used : OutputBufferEmitter(edu.snu.mist.core.utils.OutputBufferEmitter) TimeWindowOperator(edu.snu.mist.core.operators.window.TimeWindowOperator) MistEvent(edu.snu.mist.core.MistEvent) Test(org.junit.Test)

Example 24 with OutputBufferEmitter

use of edu.snu.mist.core.utils.OutputBufferEmitter in project mist by snuspl.

the class FixedSizeWindowOperatorTest method testSlidingCountWindowOperator.

/**
 * Test CountWindowOperator creating sliding window.
 * It receives some continuous data stream and groups them as a collection.
 */
@Test
public void testSlidingCountWindowOperator() throws InterruptedException {
    final int windowSize = 5;
    final int emissionInterval = 3;
    final CountWindowOperator<Integer> countWindowOperator = new CountWindowOperator<>(windowSize, emissionInterval);
    final List<MistEvent> result = new LinkedList<>();
    countWindowOperator.setOutputEmitter(new OutputBufferEmitter(result));
    // (1)Window1(3):
    // (2)Window2--------(6):
    // (5)Window3-------(9):
    // (8)Window4--------------: (will not be emitted)
    // d1----d2--d3--d4-w1-d5--d6--d7-w2-d8-d9:
    // expected results:
    // d1, d2, d3 in Window1
    // d2, d3, d4, d5, d6, w1 in Window2
    // d5, d6, d7, d8, d9, w2 in Window3
    countWindowOperator.processLeftData(d1);
    countWindowOperator.processLeftData(d2);
    Assert.assertEquals(0, result.size());
    countWindowOperator.processLeftData(d3);
    Assert.assertEquals(1, result.size());
    final Collection<Integer> expectedResult1 = new LinkedList<>();
    expectedResult1.add(1);
    expectedResult1.add(2);
    expectedResult1.add(3);
    OperatorTestUtils.checkWindowData(result.get(0), expectedResult1, 1L, emissionInterval, d3.getTimestamp());
    countWindowOperator.processLeftData(d4);
    countWindowOperator.processLeftWatermark(w1);
    countWindowOperator.processLeftData(d5);
    Assert.assertEquals(1, result.size());
    countWindowOperator.processLeftData(d6);
    Assert.assertEquals(3, result.size());
    final Collection<Integer> expectedResult2 = new LinkedList<>();
    expectedResult2.add(2);
    expectedResult2.add(3);
    expectedResult2.add(4);
    expectedResult2.add(5);
    expectedResult2.add(6);
    OperatorTestUtils.checkWindowData(result.get(1), expectedResult2, 2L, windowSize, d6.getTimestamp());
    Assert.assertEquals(w1, result.get(2));
    countWindowOperator.processLeftData(d7);
    countWindowOperator.processLeftWatermark(w2);
    countWindowOperator.processLeftData(d8);
    Assert.assertEquals(3, result.size());
    countWindowOperator.processLeftData(d9);
    Assert.assertEquals(5, result.size());
    final Collection<Integer> expectedResult3 = new LinkedList<>();
    expectedResult3.add(5);
    expectedResult3.add(6);
    expectedResult3.add(7);
    expectedResult3.add(8);
    expectedResult3.add(9);
    OperatorTestUtils.checkWindowData(result.get(3), expectedResult3, 5L, windowSize, d9.getTimestamp());
    Assert.assertEquals(w2, result.get(4));
}
Also used : OutputBufferEmitter(edu.snu.mist.core.utils.OutputBufferEmitter) CountWindowOperator(edu.snu.mist.core.operators.window.CountWindowOperator) MistEvent(edu.snu.mist.core.MistEvent) Test(org.junit.Test)

Example 25 with OutputBufferEmitter

use of edu.snu.mist.core.utils.OutputBufferEmitter in project mist by snuspl.

the class FixedSizeWindowOperatorTest method testCountWindowOperatorSetState.

/**
 * Test setting state of the CountWindowOperator.
 */
@Test
public void testCountWindowOperatorSetState() throws InterruptedException {
    final int windowSize = 5;
    final int emissionInterval = 3;
    // Generate a new state and set it to a new CountWindowOperator.
    final Window expectedWindow1 = new WindowImpl<>(1L, emissionInterval, new LinkedList<Integer>());
    expectedWindow1.putData(d1);
    expectedWindow1.putData(d2);
    final Window expectedWindow2 = new WindowImpl<>(2L, windowSize, new LinkedList<Integer>());
    expectedWindow2.putData(d2);
    final Queue<Window<Integer>> expectedWindowQueue = new LinkedList<>();
    expectedWindowQueue.add(expectedWindow1);
    expectedWindowQueue.add(expectedWindow2);
    final long expectedWindowCreationPoint = 2L + emissionInterval;
    final long expectedCount = 3L;
    final Map<String, Object> loadStateMap = new HashMap<>();
    loadStateMap.put("windowQueue", expectedWindowQueue);
    loadStateMap.put("windowCreationPoint", expectedWindowCreationPoint);
    loadStateMap.put("count", expectedCount);
    final CountWindowOperator<Integer> countWindowOperator = new CountWindowOperator<>(windowSize, emissionInterval);
    countWindowOperator.setState(loadStateMap);
    // Compare the original and the set operator.
    final Map<String, Object> operatorState = countWindowOperator.getStateSnapshot();
    final Queue<Window<Integer>> windowQueue = (Queue<Window<Integer>>) operatorState.get("windowQueue");
    final long windowCreationPoint = (long) operatorState.get("windowCreationPoint");
    final long count = (long) operatorState.get("count");
    Assert.assertEquals(expectedWindowQueue, windowQueue);
    Assert.assertEquals(expectedWindowCreationPoint, windowCreationPoint);
    Assert.assertEquals(expectedCount, count);
    // Test if the operator can properly process data.
    final List<MistEvent> result = new LinkedList<>();
    countWindowOperator.setOutputEmitter(new OutputBufferEmitter(result));
    countWindowOperator.processLeftData(d3);
    Assert.assertEquals(1, result.size());
    final Collection<Integer> expectedResult1 = new LinkedList<>();
    expectedResult1.add(1);
    expectedResult1.add(2);
    expectedResult1.add(3);
    OperatorTestUtils.checkWindowData(result.get(0), expectedResult1, 1L, emissionInterval, d3.getTimestamp());
}
Also used : Window(edu.snu.mist.core.operators.window.Window) WindowImpl(edu.snu.mist.core.operators.window.WindowImpl) CountWindowOperator(edu.snu.mist.core.operators.window.CountWindowOperator) MistEvent(edu.snu.mist.core.MistEvent) OutputBufferEmitter(edu.snu.mist.core.utils.OutputBufferEmitter) Test(org.junit.Test)

Aggregations

MistEvent (edu.snu.mist.core.MistEvent)26 OutputBufferEmitter (edu.snu.mist.core.utils.OutputBufferEmitter)26 Test (org.junit.Test)25 MistDataEvent (edu.snu.mist.core.MistDataEvent)17 LinkedList (java.util.LinkedList)15 Tuple2 (edu.snu.mist.common.types.Tuple2)7 MistWatermarkEvent (edu.snu.mist.core.MistWatermarkEvent)6 WindowImpl (edu.snu.mist.core.operators.window.WindowImpl)6 List (java.util.List)5 Assert (org.junit.Assert)5 CepEventPattern (edu.snu.mist.common.cep.CepEventPattern)4 CepExampleClass (edu.snu.mist.core.utils.CepExampleClass)4 FindMaxIntFunction (edu.snu.mist.core.utils.FindMaxIntFunction)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 ImmutableList (com.google.common.collect.ImmutableList)3 ApplyStatefulFunction (edu.snu.mist.common.functions.ApplyStatefulFunction)3 MISTBiFunction (edu.snu.mist.common.functions.MISTBiFunction)3 MISTPredicate (edu.snu.mist.common.functions.MISTPredicate)3 RuleBasedEQPredicate (edu.snu.mist.common.predicates.RuleBasedEQPredicate)3