예제 #1
0
  @Test
  public void testDefaultTriggerWithSessionWindow() throws Exception {
    ReduceFnTester<Integer, Iterable<Integer>, IntervalWindow> tester =
        ReduceFnTester.nonCombining(
            Sessions.withGapDuration(Duration.millis(10)),
            DefaultTrigger.<IntervalWindow>of(),
            AccumulationMode.DISCARDING_FIRED_PANES,
            Duration.millis(100));

    tester.injectElements(
        TimestampedValue.of(1, new Instant(1)), TimestampedValue.of(2, new Instant(9)));

    // no output, because we merged into the [9-19) session
    tester.advanceInputWatermark(new Instant(10));
    assertThat(tester.extractOutput(), Matchers.emptyIterable());

    tester.injectElements(
        TimestampedValue.of(3, new Instant(15)), TimestampedValue.of(4, new Instant(30)));

    tester.advanceInputWatermark(new Instant(100));
    assertThat(
        tester.extractOutput(),
        Matchers.contains(
            isSingleWindowedValue(Matchers.containsInAnyOrder(1, 2, 3), 1, 1, 25),
            isSingleWindowedValue(Matchers.contains(4), 30, 30, 40)));
    assertFalse(tester.isMarkedFinished(new IntervalWindow(new Instant(1), new Instant(25))));
    assertFalse(tester.isMarkedFinished(new IntervalWindow(new Instant(30), new Instant(40))));
    tester.assertHasOnlyGlobalAndPaneInfoFor(
        new IntervalWindow(new Instant(1), new Instant(25)),
        new IntervalWindow(new Instant(30), new Instant(40)));
  }
예제 #2
0
  @Test
  public void testDefaultTriggerWithContainedSessionWindow() throws Exception {
    ReduceFnTester<Integer, Iterable<Integer>, IntervalWindow> tester =
        ReduceFnTester.nonCombining(
            Sessions.withGapDuration(Duration.millis(10)),
            DefaultTrigger.<IntervalWindow>of(),
            AccumulationMode.DISCARDING_FIRED_PANES,
            Duration.millis(100));

    tester.injectElements(
        TimestampedValue.of(1, new Instant(1)),
        TimestampedValue.of(2, new Instant(9)),
        TimestampedValue.of(3, new Instant(7)));

    tester.advanceInputWatermark(new Instant(20));
    Iterable<WindowedValue<Iterable<Integer>>> extractOutput = tester.extractOutput();
    assertThat(
        extractOutput,
        Matchers.contains(isSingleWindowedValue(Matchers.containsInAnyOrder(1, 2, 3), 1, 1, 19)));
    tester.assertHasOnlyGlobalAndPaneInfoFor(new IntervalWindow(new Instant(1), new Instant(19)));
  }
  /**
   * 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));
  }
예제 #5
0
  @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)));
  }