Search in sources :

Example 16 with CacheEntry

use of org.cache2k.CacheEntry 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 17 with CacheEntry

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

use of org.cache2k.CacheEntry 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

CacheEntry (org.cache2k.CacheEntry)18 Test (org.junit.Test)16 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)11 CacheRule (org.cache2k.test.util.CacheRule)9 Cache (org.cache2k.Cache)8 Condition (org.cache2k.test.util.Condition)8 IntCacheRule (org.cache2k.test.util.IntCacheRule)8 ExpiryPolicy (org.cache2k.expiry.ExpiryPolicy)7 CacheEntryCreatedListener (org.cache2k.event.CacheEntryCreatedListener)4 CacheEntryUpdatedListener (org.cache2k.event.CacheEntryUpdatedListener)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 CacheEntryExpiredListener (org.cache2k.event.CacheEntryExpiredListener)2 CacheEntryRemovedListener (org.cache2k.event.CacheEntryRemovedListener)2 IOException (java.io.IOException)1 NoSuchElementException (java.util.NoSuchElementException)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 CacheException (javax.cache.CacheException)1 CreatedExpiryPolicy (javax.cache.expiry.CreatedExpiryPolicy)1 Duration (javax.cache.expiry.Duration)1