@Test public void testContinuation() throws Exception { assertEquals( AfterPane.elementCountAtLeast(1), AfterPane.elementCountAtLeast(100).getContinuationTrigger()); assertEquals( AfterPane.elementCountAtLeast(1), AfterPane.elementCountAtLeast(100).getContinuationTrigger().getContinuationTrigger()); }
@Test public void testAfterPaneWithFixedWindow() throws Exception { Duration windowDuration = Duration.millis(10); ReduceFnTester<Integer, Iterable<Integer>, IntervalWindow> tester = ReduceFnTester.nonCombining( FixedWindows.of(windowDuration), AfterPane.<IntervalWindow>elementCountAtLeast(2), AccumulationMode.DISCARDING_FIRED_PANES, Duration.millis(100)); tester.injectElements( TimestampedValue.of(1, new Instant(1)), // first in window [0, 10) TimestampedValue.of(2, new Instant(9))); assertThat( tester.extractOutput(), Matchers.contains( WindowMatchers.isSingleWindowedValue(Matchers.containsInAnyOrder(1, 2), 1, 0, 10))); // This element belongs in the window that has already fired. It should not be re-output because // that trigger (which was one-time) has already gone off. tester.injectElements(TimestampedValue.of(6, new Instant(2))); assertThat(tester.extractOutput(), Matchers.emptyIterable()); assertTrue(tester.isMarkedFinished(new IntervalWindow(new Instant(0), new Instant(10)))); assertFalse(tester.isMarkedFinished(new IntervalWindow(new Instant(10), new Instant(20)))); tester.assertHasOnlyGlobalAndFinishedSetsFor( new IntervalWindow(new Instant(0), new Instant(10))); }
@Test public void testAfterPaneWithGlobalWindowsAndCombining() throws Exception { Duration windowDuration = Duration.millis(10); ReduceFnTester<Integer, Integer, IntervalWindow> tester = ReduceFnTester.combining( FixedWindows.of(windowDuration), AfterPane.<IntervalWindow>elementCountAtLeast(2), AccumulationMode.DISCARDING_FIRED_PANES, new SumIntegerFn().<String>asKeyedFn(), VarIntCoder.of(), Duration.millis(100)); tester.injectElements( TimestampedValue.of(1, new Instant(1)), TimestampedValue.of(2, new Instant(9))); assertThat( tester.extractOutput(), Matchers.contains(WindowMatchers.isSingleWindowedValue(Matchers.equalTo(3), 1, 0, 10))); // This element should not be output because that trigger (which was one-time) has already // gone off. tester.injectElements(TimestampedValue.of(6, new Instant(2))); assertThat(tester.extractOutput(), Matchers.emptyIterable()); assertTrue(tester.isMarkedFinished(new IntervalWindow(new Instant(0), new Instant(10)))); assertFalse(tester.isMarkedFinished(new IntervalWindow(new Instant(10), new Instant(20)))); tester.assertHasOnlyGlobalAndFinishedSetsFor( new IntervalWindow(new Instant(0), new Instant(10))); }
@Test public void testFireDeadline() throws Exception { assertEquals( BoundedWindow.TIMESTAMP_MAX_VALUE, AfterPane.elementCountAtLeast(1) .getWatermarkThatGuaranteesFiring(new IntervalWindow(new Instant(0), new Instant(10)))); }
/** * Tests that if the EOW is finished in both as well as the merged window, then it is finished in * the merged result. * * <p>Because windows are discarded when a trigger finishes, we need to embed this in a sequence * in order to check that it is re-activated. So this test is potentially sensitive to other * triggers' correctness. */ @Test public void testEarlyAndLateOnMergeAlreadyFinished() throws Exception { tester = TriggerTester.forTrigger( AfterWatermark.<IntervalWindow>pastEndOfWindow() .withEarlyFirings(AfterPane.<IntervalWindow>elementCountAtLeast(100)) .withLateFirings(AfterPane.<IntervalWindow>elementCountAtLeast(1)), Sessions.withGapDuration(Duration.millis(10))); tester.injectElements(1); tester.injectElements(5); IntervalWindow firstWindow = new IntervalWindow(new Instant(1), new Instant(11)); IntervalWindow secondWindow = new IntervalWindow(new Instant(5), new Instant(15)); IntervalWindow mergedWindow = new IntervalWindow(new Instant(1), new Instant(15)); // Finish the AfterWatermark.pastEndOfWindow() bit of the trigger in both windows tester.advanceInputWatermark(new Instant(15)); assertTrue(tester.shouldFire(firstWindow)); assertTrue(tester.shouldFire(secondWindow)); tester.fireIfShouldFire(firstWindow); tester.fireIfShouldFire(secondWindow); // Confirm that we are on the late trigger by probing assertFalse(tester.shouldFire(firstWindow)); assertFalse(tester.shouldFire(secondWindow)); tester.injectElements(1); tester.injectElements(5); assertTrue(tester.shouldFire(firstWindow)); assertTrue(tester.shouldFire(secondWindow)); tester.fireIfShouldFire(firstWindow); tester.fireIfShouldFire(secondWindow); // Merging should leave it on the late trigger tester.mergeWindows(); // Confirm that we are on the late trigger by probing assertFalse(tester.shouldFire(mergedWindow)); tester.injectElements(1); assertTrue(tester.shouldFire(mergedWindow)); }
/** * Tests that the trigger rewinds to be non-finished in the merged window. * * <p>Because windows are discarded when a trigger finishes, we need to embed this in a sequence * in order to check that it is re-activated. So this test is potentially sensitive to other * triggers' correctness. */ @Test public void testOnMergeRewinds() throws Exception { tester = TriggerTester.forTrigger( AfterEach.inOrder( AfterWatermark.<IntervalWindow>pastEndOfWindow(), Repeatedly.forever(AfterPane.<IntervalWindow>elementCountAtLeast(1))), Sessions.withGapDuration(Duration.millis(10))); tester.injectElements(1); tester.injectElements(5); IntervalWindow firstWindow = new IntervalWindow(new Instant(1), new Instant(11)); IntervalWindow secondWindow = new IntervalWindow(new Instant(5), new Instant(15)); IntervalWindow mergedWindow = new IntervalWindow(new Instant(1), new Instant(15)); // Finish the AfterWatermark.pastEndOfWindow() trigger in only the first window tester.advanceInputWatermark(new Instant(11)); assertTrue(tester.shouldFire(firstWindow)); assertFalse(tester.shouldFire(secondWindow)); tester.fireIfShouldFire(firstWindow); // Confirm that we are on the second trigger by probing assertFalse(tester.shouldFire(firstWindow)); tester.injectElements(1); assertTrue(tester.shouldFire(firstWindow)); tester.fireIfShouldFire(firstWindow); // Merging should re-activate the watermark trigger in the merged window tester.mergeWindows(); // Confirm that we are not on the second trigger by probing assertFalse(tester.shouldFire(mergedWindow)); tester.injectElements(1); assertFalse(tester.shouldFire(mergedWindow)); // And confirm that advancing the watermark fires again tester.advanceInputWatermark(new Instant(15)); assertTrue(tester.shouldFire(mergedWindow)); }
@Test public void testAfterPaneWithMerging() throws Exception { Duration windowDuration = Duration.millis(10); ReduceFnTester<Integer, Iterable<Integer>, IntervalWindow> tester = ReduceFnTester.nonCombining( Sessions.withGapDuration(windowDuration), AfterPane.<IntervalWindow>elementCountAtLeast(2), AccumulationMode.DISCARDING_FIRED_PANES, Duration.millis(100)); assertThat(tester.extractOutput(), Matchers.emptyIterable()); tester.injectElements( TimestampedValue.of(1, new Instant(1)), // in [1, 11) TimestampedValue.of(2, new Instant(2))); // in [2, 12) assertThat( tester.extractOutput(), Matchers.contains( WindowMatchers.isSingleWindowedValue(Matchers.containsInAnyOrder(1, 2), 1, 1, 12))); // Because we closed the previous window, we don't have it around to merge with. tester.injectElements( TimestampedValue.of(3, new Instant(7)), // in [7, 17) TimestampedValue.of(4, new Instant(8))); // in [8, 18) assertThat( tester.extractOutput(), Matchers.contains( WindowMatchers.isSingleWindowedValue(Matchers.containsInAnyOrder(3, 4), 7, 7, 18))); assertTrue(tester.isMarkedFinished(new IntervalWindow(new Instant(1), new Instant(12)))); tester.assertHasOnlyGlobalAndFinishedSetsFor( new IntervalWindow(new Instant(1), new Instant(12)), new IntervalWindow(new Instant(7), new Instant(18))); }