use of org.cache2k.test.util.Condition 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.test.util.Condition 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();
}
use of org.cache2k.test.util.Condition 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.test.util.Condition in project cache2k by cache2k.
the class ListenerTest method asyncExpiredListenerCalled.
@Test
public void asyncExpiredListenerCalled() {
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);
}
});
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.test.util.Condition 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;
}
});
}
Aggregations