use of org.apache.geode.cache.LoaderHelper in project geode by apache.
the class GemcachedBinaryClientJUnitTest method testCacheLoaderException.
@SuppressWarnings("unchecked")
public void testCacheLoaderException() throws Exception {
MemcachedClient client = createMemcachedClient();
assertTrue(client.set("key", 0, "value").get());
GemFireCacheImpl cache = GemFireCacheImpl.getInstance();
Region region = cache.getRegion(GemFireMemcachedServer.REGION_NAME);
region.getAttributesMutator().setCacheLoader(new CacheLoader() {
@Override
public void close() {
}
@Override
public Object load(LoaderHelper helper) throws CacheLoaderException {
if (helper.getKey().equals(KeyWrapper.getWrappedKey("exceptionkey".getBytes()))) {
throw new RuntimeException("ExpectedStrings: Cache loader exception");
}
return null;
}
});
long start = System.nanoTime();
try {
client.get("exceptionkey");
throw new RuntimeException("expected exception not thrown");
} catch (Exception e) {
// expected
}
assertEquals("value", client.get("key"));
}
use of org.apache.geode.cache.LoaderHelper in project geode by apache.
the class DiskRegionDUnitTest method testLRUCCSizeOne.
/**
* Tests a disk-based region with an {@link LRUCapacityController} with size 1 and an eviction
* action of "overflow".
*/
@Test
public void testLRUCCSizeOne() throws CacheException {
int threshold = 1;
final String name = this.getUniqueName();
AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.LOCAL);
factory.setEvictionAttributes(EvictionAttributes.createLRUEntryAttributes(threshold, EvictionAction.OVERFLOW_TO_DISK));
factory.setCacheLoader(new CacheLoader() {
public Object load(LoaderHelper helper) throws CacheLoaderException {
return "LOADED VALUE";
}
public void close() {
}
});
DiskStoreFactory dsf = getCache().createDiskStoreFactory();
factory.setDiskSynchronous(true);
File d = new File("DiskRegions" + OSProcess.getId());
d.mkdirs();
dsf.setDiskDirs(new File[] { d });
DiskStore ds = dsf.create(name);
factory.setDiskStoreName(ds.getName());
Region region = createRegion(name, factory.create());
LRUStatistics lruStats = getLRUStats(region);
assertNotNull(lruStats);
for (int i = 1; i <= 1; i++) {
Object key = new Integer(i);
Object value = String.valueOf(i);
region.put(key, value);
assertEquals(1, lruStats.getCounter());
assertEquals(0, lruStats.getEvictions());
}
for (int i = 2; i <= 10; i++) {
Object key = new Integer(i);
Object value = String.valueOf(i);
region.put(key, value);
assertEquals(1, lruStats.getCounter());
assertEquals(i - 1, lruStats.getEvictions());
}
for (int i = 11; i <= 20; i++) {
Object key = new Integer(i);
// Object value = String.valueOf(i);
// Invoke loader
region.get(key);
assertEquals(1, lruStats.getCounter());
assertEquals(i - 1, lruStats.getEvictions());
}
}
use of org.apache.geode.cache.LoaderHelper in project geode by apache.
the class GlobalRegionDUnitTest method testPutGetTimeout.
/**
* Tests that {@link Region#put} and {@link Region#get} timeout when another VM holds the
* distributed lock on the entry in question.
*/
@Test
public void testPutGetTimeout() {
assertEquals(Scope.GLOBAL, getRegionAttributes().getScope());
final String name = this.getUniqueName();
final Object key = "KEY";
final Object value = "VALUE";
Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
VM vm1 = host.getVM(1);
SerializableRunnable create = new CacheSerializableRunnable("Create Region") {
public void run2() throws CacheException {
createRegion(name);
}
};
vm0.invoke(create);
vm1.invoke(create);
vm0.invoke(new CacheSerializableRunnable("Lock entry") {
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(name);
Lock lock = region.getDistributedLock(key);
lock.lock();
}
});
vm1.invoke(new CacheSerializableRunnable("Attempt get/put") {
public void run2() throws CacheException {
Cache cache = getCache();
cache.setLockTimeout(1);
cache.setSearchTimeout(1);
Region region = getRootRegion().getSubregion(name);
try {
region.put(key, value);
fail("Should have thrown a TimeoutException on put");
} catch (TimeoutException ex) {
// pass..
}
// With a loader, should try to lock and time out
region.getAttributesMutator().setCacheLoader(new TestCacheLoader() {
public Object load2(LoaderHelper helper) {
return null;
}
});
try {
region.get(key);
fail("Should have thrown a TimeoutException on get");
} catch (TimeoutException ex) {
// pass..
}
// Without a loader, should succeed
region.getAttributesMutator().setCacheLoader(null);
region.get(key);
}
});
vm0.invoke(new CacheSerializableRunnable("Unlock entry") {
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(name);
Lock lock = region.getDistributedLock(key);
lock.unlock();
}
});
}
use of org.apache.geode.cache.LoaderHelper in project geode by apache.
the class LRUEvictionControllerDUnitTest method testCacheLoader.
/**
* Carefully verifies that region operations effect the {@link LRUStatistics} as expected in the
* presense of a {@link CacheLoader}.
*/
@Test
public void testCacheLoader() throws CacheException {
int threshold = 10;
final String name = this.getUniqueName();
AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.LOCAL);
factory.setEvictionAttributes(EvictionAttributes.createLRUEntryAttributes(threshold));
factory.setCacheLoader(new CacheLoader() {
public Object load(LoaderHelper helper) throws CacheLoaderException {
return "LOADED VALUE";
}
public void close() {
}
});
Region region;
if (usingMain) {
DistributedSystem system = DistributedSystem.connect(new Properties());
Cache cache = CacheFactory.create(system);
region = cache.createRegion("Test", factory.create());
} else {
region = createRegion(name, factory.create());
}
LRUStatistics lruStats = getLRUStats(region);
assertNotNull(lruStats);
for (int i = 1; i <= 10; i++) {
Object key = new Integer(i);
Object value = String.valueOf(i);
region.put(key, value);
assertEquals(i, lruStats.getCounter());
assertEquals(0, lruStats.getEvictions());
}
for (int i = 11; i <= 20; i++) {
Object key = new Integer(i);
// Object value = String.valueOf(i);
// Invoke loader
region.get(key);
assertEquals(10, lruStats.getCounter());
assertEquals(i - 10, lruStats.getEvictions());
}
}
use of org.apache.geode.cache.LoaderHelper in project geode by apache.
the class LocalRegionDUnitTest method testLocalLoaderNetSearch.
/**
* Tests that if a <code>CacheLoader</code> for a local region invokes
* {@link LoaderHelper#netSearch}, a {@link CacheLoaderException} is thrown.
*/
@Test
public void testLocalLoaderNetSearch() throws CacheException {
assertEquals(Scope.LOCAL, getRegionAttributes().getScope());
final String name = this.getUniqueName();
final Object key = this.getUniqueName();
TestCacheLoader loader = new TestCacheLoader() {
public Object load2(LoaderHelper helper) throws CacheLoaderException {
try {
helper.netSearch(true);
} catch (TimeoutException ex) {
Assert.fail("Why did I timeout?", ex);
}
return null;
}
};
AttributesFactory factory = new AttributesFactory(getRegionAttributes());
factory.setCacheLoader(loader);
Region region = createRegion(name, factory.create());
assertEquals(Scope.LOCAL, region.getAttributes().getScope());
try {
region.get(key);
fail("Should have thrown a CacheLoaderException");
} catch (CacheLoaderException ex) {
String expected = org.apache.geode.internal.cache.LoaderHelperImpl.NET_SEARCH_LOCAL.toLocalizedString();
String message = ex.getMessage();
assertTrue("Unexpected message \"" + message + "\"", message.indexOf(expected) != -1);
}
}
Aggregations