use of org.apache.geode.cache.EntryEvent in project geode by apache.
the class MultiVMRegionTestCase method testRemoteCacheWriter.
/**
* Tests that a remote {@link CacheWriter} is invoked and that <code>CacheWriter</code> arguments
* and {@link CacheWriterException}s are propagated appropriately.
*/
@Test
public void testRemoteCacheWriter() throws Exception {
assertTrue(getRegionAttributes().getScope().isDistributed());
final String name = this.getUniqueName();
final Object key = "KEY";
final Object oldValue = "OLD_VALUE";
final Object newValue = "NEW_VALUE";
final Object arg = "ARG";
final Object exception = "EXCEPTION";
final Object key2 = "KEY2";
final Object value2 = "VALUE2";
SerializableRunnable create = new CacheSerializableRunnable("Create Region") {
@Override
public void run2() throws CacheException {
Region region = createRegion(name);
// Put key2 in the region before any callbacks are
// registered, so it can be destroyed later
region.put(key2, value2);
assertEquals(1, region.size());
if (region.getAttributes().getOffHeap() && !(region instanceof PartitionedRegion)) {
GemFireCacheImpl gfc = (GemFireCacheImpl) getCache();
MemoryAllocatorImpl ma = (MemoryAllocatorImpl) gfc.getOffHeapStore();
LocalRegion reRegion;
reRegion = (LocalRegion) region;
RegionEntry re = reRegion.getRegionEntry(key2);
StoredObject so = (StoredObject) re._getValue();
assertEquals(1, so.getRefCount());
assertEquals(1, ma.getStats().getObjects());
}
}
};
Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
VM vm1 = host.getVM(1);
vm0.invoke(create);
vm1.invoke(create);
//////// Create
vm1.invoke(new CacheSerializableRunnable("Set Writer") {
@Override
public void run2() throws CacheException {
final Region region = getRootRegion().getSubregion(name);
writer = new TestCacheWriter() {
@Override
public void beforeCreate2(EntryEvent event) throws CacheWriterException {
if (exception.equals(event.getCallbackArgument())) {
String s = "Test Exception";
throw new CacheWriterException(s);
}
assertEquals(region, event.getRegion());
assertTrue(event.getOperation().isCreate());
assertTrue(event.getOperation().isDistributed());
assertFalse(event.getOperation().isExpiration());
assertTrue(event.isOriginRemote());
assertEquals(key, event.getKey());
assertEquals(null, event.getOldValue());
assertEquals(oldValue, event.getNewValue());
assertFalse(event.getOperation().isLoad());
assertFalse(event.getOperation().isLocalLoad());
assertFalse(event.getOperation().isNetLoad());
assertFalse(event.getOperation().isNetSearch());
}
};
region.getAttributesMutator().setCacheWriter(writer);
flushIfNecessary(region);
}
});
vm0.invoke(new CacheSerializableRunnable("Create with Exception") {
@Override
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(name);
try {
region.put(key, oldValue, exception);
fail("Should have thrown a CacheWriterException");
} catch (CacheWriterException ex) {
assertNull(region.getEntry(key));
assertEquals(1, region.size());
if (region.getAttributes().getOffHeap() && !(region instanceof PartitionedRegion)) {
GemFireCacheImpl gfc = (GemFireCacheImpl) getCache();
MemoryAllocatorImpl ma = (MemoryAllocatorImpl) gfc.getOffHeapStore();
assertEquals(1, ma.getStats().getObjects());
}
}
}
});
vm1.invoke(new SerializableRunnable("Verify callback") {
@Override
public void run() {
assertTrue(writer.wasInvoked());
}
});
vm0.invoke(new CacheSerializableRunnable("Create with Argument") {
@Override
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(name);
region.put(key, oldValue, arg);
assertEquals(2, region.size());
if (region.getAttributes().getOffHeap() && !(region instanceof PartitionedRegion)) {
GemFireCacheImpl gfc = (GemFireCacheImpl) getCache();
MemoryAllocatorImpl ma = (MemoryAllocatorImpl) gfc.getOffHeapStore();
assertEquals(2, ma.getStats().getObjects());
LocalRegion reRegion;
reRegion = (LocalRegion) region;
StoredObject so = (StoredObject) reRegion.getRegionEntry(key)._getValue();
assertEquals(1, so.getRefCount());
}
}
});
vm1.invoke(new SerializableRunnable("Verify callback") {
@Override
public void run() {
assertTrue(writer.wasInvoked());
}
});
//////// Update
vm1.invoke(new CacheSerializableRunnable("Set Writer") {
@Override
public void run2() throws CacheException {
final Region region = getRootRegion().getSubregion(name);
writer = new TestCacheWriter() {
@Override
public void beforeUpdate2(EntryEvent event) throws CacheWriterException {
Object argument = event.getCallbackArgument();
if (exception.equals(argument)) {
String s = "Test Exception";
throw new CacheWriterException(s);
}
assertEquals(arg, argument);
assertEquals(region, event.getRegion());
assertTrue(event.getOperation().isUpdate());
assertTrue(event.getOperation().isDistributed());
assertFalse(event.getOperation().isExpiration());
assertTrue(event.isOriginRemote());
assertEquals(key, event.getKey());
assertEquals(oldValue, event.getOldValue());
assertEquals(newValue, event.getNewValue());
assertFalse(event.getOperation().isLoad());
assertFalse(event.getOperation().isLocalLoad());
assertFalse(event.getOperation().isNetLoad());
assertFalse(event.getOperation().isNetSearch());
}
};
region.getAttributesMutator().setCacheWriter(writer);
}
});
vm0.invoke(new CacheSerializableRunnable("Update with Exception") {
@Override
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(name);
try {
region.put(key, newValue, exception);
fail("Should have thrown a CacheWriterException");
} catch (CacheWriterException ex) {
Region.Entry entry = region.getEntry(key);
assertEquals(oldValue, entry.getValue());
assertEquals(2, region.size());
if (region.getAttributes().getOffHeap() && !(region instanceof PartitionedRegion)) {
GemFireCacheImpl gfc = (GemFireCacheImpl) getCache();
MemoryAllocatorImpl ma = (MemoryAllocatorImpl) gfc.getOffHeapStore();
assertEquals(2, ma.getStats().getObjects());
LocalRegion reRegion;
reRegion = (LocalRegion) region;
StoredObject so = (StoredObject) reRegion.getRegionEntry(key)._getValue();
assertEquals(1, so.getRefCount());
}
}
}
});
vm1.invoke(new SerializableRunnable("Verify callback") {
@Override
public void run() {
assertTrue(writer.wasInvoked());
}
});
vm0.invoke(new CacheSerializableRunnable("Update with Argument") {
@Override
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(name);
region.put(key, newValue, arg);
assertEquals(2, region.size());
if (region.getAttributes().getOffHeap() && !(region instanceof PartitionedRegion)) {
GemFireCacheImpl gfc = (GemFireCacheImpl) getCache();
MemoryAllocatorImpl ma = (MemoryAllocatorImpl) gfc.getOffHeapStore();
assertEquals(2, ma.getStats().getObjects());
}
}
});
vm1.invoke(new SerializableRunnable("Verify callback") {
@Override
public void run() {
assertTrue(writer.wasInvoked());
}
});
//////// Destroy
vm1.invoke(new CacheSerializableRunnable("Set Writer") {
@Override
public void run2() throws CacheException {
final Region region = getRootRegion().getSubregion(name);
writer = new TestCacheWriter() {
@Override
public void beforeDestroy2(EntryEvent event) throws CacheWriterException {
Object argument = event.getCallbackArgument();
if (exception.equals(argument)) {
String s = "Test Exception";
throw new CacheWriterException(s);
}
assertEquals(arg, argument);
assertEquals(region, event.getRegion());
assertTrue(event.getOperation().isDestroy());
assertTrue(event.getOperation().isDistributed());
assertFalse(event.getOperation().isExpiration());
assertTrue(event.isOriginRemote());
assertEquals(key, event.getKey());
assertEquals(newValue, event.getOldValue());
assertNull(event.getNewValue());
assertFalse(event.getOperation().isLoad());
assertFalse(event.getOperation().isLocalLoad());
assertFalse(event.getOperation().isNetLoad());
assertFalse(event.getOperation().isNetSearch());
}
};
region.getAttributesMutator().setCacheWriter(writer);
}
});
vm0.invoke(new CacheSerializableRunnable("Destroy with Exception") {
@Override
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(name);
try {
region.destroy(key, exception);
fail("Should have thrown a CacheWriterException");
} catch (CacheWriterException ex) {
assertNotNull(region.getEntry(key));
assertEquals(2, region.size());
if (region.getAttributes().getOffHeap() && !(region instanceof PartitionedRegion)) {
GemFireCacheImpl gfc = (GemFireCacheImpl) getCache();
MemoryAllocatorImpl ma = (MemoryAllocatorImpl) gfc.getOffHeapStore();
assertEquals(2, ma.getStats().getObjects());
}
}
}
});
vm1.invoke(new SerializableRunnable("Verify callback") {
@Override
public void run() {
assertTrue(writer.wasInvoked());
}
});
vm0.invoke(new CacheSerializableRunnable("Destroy with Argument") {
@Override
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(name);
region.destroy(key, arg);
assertEquals(1, region.size());
if (region.getAttributes().getOffHeap() && !(region instanceof PartitionedRegion)) {
GemFireCacheImpl gfc = (GemFireCacheImpl) getCache();
MemoryAllocatorImpl ma = (MemoryAllocatorImpl) gfc.getOffHeapStore();
assertEquals(1, ma.getStats().getObjects());
}
}
});
vm1.invoke(new SerializableRunnable("Verify callback") {
@Override
public void run() {
assertTrue(writer.wasInvoked());
}
});
//////// Region Destroy
vm1.invoke(new CacheSerializableRunnable("Set Writer") {
@Override
public void run2() throws CacheException {
final Region region = getRootRegion().getSubregion(name);
writer = new TestCacheWriter() {
@Override
public void beforeRegionDestroy2(RegionEvent event) throws CacheWriterException {
Object argument = event.getCallbackArgument();
if (exception.equals(argument)) {
String s = "Test Exception";
throw new CacheWriterException(s);
}
assertEquals(arg, argument);
assertEquals(region, event.getRegion());
assertTrue(event.getOperation().isRegionDestroy());
assertTrue(event.getOperation().isDistributed());
assertFalse(event.getOperation().isExpiration());
assertTrue(event.isOriginRemote());
}
};
region.getAttributesMutator().setCacheWriter(writer);
}
});
vm0.invoke(new CacheSerializableRunnable("Destroy with Exception") {
@Override
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(name);
try {
region.destroyRegion(exception);
fail("Should have thrown a CacheWriterException");
} catch (CacheWriterException ex) {
if (region.isDestroyed()) {
fail("should not have an exception if region is destroyed", ex);
}
assertEquals(1, region.size());
if (region.getAttributes().getOffHeap() && !(region instanceof PartitionedRegion)) {
GemFireCacheImpl gfc = (GemFireCacheImpl) getCache();
MemoryAllocatorImpl ma = (MemoryAllocatorImpl) gfc.getOffHeapStore();
assertEquals(1, ma.getStats().getObjects());
}
}
}
});
vm1.invoke(new SerializableRunnable("Verify callback") {
@Override
public void run() {
assertTrue(writer.wasInvoked());
}
});
vm0.invoke(new CacheSerializableRunnable("Destroy with Argument") {
@Override
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(name);
assertEquals(1, region.size());
if (region.getAttributes().getOffHeap() && !(region instanceof PartitionedRegion)) {
GemFireCacheImpl gfc = (GemFireCacheImpl) getCache();
MemoryAllocatorImpl ma = (MemoryAllocatorImpl) gfc.getOffHeapStore();
assertEquals(1, ma.getStats().getObjects());
}
region.destroyRegion(arg);
if (region.getAttributes().getOffHeap() && !(region instanceof PartitionedRegion)) {
GemFireCacheImpl gfc = (GemFireCacheImpl) getCache();
final MemoryAllocatorImpl ma = (MemoryAllocatorImpl) gfc.getOffHeapStore();
WaitCriterion waitForStatChange = new WaitCriterion() {
@Override
public boolean done() {
return ma.getStats().getObjects() == 0;
}
@Override
public String description() {
return "never saw off-heap object count go to zero. Last value was " + ma.getStats().getObjects();
}
};
Wait.waitForCriterion(waitForStatChange, 3000, 10, true);
}
}
});
vm1.invoke(new SerializableRunnable("Verify callback") {
@Override
public void run() {
assertTrue(writer.wasInvoked());
}
});
}
use of org.apache.geode.cache.EntryEvent in project geode by apache.
the class MultiVMRegionTestCase method testTXUpdateLoadNoConflict.
/**
* Tests that the push of a loaded value does not cause a conflict on the side receiving the
* update
*/
@Ignore("TODO: this test always hits early out")
@Test
public void testTXUpdateLoadNoConflict() throws Exception {
/*
* this no longer holds true - we have load conflicts now
*
*/
if (true) {
return;
}
assumeTrue(supportsTransactions());
assumeFalse(getRegionAttributes().getScope().isGlobal());
assumeFalse(getRegionAttributes().getDataPolicy().withPersistence());
assertTrue(getRegionAttributes().getScope().isDistributed());
CacheTransactionManager txMgr = this.getCache().getCacheTransactionManager();
final String rgnName = getUniqueName();
SerializableRunnable create = new SerializableRunnable("testTXUpdateLoadNoConflict: Create Region & Load value") {
@Override
public void run() {
CacheTransactionManager txMgr2 = getCache().getCacheTransactionManager();
MyTransactionListener tl = new MyTransactionListener();
txMgr2.addListener(tl);
try {
Region rgn = createRegion(rgnName);
AttributesMutator mutator = rgn.getAttributesMutator();
mutator.setCacheLoader(new CacheLoader() {
int count = 0;
@Override
public Object load(LoaderHelper helper) throws CacheLoaderException {
count++;
return "LV " + count;
}
@Override
public void close() {
}
});
Object value = rgn.get("key");
assertEquals("LV 1", value);
getSystem().getLogWriter().info("testTXUpdateLoadNoConflict: loaded Key");
flushIfNecessary(rgn);
} catch (CacheException e) {
fail("While creating region", e);
}
}
};
VM vm0 = Host.getHost(0).getVM(0);
try {
MyTransactionListener tl = new MyTransactionListener();
txMgr.addListener(tl);
AttributesFactory rgnAtts = new AttributesFactory(getRegionAttributes());
rgnAtts.setDataPolicy(DataPolicy.REPLICATE);
Region rgn = createRegion(rgnName, rgnAtts.create());
txMgr.begin();
TransactionId myTXId = txMgr.getTransactionId();
rgn.create("key", "txValue");
vm0.invoke(create);
{
TXStateProxy tx = ((TXManagerImpl) txMgr).internalSuspend();
assertTrue(rgn.containsKey("key"));
assertEquals("LV 1", rgn.getEntry("key").getValue());
((TXManagerImpl) txMgr).internalResume(tx);
}
// make sure transactional view is still correct
assertEquals("txValue", rgn.getEntry("key").getValue());
txMgr.commit();
getSystem().getLogWriter().info("testTXUpdateLoadNoConflict: did commit");
assertEquals("txValue", rgn.getEntry("key").getValue());
{
Collection events = tl.lastEvent.getCreateEvents();
assertEquals(1, events.size());
EntryEvent ev = (EntryEvent) events.iterator().next();
assertEquals(myTXId, ev.getTransactionId());
assertTrue(ev.getRegion() == rgn);
assertEquals("key", ev.getKey());
assertEquals("txValue", ev.getNewValue());
assertEquals(null, ev.getOldValue());
assertTrue(!ev.getOperation().isLocalLoad());
assertTrue(!ev.getOperation().isNetLoad());
assertTrue(!ev.getOperation().isLoad());
assertTrue(!ev.getOperation().isNetSearch());
assertTrue(!ev.getOperation().isExpiration());
assertEquals(null, ev.getCallbackArgument());
assertEquals(true, ev.isCallbackArgumentAvailable());
assertTrue(!ev.isOriginRemote());
assertTrue(ev.getOperation().isDistributed());
}
// Now setup recreate the region in the controller with NONE
// so test can do local destroys.
rgn.localDestroyRegion();
rgnAtts.setDataPolicy(DataPolicy.NORMAL);
rgn = createRegion(rgnName, rgnAtts.create());
// now see if net loader is working
Object v2 = rgn.get("key2");
assertEquals("LV 2", v2);
// now confirm that netload does not cause a conflict
txMgr.begin();
myTXId = txMgr.getTransactionId();
rgn.create("key3", "txValue3");
{
TXStateProxy tx = ((TXManagerImpl) txMgr).internalSuspend();
// do a get outside of the transaction to force a net load
Object v3 = rgn.get("key3");
assertEquals("LV 3", v3);
((TXManagerImpl) txMgr).internalResume(tx);
}
// make sure transactional view is still correct
assertEquals("txValue3", rgn.getEntry("key3").getValue());
txMgr.commit();
getSystem().getLogWriter().info("testTXUpdateLoadNoConflict: did commit");
assertEquals("txValue3", rgn.getEntry("key3").getValue());
{
Collection events = tl.lastEvent.getCreateEvents();
assertEquals(1, events.size());
EntryEvent ev = (EntryEvent) events.iterator().next();
assertEquals(myTXId, ev.getTransactionId());
assertTrue(ev.getRegion() == rgn);
assertEquals("key3", ev.getKey());
assertEquals("txValue3", ev.getNewValue());
assertEquals(null, ev.getOldValue());
assertTrue(!ev.getOperation().isLocalLoad());
assertTrue(!ev.getOperation().isNetLoad());
assertTrue(!ev.getOperation().isLoad());
assertTrue(!ev.getOperation().isNetSearch());
assertTrue(!ev.getOperation().isExpiration());
assertEquals(null, ev.getCallbackArgument());
assertEquals(true, ev.isCallbackArgumentAvailable());
assertTrue(!ev.isOriginRemote());
assertTrue(ev.getOperation().isDistributed());
}
// now see if tx net loader is working
// now confirm that netload does not cause a conflict
txMgr.begin();
myTXId = txMgr.getTransactionId();
Object v4 = rgn.get("key4");
assertEquals("LV 4", v4);
assertEquals("LV 4", rgn.get("key4"));
assertEquals("LV 4", rgn.getEntry("key4").getValue());
txMgr.rollback();
// confirm that netLoad is transactional
assertEquals("LV 5", rgn.get("key4"));
assertEquals("LV 5", rgn.getEntry("key4").getValue());
// make sure non-tx netsearch works
assertEquals("txValue", rgn.get("key"));
assertEquals("txValue", rgn.getEntry("key").getValue());
// make sure net-search result does not conflict with commit
rgn.localInvalidate("key");
txMgr.begin();
myTXId = txMgr.getTransactionId();
rgn.put("key", "new txValue");
{
TXStateProxy tx = ((TXManagerImpl) txMgr).internalSuspend();
// do a get outside of the transaction to force a netsearch
// does a netsearch
assertEquals("txValue", rgn.get("key"));
assertEquals("txValue", rgn.getEntry("key").getValue());
((TXManagerImpl) txMgr).internalResume(tx);
}
// make sure transactional view is still correct
assertEquals("new txValue", rgn.getEntry("key").getValue());
txMgr.commit();
// give other side change to process commit
flushIfNecessary(rgn);
getSystem().getLogWriter().info("testTXUpdateLoadNoConflict: did commit");
assertEquals("new txValue", rgn.getEntry("key").getValue());
{
Collection events = tl.lastEvent.getPutEvents();
assertEquals(1, events.size());
EntryEvent ev = (EntryEvent) events.iterator().next();
assertEquals(myTXId, ev.getTransactionId());
assertTrue(ev.getRegion() == rgn);
assertEquals("key", ev.getKey());
assertEquals("new txValue", ev.getNewValue());
assertEquals(null, ev.getOldValue());
assertTrue(!ev.getOperation().isLocalLoad());
assertTrue(!ev.getOperation().isNetLoad());
assertTrue(!ev.getOperation().isLoad());
assertTrue(!ev.getOperation().isNetSearch());
assertTrue(!ev.getOperation().isExpiration());
assertEquals(null, ev.getCallbackArgument());
assertEquals(true, ev.isCallbackArgumentAvailable());
assertTrue(!ev.isOriginRemote());
assertTrue(ev.getOperation().isDistributed());
}
// make sure tx local invalidate allows netsearch
Object localCmtValue = rgn.getEntry("key").getValue();
txMgr.begin();
assertSame(localCmtValue, rgn.getEntry("key").getValue());
rgn.localInvalidate("key");
assertNull(rgn.getEntry("key").getValue());
// now make sure a get will do a netsearch and find the value
// in the other vm instead of the one in local cmt state
Object txValue = rgn.get("key");
assertNotSame(localCmtValue, txValue);
assertSame(txValue, rgn.get("key"));
assertNotSame(localCmtValue, rgn.getEntry("key").getValue());
// make sure we did a search and not a load
assertEquals(localCmtValue, rgn.getEntry("key").getValue());
// now make sure that if we do a tx distributed invalidate
// that we will do a load and not a search
rgn.invalidate("key");
assertNull(rgn.getEntry("key").getValue());
txValue = rgn.get("key");
assertEquals("LV 6", txValue);
assertSame(txValue, rgn.get("key"));
assertEquals("LV 6", rgn.getEntry("key").getValue());
// now make sure after rollback that local cmt state has not changed
txMgr.rollback();
assertSame(localCmtValue, rgn.getEntry("key").getValue());
} catch (Exception e) {
CacheFactory.getInstance(getSystem()).close();
getSystem().getLogWriter().fine("testTXUpdateLoadNoConflict: Caused exception in createRegion");
throw e;
}
}
use of org.apache.geode.cache.EntryEvent in project geode by apache.
the class MultiVMRegionTestCase method assertCacheCallbackEvents.
public static void assertCacheCallbackEvents(String regionName, TransactionId txId, Object key, Object oldValue, Object newValue) {
Cache johnnyCash = CacheFactory.getAnyInstance();
Region re = johnnyCash.getRegion("root").getSubregion(regionName);
MyTransactionListener tl = (MyTransactionListener) johnnyCash.getCacheTransactionManager().getListeners()[0];
tl.expectedTxId = txId;
assertNotNull("Cannot assert TX Callout Events with a null Region: " + regionName, re);
final CountingDistCacheListener cdcl = (CountingDistCacheListener) re.getAttributes().getCacheListeners()[0];
// May need to wait a bit for the event to be received
WaitCriterion ev = new WaitCriterion() {
@Override
public boolean done() {
return cdcl.getEntryEvent() != null;
}
@Override
public String description() {
return "waiting for entry event";
}
};
Wait.waitForCriterion(ev, 60 * 1000, 200, true);
EntryEvent listenEvent = cdcl.getEntryEvent();
assertNotNull("Cannot assert TX CacheListener Events with a null Entry Event", listenEvent);
assertEquals(re, listenEvent.getRegion());
assertEquals(txId, listenEvent.getTransactionId());
assertEquals(key, listenEvent.getKey());
assertEquals(oldValue, listenEvent.getOldValue());
assertEquals(newValue, listenEvent.getNewValue());
assertEquals(null, listenEvent.getCallbackArgument());
assertEquals(true, listenEvent.isCallbackArgumentAvailable());
assertTrue(!listenEvent.getOperation().isLoad());
assertTrue(!listenEvent.getOperation().isNetLoad());
assertTrue(!listenEvent.getOperation().isNetSearch());
assertTrue(!listenEvent.getOperation().isLocalLoad());
assertTrue(listenEvent.getOperation().isDistributed());
assertTrue(!listenEvent.getOperation().isExpiration());
assertTrue(listenEvent.isOriginRemote());
cdcl.setEntryEvent(null);
}
use of org.apache.geode.cache.EntryEvent in project geode by apache.
the class MultiVMRegionTestCase method testCacheLoaderModifyingArgument.
/**
* Tests that when a <code>CacheLoader</code> modifies the callback argument in place, the change
* is visible to the <code>CacheWriter</code> even if it is in another VM.
*/
@Test
public void testCacheLoaderModifyingArgument() throws Exception {
assertTrue(getRegionAttributes().getScope().isDistributed());
final String name = this.getUniqueName();
final Object key = "KEY";
final Object value = "VALUE";
final Object one = "ONE";
final Object two = "TWO";
SerializableRunnable create = new CacheSerializableRunnable("Create Region") {
@Override
public void run2() throws CacheException {
createRegion(name);
}
};
Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
VM vm1 = host.getVM(1);
vm0.invoke(create);
vm1.invoke(create);
CacheSerializableRunnable setLoader = new CacheSerializableRunnable("Set CacheLoader") {
@Override
public void run2() throws CacheException {
final Region region = getRootRegion().getSubregion(name);
loader = new TestCacheLoader() {
@Override
public Object load2(LoaderHelper helper) throws CacheLoaderException {
Object[] array = (Object[]) helper.getArgument();
assertEquals(one, array[0]);
array[0] = two;
return value;
}
};
region.getAttributesMutator().setCacheLoader(loader);
flushIfNecessary(region);
}
};
vm0.invoke(setLoader);
// if this is a partitioned region, we need the loader in both vms
vm1.invoke(new CacheSerializableRunnable("Conditionally create second loader") {
@Override
public void run2() throws CacheException {
final Region region = getRootRegion().getSubregion(name);
if (region.getAttributes().getPartitionAttributes() != null) {
loader = new TestCacheLoader() {
@Override
public Object load2(LoaderHelper helper) throws CacheLoaderException {
Object[] array = (Object[]) helper.getArgument();
assertEquals(one, array[0]);
array[0] = two;
return value;
}
};
region.getAttributesMutator().setCacheLoader(loader);
}
}
});
vm1.invoke(new CacheSerializableRunnable("Set CacheWriter") {
@Override
public void run2() throws CacheException {
final Region region = getRootRegion().getSubregion(name);
writer = new TestCacheWriter() {
@Override
public void beforeCreate2(EntryEvent event) throws CacheWriterException {
Object[] array = (Object[]) event.getCallbackArgument();
assertEquals(two, array[0]);
}
};
region.getAttributesMutator().setCacheWriter(writer);
flushIfNecessary(region);
}
});
vm0.invoke(new CacheSerializableRunnable("Create entry") {
@Override
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(name);
Object[] array = { one };
Object result = region.get(key, array);
assertTrue(loader.wasInvoked());
assertEquals(value, result);
}
});
vm1.invoke(new CacheSerializableRunnable("Validate callback") {
@Override
public void run2() throws CacheException {
assertTrue(writer.wasInvoked());
}
});
}
use of org.apache.geode.cache.EntryEvent in project geode by apache.
the class LocalRegionDUnitTest method testLocalUpdateModifiedCallbackArgument.
/**
* Tests that a local writer receives a modified version of the callback argument on an update.
*/
@Test
public void testLocalUpdateModifiedCallbackArgument() throws CacheException {
final String name = this.getUniqueName();
final Object key = "KEY";
final Object value = "VALUE";
final Object one = "ONE";
final Object two = "TWO";
TestCacheLoader loader = new TestCacheLoader() {
public Object load2(LoaderHelper helper) throws CacheLoaderException {
Object[] array = (Object[]) helper.getArgument();
assertEquals(one, array[0]);
array[0] = two;
return value;
}
};
TestCacheWriter writer = new TestCacheWriter() {
public void beforeCreate2(EntryEvent event) throws CacheWriterException {
}
public void beforeUpdate2(EntryEvent event) throws CacheWriterException {
Object[] array = (Object[]) event.getCallbackArgument();
assertEquals(two, array[0]);
}
};
AttributesFactory factory = new AttributesFactory(getRegionAttributes());
factory.setCacheLoader(loader);
factory.setCacheWriter(writer);
Region region = createRegion(name, factory.create());
region.create(key, null);
assertFalse(loader.wasInvoked());
assertTrue(writer.wasInvoked());
Object[] array = new Object[] { one };
assertEquals(value, region.get(key, array));
assertTrue(loader.wasInvoked());
assertTrue(writer.wasInvoked());
}
Aggregations