For IntelliJ IDEA,
Android Studio or Eclipse



@Override public void close() throws Exception { if (state == State.BUNDLE_STARTED) { finishBundle(); } if (state == State.BUNDLE_FINISHED) { fnInvoker.invokeTeardown(); fn = null; fnInvoker = null; } state = State.TORN_DOWN; }
/** * A convenience operation that first calls {@link #startBundle}, * then calls {@link #processElement} on each of the input elements, then * calls {@link #finishBundle}, then returns the result of * {@link #takeOutputElements}. */ public List<OutputT> processBundle(Iterable <? extends InputT> inputElements) throws Exception { startBundle(); for (InputT inputElement : inputElements) { processElement(inputElement); } finishBundle(); return takeOutputElements(); }
@Test public void fnWithSideInputExplicit() throws Exception { PCollection<Integer> pCollection = p.apply(Create.of(-2)); final PCollectionView<Integer> value = pCollection.apply( View.<Integer>asSingleton().withDefaultValue(0)); try (DoFnTester<Integer, Integer> tester = DoFnTester.of(new SideInputDoFn(value))) { tester.setSideInput(value, GlobalWindow.INSTANCE, -2); tester.processElement(16); tester.processElement(32); tester.processElement(64); tester.processElement(128); tester.finishBundle(); assertThat(tester.peekOutputElements(), containsInAnyOrder(-2, -2, -2, -2)); } }
@Test public void peekValuesInWindow() throws Exception { try (DoFnTester<Long, String> tester = DoFnTester.of(new CounterDoFn())) { tester.startBundle(); tester.processElement(1L); tester.processElement(2L); tester.finishBundle(); assertThat( tester.peekOutputElementsInWindow(GlobalWindow.INSTANCE), containsInAnyOrder( TimestampedValue.of("1", new Instant(1000L)), TimestampedValue.of("2", new Instant(2000L)))); assertThat( tester.peekOutputElementsInWindow(new IntervalWindow(new Instant(0L), new Instant(10L))), Matchers.emptyIterable()); } }
@Test public void testSupportsWindowParameter() throws Exception { Instant now = Instant.now(); try (DoFnTester<Integer, KV<Integer, BoundedWindow>> tester = DoFnTester.of(new DoFnWithWindowParameter())) { BoundedWindow firstWindow = new IntervalWindow(now, now.plus(Duration.standardMinutes(1))); tester.processWindowedElement(1, now, firstWindow); tester.processWindowedElement(2, now, firstWindow); BoundedWindow secondWindow = new IntervalWindow(now, now.plus(Duration.standardMinutes(4))); tester.processWindowedElement(3, now, secondWindow); tester.finishBundle(); assertThat( tester.peekOutputElementsInWindow(firstWindow), containsInAnyOrder( TimestampedValue.of(KV.of(1, firstWindow), now), TimestampedValue.of(KV.of(2, firstWindow), now))); assertThat( tester.peekOutputElementsInWindow(secondWindow), containsInAnyOrder( TimestampedValue.of(KV.of(3, secondWindow), now))); } }
assertThat(take, hasItems("5", "6")); tester.finishBundle();
/** * @deprecated Use {@link TestPipeline} with the {@code DirectRunner}. */ @Deprecated @Override public void close() throws Exception { if (state == State.BUNDLE_STARTED) { finishBundle(); } if (state == State.BUNDLE_FINISHED) { fnInvoker.invokeTeardown(); fn = null; fnInvoker = null; } state = State.TORN_DOWN; }
/** * @deprecated Use {@link TestPipeline} with the {@code DirectRunner}. */ @Deprecated public List<OutputT> processBundle(Iterable <? extends InputT> inputElements) throws Exception { startBundle(); for (InputT inputElement : inputElements) { processElement(inputElement); } finishBundle(); return takeOutputElements(); }
@Test public void fnWithSideInputExplicit() throws Exception { PCollection<Integer> pCollection = p.apply(Create.of(-2)); final PCollectionView<Integer> value = pCollection.apply( View.<Integer>asSingleton().withDefaultValue(0)); try (DoFnTester<Integer, Integer> tester = DoFnTester.of(new SideInputDoFn(value))) { tester.setSideInput(value, GlobalWindow.INSTANCE, -2); tester.processElement(16); tester.processElement(32); tester.processElement(64); tester.processElement(128); tester.finishBundle(); assertThat(tester.peekOutputElements(), containsInAnyOrder(-2, -2, -2, -2)); } }
@Test public void peekValuesInWindow() throws Exception { try (DoFnTester<Long, String> tester = DoFnTester.of(new CounterDoFn())) { tester.startBundle(); tester.processElement(1L); tester.processElement(2L); tester.finishBundle(); assertThat( tester.peekOutputElementsInWindow(GlobalWindow.INSTANCE), containsInAnyOrder( TimestampedValue.of("1", new Instant(1000L)), TimestampedValue.of("2", new Instant(2000L)))); assertThat( tester.peekOutputElementsInWindow(new IntervalWindow(new Instant(0L), new Instant(10L))), Matchers.emptyIterable()); } }
@Test public void testSupportsWindowParameter() throws Exception { Instant now = Instant.now(); try (DoFnTester<Integer, KV<Integer, BoundedWindow>> tester = DoFnTester.of(new DoFnWithWindowParameter())) { BoundedWindow firstWindow = new IntervalWindow(now, now.plus(Duration.standardMinutes(1))); tester.processWindowedElement(1, now, firstWindow); tester.processWindowedElement(2, now, firstWindow); BoundedWindow secondWindow = new IntervalWindow(now, now.plus(Duration.standardMinutes(4))); tester.processWindowedElement(3, now, secondWindow); tester.finishBundle(); assertThat( tester.peekOutputElementsInWindow(firstWindow), containsInAnyOrder( TimestampedValue.of(KV.of(1, firstWindow), now), TimestampedValue.of(KV.of(2, firstWindow), now))); assertThat( tester.peekOutputElementsInWindow(secondWindow), containsInAnyOrder( TimestampedValue.of(KV.of(3, secondWindow), now))); } }
assertThat(take, hasItems("5", "6")); tester.finishBundle();