use of org.apache.geode.cache.Region.Entry in project geode by apache.
the class RegionTestCase method testRegionTtlInvalidate.
/**
* Tests that a region expires with an invalidation after a given time to live.
*/
@Test
public void testRegionTtlInvalidate() throws CacheException, InterruptedException {
if (getRegionAttributes().getPartitionAttributes() != null)
return;
Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
final String name = this.getUniqueName();
vm0.invoke(new CacheSerializableRunnable("testRegionTtlInvalidate") {
public void run2() throws CacheException {
// ms
final int timeout = 22;
final Object key = "KEY";
final Object value = "VALUE";
AttributesFactory factory = new AttributesFactory(getRegionAttributes());
ExpirationAttributes expire = new ExpirationAttributes(timeout, ExpirationAction.INVALIDATE);
factory.setRegionTimeToLive(expire);
factory.setStatisticsEnabled(true);
RegionAttributes attrs = factory.create();
Region region = null;
Region.Entry entry = null;
long tilt;
ExpiryTask.suspendExpiration();
try {
System.setProperty(LocalRegion.EXPIRY_MS_PROPERTY, "true");
try {
region = createRegion(name, attrs);
region.put(key, value);
region.put("k2", "v2");
tilt = System.currentTimeMillis() + timeout;
entry = region.getEntry(key);
assertNotNull(entry.getValue());
} finally {
System.getProperties().remove(LocalRegion.EXPIRY_MS_PROPERTY);
}
} finally {
ExpiryTask.permitExpiration();
}
waitForInvalidate(entry, tilt, 10);
waitForInvalidate(region.getEntry("k2"), tilt, 10);
}
});
}
use of org.apache.geode.cache.Region.Entry in project geode by apache.
the class RegionTestCase method testInvalidateEntry.
/**
* Tests invalidating a region entry
*/
@Test
public void testInvalidateEntry() throws CacheException {
String name = this.getUniqueName();
Object key = "KEY";
Object value = "VALUE";
Region region = createRegion(name);
region.put(key, value);
int beforeInvalidates = ((org.apache.geode.internal.cache.GemFireCacheImpl) getCache()).getCachePerfStats().getInvalidates();
Region.Entry entry = region.getEntry(key);
region.invalidate(key);
if (entry.isLocal()) {
assertNull(entry.getValue());
}
assertNull(region.get(key));
int afterInvalidates = ((org.apache.geode.internal.cache.GemFireCacheImpl) getCache()).getCachePerfStats().getInvalidates();
assertEquals("Invalidate CachePerfStats incorrect", beforeInvalidates + 1, afterInvalidates);
}
use of org.apache.geode.cache.Region.Entry in project geode by apache.
the class RegionTestCase method testLocalDestroyRegion.
/**
* Tests locally destroying an entire region and that accessing it after it has been destory
* causes a {@link RegionDestroyedException}.
*
* @see Region#localDestroyRegion
*/
@Test
public void testLocalDestroyRegion() throws CacheException {
String name = this.getUniqueName();
Object key = "KEY";
Object value = "VALUE";
Region region = createRegion(name);
region.put(key, value);
Region.Entry entry = region.getEntry(key);
assertNotNull(entry);
region.createSubregion("SUB", region.getAttributes());
region.localDestroyRegion();
assertTrue(entry.isDestroyed());
assertTrue(region.isDestroyed());
try {
region.containsKey(key);
fail("Should have thrown a RegionDestroyedException");
} catch (RegionDestroyedException ex) {
// pass..
}
try {
region.containsValueForKey(key);
fail("Should have thrown a RegionDestroyedException");
} catch (RegionDestroyedException ex) {
// pass..
}
try {
region.create(key, value);
fail("Should have thrown a RegionDestroyedException");
} catch (RegionDestroyedException ex) {
// pass..
}
try {
region.create(key, value, "BLAH");
fail("Should have thrown a RegionDestroyedException");
} catch (RegionDestroyedException ex) {
// pass..
}
try {
region.createSubregion("SUB", this.getRegionAttributes());
fail("Should have thrown a RegionDestroyedException");
} catch (RegionDestroyedException ex) {
// pass..
}
try {
region.destroy(key);
fail("Should have thrown a RegionDestroyedException");
} catch (RegionDestroyedException ex) {
// pass..
}
try {
region.destroy(key, "BLAH");
fail("Should have thrown a RegionDestroyedException");
} catch (RegionDestroyedException ex) {
// pass..
}
try {
region.destroyRegion();
fail("Should have thrown a RegionDestroyedException");
} catch (RegionDestroyedException ex) {
// pass..
}
try {
region.destroyRegion("ARG");
fail("Should have thrown a RegionDestroyedException");
} catch (RegionDestroyedException ex) {
// pass..
}
try {
region.entrySet(false);
fail("Should have thrown a RegionDestroyedException");
} catch (RegionDestroyedException ex) {
// pass..
}
try {
region.get(key);
fail("Should have thrown a RegionDestroyedException");
} catch (RegionDestroyedException ex) {
// pass..
}
try {
region.get(key, "ARG");
fail("Should have thrown a RegionDestroyedException");
} catch (RegionDestroyedException ex) {
// pass..
}
try {
region.containsKey(key);
fail("Should have thrown a RegionDestroyedException");
} catch (RegionDestroyedException ex) {
// pass..
}
region.getAttributes();
try {
region.getAttributesMutator();
fail("Should have thrown a RegionDestroyedException");
} catch (RegionDestroyedException ex) {
// pass..
}
try {
region.containsKey(key);
fail("Should have thrown a RegionDestroyedException");
} catch (RegionDestroyedException ex) {
// pass..
}
try {
region.getCache();
} catch (RegionDestroyedException ex) {
fail("getCache() shouldn't have thrown a RegionDestroyedException");
}
try {
region.getEntry(key);
fail("Should have thrown a RegionDestroyedException");
} catch (RegionDestroyedException ex) {
// pass..
}
try {
region.getDistributedLock(key);
fail("Should have thrown a RegionDestroyedException");
} catch (RegionDestroyedException ex) {
// pass..
}
assertEquals(name, region.getName());
region.getParentRegion();
assertEquals("/root/" + name, region.getFullPath());
try {
region.getRegionDistributedLock();
fail("Should have thrown a RegionDestroyedException");
} catch (RegionDestroyedException ex) {
// pass..
}
try {
region.getStatistics();
fail("Should have thrown a RegionDestroyedException");
} catch (RegionDestroyedException ex) {
// pass..
}
try {
region.getSubregion("SUB");
fail("Should have thrown a RegionDestroyedException");
} catch (RegionDestroyedException ex) {
// pass..
}
region.getUserAttribute();
try {
region.invalidate(key);
fail("Should have thrown a RegionDestroyedException");
} catch (RegionDestroyedException ex) {
// pass..
}
try {
region.invalidateRegion();
fail("Should have thrown a RegionDestroyedException");
} catch (RegionDestroyedException ex) {
// pass..
}
try {
region.keySet();
fail("Should have thrown a RegionDestroyedException");
} catch (RegionDestroyedException ex) {
// pass..
}
try {
region.localDestroy(key);
fail("Should have thrown a RegionDestroyedException");
} catch (RegionDestroyedException ex) {
// pass..
}
try {
region.localDestroyRegion();
fail("Should have thrown a RegionDestroyedException");
} catch (RegionDestroyedException ex) {
// pass..
}
try {
region.localInvalidate(key);
fail("Should have thrown a RegionDestroyedException");
} catch (RegionDestroyedException ex) {
// pass..
}
try {
region.localInvalidateRegion();
fail("Should have thrown a RegionDestroyedException");
} catch (RegionDestroyedException ex) {
// pass..
}
try {
region.put(key, value);
fail("Should have thrown a RegionDestroyedException");
} catch (RegionDestroyedException ex) {
// pass..
}
try {
region.put(key, value, "ARG");
fail("Should have thrown a RegionDestroyedException");
} catch (RegionDestroyedException ex) {
// pass..
}
try {
region.setUserAttribute("ATTR");
fail("Should have thrown a RegionDestroyedException");
} catch (RegionDestroyedException ex) {
// pass..
}
try {
region.subregions(true);
fail("Should have thrown a RegionDestroyedException");
} catch (RegionDestroyedException ex) {
// pass..
}
try {
region.values();
fail("Should have thrown a RegionDestroyedException");
} catch (RegionDestroyedException ex) {
// pass..
}
}
use of org.apache.geode.cache.Region.Entry in project geode by apache.
the class RegionTestCase method testRegionTtlDestroy.
/**
* Tests that a region expires with a destruction after a given time to live.
*/
@Test
public void testRegionTtlDestroy() throws CacheException, InterruptedException {
if (getRegionAttributes().getPartitionAttributes() != null)
return;
final String name = this.getUniqueName();
// ms
final int timeout = 22;
final Object key = "KEY";
final Object value = "VALUE";
AttributesFactory factory = new AttributesFactory(getRegionAttributes());
ExpirationAttributes expire = new ExpirationAttributes(timeout, ExpirationAction.DESTROY);
factory.setRegionTimeToLive(expire);
factory.setStatisticsEnabled(true);
RegionAttributes attrs = factory.create();
Region region = null;
long tilt;
System.setProperty(LocalRegion.EXPIRY_MS_PROPERTY, "true");
ExpiryTask.suspendExpiration();
try {
try {
region = createRegion(name, attrs);
assertFalse(region.isDestroyed());
tilt = System.currentTimeMillis() + timeout;
region.put(key, value);
Region.Entry entry = region.getEntry(key);
assertNotNull(entry.getValue());
} finally {
System.getProperties().remove(LocalRegion.EXPIRY_MS_PROPERTY);
}
} finally {
ExpiryTask.permitExpiration();
}
waitForRegionDestroy(region, tilt);
}
use of org.apache.geode.cache.Region.Entry in project geode by apache.
the class RegionTestCase method testEntryIdleTimeout3.
/**
* Configure entry expiration with a idle time. Create an entry and records its scheduled
* expiration time. Then mutate the region expiration configuration and confirm that the entry's
* expiration time is rescheduled.
*/
@Test
public void testEntryIdleTimeout3() {
final String name = this.getUniqueName();
// test no longer waits for this expiration to happen
// ms
final int timeout1 = 500 * 1000;
// ms
final int timeout2 = 2000 * 1000;
final String key1 = "KEY1";
final String value1 = "VALUE1";
AttributesFactory factory = new AttributesFactory(getRegionAttributes());
ExpirationAttributes expire1 = new ExpirationAttributes(timeout1, ExpirationAction.INVALIDATE);
factory.setEntryIdleTimeout(expire1);
factory.setStatisticsEnabled(true);
TestCacheListener list = new TestCacheListener() {
public void afterCreate2(EntryEvent e) {
}
public void afterUpdate2(EntryEvent e) {
}
public void afterInvalidate2(EntryEvent e) {
eventCount++;
}
};
eventCount = 0;
factory.addCacheListener(list);
RegionAttributes attrs = factory.create();
LocalRegion region;
System.setProperty(LocalRegion.EXPIRY_MS_PROPERTY, "true");
try {
region = (LocalRegion) createRegion(name, attrs);
} finally {
System.getProperties().remove(LocalRegion.EXPIRY_MS_PROPERTY);
}
region.create(key1, value1);
EntryExpiryTask eet = region.getEntryExpiryTask(key1);
final long firstExpiryTime = eet.getExpirationTime();
AttributesMutator mutt = region.getAttributesMutator();
ExpirationAttributes expire2 = new ExpirationAttributes(timeout2, ExpirationAction.INVALIDATE);
mutt.setEntryIdleTimeout(expire2);
eet = region.getEntryExpiryTask(key1);
final long secondExpiryTime = eet.getExpirationTime();
if ((secondExpiryTime - firstExpiryTime) <= 0) {
fail("expiration time should have been greater after changing region config from 500 to 2000. firstExpiryTime=" + firstExpiryTime + " secondExpiryTime=" + secondExpiryTime);
}
// now set back to be more recent
mutt = region.getAttributesMutator();
ExpirationAttributes expire3 = new ExpirationAttributes(timeout1, ExpirationAction.INVALIDATE);
mutt.setEntryIdleTimeout(expire3);
eet = region.getEntryExpiryTask(key1);
final long thirdExpiryTime = eet.getExpirationTime();
assertEquals(firstExpiryTime, thirdExpiryTime);
// confirm that it still has not expired
assertEquals(0, eventCount);
// now set it to a really short time and make sure it expires immediately
Wait.waitForExpiryClockToChange(region);
final Region.Entry entry = region.getEntry(key1);
mutt = region.getAttributesMutator();
ExpirationAttributes expire4 = new ExpirationAttributes(1, ExpirationAction.INVALIDATE);
mutt.setEntryIdleTimeout(expire4);
WaitCriterion wc = new WaitCriterion() {
public boolean done() {
return fetchEntryValue(entry) == null;
}
public String description() {
return "entry never became invalid";
}
};
Wait.waitForCriterion(wc, 10 * 1000, 10, true);
WaitCriterion waitForEventCountToBeOne = new WaitCriterion() {
public boolean done() {
return eventCount == 1;
}
public String description() {
return "eventCount never became 1";
}
};
Wait.waitForCriterion(waitForEventCountToBeOne, 10 * 1000, 10, true);
eventCount = 0;
}
Aggregations