Search in sources :

Example 1 with CacheLoaderException

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());
}
Also used : ResiliencePolicy(org.cache2k.integration.ResiliencePolicy) MutableCacheEntry(org.cache2k.processor.MutableCacheEntry) CacheEntry(org.cache2k.CacheEntry) IntCacheRule(org.cache2k.test.util.IntCacheRule) CacheRule(org.cache2k.test.util.CacheRule) NoSuchElementException(java.util.NoSuchElementException) EntryProcessingException(org.cache2k.processor.EntryProcessingException) CacheLoaderException(org.cache2k.integration.CacheLoaderException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicLong(java.util.concurrent.atomic.AtomicLong) CacheLoaderException(org.cache2k.integration.CacheLoaderException) ExceptionInformation(org.cache2k.integration.ExceptionInformation) Test(org.junit.Test)

Example 2 with CacheLoaderException

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) {
    }
}
Also used : CacheEntry(org.cache2k.CacheEntry) CacheLoaderException(org.cache2k.integration.CacheLoaderException) ExceptionWrapper(org.cache2k.core.ExceptionWrapper) HashMap(java.util.HashMap) Map(java.util.Map) InternalCache(org.cache2k.core.InternalCache) Cache(org.cache2k.Cache)

Example 3 with CacheLoaderException

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);
}
Also used : CacheLoaderException(org.cache2k.integration.CacheLoaderException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) NoSuchElementException(java.util.NoSuchElementException) CacheLoaderException(org.cache2k.integration.CacheLoaderException)

Example 4 with CacheLoaderException

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) {
    }
}
Also used : CacheLoaderException(org.cache2k.integration.CacheLoaderException) ExceptionWrapper(org.cache2k.core.ExceptionWrapper) InternalCache(org.cache2k.core.InternalCache) Cache(org.cache2k.Cache)

Example 5 with CacheLoaderException

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));
}
Also used : CacheLoaderException(org.cache2k.integration.CacheLoaderException) IOException(java.io.IOException) IOException(java.io.IOException) NoSuchElementException(java.util.NoSuchElementException) CacheLoaderException(org.cache2k.integration.CacheLoaderException)

Aggregations

CacheLoaderException (org.cache2k.integration.CacheLoaderException)5 NoSuchElementException (java.util.NoSuchElementException)3 Cache (org.cache2k.Cache)2 CacheEntry (org.cache2k.CacheEntry)2 ExceptionWrapper (org.cache2k.core.ExceptionWrapper)2 InternalCache (org.cache2k.core.InternalCache)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 ExceptionInformation (org.cache2k.integration.ExceptionInformation)1 ResiliencePolicy (org.cache2k.integration.ResiliencePolicy)1 EntryProcessingException (org.cache2k.processor.EntryProcessingException)1 MutableCacheEntry (org.cache2k.processor.MutableCacheEntry)1 CacheRule (org.cache2k.test.util.CacheRule)1 IntCacheRule (org.cache2k.test.util.IntCacheRule)1 Test (org.junit.Test)1