Search in sources :

Example 71 with SerializableCallable

use of org.apache.geode.test.dunit.SerializableCallable in project geode by apache.

the class PRTransactionDUnitTest method beginTx.

@SuppressWarnings({ "rawtypes", "serial" })
private SerializableCallable beginTx() {
    return new SerializableCallable("begin tx") {

        @Override
        public Object call() {
            PartitionedRegion pr = (PartitionedRegion) basicGetCache().getRegion(Region.SEPARATOR + CustomerPartitionedRegionName);
            CacheTransactionManager mgr = basicGetCache().getCacheTransactionManager();
            CustId cust1 = new CustId(1);
            mgr.begin();
            Object value = pr.get(cust1);
            assertNotNull(value);
            return mgr.suspend();
        }
    };
}
Also used : CustId(org.apache.geode.internal.cache.execute.data.CustId) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) CacheTransactionManager(org.apache.geode.cache.CacheTransactionManager)

Example 72 with SerializableCallable

use of org.apache.geode.test.dunit.SerializableCallable in project geode by apache.

the class PRFunctionExecutionTimeOutDUnitTest method testRemoteMultiKeyExecution_byName.

/**
   * Test multi-key remote execution by a pure accessor.Then test it using timeout and multiple
   * getResult.
   * 
   * @throws Exception
   */
@Test
public void testRemoteMultiKeyExecution_byName() 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, TEST_FUNCTION_TIMEOUT);
            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);
            final HashSet testKeysSet = new HashSet();
            for (int i = (pr.getTotalNumberOfBuckets() * 2); i > 0; i--) {
                testKeysSet.add("execKey-" + i);
            }
            DistributedSystem.setThreadsSocketPolicy(false);
            Function function = new TestFunction(true, TEST_FUNCTION_TIMEOUT);
            FunctionService.registerFunction(function);
            Execution dataSet = FunctionService.onRegion(pr);
            try {
                dataSet.withFilter(testKeysSet).setArguments(Boolean.TRUE).execute(function.getId());
            } catch (Exception expected) {
                assertTrue(expected.getMessage(), expected.getMessage().contains("No target node found for KEY"));
            }
            int j = 0;
            HashSet origVals = new HashSet();
            for (Iterator i = testKeysSet.iterator(); i.hasNext(); ) {
                Integer val = new Integer(j++);
                origVals.add(val);
                pr.put(i.next(), val);
            }
            ResultCollector rs = dataSet.withFilter(testKeysSet).setArguments(Boolean.TRUE).execute(function.getId());
            List l = ((List) rs.getResult());
            assertEquals(3, l.size());
            for (Iterator i = l.iterator(); i.hasNext(); ) {
                assertEquals(Boolean.TRUE, i.next());
            }
            try {
                rs.getResult();
                fail("Did not get the expected exception.");
            } catch (FunctionException fe) {
                assertTrue(fe.getMessage(), fe.getMessage().contains("Result already collected"));
            }
            // DefaultResultCollector rc2 = new DefaultResultCollector();
            ResultCollector rc2 = dataSet.withFilter(testKeysSet).setArguments(testKeysSet).execute(function.getId());
            List l2 = ((List) rc2.getResult());
            assertEquals(3, l2.size());
            HashSet foundVals = new HashSet();
            for (Iterator i = l2.iterator(); i.hasNext(); ) {
                ArrayList subL = (ArrayList) i.next();
                assertTrue(subL.size() > 0);
                for (Iterator subI = subL.iterator(); subI.hasNext(); ) {
                    assertTrue(foundVals.add(subI.next()));
                }
            }
            assertEquals(origVals, foundVals);
            try {
                rc2.getResult();
                fail("Did not get the expected exception.");
            } catch (FunctionException fe) {
                assertTrue(fe.getMessage(), fe.getMessage().contains("Result already collected"));
            }
            ResultCollector rst = dataSet.withFilter(testKeysSet).setArguments(Boolean.TRUE).execute(function.getId());
            try {
                rst.getResult(1000, TimeUnit.MILLISECONDS);
                fail("Did not get the expected exception.");
            } catch (FunctionException fe) {
                assertTrue(fe.getMessage(), fe.getMessage().contains("All results not recieved in time provided."));
            }
            try {
                rst.getResult();
                fail("Did not get the expected exception.");
            } catch (FunctionException fe) {
                assertTrue(fe.getMessage(), fe.getMessage().contains("Result already collected"));
            }
            ResultCollector rct2 = dataSet.withFilter(testKeysSet).setArguments(testKeysSet).execute(function.getId());
            try {
                rct2.getResult(1000, TimeUnit.MILLISECONDS);
                fail("Did not get the expected exception.");
            } catch (FunctionException fe) {
                assertTrue(fe.getMessage(), fe.getMessage().contains("All results not recieved in time provided."));
            }
            try {
                rct2.getResult();
                fail("Did not get the expected exception.");
            } catch (FunctionException fe) {
                assertTrue(fe.getMessage(), fe.getMessage().contains("Result already collected"));
            }
            return Boolean.TRUE;
        }
    });
    assertEquals(Boolean.TRUE, o);
}
Also used : TestFunction(org.apache.geode.internal.cache.functions.TestFunction) RegionAttributes(org.apache.geode.cache.RegionAttributes) FunctionException(org.apache.geode.cache.execute.FunctionException) ArrayList(java.util.ArrayList) Host(org.apache.geode.test.dunit.Host) FunctionException(org.apache.geode.cache.execute.FunctionException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) 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) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 73 with SerializableCallable

use of org.apache.geode.test.dunit.SerializableCallable in project geode by apache.

the class PRFunctionExecutionTimeOutDUnitTest method testRemoteMultiKeyExecution_timeout.

/**
   * Test multi-key remote execution by a pure accessor.
   * 
   * @throws Exception
   */
@Test
public void testRemoteMultiKeyExecution_timeout() 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, TEST_FUNCTION_TIMEOUT);
            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);
            final HashSet testKeysSet = new HashSet();
            for (int i = (pr.getTotalNumberOfBuckets() * 2); i > 0; i--) {
                testKeysSet.add("execKey-" + i);
            }
            DistributedSystem.setThreadsSocketPolicy(false);
            Function function = new TestFunction(true, TEST_FUNCTION_TIMEOUT);
            FunctionService.registerFunction(function);
            Execution dataSet = FunctionService.onRegion(pr);
            int j = 0;
            HashSet origVals = new HashSet();
            for (Iterator i = testKeysSet.iterator(); i.hasNext(); ) {
                Integer val = new Integer(j++);
                origVals.add(val);
                pr.put(i.next(), val);
            }
            ResultCollector rs = dataSet.withFilter(testKeysSet).setArguments("TestingTimeOut").execute(function.getId());
            List l = ((List) rs.getResult(8000, TimeUnit.MILLISECONDS));
            // this test may fail..but rarely
            assertEquals(3, l.size());
            ResultCollector rst = dataSet.withFilter(testKeysSet).setArguments("TestingTimeOut").execute(function.getId());
            rst.getResult(8000, TimeUnit.MILLISECONDS);
            assertEquals(3, l.size());
            try {
                rs.getResult();
                fail("Did not get the expected exception.");
            } catch (FunctionException fe) {
                assertTrue(fe.getMessage(), fe.getMessage().contains("Result already collected"));
            }
            return Boolean.TRUE;
        }
    });
    assertEquals(Boolean.TRUE, o);
}
Also used : TestFunction(org.apache.geode.internal.cache.functions.TestFunction) RegionAttributes(org.apache.geode.cache.RegionAttributes) FunctionException(org.apache.geode.cache.execute.FunctionException) Host(org.apache.geode.test.dunit.Host) FunctionException(org.apache.geode.cache.execute.FunctionException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) 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) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 74 with SerializableCallable

use of org.apache.geode.test.dunit.SerializableCallable in project geode by apache.

the class PRFunctionExecutionWithResultSenderDUnitTest method testlonerSystem_Bug41832.

@Test
public void testlonerSystem_Bug41832() throws Exception {
    final String rName = getUniqueName();
    Host host = Host.getHost(0);
    final VM datastore = host.getVM(3);
    Object o = datastore.invoke(new SerializableCallable("Create region") {

        public Object call() throws Exception {
            // creates loner cache
            createLonerCache();
            AttributesFactory factory = new AttributesFactory();
            factory.setDataPolicy(DataPolicy.EMPTY);
            Region region = getCache().createRegion(rName, factory.create());
            Function function = new TestFunction(true, TEST_FUNCTION2);
            FunctionService.registerFunction(function);
            Execution dataSet = FunctionService.onRegion(region);
            try {
                ResultCollector rc = dataSet.setArguments(Boolean.TRUE).execute(function.getId());
                Object o = rc.getResult();
                fail("Expected Function Exception");
            } catch (Exception expected) {
                assertTrue(expected.getMessage().startsWith("No Replicated Region found for executing function"));
                return Boolean.TRUE;
            }
            return Boolean.FALSE;
        }
    });
    assertEquals(Boolean.TRUE, o);
}
Also used : Function(org.apache.geode.cache.execute.Function) TestFunction(org.apache.geode.internal.cache.functions.TestFunction) AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) Execution(org.apache.geode.cache.execute.Execution) TestFunction(org.apache.geode.internal.cache.functions.TestFunction) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) Region(org.apache.geode.cache.Region) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Host(org.apache.geode.test.dunit.Host) ResultCollector(org.apache.geode.cache.execute.ResultCollector) FunctionException(org.apache.geode.cache.execute.FunctionException) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 75 with SerializableCallable

use of org.apache.geode.test.dunit.SerializableCallable in project geode by apache.

the class PRFunctionExecutionWithResultSenderDUnitTest method testExecutionOnAllNodes_byName.

@Test
public void testExecutionOnAllNodes_byName() throws Exception {
    final String rName = getUniqueName();
    Host host = Host.getHost(0);
    final VM datastore0 = host.getVM(0);
    final VM datastore1 = host.getVM(1);
    final VM datastore2 = host.getVM(2);
    final VM datastore3 = host.getVM(3);
    getCache();
    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());
            pa.setTotalNumBuckets(17);
            raf.setPartitionAttributes(pa);
            getCache().createRegion(rName, raf.create());
            Function function = new TestFunction(true, TestFunction.TEST_FUNCTION9);
            FunctionService.registerFunction(function);
            return Boolean.TRUE;
        }
    };
    datastore0.invoke(dataStoreCreate);
    datastore1.invoke(dataStoreCreate);
    datastore2.invoke(dataStoreCreate);
    datastore3.invoke(dataStoreCreate);
    Object o = datastore3.invoke(new SerializableCallable("Create data, invoke exectuable") {

        public Object call() throws Exception {
            PartitionedRegion pr = (PartitionedRegion) getCache().getRegion(rName);
            DistributedSystem.setThreadsSocketPolicy(false);
            final HashSet testKeys = new HashSet();
            for (int i = (pr.getTotalNumberOfBuckets() * 3); i > 0; i--) {
                testKeys.add("execKey-" + i);
            }
            int j = 0;
            for (Iterator i = testKeys.iterator(); i.hasNext(); ) {
                Integer val = new Integer(j++);
                pr.put(i.next(), val);
            }
            // Assert there is data in each bucket
            for (int bid = 0; bid < pr.getTotalNumberOfBuckets(); bid++) {
                assertTrue(pr.getBucketKeys(bid).size() > 0);
            }
            Function function = new TestFunction(true, TestFunction.TEST_FUNCTION9);
            FunctionService.registerFunction(function);
            Execution dataSet = FunctionService.onRegion(pr);
            ResultCollector rc1 = dataSet.setArguments(Boolean.TRUE).execute(function.getId());
            List l = ((List) rc1.getResult());
            LogWriterUtils.getLogWriter().info("PRFunctionExecutionDUnitTest#testExecutionOnAllNodes_byName : Result size :" + l.size() + " Result : " + l);
            assertEquals(4, l.size());
            for (int i = 0; i < 4; i++) {
                assertEquals(Boolean.TRUE, l.iterator().next());
            }
            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) FunctionException(org.apache.geode.cache.execute.FunctionException) Function(org.apache.geode.cache.execute.Function) TestFunction(org.apache.geode.internal.cache.functions.TestFunction) AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) 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)

Aggregations

SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)502 VM (org.apache.geode.test.dunit.VM)326 Test (org.junit.Test)314 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)311 Host (org.apache.geode.test.dunit.Host)306 Region (org.apache.geode.cache.Region)224 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)157 IgnoredException (org.apache.geode.test.dunit.IgnoredException)155 AttributesFactory (org.apache.geode.cache.AttributesFactory)139 Cache (org.apache.geode.cache.Cache)109 PartitionAttributesFactory (org.apache.geode.cache.PartitionAttributesFactory)95 FunctionException (org.apache.geode.cache.execute.FunctionException)88 ArrayList (java.util.ArrayList)83 HashSet (java.util.HashSet)83 CacheLoaderException (org.apache.geode.cache.CacheLoaderException)77 Execution (org.apache.geode.cache.execute.Execution)74 CommitConflictException (org.apache.geode.cache.CommitConflictException)70 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)66 Function (org.apache.geode.cache.execute.Function)63 IOException (java.io.IOException)62