use of org.cache2k.integration.CacheLoaderException in project cache2k by cache2k.
the class EntryProcessorTest method setException_policy_called.
@Test
public void setException_policy_called() {
final String _TEXT = "set inside process";
final AtomicLong _retryLoadAfter = new AtomicLong();
final ResiliencePolicy<Integer, Integer> _policy = new ResiliencePolicy<Integer, Integer>() {
@Override
public long suppressExceptionUntil(final Integer key, final ExceptionInformation exceptionInformation, final CacheEntry<Integer, Integer> cachedContent) {
return 0;
}
@Override
public long retryLoadAfter(final Integer key, final ExceptionInformation exceptionInformation) {
_retryLoadAfter.incrementAndGet();
return ETERNAL;
}
};
Cache<Integer, Integer> c = target.cache(new CacheRule.Specialization<Integer, Integer>() {
@Override
public void extend(final Cache2kBuilder<Integer, Integer> b) {
b.resiliencePolicy(_policy);
}
});
c.invoke(KEY, new EntryProcessor<Integer, Integer, Object>() {
@Override
public Object process(final MutableCacheEntry<Integer, Integer> e) throws Exception {
e.setException(new IllegalStateException(_TEXT));
return null;
}
});
try {
c.get(KEY);
fail();
} catch (CacheLoaderException ex) {
assertTrue(ex.getCause().toString().contains(_TEXT));
}
assertEquals(1, _retryLoadAfter.get());
}
use of org.cache2k.integration.CacheLoaderException in project cache2k by cache2k.
the class BasicCacheOperationsTest method peekAll_Exception.
@Test
public void peekAll_Exception() {
((Cache) cache).put(KEY, new ExceptionWrapper(OUCH));
Map<Integer, Integer> m = cache.peekAll(toIterable(KEY, OTHER_KEY));
assertEquals(1, m.size());
assertEquals(1, m.values().size());
assertEquals(1, m.keySet().size());
assertEquals(1, m.entrySet().size());
try {
m.get(KEY);
fail("Exception expected");
} catch (CacheLoaderException ex) {
}
Iterator<Integer> it = m.keySet().iterator();
assertTrue(it.hasNext());
assertEquals(KEY, it.next());
assertFalse("one entry", it.hasNext());
it = m.values().iterator();
assertTrue(it.hasNext());
try {
assertEquals(KEY, it.next());
fail("Exception expected");
} catch (CacheLoaderException ex) {
}
Iterator<Map.Entry<Integer, Integer>> ei = m.entrySet().iterator();
assertTrue(ei.hasNext());
Map.Entry<Integer, Integer> e = ei.next();
assertEquals(KEY, e.getKey());
try {
e.getValue();
fail("Exception expected");
} catch (CacheLoaderException ex) {
}
}
use of org.cache2k.integration.CacheLoaderException in project cache2k by cache2k.
the class HeapCache method computeIfAbsent.
/**
* Code duplicates with {@link Cache#get(Object)}
*/
@Override
public V computeIfAbsent(final K key, final Callable<V> callable) {
Entry<K, V> e;
for (; ; ) {
e = lookupOrNewEntry(key);
if (e.hasFreshData(clock)) {
return returnValue(e);
}
synchronized (e) {
e.waitForProcessing();
if (e.hasFreshData(clock)) {
return returnValue(e);
}
if (e.isGone()) {
metrics.goneSpin();
continue;
}
e.startProcessing();
break;
}
}
metrics.peekMiss();
boolean _finished = false;
long t = 0, t0 = 0;
V _value;
try {
if (isNoLastModificationTime()) {
_value = callable.call();
} else {
t0 = clock.millis();
_value = callable.call();
t = clock.millis();
}
synchronized (e) {
insertOrUpdateAndCalculateExpiry(e, _value, t0, t, INSERT_STAT_PUT);
e.processingDone();
}
_finished = true;
} catch (Exception ex) {
throw new CacheLoaderException(ex);
} finally {
e.ensureAbort(_finished);
}
return returnValue(e);
}
use of org.cache2k.integration.CacheLoaderException in project cache2k by cache2k.
the class BasicCacheOperationsTest method peekAndRemove_Exception.
@Test
public void peekAndRemove_Exception() {
((Cache) cache).put(KEY, new ExceptionWrapper(OUCH));
try {
cache.peekAndRemove(KEY);
fail("exception expected");
} catch (CacheLoaderException ex) {
}
}
use of org.cache2k.integration.CacheLoaderException in project cache2k by cache2k.
the class BasicCacheOperationsTest method computeIfAbsent_Exception.
@Test
public void computeIfAbsent_Exception() {
try {
cache.computeIfAbsent(KEY, new Callable<Integer>() {
@Override
public Integer call() throws Exception {
throw new IOException("for testing");
}
});
fail("CacheLoaderException expected");
} catch (CacheLoaderException ex) {
}
statistics().getCount.expect(1).missCount.expect(1).putCount.expect(0).expectAllZero();
assertFalse(cache.containsKey(KEY));
}
Aggregations