Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> set(long... timestamps) {
Set<String> result = new HashSet<>();
for (long timestamp : timestamps) {
Expand All @@ -55,7 +60,10 @@ public static Set<String> 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 <T, W extends BoundedWindow> Map<W, Set<String>> runWindowFn(
WindowFn<T, W> windowFn, List<Long> timestamps) throws Exception {
List<TimestampedValue<T>> timestampedValues = new ArrayList<>();
Expand All @@ -69,7 +77,10 @@ public static <T, W extends BoundedWindow> Map<W, Set<String>> 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 <T, W extends BoundedWindow> Map<W, Set<String>> runWindowFnWithValue(
WindowFn<T, W> windowFn, List<TimestampedValue<T>> timestampedValues) throws Exception {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Object, IntervalWindow> 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<IntervalWindow, Set<String>> expected = new HashMap<>();

final List<Long> 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<Object, IntervalWindow> 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
Expand All @@ -89,59 +87,43 @@ public void testDaysCompatibility() throws IncompatibleWindowException {

@Test
public void testWeeks() throws Exception {
Map<IntervalWindow, Set<String>> expected = new HashMap<>();

final List<Long> 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<Object, IntervalWindow> 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<IntervalWindow, Set<String>> expected = new HashMap<>();

final List<Long> 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<Object, IntervalWindow> 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
Expand All @@ -154,64 +136,44 @@ public void testMonthsCompatibility() throws IncompatibleWindowException {

@Test
public void testMultiMonths() throws Exception {
Map<IntervalWindow, Set<String>> expected = new HashMap<>();

final List<Long> 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<Object, IntervalWindow> 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<IntervalWindow, Set<String>> expected = new HashMap<>();

final List<Long> 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<Object, IntervalWindow> 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
Expand All @@ -224,30 +186,22 @@ public void testYearsCompatibility() throws IncompatibleWindowException {

@Test
public void testTimeZone() throws Exception {
Map<IntervalWindow, Set<String>> expected = new HashMap<>();

DateTimeZone timeZone = DateTimeZone.forID("America/Los_Angeles");

final List<Long> 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<Object, IntervalWindow> 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
Expand Down
Loading
Loading