in geode-core/src/integrationTest/java/org/apache/geode/TXJUnitTest.java [3317:3873]
public void testCacheCallbacks() throws CacheException {
final String key1 = "Key1";
final String value1 = "value1";
final String value2 = "value2";
final String callBackArg = "call back arg";
// install listeners
AttributesMutator<String, String> mutator = region.getAttributesMutator();
TXCallBackValidator cbv = new TXCallBackValidator();
// Cache Listener
ValidatableCacheListener vCl = new ValidatableCacheListener() {
TXCallBackValidator v;
int callCount;
int prevCallCount;
EntryEvent lastEvent;
@Override
public void validate() {
v.validate(lastEvent, callCount);
}
void validate(EntryEvent event) {
v.validate(event, ++callCount);
}
@Override
public void setValidator(TXCallBackValidator v) {
this.v = v;
}
@Override
public void close() {}
@Override
public void afterCreate(EntryEvent event) {
lastEvent = event;
if (v.isSuspendValidation()) {
return;
}
validate(event);
v.setPassedValidation(false);
assertTrue("IsCreate Assertion!", v.isCreate());
assertTrue(event.getRegion().containsKey(v.getKey()));
assertTrue(event.getRegion().containsValueForKey(v.getKey()));
assertNotNull(event.getRegion().getEntry(event.getKey()).getValue());
v.setPassedValidation(true);
}
@Override
public void afterUpdate(EntryEvent event) {
lastEvent = event;
if (v.isSuspendValidation()) {
return;
}
validate(event);
v.setPassedValidation(false);
assertTrue("IsUpdate Assertion!", v.isUpdate());
assertTrue(event.getRegion().containsKey(v.getKey()));
assertTrue(event.getRegion().containsValueForKey(v.getKey()));
assertNotNull(event.getRegion().getEntry(event.getKey()).getValue());
v.setPassedValidation(true);
}
@Override
public void afterInvalidate(EntryEvent event) {
lastEvent = event;
if (v.isSuspendValidation()) {
return;
}
validate(event);
v.setPassedValidation(false);
assertTrue("IsInvalidate Assertion!", v.isInvalidate());
assertTrue(event.getRegion().containsKey(v.getKey()));
assertTrue(!event.getRegion().containsValueForKey(v.getKey()));
assertNull(event.getRegion().getEntry(event.getKey()).getValue());
v.setPassedValidation(true);
}
@Override
public void afterDestroy(EntryEvent event) {
lastEvent = event;
if (v.isSuspendValidation()) {
return;
}
validate(event);
v.setPassedValidation(false);
assertTrue("IsDestroy Assertion!", v.isDestroy());
assertTrue(!event.getRegion().containsKey(v.getKey()));
assertTrue(!event.getRegion().containsValueForKey(v.getKey()));
assertNull(event.getRegion().getEntry(event.getKey()));
v.setPassedValidation(true);
}
@Override
public void afterRegionInvalidate(RegionEvent event) {
fail("Unexpected invocation of afterRegionInvalidate");
}
@Override
public void afterRegionDestroy(RegionEvent event) {
if (!event.getOperation().isClose()) {
fail("Unexpected invocation of afterRegionDestroy");
}
}
@Override
public void afterRegionClear(RegionEvent event) {}
@Override
public void afterRegionCreate(RegionEvent event) {}
@Override
public void afterRegionLive(RegionEvent event) {}
@Override
public void reset() {
lastEvent = null;
prevCallCount = callCount;
}
@Override
public void validateNoEvents() {
assertNull("Did not expect listener callback", lastEvent);
assertEquals(prevCallCount, callCount);
}
@Override
public void setExpectedCount(int count) {
callCount = count;
}
@Override
public int getCallCount() {
return callCount;
}
};
vCl.setValidator(cbv);
mutator.addCacheListener(vCl);
// CacheWriter
ValidatableCacheWriter vCw = new ValidatableCacheWriter() {
TXCallBackValidator v;
int callCount;
int prevCallCount;
EntryEvent lastEvent;
@Override
public int getCallCount() {
return callCount;
}
@Override
public void localDestroyMakeup(int count) {
callCount += count;
}
@Override
public void validate() {
v.validate(lastEvent, callCount);
}
void validate(EntryEvent event) {
v.validate(event, ++callCount);
}
@Override
public void setValidator(TXCallBackValidator v) {
this.v = v;
}
@Override
public void close() {}
@Override
public void beforeCreate(EntryEvent event) {
lastEvent = event;
if (v.isSuspendValidation()) {
return;
}
validate(event);
v.setPassedValidation(false);
assertTrue("IsCreate Assertion!", v.isCreate());
assertTrue(!event.getRegion().containsKey(v.getKey()));
assertTrue(!event.getRegion().containsValueForKey(v.getKey()));
assertNull(event.getRegion().getEntry(event.getKey()));
v.setPassedValidation(true);
}
@Override
public void beforeUpdate(EntryEvent event) {
lastEvent = event;
if (v.isSuspendValidation()) {
return;
}
validate(event);
v.setPassedValidation(false);
assertTrue("IsUpdate Assertion!", v.isUpdate());
assertTrue(event.getRegion().containsKey(v.getKey()));
// Can not assert the following line, as the value being update may be invalid
// assertTrue(event.getRegion().containsValueForKey(this.v.getKey()));
v.setPassedValidation(true);
}
@Override
public void beforeDestroy(EntryEvent event) {
lastEvent = event;
if (v.isSuspendValidation()) {
return;
}
validate(event);
v.setPassedValidation(false);
assertTrue("IsDestroy Assertion!", v.isDestroy());
assertTrue(event.getRegion().containsKey(v.getKey()));
v.setPassedValidation(true);
}
@Override
public void beforeRegionDestroy(RegionEvent event) {
fail("Unexpected invocation of beforeRegionDestroy");
}
@Override
public void beforeRegionClear(RegionEvent event) {
fail("Unexpected invocation of beforeRegionClear");
}
@Override
public void reset() {
lastEvent = null;
prevCallCount = callCount;
}
@Override
public void validateNoEvents() {
assertNull("Did not expect a writer event", lastEvent);
assertEquals(prevCallCount, callCount);
}
};
vCw.setValidator(cbv);
mutator.setCacheWriter(vCw);
// Cache Loader
mutator.setCacheLoader(new CacheLoader() {
int count = 0;
@Override
public Object load(LoaderHelper helper) throws CacheLoaderException {
return count++;
}
@Override
public void close() {}
});
// Use this to track the number of callout method invocations
int appCallCount = 1;
// Create => beforeCreate/afterCreate tests
cbv.setKey(key1);
cbv.setCallBackArg(callBackArg);
cbv.setNewValue(value1, false);
cbv.setOldValue(null, true);
cbv.setIsDistributed(true);
cbv.setIsLoad(false);
cbv.setIsCreate(true);
cbv.setIsUpdate(false);
// Test non-transactional create expecting beforeCreate/afterCreate call
cbv.setTXId(txMgr.getTransactionId());
cbv.setExpectedCount(appCallCount++);
region.create(key1, value1, callBackArg);
assertTrue("Non-TX Create Validation Assertion", cbv.passedValidation());
cbv.suspendValidation(true);
region.localDestroy(key1);
cbv.suspendValidation(false);
// Test transactional create expecting afterCreate call
txMgr.begin();
cbv.setTXId(txMgr.getTransactionId());
cbv.setExpectedCount(appCallCount++);
region.create(key1, value1, callBackArg);
txMgr.commit();
assertTrue("TX Create Validation Assertion", cbv.passedValidation());
cbv.suspendValidation(true);
region.localDestroy(key1);
// Put => afterCreate tests
cbv.suspendValidation(false);
cbv.setNewValue(value2, false);
cbv.setOldValue(null, true);
cbv.setIsDistributed(true);
cbv.setIsLoad(false);
cbv.setIsCreate(true);
cbv.setIsUpdate(false);
// Test non-transactional put expecting afterCreate call due to no
// previous Entry
cbv.setTXId(txMgr.getTransactionId());
cbv.setExpectedCount(appCallCount++);
region.put(key1, value2, callBackArg);
assertTrue("Non-TX Put->Create Validation Assertion", cbv.passedValidation());
cbv.suspendValidation(true);
region.localDestroy(key1);
cbv.suspendValidation(false);
// Test transactional put expecting afterCreate call due to no
// previous Entry
txMgr.begin();
cbv.setTXId(txMgr.getTransactionId());
cbv.setExpectedCount(appCallCount++);
region.put(key1, value2, callBackArg);
txMgr.commit();
assertTrue("TX Put->Create Validation Assertion", cbv.passedValidation());
// Put => afterUpdate tests
cbv.setNewValue(value1, false);
cbv.setOldValue(value2, false);
cbv.setIsDistributed(true);
cbv.setIsLoad(false);
cbv.setIsCreate(false);
cbv.setIsUpdate(true);
// Test non-transactional put expecting afterUpdate call due to
// previous Entry
cbv.setTXId(txMgr.getTransactionId());
cbv.setExpectedCount(appCallCount++);
region.put(key1, value1, callBackArg);
assertTrue("Non-TX Put->Update Validation Assertion", cbv.passedValidation());
cbv.suspendValidation(true);
region.localDestroy(key1);
region.put(key1, value2);
cbv.suspendValidation(false);
// Test transactional put expecting afterUpdate call due to
// previous Entry
txMgr.begin();
cbv.setTXId(txMgr.getTransactionId());
cbv.setExpectedCount(appCallCount++);
region.put(key1, value1, callBackArg);
txMgr.commit();
assertTrue("TX Put->Update Validation Assertion", cbv.passedValidation());
// LocalDestroy => afterDestroy, non-distributed tests
cbv.setNewValue(null, true);
cbv.setOldValue(value1, false);
cbv.setIsDistributed(false);
cbv.setIsLoad(false);
cbv.setIsDestroy(true);
cbv.setIsUpdate(false);
// Test non-transactional localDestroy, expecting afterDestroy,
// non-distributed
cbv.setTXId(txMgr.getTransactionId());
cbv.setExpectedCount(appCallCount++);
region.localDestroy(key1, callBackArg);
if (!isPR()) {
vCw.localDestroyMakeup(1); // Account for cacheWriter not begin called
}
assertTrue("Non-TX LocalDestroy Validation Assertion", cbv.passedValidation());
cbv.suspendValidation(true);
region.create(key1, value1);
cbv.suspendValidation(false);
// Test transactional localDestroy expecting afterDestroy,
// non-distributed
txMgr.begin();
cbv.setTXId(txMgr.getTransactionId());
cbv.setExpectedCount(appCallCount++);
region.localDestroy(key1, callBackArg);
if (!isPR()) {
vCw.localDestroyMakeup(1); // Account for cacheWriter not begin called
}
txMgr.commit();
assertTrue("TX LocalDestroy Validation Assertion", cbv.passedValidation());
// Destroy => afterDestroy, distributed tests
cbv.setNewValue(null, true);
cbv.setOldValue(value1, false);
cbv.setIsDistributed(true);
cbv.setIsLoad(false);
cbv.setIsDestroy(true);
cbv.suspendValidation(true);
region.create(key1, value1);
cbv.suspendValidation(false);
// Test non-transactional Destroy, expecting afterDestroy,
// distributed
cbv.setTXId(txMgr.getTransactionId());
cbv.setExpectedCount(appCallCount++);
region.destroy(key1, callBackArg);
assertTrue("Non-TX Destroy Validation Assertion", cbv.passedValidation());
cbv.suspendValidation(true);
region.create(key1, value1);
cbv.suspendValidation(false);
// Test transactional Destroy, expecting afterDestroy,
// distributed
txMgr.begin();
cbv.setTXId(txMgr.getTransactionId());
cbv.setExpectedCount(appCallCount++);
region.destroy(key1, callBackArg);
txMgr.commit();
assertTrue("TX Destroy Validation Assertion", cbv.passedValidation());
// localInvalidate => afterInvalidate, non-distributed tests
cbv.setNewValue(null, true);
cbv.setOldValue(value1, false);
cbv.setIsDistributed(false);
cbv.setIsLoad(false);
cbv.setIsInvalidate(true);
cbv.setIsDestroy(false);
cbv.suspendValidation(true);
region.create(key1, value1);
cbv.suspendValidation(false);
// Test non-transactional localInvalidate, expecting afterInvalidate
// non-distributed
cbv.setTXId(txMgr.getTransactionId());
cbv.setExpectedCount(appCallCount++);
region.localInvalidate(key1, callBackArg);
assertTrue("Non-TX LocalInvalidate Validation Assertion", cbv.passedValidation());
vCw.localDestroyMakeup(1); // Account for cacheWriter not begin called
cbv.suspendValidation(true);
region.put(key1, value1);
cbv.suspendValidation(false);
// Test transactional localInvalidate, expecting afterInvalidate
// non-distributed
txMgr.begin();
cbv.setTXId(txMgr.getTransactionId());
cbv.setExpectedCount(appCallCount++);
region.localInvalidate(key1, callBackArg);
txMgr.commit();
assertTrue("TX LocalInvalidate Validation Assertion", cbv.passedValidation());
vCw.localDestroyMakeup(1); // Account for cacheWriter not begin called
cbv.suspendValidation(true);
region.localDestroy(key1);
// Invalidate => afterInvalidate, distributed tests
cbv.setNewValue(null, true);
cbv.setOldValue(value1, false);
cbv.setIsDistributed(true);
cbv.setIsLoad(false);
cbv.suspendValidation(true);
region.create(key1, value1);
cbv.suspendValidation(false);
// Test non-transactional Invalidate, expecting afterInvalidate
// distributed
cbv.setTXId(txMgr.getTransactionId());
cbv.setExpectedCount(appCallCount++);
region.invalidate(key1, callBackArg);
vCw.localDestroyMakeup(1); // Account for cacheWriter not begin called
assertTrue("Non-TX Invalidate Validation Assertion", cbv.passedValidation());
cbv.suspendValidation(true);
region.put(key1, value1);
cbv.suspendValidation(false);
// Test transactional Invalidate, expecting afterInvalidate
// distributed
txMgr.begin();
cbv.setTXId(txMgr.getTransactionId());
cbv.setExpectedCount(appCallCount++);
region.invalidate(key1, callBackArg);
txMgr.commit();
vCw.localDestroyMakeup(1); // Account for cacheWriter not begin called
assertTrue("TX Invalidate Validation Assertion", cbv.passedValidation());
cbv.suspendValidation(true);
region.localDestroy(key1);
cbv.suspendValidation(false);
// Create load Event tests
int loaderValCheck = 0;
cbv.setNewValue(loaderValCheck++, false);
cbv.setCallBackArg(null);
cbv.setOldValue(null, false);
cbv.setIsDistributed(true);
cbv.setIsCreate(true);
cbv.setIsUpdate(false);
cbv.setIsLoad(true);
// Test non-transactional load, expecting afterCreate distributed
cbv.setTXId(txMgr.getTransactionId());
cbv.setExpectedCount(appCallCount++);
region.get(key1);
assertTrue("Non-TX Invalidate Validation Assertion", cbv.passedValidation());
vCl.validate();
vCw.validate();
cbv.suspendValidation(true);
region.localDestroy(key1);
cbv.suspendValidation(false);
// Test transactional load, expecting afterCreate distributed
vCl.reset();
txMgr.begin();
cbv.setTXId(txMgr.getTransactionId());
cbv.setNewValue(loaderValCheck++, false);
cbv.setExpectedCount(appCallCount++);
region.get(key1);
txMgr.rollback();
assertTrue("TX Invalidate Validation Assertion", cbv.passedValidation());
vCw.validate();
vCl.validateNoEvents();
vCl.setExpectedCount(vCl.getCallCount() + 1);
txMgr.begin();
cbv.setTXId(txMgr.getTransactionId());
cbv.setNewValue(loaderValCheck++, false);
cbv.setExpectedCount(appCallCount++);
region.get(key1);
vCw.validate();
vCw.reset();
txMgr.commit();
vCw.validateNoEvents();
assertTrue("TX Invalidate Validation Assertion", cbv.passedValidation());
vCl.validate();
cbv.suspendValidation(true);
region.localDestroy(key1);
cbv.suspendValidation(false);
// Update load Event tests
cbv.suspendValidation(true);
region.create(key1, null);
cbv.suspendValidation(false);
assertTrue(region.containsKey(key1));
assertTrue(!region.containsValueForKey(key1));
cbv.setNewValue(loaderValCheck++, false);
cbv.setOldValue(null, false);
cbv.setIsDistributed(true);
cbv.setCallBackArg(null);
cbv.setIsCreate(false);
cbv.setIsUpdate(true);
cbv.setIsLoad(true);
// Test non-transactional load, expecting afterUpdate distributed
cbv.setTXId(txMgr.getTransactionId());
cbv.setExpectedCount(appCallCount++);
region.get(key1);
assertTrue("Non-TX Invalidate Validation Assertion", cbv.passedValidation());
vCw.validate();
vCl.validate();
cbv.suspendValidation(true);
region.invalidate(key1);
cbv.suspendValidation(false);
assertTrue(region.containsKey(key1));
assertTrue(!region.containsValueForKey(key1));
// Test transactional load, expecting afterUpdate distributed
txMgr.begin();
cbv.setTXId(txMgr.getTransactionId());
cbv.setExpectedCount(appCallCount++);
cbv.setNewValue(loaderValCheck++, false);
region.get(key1);
vCw.validate();
vCw.reset();
txMgr.commit();
vCw.validateNoEvents();
vCl.validate();
cbv.suspendValidation(true);
region.invalidate(key1);
cbv.suspendValidation(false);
vCl.reset();
txMgr.begin();
cbv.setTXId(txMgr.getTransactionId());
cbv.setExpectedCount(appCallCount++);
cbv.setNewValue(loaderValCheck++, false);
region.get(key1);
txMgr.rollback();
assertTrue("TX Invalidate Validation Assertion", cbv.passedValidation());
vCw.validate();
vCl.validateNoEvents();
}