use of com.github.benmanes.caffeine.cache.testing.CacheSpec.Listener in project caffeine by ben-manes.
the class BoundedLocalCacheTest method clear_update.
@Test(groups = "slow")
public void clear_update() {
Integer key = 0;
Integer oldValue = 1;
Integer newValue = 2;
Thread invalidator = Thread.currentThread();
AtomicBoolean started = new AtomicBoolean();
AtomicBoolean writing = new AtomicBoolean();
AtomicInteger clearedValue = new AtomicInteger();
AtomicInteger removedValues = new AtomicInteger();
AtomicInteger previousValue = new AtomicInteger();
CacheWriter<Integer, Integer> writer = new CacheWriter<Integer, Integer>() {
@Override
public void write(Integer key, Integer value) {
if (started.get()) {
writing.set(true);
await().until(invalidator::getState, is(Thread.State.BLOCKED));
}
}
@Override
public void delete(Integer key, Integer value, RemovalCause cause) {
clearedValue.set(value);
}
};
RemovalListener<Integer, Integer> listener = (k, v, cause) -> removedValues.addAndGet(v);
Cache<Integer, Integer> cache = Caffeine.newBuilder().removalListener(listener).executor(Runnable::run).maximumSize(100).writer(writer).build();
cache.put(key, oldValue);
started.set(true);
ConcurrentTestHarness.execute(() -> previousValue.set(cache.asMap().put(key, newValue)));
await().untilTrue(writing);
cache.asMap().clear();
await().untilAtomic(clearedValue, is(newValue));
await().untilAtomic(previousValue, is(oldValue));
await().untilAtomic(removedValues, is(oldValue + newValue));
}
use of com.github.benmanes.caffeine.cache.testing.CacheSpec.Listener in project caffeine by ben-manes.
the class BoundedLocalCacheTest method evict_update.
@Test(groups = "slow")
public void evict_update() {
Integer key = 0;
Integer oldValue = 1;
Integer newValue = 2;
Thread evictor = Thread.currentThread();
AtomicBoolean started = new AtomicBoolean();
AtomicBoolean writing = new AtomicBoolean();
AtomicInteger evictedValue = new AtomicInteger();
AtomicInteger removedValues = new AtomicInteger();
AtomicInteger previousValue = new AtomicInteger();
CacheWriter<Integer, Integer> writer = new CacheWriter<Integer, Integer>() {
@Override
public void write(Integer key, Integer value) {
if (started.get()) {
writing.set(true);
await().until(evictor::getState, is(Thread.State.BLOCKED));
}
}
@Override
public void delete(Integer key, Integer value, RemovalCause cause) {
evictedValue.set(value);
}
};
RemovalListener<Integer, Integer> listener = (k, v, cause) -> removedValues.addAndGet(v);
Cache<Integer, Integer> cache = Caffeine.newBuilder().removalListener(listener).executor(Runnable::run).maximumSize(100).writer(writer).build();
BoundedLocalCache<Integer, Integer> localCache = asBoundedLocalCache(cache);
cache.put(key, oldValue);
started.set(true);
ConcurrentTestHarness.execute(() -> previousValue.set(localCache.put(key, newValue)));
await().untilTrue(writing);
Node<Integer, Integer> node = localCache.data.values().iterator().next();
localCache.evictEntry(node, RemovalCause.SIZE, 0L);
await().untilAtomic(evictedValue, is(newValue));
await().untilAtomic(previousValue, is(oldValue));
await().untilAtomic(removedValues, is(oldValue + newValue));
}
Aggregations