Search in sources :

Example 6 with CacheSpec

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

the class RefreshAfterWriteTest method invalidate.

/* ---------------- invalidate -------------- */
@Test(dataProvider = "caches")
@CacheSpec(population = Population.EMPTY, refreshAfterWrite = Expire.ONE_MINUTE, executor = CacheExecutor.THREADED, removalListener = Listener.CONSUMING)
public void invalidate(CacheContext context) {
    AtomicBoolean refresh = new AtomicBoolean();
    Integer key = context.absentKey();
    Integer original = 1;
    Integer refreshed = 2;
    LoadingCache<Integer, Integer> cache = context.build(k -> {
        await().untilTrue(refresh);
        return refreshed;
    });
    cache.put(key, original);
    context.ticker().advance(2, TimeUnit.MINUTES);
    assertThat(cache.getIfPresent(key), is(original));
    cache.invalidate(key);
    refresh.set(true);
    await().until(() -> cache.getIfPresent(key), is(refreshed));
    await().until(() -> cache, hasRemovalNotifications(context, 1, RemovalCause.EXPLICIT));
    await().until(() -> context, both(hasLoadSuccessCount(1)).and(hasLoadFailureCount(0)));
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.testng.annotations.Test) CacheSpec(com.github.benmanes.caffeine.cache.testing.CacheSpec)

Example 7 with CacheSpec

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

the class RefreshAfterWriteTest method get_sameFuture.

@Test(dataProvider = "caches")
@CacheSpec(implementation = Implementation.Caffeine, population = Population.EMPTY, refreshAfterWrite = Expire.ONE_MINUTE, executor = CacheExecutor.THREADED, compute = Compute.ASYNC, values = ReferenceType.STRONG)
public void get_sameFuture(CacheContext context) {
    AtomicBoolean done = new AtomicBoolean();
    AsyncLoadingCache<Integer, Integer> cache = context.buildAsync(key -> {
        await().untilTrue(done);
        return -key;
    });
    Integer key = 1;
    cache.synchronous().put(key, key);
    CompletableFuture<Integer> original = cache.get(key);
    for (int i = 0; i < 10; i++) {
        context.ticker().advance(1, TimeUnit.MINUTES);
        CompletableFuture<Integer> next = cache.get(key);
        assertThat(next, is(sameInstance(original)));
    }
    done.set(true);
    await().until(() -> cache.synchronous().getIfPresent(key), is(-key));
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.testng.annotations.Test) CacheSpec(com.github.benmanes.caffeine.cache.testing.CacheSpec)

Example 8 with CacheSpec

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

the class AsyncLoadingCacheTest method getFunc_absent_cancelled.

@CheckNoWriter
@Test(dataProvider = "caches")
@CacheSpec(loader = Loader.NULL, executor = CacheExecutor.THREADED)
public void getFunc_absent_cancelled(AsyncLoadingCache<Integer, Integer> cache, CacheContext context) {
    AtomicBoolean done = new AtomicBoolean();
    CompletableFuture<Integer> valueFuture = cache.get(context.absentKey(), k -> {
        Awaits.await().until(done::get);
        return null;
    });
    valueFuture.whenComplete((r, e) -> done.set(true));
    valueFuture.cancel(true);
    Awaits.await().untilTrue(done);
    await().until(() -> context, both(hasMissCount(1)).and(hasHitCount(0)));
    await().until(() -> context, both(hasLoadSuccessCount(0)).and(hasLoadFailureCount(1)));
    assertThat(valueFuture.isDone(), is(true));
    assertThat(cache.getIfPresent(context.absentKey()), is(nullValue()));
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CheckNoWriter(com.github.benmanes.caffeine.cache.testing.CheckNoWriter) Test(org.testng.annotations.Test) CacheSpec(com.github.benmanes.caffeine.cache.testing.CacheSpec)

Example 9 with CacheSpec

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

the class AsyncLoadingCacheTest method get_absent_failure_async.

@CheckNoWriter
@Test(dataProvider = "caches")
@CacheSpec(loader = Loader.EXCEPTIONAL, executor = CacheExecutor.THREADED, executorFailure = ExecutorFailure.IGNORED)
public void get_absent_failure_async(AsyncLoadingCache<Integer, Integer> cache, CacheContext context) throws InterruptedException {
    AtomicBoolean done = new AtomicBoolean();
    Integer key = context.absentKey();
    CompletableFuture<Integer> valueFuture = cache.get(key);
    valueFuture.whenComplete((r, e) -> done.set(true));
    Awaits.await().untilTrue(done);
    Awaits.await().until(() -> !cache.synchronous().asMap().containsKey(context.absentKey()));
    Awaits.await().until(() -> context, both(hasMissCount(1)).and(hasHitCount(0)));
    Awaits.await().until(() -> context, both(hasLoadSuccessCount(0)).and(hasLoadFailureCount(1)));
    assertThat(valueFuture.isCompletedExceptionally(), is(true));
    assertThat(cache.getIfPresent(key), is(nullValue()));
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CheckNoWriter(com.github.benmanes.caffeine.cache.testing.CheckNoWriter) Test(org.testng.annotations.Test) CacheSpec(com.github.benmanes.caffeine.cache.testing.CacheSpec)

Example 10 with CacheSpec

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

the class AsyncLoadingCacheTest method getFunc_absent_failure_async.

@CheckNoWriter
@Test(dataProvider = "caches")
@CacheSpec(executor = CacheExecutor.THREADED, executorFailure = ExecutorFailure.IGNORED)
public void getFunc_absent_failure_async(AsyncLoadingCache<Integer, Integer> cache, CacheContext context) {
    AtomicBoolean ready = new AtomicBoolean();
    AtomicBoolean done = new AtomicBoolean();
    CompletableFuture<Integer> valueFuture = cache.get(context.absentKey(), k -> {
        Awaits.await().untilTrue(ready);
        throw new IllegalStateException();
    });
    valueFuture.whenComplete((r, e) -> done.set(true));
    ready.set(true);
    Awaits.await().untilTrue(done);
    Awaits.await().until(() -> !cache.synchronous().asMap().containsKey(context.absentKey()));
    Awaits.await().until(() -> context, both(hasMissCount(1)).and(hasHitCount(0)));
    Awaits.await().until(() -> context, both(hasLoadSuccessCount(0)).and(hasLoadFailureCount(1)));
    assertThat(valueFuture.isCompletedExceptionally(), is(true));
    assertThat(cache.getIfPresent(context.absentKey()), is(nullValue()));
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CheckNoWriter(com.github.benmanes.caffeine.cache.testing.CheckNoWriter) 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