use of org.cache2k.test.util.Condition in project cache2k by cache2k.
the class CacheLoaderTest method prefetch.
@Test
public void prefetch() {
final Cache<Integer, Integer> c = cacheWithLoader();
c.prefetch(1);
assertTrue(isLoadStarted(c));
ConcurrencyHelper.await(new Condition() {
@Override
public boolean check() throws Exception {
return c.containsKey(1);
}
});
}
use of org.cache2k.test.util.Condition in project cache2k by cache2k.
the class CacheLoaderTest method prefetchAll.
@Test
public void prefetchAll() {
final Cache<Integer, Integer> c = cacheWithLoader();
c.prefetchAll(toIterable(1, 2, 3), null);
assertTrue(isLoadStarted(c));
ConcurrencyHelper.await(new Condition() {
@Override
public boolean check() throws Exception {
return c.containsKey(1);
}
});
}
use of org.cache2k.test.util.Condition in project cache2k by cache2k.
the class ListenerTest method manyAsyncUpdateListenerCalled.
/**
* Check that we do not miss events.
*/
@Test(timeout = TestingParameters.MAX_FINISH_WAIT_MILLIS)
public void manyAsyncUpdateListenerCalled() {
final AtomicInteger _callCount = new AtomicInteger();
final ConcurrentMap<Integer, Integer> _seenValues = new ConcurrentHashMap<Integer, Integer>();
Cache<Integer, Integer> c = target.cache(new CacheRule.Specialization<Integer, Integer>() {
@Override
public void extend(final Cache2kBuilder<Integer, Integer> b) {
b.addAsyncListener(new CacheEntryUpdatedListener<Integer, Integer>() {
@Override
public void onEntryUpdated(final Cache<Integer, Integer> cache, final CacheEntry<Integer, Integer> currentEntry, final CacheEntry<Integer, Integer> entryWithNewData) {
_seenValues.put(entryWithNewData.getValue(), entryWithNewData.getValue());
_callCount.incrementAndGet();
}
});
}
});
c.put(1, 2);
assertEquals(0, _callCount.get());
final int _UPDATE_COUNT = 123;
for (int i = 0; i < _UPDATE_COUNT; i++) {
c.put(1, i);
}
ConcurrencyHelper.await(new Condition() {
@Override
public boolean check() throws Exception {
return _callCount.get() == _UPDATE_COUNT;
}
});
assertEquals("Event dispatching is using copied events", 123, _seenValues.size());
}
use of org.cache2k.test.util.Condition in project cache2k by cache2k.
the class ListenerTest method asyncUpdateListenerCalled.
/**
* If the listener is not executed in separate thread, this would block
*/
@Test(timeout = TestingParameters.MAX_FINISH_WAIT_MILLIS)
public void asyncUpdateListenerCalled() {
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 CacheEntryUpdatedListener<Integer, Integer>() {
@Override
public void onEntryUpdated(final Cache<Integer, Integer> cache, final CacheEntry<Integer, Integer> currentEntry, final CacheEntry<Integer, Integer> entryWithNewData) {
try {
_fire.await();
} catch (InterruptedException ignore) {
}
_callCount.incrementAndGet();
}
});
}
});
c.put(1, 2);
assertEquals(0, _callCount.get());
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.test.util.Condition 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());
}
Aggregations