use of org.apache.geode.cache.execute.FunctionAdapter in project geode by apache.
the class RemoteTransactionDUnitTest method testNestedTxFunction.
@Test
public void testNestedTxFunction() {
Host host = Host.getHost(0);
VM accessor = host.getVM(0);
VM datastore1 = host.getVM(1);
VM datastore2 = host.getVM(2);
initAccessorAndDataStore(accessor, datastore1, datastore2, 0);
class NestedTxFunction2 extends FunctionAdapter {
static final String id = "NestedTXFunction2";
@Override
public void execute(FunctionContext context) {
TXManagerImpl mgr = getGemfireCache().getTxManager();
assertNotNull(mgr.getTXState());
try {
mgr.commit();
fail("expected exceptio not thrown");
} catch (UnsupportedOperationInTransactionException e) {
}
context.getResultSender().lastResult(Boolean.TRUE);
}
@Override
public String getId() {
return id;
}
}
class NestedTxFunction extends FunctionAdapter {
static final String id = "NestedTXFunction";
@Override
public void execute(FunctionContext context) {
Region r = null;
if (context instanceof RegionFunctionContext) {
r = PartitionRegionHelper.getLocalDataForContext((RegionFunctionContext) context);
} else {
r = getGemfireCache().getRegion(CUSTOMER);
}
assertNotNull(getGemfireCache().getTxManager().getTXState());
PartitionedRegion pr = (PartitionedRegion) getGemfireCache().getRegion(CUSTOMER);
Set filter = new HashSet();
filter.add(expectedCustId);
LogWriterUtils.getLogWriter().info("SWAP:inside NestedTxFunc calling func2:");
r.put(expectedCustId, expectedCustomer);
FunctionService.onRegion(pr).withFilter(filter).execute(new NestedTxFunction2()).getResult();
assertNotNull(getGemfireCache().getTxManager().getTXState());
context.getResultSender().lastResult(Boolean.TRUE);
}
@Override
public boolean optimizeForWrite() {
return true;
}
@Override
public String getId() {
return id;
}
}
accessor.invoke(new SerializableCallable() {
public Object call() throws Exception {
TXManagerImpl mgr = getGemfireCache().getTxManager();
PartitionedRegion pr = (PartitionedRegion) getGemfireCache().getRegion(CUSTOMER);
mgr.begin();
Set filter = new HashSet();
filter.add(expectedCustId);
FunctionService.onRegion(pr).withFilter(filter).execute(new NestedTxFunction()).getResult();
assertNotNull(getGemfireCache().getTxManager().getTXState());
mgr.commit();
assertEquals(expectedCustomer, pr.get(expectedCustId));
return null;
}
});
}
use of org.apache.geode.cache.execute.FunctionAdapter 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));
}
use of org.apache.geode.cache.execute.FunctionAdapter 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."));
}
}
use of org.apache.geode.cache.execute.FunctionAdapter 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);
}
}
}
use of org.apache.geode.cache.execute.FunctionAdapter 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);
}
}
}
Aggregations