Search in sources :

Example 31 with TestFunction

use of org.apache.geode.internal.cache.functions.TestFunction in project geode by apache.

the class FunctionCommandsDUnitTest method testExecuteFunctionOnRegion.

@Test
public void testExecuteFunctionOnRegion() {
    setUpJmxManagerOnVm0ThenConnect(null);
    final Function function = new TestFunction(true, TestFunction.TEST_FUNCTION1);
    Host.getHost(0).getVM(0).invoke(new SerializableRunnable() {

        public void run() {
            RegionFactory<Integer, Integer> dataRegionFactory = getCache().createRegionFactory(RegionShortcut.REPLICATE);
            Region region = dataRegionFactory.create(REGION_NAME);
            assertNotNull(region);
            FunctionService.registerFunction(function);
        }
    });
    String command = "execute function --id=" + function.getId() + " --region=" + REGION_NAME;
    getLogWriter().info("testExecuteFunctionOnRegion command=" + command);
    CommandResult cmdResult = executeCommand(command);
    if (cmdResult != null) {
        assertEquals(Result.Status.OK, cmdResult.getStatus());
        getLogWriter().info("testExecuteFunctionOnRegion cmdResult=" + cmdResult);
        String stringResult = commandResultToString(cmdResult);
        getLogWriter().info("testExecuteFunctionOnRegion stringResult=" + stringResult);
        assertTrue(stringResult.contains("Execution summary"));
    } else {
        fail("testExecuteFunctionOnRegion did not return CommandResult");
    }
}
Also used : Function(org.apache.geode.cache.execute.Function) TestFunction(org.apache.geode.internal.cache.functions.TestFunction) TestFunction(org.apache.geode.internal.cache.functions.TestFunction) RegionFactory(org.apache.geode.cache.RegionFactory) Region(org.apache.geode.cache.Region) CommandResult(org.apache.geode.management.internal.cli.result.CommandResult) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 32 with TestFunction

use of org.apache.geode.internal.cache.functions.TestFunction in project geode by apache.

the class FunctionExecution_ExceptionDUnitTest method testRemoteMultiKeyExecution_SendException.

@Test
public void testRemoteMultiKeyExecution_SendException() throws Exception {
    final String rName = getUniqueName();
    Host host = Host.getHost(0);
    final VM accessor = host.getVM(3);
    final VM datastore0 = host.getVM(0);
    final VM datastore1 = host.getVM(1);
    final VM datastore2 = host.getVM(2);
    getCache();
    accessor.invoke(new SerializableCallable("Create PR") {

        public Object call() throws Exception {
            RegionAttributes ra = PartitionedRegionTestHelper.createRegionAttrsForPR(0, 0);
            getCache().createRegion(rName, ra);
            return Boolean.TRUE;
        }
    });
    SerializableCallable dataStoreCreate = new SerializableCallable("Create PR with Function Factory") {

        public Object call() throws Exception {
            RegionAttributes ra = PartitionedRegionTestHelper.createRegionAttrsForPR(0, 10);
            AttributesFactory raf = new AttributesFactory(ra);
            PartitionAttributesImpl pa = new PartitionAttributesImpl();
            pa.setAll(ra.getPartitionAttributes());
            raf.setPartitionAttributes(pa);
            getCache().createRegion(rName, raf.create());
            Function function = new TestFunction(true, TestFunction.TEST_FUNCTION_SEND_EXCEPTION);
            FunctionService.registerFunction(function);
            return Boolean.TRUE;
        }
    };
    datastore0.invoke(dataStoreCreate);
    datastore1.invoke(dataStoreCreate);
    datastore2.invoke(dataStoreCreate);
    Object o = accessor.invoke(new SerializableCallable("Create data, invoke exectuable") {

        public Object call() throws Exception {
            PartitionedRegion pr = (PartitionedRegion) getCache().getRegion(rName);
            DistributedSystem.setThreadsSocketPolicy(false);
            Function function = new TestFunction(true, TestFunction.TEST_FUNCTION_SEND_EXCEPTION);
            FunctionService.registerFunction(function);
            Execution dataSet = FunctionService.onRegion(pr);
            HashSet origVals = new HashSet();
            for (int i = 0; i < 3; i++) {
                Integer val = new Integer(i);
                origVals.add(val);
                pr.put(val, "MyValue_" + i);
            }
            ResultCollector rs1 = dataSet.withFilter(origVals).setArguments((Serializable) origVals).execute(function);
            List results = (ArrayList) rs1.getResult();
            assertEquals(((origVals.size() * 3) + 3), results.size());
            Iterator resultIterator = results.iterator();
            int exceptionCount = 0;
            while (resultIterator.hasNext()) {
                Object o = resultIterator.next();
                if (o instanceof MyFunctionExecutionException) {
                    exceptionCount++;
                }
            }
            assertEquals(3, exceptionCount);
            return Boolean.TRUE;
        }
    });
    assertEquals(Boolean.TRUE, o);
}
Also used : TestFunction(org.apache.geode.internal.cache.functions.TestFunction) RegionAttributes(org.apache.geode.cache.RegionAttributes) Host(org.apache.geode.test.dunit.Host) Function(org.apache.geode.cache.execute.Function) TestFunction(org.apache.geode.internal.cache.functions.TestFunction) AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesImpl(org.apache.geode.internal.cache.PartitionAttributesImpl) Execution(org.apache.geode.cache.execute.Execution) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) ResultCollector(org.apache.geode.cache.execute.ResultCollector) HashSet(java.util.HashSet) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 33 with TestFunction

use of org.apache.geode.internal.cache.functions.TestFunction in project geode by apache.

the class FunctionExecution_ExceptionDUnitTest method testSingleKeyExecution_SendException_Datastore.

@Test
public void testSingleKeyExecution_SendException_Datastore() throws Exception {
    final String rName = getUniqueName();
    Host host = Host.getHost(0);
    final VM datastore = host.getVM(3);
    getCache();
    datastore.invoke(new SerializableCallable("Create PR with Function Factory") {

        public Object call() throws Exception {
            RegionAttributes ra = PartitionedRegionTestHelper.createRegionAttrsForPR(0, 10);
            AttributesFactory raf = new AttributesFactory(ra);
            PartitionAttributesImpl pa = new PartitionAttributesImpl();
            pa.setAll(ra.getPartitionAttributes());
            raf.setPartitionAttributes(pa);
            getCache().createRegion(rName, raf.create());
            Function function = new TestFunction(true, TestFunction.TEST_FUNCTION_SEND_EXCEPTION);
            FunctionService.registerFunction(function);
            return Boolean.TRUE;
        }
    });
    Object o = datastore.invoke(new SerializableCallable("Create data, invoke exectuable") {

        public Object call() throws Exception {
            PartitionedRegion pr = (PartitionedRegion) getCache().getRegion(rName);
            final String testKey = "execKey";
            final Set testKeysSet = new HashSet();
            testKeysSet.add(testKey);
            DistributedSystem.setThreadsSocketPolicy(false);
            Function function = new TestFunction(true, TestFunction.TEST_FUNCTION_SEND_EXCEPTION);
            FunctionService.registerFunction(function);
            // DefaultResultCollector rs = new DefaultResultCollector();
            // .withCollector(rs);
            Execution dataSet = FunctionService.onRegion(pr);
            pr.put(testKey, new Integer(1));
            ResultCollector rs1 = null;
            rs1 = dataSet.withFilter(testKeysSet).setArguments(Boolean.TRUE).execute(function);
            ArrayList results = (ArrayList) rs1.getResult();
            assertTrue(results.get(0) instanceof Exception);
            rs1 = dataSet.withFilter(testKeysSet).setArguments((Serializable) testKeysSet).execute(function);
            results = (ArrayList) rs1.getResult();
            assertEquals((testKeysSet.size() + 1), results.size());
            Iterator resultIterator = results.iterator();
            int exceptionCount = 0;
            while (resultIterator.hasNext()) {
                Object o = resultIterator.next();
                if (o instanceof MyFunctionExecutionException) {
                    exceptionCount++;
                }
            }
            assertEquals(1, exceptionCount);
            return Boolean.TRUE;
        }
    });
    assertEquals(Boolean.TRUE, o);
}
Also used : Serializable(java.io.Serializable) HashSet(java.util.HashSet) Set(java.util.Set) TestFunction(org.apache.geode.internal.cache.functions.TestFunction) RegionAttributes(org.apache.geode.cache.RegionAttributes) ArrayList(java.util.ArrayList) Host(org.apache.geode.test.dunit.Host) Function(org.apache.geode.cache.execute.Function) TestFunction(org.apache.geode.internal.cache.functions.TestFunction) AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesImpl(org.apache.geode.internal.cache.PartitionAttributesImpl) Execution(org.apache.geode.cache.execute.Execution) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) Iterator(java.util.Iterator) ResultCollector(org.apache.geode.cache.execute.ResultCollector) HashSet(java.util.HashSet) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 34 with TestFunction

use of org.apache.geode.internal.cache.functions.TestFunction in project geode by apache.

the class MyFunctionException method executeFunction_NoResult.

public static void executeFunction_NoResult() {
    Function function = new TestFunction(false, TestFunction.TEST_FUNCTION1);
    Execution dataset = FunctionService.onRegion(region);
    dataset.execute(function);
}
Also used : Function(org.apache.geode.cache.execute.Function) TestFunction(org.apache.geode.internal.cache.functions.TestFunction) DistributedRegionFunction(org.apache.geode.internal.cache.functions.DistributedRegionFunction) Execution(org.apache.geode.cache.execute.Execution) TestFunction(org.apache.geode.internal.cache.functions.TestFunction)

Example 35 with TestFunction

use of org.apache.geode.internal.cache.functions.TestFunction in project geode by apache.

the class MyFunctionException method executeFunction_NoLastResult.

public static void executeFunction_NoLastResult() {
    Set filter = new HashSet();
    for (int i = 100; i < 120; i++) {
        filter.add("execKey-" + i);
    }
    try {
        FunctionService.onRegion(region).withFilter(filter).execute(new TestFunction(true, TestFunction.TEST_FUNCTION_NO_LASTRESULT)).getResult();
        fail("FunctionException expected : Function did not send last result");
    } catch (Exception ex) {
        // TODO: this is too broad -- catch just the expected exception
        assertTrue(ex.getMessage().contains("did not send last result"));
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) TestFunction(org.apache.geode.internal.cache.functions.TestFunction) IgnoredException(org.apache.geode.test.dunit.IgnoredException) FunctionInvocationTargetException(org.apache.geode.cache.execute.FunctionInvocationTargetException) IOException(java.io.IOException) DistribuedRegionFunctionFunctionInvocationException(org.apache.geode.internal.cache.functions.DistribuedRegionFunctionFunctionInvocationException) HashSet(java.util.HashSet)

Aggregations

TestFunction (org.apache.geode.internal.cache.functions.TestFunction)214 Function (org.apache.geode.cache.execute.Function)198 Test (org.junit.Test)151 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)150 HashSet (java.util.HashSet)112 Execution (org.apache.geode.cache.execute.Execution)106 ResultCollector (org.apache.geode.cache.execute.ResultCollector)104 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)104 ArrayList (java.util.ArrayList)96 FunctionException (org.apache.geode.cache.execute.FunctionException)89 List (java.util.List)81 ClientServerTest (org.apache.geode.test.junit.categories.ClientServerTest)79 IgnoredException (org.apache.geode.test.dunit.IgnoredException)72 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)72 Iterator (java.util.Iterator)67 Region (org.apache.geode.cache.Region)65 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)57 AttributesFactory (org.apache.geode.cache.AttributesFactory)55 VM (org.apache.geode.test.dunit.VM)55 Host (org.apache.geode.test.dunit.Host)54