Search in sources :

Example 11 with CacheSpec

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));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.testng.annotations.Test) CacheSpec(com.github.benmanes.caffeine.cache.testing.CacheSpec)

Example 12 with CacheSpec

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();
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.testng.annotations.Test) CacheSpec(com.github.benmanes.caffeine.cache.testing.CacheSpec)

Example 13 with CacheSpec

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()));
}
Also used : CacheStats(com.github.benmanes.caffeine.cache.stats.CacheStats) CheckNoWriter(com.github.benmanes.caffeine.cache.testing.CheckNoWriter) Test(org.testng.annotations.Test) CheckNoStats(com.github.benmanes.caffeine.cache.testing.CheckNoStats) CacheSpec(com.github.benmanes.caffeine.cache.testing.CacheSpec)

Example 14 with CacheSpec

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));
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Arrays.asList(java.util.Arrays.asList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Test(org.testng.annotations.Test) CacheSpec(com.github.benmanes.caffeine.cache.testing.CacheSpec)

Example 15 with CacheSpec

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));
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.testng.annotations.Test) CacheSpec(com.github.benmanes.caffeine.cache.testing.CacheSpec)

Aggregations

CacheSpec (com.github.benmanes.caffeine.cache.testing.CacheSpec)16 Test (org.testng.annotations.Test)16 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)14 CheckNoWriter (com.github.benmanes.caffeine.cache.testing.CheckNoWriter)5 ImmutableList (com.google.common.collect.ImmutableList)2 Arrays.asList (java.util.Arrays.asList)2 List (java.util.List)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 CacheStats (com.github.benmanes.caffeine.cache.stats.CacheStats)1 CheckNoStats (com.github.benmanes.caffeine.cache.testing.CheckNoStats)1