@Test public void testValidOutputTimes() throws Exception { for (long timestamp : Arrays.asList(200, 800, 700)) { WindowFnTestUtils.validateGetOutputTimestamp(FixedWindows.of(new Duration(500)), timestamp); } }
void checkConstructionFailure(int size, int offset) { try { FixedWindows.of(Duration.standardSeconds(size)).withOffset(Duration.standardSeconds(offset)); fail("should have failed"); } catch (IllegalArgumentException e) { assertThat( e.toString(), containsString("FixedWindows WindowingStrategies must have 0 <= offset < size")); } }
@Test public void testVerifyCompatibility() throws IncompatibleWindowException { FixedWindows.of(new Duration(10)).verifyCompatibility(FixedWindows.of(new Duration(10))); thrown.expect(IncompatibleWindowException.class); FixedWindows.of(new Duration(10)).verifyCompatibility(FixedWindows.of(new Duration(20))); }
@Test public void testEquality() { assertTrue(FixedWindows.of(new Duration(10)).isCompatible(FixedWindows.of(new Duration(10)))); assertTrue(FixedWindows.of(new Duration(10)).isCompatible(FixedWindows.of(new Duration(10)))); assertTrue(FixedWindows.of(new Duration(10)).isCompatible(FixedWindows.of(new Duration(10)))); assertFalse(FixedWindows.of(new Duration(10)).isCompatible(FixedWindows.of(new Duration(20)))); assertFalse(FixedWindows.of(new Duration(10)).isCompatible(FixedWindows.of(new Duration(20)))); }
/** Tests that the last hour of the universe in fact ends at the end of time. */ @Test public void testEndOfTime() { Instant endOfGlobalWindow = GlobalWindow.INSTANCE.maxTimestamp(); FixedWindows windowFn = FixedWindows.of(Duration.standardHours(1)); IntervalWindow truncatedWindow = windowFn.assignWindow(endOfGlobalWindow.minus(1)); assertThat(truncatedWindow.maxTimestamp(), equalTo(endOfGlobalWindow)); }
@Test public void garbageCollectionTimeAfterEndOfGlobalWindow() { FixedWindows windowFn = FixedWindows.of(Duration.standardMinutes(5)); WindowingStrategy<?, ?> strategy = WindowingStrategy.globalDefault().withWindowFn(windowFn); IntervalWindow window = windowFn.assignWindow(new Instant(BoundedWindow.TIMESTAMP_MAX_VALUE)); assertThat(window.maxTimestamp(), equalTo(GlobalWindow.INSTANCE.maxTimestamp())); assertThat( LateDataUtils.garbageCollectionTime(window, strategy), equalTo(GlobalWindow.INSTANCE.maxTimestamp())); }
/** * Applies a window to the input collection if one hasn't already been specified. * * @return the input collection if it already has been windowed, otherwise a the same collection inside a default * window. */ public static <T> PCollection<T> ofDefaultWindow(PCollection<T> in) { if (in.getWindowingStrategy() != WindowingStrategy.globalDefault() && in.getWindowingStrategy() != null) return in; return in.apply("ApplyDefaultWindow", Window.<T> into(FixedWindows.of(DEFAULT_WINDOW_SIZE))); }
FixedWindows fixedWindows = FixedWindows.of(durationParameter(parameters, 1)); if (parameters.size() == 3) { fixedWindows = fixedWindows.withOffset(durationParameter(parameters, 2));
@Test public void beforeEndOfGlobalWindowSame() { FixedWindows windowFn = FixedWindows.of(Duration.standardMinutes(5)); Duration allowedLateness = Duration.standardMinutes(2); WindowingStrategy<?, ?> strategy = WindowingStrategy.globalDefault() .withWindowFn(windowFn) .withAllowedLateness(allowedLateness); IntervalWindow window = windowFn.assignWindow(new Instant(10)); assertThat( LateDataUtils.garbageCollectionTime(window, strategy), equalTo(window.maxTimestamp().plus(allowedLateness))); }
/** * Applies a window to the input collection if one hasn't already been specified. * * @return the input collection if it already has been windowed, otherwise a the same collection inside a default * window. */ public static <T> PCollection<T> ofDefaultWindow(PCollection<T> in) { if (in.getWindowingStrategy() != WindowingStrategy.globalDefault() && in.getWindowingStrategy() != null) return in; return in.apply("ApplyDefaultWindow", Window.<T> into(FixedWindows.of(DEFAULT_WINDOW_SIZE))); }
@Test public void testDisplayData() { Duration offset = Duration.standardSeconds(1234); Duration size = Duration.standardSeconds(2345); FixedWindows fixedWindows = FixedWindows.of(size).withOffset(offset); DisplayData displayData = DisplayData.from(fixedWindows); assertThat(displayData, hasDisplayItem("size", size)); assertThat(displayData, hasDisplayItem("offset", offset)); } }
@Test public void garbageCollectionTimeAfterEndOfGlobalWindowWithLateness() { FixedWindows windowFn = FixedWindows.of(Duration.standardMinutes(5)); Duration allowedLateness = Duration.millis(Long.MAX_VALUE); WindowingStrategy<?, ?> strategy = WindowingStrategy.globalDefault() .withWindowFn(windowFn) .withAllowedLateness(allowedLateness); IntervalWindow window = windowFn.assignWindow(new Instant(-100)); assertThat( window.maxTimestamp().plus(allowedLateness), Matchers.greaterThan(GlobalWindow.INSTANCE.maxTimestamp())); assertThat( LateDataUtils.garbageCollectionTime(window, strategy), equalTo(GlobalWindow.INSTANCE.maxTimestamp())); } }
@Test @Category({NeedsRunner.class, UsesTestStream.class}) public void testWaitWithSomeSignalWindowsEmpty() { testWaitWithParameters( Duration.standardMinutes(1) /* duration */, Duration.ZERO /* lateness */, 20 /* numMainElements */, FixedWindows.of(Duration.standardSeconds(1)), 10 /* numSignalElements */, FixedWindows.of(Duration.standardSeconds(1))); }
@Test public void testFixedOffsetWindow() throws Exception { Map<IntervalWindow, Set<String>> expected = new HashMap<>(); expected.put(new IntervalWindow(new Instant(-5), new Instant(5)), set(1, 2)); expected.put(new IntervalWindow(new Instant(5), new Instant(15)), set(5, 9, 10, 11)); expected.put(new IntervalWindow(new Instant(95), new Instant(105)), set(100)); assertEquals( expected, runWindowFn( FixedWindows.of(new Duration(10)).withOffset(new Duration(5)), Arrays.asList(1L, 2L, 5L, 9L, 10L, 11L, 100L))); }
/** Tests that onElement correctly passes the data on to the subtrigger. */ @Test public void testOnElement() throws Exception { setUp(FixedWindows.of(Duration.millis(10))); tester.injectElements(37); verify(mockTrigger).onElement(Mockito.any()); }
@Test public void testTimeUnit() throws Exception { Map<IntervalWindow, Set<String>> expected = new HashMap<>(); expected.put(new IntervalWindow(new Instant(-5000), new Instant(5000)), set(1, 2, 1000)); expected.put(new IntervalWindow(new Instant(5000), new Instant(15000)), set(5000, 5001, 10000)); assertEquals( expected, runWindowFn( FixedWindows.of(Duration.standardSeconds(10)).withOffset(Duration.standardSeconds(5)), Arrays.asList(1L, 2L, 1000L, 5000L, 5001L, 10000L))); }
@Test public void testVerifyCompatibility() throws IncompatibleWindowException { Sessions.withGapDuration(new Duration(10)) .verifyCompatibility(Sessions.withGapDuration(new Duration(10))); thrown.expect(IncompatibleWindowException.class); Sessions.withGapDuration(new Duration(10)) .verifyCompatibility(FixedWindows.of(new Duration(10))); }