Search in sources :

Example 16 with FunctionContext

use of org.apache.geode.cache.execute.FunctionContext in project geode by apache.

the class MyFunctionException method executeInlineFunction.

public static void executeInlineFunction() {
    List list = (List) FunctionService.onRegion(region).setArguments(Boolean.TRUE).execute(new FunctionAdapter() {

        @Override
        public void execute(FunctionContext context) {
            if (context.getArguments() instanceof String) {
                context.getResultSender().lastResult("Success");
            } else if (context.getArguments() instanceof Boolean) {
                context.getResultSender().lastResult(Boolean.TRUE);
            }
        }

        @Override
        public String getId() {
            return "Function";
        }

        @Override
        public boolean hasResult() {
            return true;
        }
    }).getResult();
    assertEquals(1, list.size());
    assertEquals(Boolean.TRUE, list.get(0));
}
Also used : FunctionAdapter(org.apache.geode.cache.execute.FunctionAdapter) List(java.util.List) ArrayList(java.util.ArrayList) FunctionContext(org.apache.geode.cache.execute.FunctionContext)

Example 17 with FunctionContext

use of org.apache.geode.cache.execute.FunctionContext in project geode by apache.

the class ClientServerFunctionExecutionDUnitTest method serverExecution_Inline_InvalidAttributes.

public static void serverExecution_Inline_InvalidAttributes() {
    DistributedSystem.setThreadsSocketPolicy(false);
    Execution member = FunctionService.onServer(pool);
    try {
        ResultCollector rs = member.setArguments(Boolean.TRUE).execute(new FunctionAdapter() {

            public void execute(FunctionContext context) {
                if (context.getArguments() instanceof String) {
                    context.getResultSender().lastResult("Success");
                } else if (context.getArguments() instanceof Boolean) {
                    context.getResultSender().lastResult(Boolean.TRUE);
                }
            }

            public String getId() {
                return getClass().getName();
            }

            public boolean hasResult() {
                return false;
            }

            public boolean isHA() {
                return true;
            }
        });
        fail("Should have failed with Invalid attributes.");
    } catch (Exception ex) {
        LogWriterUtils.getLogWriter().info("Exception : ", ex);
        assertTrue(ex.getMessage().contains("For Functions with isHA true, hasResult must also be true."));
    }
}
Also used : Execution(org.apache.geode.cache.execute.Execution) FunctionAdapter(org.apache.geode.cache.execute.FunctionAdapter) ResultCollector(org.apache.geode.cache.execute.ResultCollector) FunctionContext(org.apache.geode.cache.execute.FunctionContext) ServerConnectivityException(org.apache.geode.cache.client.ServerConnectivityException) ServerOperationException(org.apache.geode.cache.client.ServerOperationException) IgnoredException(org.apache.geode.test.dunit.IgnoredException)

Example 18 with FunctionContext

use of org.apache.geode.cache.execute.FunctionContext in project geode by apache.

the class PRClientServerRegionFunctionExecutionNoSingleHopDUnitTest method FunctionExecution_Inline_Bug40714.

public static void FunctionExecution_Inline_Bug40714() {
    Region region = cache.getRegion(PartitionedRegionName);
    assertNotNull(region);
    final HashSet testKeysSet = new HashSet();
    for (int i = (totalNumBuckets.intValue() * 10); i > 0; i--) {
        testKeysSet.add("execKey-" + i);
    }
    int j = 0;
    for (Iterator i = testKeysSet.iterator(); i.hasNext(); ) {
        Integer val = new Integer(j++);
        region.put(i.next(), val);
    }
    HashMap resultMap = (HashMap) FunctionService.onRegion(region).setArguments(Boolean.TRUE).execute(new FunctionAdapter() {

        public void execute(FunctionContext context) {
            if (context.getArguments() instanceof String) {
                context.getResultSender().lastResult("Success");
            } else if (context.getArguments() instanceof Boolean) {
                context.getResultSender().lastResult(Boolean.TRUE);
            }
        }

        public String getId() {
            return "Function";
        }

        public boolean hasResult() {
            return true;
        }
    }).getResult();
    assertEquals(3, resultMap.size());
    Iterator mapIterator = resultMap.entrySet().iterator();
    Map.Entry entry = null;
    DistributedMember key = null;
    ArrayList resultListForMember = null;
    while (mapIterator.hasNext()) {
        entry = (Map.Entry) mapIterator.next();
        key = (DistributedMember) entry.getKey();
        resultListForMember = (ArrayList) entry.getValue();
        for (Object result : resultListForMember) {
            assertEquals(Boolean.TRUE, result);
        }
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) FunctionContext(org.apache.geode.cache.execute.FunctionContext) RegionFunctionContext(org.apache.geode.cache.execute.RegionFunctionContext) Iterator(java.util.Iterator) DistributedMember(org.apache.geode.distributed.DistributedMember) Region(org.apache.geode.cache.Region) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) FunctionAdapter(org.apache.geode.cache.execute.FunctionAdapter) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 19 with FunctionContext

use of org.apache.geode.cache.execute.FunctionContext in project geode by apache.

the class PRClientServerRegionFunctionExecutionSelectorNoSingleHopDUnitTest method FunctionExecution_Inline_Bug40714.

public static void FunctionExecution_Inline_Bug40714() {
    Region region = cache.getRegion(PartitionedRegionName);
    assertNotNull(region);
    final HashSet testKeysSet = new HashSet();
    for (int i = (totalNumBuckets.intValue() * 10); i > 0; i--) {
        testKeysSet.add("execKey-" + i);
    }
    int j = 0;
    for (Iterator i = testKeysSet.iterator(); i.hasNext(); ) {
        Integer val = new Integer(j++);
        region.put(i.next(), val);
    }
    HashMap resultMap = (HashMap) FunctionService.onRegion(region).setArguments(Boolean.TRUE).execute(new FunctionAdapter() {

        public void execute(FunctionContext context) {
            if (context.getArguments() instanceof String) {
                context.getResultSender().lastResult("Success");
            } else if (context.getArguments() instanceof Boolean) {
                context.getResultSender().lastResult(Boolean.TRUE);
            }
        }

        public String getId() {
            return "Function";
        }

        public boolean hasResult() {
            return true;
        }
    }).getResult();
    assertEquals(3, resultMap.size());
    Iterator mapIterator = resultMap.entrySet().iterator();
    Map.Entry entry = null;
    DistributedMember key = null;
    ArrayList resultListForMember = null;
    while (mapIterator.hasNext()) {
        entry = (Map.Entry) mapIterator.next();
        key = (DistributedMember) entry.getKey();
        resultListForMember = (ArrayList) entry.getValue();
        for (Object result : resultListForMember) {
            assertEquals(Boolean.TRUE, result);
        }
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) FunctionContext(org.apache.geode.cache.execute.FunctionContext) RegionFunctionContext(org.apache.geode.cache.execute.RegionFunctionContext) Iterator(java.util.Iterator) DistributedMember(org.apache.geode.distributed.DistributedMember) Region(org.apache.geode.cache.Region) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) FunctionAdapter(org.apache.geode.cache.execute.FunctionAdapter) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 20 with FunctionContext

use of org.apache.geode.cache.execute.FunctionContext in project geode by apache.

the class SizeExportLogsFunctionTest method sizeGreaterThanDiskAvailable_sendsErrorResult.

@Test
public void sizeGreaterThanDiskAvailable_sendsErrorResult() throws Throwable {
    server.withProperties(config).startServer();
    FunctionContext context = new FunctionContextImpl("functionId", nonFilteringArgs, resultSender);
    SizeExportLogsFunction testFunction = new SizeExportLogsFunction();
    SizeExportLogsFunction spyFunction = spy(testFunction);
    long fakeDiskAvailable = 1024;
    doReturn(fakeDiskAvailable).when(spyFunction).getDiskAvailable(Matchers.any(DistributionConfig.class));
    spyFunction.execute(context);
    assertThatThrownBy(resultSender::getResults).isInstanceOf(ManagementException.class);
}
Also used : FunctionContextImpl(org.apache.geode.internal.cache.execute.FunctionContextImpl) DistributionConfig(org.apache.geode.distributed.internal.DistributionConfig) FunctionContext(org.apache.geode.cache.execute.FunctionContext) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Aggregations

FunctionContext (org.apache.geode.cache.execute.FunctionContext)68 FunctionAdapter (org.apache.geode.cache.execute.FunctionAdapter)38 Test (org.junit.Test)35 HashSet (java.util.HashSet)30 RegionFunctionContext (org.apache.geode.cache.execute.RegionFunctionContext)30 ArrayList (java.util.ArrayList)29 FunctionException (org.apache.geode.cache.execute.FunctionException)28 ResultCollector (org.apache.geode.cache.execute.ResultCollector)28 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)28 Region (org.apache.geode.cache.Region)27 Execution (org.apache.geode.cache.execute.Execution)26 IgnoredException (org.apache.geode.test.dunit.IgnoredException)24 List (java.util.List)20 Set (java.util.Set)19 Iterator (java.util.Iterator)16 FunctionInvocationTargetException (org.apache.geode.cache.execute.FunctionInvocationTargetException)15 InternalDistributedMember (org.apache.geode.distributed.internal.membership.InternalDistributedMember)15 UnitTest (org.apache.geode.test.junit.categories.UnitTest)15 DistributedMember (org.apache.geode.distributed.DistributedMember)14 InternalCache (org.apache.geode.internal.cache.InternalCache)14