Search in sources :

Example 71 with DoFn

use of org.apache.beam.sdk.transforms.DoFn in project beam by apache.

the class DoFnInvokersTest method testTruncateFnWithHasDefaultMethodsWhenUnbounded.

@Test
public void testTruncateFnWithHasDefaultMethodsWhenUnbounded() throws Exception {
    class UnboundedMockFn extends DoFn<String, String> {

        @ProcessElement
        public void processElement(ProcessContext c, RestrictionTracker<RestrictionWithUnboundedDefaultTracker, Void> tracker, WatermarkEstimator<WatermarkEstimatorStateWithDefaultWatermarkEstimator> watermarkEstimator) {
        }

        @GetInitialRestriction
        public RestrictionWithUnboundedDefaultTracker getInitialRestriction(@Element String element) {
            return null;
        }

        @GetInitialWatermarkEstimatorState
        public WatermarkEstimatorStateWithDefaultWatermarkEstimator getInitialWatermarkEstimatorState() {
            return null;
        }
    }
    UnboundedMockFn fn = mock(UnboundedMockFn.class);
    DoFnInvoker<String, String> invoker = DoFnInvokers.invokerFor(fn);
    CoderRegistry coderRegistry = CoderRegistry.createDefault();
    coderRegistry.registerCoderProvider(CoderProviders.fromStaticMethods(RestrictionWithUnboundedDefaultTracker.class, CoderForDefaultTracker.class));
    coderRegistry.registerCoderForClass(WatermarkEstimatorStateWithDefaultWatermarkEstimator.class, new CoderForWatermarkEstimatorStateWithDefaultWatermarkEstimator());
    assertThat(invoker.<RestrictionWithBoundedDefaultTracker>invokeGetRestrictionCoder(coderRegistry), instanceOf(CoderForDefaultTracker.class));
    assertThat(invoker.invokeGetWatermarkEstimatorStateCoder(coderRegistry), instanceOf(CoderForWatermarkEstimatorStateWithDefaultWatermarkEstimator.class));
    RestrictionTracker tracker = invoker.invokeNewTracker(new FakeArgumentProvider<String, String>() {

        @Override
        public Object restriction() {
            return new RestrictionWithUnboundedDefaultTracker();
        }
    });
    assertThat(tracker, instanceOf(UnboundedDefaultTracker.class));
    TruncateResult<?> result = invoker.invokeTruncateRestriction(new FakeArgumentProvider<String, String>() {

        @Override
        public RestrictionTracker restrictionTracker() {
            return tracker;
        }

        @Override
        public String element(DoFn<String, String> doFn) {
            return "blah";
        }

        @Override
        public Object restriction() {
            return "foo";
        }
    });
    assertNull(result);
    assertEquals(stop(), invoker.invokeProcessElement(mockArgumentProvider));
    assertThat(invoker.invokeNewWatermarkEstimator(new FakeArgumentProvider<String, String>() {

        @Override
        public Object watermarkEstimatorState() {
            return new WatermarkEstimatorStateWithDefaultWatermarkEstimator();
        }
    }), instanceOf(DefaultWatermarkEstimator.class));
}
Also used : RestrictionTracker(org.apache.beam.sdk.transforms.splittabledofn.RestrictionTracker) CoderRegistry(org.apache.beam.sdk.coders.CoderRegistry) DoFn(org.apache.beam.sdk.transforms.DoFn) HasDefaultWatermarkEstimator(org.apache.beam.sdk.transforms.splittabledofn.HasDefaultWatermarkEstimator) WatermarkEstimator(org.apache.beam.sdk.transforms.splittabledofn.WatermarkEstimator) FakeArgumentProvider(org.apache.beam.sdk.transforms.reflect.DoFnInvoker.FakeArgumentProvider) HasDefaultWatermarkEstimator(org.apache.beam.sdk.transforms.splittabledofn.HasDefaultWatermarkEstimator) Test(org.junit.Test)

Example 72 with DoFn

use of org.apache.beam.sdk.transforms.DoFn in project beam by apache.

the class DoFnInvokersTest method testOnTimerWithWindow.

@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(TimerDeclaration.PREFIX + timerId, "", mockArgumentProvider);
    assertThat(fn.window, equalTo(testWindow));
}
Also used : DoFn(org.apache.beam.sdk.transforms.DoFn) Instant(org.joda.time.Instant) IntervalWindow(org.apache.beam.sdk.transforms.windowing.IntervalWindow) TimerSpec(org.apache.beam.sdk.state.TimerSpec) Test(org.junit.Test)

Example 73 with DoFn

use of org.apache.beam.sdk.transforms.DoFn in project beam by apache.

the class DoFnInvokersTest method testDefaultWatermarkEstimatorStateAndCoder.

@Test
public void testDefaultWatermarkEstimatorStateAndCoder() throws Exception {
    class MockFn extends DoFn<String, String> {

        @ProcessElement
        public void processElement(ProcessContext c, RestrictionTracker<RestrictionWithBoundedDefaultTracker, Void> tracker) {
        }

        @GetInitialRestriction
        public RestrictionWithBoundedDefaultTracker getInitialRestriction(@Element String element) {
            return null;
        }
    }
    MockFn fn = mock(MockFn.class);
    DoFnInvoker<String, String> invoker = DoFnInvokers.invokerFor(fn);
    CoderRegistry coderRegistry = CoderRegistry.createDefault();
    coderRegistry.registerCoderProvider(CoderProviders.fromStaticMethods(RestrictionWithBoundedDefaultTracker.class, CoderForDefaultTracker.class));
    assertEquals(VoidCoder.of(), invoker.invokeGetWatermarkEstimatorStateCoder(coderRegistry));
    assertNull(invoker.invokeGetInitialWatermarkEstimatorState(new FakeArgumentProvider<>()));
}
Also used : RestrictionTracker(org.apache.beam.sdk.transforms.splittabledofn.RestrictionTracker) CoderRegistry(org.apache.beam.sdk.coders.CoderRegistry) DoFn(org.apache.beam.sdk.transforms.DoFn) FakeArgumentProvider(org.apache.beam.sdk.transforms.reflect.DoFnInvoker.FakeArgumentProvider) Test(org.junit.Test)

Example 74 with DoFn

use of org.apache.beam.sdk.transforms.DoFn in project beam by apache.

the class DoFnInvokersTest method testBundleFinalizer.

@Test
public void testBundleFinalizer() {
    class BundleFinalizerDoFn extends DoFn<String, String> {

        @ProcessElement
        public void processElement(BundleFinalizer bundleFinalizer) {
            bundleFinalizer.afterBundleCommit(Instant.ofEpochSecond(42L), null);
        }
    }
    BundleFinalizer mockBundleFinalizer = mock(BundleFinalizer.class);
    when(mockArgumentProvider.bundleFinalizer()).thenReturn(mockBundleFinalizer);
    DoFnInvoker<String, String> invoker = DoFnInvokers.invokerFor(new BundleFinalizerDoFn());
    invoker.invokeProcessElement(mockArgumentProvider);
    verify(mockBundleFinalizer).afterBundleCommit(eq(Instant.ofEpochSecond(42L)), eq(null));
}
Also used : DoFn(org.apache.beam.sdk.transforms.DoFn) BundleFinalizer(org.apache.beam.sdk.transforms.DoFn.BundleFinalizer) Test(org.junit.Test)

Example 75 with DoFn

use of org.apache.beam.sdk.transforms.DoFn in project beam by apache.

the class DoFnSignaturesProcessElementTest method testBadGenericWildCards.

@Test
public void testBadGenericWildCards() throws Exception {
    thrown.expect(IllegalArgumentException.class);
    thrown.expectMessage("DoFn<Integer, ? super Integer>.ProcessContext");
    thrown.expectMessage("must have type");
    thrown.expectMessage("DoFn<Integer, String>.ProcessContext");
    analyzeProcessElementMethod(new AnonymousMethod() {

        private void method(DoFn<Integer, ? super Integer>.ProcessContext c) {
        }
    });
}
Also used : DoFn(org.apache.beam.sdk.transforms.DoFn) AnonymousMethod(org.apache.beam.sdk.transforms.reflect.DoFnSignaturesTestUtils.AnonymousMethod) Test(org.junit.Test)

Aggregations

DoFn (org.apache.beam.sdk.transforms.DoFn)154 Test (org.junit.Test)98 Pipeline (org.apache.beam.sdk.Pipeline)60 KV (org.apache.beam.sdk.values.KV)45 TupleTag (org.apache.beam.sdk.values.TupleTag)28 StateSpec (org.apache.beam.sdk.state.StateSpec)26 Instant (org.joda.time.Instant)26 ArrayList (java.util.ArrayList)23 TestPipeline (org.apache.beam.sdk.testing.TestPipeline)23 BoundedWindow (org.apache.beam.sdk.transforms.windowing.BoundedWindow)22 PCollection (org.apache.beam.sdk.values.PCollection)21 TimerSpec (org.apache.beam.sdk.state.TimerSpec)19 WindowedValue (org.apache.beam.sdk.util.WindowedValue)18 PCollectionView (org.apache.beam.sdk.values.PCollectionView)18 HashMap (java.util.HashMap)17 Coder (org.apache.beam.sdk.coders.Coder)17 List (java.util.List)16 Map (java.util.Map)14 ValueState (org.apache.beam.sdk.state.ValueState)14 RunnerApi (org.apache.beam.model.pipeline.v1.RunnerApi)13