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