Search in sources :

Example 11 with CacheEntry

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

the class TimingHandlerTest method expireAfterWrite_policy_limit.

/**
 * The configuration setting serves as a time limit.
 */
@Test
public void expireAfterWrite_policy_limit() {
    TimingHandler h = TimingHandler.of(CLOCK, Cache2kBuilder.forUnknownTypes().expiryPolicy(new ExpiryPolicy() {

        @Override
        public long calculateExpiryTime(Object key, Object value, long loadTime, CacheEntry oldEntry) {
            return ETERNAL;
        }
    }).expireAfterWrite(5, TimeUnit.MINUTES).toConfiguration());
    Entry e = new Entry();
    long t = h.calculateNextRefreshTime(e, null, NOW);
    assertNotEquals(Long.MAX_VALUE, t);
    assertEquals(NOW + TimeUnit.MINUTES.toMillis(5), t);
    t = h.calculateNextRefreshTime(e, null, NOW);
    assertEquals(NOW + TimeUnit.MINUTES.toMillis(5), t);
}
Also used : CacheEntry(org.cache2k.CacheEntry) ExpiryPolicy(org.cache2k.expiry.ExpiryPolicy) CacheEntry(org.cache2k.CacheEntry) Test(org.junit.Test)

Example 12 with CacheEntry

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

the class TimingHandlerTest method expireAfterWrite_policy_limit_sharp.

/**
 * Maximum expiry is limited when sharp expiry requested.
 */
@Test
public void expireAfterWrite_policy_limit_sharp() {
    long _DURATION = 1000000;
    final long _SHARP_POINT_IN_TIME = NOW + 5000000;
    TimingHandler h = TimingHandler.of(CLOCK, Cache2kBuilder.forUnknownTypes().expiryPolicy(new ExpiryPolicy() {

        @Override
        public long calculateExpiryTime(Object key, Object value, long loadTime, CacheEntry oldEntry) {
            return -_SHARP_POINT_IN_TIME;
        }
    }).expireAfterWrite(_DURATION, TimeUnit.MILLISECONDS).toConfiguration());
    Entry e = new Entry();
    long t = h.calculateNextRefreshTime(e, null, NOW);
    assertNotEquals(Long.MAX_VALUE, t);
    assertEquals("max expiry, but not sharp", NOW + _DURATION, t);
    long _later = NOW + _DURATION;
    t = h.calculateNextRefreshTime(e, null, _later);
    assertEquals(_later + _DURATION, t);
    _later = _SHARP_POINT_IN_TIME - _DURATION / 2;
    t = h.calculateNextRefreshTime(e, null, _later);
    assertEquals("requested expiry via duration too close", -_SHARP_POINT_IN_TIME, t);
    _later = _SHARP_POINT_IN_TIME - _DURATION - 1;
    t = h.calculateNextRefreshTime(e, null, _later);
    assertTrue(t <= _later + _DURATION);
    assertEquals(_later + 1, t);
}
Also used : CacheEntry(org.cache2k.CacheEntry) ExpiryPolicy(org.cache2k.expiry.ExpiryPolicy) CacheEntry(org.cache2k.CacheEntry) Test(org.junit.Test)

Example 13 with CacheEntry

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

the class ListenerTest method asyncHalfExpiredAfterUpdate.

/**
 * Expire time is load time if entry is modified, yields: Expiry listener is called. Entry
 * stays in the cache, we need to implement the removal
 */
@Test
public void asyncHalfExpiredAfterUpdate() {
    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 loadTime;
                    }
                    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());
    assertEquals(1, latestInfo(c).getExpiredCount());
}
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)

Example 14 with CacheEntry

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

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

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