use of org.apache.geode.test.dunit.SerializableCallable in project geode by apache.
the class OnGroupsFunctionExecutionDUnitTest method dotestBasicClientServerFunction.
private void dotestBasicClientServerFunction(final boolean register, final boolean withArgs) {
Host host = Host.getHost(0);
VM server0 = host.getVM(0);
VM server1 = host.getVM(1);
VM server2 = host.getVM(2);
VM client = host.getVM(3);
VM locator = Host.getLocator();
final String regionName = getName();
initVM(server0, "mg,g0", regionName, true);
initVM(server1, "g1", regionName, true);
initVM(server2, "g0,g1", regionName, true);
if (register) {
registerFunction(server0);
registerFunction(server1);
registerFunction(server2);
}
final int locatorPort = getLocatorPort(locator);
final String hostName = host.getHostName();
client.invoke(new SerializableCallable() {
@Override
public Object call() throws Exception {
try {
Cache c = CacheFactory.getAnyInstance();
c.close();
} catch (CacheClosedException cce) {
}
disconnectFromDS();
LogWriterUtils.getLogWriter().fine("SWAP:creating client cache");
ClientCacheFactory ccf = new ClientCacheFactory();
ccf.addPoolLocator(hostName, locatorPort);
ccf.setPoolServerGroup("mg");
ccf.set(LOG_LEVEL, LogWriterUtils.getDUnitLogLevel());
ClientCache c = ccf.create();
c.getLogger().info("SWAP:invoking function from client on g0");
Execution e = InternalFunctionService.onServers(c, "g0");
if (withArgs) {
ArrayList<String> args = new ArrayList<String>();
args.add("g0");
e = e.setArguments(args);
}
if (register) {
e.execute(OnGroupsFunction.Id).getResult();
} else {
e.execute(new OnGroupsFunction()).getResult();
}
return null;
}
});
verifyAndResetInvocationCount(server0, 1);
verifyAndResetInvocationCount(server1, 0);
verifyAndResetInvocationCount(server2, 1);
client.invoke(new SerializableCallable() {
@Override
public Object call() throws Exception {
ClientCache c = ClientCacheFactory.getAnyInstance();
c.getLogger().fine("SWAP:invoking function from client on mg");
Execution e = InternalFunctionService.onServers(c, "mg");
if (withArgs) {
ArrayList<String> args = new ArrayList<String>();
args.add("mg");
e = e.setArguments(args);
}
if (register) {
e.execute(OnGroupsFunction.Id).getResult();
} else {
e.execute(new OnGroupsFunction()).getResult();
}
return null;
}
});
verifyAndResetInvocationCount(server0, 1);
verifyAndResetInvocationCount(server1, 0);
verifyAndResetInvocationCount(server2, 0);
client.invoke(new SerializableCallable() {
@Override
public Object call() throws Exception {
ClientCache c = ClientCacheFactory.getAnyInstance();
c.getLogger().fine("SWAP:invoking function from client on g0 g1");
Execution e = InternalFunctionService.onServers(c, "g0", "g1");
if (withArgs) {
ArrayList<String> args = new ArrayList<String>();
args.add("g0");
args.add("g1");
e = e.setArguments(args);
}
if (register) {
e.execute(OnGroupsFunction.Id).getResult();
} else {
e.execute(new OnGroupsFunction()).getResult();
}
return null;
}
});
verifyAndResetInvocationCount(server0, 1);
verifyAndResetInvocationCount(server1, 1);
verifyAndResetInvocationCount(server2, 1);
}
use of org.apache.geode.test.dunit.SerializableCallable in project geode by apache.
the class OnGroupsFunctionExecutionDUnitTest method testClientServerOneMemberFailure.
@Test
public void testClientServerOneMemberFailure() {
Host host = Host.getHost(0);
VM server0 = host.getVM(0);
VM server1 = host.getVM(1);
VM server2 = host.getVM(2);
VM client = host.getVM(3);
VM locator = Host.getLocator();
final String regionName = getName();
initVM(server0, "mg,g0", regionName, true);
initVM(server1, "g1", regionName, true);
initVM(server2, "g0,g1,g2", regionName, true);
final int locatorPort = getLocatorPort(locator);
final String hostName = host.getHostName();
client.invoke(new SerializableCallable() {
@Override
public Object call() throws Exception {
try {
Cache c = CacheFactory.getAnyInstance();
c.close();
} catch (CacheClosedException cce) {
}
disconnectFromDS();
LogWriterUtils.getLogWriter().fine("SWAP:creating client cache");
ClientCacheFactory ccf = new ClientCacheFactory();
ccf.addPoolLocator(hostName, locatorPort);
ccf.setPoolServerGroup("mg");
ccf.set(LOG_LEVEL, LogWriterUtils.getDUnitLogLevel());
ClientCache c = ccf.create();
Execution e = InternalFunctionService.onServers(c, "g1");
ArrayList<String> args = new ArrayList<String>();
args.add("disconnect");
args.add("g2");
e = e.setArguments(args);
IgnoredException.addIgnoredException("FunctionInvocationTargetException");
try {
e.execute(new OnGroupsExceptionFunction()).getResult();
fail("expected exception not thrown");
} catch (FunctionException ex) {
assertTrue(ex.getCause() instanceof FunctionInvocationTargetException);
}
return null;
}
});
}
use of org.apache.geode.test.dunit.SerializableCallable in project geode by apache.
the class PRTransactionDUnitTest method basicPRTXInCacheListener.
/**
* This method executes a transaction inside a cache listener
*
* @param bucketRedundancy redundancy for the colocated PRs
*/
protected void basicPRTXInCacheListener(int bucketRedundancy) {
createCacheInAllVms();
redundancy = new Integer(bucketRedundancy);
localMaxmemory = new Integer(50);
totalNumBuckets = new Integer(11);
// Create Customer PartitionedRegion in All VMs
createPRWithCoLocation(CustomerPartitionedRegionName, null);
createPRWithCoLocation(OrderPartitionedRegionName, CustomerPartitionedRegionName);
// register Cache Listeners
SerializableCallable registerListeners = new SerializableCallable() {
public Object call() throws Exception {
Region custRegion = basicGetCache().getRegion(Region.SEPARATOR + CustomerPartitionedRegionName);
custRegion.getAttributesMutator().addCacheListener(new TransactionListener());
return null;
}
};
dataStore1.invoke(registerListeners);
dataStore2.invoke(registerListeners);
dataStore3.invoke(registerListeners);
// Put the customer 1-10 in CustomerPartitionedRegion
accessor.invoke(() -> PRColocationDUnitTest.putCustomerPartitionedRegion(CustomerPartitionedRegionName));
dataStore1.invoke(() -> PRTransactionDUnitTest.validatePRTXInCacheListener());
dataStore2.invoke(() -> PRTransactionDUnitTest.validatePRTXInCacheListener());
dataStore3.invoke(() -> PRTransactionDUnitTest.validatePRTXInCacheListener());
}
use of org.apache.geode.test.dunit.SerializableCallable in project geode by apache.
the class PRTransactionDUnitTest method basicPRTXInFunction.
/**
* Test all the basic functionality of colocated transactions. This method invokes
* {@link MyTransactionFunction} and tells it what to test, using different arguments.
*
* @param redundantBuckets redundant buckets for colocated PRs
* @param populateData if false tests are carried out on empty colocated PRs
*/
protected void basicPRTXInFunction(int redundantBuckets, boolean populateData) {
if (populateData) {
createPopulateAndVerifyCoLocatedPRs(redundantBuckets);
} else {
createColocatedPRs(redundantBuckets);
}
SerializableCallable registerFunction = new SerializableCallable("register Fn") {
public Object call() throws Exception {
Function txFunction = new MyTransactionFunction();
FunctionService.registerFunction(txFunction);
return Boolean.TRUE;
}
};
dataStore1.invoke(registerFunction);
dataStore2.invoke(registerFunction);
dataStore3.invoke(registerFunction);
accessor.invoke(new SerializableCallable("run function") {
public Object call() throws Exception {
PartitionedRegion pr = (PartitionedRegion) basicGetCache().getRegion(Region.SEPARATOR + CustomerPartitionedRegionName);
PartitionedRegion orderpr = (PartitionedRegion) basicGetCache().getRegion(Region.SEPARATOR + OrderPartitionedRegionName);
CustId custId = new CustId(2);
Customer newCus = new Customer("foo", "bar");
Order order = new Order("fooOrder");
OrderId orderId = new OrderId(22, custId);
ArrayList args = new ArrayList();
Function txFunction = new MyTransactionFunction();
FunctionService.registerFunction(txFunction);
Execution e = FunctionService.onRegion(pr);
Set filter = new HashSet();
// test transaction non-coLocated operations
filter.clear();
args.clear();
args.add(new Integer(VERIFY_NON_COLOCATION));
LogWriterUtils.getLogWriter().info("VERIFY_NON_COLOCATION");
args.add(custId);
args.add(newCus);
args.add(orderId);
args.add(order);
filter.add(custId);
try {
e.withFilter(filter).setArguments(args).execute(txFunction.getId()).getResult();
fail("Expected exception was not thrown");
} catch (FunctionException fe) {
LogWriterUtils.getLogWriter().info("Caught Expected exception");
if (fe.getCause() instanceof TransactionDataNotColocatedException) {
} else {
throw new TestException("Expected to catch FunctionException with cause TransactionDataNotColocatedException" + " but cause is " + fe.getCause(), fe.getCause());
}
}
// verify that the transaction modifications are applied
args.set(0, new Integer(VERIFY_TX));
LogWriterUtils.getLogWriter().info("VERIFY_TX");
orderpr.put(orderId, order);
assertNotNull(orderpr.get(orderId));
e.withFilter(filter).setArguments(args).execute(txFunction.getId()).getResult();
assertTrue("Unexpected customer value after commit", newCus.equals(pr.get(custId)));
Order commitedOrder = (Order) orderpr.get(orderId);
assertTrue("Unexpected order value after commit. Expected:" + order + " Found:" + commitedOrder, order.equals(commitedOrder));
// verify conflict detection
args.set(0, new Integer(VERIFY_TXSTATE_CONFLICT));
e.withFilter(filter).setArguments(args).execute(txFunction.getId()).getResult();
// verify that the transaction is rolled back
args.set(0, new Integer(VERIFY_ROLLBACK));
LogWriterUtils.getLogWriter().info("VERIFY_ROLLBACK");
e.withFilter(filter).setArguments(args).execute(txFunction.getId()).getResult();
// verify destroy
args.set(0, new Integer(VERIFY_DESTROY));
LogWriterUtils.getLogWriter().info("VERIFY_DESTROY");
e.withFilter(filter).setArguments(args).execute(txFunction.getId()).getResult();
// verify invalidate
args.set(0, new Integer(VERIFY_INVALIDATE));
LogWriterUtils.getLogWriter().info("VERIFY_INVALIDATE");
e.withFilter(filter).setArguments(args).execute(txFunction.getId()).getResult();
return Boolean.TRUE;
}
});
}
use of org.apache.geode.test.dunit.SerializableCallable in project geode by apache.
the class PRTransactionDUnitTest method testRepeatableRead.
@Test
public void testRepeatableRead() throws Exception {
createColocatedPRs(1);
SerializableCallable registerFunction = new SerializableCallable("register Fn") {
public Object call() throws Exception {
Function txFunction = new MyTransactionFunction();
FunctionService.registerFunction(txFunction);
return Boolean.TRUE;
}
};
dataStore1.invoke(registerFunction);
dataStore2.invoke(registerFunction);
dataStore3.invoke(registerFunction);
accessor.invoke(new SerializableCallable("run function") {
public Object call() throws Exception {
PartitionedRegion pr = (PartitionedRegion) basicGetCache().getRegion(Region.SEPARATOR + CustomerPartitionedRegionName);
PartitionedRegion orderpr = (PartitionedRegion) basicGetCache().getRegion(Region.SEPARATOR + OrderPartitionedRegionName);
CustId custId = new CustId(2);
Customer newCus = new Customer("foo", "bar");
Order order = new Order("fooOrder");
OrderId orderId = new OrderId(22, custId);
ArrayList args = new ArrayList();
Function txFunction = new MyTransactionFunction();
FunctionService.registerFunction(txFunction);
Execution e = FunctionService.onRegion(pr);
Set filter = new HashSet();
filter.clear();
args.clear();
args.add(new Integer(VERIFY_REP_READ));
LogWriterUtils.getLogWriter().info("VERIFY_REP_READ");
args.add(custId);
args.add(newCus);
args.add(orderId);
args.add(order);
filter.add(custId);
e.withFilter(filter).setArguments(args).execute(txFunction.getId()).getResult();
return null;
}
});
}
Aggregations