use of org.apache.geode.cache.EntryEvent 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");
}
});
}
use of org.apache.geode.cache.EntryEvent in project geode by apache.
the class SearchAndLoadDUnitTest method testOneHopNetWrite.
@Test
public void testOneHopNetWrite() throws CacheException, InterruptedException {
Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
VM vm1 = host.getVM(1);
final String name = this.getUniqueName() + "Region";
final String objectName = "Object7";
final Integer value = new Integer(483);
final Integer updateValue = new Integer(484);
vm0.invoke(new SerializableRunnable("Create replicated region with cacheWriter") {
public void run() {
netWriteInvoked = false;
operationWasCreate = false;
originWasRemote = false;
writerInvocationCount = 0;
try {
AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.DISTRIBUTED_ACK);
factory.setDataPolicy(DataPolicy.PERSISTENT_REPLICATE);
factory.setCacheWriter(new CacheWriter() {
public void beforeCreate(EntryEvent e) throws CacheWriterException {
e.getRegion().getCache().getLogger().info("cache writer beforeCreate invoked for " + e);
netWriteInvoked = true;
operationWasCreate = true;
originWasRemote = e.isOriginRemote();
writerInvocationCount++;
return;
}
public void beforeUpdate(EntryEvent e) throws CacheWriterException {
e.getRegion().getCache().getLogger().info("cache writer beforeUpdate invoked for " + e);
netWriteInvoked = true;
operationWasCreate = false;
originWasRemote = e.isOriginRemote();
writerInvocationCount++;
return;
}
public void beforeDestroy(EntryEvent e) throws CacheWriterException {
}
public void beforeRegionDestroy(RegionEvent e) throws CacheWriterException {
}
public void beforeRegionClear(RegionEvent e) throws CacheWriterException {
}
public void close() {
}
});
createRegion(name, factory.create());
} catch (CacheException ex) {
Assert.fail("While creating replicated region", ex);
}
}
});
vm1.invoke(new SerializableRunnable("Create empty Region") {
public void run() {
try {
AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.DISTRIBUTED_ACK);
factory.setDataPolicy(DataPolicy.EMPTY);
createRegion(name, factory.create());
} catch (CacheException ex) {
Assert.fail("While creating empty region", ex);
}
}
});
vm1.invoke(new SerializableRunnable("do a put that should be proxied in the other vm and invoke its cache writer") {
public void run() {
try {
getRootRegion().getSubregion(name).put(objectName, value);
} catch (CacheWriterException cwe) {
} catch (TimeoutException te) {
}
}
});
vm0.invoke(new SerializableRunnable("ensure that cache writer was invoked with correct settings in event") {
public void run() {
assertTrue("expected cache writer to be invoked", netWriteInvoked);
assertTrue("expected originRemote to be true", originWasRemote);
assertTrue("expected event to be create", operationWasCreate);
assertEquals("expected only one cache writer invocation", 1, writerInvocationCount);
// set flags for the next test - updating the same key
netWriteInvoked = false;
writerInvocationCount = 0;
}
});
vm1.invoke(new SerializableRunnable("do an update that should be proxied in the other vm and invoke its cache writer") {
public void run() {
try {
getRootRegion().getSubregion(name).put(objectName, updateValue);
} catch (CacheWriterException cwe) {
} catch (TimeoutException te) {
}
}
});
vm0.invoke(new SerializableRunnable("ensure that cache writer was invoked with correct settings in event") {
public void run() {
assertTrue("expected cache writer to be invoked", netWriteInvoked);
assertTrue("expected originRemote to be true", originWasRemote);
assertTrue("expected event to be create", operationWasCreate);
assertEquals("expected only one cache writer invocation", 1, writerInvocationCount);
}
});
}
use of org.apache.geode.cache.EntryEvent in project geode by apache.
the class RegionTestCase method testEntryIdleInvalidate.
/**
* Tests that an entry in a local region that remains idle for a given amount of time is
* invalidated.
*/
@Test
public void testEntryIdleInvalidate() throws CacheException, InterruptedException {
final String name = this.getUniqueName();
// ms
final int timeout = 20;
final String key = "KEY";
final String value = "VALUE";
AttributesFactory factory = new AttributesFactory(getRegionAttributes());
ExpirationAttributes expire = new ExpirationAttributes(timeout, ExpirationAction.INVALIDATE);
factory.setEntryIdleTimeout(expire);
factory.setStatisticsEnabled(true);
TestCacheListener list = new TestCacheListener() {
public void afterCreate2(EntryEvent e) {
}
public void afterUpdate2(EntryEvent e) {
}
public void afterInvalidate2(EntryEvent e) {
}
};
factory.setCacheListener(list);
RegionAttributes attrs = factory.create();
Region region = null;
System.setProperty(LocalRegion.EXPIRY_MS_PROPERTY, "true");
try {
region = createRegion(name, attrs);
// DebuggerSupport.waitForJavaDebugger(getLogWriter(), "Set breakpoint in
// invalidate");
ExpiryTask.suspendExpiration();
Region.Entry entry = null;
long tilt;
try {
region.create(key, value);
tilt = System.currentTimeMillis() + timeout;
assertTrue(list.waitForInvocation(333));
entry = region.getEntry(key);
assertNotNull(entry.getValue());
} finally {
ExpiryTask.permitExpiration();
}
waitForInvalidate(entry, tilt);
ExpiryTask.suspendExpiration();
try {
region.put(key, value);
tilt = System.currentTimeMillis() + timeout;
entry = region.getEntry(key);
assertNotNull(entry.getValue());
} finally {
ExpiryTask.permitExpiration();
}
waitForInvalidate(entry, tilt);
} finally {
System.getProperties().remove(LocalRegion.EXPIRY_MS_PROPERTY);
}
}
use of org.apache.geode.cache.EntryEvent in project geode by apache.
the class SearchAndLoadDUnitTest method testNetWrite.
@Test
public void testNetWrite() throws CacheException, InterruptedException {
Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
VM vm1 = host.getVM(1);
final String name = this.getUniqueName() + "-ACK";
final String objectName = "Gemfire7";
final Integer value = new Integer(483);
vm0.invoke(new SerializableRunnable("Create ACK Region with cacheWriter") {
public void run() {
netWriteInvoked = false;
try {
AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.DISTRIBUTED_ACK);
factory.setCacheWriter(new CacheWriter() {
public void beforeCreate(EntryEvent e) throws CacheWriterException {
netWriteInvoked = true;
return;
}
public void beforeUpdate(EntryEvent e) throws CacheWriterException {
netWriteInvoked = true;
return;
}
public void beforeDestroy(EntryEvent e) throws CacheWriterException {
return;
}
public void beforeRegionDestroy(RegionEvent e) throws CacheWriterException {
return;
}
public void beforeRegionClear(RegionEvent e) throws CacheWriterException {
return;
}
public void close() {
}
});
createRegion(name, factory.create());
} catch (CacheException ex) {
Assert.fail("While creating ACK region", ex);
}
}
});
vm1.invoke(new SerializableRunnable("Create ACK Region") {
public void run() {
loaderInvoked = false;
remoteLoaderInvoked = false;
netWriteInvoked = false;
try {
AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.DISTRIBUTED_ACK);
createRegion(name, factory.create());
} catch (CacheException ex) {
Assert.fail("While creating ACK region", ex);
}
}
});
vm1.invoke(new SerializableRunnable("Do a put operation resulting in cache writer notification in other vm") {
public void run() {
try {
getRootRegion().getSubregion(name).put(objectName, value);
try {
Object result = getRootRegion().getSubregion(name).get(objectName);
assertEquals(result, value);
} catch (CacheLoaderException cle) {
} catch (TimeoutException te) {
}
} catch (CacheWriterException cwe) {
} catch (TimeoutException te) {
}
}
});
vm0.invoke(new SerializableRunnable("ensure that cache writer was invoked") {
public void run() {
assertTrue("expected cache writer to be invoked", netWriteInvoked);
}
});
}
use of org.apache.geode.cache.EntryEvent in project geode by apache.
the class TXOrderDUnitTest method testFarSideOpForLoad.
/**
* Tests fix for #40870 Remote CacheListeners invoke afterCreate with Operation.LOCAL_LOAD_CREATE
* when create executed transactionally"
*/
@Ignore("TODO: test is disabled")
@Test
public void testFarSideOpForLoad() throws Exception {
Host host = Host.getHost(0);
VM vm1 = host.getVM(0);
VM vm2 = host.getVM(1);
vm1.invoke(new SerializableCallable() {
public Object call() throws Exception {
AttributesFactory af = new AttributesFactory();
af.setDataPolicy(DataPolicy.REPLICATE);
af.setScope(Scope.DISTRIBUTED_ACK);
CacheListener cl1 = new CacheListenerAdapter() {
public void afterCreate(EntryEvent e) {
assertTrue(e.getOperation().isLocalLoad());
}
};
af.addCacheListener(cl1);
CacheLoader cl = new CacheLoader() {
public Object load(LoaderHelper helper) throws CacheLoaderException {
LogWriterUtils.getLogWriter().info("Loading value:" + helper.getKey() + "_value");
return helper.getKey() + "_value";
}
public void close() {
}
};
af.setCacheLoader(cl);
createRootRegion("r1", af.create());
return null;
}
});
vm2.invoke(new SerializableCallable() {
public Object call() throws Exception {
AttributesFactory af = new AttributesFactory();
af.setDataPolicy(DataPolicy.REPLICATE);
af.setScope(Scope.DISTRIBUTED_ACK);
CacheListener cl1 = new CacheListenerAdapter() {
public void afterCreate(EntryEvent e) {
LogWriterUtils.getLogWriter().info("op:" + e.getOperation().toString());
assertTrue(!e.getOperation().isLocalLoad());
}
};
af.addCacheListener(cl1);
createRootRegion("r1", af.create());
return null;
}
});
vm1.invoke(new SerializableCallable() {
public Object call() throws Exception {
Region r = getRootRegion("r1");
getCache().getCacheTransactionManager().begin();
r.get("obj_2");
getCache().getCacheTransactionManager().commit();
return null;
}
});
}
Aggregations