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);
}
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);
}
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);
}
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);
}
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);
}
Aggregations