Search in sources :

Example 1 with ExpiryPolicy

use of org.cache2k.expiry.ExpiryPolicy in project cache2k by cache2k.

the class TimingHandlerTest method expireAfterWrite_policy_limit_nonSharp.

@Test
public void expireAfterWrite_policy_limit_nonSharp() {
    long _DURATION = 1000000;
    final long _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 _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 = _POINT_IN_TIME - _DURATION / 2;
    t = h.calculateNextRefreshTime(e, null, _later);
    assertEquals("requested expiry via duration too close", _POINT_IN_TIME, t);
    _later = _POINT_IN_TIME - _DURATION - 1;
    t = h.calculateNextRefreshTime(e, null, _later);
    assertTrue(t <= _later + _DURATION);
    assertEquals(_later + _DURATION, t);
}
Also used : CacheEntry(org.cache2k.CacheEntry) ExpiryPolicy(org.cache2k.expiry.ExpiryPolicy) CacheEntry(org.cache2k.CacheEntry) Test(org.junit.Test)

Example 2 with ExpiryPolicy

use of org.cache2k.expiry.ExpiryPolicy in project cache2k by cache2k.

the class TimingHandlerTest method policy_sharp.

/**
 * Sharp is honored in the calculation phase.
 */
@Test
public void policy_sharp() {
    TimingHandler h = TimingHandler.of(CLOCK, Cache2kBuilder.forUnknownTypes().expiryPolicy(new ExpiryPolicy() {

        @Override
        public long calculateExpiryTime(Object key, Object value, long loadTime, CacheEntry oldEntry) {
            return loadTime + 1;
        }
    }).sharpExpiry(true).toConfiguration());
    Entry e = new Entry();
    long t = h.calculateNextRefreshTime(e, null, NOW);
    assertEquals(-NOW - 1, t);
}
Also used : CacheEntry(org.cache2k.CacheEntry) ExpiryPolicy(org.cache2k.expiry.ExpiryPolicy) CacheEntry(org.cache2k.CacheEntry) Test(org.junit.Test)

Example 3 with ExpiryPolicy

use of org.cache2k.expiry.ExpiryPolicy in project cache2k by cache2k.

the class TimingHandlerTest method expireAfterWrite_policy_limit_sharp_close.

/**
 * Maximum expiry is limited when sharp expiry requested.
 * Corner case if expiry will happen close to requested point in time.
 */
@Test
public void expireAfterWrite_policy_limit_sharp_close() {
    long _DURATION = 100;
    final long _SHARP_POINT_IN_TIME = NOW + 5000;
    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 _later = _SHARP_POINT_IN_TIME - _DURATION - 1;
    long t = h.calculateNextRefreshTime(e, null, _later);
    assertTrue("expect gap bigger then duration", HeapCache.TUNABLE.sharpExpirySafetyGapMillis > _DURATION);
    assertNotEquals(_SHARP_POINT_IN_TIME - HeapCache.TUNABLE.sharpExpirySafetyGapMillis, t);
    assertTrue(Math.abs(t) > NOW);
    assertTrue(Math.abs(t) < _SHARP_POINT_IN_TIME);
}
Also used : CacheEntry(org.cache2k.CacheEntry) ExpiryPolicy(org.cache2k.expiry.ExpiryPolicy) CacheEntry(org.cache2k.CacheEntry) Test(org.junit.Test)

Example 4 with ExpiryPolicy

use of org.cache2k.expiry.ExpiryPolicy 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 5 with ExpiryPolicy

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

Aggregations

CacheEntry (org.cache2k.CacheEntry)7 ExpiryPolicy (org.cache2k.expiry.ExpiryPolicy)7 Test (org.junit.Test)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 CacheRule (org.cache2k.test.util.CacheRule)2 Condition (org.cache2k.test.util.Condition)2 IntCacheRule (org.cache2k.test.util.IntCacheRule)2