Search in sources :

Example 1 with SessionWindowOperator

use of edu.snu.mist.core.operators.window.SessionWindowOperator in project mist by snuspl.

the class SessionWindowOperatorTest method testSessionWindowOperatorSetState.

/**
 * Test setting state of the SessionWindowOperator.
 */
@Test
public void testSessionWindowOperatorSetState() throws InterruptedException {
    final int sessionInterval = 500;
    // Generate a new state and set it to a new SessionWindowOperator.
    final Window expectedCurrentWindow = new WindowImpl<>(d5.getTimestamp(), Long.MAX_VALUE, new LinkedList<>());
    expectedCurrentWindow.putData(d5);
    expectedCurrentWindow.putWatermark(w2);
    expectedCurrentWindow.setEnd(w2.getTimestamp());
    final long expectedLatestDataTimestamp = d5.getTimestamp();
    final boolean expectedStartedNewWindow = true;
    final Map<String, Object> loadStateMap = new HashMap<>();
    loadStateMap.put("currentWindow", expectedCurrentWindow);
    loadStateMap.put("latestDataTimestamp", expectedLatestDataTimestamp);
    loadStateMap.put("startedNewWindow", expectedStartedNewWindow);
    final SessionWindowOperator sessionWindowOperator = new SessionWindowOperator(sessionInterval);
    sessionWindowOperator.setState(loadStateMap);
    // Compare the original and the set operator.
    final Map<String, Object> operatorState = sessionWindowOperator.getStateSnapshot();
    final Window<Integer> currentWindow = (Window<Integer>) operatorState.get("currentWindow");
    final long latestDataTimestamp = (long) operatorState.get("latestDataTimestamp");
    final boolean startedNewWindow = (boolean) operatorState.get("startedNewWindow");
    Assert.assertEquals(expectedCurrentWindow, currentWindow);
    Assert.assertEquals(expectedLatestDataTimestamp, latestDataTimestamp);
    Assert.assertEquals(expectedStartedNewWindow, startedNewWindow);
    // Test if the operator can properly process data.
    final List<MistEvent> result = new LinkedList<>();
    sessionWindowOperator.setOutputEmitter(new OutputBufferEmitter(result));
    sessionWindowOperator.setState(operatorState);
    sessionWindowOperator.processLeftData(d6);
    Assert.assertEquals(2, result.size());
    final Collection<Integer> expectedResult = new LinkedList<>();
    expectedResult.add(5);
    OperatorTestUtils.checkWindowData(result.get(0), expectedResult, d5.getTimestamp(), w2.getTimestamp() - d5.getTimestamp() + 1, w2.getTimestamp());
    Assert.assertEquals(w2, result.get(1));
}
Also used : Window(edu.snu.mist.core.operators.window.Window) SessionWindowOperator(edu.snu.mist.core.operators.window.SessionWindowOperator) WindowImpl(edu.snu.mist.core.operators.window.WindowImpl) MistEvent(edu.snu.mist.core.MistEvent) OutputBufferEmitter(edu.snu.mist.core.utils.OutputBufferEmitter) Test(org.junit.Test)

Example 2 with SessionWindowOperator

use of edu.snu.mist.core.operators.window.SessionWindowOperator in project mist by snuspl.

the class SessionWindowOperatorTest method testSessionWindowOperatorGetState.

/**
 * Test getting state of the SessionWindowOperator.
 */
@Test
public void testSessionWindowOperatorGetState() throws InterruptedException {
    final int sessionInterval = 500;
    // Generate the current SessionWindowOperator.
    final SessionWindowOperator<Integer> sessionWindowOperator = new SessionWindowOperator<>(sessionInterval);
    sessionWindowOperator.processLeftData(d6);
    sessionWindowOperator.processLeftWatermark(w3);
    // Generate the expected SessionWindowOperator's state.
    final Window expectedCurrentWindow = new WindowImpl<>(d6.getTimestamp(), Long.MAX_VALUE, new LinkedList<>());
    expectedCurrentWindow.putData(d6);
    expectedCurrentWindow.putWatermark(w3);
    expectedCurrentWindow.setEnd(w3.getTimestamp());
    final long expectedLatestDataTimestamp = d6.getTimestamp();
    final boolean expectedStartedNewWindow = true;
    // Get the current SessionWindowOperator's state.
    final Map<String, Object> operatorState = sessionWindowOperator.getStateSnapshot();
    final Window<Integer> currentWindow = (Window<Integer>) operatorState.get("currentWindow");
    final long latestDataTimestamp = (long) operatorState.get("latestDataTimestamp");
    final boolean startedNewWindow = (boolean) operatorState.get("startedNewWindow");
    // Compare the expected and original operator's state.
    Assert.assertEquals(expectedCurrentWindow, currentWindow);
    Assert.assertEquals(expectedLatestDataTimestamp, latestDataTimestamp);
    Assert.assertEquals(expectedStartedNewWindow, startedNewWindow);
}
Also used : SessionWindowOperator(edu.snu.mist.core.operators.window.SessionWindowOperator) Window(edu.snu.mist.core.operators.window.Window) WindowImpl(edu.snu.mist.core.operators.window.WindowImpl) Test(org.junit.Test)

Example 3 with SessionWindowOperator

use of edu.snu.mist.core.operators.window.SessionWindowOperator in project mist by snuspl.

the class SessionWindowOperatorTest method testSessionWindowOperator.

/**
 * Test whether SessionWindowOperator creates windows properly.
 * It receives some continuous data stream and groups them as a collection.
 */
@Test
public void testSessionWindowOperator() throws InterruptedException {
    final int sessionInterval = 500;
    final SessionWindowOperator<Integer> sessionWindowOperator = new SessionWindowOperator<>(sessionInterval);
    final List<MistEvent> result = new LinkedList<>();
    sessionWindowOperator.setOutputEmitter(new OutputBufferEmitter(result));
    // (200)Window1-(1200):
    // (1750)Window2-----(2000):
    // (2700)Window3-(3100):
    // (3700)Window5---: (will not be emitted)
    // d1-----d2-----w1---------d3--------d4-------d5-------d6----w2------w3-------w4:
    // expected results:
    // d1, d2, w1 in Window1
    // d3, d4, d5 in Window2
    // d6, w3 in Window3
    sessionWindowOperator.processLeftData(d1);
    sessionWindowOperator.processLeftData(d2);
    sessionWindowOperator.processLeftWatermark(w1);
    Assert.assertEquals(0, result.size());
    sessionWindowOperator.processLeftData(d3);
    Assert.assertEquals(2, result.size());
    final Collection<Integer> expectedResult1 = new LinkedList<>();
    expectedResult1.add(1);
    expectedResult1.add(2);
    OperatorTestUtils.checkWindowData(result.get(0), expectedResult1, d1.getTimestamp(), w1.getTimestamp() - d1.getTimestamp() + 1, w1.getTimestamp());
    Assert.assertEquals(w1, result.get(1));
    sessionWindowOperator.processLeftData(d4);
    sessionWindowOperator.processLeftData(d5);
    Assert.assertEquals(2, result.size());
    sessionWindowOperator.processLeftData(d6);
    Assert.assertEquals(3, result.size());
    final Collection<Integer> expectedResult2 = new LinkedList<>();
    expectedResult2.add(3);
    expectedResult2.add(4);
    expectedResult2.add(5);
    OperatorTestUtils.checkWindowData(result.get(2), expectedResult2, d3.getTimestamp(), d5.getTimestamp() - d3.getTimestamp() + 1, d5.getTimestamp());
    sessionWindowOperator.processLeftWatermark(w2);
    sessionWindowOperator.processLeftWatermark(w3);
    Assert.assertEquals(3, result.size());
    sessionWindowOperator.processLeftWatermark(w4);
    Assert.assertEquals(5, result.size());
    final Collection<Integer> expectedResult3 = new LinkedList<>();
    expectedResult3.add(6);
    OperatorTestUtils.checkWindowData(result.get(3), expectedResult3, d6.getTimestamp(), w3.getTimestamp() - d6.getTimestamp() + 1, w3.getTimestamp());
    Assert.assertEquals(w3, result.get(4));
}
Also used : SessionWindowOperator(edu.snu.mist.core.operators.window.SessionWindowOperator) OutputBufferEmitter(edu.snu.mist.core.utils.OutputBufferEmitter) MistEvent(edu.snu.mist.core.MistEvent) Test(org.junit.Test)

Aggregations

SessionWindowOperator (edu.snu.mist.core.operators.window.SessionWindowOperator)3 Test (org.junit.Test)3 MistEvent (edu.snu.mist.core.MistEvent)2 Window (edu.snu.mist.core.operators.window.Window)2 WindowImpl (edu.snu.mist.core.operators.window.WindowImpl)2 OutputBufferEmitter (edu.snu.mist.core.utils.OutputBufferEmitter)2