@Test public void testGetOffsetFixed_RI() { DateTimeZone zone = DateTimeZone.forID("+01:00"); assertEquals(1L * DateTimeConstants.MILLIS_PER_HOUR, zone.getOffset(new Instant(TEST_TIME_SUMMER))); assertEquals(1L * DateTimeConstants.MILLIS_PER_HOUR, zone.getOffset(new Instant(TEST_TIME_WINTER))); assertEquals(zone.getOffset(DateTimeUtils.currentTimeMillis()), zone.getOffset(null)); }
@Test public void testSimpleFixedWindow() throws Exception { Map<IntervalWindow, Set<String>> 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(new Duration(10)), Arrays.asList(1L, 2L, 5L, 9L, 10L, 11L, 100L))); }
@Test public void testTimeUnit() throws Exception { Map<IntervalWindow, Set<String>> 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))); }
@Test public void getSynchronizedProcessingTimeIsWatermarkSynchronizedInputTime() { when(watermarks.getSynchronizedProcessingInputTime()).thenReturn(new Instant(12345L)); assertThat(internals.currentSynchronizedProcessingTime(), equalTo(new Instant(12345L))); }
@Test @Category(NeedsRunner.class) public void testCreateParameterizedType() throws Exception { PCollection<TimestampedValue<String>> output = p.apply( Create.of( TimestampedValue.of("a", new Instant(0)), TimestampedValue.of("b", new Instant(0)))); PAssert.that(output) .containsInAnyOrder( TimestampedValue.of("a", new Instant(0)), TimestampedValue.of("b", new Instant(0))); p.run(); } /** An unserializable class to demonstrate encoding of elements. */
@Test @Category(NeedsRunner.class) public void withTimestampsBackwardsInTimeShouldThrow() { SerializableFunction<String, Instant> timestampFn = input -> new Instant(Long.valueOf(input)); SerializableFunction<String, Instant> backInTimeFn = input -> new Instant(Long.valueOf(input)).minus(Duration.millis(1000L)); String yearTwoThousand = "946684800000"; p.apply(Create.of("1234", "0", Integer.toString(Integer.MAX_VALUE), yearTwoThousand)) .apply("WithTimestamps", WithTimestamps.of(timestampFn)) .apply("AddSkew", WithTimestamps.of(backInTimeFn)); thrown.expect(PipelineExecutionException.class); thrown.expectCause(isA(IllegalArgumentException.class)); thrown.expectMessage("no earlier than the timestamp of the current input"); p.run(); }
@Parameters(name = "{index}: {0}") public static Iterable<TestStream<?>> data() { return ImmutableList.of( TestStream.create(VarIntCoder.of()).advanceWatermarkToInfinity(), TestStream.create(VarIntCoder.of()) .advanceWatermarkTo(new Instant(42)) .advanceWatermarkToInfinity(), TestStream.create(VarIntCoder.of()) .addElements(TimestampedValue.of(3, new Instant(17))) .advanceWatermarkToInfinity(), TestStream.create(StringUtf8Coder.of()) .advanceProcessingTime(Duration.millis(82)) .advanceWatermarkToInfinity()); }
@Override public void populateDisplayData(Builder builder) { builder .add(DisplayData.item("string", stringValue)) .add(DisplayData.item("long", intValue)) .add(DisplayData.item("double", floatValue)) .add(DisplayData.item("boolean", boolValue)) .add(DisplayData.item("instant", new Instant(0))) .add(DisplayData.item("duration", Duration.millis(durationMillis))) .add( DisplayData.item("class", DisplayDataTest.class) .withLinkUrl("http://abc") .withLabel("baz")); } };
@Test public void testSlightlyOverlapping() throws Exception { Map<IntervalWindow, Set<String>> 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(new Duration(7)).every(new Duration(5)); assertEquals(expected, runWindowFn(windowFn, Arrays.asList(1L, 2L, 5L, 9L, 10L, 11L))); assertThat(windowFn.assignsToOneWindow(), is(false)); }
@Test public void getOutputWatermarkTimeUsesWatermarkTime() { when(watermarks.getOutputWatermark()).thenReturn(new Instant(25525L)); assertThat(internals.currentOutputWatermarkTime(), equalTo(new Instant(25525L))); } }
@Test public void testEquality() { new EqualsTester() .addEqualityGroup( TimestampedValue.of("foo", new Instant(1000)), TimestampedValue.of("foo", new Instant(1000))) .addEqualityGroup(TimestampedValue.of("foo", new Instant(2000))) .addEqualityGroup(TimestampedValue.of("bar", new Instant(1000))) .addEqualityGroup( TimestampedValue.of("foo", BoundedWindow.TIMESTAMP_MIN_VALUE), TimestampedValue.atMinimumTimestamp("foo")) .testEquals(); }
@Test public void testConsecutive() throws Exception { Map<IntervalWindow, Set<String>> 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(new Duration(10)), Arrays.asList(1L, 2L, 5L, 9L, 100L, 101L))); }
@Test public void testGetOffset_RI() { DateTimeZone zone = DateTimeZone.forID("Europe/Paris"); assertEquals(2L * DateTimeConstants.MILLIS_PER_HOUR, zone.getOffset(new Instant(TEST_TIME_SUMMER))); assertEquals(1L * DateTimeConstants.MILLIS_PER_HOUR, zone.getOffset(new Instant(TEST_TIME_WINTER))); assertEquals(zone.getOffset(DateTimeUtils.currentTimeMillis()), zone.getOffset(null)); }
@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))); }
@Test public void getSynchronizedProcessingTimeIsWatermarkSynchronizedInputTime() { when(watermarks.getSynchronizedProcessingInputTime()).thenReturn(new Instant(12345L)); assertThat(internals.currentSynchronizedProcessingTime(), equalTo(new Instant(12345L))); }
@Test @Category(NeedsRunner.class) public void timestampsSucceeds() { PCollection<String> timestamped = pipeline.apply( Create.timestamped( TimestampedValue.of("foo", new Instant(0L)), TimestampedValue.of("bar", new Instant(1L)))); PCollection<TimestampedValue<String>> reified = timestamped.apply(Reify.timestamps()); PAssert.that(reified) .containsInAnyOrder( TimestampedValue.of("foo", new Instant(0)), TimestampedValue.of("bar", new Instant(1))); pipeline.run(); }
@Test public void testFireDeadline() throws Exception { assertEquals( BoundedWindow.TIMESTAMP_MAX_VALUE, Never.ever() .getWatermarkThatGuaranteesFiring(new IntervalWindow(new Instant(0), new Instant(10)))); }
/** Tests that the watermark that guarantees firing is that of the subtrigger. */ @Test public void testFireDeadline() throws Exception { setUp(FixedWindows.of(Duration.millis(10))); IntervalWindow window = new IntervalWindow(new Instant(0), new Instant(10)); Instant arbitraryInstant = new Instant(34957849); when(mockTrigger.getWatermarkThatGuaranteesFiring(Mockito.<IntervalWindow>any())) .thenReturn(arbitraryInstant); assertThat( Repeatedly.forever(mockTrigger).getWatermarkThatGuaranteesFiring(window), equalTo(arbitraryInstant)); }
@Test public void getInputWatermarkTimeUsesWatermarkTime() { when(watermarks.getInputWatermark()).thenReturn(new Instant(8765L)); assertThat(internals.currentInputWatermarkTime(), equalTo(new Instant(8765L))); }
@Test @Category(NeedsRunner.class) public void testGloballyEventTimestamp() { PCollection<String> output = p.apply( Create.timestamped( TimestampedValue.of("foo", new Instant(100)), TimestampedValue.of("bar", new Instant(300)), TimestampedValue.of("baz", new Instant(200)))) .apply(Latest.globally()); PAssert.that(output).containsInAnyOrder("bar"); p.run(); }