use of org.apache.geode.cache.util.CacheListenerAdapter in project geode by apache.
the class Bug34387DUnitTest method testCreateAndLI.
/**
* test create followed by localInvalidate
*/
@Test
public void testCreateAndLI() throws CacheException {
initOtherId();
AttributesFactory af = new AttributesFactory();
af.setDataPolicy(DataPolicy.REPLICATE);
af.setScope(Scope.DISTRIBUTED_ACK);
af.setConcurrencyChecksEnabled(true);
callbackFailure = false;
CacheListener cl1 = new CacheListenerAdapter() {
public void afterCreate(EntryEvent e) {
callbackAssertEquals("key not equal", "createKey", e.getKey());
callbackAssertEquals("value not equal", "createValue", e.getNewValue());
Bug34387DUnitTest.this.invokeCount++;
}
};
af.addCacheListener(cl1);
Region r1 = createRootRegion("r1", af.create());
this.invokeCount = 0;
assertNull(r1.getEntry("createKey"));
doCommitOtherVm(false);
assertNotNull(r1.getEntry("createKey"));
assertEquals("createValue", r1.getEntry("createKey").getValue());
assertEquals(1, this.invokeCount);
assertFalse("Errors in callbacks; check logs for details", callbackFailure);
}
use of org.apache.geode.cache.util.CacheListenerAdapter in project geode by apache.
the class Bug38013DUnitTest method doCreateOtherVm.
private void doCreateOtherVm() {
VM vm = getOtherVm();
vm.invoke(new CacheSerializableRunnable("create root") {
public void run2() throws CacheException {
getSystem();
AttributesFactory af = new AttributesFactory();
CacheListener cl = new CacheListenerAdapter() {
public void afterCreate(EntryEvent event) {
// getLogWriter().info("afterCreate " + event.getKey());
if (event.getCallbackArgument() != null) {
lastCallback = event.getCallbackArgument();
}
}
public void afterUpdate(EntryEvent event) {
// getLogWriter().info("afterUpdate " + event.getKey());
if (event.getCallbackArgument() != null) {
lastCallback = event.getCallbackArgument();
}
}
public void afterInvalidate(EntryEvent event) {
if (event.getCallbackArgument() != null) {
lastCallback = event.getCallbackArgument();
}
}
public void afterDestroy(EntryEvent event) {
if (event.getCallbackArgument() != null) {
lastCallback = event.getCallbackArgument();
}
}
};
af.setCacheListener(cl);
// create a pr with a data store
PartitionAttributesFactory paf = new PartitionAttributesFactory();
paf.setRedundantCopies(0);
// use defaults so this is a data store
af.setPartitionAttributes(paf.create());
createRootRegion("bug38013", af.create());
}
});
}
use of org.apache.geode.cache.util.CacheListenerAdapter in project geode by apache.
the class ProxyJUnitTest method testExpiration.
/**
* Make sure a proxy region expiration behaves as expected
*/
@Test
public void testExpiration() throws Exception {
System.setProperty(LocalRegion.EXPIRY_MS_PROPERTY, "true");
try {
// now make sure they don't on proxy
{
AttributesFactory af = new AttributesFactory();
af.setStatisticsEnabled(true);
af.setEntryIdleTimeout(new ExpirationAttributes(1, ExpirationAction.LOCAL_INVALIDATE));
af.setEntryTimeToLive(new ExpirationAttributes(2, ExpirationAction.LOCAL_DESTROY));
af.setDataPolicy(DataPolicy.EMPTY);
try {
af.create();
fail("expected IllegalStateException");
} catch (IllegalStateException expected) {
}
}
// make sure regionIdleTimeout works on proxy
{
CacheListener cl1 = new CacheListenerAdapter() {
public void afterRegionDestroy(RegionEvent e) {
clInvokeCount++;
}
public void afterRegionInvalidate(RegionEvent e) {
clInvokeCount++;
}
};
AttributesFactory af = new AttributesFactory();
af.setStatisticsEnabled(true);
final int EXPIRE_MS = 500;
af.setRegionIdleTimeout(new ExpirationAttributes(EXPIRE_MS, ExpirationAction.LOCAL_DESTROY));
af.addCacheListener(cl1);
af.setDataPolicy(DataPolicy.EMPTY);
clearCallbackState();
Region r = this.c.createRegion("rEMPTY", af.create());
assertTrue(clInvokeCount == 0);
r.put("key", "value");
long endTime = System.currentTimeMillis() + (EXPIRE_MS * 2);
do {
r.get("key");
} while (System.currentTimeMillis() < endTime);
assertEquals(0, this.clInvokeCount);
Thread.sleep(EXPIRE_MS * 2);
boolean done = false;
try {
for (StopWatch time = new StopWatch(true); !done && time.elapsedTimeMillis() < 1000; done = (ProxyJUnitTest.this.clInvokeCount == 1)) {
Thread.sleep(200);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
assertTrue("waiting for invocation", done);
}
// make sure regionTimeToLive works on proxy
{
CacheListener cl1 = new CacheListenerAdapter() {
public void afterRegionDestroy(RegionEvent e) {
clInvokeCount++;
}
public void afterRegionInvalidate(RegionEvent e) {
clInvokeCount++;
}
};
AttributesFactory af = new AttributesFactory();
af.setStatisticsEnabled(true);
final int EXPIRE_MS = 500;
af.setRegionTimeToLive(new ExpirationAttributes(EXPIRE_MS, ExpirationAction.LOCAL_DESTROY));
af.addCacheListener(cl1);
af.setDataPolicy(DataPolicy.EMPTY);
clearCallbackState();
Region r = this.c.createRegion("rEMPTY", af.create());
assertTrue(clInvokeCount == 0);
r.put("key", "value");
long endTime = System.currentTimeMillis() + (EXPIRE_MS * 2);
do {
r.put("key", "value");
} while (System.currentTimeMillis() < endTime);
assertEquals(0, this.clInvokeCount);
Thread.sleep(EXPIRE_MS * 2);
boolean done = false;
try {
for (StopWatch time = new StopWatch(true); !done && time.elapsedTimeMillis() < 1000; done = (ProxyJUnitTest.this.clInvokeCount == 1)) {
Thread.sleep(200);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
assertTrue("waiting for invocation", done);
}
} finally {
System.clearProperty(LocalRegion.EXPIRY_MS_PROPERTY);
assertEquals(null, System.getProperty(LocalRegion.EXPIRY_MS_PROPERTY));
}
}
use of org.apache.geode.cache.util.CacheListenerAdapter in project geode by apache.
the class Bug34387DUnitTest method testCreateAndLD.
////////////////////// Test Methods //////////////////////
/**
* test create followed by localDestroy
*/
@Test
public void testCreateAndLD() throws CacheException {
initOtherId();
AttributesFactory af = new AttributesFactory();
af.setDataPolicy(DataPolicy.REPLICATE);
af.setScope(Scope.DISTRIBUTED_ACK);
af.setConcurrencyChecksEnabled(true);
callbackFailure = false;
CacheListener cl1 = new CacheListenerAdapter() {
public void afterCreate(EntryEvent e) {
callbackAssertEquals("Keys not equal", "createKey", e.getKey());
callbackAssertEquals("Values not equal", "createValue", e.getNewValue());
Bug34387DUnitTest.this.invokeCount++;
}
};
af.addCacheListener(cl1);
Region r1 = createRootRegion("r1", af.create());
this.invokeCount = 0;
assertNull(r1.getEntry("createKey"));
doCommitOtherVm(true);
assertNotNull(r1.getEntry("createKey"));
assertEquals("createValue", r1.getEntry("createKey").getValue());
assertEquals(1, this.invokeCount);
assertFalse("Errors in callbacks; check logs for details", callbackFailure);
}
use of org.apache.geode.cache.util.CacheListenerAdapter in project geode by apache.
the class SlowRecDUnitTest method doCreateOtherVm.
private void doCreateOtherVm(final Properties p, final boolean addListener) {
VM vm = getOtherVm();
vm.invoke(new CacheSerializableRunnable("create root") {
public void run2() throws CacheException {
getSystem(p);
createAckRegion(true, false);
AttributesFactory af = new AttributesFactory();
af.setScope(Scope.DISTRIBUTED_NO_ACK);
af.setDataPolicy(DataPolicy.REPLICATE);
if (addListener) {
CacheListener cl = new CacheListenerAdapter() {
public void afterUpdate(EntryEvent event) {
// make the slow receiver event slower!
try {
Thread.sleep(500);
} catch (InterruptedException shuttingDown) {
fail("interrupted");
}
}
};
af.setCacheListener(cl);
} else {
CacheListener cl = new CacheListenerAdapter() {
public void afterCreate(EntryEvent event) {
if (event.getCallbackArgument() != null) {
lastCallback = event.getCallbackArgument();
}
if (event.getKey().equals("sleepkey")) {
int sleepMs = ((Integer) event.getNewValue()).intValue();
try {
Thread.sleep(sleepMs);
} catch (InterruptedException ignore) {
fail("interrupted");
}
}
}
public void afterUpdate(EntryEvent event) {
if (event.getCallbackArgument() != null) {
lastCallback = event.getCallbackArgument();
}
if (event.getKey().equals("sleepkey")) {
int sleepMs = ((Integer) event.getNewValue()).intValue();
try {
Thread.sleep(sleepMs);
} catch (InterruptedException ignore) {
fail("interrupted");
}
}
}
public void afterInvalidate(EntryEvent event) {
if (event.getCallbackArgument() != null) {
lastCallback = event.getCallbackArgument();
}
}
public void afterDestroy(EntryEvent event) {
if (event.getCallbackArgument() != null) {
lastCallback = event.getCallbackArgument();
}
}
};
af.setCacheListener(cl);
}
Region r1 = createRootRegion("slowrec", af.create());
// place holder so we receive updates
r1.create("key", "value");
}
});
}
Aggregations