Search in sources :

Example 16 with Cache

use of org.cache2k.Cache in project cache2k by cache2k.

the class BasicCacheOperationsTest method getEntry_Exception.

@Test
public void getEntry_Exception() {
    ((Cache) cache).put(KEY, new ExceptionWrapper(OUCH));
    CacheEntry<Integer, Integer> e = cache.getEntry(KEY);
    assertEquals(KEY, e.getKey());
    entryHasException(e);
    assertEquals(OUCH, e.getException());
}
Also used : ExceptionWrapper(org.cache2k.core.ExceptionWrapper) InternalCache(org.cache2k.core.InternalCache) Cache(org.cache2k.Cache)

Example 17 with Cache

use of org.cache2k.Cache 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 18 with Cache

use of org.cache2k.Cache in project cache2k by cache2k.

the class BasicCacheOperationsTest method peekAndReplace_Exception.

@Test(expected = CacheLoaderException.class)
public void peekAndReplace_Exception() {
    ((Cache) cache).put(KEY, new ExceptionWrapper(OUCH));
    cache.peekAndReplace(KEY, VALUE);
}
Also used : ExceptionWrapper(org.cache2k.core.ExceptionWrapper) InternalCache(org.cache2k.core.InternalCache) Cache(org.cache2k.Cache)

Example 19 with Cache

use of org.cache2k.Cache in project cache2k by cache2k.

the class ExceptionPropagatorTest method prepCache.

Cache<Integer, Integer> prepCache() {
    Cache c = target.cache();
    c.put(KEY, new ExceptionWrapper(new IllegalArgumentException("Test")));
    assertTrue(c.containsKey(KEY));
    return c;
}
Also used : ExceptionWrapper(org.cache2k.core.ExceptionWrapper) Cache(org.cache2k.Cache)

Example 20 with Cache

use of org.cache2k.Cache in project cache2k by cache2k.

the class ListenerTest method manyAsyncUpdateListenerCalled.

/**
 * Check that we do not miss events.
 */
@Test(timeout = TestingParameters.MAX_FINISH_WAIT_MILLIS)
public void manyAsyncUpdateListenerCalled() {
    final AtomicInteger _callCount = new AtomicInteger();
    final ConcurrentMap<Integer, Integer> _seenValues = new ConcurrentHashMap<Integer, Integer>();
    Cache<Integer, Integer> c = target.cache(new CacheRule.Specialization<Integer, Integer>() {

        @Override
        public void extend(final Cache2kBuilder<Integer, Integer> b) {
            b.addAsyncListener(new CacheEntryUpdatedListener<Integer, Integer>() {

                @Override
                public void onEntryUpdated(final Cache<Integer, Integer> cache, final CacheEntry<Integer, Integer> currentEntry, final CacheEntry<Integer, Integer> entryWithNewData) {
                    _seenValues.put(entryWithNewData.getValue(), entryWithNewData.getValue());
                    _callCount.incrementAndGet();
                }
            });
        }
    });
    c.put(1, 2);
    assertEquals(0, _callCount.get());
    final int _UPDATE_COUNT = 123;
    for (int i = 0; i < _UPDATE_COUNT; i++) {
        c.put(1, i);
    }
    ConcurrencyHelper.await(new Condition() {

        @Override
        public boolean check() throws Exception {
            return _callCount.get() == _UPDATE_COUNT;
        }
    });
    assertEquals("Event dispatching is using copied events", 123, _seenValues.size());
}
Also used : Condition(org.cache2k.test.util.Condition) IntCacheRule(org.cache2k.test.util.IntCacheRule) CacheRule(org.cache2k.test.util.CacheRule) CacheEntry(org.cache2k.CacheEntry) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CacheEntryUpdatedListener(org.cache2k.event.CacheEntryUpdatedListener) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Cache(org.cache2k.Cache) Test(org.junit.Test)

Aggregations

Cache (org.cache2k.Cache)60 Test (org.junit.Test)49 CacheManager (org.cache2k.CacheManager)11 Cache2kBuilder (org.cache2k.Cache2kBuilder)9 CacheEntry (org.cache2k.CacheEntry)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 ExceptionWrapper (org.cache2k.core.ExceptionWrapper)8 InternalCache (org.cache2k.core.InternalCache)8 CacheRule (org.cache2k.test.util.CacheRule)6 Condition (org.cache2k.test.util.Condition)6 IntCacheRule (org.cache2k.test.util.IntCacheRule)5 CountDownLatch (java.util.concurrent.CountDownLatch)3 MBeanInfo (javax.management.MBeanInfo)3 Log (org.cache2k.core.util.Log)3 CacheEntryCreatedListener (org.cache2k.event.CacheEntryCreatedListener)3 CacheEntryUpdatedListener (org.cache2k.event.CacheEntryUpdatedListener)3 ArrayList (java.util.ArrayList)2 CacheLoaderException (org.cache2k.integration.CacheLoaderException)2 Collection (java.util.Collection)1 Date (java.util.Date)1