use of edu.snu.mist.core.utils.FindMaxIntFunction in project mist by snuspl.
the class ApplyStatefulOperatorTest method testApplyStatefulOperator.
/**
* Test ApplyStatefulOperator.
* It calculates the maximum value through some inputs.
*/
@Test
public void testApplyStatefulOperator() {
// input events
// expected results: 10--20--20--30--watermark--
final MistDataEvent data10 = new MistDataEvent(10, 0L);
final MistDataEvent data20 = new MistDataEvent(20, 1L);
final MistDataEvent data15 = new MistDataEvent(15, 2L);
final MistDataEvent data30 = new MistDataEvent(30, 3L);
final MistWatermarkEvent watermarkEvent = new MistWatermarkEvent(4L);
// the state managing function finding maximum integer value among received inputs
final ApplyStatefulFunction applyStatefulFunction = new FindMaxIntFunction();
final ApplyStatefulOperator<Integer, Integer> applyStatefulOperator = new ApplyStatefulOperator<>(applyStatefulFunction);
final List<MistEvent> result = new LinkedList<>();
applyStatefulOperator.setOutputEmitter(new OutputBufferEmitter(result));
applyStatefulOperator.processLeftData(data10);
Assert.assertEquals(1, result.size());
Assert.assertEquals(data10, result.get(0));
applyStatefulOperator.processLeftData(data20);
Assert.assertEquals(2, result.size());
Assert.assertEquals(data20, result.get(1));
applyStatefulOperator.processLeftData(data15);
Assert.assertEquals(3, result.size());
Assert.assertTrue(result.get(2) instanceof MistDataEvent);
Assert.assertEquals(20, ((MistDataEvent) result.get(2)).getValue());
Assert.assertEquals(2L, result.get(2).getTimestamp());
applyStatefulOperator.processLeftData(data30);
Assert.assertEquals(4, result.size());
Assert.assertEquals(data30, result.get(3));
applyStatefulOperator.processLeftWatermark(watermarkEvent);
Assert.assertEquals(5, result.size());
Assert.assertEquals(watermarkEvent, result.get(4));
}
use of edu.snu.mist.core.utils.FindMaxIntFunction in project mist by snuspl.
the class ApplyStatefulWindowOperatorTest method testApplyStatefulWindowOperator.
/**
* Test ApplyStatefulWindowOperator.
* It calculates the maximum value from the collection.
*/
@Test
public void testApplyStatefulWindowOperator() {
// input stream events
final WindowImpl<Integer> window1 = new WindowImpl<>(0L, 100L);
final WindowImpl<Integer> window2 = new WindowImpl<>(100L, 100L);
window1.putData(new MistDataEvent(10, 10));
window1.putData(new MistDataEvent(20, 20));
window1.putData(new MistDataEvent(30, 30));
window1.putData(new MistDataEvent(15, 90));
window2.putData(new MistDataEvent(10, 110));
window2.putData(new MistDataEvent(20, 120));
final MistDataEvent dataEvent1 = new MistDataEvent(window1, 90L);
final MistDataEvent dataEvent2 = new MistDataEvent(window2, 190L);
final MistWatermarkEvent watermarkEvent = new MistWatermarkEvent(201L);
// the state finding maximum integer value among received inputs
final ApplyStatefulFunction<Integer, Integer> applyStatefulFunction = new FindMaxIntFunction();
final ApplyStatefulWindowOperator<Integer, Integer> applyStatefulWindowOperator = new ApplyStatefulWindowOperator<>(applyStatefulFunction);
final List<MistEvent> result = new LinkedList<>();
applyStatefulWindowOperator.setOutputEmitter(new OutputBufferEmitter(result));
// check that the operator processes a window input properly
applyStatefulWindowOperator.processLeftData(dataEvent1);
Assert.assertEquals(1, result.size());
Assert.assertTrue(result.get(0).isData());
Assert.assertEquals(30, ((MistDataEvent) result.get(0)).getValue());
Assert.assertEquals(90L, result.get(0).getTimestamp());
// check that the operator processes another window separately
applyStatefulWindowOperator.processLeftData(dataEvent2);
Assert.assertEquals(2, result.size());
Assert.assertTrue(result.get(1).isData());
Assert.assertEquals(20, ((MistDataEvent) result.get(1)).getValue());
Assert.assertEquals(190L, result.get(1).getTimestamp());
// check that the operator processes watermark event well
applyStatefulWindowOperator.processLeftWatermark(watermarkEvent);
Assert.assertEquals(3, result.size());
Assert.assertEquals(watermarkEvent, result.get(2));
}
use of edu.snu.mist.core.utils.FindMaxIntFunction 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.utils.FindMaxIntFunction 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);
}
Aggregations