Search in sources :

Example 11 with Condition

use of org.cache2k.test.util.Condition in project cache2k by cache2k.

the class ListenerTest method asyncReallyExpiredAfterUpdate.

/**
 * Expire time is 0 if entry is modified, yields: Expiry listener is called and entry
 * is removed from cache.
 */
@Test
public void asyncReallyExpiredAfterUpdate() {
    final AtomicInteger _expireCallCount = new AtomicInteger();
    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) {
                    _expireCallCount.incrementAndGet();
                }
            }).eternal(true).keepDataAfterExpired(false).expiryPolicy(new ExpiryPolicy<Integer, Integer>() {

                @Override
                public long calculateExpiryTime(final Integer key, final Integer value, final long loadTime, final CacheEntry<Integer, Integer> oldEntry) {
                    if (oldEntry != null) {
                        return 0;
                    }
                    return ETERNAL;
                }
            });
        }
    });
    c.put(1, 1);
    c.put(1, 2);
    ConcurrencyHelper.await(new Condition() {

        @Override
        public boolean check() throws Exception {
            return _expireCallCount.get() == 1;
        }
    });
    assertEquals(0, latestInfo(c).getSize());
}
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) ExpiryPolicy(org.cache2k.expiry.ExpiryPolicy) Test(org.junit.Test)

Aggregations

AtomicInteger (java.util.concurrent.atomic.AtomicInteger)11 Condition (org.cache2k.test.util.Condition)11 Test (org.junit.Test)11 CacheEntry (org.cache2k.CacheEntry)8 CacheRule (org.cache2k.test.util.CacheRule)8 IntCacheRule (org.cache2k.test.util.IntCacheRule)8 Cache (org.cache2k.Cache)6 CountDownLatch (java.util.concurrent.CountDownLatch)3 CacheEntryUpdatedListener (org.cache2k.event.CacheEntryUpdatedListener)3 ExpiryPolicy (org.cache2k.expiry.ExpiryPolicy)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Log (org.cache2k.core.util.Log)1 CacheEntryCreatedListener (org.cache2k.event.CacheEntryCreatedListener)1 CacheEntryExpiredListener (org.cache2k.event.CacheEntryExpiredListener)1 CacheEntryRemovedListener (org.cache2k.event.CacheEntryRemovedListener)1