use of org.apache.samza.operators.triggers.Triggers in project samza by apache.
the class TestWindowOperator method testEndOfStreamFlushesWithEarlyTriggerFirings.
@Test
public void testEndOfStreamFlushesWithEarlyTriggerFirings() throws Exception {
OperatorSpecGraph sgb = this.getTumblingWindowStreamGraph(AccumulationMode.DISCARDING, Duration.ofSeconds(1), Triggers.repeat(Triggers.count(2))).getOperatorSpecGraph();
List<WindowPane<Integer, Collection<IntegerEnvelope>>> windowPanes = new ArrayList<>();
TestClock testClock = new TestClock();
StreamOperatorTask task = new StreamOperatorTask(sgb, testClock);
task.init(this.context);
MessageCollector messageCollector = envelope -> windowPanes.add((WindowPane<Integer, Collection<IntegerEnvelope>>) envelope.getMessage());
Assert.assertEquals(windowPanes.size(), 0);
List<Integer> integerList = ImmutableList.of(1, 2, 1, 2, 1);
integerList.forEach(n -> task.processAsync(new IntegerEnvelope(n), messageCollector, taskCoordinator, taskCallback));
// early triggers should emit (1,2) and (1,2) in the same window.
Assert.assertEquals(windowPanes.size(), 2);
testClock.advanceTime(Duration.ofSeconds(1));
Assert.assertEquals(windowPanes.size(), 2);
final IncomingMessageEnvelope endOfStream = IncomingMessageEnvelope.buildEndOfStreamEnvelope(new SystemStreamPartition("kafka", "integers", new Partition(0)));
task.processAsync(endOfStream, messageCollector, taskCoordinator, taskCallback);
// end of stream flushes the last entry (1)
Assert.assertEquals(windowPanes.size(), 3);
Assert.assertEquals((windowPanes.get(0).getMessage()).size(), 2);
verify(taskCoordinator, times(1)).commit(TaskCoordinator.RequestScope.CURRENT_TASK);
verify(taskCoordinator, times(1)).shutdown(TaskCoordinator.RequestScope.CURRENT_TASK);
}
Aggregations