Search in sources :

Example 51 with Cache

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

the class ListenerTest method asyncExpiredListenerCalledSharpExpiry.

@Test
public void asyncExpiredListenerCalledSharpExpiry() {
    final AtomicInteger _callCount = new AtomicInteger();
    final long _EXPIRY_MILLIS = TestingParameters.MINIMAL_TICK_MILLIS;
    final Cache<Integer, Integer> c = target.cache(new CacheRule.Specialization<Integer, Integer>() {

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

                @Override
                public void onEntryExpired(final Cache<Integer, Integer> c, final CacheEntry<Integer, Integer> e) {
                    _callCount.incrementAndGet();
                }
            }).expireAfterWrite(_EXPIRY_MILLIS, TimeUnit.MILLISECONDS).sharpExpiry(true);
        }
    });
    final int ANY_KEY = 1;
    TimeBox.millis(_EXPIRY_MILLIS).work(new Runnable() {

        @Override
        public void run() {
            c.put(ANY_KEY, 4711);
        }
    }).check(new Runnable() {

        @Override
        public void run() {
            assertEquals(0, _callCount.get());
            assertTrue(c.containsKey(ANY_KEY));
        }
    });
    ConcurrencyHelper.await(new Condition() {

        @Override
        public boolean check() throws Exception {
            return _callCount.get() == 1;
        }
    });
}
Also used : CacheEntryExpiredListener(org.cache2k.event.CacheEntryExpiredListener) 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) Cache(org.cache2k.Cache) Test(org.junit.Test)

Example 52 with Cache

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

the class ListenerTest method asyncUpdateListenerException.

@Test
public void asyncUpdateListenerException() {
    String _logName = getClass().getName() + ".asyncUpdateListenerException";
    final Log.SuppressionCounter _suppressionCounter = new Log.SuppressionCounter();
    Log.registerSuppression("org.cache2k.Cache/default:" + _logName, _suppressionCounter);
    Cache<Integer, Integer> c = Cache2kBuilder.of(Integer.class, Integer.class).name(_logName).eternal(true).addAsyncListener(new CacheEntryUpdatedListener<Integer, Integer>() {

        @Override
        public void onEntryUpdated(final Cache<Integer, Integer> cache, final CacheEntry<Integer, Integer> currentEntry, final CacheEntry<Integer, Integer> entryWithNewData) {
            throw new RuntimeException("ouch");
        }
    }).build();
    c.put(1, 2);
    c.put(1, 2);
    ConcurrencyHelper.await(new Condition() {

        @Override
        public boolean check() throws Exception {
            return _suppressionCounter.getWarnCount() == 1;
        }
    });
    c.close();
}
Also used : Condition(org.cache2k.test.util.Condition) Log(org.cache2k.core.util.Log) CacheEntry(org.cache2k.CacheEntry) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CacheEntryUpdatedListener(org.cache2k.event.CacheEntryUpdatedListener) Cache(org.cache2k.Cache) Test(org.junit.Test)

Example 53 with Cache

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

the class ListenerTest method asyncCreatedListenerCalled.

/**
 * If the listener is not executed in separate thread, this would block
 */
@Test(timeout = TestingParameters.MAX_FINISH_WAIT_MILLIS)
public void asyncCreatedListenerCalled() {
    final AtomicInteger _callCount = new AtomicInteger();
    final CountDownLatch _fire = new CountDownLatch(1);
    Cache<Integer, Integer> c = target.cache(new CacheRule.Specialization<Integer, Integer>() {

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

                @Override
                public void onEntryCreated(final Cache<Integer, Integer> c, final CacheEntry<Integer, Integer> e) {
                    try {
                        _fire.await();
                    } catch (InterruptedException ignore) {
                    }
                    _callCount.incrementAndGet();
                }
            });
        }
    });
    c.put(1, 2);
    assertEquals(0, _callCount.get());
    _fire.countDown();
    ConcurrencyHelper.await(new Condition() {

        @Override
        public boolean check() throws Exception {
            return _callCount.get() == 1;
        }
    });
}
Also used : Condition(org.cache2k.test.util.Condition) CountDownLatch(java.util.concurrent.CountDownLatch) 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) CacheEntryCreatedListener(org.cache2k.event.CacheEntryCreatedListener) Cache(org.cache2k.Cache) Test(org.junit.Test)

Example 54 with Cache

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

the class ListenerTest method asyncRemovedListenerCalled.

/**
 * If the listener is not executed in separate thread, this would block
 */
@Test(timeout = TestingParameters.MAX_FINISH_WAIT_MILLIS)
public void asyncRemovedListenerCalled() {
    final AtomicInteger _callCount = new AtomicInteger();
    final CountDownLatch _fire = new CountDownLatch(1);
    Cache<Integer, Integer> c = target.cache(new CacheRule.Specialization<Integer, Integer>() {

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

                @Override
                public void onEntryRemoved(final Cache<Integer, Integer> c, final CacheEntry<Integer, Integer> e) {
                    try {
                        _fire.await();
                    } catch (InterruptedException ignore) {
                    }
                    _callCount.incrementAndGet();
                }
            });
        }
    });
    c.put(1, 2);
    assertEquals(0, _callCount.get());
    c.put(1, 2);
    assertEquals(0, _callCount.get());
    c.remove(1);
    assertEquals(0, _callCount.get());
    _fire.countDown();
    ConcurrencyHelper.await(new Condition() {

        @Override
        public boolean check() throws Exception {
            return _callCount.get() == 1;
        }
    });
}
Also used : Condition(org.cache2k.test.util.Condition) CountDownLatch(java.util.concurrent.CountDownLatch) IntCacheRule(org.cache2k.test.util.IntCacheRule) CacheRule(org.cache2k.test.util.CacheRule) CacheEntry(org.cache2k.CacheEntry) CacheEntryRemovedListener(org.cache2k.event.CacheEntryRemovedListener) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Cache(org.cache2k.Cache) Test(org.junit.Test)

Example 55 with Cache

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

the class IntegrationTest method duplicateListener.

@Test(expected = ConfigurationException.class)
public void duplicateListener() {
    Cache c = new Cache2kBuilder<String, String>() {
    }.manager(CacheManager.getInstance("specialCases")).name("duplicateListener").build();
    c.close();
}
Also used : 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