use of edu.snu.mist.core.MistDataEvent in project mist by snuspl.
the class AggregateWindowOperatorTest method testAggregateWindowOperator.
/**
* Test AggregateWindowOperator.
* It calculates the maximum value from the collection.
*/
@Test
public void testAggregateWindowOperator() {
// input stream events
final WindowImpl<Integer> window = new WindowImpl<>(0L, 100L);
window.putData(new MistDataEvent(10, 10));
window.putData(new MistDataEvent(20, 20));
window.putData(new MistDataEvent(15, 30));
window.putData(new MistDataEvent(30, 90));
final MistDataEvent dataEvent = new MistDataEvent(window, 90L);
final MistWatermarkEvent watermarkEvent = new MistWatermarkEvent(101L);
// functions that dealing with input WindowData
final MISTFunction<WindowData<Integer>, String> aggregateFunc = (windowData) -> {
return windowData.getDataCollection().toString() + ", " + windowData.getStart() + ", " + windowData.getEnd();
};
final AggregateWindowOperator<Integer, String> aggregateWindowOperator = new AggregateWindowOperator<>(aggregateFunc);
final List<MistEvent> result = new LinkedList<>();
aggregateWindowOperator.setOutputEmitter(new OutputBufferEmitter(result));
aggregateWindowOperator.processLeftData(dataEvent);
Assert.assertEquals(1, result.size());
Assert.assertTrue(result.get(0).isData());
Assert.assertEquals("[10, 20, 15, 30], 0, 99", ((MistDataEvent) result.get(0)).getValue());
Assert.assertEquals(90L, result.get(0).getTimestamp());
aggregateWindowOperator.processLeftWatermark(watermarkEvent);
Assert.assertEquals(2, result.size());
Assert.assertEquals(watermarkEvent, result.get(1));
}
use of edu.snu.mist.core.MistDataEvent 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());
}
use of edu.snu.mist.core.MistDataEvent 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);
}
use of edu.snu.mist.core.MistDataEvent in project mist by snuspl.
the class ConditionalBranchOperatorTest method testConditionalBranchOperator.
/**
* Test conditional branch operation.
* It classifies the input string according to it's length.
*/
@Test
public void testConditionalBranchOperator() throws InjectionException {
// input stream events
final MistDataEvent d1 = new MistDataEvent("1", 1L);
final MistDataEvent d2 = new MistDataEvent("22", 2L);
final MistDataEvent d3 = new MistDataEvent("333", 3L);
final MistDataEvent d4 = new MistDataEvent("4444", 4L);
final MistDataEvent d5 = new MistDataEvent("55555", 5L);
final MistWatermarkEvent w1 = new MistWatermarkEvent(6L);
// classify the string according to it's length
final List<MISTPredicate<String>> predicates = new ArrayList<>();
// "1" will be passed with index 1
predicates.add((input) -> input.length() < 2);
// "22" will be passed with index 2
predicates.add((input) -> input.length() < 3);
// "333", "4444" will be passed with index 3
predicates.add((input) -> input.length() < 5);
// "55555" will not be passed
final List<Tuple<MistEvent, Integer>> result = new LinkedList<>();
final ConditionalBranchOperator<String> conditionalBranchOperator = new ConditionalBranchOperator<>(predicates);
conditionalBranchOperator.setOutputEmitter(new IndexOutputEmitter(result));
conditionalBranchOperator.processLeftData(d1);
conditionalBranchOperator.processLeftData(d2);
conditionalBranchOperator.processLeftData(d3);
conditionalBranchOperator.processLeftData(d4);
conditionalBranchOperator.processLeftData(d5);
conditionalBranchOperator.processLeftWatermark(w1);
Assert.assertEquals(5, result.size());
Assert.assertEquals(new Tuple<>(d1, 1), result.get(0));
Assert.assertEquals(new Tuple<>(d2, 2), result.get(1));
Assert.assertEquals(new Tuple<>(d3, 3), result.get(2));
Assert.assertEquals(new Tuple<>(d4, 3), result.get(3));
Assert.assertEquals(new Tuple<>(w1, 0), result.get(4));
}
use of edu.snu.mist.core.MistDataEvent in project mist by snuspl.
the class OperatorTestUtils method checkWindowData.
/**
* Checks the windowed result is equal to the expected result.
*/
public static void checkWindowData(final MistEvent result, final Collection<Integer> expectedResult, final long expectedWindowStartMoment, final long expectedWindowSize, final long expectedWindowTimestamp) {
Assert.assertTrue(result.isData());
Assert.assertTrue(((MistDataEvent) result).getValue() instanceof WindowData);
final WindowData windowData = (WindowData) ((MistDataEvent) result).getValue();
Assert.assertEquals(expectedResult, windowData.getDataCollection());
Assert.assertEquals(expectedWindowStartMoment, windowData.getStart());
Assert.assertEquals(expectedWindowSize, windowData.getEnd() - windowData.getStart() + 1);
Assert.assertEquals(expectedWindowTimestamp, result.getTimestamp());
}
Aggregations