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