use of org.apache.geode.cache.RegionEvent in project geode by apache.
the class RegionTestCase method testCloseRegion.
/**
* Tests closing a region, and checks different behavior when this is a disk region with
* persistBackup.
*/
@Test
public void testCloseRegion() throws CacheException {
// @todo added a remote region to make sure close just does a localDestroy
String name = this.getUniqueName();
AttributesFactory fac = new AttributesFactory(getRegionAttributes());
TestCacheListener list = new TestCacheListener() {
public void afterCreate2(EntryEvent event) {
// do nothing
}
public void afterRegionDestroy2(RegionEvent re) {
assertEquals(Operation.REGION_CLOSE, re.getOperation());
}
public void close2() {
// okay
}
};
fac.setCacheListener(list);
RegionAttributes attrs = fac.create();
Region region = createRegion(name, attrs);
File diskDir = null;
if (attrs.getDataPolicy().withPersistence()) {
diskDir = getCache().findDiskStore(attrs.getDiskStoreName()).getDiskDirs()[0];
// @todo We no longer start with a clean slate because the DiskStore hangs around.
// If we want a clean slate then we need to destroy the DiskStore after each
// test completes.
// assert that if this is a disk region, the disk dirs are empty
// to make sure we start with a clean slate
getCache().getLogger().info("list=" + Arrays.toString(diskDir.list()));
// assertIndexDetailsEquals("list="+Arrays.toString(diskDir.list()),
// 0, diskDir.list().length);
}
for (int i = 0; i < 1000; i++) {
region.put(new Integer(i), String.valueOf(i));
}
// reset wasInvoked after creates
assertTrue(list.wasInvoked());
// assert that if this is a disk region, the disk dirs are not empty
if (attrs.getDataPolicy().withPersistence()) {
assertTrue(diskDir.list().length > 0);
}
boolean persistent = region.getAttributes().getDataPolicy().withPersistence();
region.close();
// assert that if this is a disk region, the disk dirs are not empty
if (attrs.getDataPolicy().withPersistence()) {
assertTrue(diskDir.list().length > 0);
}
assertTrue(list.waitForInvocation(333));
assertTrue(list.isClosed());
assertTrue(region.isDestroyed());
// if (persistent) {
// // remove this when bug #41049 is fixed
// return;
// }
// if this is a disk region, then check to see if recreating the region
// repopulates with data
region = createRegion(name, attrs);
if (attrs.getDataPolicy().withPersistence()) {
for (int i = 0; i < 1000; i++) {
Region.Entry entry = region.getEntry(new Integer(i));
assertNotNull("entry " + i + " not found", entry);
assertEquals(String.valueOf(i), entry.getValue());
}
assertEquals(1000, region.keySet().size());
} else {
assertEquals(0, region.keySet().size());
}
region.localDestroyRegion();
}
use of org.apache.geode.cache.RegionEvent in project geode by apache.
the class RegionReliabilityTestCase method testRegionDistributionException.
@Test
public void testRegionDistributionException() throws Exception {
if (getRegionScope().isDistributedNoAck())
// skip test under DistributedNoAck
return;
final String name = this.getUniqueName();
final String roleA = name + "-A";
final String[] requiredRoles = { roleA };
Set requiredRolesSet = new HashSet();
for (int i = 0; i < requiredRoles.length; i++) {
requiredRolesSet.add(InternalRole.getRole(requiredRoles[i]));
}
assertEquals(requiredRoles.length, requiredRolesSet.size());
// connect controller to system...
Properties config = new Properties();
config.setProperty(ROLES, "");
getSystem(config);
getCache();
RegionMembershipListener listener = new RegionMembershipListenerAdapter() {
public void afterRemoteRegionDeparture(RegionEvent event) {
synchronized (detectedDeparture_testRegionDistributionException) {
detectedDeparture_testRegionDistributionException[0] = Boolean.TRUE;
detectedDeparture_testRegionDistributionException.notify();
}
}
};
// create region in controller...
MembershipAttributes ra = new MembershipAttributes(requiredRoles, LossAction.NO_ACCESS, ResumptionAction.NONE);
AttributesFactory fac = new AttributesFactory();
fac.setMembershipAttributes(ra);
fac.setScope(getRegionScope());
// fac.addCacheListener(listener);
RegionAttributes attr = fac.create();
Region region = createRootRegion(name, attr);
assertTrue(((AbstractRegion) region).requiresReliabilityCheck());
// use vm1 to create role
CacheSerializableRunnable createRegion = new CacheSerializableRunnable("Create Region") {
public void run2() throws CacheException {
createConnection(new String[] { roleA });
AttributesFactory fac = new AttributesFactory();
fac.setScope(getRegionScope());
RegionAttributes attr = fac.create();
createRootRegion(name, attr);
}
};
Host.getHost(0).getVM(1).invoke(createRegion);
region.put("DESTROY_ME", "VAL");
region.put("INVALIDATE_ME", "VAL");
// define the afterReleaseLocalLocks callback
SerializableRunnable removeRequiredRole = new SerializableRunnable() {
public void run() {
Host.getHost(0).getVM(1).invoke(new SerializableRunnable("Close Region") {
public void run() {
getRootRegion(name).close();
}
});
// try {
// synchronized (detectedDeparture_testRegionDistributionException) {
// while (detectedDeparture_testRegionDistributionException[0] == Boolean.FALSE) {
// detectedDeparture_testRegionDistributionException.wait();
// }
// }
// }
// catch (InterruptedException e) {}
}
};
DistributedCacheOperation.setBeforePutOutgoing(() -> {
try {
removeRequiredRole.run();
} catch (Exception e) {
throw new RuntimeException(e);
}
});
Runnable reset = new Runnable() {
public void run() {
// synchronized (detectedDeparture_testRegionDistributionException) {
// detectedDeparture_testRegionDistributionException[0] = Boolean.FALSE;
// }
}
};
// PUT
try {
region.put("KEY", "VAL");
fail("Should have thrown RegionDistributionException");
} catch (RegionDistributionException e) {
// pass
}
// INVALIDATE
reset.run();
Host.getHost(0).getVM(1).invoke(createRegion);
try {
region.invalidate("INVALIDATE_ME");
fail("Should have thrown RegionDistributionException");
} catch (RegionDistributionException e) {
// pass
}
// DESTROY
reset.run();
Host.getHost(0).getVM(1).invoke(createRegion);
try {
region.destroy("DESTROY_ME");
fail("Should have thrown RegionDistributionException");
} catch (RegionDistributionException e) {
// pass
}
// CLEAR
reset.run();
Host.getHost(0).getVM(1).invoke(createRegion);
try {
region.clear();
fail("Should have thrown RegionDistributionException");
} catch (RegionDistributionException e) {
// pass
}
// PUTALL
reset.run();
Host.getHost(0).getVM(1).invoke(createRegion);
try {
Map putAll = new HashMap();
putAll.put("PUTALL_ME", "VAL");
region.putAll(putAll);
fail("Should have thrown RegionDistributionException");
} catch (RegionDistributionException e) {
// pass
}
// INVALIDATE REGION
reset.run();
Host.getHost(0).getVM(1).invoke(createRegion);
try {
region.invalidateRegion();
fail("Should have thrown RegionDistributionException");
} catch (RegionDistributionException e) {
// pass
}
// DESTROY REGION
reset.run();
Host.getHost(0).getVM(1).invoke(createRegion);
try {
region.destroyRegion();
fail("Should have thrown RegionDistributionException");
} catch (RegionDistributionException e) {
// pass
}
}
use of org.apache.geode.cache.RegionEvent in project geode by apache.
the class HAClearDUnitTest method createClientCache.
public static void createClientCache(String hostName, Integer port1, Integer port2, Boolean listenerAttached, Boolean registerInterest) throws Exception {
int PORT1 = port1.intValue();
int PORT2 = port2.intValue();
boolean isListenerAttached = listenerAttached.booleanValue();
boolean isRegisterInterest = registerInterest.booleanValue();
Properties props = new Properties();
props.setProperty(MCAST_PORT, "0");
props.setProperty(LOCATORS, "");
new HAClearDUnitTest().createCache(props);
AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.DISTRIBUTED_ACK);
ClientServerTestCase.configureConnectionPool(factory, hostName, new int[] { PORT1, PORT2 }, true, -1, 2, null);
if (isListenerAttached) {
factory.setCacheListener(new CacheListenerAdapter() {
public void afterRegionClear(RegionEvent event) {
LogWriterUtils.getLogWriter().info("-------> afterRegionClear received");
synchronized (HAClearDUnitTest.class) {
gotClearCallback = true;
HAClearDUnitTest.class.notifyAll();
}
}
public void afterRegionDestroy(RegionEvent event) {
synchronized (HAClearDUnitTest.class) {
LogWriterUtils.getLogWriter().info("-------> afterRegionDestroy received");
gotDestroyRegionCallback = true;
HAClearDUnitTest.class.notifyAll();
}
}
});
}
RegionAttributes attrs = factory.create();
cache.createRegion(REGION_NAME, attrs);
Region region = cache.getRegion(Region.SEPARATOR + REGION_NAME);
assertNotNull(region);
if (isRegisterInterest) {
region.registerInterest("ALL_KEYS", InterestResultPolicy.NONE);
} else {
// don't want data but do want events
region.registerInterestRegex("*.", false, false);
}
}
use of org.apache.geode.cache.RegionEvent in project geode by apache.
the class EventIdOptimizationDUnitTest method createClientCache2.
/**
* Creates the client cache2, connected to server3
*
* @param port - bridgeserver port
* @throws Exception - thrown if any problem occurs in setting up the client
*/
public static void createClientCache2(String hostName, Integer port) throws Exception {
Properties props = new Properties();
props.setProperty(MCAST_PORT, "0");
props.setProperty(LOCATORS, "");
new EventIdOptimizationDUnitTest().createCache(props);
AttributesFactory factory = new AttributesFactory();
ClientServerTestCase.configureConnectionPool(factory, hostName, port.intValue(), -1, true, -1, 2, null);
factory.setScope(Scope.DISTRIBUTED_ACK);
factory.addCacheListener(new CacheListenerAdapter() {
public void afterCreate(EntryEvent event) {
String key = (String) event.getKey();
validateEventsAtReceivingClientListener(key);
}
public void afterDestroy(EntryEvent event) {
String key = (String) event.getKey();
validateEventsAtReceivingClientListener(key);
}
public void afterRegionDestroy(RegionEvent event) {
validateEventsAtReceivingClientListener(" <destroyRegion Event> ");
}
public void afterRegionClear(RegionEvent event) {
validateEventsAtReceivingClientListener(" <clearRegion Event> ");
}
});
RegionAttributes attrs = factory.create();
Region region = cache.createRegion(REGION_NAME, attrs);
region.registerInterest("ALL_KEYS");
for (int i = 0; i < eventIds.length; i++) {
region = cache.createRegion(REGION_NAME + i, attrs);
region.registerInterest("ALL_KEYS");
}
pool = (PoolImpl) PoolManager.find("testPool");
}
use of org.apache.geode.cache.RegionEvent in project geode by apache.
the class DistributionManagerDUnitTest method getSleepingListener.
static CacheListener getSleepingListener(final boolean playDead) {
regionDestroyedInvoked = false;
return new CacheListenerAdapter() {
@Override
public void afterCreate(EntryEvent event) {
try {
if (playDead) {
MembershipManagerHelper.beSickMember(getSystemStatic());
MembershipManagerHelper.playDead(getSystemStatic());
}
Thread.sleep(15000);
} catch (InterruptedException ie) {
fail("interrupted", ie);
}
}
@Override
public void afterRegionDestroy(RegionEvent event) {
LogWriter logger = myCache.getLogger();
logger.info("afterRegionDestroyed invoked in sleeping listener");
logger.info("<ExpectedException action=remove>service failure</ExpectedException>");
logger.info("<ExpectedException action=remove>org.apache.geode.ForcedDisconnectException</ExpectedException>");
regionDestroyedInvoked = true;
}
};
}
Aggregations