Search in sources :

Example 16 with CacheSpec

use of com.github.benmanes.caffeine.cache.testing.CacheSpec in project caffeine by ben-manes.

the class EvictionTest method put_asyncWeight.

@Test(dataProvider = "caches")
@CacheSpec(implementation = Implementation.Caffeine, maximumSize = Maximum.FULL, weigher = CacheWeigher.COLLECTION, population = Population.EMPTY, keys = ReferenceType.STRONG, values = ReferenceType.STRONG)
@SuppressWarnings("FutureReturnValueIgnored")
public void put_asyncWeight(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(5L));
    Awaits.await().until(() -> cache.synchronous().estimatedSize(), is(1L));
}
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)

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