Search in sources :

Example 6 with FunctionContext

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

the class ListDiskStoresFunctionJUnitTest method testExecuteOnMemberWithANonGemFireCache.

@Test
@SuppressWarnings("unchecked")
public void testExecuteOnMemberWithANonGemFireCache() throws Throwable {
    final Cache mockCache = mockContext.mock(Cache.class, "Cache");
    final FunctionContext mockFunctionContext = mockContext.mock(FunctionContext.class, "FunctionContext");
    final TestResultSender testResultSender = new TestResultSender();
    mockContext.checking(new Expectations() {

        {
            oneOf(mockFunctionContext).getResultSender();
            will(returnValue(testResultSender));
        }
    });
    final ListDiskStoresFunction function = createListDiskStoresFunction(mockCache);
    function.execute(mockFunctionContext);
    final List<?> results = testResultSender.getResults();
    assertNotNull(results);
    assertEquals(1, results.size());
    final Set<DiskStoreDetails> diskStoreDetails = (Set<DiskStoreDetails>) results.get(0);
    assertNotNull(diskStoreDetails);
    assertTrue(diskStoreDetails.isEmpty());
}
Also used : Expectations(org.jmock.Expectations) Set(java.util.Set) DiskStoreDetails(org.apache.geode.management.internal.cli.domain.DiskStoreDetails) FunctionContext(org.apache.geode.cache.execute.FunctionContext) InternalCache(org.apache.geode.internal.cache.InternalCache) Cache(org.apache.geode.cache.Cache) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Example 7 with FunctionContext

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

the class ListDiskStoresFunctionJUnitTest method testExecuteThrowsRuntimeException.

@Test(expected = RuntimeException.class)
public void testExecuteThrowsRuntimeException() throws Throwable {
    final InternalCache mockCache = mockContext.mock(InternalCache.class, "Cache");
    final InternalDistributedMember mockMember = mockContext.mock(InternalDistributedMember.class, "DistributedMember");
    final FunctionContext mockFunctionContext = mockContext.mock(FunctionContext.class, "FunctionContext");
    final TestResultSender testResultSender = new TestResultSender();
    mockContext.checking(new Expectations() {

        {
            oneOf(mockCache).getMyId();
            will(returnValue(mockMember));
            oneOf(mockCache).listDiskStoresIncludingRegionOwned();
            will(throwException(new RuntimeException("expected")));
            oneOf(mockFunctionContext).getResultSender();
            will(returnValue(testResultSender));
        }
    });
    final ListDiskStoresFunction function = createListDiskStoresFunction(mockCache);
    function.execute(mockFunctionContext);
    try {
        testResultSender.getResults();
    } catch (Throwable throwable) {
        assertTrue(throwable instanceof RuntimeException);
        assertEquals("expected", throwable.getMessage());
        throw throwable;
    }
}
Also used : Expectations(org.jmock.Expectations) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) InternalCache(org.apache.geode.internal.cache.InternalCache) FunctionContext(org.apache.geode.cache.execute.FunctionContext) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Example 8 with FunctionContext

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

the class ListIndexFunctionJUnitTest method testExecuteThrowsException.

@Test(expected = RuntimeException.class)
public void testExecuteThrowsException() throws Throwable {
    final Cache mockCache = mockContext.mock(Cache.class, "Cache");
    final DistributedSystem mockDistributedSystem = mockContext.mock(DistributedSystem.class, "DistributedSystem");
    final DistributedMember mockDistributedMember = mockContext.mock(DistributedMember.class, "DistributedMember");
    final QueryService mockQueryService = mockContext.mock(QueryService.class, "QueryService");
    final FunctionContext mockFunctionContext = mockContext.mock(FunctionContext.class, "FunctionContext");
    final TestResultSender testResultSender = new TestResultSender();
    mockContext.checking(new Expectations() {

        {
            oneOf(mockCache).getDistributedSystem();
            will(returnValue(mockDistributedSystem));
            oneOf(mockCache).getQueryService();
            will(returnValue(mockQueryService));
            oneOf(mockDistributedSystem).getDistributedMember();
            will(returnValue(mockDistributedMember));
            oneOf(mockQueryService).getIndexes();
            will(throwException(new RuntimeException("expected")));
            oneOf(mockFunctionContext).getResultSender();
            will(returnValue(testResultSender));
        }
    });
    final ListIndexFunction function = createListIndexFunction(mockCache);
    function.execute(mockFunctionContext);
    try {
        testResultSender.getResults();
    } catch (Throwable t) {
        assertTrue(t instanceof RuntimeException);
        assertEquals("expected", t.getMessage());
        throw t;
    }
}
Also used : Expectations(org.jmock.Expectations) QueryService(org.apache.geode.cache.query.QueryService) DistributedMember(org.apache.geode.distributed.DistributedMember) DistributedSystem(org.apache.geode.distributed.DistributedSystem) FunctionContext(org.apache.geode.cache.execute.FunctionContext) Cache(org.apache.geode.cache.Cache) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Example 9 with FunctionContext

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

the class ExportLogsFunctionIntegrationTest method verifyExportLogsFunctionDoesNotBlowUp.

public static void verifyExportLogsFunctionDoesNotBlowUp() throws Throwable {
    ExportLogsFunction.Args args = new ExportLogsFunction.Args(null, null, "info", false, false, false);
    CapturingResultSender resultSender = new CapturingResultSender();
    FunctionContext context = new FunctionContextImpl("functionId", args, resultSender);
    new ExportLogsFunction().execute(context);
    if (resultSender.getThrowable() != null) {
        throw resultSender.getThrowable();
    }
}
Also used : FunctionContextImpl(org.apache.geode.internal.cache.execute.FunctionContextImpl) FunctionContext(org.apache.geode.cache.execute.FunctionContext)

Example 10 with FunctionContext

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

the class FunctionServiceStatsDUnitTest method testP2PMembersFunctionExecutionStats.

/**
   * Test the execution of function on all memebers haveResults = true
   * 
   * member1 calls for the function executions sp the results received on memeber 1 should be equal
   * to the no of function execution calls. Function Execution should happen on all other members
   * too. so the no of function execution calls and no of function executions completed should be
   * equal tio the no of functions from member 1
   */
@Test
public void testP2PMembersFunctionExecutionStats() throws Exception {
    Host host = Host.getHost(0);
    VM member1 = host.getVM(0);
    VM member2 = host.getVM(1);
    VM member3 = host.getVM(2);
    VM member4 = host.getVM(3);
    SerializableCallable connectToDistributedSystem = new SerializableCallable("connectToDistributedSystem") {

        public Object call() throws Exception {
            Properties props = new Properties();
            try {
                ds = getSystem(props);
                assertNotNull(ds);
            } catch (Exception e) {
                Assert.fail("Failed while creating the Distribued System", e);
            }
            return Boolean.TRUE;
        }
    };
    member1.invoke(connectToDistributedSystem);
    member2.invoke(connectToDistributedSystem);
    member3.invoke(connectToDistributedSystem);
    member4.invoke(connectToDistributedSystem);
    member1.invoke(initializeStats);
    member2.invoke(initializeStats);
    member3.invoke(initializeStats);
    member4.invoke(initializeStats);
    final int noOfMembers = 1;
    final Function inlineFunction = new FunctionAdapter() {

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

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

        public boolean hasResult() {
            return true;
        }
    };
    member1.invoke(new SerializableCallable("excuteOnMembers_InlineFunction") {

        public Object call() throws Exception {
            assertNotNull(ds);
            Execution memberExecution = null;
            DistributedMember localmember = ds.getDistributedMember();
            memberExecution = FunctionService.onMember(localmember);
            memberExecution.setArguments("Key");
            try {
                ResultCollector rc = memberExecution.execute(inlineFunction);
                int size = ((List) rc.getResult()).size();
                resultReceived_Aggregate += size;
                noOfExecutionCalls_Aggregate++;
                noOfExecutionsCompleted_Aggregate++;
                resultReceived_Inline += size;
                noOfExecutionCalls_Inline++;
                noOfExecutionsCompleted_Inline++;
            } catch (Exception e) {
                LogWriterUtils.getLogWriter().info("Exception Occurred : " + e.getMessage());
                e.printStackTrace();
                Assert.fail("Test failed", e);
            }
            return Boolean.TRUE;
        }
    });
    member1.invoke(new SerializableCallable("checkFunctionExecutionStatsForMember1") {

        public Object call() throws Exception {
            FunctionServiceStats functionServiceStats = ds.getFunctionServiceStats();
            waitNoFunctionsRunning(functionServiceStats);
            assertEquals(noOfExecutionCalls_Aggregate, functionServiceStats.getFunctionExecutionCalls());
            assertEquals(noOfExecutionsCompleted_Aggregate, functionServiceStats.getFunctionExecutionsCompleted());
            assertEquals(resultReceived_Aggregate, functionServiceStats.getResultsReceived());
            FunctionStats functionStats = FunctionStats.getFunctionStats(inlineFunction.getId(), ds);
            assertEquals(noOfExecutionCalls_Inline, functionStats.getFunctionExecutionCalls());
            assertEquals(noOfExecutionsCompleted_Inline, functionStats.getFunctionExecutionsCompleted());
            assertEquals(resultReceived_Inline, functionStats.getResultsReceived());
            return Boolean.TRUE;
        }
    });
    SerializableCallable checkFunctionExecutionStatsForOtherMember = new SerializableCallable("checkFunctionExecutionStatsForOtherMember") {

        public Object call() throws Exception {
            FunctionServiceStats functionServiceStats = ds.getFunctionServiceStats();
            waitNoFunctionsRunning(functionServiceStats);
            // One function Execution took place on there members
            // noOfExecutionCalls_Aggregate++;
            // noOfExecutionsCompleted_Aggregate++;
            assertEquals(noOfExecutionCalls_Aggregate, functionServiceStats.getFunctionExecutionCalls());
            assertEquals(noOfExecutionsCompleted_Aggregate, functionServiceStats.getFunctionExecutionsCompleted());
            FunctionStats functionStats = FunctionStats.getFunctionStats(inlineFunction.getId(), ds);
            // noOfExecutionCalls_Inline++;
            // noOfExecutionsCompleted_Inline++;
            assertEquals(noOfExecutionCalls_Inline, functionStats.getFunctionExecutionCalls());
            assertEquals(noOfExecutionsCompleted_Inline, functionStats.getFunctionExecutionsCompleted());
            return Boolean.TRUE;
        }
    };
    member2.invoke(checkFunctionExecutionStatsForOtherMember);
    member3.invoke(checkFunctionExecutionStatsForOtherMember);
    member4.invoke(checkFunctionExecutionStatsForOtherMember);
    SerializableCallable closeDistributedSystem = new SerializableCallable("closeDistributedSystem") {

        public Object call() throws Exception {
            if (getCache() != null && !getCache().isClosed()) {
                getCache().close();
                getCache().getDistributedSystem().disconnect();
            }
            return Boolean.TRUE;
        }
    };
    member1.invoke(closeDistributedSystem);
    member2.invoke(closeDistributedSystem);
    member3.invoke(closeDistributedSystem);
    member4.invoke(closeDistributedSystem);
}
Also used : Host(org.apache.geode.test.dunit.Host) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) FunctionException(org.apache.geode.cache.execute.FunctionException) IOException(java.io.IOException) FunctionContext(org.apache.geode.cache.execute.FunctionContext) Function(org.apache.geode.cache.execute.Function) TestFunction(org.apache.geode.internal.cache.functions.TestFunction) Execution(org.apache.geode.cache.execute.Execution) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) DistributedMember(org.apache.geode.distributed.DistributedMember) FunctionAdapter(org.apache.geode.cache.execute.FunctionAdapter) ResultCollector(org.apache.geode.cache.execute.ResultCollector) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

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