Search in sources :

Example 31 with Function

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

the class FunctionCommandsDUnitTest method testDestroyOnMember.

@Test
public void testDestroyOnMember() {
    setUpJmxManagerOnVm0ThenConnect(null);
    Function function = new TestFunction(true, TestFunction.TEST_FUNCTION1);
    FunctionService.registerFunction(function);
    final VM vm1 = Host.getHost(0).getVM(1);
    final String vm1MemberId = (String) vm1.invoke(() -> getMemberId());
    String command = "destroy function --id=" + function.getId() + " --member=" + vm1MemberId;
    getLogWriter().info("testDestroyOnMember command=" + command);
    CommandResult cmdResult = executeCommand(command);
    if (cmdResult != null) {
        String strCmdResult = commandResultToString(cmdResult);
        assertEquals(Result.Status.OK, cmdResult.getStatus());
        getLogWriter().info("testDestroyOnMember strCmdResult=" + strCmdResult);
        assertTrue(strCmdResult.contains("Destroyed TestFunction1 Successfully"));
    } else {
        fail("testDestroyOnMember failed as did not get 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) 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 Function

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

the class FunctionCommandsDUnitTest method testExecuteFunctionOnRegionBug51480.

@Test
public void testExecuteFunctionOnRegionBug51480() {
    setupForBug51480();
    // check if DistributedRegionMXBean is available so that command will not fail
    final VM manager = Host.getHost(0).getVM(0);
    manager.invoke(checkRegionMBeans);
    final Function function = new TestFunction(true, TestFunction.TEST_FUNCTION1);
    Host.getHost(0).getVM(0).invoke(new SerializableRunnable() {

        public void run() {
            FunctionService.registerFunction(function);
        }
    });
    String command = "execute function --id=" + function.getId() + " --region=" + REGION_ONE;
    getLogWriter().info("testExecuteFunctionOnRegionBug51480 command=" + command);
    CommandResult cmdResult = executeCommand(command);
    if (cmdResult != null) {
        getLogWriter().info("testExecuteFunctionOnRegionBug51480 cmdResult=" + cmdResult);
        assertEquals(Result.Status.OK, cmdResult.getStatus());
        String stringResult = commandResultToString(cmdResult);
        getLogWriter().info("testExecuteFunctionOnRegionBug51480 stringResult=" + stringResult);
        assertTrue(stringResult.contains("Execution summary"));
    } else {
        fail("testExecuteFunctionOnRegionBug51480 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) 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 33 with Function

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

the class FunctionCommandsDUnitTest method setupWith2Regions.

void setupWith2Regions() {
    final VM vm1 = Host.getHost(0).getVM(1);
    final VM vm2 = Host.getHost(0).getVM(2);
    setUpJmxManagerOnVm0ThenConnect(null);
    vm1.invoke(new SerializableRunnable() {

        public void run() {
            final Function function = new TestFunction(true, TestFunction.TEST_FUNCTION1);
            FunctionService.registerFunction(function);
            // no need to close cache as it will be closed as part of teardown2
            Cache cache = getCache();
            RegionFactory<Integer, Integer> dataRegionFactory = cache.createRegionFactory(RegionShortcut.PARTITION);
            Region region = dataRegionFactory.create("RegionOne");
            for (int i = 0; i < 10; i++) {
                region.put("key" + (i + 200), "value" + (i + 200));
            }
            region = dataRegionFactory.create("RegionTwo");
            for (int i = 0; i < 1000; i++) {
                region.put("key" + (i + 200), "value" + (i + 200));
            }
        }
    });
    vm2.invoke(new SerializableRunnable() {

        public void run() {
            final Function function = new TestFunction(true, TestFunction.TEST_FUNCTION1);
            FunctionService.registerFunction(function);
            // no need to close cache as it will be closed as part of teardown2
            Cache cache = getCache();
            RegionFactory<Integer, Integer> dataRegionFactory = cache.createRegionFactory(RegionShortcut.PARTITION);
            Region region = dataRegionFactory.create("RegionOne");
            for (int i = 0; i < 10000; i++) {
                region.put("key" + (i + 400), "value" + (i + 400));
            }
            region = dataRegionFactory.create("Regiontwo");
            for (int i = 0; i < 10; i++) {
                region.put("key" + (i + 200), "value" + (i + 200));
            }
        }
    });
}
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) Cache(org.apache.geode.cache.Cache)

Example 34 with Function

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

the class FunctionCommandsDUnitTest method testExecuteFunctionOnGroups.

// GEODE-1563: JMX RMI (java.rmi.NoSuchObjectException: no such object
@Category(FlakyTest.class)
// in table)
@Test
public void testExecuteFunctionOnGroups() {
    Properties localProps = new Properties();
    localProps.setProperty(NAME, "Manager");
    localProps.setProperty(GROUPS, "Group0");
    setUpJmxManagerOnVm0ThenConnect(localProps);
    Function function = new TestFunction(true, TestFunction.TEST_FUNCTION1);
    FunctionService.registerFunction(function);
    VM vm1 = Host.getHost(0).getVM(1);
    VM vm2 = Host.getHost(0).getVM(2);
    String vm1id = (String) vm1.invoke(new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            Properties localProps = new Properties();
            localProps.setProperty(GROUPS, "Group1");
            getSystem(localProps);
            Cache cache = getCache();
            Function function = new TestFunction(true, TestFunction.TEST_FUNCTION1);
            FunctionService.registerFunction(function);
            return cache.getDistributedSystem().getDistributedMember().getId();
        }
    });
    String vm2id = (String) vm2.invoke(new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            Properties localProps = new Properties();
            localProps.setProperty(GROUPS, "Group2");
            getSystem(localProps);
            Cache cache = getCache();
            Function function = new TestFunction(true, TestFunction.TEST_FUNCTION1);
            FunctionService.registerFunction(function);
            return cache.getDistributedSystem().getDistributedMember().getId();
        }
    });
    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);
            Function function = new TestFunction(true, TestFunction.TEST_FUNCTION1);
            assertNotNull(region);
            FunctionService.registerFunction(function);
        }
    });
    String command = "execute function --id=" + TestFunction.TEST_FUNCTION1 + " --groups=Group1,Group2";
    getLogWriter().info("testExecuteFunctionOnGroups command=" + command);
    CommandResult cmdResult = executeCommand(command);
    getLogWriter().info("testExecuteFunctionOnGroups cmdResult=" + cmdResult);
    assertEquals(Result.Status.OK, cmdResult.getStatus());
    TabularResultData resultData = (TabularResultData) cmdResult.getResultData();
    List<String> members = resultData.retrieveAllValues("Member ID/Name");
    getLogWriter().info("testExecuteFunctionOnGroups members=" + members);
    assertTrue(members.size() == 2 && members.contains(vm1id) && members.contains(vm2id));
}
Also used : TestFunction(org.apache.geode.internal.cache.functions.TestFunction) TabularResultData(org.apache.geode.management.internal.cli.result.TabularResultData) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) CommandResult(org.apache.geode.management.internal.cli.result.CommandResult) Function(org.apache.geode.cache.execute.Function) TestFunction(org.apache.geode.internal.cache.functions.TestFunction) RegionFactory(org.apache.geode.cache.RegionFactory) Region(org.apache.geode.cache.Region) Cache(org.apache.geode.cache.Cache) Category(org.junit.experimental.categories.Category) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 35 with Function

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

the class FunctionCommandsDUnitTest method testDestroyOnGroups.

@Test
public void testDestroyOnGroups() {
    Properties localProps = new Properties();
    localProps.setProperty(NAME, "Manager");
    localProps.setProperty(GROUPS, "Group0");
    setUpJmxManagerOnVm0ThenConnect(localProps);
    Function function = new TestFunction(true, TestFunction.TEST_FUNCTION1);
    FunctionService.registerFunction(function);
    VM vm1 = Host.getHost(0).getVM(1);
    VM vm2 = Host.getHost(0).getVM(2);
    String vm1id = (String) vm1.invoke(new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            Properties localProps = new Properties();
            localProps.setProperty(GROUPS, "Group1");
            getSystem(localProps);
            Cache cache = getCache();
            Function function = new TestFunction(true, TestFunction.TEST_FUNCTION1);
            FunctionService.registerFunction(function);
            return cache.getDistributedSystem().getDistributedMember().getId();
        }
    });
    String vm2id = (String) vm2.invoke(new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            Properties localProps = new Properties();
            localProps.setProperty(GROUPS, "Group2");
            getSystem(localProps);
            Cache cache = getCache();
            Function function = new TestFunction(true, TestFunction.TEST_FUNCTION1);
            FunctionService.registerFunction(function);
            return cache.getDistributedSystem().getDistributedMember().getId();
        }
    });
    Host.getHost(0).getVM(0).invoke(new SerializableRunnable() {

        public void run() {
            Function function = new TestFunction(true, TestFunction.TEST_FUNCTION1);
            FunctionService.registerFunction(function);
        }
    });
    String command = "destroy function --id=" + TestFunction.TEST_FUNCTION1 + " --groups=Group1,Group2";
    getLogWriter().info("testDestroyOnGroups command=" + command);
    CommandResult cmdResult = executeCommand(command);
    getLogWriter().info("testDestroyOnGroups cmdResult=" + cmdResult);
    assertEquals(Result.Status.OK, cmdResult.getStatus());
    String content = null;
    try {
        content = cmdResult.getContent().get("message").toString();
        getLogWriter().info("testDestroyOnGroups content = " + content);
    } catch (GfJsonException e) {
        fail("testDestroyOnGroups exception=" + e);
    }
    assertNotNull(content);
    assertTrue(content.equals("[\"Destroyed " + TestFunction.TEST_FUNCTION1 + " Successfully on " + vm1id + "," + vm2id + "\"]") || content.equals("[\"Destroyed " + TestFunction.TEST_FUNCTION1 + " Successfully on " + vm2id + "," + vm1id + "\"]"));
}
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) GfJsonException(org.apache.geode.management.internal.cli.json.GfJsonException) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) Cache(org.apache.geode.cache.Cache) 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)

Aggregations

Function (org.apache.geode.cache.execute.Function)261 TestFunction (org.apache.geode.internal.cache.functions.TestFunction)204 Test (org.junit.Test)156 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)148 HashSet (java.util.HashSet)124 FunctionException (org.apache.geode.cache.execute.FunctionException)122 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)122 Execution (org.apache.geode.cache.execute.Execution)121 ArrayList (java.util.ArrayList)110 ResultCollector (org.apache.geode.cache.execute.ResultCollector)110 List (java.util.List)86 Region (org.apache.geode.cache.Region)83 IgnoredException (org.apache.geode.test.dunit.IgnoredException)75 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)74 Iterator (java.util.Iterator)68 ClientServerTest (org.apache.geode.test.junit.categories.ClientServerTest)67 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)63 VM (org.apache.geode.test.dunit.VM)62 Host (org.apache.geode.test.dunit.Host)61 AttributesFactory (org.apache.geode.cache.AttributesFactory)56