Search in sources :

Example 1 with WatermarkImpl

use of org.apache.apex.malhar.lib.window.impl.WatermarkImpl in project apex-malhar by apache.

the class WindowedOperatorTest method testWatermarkAndAllowedLateness.

@Test
public void testWatermarkAndAllowedLateness() {
    WindowedOperatorImpl<Long, MutableLong, Long> windowedOperator = createDefaultWindowedOperator();
    CollectorTestSink controlSink = new CollectorTestSink();
    windowedOperator.controlOutput.setSink(controlSink);
    windowedOperator.setWindowOption(new WindowOption.TimeWindows(Duration.millis(1000)));
    windowedOperator.setAllowedLateness(Duration.millis(1000));
    windowedOperator.setup(testMeta.operatorContext);
    windowedOperator.beginWindow(1);
    windowedOperator.processTuple(new Tuple.TimestampedTuple<>(BASE + 100L, 2L));
    Assert.assertEquals("There should be exactly one window in the storage", 1, plainDataStorage.size());
    Assert.assertEquals("There should be exactly one window in the storage", 1, windowStateStorage.size());
    Map.Entry<Window, WindowState> entry = windowStateStorage.entries().iterator().next();
    Window window = entry.getKey();
    WindowState windowState = entry.getValue();
    Assert.assertEquals(-1, windowState.watermarkArrivalTime);
    Assert.assertEquals(2L, plainDataStorage.get(window).longValue());
    windowedOperator.processTuple(new Tuple.TimestampedTuple<>(BASE + 200L, 3L));
    Assert.assertEquals(5L, plainDataStorage.get(window).longValue());
    windowedOperator.processWatermark(new WatermarkImpl(BASE + 1200));
    windowedOperator.endWindow();
    Assert.assertTrue(windowState.watermarkArrivalTime >= 0);
    Assert.assertEquals("We should get one watermark tuple", 1, controlSink.getCount(false));
    windowedOperator.beginWindow(2);
    windowedOperator.processTuple(new Tuple.TimestampedTuple<>(BASE + 900L, 4L));
    Assert.assertEquals("Late but not too late", 9L, plainDataStorage.get(window).longValue());
    windowedOperator.processWatermark(new WatermarkImpl(BASE + 3000));
    windowedOperator.endWindow();
    Assert.assertEquals("We should get two watermark tuples", 2, controlSink.getCount(false));
    windowedOperator.beginWindow(3);
    // this tuple should be dropped
    windowedOperator.processTuple(new Tuple.TimestampedTuple<>(BASE + 120L, 5L));
    Assert.assertEquals("The window should be dropped because it's too late", 0, plainDataStorage.size());
    Assert.assertEquals("The window should be dropped because it's too late", 0, windowStateStorage.size());
    windowedOperator.endWindow();
    windowedOperator.teardown();
}
Also used : MutableLong(org.apache.commons.lang3.mutable.MutableLong) WatermarkImpl(org.apache.apex.malhar.lib.window.impl.WatermarkImpl) MutableLong(org.apache.commons.lang3.mutable.MutableLong) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) CollectorTestSink(org.apache.apex.malhar.lib.testbench.CollectorTestSink) Test(org.junit.Test)

Aggregations

HashMap (java.util.HashMap)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 CollectorTestSink (org.apache.apex.malhar.lib.testbench.CollectorTestSink)1 WatermarkImpl (org.apache.apex.malhar.lib.window.impl.WatermarkImpl)1 MutableLong (org.apache.commons.lang3.mutable.MutableLong)1 Test (org.junit.Test)1