Search in sources :

Example 46 with Category

use of org.junit.experimental.categories.Category in project beam by apache.

the class ParDoTest method testBagStateSideInput.

@Test
@Category({ ValidatesRunner.class, UsesStatefulParDo.class })
public void testBagStateSideInput() {
    final PCollectionView<List<Integer>> listView = pipeline.apply("Create list for side input", Create.of(2, 1, 0)).apply(View.<Integer>asList());
    final String stateId = "foo";
    DoFn<KV<String, Integer>, List<Integer>> fn = new DoFn<KV<String, Integer>, List<Integer>>() {

        @StateId(stateId)
        private final StateSpec<BagState<Integer>> bufferState = StateSpecs.bag(VarIntCoder.of());

        @ProcessElement
        public void processElement(ProcessContext c, @StateId(stateId) BagState<Integer> state) {
            Iterable<Integer> currentValue = state.read();
            state.add(c.element().getValue());
            if (Iterables.size(state.read()) >= 4) {
                List<Integer> sorted = Lists.newArrayList(currentValue);
                Collections.sort(sorted);
                c.output(sorted);
                List<Integer> sideSorted = Lists.newArrayList(c.sideInput(listView));
                Collections.sort(sideSorted);
                c.output(sideSorted);
            }
        }
    };
    PCollection<List<Integer>> output = pipeline.apply("Create main input", Create.of(KV.of("hello", 97), KV.of("hello", 42), KV.of("hello", 84), KV.of("hello", 12))).apply(ParDo.of(fn).withSideInputs(listView));
    PAssert.that(output).containsInAnyOrder(Lists.newArrayList(12, 42, 84, 97), Lists.newArrayList(0, 1, 2));
    pipeline.run();
}
Also used : StringUtils.byteArrayToJsonString(org.apache.beam.sdk.util.StringUtils.byteArrayToJsonString) Matchers.containsString(org.hamcrest.Matchers.containsString) KV(org.apache.beam.sdk.values.KV) StateSpec(org.apache.beam.sdk.state.StateSpec) ArrayList(java.util.ArrayList) List(java.util.List) TupleTagList(org.apache.beam.sdk.values.TupleTagList) ImmutableList(com.google.common.collect.ImmutableList) BagState(org.apache.beam.sdk.state.BagState) Category(org.junit.experimental.categories.Category) Test(org.junit.Test)

Example 47 with Category

use of org.junit.experimental.categories.Category in project beam by apache.

the class ParDoTest method testBagStateCoderInference.

@Test
@Category({ ValidatesRunner.class, UsesStatefulParDo.class })
public void testBagStateCoderInference() {
    final String stateId = "foo";
    Coder<MyInteger> myIntegerCoder = MyIntegerCoder.of();
    pipeline.getCoderRegistry().registerCoderForClass(MyInteger.class, myIntegerCoder);
    DoFn<KV<String, Integer>, List<MyInteger>> fn = new DoFn<KV<String, Integer>, List<MyInteger>>() {

        @StateId(stateId)
        private final StateSpec<BagState<MyInteger>> bufferState = StateSpecs.bag();

        @ProcessElement
        public void processElement(ProcessContext c, @StateId(stateId) BagState<MyInteger> state) {
            Iterable<MyInteger> currentValue = state.read();
            state.add(new MyInteger(c.element().getValue()));
            if (Iterables.size(state.read()) >= 4) {
                List<MyInteger> sorted = Lists.newArrayList(currentValue);
                Collections.sort(sorted);
                c.output(sorted);
            }
        }
    };
    PCollection<List<MyInteger>> output = pipeline.apply(Create.of(KV.of("hello", 97), KV.of("hello", 42), KV.of("hello", 84), KV.of("hello", 12))).apply(ParDo.of(fn)).setCoder(ListCoder.of(myIntegerCoder));
    PAssert.that(output).containsInAnyOrder(Lists.newArrayList(new MyInteger(12), new MyInteger(42), new MyInteger(84), new MyInteger(97)));
    pipeline.run();
}
Also used : StringUtils.byteArrayToJsonString(org.apache.beam.sdk.util.StringUtils.byteArrayToJsonString) Matchers.containsString(org.hamcrest.Matchers.containsString) KV(org.apache.beam.sdk.values.KV) StateSpec(org.apache.beam.sdk.state.StateSpec) ArrayList(java.util.ArrayList) List(java.util.List) TupleTagList(org.apache.beam.sdk.values.TupleTagList) ImmutableList(com.google.common.collect.ImmutableList) BagState(org.apache.beam.sdk.state.BagState) Category(org.junit.experimental.categories.Category) Test(org.junit.Test)

Example 48 with Category

use of org.junit.experimental.categories.Category in project beam by apache.

the class ParDoTest method testCombiningState.

@Test
@Category({ ValidatesRunner.class, UsesStatefulParDo.class })
public void testCombiningState() {
    final String stateId = "foo";
    DoFn<KV<String, Double>, String> fn = new DoFn<KV<String, Double>, String>() {

        private static final double EPSILON = 0.0001;

        @StateId(stateId)
        private final StateSpec<CombiningState<Double, CountSum<Double>, Double>> combiningState = StateSpecs.combining(new Mean.CountSumCoder<Double>(), Mean.<Double>of());

        @ProcessElement
        public void processElement(ProcessContext c, @StateId(stateId) CombiningState<Double, CountSum<Double>, Double> state) {
            state.add(c.element().getValue());
            Double currentValue = state.read();
            if (Math.abs(currentValue - 0.5) < EPSILON) {
                c.output("right on");
            }
        }
    };
    PCollection<String> output = pipeline.apply(Create.of(KV.of("hello", 0.3), KV.of("hello", 0.6), KV.of("hello", 0.6))).apply(ParDo.of(fn));
    // There should only be one moment at which the average is exactly 0.5
    PAssert.that(output).containsInAnyOrder("right on");
    pipeline.run();
}
Also used : StateSpec(org.apache.beam.sdk.state.StateSpec) CountSum(org.apache.beam.sdk.transforms.Mean.CountSum) CombiningState(org.apache.beam.sdk.state.CombiningState) StringUtils.byteArrayToJsonString(org.apache.beam.sdk.util.StringUtils.byteArrayToJsonString) Matchers.containsString(org.hamcrest.Matchers.containsString) KV(org.apache.beam.sdk.values.KV) Category(org.junit.experimental.categories.Category) Test(org.junit.Test)

Example 49 with Category

use of org.junit.experimental.categories.Category in project beam by apache.

the class SpannerIOTest method singleMutationPipeline.

@Test
@Category(NeedsRunner.class)
public void singleMutationPipeline() throws Exception {
    Mutation mutation = Mutation.newInsertOrUpdateBuilder("test").set("one").to(2).build();
    PCollection<Mutation> mutations = pipeline.apply(Create.of(mutation));
    mutations.apply(SpannerIO.write().withProjectId("test-project").withInstanceId("test-instance").withDatabaseId("test-database").withServiceFactory(serviceFactory));
    pipeline.run();
    verify(serviceFactory.mockSpanner()).getDatabaseClient(DatabaseId.of("test-project", "test-instance", "test-database"));
    verify(serviceFactory.mockDatabaseClient(), times(1)).writeAtLeastOnce(argThat(new IterableOfSize(1)));
}
Also used : Mutation(com.google.cloud.spanner.Mutation) Category(org.junit.experimental.categories.Category) Test(org.junit.Test)

Example 50 with Category

use of org.junit.experimental.categories.Category in project geode by apache.

the class PutAllCSDUnitTest method testRAVersionsOnClientsWithNotificationsOnly.

/**
   * basically same test as testVersionsOnClientsWithNotificationsOnly but also do a removeAll
   */
// GEODE-1419
@Category(FlakyTest.class)
@Test
public void testRAVersionsOnClientsWithNotificationsOnly() {
    final String title = "testRAVersionsInClients";
    disconnectAllFromDS();
    final Host host = Host.getHost(0);
    VM server1 = host.getVM(0);
    VM server2 = host.getVM(1);
    VM client1 = host.getVM(2);
    VM client2 = host.getVM(3);
    final String regionName = getUniqueName();
    final String serverHost = NetworkUtils.getServerHostName(server1.getHost());
    // set notifyBySubscription=true to test register interest
    final int serverPort1 = createBridgeServer(server1, regionName, 0, true, 0, null);
    final int serverPort2 = createBridgeServer(server2, regionName, 0, true, 0, null);
    // set queueRedundency=1
    createBridgeClient(client1, regionName, serverHost, new int[] { serverPort1 }, 0, 59000, true);
    createBridgeClient(client2, regionName, serverHost, new int[] { serverPort2 }, 0, 59000, true);
    server1.invoke(addExceptionTag1(expectedExceptions));
    server2.invoke(addExceptionTag1(expectedExceptions));
    client1.invoke(addExceptionTag1(expectedExceptions));
    client2.invoke(addExceptionTag1(expectedExceptions));
    client1.invoke(new CacheSerializableRunnable(title + "client1 putAll+removeAll") {

        @Override
        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(regionName);
            doPutAll(regionName, "key-", numberOfEntries * 2);
            assertEquals(numberOfEntries * 2, region.size());
            doRemoveAll(regionName, "key-", numberOfEntries);
            assertEquals(numberOfEntries, region.size());
        }
    });
    client2.invoke(new CacheSerializableRunnable(title + "client2 versions collection") {

        @Override
        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(regionName);
            region.registerInterest("ALL_KEYS");
            assertEquals(numberOfEntries, region.size());
            LogWriterUtils.getLogWriter().info("client2 registerInterest ALL_KEYS at " + region.getFullPath());
        }
    });
    client1RAVersions = (List<VersionTag>) client1.invoke(new SerializableCallable(title + "client1 versions collection") {

        @Override
        public Object call() throws CacheException {
            Region region = getRootRegion().getSubregion(regionName);
            assertEquals(numberOfEntries, region.size());
            List<VersionTag> versions = new ArrayList<VersionTag>(numberOfEntries * 2);
            RegionMap entries = ((LocalRegion) region).entries;
            assertEquals(numberOfEntries * 2, entries.size());
            for (Object key : entries.keySet()) {
                RegionEntry internalRegionEntry = entries.getEntry(key);
                VersionTag tag = internalRegionEntry.getVersionStamp().asVersionTag();
                LogWriterUtils.getLogWriter().info("Entry version tag on client for " + key + ": " + tag);
                versions.add(tag);
            }
            return versions;
        }
    });
    client2RAVersions = (List<VersionTag>) client2.invoke(new SerializableCallable(title + "client2 versions collection") {

        @Override
        public Object call() throws CacheException {
            Region region = getRootRegion().getSubregion(regionName);
            assertEquals(numberOfEntries, region.size());
            List<VersionTag> versions = new ArrayList<VersionTag>(numberOfEntries * 2);
            RegionMap entries = ((LocalRegion) region).entries;
            assertEquals(numberOfEntries * 2, entries.size());
            for (Object key : entries.keySet()) {
                RegionEntry internalRegionEntry = entries.getEntry(key);
                VersionTag tag = internalRegionEntry.getVersionStamp().asVersionTag();
                LogWriterUtils.getLogWriter().info("Entry version tag on client for " + key + ": " + tag);
                versions.add(tag);
            }
            return versions;
        }
    });
    assertEquals(numberOfEntries * 2, client1RAVersions.size());
    LogWriterUtils.getLogWriter().info(Arrays.toString(client1RAVersions.toArray()));
    LogWriterUtils.getLogWriter().info(Arrays.toString(client2RAVersions.toArray()));
    for (VersionTag tag : client1RAVersions) {
        if (!client2RAVersions.contains(tag)) {
            fail("client 2 does not have the tag contained in client 1" + tag);
        }
    }
}
Also used : CacheException(org.apache.geode.cache.CacheException) Host(org.apache.geode.test.dunit.Host) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) VersionTag(org.apache.geode.internal.cache.versions.VersionTag) Region(org.apache.geode.cache.Region) List(java.util.List) ArrayList(java.util.ArrayList) Category(org.junit.experimental.categories.Category) ClientSubscriptionTest(org.apache.geode.test.junit.categories.ClientSubscriptionTest) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) ClientServerTest(org.apache.geode.test.junit.categories.ClientServerTest) Test(org.junit.Test)

Aggregations

Category (org.junit.experimental.categories.Category)900 Test (org.junit.Test)856 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)148 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)121 File (java.io.File)102 Instant (org.joda.time.Instant)92 KV (org.apache.beam.sdk.values.KV)86 ArrayList (java.util.ArrayList)84 Row (org.apache.beam.sdk.values.Row)71 Schema (org.apache.beam.sdk.schemas.Schema)65 VM (org.apache.geode.test.dunit.VM)65 QuickTest (com.hazelcast.test.annotation.QuickTest)57 List (java.util.List)57 Matchers.containsString (org.hamcrest.Matchers.containsString)55 InputStream (java.io.InputStream)49 NightlyTest (com.hazelcast.test.annotation.NightlyTest)47 FileListView (com.owncloud.android.test.ui.models.FileListView)46 UsesSchema (org.apache.beam.sdk.testing.UsesSchema)43 StringUtils.byteArrayToJsonString (org.apache.beam.sdk.util.StringUtils.byteArrayToJsonString)41 IOException (java.io.IOException)40