use of com.github.benmanes.caffeine.cache.testing.CacheSpec in project caffeine by ben-manes.
the class BoundedLocalCacheTest method updateRecency_onReplaceConditionally.
@Test(dataProvider = "caches")
@CacheSpec(compute = Compute.SYNC, implementation = Implementation.Caffeine, population = Population.FULL, maximumSize = Maximum.FULL)
public void updateRecency_onReplaceConditionally(Cache<Integer, Integer> cache, CacheContext context) {
BoundedLocalCache<Integer, Integer> localCache = asBoundedLocalCache(cache);
Node<Integer, Integer> first = firstBeforeAccess(localCache, context);
Integer value = first.getValue();
updateRecency(localCache, context, () -> localCache.replace(first.getKey(), value, value));
}
use of com.github.benmanes.caffeine.cache.testing.CacheSpec in project caffeine by ben-manes.
the class BoundedLocalCacheTest method drain_nonblocking.
@Test(dataProvider = "caches")
@CacheSpec(compute = Compute.SYNC, implementation = Implementation.Caffeine, population = Population.EMPTY, maximumSize = Maximum.FULL)
public void drain_nonblocking(Cache<Integer, Integer> cache, CacheContext context) {
BoundedLocalCache<Integer, Integer> localCache = asBoundedLocalCache(cache);
AtomicBoolean done = new AtomicBoolean();
Runnable task = () -> {
localCache.lazySetDrainStatus(REQUIRED);
localCache.scheduleDrainBuffers();
done.set(true);
};
localCache.evictionLock.lock();
try {
ConcurrentTestHarness.execute(task);
Awaits.await().untilTrue(done);
} finally {
localCache.evictionLock.unlock();
}
}
use of com.github.benmanes.caffeine.cache.testing.CacheSpec in project caffeine by ben-manes.
the class CacheTest method stats.
/* ---------------- stats -------------- */
@CacheSpec
@CheckNoWriter
@CheckNoStats
@Test(dataProvider = "caches")
public void stats(Cache<Integer, Integer> cache, CacheContext context) {
CacheStats stats = cache.stats().plus(new CacheStats(1, 2, 3, 4, 5, 6, 7).minus(new CacheStats(6, 5, 4, 3, 2, 1, 0)));
assertThat(stats, is(new CacheStats(0, 0, 0, 1, 3, 5, 7)));
assertThat(cache.policy().isRecordingStats(), is(context.isRecordingStats()));
}
use of com.github.benmanes.caffeine.cache.testing.CacheSpec in project caffeine by ben-manes.
the class EvictionTest method evict_zero_async.
@Test(dataProvider = "caches")
@CacheSpec(implementation = Implementation.Caffeine, maximumSize = Maximum.ZERO, weigher = CacheWeigher.COLLECTION, population = Population.EMPTY, keys = ReferenceType.STRONG, values = ReferenceType.STRONG)
@SuppressWarnings("FutureReturnValueIgnored")
public void evict_zero_async(AsyncLoadingCache<Integer, List<Integer>> cache, CacheContext context, Eviction<?, ?> eviction) {
AtomicBoolean ready = new AtomicBoolean();
AtomicBoolean done = new AtomicBoolean();
CompletableFuture<List<Integer>> valueFuture = CompletableFuture.supplyAsync(() -> {
Awaits.await().untilTrue(ready);
return ImmutableList.of(1, 2, 3, 4, 5);
});
valueFuture.whenComplete((r, e) -> done.set(true));
cache.put(context.absentKey(), valueFuture);
assertThat(eviction.weightedSize().getAsLong(), is(0L));
assertThat(cache.synchronous().estimatedSize(), is(1L));
ready.set(true);
Awaits.await().untilTrue(done);
Awaits.await().until(() -> eviction.weightedSize().getAsLong(), is(0L));
Awaits.await().until(() -> cache.synchronous().estimatedSize(), is(0L));
assertThat(context, hasRemovalNotifications(context, 1, RemovalCause.SIZE));
verifyWriter(context, (verifier, writer) -> verifier.deletions(1, RemovalCause.SIZE));
}
use of com.github.benmanes.caffeine.cache.testing.CacheSpec in project caffeine by ben-manes.
the class EvictionTest method evict_weighted_async.
@Test(dataProvider = "caches")
@CacheSpec(implementation = Implementation.Caffeine, maximumSize = Maximum.TEN, weigher = CacheWeigher.VALUE, population = Population.EMPTY, keys = ReferenceType.STRONG, values = ReferenceType.STRONG, removalListener = Listener.CONSUMING)
@SuppressWarnings("FutureReturnValueIgnored")
public void evict_weighted_async(AsyncLoadingCache<Integer, Integer> cache, CacheContext context, Eviction<?, ?> eviction) {
AtomicBoolean ready = new AtomicBoolean();
AtomicBoolean done = new AtomicBoolean();
CompletableFuture<Integer> valueFuture = CompletableFuture.supplyAsync(() -> {
Awaits.await().untilTrue(ready);
return 6;
});
valueFuture.whenComplete((r, e) -> done.set(true));
cache.put(5, CompletableFuture.completedFuture(5));
cache.put(4, CompletableFuture.completedFuture(4));
cache.put(6, valueFuture);
assertThat(eviction.weightedSize().getAsLong(), is(9L));
assertThat(cache.synchronous().estimatedSize(), is(3L));
ready.set(true);
Awaits.await().untilTrue(done);
Awaits.await().until(context::consumedNotifications, hasSize(1));
Awaits.await().until(() -> cache.synchronous().estimatedSize(), is(2L));
Awaits.await().until(() -> eviction.weightedSize().getAsLong(), is(10L));
assertThat(context, hasEvictionWeight(5L));
assertThat(context, hasRemovalNotifications(context, 1, RemovalCause.SIZE));
verifyWriter(context, (verifier, writer) -> verifier.deletions(1, RemovalCause.SIZE));
}
Aggregations