@Override public void onTimer( String timerId, BoundedWindow window, Instant timestamp, TimeDomain timeDomain) { // The effective timestamp is when derived elements will have their timestamp set, if not // otherwise specified. If this is an event time timer, then they have the timestamp of the // timer itself. Otherwise, they are set to the input timestamp, which is by definition // non-late. Instant effectiveTimestamp; switch (timeDomain) { case EVENT_TIME: effectiveTimestamp = timestamp; break; case PROCESSING_TIME: case SYNCHRONIZED_PROCESSING_TIME: effectiveTimestamp = stepContext.timerInternals().currentInputWatermarkTime(); break; default: throw new IllegalArgumentException(String.format("Unknown time domain: %s", timeDomain)); } OnTimerArgumentProvider argumentProvider = new OnTimerArgumentProvider(window, effectiveTimestamp, timeDomain); invoker.invokeOnTimer(timerId, argumentProvider); }
private void invokeOnTimer(String timerId, DoFn<String, String> fn) { DoFnInvokers.invokerFor(fn).invokeOnTimer(timerId, mockArgumentProvider); }
@Test public void testOnTimerHelloWord() throws Exception { final String timerId = "my-timer-id"; class SimpleTimerDoFn extends DoFn<String, String> { public String status = "not yet"; @TimerId(timerId) private final TimerSpec myTimer = TimerSpecs.timer(TimeDomain.PROCESSING_TIME); @ProcessElement public void process(ProcessContext c) {} @OnTimer(timerId) public void onMyTimer() { status = "OK now"; } } SimpleTimerDoFn fn = new SimpleTimerDoFn(); DoFnInvoker<String, String> invoker = DoFnInvokers.invokerFor(fn); invoker.invokeOnTimer(timerId, mockArgumentProvider); assertThat(fn.status, equalTo("OK now")); }
@Test public void testOnTimerWithWindow() throws Exception { final String timerId = "my-timer-id"; final IntervalWindow testWindow = new IntervalWindow(new Instant(0), new Instant(15)); when(mockArgumentProvider.window()).thenReturn(testWindow); class SimpleTimerDoFn extends DoFn<String, String> { public IntervalWindow window = null; @TimerId(timerId) private final TimerSpec myTimer = TimerSpecs.timer(TimeDomain.PROCESSING_TIME); @ProcessElement public void process(ProcessContext c) {} @OnTimer(timerId) public void onMyTimer(IntervalWindow w) { window = w; } } SimpleTimerDoFn fn = new SimpleTimerDoFn(); DoFnInvoker<String, String> invoker = DoFnInvokers.invokerFor(fn); invoker.invokeOnTimer(timerId, mockArgumentProvider); assertThat(fn.window, equalTo(testWindow)); }