diff --git a/CHANGES.md b/CHANGES.md index cacff24ea2ab..eb8d107f983c 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -152,6 +152,7 @@ ## Deprecations * X behavior is deprecated and will be removed in X versions ([#X](https://github.com/apache/beam/issues/X)). +* (Java) `WindowFnTestUtils.runWindowFn`, `runWindowFnWithValue` and `set` are deprecated; use `WindowFnTestUtils.assignedWindows` instead ([#18458](https://github.com/apache/beam/issues/18458)). ## Bugfixes diff --git a/sdks/java/core/src/main/java/org/apache/beam/sdk/testing/WindowFnTestUtils.java b/sdks/java/core/src/main/java/org/apache/beam/sdk/testing/WindowFnTestUtils.java index c77c10c8faaf..50333d74cb24 100644 --- a/sdks/java/core/src/main/java/org/apache/beam/sdk/testing/WindowFnTestUtils.java +++ b/sdks/java/core/src/main/java/org/apache/beam/sdk/testing/WindowFnTestUtils.java @@ -43,7 +43,12 @@ /** A utility class for testing {@link WindowFn}s. */ public class WindowFnTestUtils { - /** Creates a Set of elements to be used as expected output in {@link #runWindowFn}. */ + /** + * Creates a Set of elements to be used as expected output in {@link #runWindowFn}. + * + * @deprecated use {@link #assignedWindows} and assert on the windows of each timestamp. + */ + @Deprecated public static Set set(long... timestamps) { Set result = new HashSet<>(); for (long timestamp : timestamps) { @@ -55,7 +60,10 @@ public static Set set(long... timestamps) { /** * Runs the {@link WindowFn} over the provided input, returning a map of windows to the timestamps * in those windows. + * + * @deprecated use {@link #assignedWindows} and assert on the windows of each timestamp. */ + @Deprecated public static Map> runWindowFn( WindowFn windowFn, List timestamps) throws Exception { List> timestampedValues = new ArrayList<>(); @@ -69,7 +77,10 @@ public static Map> runWindowFn( * Runs the {@link WindowFn} over the provided input, returning a map of windows to the timestamps * in those windows. This version allows to pass a list of {@link TimestampedValue} in case the * values are used to assign windows. + * + * @deprecated use {@link #assignedWindowsWithValue} and assert on the windows of each element. */ + @Deprecated public static Map> runWindowFnWithValue( WindowFn windowFn, List> timestampedValues) throws Exception { diff --git a/sdks/java/core/src/test/java/org/apache/beam/sdk/transforms/windowing/CalendarWindowsTest.java b/sdks/java/core/src/test/java/org/apache/beam/sdk/transforms/windowing/CalendarWindowsTest.java index c6af62eacc9b..f27413638cfd 100644 --- a/sdks/java/core/src/test/java/org/apache/beam/sdk/transforms/windowing/CalendarWindowsTest.java +++ b/sdks/java/core/src/test/java/org/apache/beam/sdk/transforms/windowing/CalendarWindowsTest.java @@ -17,18 +17,12 @@ */ package org.apache.beam.sdk.transforms.windowing; -import static org.apache.beam.sdk.testing.WindowFnTestUtils.runWindowFn; -import static org.apache.beam.sdk.testing.WindowFnTestUtils.set; +import static org.apache.beam.sdk.testing.WindowFnTestUtils.assignedWindows; import static org.apache.beam.sdk.transforms.display.DisplayDataMatchers.hasDisplayItem; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.equalTo; -import static org.junit.Assert.assertEquals; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Set; import org.apache.beam.sdk.transforms.display.DisplayData; import org.apache.beam.sdk.transforms.windowing.CalendarWindows.MonthsWindows; import org.joda.time.DateTime; @@ -51,32 +45,36 @@ private static Instant makeTimestamp(int year, int month, int day, int hours, in return new DateTime(year, month, day, hours, minutes, DateTimeZone.UTC).toInstant(); } + /** Asserts that {@code windowFn} assigns each of {@code timestamps} to exactly {@code window}. */ + private static void assertWindow( + WindowFn windowFn, IntervalWindow window, Instant... timestamps) + throws Exception { + for (Instant timestamp : timestamps) { + assertThat( + "windows for timestamp " + timestamp, + assignedWindows(windowFn, timestamp.getMillis()), + contains(window)); + } + } + @Test public void testDays() throws Exception { - Map> expected = new HashMap<>(); - - final List timestamps = - Arrays.asList( - makeTimestamp(2014, 1, 1, 0, 0).getMillis(), - makeTimestamp(2014, 1, 1, 23, 59).getMillis(), - makeTimestamp(2014, 1, 2, 0, 0).getMillis(), - makeTimestamp(2014, 1, 2, 5, 5).getMillis(), - makeTimestamp(2015, 1, 1, 0, 0).getMillis(), - makeTimestamp(2015, 1, 1, 5, 5).getMillis()); - - expected.put( + WindowFn windowFn = CalendarWindows.days(1); + assertWindow( + windowFn, new IntervalWindow(makeTimestamp(2014, 1, 1, 0, 0), makeTimestamp(2014, 1, 2, 0, 0)), - set(timestamps.get(0), timestamps.get(1))); - - expected.put( + makeTimestamp(2014, 1, 1, 0, 0), + makeTimestamp(2014, 1, 1, 23, 59)); + assertWindow( + windowFn, new IntervalWindow(makeTimestamp(2014, 1, 2, 0, 0), makeTimestamp(2014, 1, 3, 0, 0)), - set(timestamps.get(2), timestamps.get(3))); - - expected.put( + makeTimestamp(2014, 1, 2, 0, 0), + makeTimestamp(2014, 1, 2, 5, 5)); + assertWindow( + windowFn, new IntervalWindow(makeTimestamp(2015, 1, 1, 0, 0), makeTimestamp(2015, 1, 2, 0, 0)), - set(timestamps.get(4), timestamps.get(5))); - - assertEquals(expected, runWindowFn(CalendarWindows.days(1), timestamps)); + makeTimestamp(2015, 1, 1, 0, 0), + makeTimestamp(2015, 1, 1, 5, 5)); } @Test @@ -89,59 +87,43 @@ public void testDaysCompatibility() throws IncompatibleWindowException { @Test public void testWeeks() throws Exception { - Map> expected = new HashMap<>(); - - final List timestamps = - Arrays.asList( - makeTimestamp(2014, 1, 1, 0, 0).getMillis(), - makeTimestamp(2014, 1, 5, 5, 5).getMillis(), - makeTimestamp(2014, 1, 8, 0, 0).getMillis(), - makeTimestamp(2014, 1, 12, 5, 5).getMillis(), - makeTimestamp(2015, 1, 1, 0, 0).getMillis(), - makeTimestamp(2015, 1, 6, 5, 5).getMillis()); - - expected.put( + WindowFn windowFn = + CalendarWindows.weeks(1, DateTimeConstants.WEDNESDAY); + assertWindow( + windowFn, new IntervalWindow(makeTimestamp(2014, 1, 1, 0, 0), makeTimestamp(2014, 1, 8, 0, 0)), - set(timestamps.get(0), timestamps.get(1))); - - expected.put( + makeTimestamp(2014, 1, 1, 0, 0), + makeTimestamp(2014, 1, 5, 5, 5)); + assertWindow( + windowFn, new IntervalWindow(makeTimestamp(2014, 1, 8, 0, 0), makeTimestamp(2014, 1, 15, 0, 0)), - set(timestamps.get(2), timestamps.get(3))); - - expected.put( + makeTimestamp(2014, 1, 8, 0, 0), + makeTimestamp(2014, 1, 12, 5, 5)); + assertWindow( + windowFn, new IntervalWindow(makeTimestamp(2014, 12, 31, 0, 0), makeTimestamp(2015, 1, 7, 0, 0)), - set(timestamps.get(4), timestamps.get(5))); - - assertEquals( - expected, runWindowFn(CalendarWindows.weeks(1, DateTimeConstants.WEDNESDAY), timestamps)); + makeTimestamp(2015, 1, 1, 0, 0), + makeTimestamp(2015, 1, 6, 5, 5)); } @Test public void testMonths() throws Exception { - Map> expected = new HashMap<>(); - - final List timestamps = - Arrays.asList( - makeTimestamp(2014, 1, 1, 0, 0).getMillis(), - makeTimestamp(2014, 1, 31, 5, 5).getMillis(), - makeTimestamp(2014, 2, 1, 0, 0).getMillis(), - makeTimestamp(2014, 2, 15, 5, 5).getMillis(), - makeTimestamp(2015, 1, 1, 0, 0).getMillis(), - makeTimestamp(2015, 1, 31, 5, 5).getMillis()); - - expected.put( + WindowFn windowFn = CalendarWindows.months(1); + assertWindow( + windowFn, new IntervalWindow(makeTimestamp(2014, 1, 1, 0, 0), makeTimestamp(2014, 2, 1, 0, 0)), - set(timestamps.get(0), timestamps.get(1))); - - expected.put( + makeTimestamp(2014, 1, 1, 0, 0), + makeTimestamp(2014, 1, 31, 5, 5)); + assertWindow( + windowFn, new IntervalWindow(makeTimestamp(2014, 2, 1, 0, 0), makeTimestamp(2014, 3, 1, 0, 0)), - set(timestamps.get(2), timestamps.get(3))); - - expected.put( + makeTimestamp(2014, 2, 1, 0, 0), + makeTimestamp(2014, 2, 15, 5, 5)); + assertWindow( + windowFn, new IntervalWindow(makeTimestamp(2015, 1, 1, 0, 0), makeTimestamp(2015, 2, 1, 0, 0)), - set(timestamps.get(4), timestamps.get(5))); - - assertEquals(expected, runWindowFn(CalendarWindows.months(1), timestamps)); + makeTimestamp(2015, 1, 1, 0, 0), + makeTimestamp(2015, 1, 31, 5, 5)); } @Test @@ -154,64 +136,44 @@ public void testMonthsCompatibility() throws IncompatibleWindowException { @Test public void testMultiMonths() throws Exception { - Map> expected = new HashMap<>(); - - final List timestamps = - Arrays.asList( - makeTimestamp(2014, 3, 5, 0, 0).getMillis(), - makeTimestamp(2014, 10, 4, 23, 59).getMillis(), - makeTimestamp(2014, 10, 5, 0, 0).getMillis(), - makeTimestamp(2015, 3, 1, 0, 0).getMillis(), - makeTimestamp(2016, 1, 5, 0, 0).getMillis(), - makeTimestamp(2016, 1, 31, 5, 5).getMillis()); - - expected.put( + WindowFn windowFn = + CalendarWindows.months(7).withStartingMonth(2014, 3).beginningOnDay(5); + assertWindow( + windowFn, new IntervalWindow(makeTimestamp(2014, 3, 5, 0, 0), makeTimestamp(2014, 10, 5, 0, 0)), - set(timestamps.get(0), timestamps.get(1))); - - expected.put( + makeTimestamp(2014, 3, 5, 0, 0), + makeTimestamp(2014, 10, 4, 23, 59)); + assertWindow( + windowFn, new IntervalWindow(makeTimestamp(2014, 10, 5, 0, 0), makeTimestamp(2015, 5, 5, 0, 0)), - set(timestamps.get(2), timestamps.get(3))); - - expected.put( + makeTimestamp(2014, 10, 5, 0, 0), + makeTimestamp(2015, 3, 1, 0, 0)); + assertWindow( + windowFn, new IntervalWindow(makeTimestamp(2015, 12, 5, 0, 0), makeTimestamp(2016, 7, 5, 0, 0)), - set(timestamps.get(4), timestamps.get(5))); - - assertEquals( - expected, - runWindowFn( - CalendarWindows.months(7).withStartingMonth(2014, 3).beginningOnDay(5), timestamps)); + makeTimestamp(2016, 1, 5, 0, 0), + makeTimestamp(2016, 1, 31, 5, 5)); } @Test public void testYears() throws Exception { - Map> expected = new HashMap<>(); - - final List timestamps = - Arrays.asList( - makeTimestamp(2000, 5, 5, 0, 0).getMillis(), - makeTimestamp(2010, 5, 4, 23, 59).getMillis(), - makeTimestamp(2010, 5, 5, 0, 0).getMillis(), - makeTimestamp(2015, 3, 1, 0, 0).getMillis(), - makeTimestamp(2052, 1, 5, 0, 0).getMillis(), - makeTimestamp(2060, 5, 4, 5, 5).getMillis()); - - expected.put( + WindowFn windowFn = + CalendarWindows.years(10).withStartingYear(2000).beginningOnDay(5, 5); + assertWindow( + windowFn, new IntervalWindow(makeTimestamp(2000, 5, 5, 0, 0), makeTimestamp(2010, 5, 5, 0, 0)), - set(timestamps.get(0), timestamps.get(1))); - - expected.put( + makeTimestamp(2000, 5, 5, 0, 0), + makeTimestamp(2010, 5, 4, 23, 59)); + assertWindow( + windowFn, new IntervalWindow(makeTimestamp(2010, 5, 5, 0, 0), makeTimestamp(2020, 5, 5, 0, 0)), - set(timestamps.get(2), timestamps.get(3))); - - expected.put( + makeTimestamp(2010, 5, 5, 0, 0), + makeTimestamp(2015, 3, 1, 0, 0)); + assertWindow( + windowFn, new IntervalWindow(makeTimestamp(2050, 5, 5, 0, 0), makeTimestamp(2060, 5, 5, 0, 0)), - set(timestamps.get(4), timestamps.get(5))); - - assertEquals( - expected, - runWindowFn( - CalendarWindows.years(10).withStartingYear(2000).beginningOnDay(5, 5), timestamps)); + makeTimestamp(2052, 1, 5, 0, 0), + makeTimestamp(2060, 5, 4, 5, 5)); } @Test @@ -224,30 +186,22 @@ public void testYearsCompatibility() throws IncompatibleWindowException { @Test public void testTimeZone() throws Exception { - Map> expected = new HashMap<>(); - DateTimeZone timeZone = DateTimeZone.forID("America/Los_Angeles"); - - final List timestamps = - Arrays.asList( - new DateTime(2014, 1, 1, 0, 0, timeZone).getMillis(), - new DateTime(2014, 1, 1, 23, 59, timeZone).getMillis(), - new DateTime(2014, 1, 2, 8, 0, DateTimeZone.UTC).getMillis(), - new DateTime(2014, 1, 3, 7, 59, DateTimeZone.UTC).getMillis()); - - expected.put( + WindowFn windowFn = CalendarWindows.days(1).withTimeZone(timeZone); + assertWindow( + windowFn, new IntervalWindow( new DateTime(2014, 1, 1, 0, 0, timeZone).toInstant(), new DateTime(2014, 1, 2, 0, 0, timeZone).toInstant()), - set(timestamps.get(0), timestamps.get(1))); - - expected.put( + new DateTime(2014, 1, 1, 0, 0, timeZone).toInstant(), + new DateTime(2014, 1, 1, 23, 59, timeZone).toInstant()); + assertWindow( + windowFn, new IntervalWindow( new DateTime(2014, 1, 2, 0, 0, timeZone).toInstant(), new DateTime(2014, 1, 3, 0, 0, timeZone).toInstant()), - set(timestamps.get(2), timestamps.get(3))); - - assertEquals(expected, runWindowFn(CalendarWindows.days(1).withTimeZone(timeZone), timestamps)); + new DateTime(2014, 1, 2, 8, 0, DateTimeZone.UTC).toInstant(), + new DateTime(2014, 1, 3, 7, 59, DateTimeZone.UTC).toInstant()); } @Test diff --git a/sdks/java/core/src/test/java/org/apache/beam/sdk/transforms/windowing/FixedWindowsTest.java b/sdks/java/core/src/test/java/org/apache/beam/sdk/transforms/windowing/FixedWindowsTest.java index c009e05023ac..0cfff8aa5375 100644 --- a/sdks/java/core/src/test/java/org/apache/beam/sdk/transforms/windowing/FixedWindowsTest.java +++ b/sdks/java/core/src/test/java/org/apache/beam/sdk/transforms/windowing/FixedWindowsTest.java @@ -17,21 +17,16 @@ */ package org.apache.beam.sdk.transforms.windowing; -import static org.apache.beam.sdk.testing.WindowFnTestUtils.runWindowFn; -import static org.apache.beam.sdk.testing.WindowFnTestUtils.set; +import static org.apache.beam.sdk.testing.WindowFnTestUtils.assignedWindows; import static org.apache.beam.sdk.transforms.display.DisplayDataMatchers.hasDisplayItem; import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.equalTo; -import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; -import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; -import java.util.Set; import org.apache.beam.sdk.transforms.display.DisplayData; import org.joda.time.Duration; import org.joda.time.Instant; @@ -46,41 +41,54 @@ public class FixedWindowsTest { @Rule public ExpectedException thrown = ExpectedException.none(); + private static IntervalWindow window(long start, long end) { + return new IntervalWindow(new Instant(start), new Instant(end)); + } + + /** Asserts that {@code windowFn} assigns {@code timestamp} to exactly {@code windows}. */ + private static void assertWindows( + WindowFn windowFn, long timestamp, IntervalWindow... windows) + throws Exception { + assertThat( + "windows for timestamp " + timestamp, + assignedWindows(windowFn, timestamp), + containsInAnyOrder(windows)); + } + @Test public void testSimpleFixedWindow() throws Exception { - Map> expected = new HashMap<>(); - expected.put(new IntervalWindow(new Instant(0), new Instant(10)), set(1, 2, 5, 9)); - expected.put(new IntervalWindow(new Instant(10), new Instant(20)), set(10, 11)); - expected.put(new IntervalWindow(new Instant(100), new Instant(110)), set(100)); - assertEquals( - expected, - runWindowFn( - FixedWindows.of(Duration.millis(10)), Arrays.asList(1L, 2L, 5L, 9L, 10L, 11L, 100L))); + FixedWindows windowFn = FixedWindows.of(Duration.millis(10)); + assertWindows(windowFn, 1, window(0, 10)); + assertWindows(windowFn, 2, window(0, 10)); + assertWindows(windowFn, 5, window(0, 10)); + assertWindows(windowFn, 9, window(0, 10)); + assertWindows(windowFn, 10, window(10, 20)); + assertWindows(windowFn, 11, window(10, 20)); + assertWindows(windowFn, 100, window(100, 110)); } @Test public void testFixedOffsetWindow() throws Exception { - Map> 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(Duration.millis(10)).withOffset(Duration.millis(5)), - Arrays.asList(1L, 2L, 5L, 9L, 10L, 11L, 100L))); + FixedWindows windowFn = FixedWindows.of(Duration.millis(10)).withOffset(Duration.millis(5)); + assertWindows(windowFn, 1, window(-5, 5)); + assertWindows(windowFn, 2, window(-5, 5)); + assertWindows(windowFn, 5, window(5, 15)); + assertWindows(windowFn, 9, window(5, 15)); + assertWindows(windowFn, 10, window(5, 15)); + assertWindows(windowFn, 11, window(5, 15)); + assertWindows(windowFn, 100, window(95, 105)); } @Test public void testTimeUnit() throws Exception { - Map> 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))); + FixedWindows windowFn = + FixedWindows.of(Duration.standardSeconds(10)).withOffset(Duration.standardSeconds(5)); + assertWindows(windowFn, 1, window(-5000, 5000)); + assertWindows(windowFn, 2, window(-5000, 5000)); + assertWindows(windowFn, 1000, window(-5000, 5000)); + assertWindows(windowFn, 5000, window(5000, 15000)); + assertWindows(windowFn, 5001, window(5000, 15000)); + assertWindows(windowFn, 10000, window(5000, 15000)); } @Test diff --git a/sdks/java/core/src/test/java/org/apache/beam/sdk/transforms/windowing/SessionsTest.java b/sdks/java/core/src/test/java/org/apache/beam/sdk/transforms/windowing/SessionsTest.java index 783fc3b37265..1b160cb8f13b 100644 --- a/sdks/java/core/src/test/java/org/apache/beam/sdk/transforms/windowing/SessionsTest.java +++ b/sdks/java/core/src/test/java/org/apache/beam/sdk/transforms/windowing/SessionsTest.java @@ -17,8 +17,7 @@ */ package org.apache.beam.sdk.transforms.windowing; -import static org.apache.beam.sdk.testing.WindowFnTestUtils.runWindowFn; -import static org.apache.beam.sdk.testing.WindowFnTestUtils.set; +import static org.apache.beam.sdk.testing.WindowFnTestUtils.assignedWindows; import static org.apache.beam.sdk.transforms.display.DisplayDataMatchers.hasDisplayItem; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.allOf; @@ -26,14 +25,18 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; -import java.util.Arrays; +import java.util.ArrayList; +import java.util.Collection; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import org.apache.beam.sdk.testing.WindowFnTestUtils; import org.apache.beam.sdk.transforms.display.DisplayData; import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.ImmutableList; +import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.ImmutableMap; +import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.ImmutableSet; import org.joda.time.Duration; import org.joda.time.Instant; import org.junit.Rule; @@ -47,52 +50,88 @@ public class SessionsTest { @Rule public ExpectedException thrown = ExpectedException.none(); + private static IntervalWindow window(long start, long end) { + return new IntervalWindow(new Instant(start), new Instant(end)); + } + + /** + * Assigns each timestamp to its session in order, merging after each one as a runner would, and + * returns the resulting sessions with the timestamps each holds. + */ + private static Map> mergedSessions( + Sessions sessions, long... timestamps) throws Exception { + Map> windows = new HashMap<>(); + for (long timestamp : timestamps) { + for (IntervalWindow window : assignedWindows(sessions, timestamp)) { + windows.computeIfAbsent(window, w -> new HashSet<>()).add(timestamp); + } + sessions.mergeWindows(new TestMergeContext(sessions, windows)); + } + return windows; + } + + /** Merges the windows Sessions picks, carrying each window's timestamps into the result. */ + private static class TestMergeContext extends WindowFn.MergeContext { + private final Map> windows; + + TestMergeContext(Sessions sessions, Map> windows) { + sessions.super(); + this.windows = windows; + } + + @Override + public Collection windows() { + return new ArrayList<>(windows.keySet()); + } + + @Override + public void merge(Collection toBeMerged, IntervalWindow mergeResult) { + Set merged = new HashSet<>(); + for (IntervalWindow window : toBeMerged) { + merged.addAll(windows.remove(window)); + } + windows.computeIfAbsent(mergeResult, w -> new HashSet<>()).addAll(merged); + } + } + @Test public void testSimple() throws Exception { - Map> expected = new HashMap<>(); - expected.put(new IntervalWindow(new Instant(0), new Instant(10)), set(0)); - expected.put(new IntervalWindow(new Instant(10), new Instant(20)), set(10)); - expected.put(new IntervalWindow(new Instant(101), new Instant(111)), set(101)); assertEquals( - expected, - runWindowFn(Sessions.withGapDuration(Duration.millis(10)), Arrays.asList(0L, 10L, 101L))); + ImmutableMap.of( + window(0, 10), ImmutableSet.of(0L), + window(10, 20), ImmutableSet.of(10L), + window(101, 111), ImmutableSet.of(101L)), + mergedSessions(Sessions.withGapDuration(Duration.millis(10)), 0, 10, 101)); } @Test public void testConsecutive() throws Exception { - Map> expected = new HashMap<>(); - expected.put(new IntervalWindow(new Instant(1), new Instant(19)), set(1, 2, 5, 9)); - expected.put(new IntervalWindow(new Instant(100), new Instant(111)), set(100, 101)); assertEquals( - expected, - runWindowFn( - Sessions.withGapDuration(Duration.millis(10)), - Arrays.asList(1L, 2L, 5L, 9L, 100L, 101L))); + ImmutableMap.of( + window(1, 19), ImmutableSet.of(1L, 2L, 5L, 9L), + window(100, 111), ImmutableSet.of(100L, 101L)), + mergedSessions(Sessions.withGapDuration(Duration.millis(10)), 1, 2, 5, 9, 100, 101)); } @Test public void testMerging() throws Exception { - Map> expected = new HashMap<>(); - expected.put(new IntervalWindow(new Instant(1), new Instant(40)), set(1, 10, 15, 22, 30)); - expected.put(new IntervalWindow(new Instant(95), new Instant(111)), set(95, 100, 101)); assertEquals( - expected, - runWindowFn( - Sessions.withGapDuration(Duration.millis(10)), - Arrays.asList(1L, 15L, 30L, 100L, 101L, 95L, 22L, 10L))); + ImmutableMap.of( + window(1, 40), ImmutableSet.of(1L, 10L, 15L, 22L, 30L), + window(95, 111), ImmutableSet.of(95L, 100L, 101L)), + mergedSessions( + Sessions.withGapDuration(Duration.millis(10)), 1, 15, 30, 100, 101, 95, 22, 10)); } @Test public void testTimeUnit() throws Exception { - Map> expected = new HashMap<>(); - expected.put(new IntervalWindow(new Instant(1), new Instant(2000)), set(1, 2, 1000)); - expected.put(new IntervalWindow(new Instant(5000), new Instant(6001)), set(5000, 5001)); - expected.put(new IntervalWindow(new Instant(10000), new Instant(11000)), set(10000)); assertEquals( - expected, - runWindowFn( - Sessions.withGapDuration(Duration.standardSeconds(1)), - Arrays.asList(1L, 2L, 1000L, 5000L, 5001L, 10000L))); + ImmutableMap.of( + window(1, 2000), ImmutableSet.of(1L, 2L, 1000L), + window(5000, 6001), ImmutableSet.of(5000L, 5001L), + window(10000, 11000), ImmutableSet.of(10000L)), + mergedSessions( + Sessions.withGapDuration(Duration.standardSeconds(1)), 1, 2, 1000, 5000, 5001, 10000)); } @Test diff --git a/sdks/java/core/src/test/java/org/apache/beam/sdk/transforms/windowing/SlidingWindowsTest.java b/sdks/java/core/src/test/java/org/apache/beam/sdk/transforms/windowing/SlidingWindowsTest.java index df463e838073..ecc891c35b01 100644 --- a/sdks/java/core/src/test/java/org/apache/beam/sdk/transforms/windowing/SlidingWindowsTest.java +++ b/sdks/java/core/src/test/java/org/apache/beam/sdk/transforms/windowing/SlidingWindowsTest.java @@ -17,20 +17,16 @@ */ package org.apache.beam.sdk.transforms.windowing; -import static org.apache.beam.sdk.testing.WindowFnTestUtils.runWindowFn; -import static org.apache.beam.sdk.testing.WindowFnTestUtils.set; +import static org.apache.beam.sdk.testing.WindowFnTestUtils.assignedWindows; import static org.apache.beam.sdk.transforms.display.DisplayDataMatchers.hasDisplayItem; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; -import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; -import java.util.Set; import org.apache.beam.sdk.transforms.display.DisplayData; import org.joda.time.Duration; import org.joda.time.Instant; @@ -45,85 +41,96 @@ public class SlidingWindowsTest { @Rule public ExpectedException thrown = ExpectedException.none(); + private static IntervalWindow window(long start, long end) { + return new IntervalWindow(new Instant(start), new Instant(end)); + } + + /** Asserts that {@code windowFn} assigns {@code timestamp} to exactly {@code windows}. */ + private static void assertWindows( + WindowFn windowFn, long timestamp, IntervalWindow... windows) + throws Exception { + assertThat( + "windows for timestamp " + timestamp, + assignedWindows(windowFn, timestamp), + containsInAnyOrder(windows)); + } + @Test public void testSimple() throws Exception { - Map> expected = new HashMap<>(); - expected.put(new IntervalWindow(new Instant(-5), new Instant(5)), set(1, 2)); - expected.put(new IntervalWindow(new Instant(0), new Instant(10)), set(1, 2, 5, 9)); - expected.put(new IntervalWindow(new Instant(5), new Instant(15)), set(5, 9, 10, 11)); - expected.put(new IntervalWindow(new Instant(10), new Instant(20)), set(10, 11)); SlidingWindows windowFn = SlidingWindows.of(Duration.millis(10)).every(Duration.millis(5)); - assertEquals(expected, runWindowFn(windowFn, Arrays.asList(1L, 2L, 5L, 9L, 10L, 11L))); + assertWindows(windowFn, 1, window(-5, 5), window(0, 10)); + assertWindows(windowFn, 2, window(-5, 5), window(0, 10)); + assertWindows(windowFn, 5, window(0, 10), window(5, 15)); + assertWindows(windowFn, 9, window(0, 10), window(5, 15)); + assertWindows(windowFn, 10, window(5, 15), window(10, 20)); + assertWindows(windowFn, 11, window(5, 15), window(10, 20)); assertThat(windowFn.assignsToOneWindow(), is(false)); } @Test public void testSlightlyOverlapping() throws Exception { - Map> expected = new HashMap<>(); - expected.put(new IntervalWindow(new Instant(-5), new Instant(2)), set(1)); - expected.put(new IntervalWindow(new Instant(0), new Instant(7)), set(1, 2, 5)); - expected.put(new IntervalWindow(new Instant(5), new Instant(12)), set(5, 9, 10, 11)); - expected.put(new IntervalWindow(new Instant(10), new Instant(17)), set(10, 11)); SlidingWindows windowFn = SlidingWindows.of(Duration.millis(7)).every(Duration.millis(5)); - assertEquals(expected, runWindowFn(windowFn, Arrays.asList(1L, 2L, 5L, 9L, 10L, 11L))); + assertWindows(windowFn, 1, window(-5, 2), window(0, 7)); + assertWindows(windowFn, 2, window(0, 7)); + assertWindows(windowFn, 5, window(0, 7), window(5, 12)); + assertWindows(windowFn, 9, window(5, 12)); + assertWindows(windowFn, 10, window(5, 12), window(10, 17)); + assertWindows(windowFn, 11, window(5, 12), window(10, 17)); assertThat(windowFn.assignsToOneWindow(), is(false)); } @Test public void testEqualSize() throws Exception { - Map> expected = new HashMap<>(); - expected.put(new IntervalWindow(new Instant(0), new Instant(3)), set(1, 2)); - expected.put(new IntervalWindow(new Instant(3), new Instant(6)), set(3, 4, 5)); - expected.put(new IntervalWindow(new Instant(6), new Instant(9)), set(6, 7)); SlidingWindows windowFn = SlidingWindows.of(Duration.millis(3)).every(Duration.millis(3)); - assertEquals(expected, runWindowFn(windowFn, Arrays.asList(1L, 2L, 3L, 4L, 5L, 6L, 7L))); + assertWindows(windowFn, 1, window(0, 3)); + assertWindows(windowFn, 2, window(0, 3)); + assertWindows(windowFn, 3, window(3, 6)); + assertWindows(windowFn, 4, window(3, 6)); + assertWindows(windowFn, 5, window(3, 6)); + assertWindows(windowFn, 6, window(6, 9)); + assertWindows(windowFn, 7, window(6, 9)); assertThat(windowFn.assignsToOneWindow(), is(true)); } @Test public void testElidings() throws Exception { - Map> expected = new HashMap<>(); - expected.put(new IntervalWindow(new Instant(0), new Instant(3)), set(1, 2)); - expected.put(new IntervalWindow(new Instant(10), new Instant(13)), set(10, 11)); - expected.put(new IntervalWindow(new Instant(100), new Instant(103)), set(100)); + // Only look at the first 3 millisecs of every 10-millisec interval. SlidingWindows windowFn = SlidingWindows.of(Duration.millis(3)).every(Duration.millis(10)); - assertEquals( - expected, - runWindowFn( - // Only look at the first 3 millisecs of every 10-millisec interval. - windowFn, Arrays.asList(1L, 2L, 3L, 5L, 9L, 10L, 11L, 100L))); + assertWindows(windowFn, 1, window(0, 3)); + assertWindows(windowFn, 2, window(0, 3)); + assertWindows(windowFn, 3); + assertWindows(windowFn, 5); + assertWindows(windowFn, 9); + assertWindows(windowFn, 10, window(10, 13)); + assertWindows(windowFn, 11, window(10, 13)); + assertWindows(windowFn, 100, window(100, 103)); assertThat(windowFn.assignsToOneWindow(), is(true)); } @Test public void testOffset() throws Exception { - Map> expected = new HashMap<>(); - expected.put(new IntervalWindow(new Instant(-8), new Instant(2)), set(1)); - expected.put(new IntervalWindow(new Instant(-3), new Instant(7)), set(1, 2, 5)); - expected.put(new IntervalWindow(new Instant(2), new Instant(12)), set(2, 5, 9, 10, 11)); - expected.put(new IntervalWindow(new Instant(7), new Instant(17)), set(9, 10, 11)); - assertEquals( - expected, - runWindowFn( - SlidingWindows.of(Duration.millis(10)) - .every(Duration.millis(5)) - .withOffset(Duration.millis(2)), - Arrays.asList(1L, 2L, 5L, 9L, 10L, 11L))); + SlidingWindows windowFn = + SlidingWindows.of(Duration.millis(10)) + .every(Duration.millis(5)) + .withOffset(Duration.millis(2)); + assertWindows(windowFn, 1, window(-8, 2), window(-3, 7)); + assertWindows(windowFn, 2, window(-3, 7), window(2, 12)); + assertWindows(windowFn, 5, window(-3, 7), window(2, 12)); + assertWindows(windowFn, 9, window(2, 12), window(7, 17)); + assertWindows(windowFn, 10, window(2, 12), window(7, 17)); + assertWindows(windowFn, 11, window(2, 12), window(7, 17)); } @Test public void testTimeUnit() throws Exception { - Map> expected = new HashMap<>(); - expected.put(new IntervalWindow(new Instant(-5000), new Instant(5000)), set(1, 2, 1000)); - expected.put( - new IntervalWindow(new Instant(0), new Instant(10000)), set(1, 2, 1000, 5000, 5001)); - expected.put(new IntervalWindow(new Instant(5000), new Instant(15000)), set(5000, 5001, 10000)); - expected.put(new IntervalWindow(new Instant(10000), new Instant(20000)), set(10000)); - assertEquals( - expected, - runWindowFn( - SlidingWindows.of(Duration.standardSeconds(10)).every(Duration.standardSeconds(5)), - Arrays.asList(1L, 2L, 1000L, 5000L, 5001L, 10000L))); + SlidingWindows windowFn = + SlidingWindows.of(Duration.standardSeconds(10)).every(Duration.standardSeconds(5)); + assertWindows(windowFn, 1, window(-5000, 5000), window(0, 10000)); + assertWindows(windowFn, 2, window(-5000, 5000), window(0, 10000)); + assertWindows(windowFn, 1000, window(-5000, 5000), window(0, 10000)); + assertWindows(windowFn, 5000, window(0, 10000), window(5000, 15000)); + assertWindows(windowFn, 5001, window(0, 10000), window(5000, 15000)); + assertWindows(windowFn, 10000, window(5000, 15000), window(10000, 20000)); } @Test