Search in sources :

Example 1 with CacheSpec

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

the class AsyncLoadingCacheTest method refresh.

/* ---------------- refresh -------------- */
@Test(dataProvider = "caches")
@CacheSpec(implementation = Implementation.Caffeine, population = Population.EMPTY, executor = CacheExecutor.THREADED, compute = Compute.ASYNC, values = ReferenceType.STRONG)
public void refresh(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.SECONDS);
        cache.synchronous().refresh(key);
        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 2 with CacheSpec

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

the class AsyncLoadingCacheTest method getFunc_absent_null_async.

@CheckNoWriter
@Test(dataProvider = "caches")
@CacheSpec(loader = Loader.NULL, executor = CacheExecutor.THREADED, executorFailure = ExecutorFailure.IGNORED)
public void getFunc_absent_null_async(AsyncLoadingCache<Integer, Integer> cache, CacheContext context) {
    Integer key = context.absentKey();
    AtomicBoolean ready = new AtomicBoolean();
    AtomicBoolean done = new AtomicBoolean();
    CompletableFuture<Integer> valueFuture = cache.get(key, k -> {
        Awaits.await().untilTrue(ready);
        return null;
    });
    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.isDone(), is(true));
    assertThat(cache.synchronous().asMap(), not(hasKey(key)));
}
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 3 with CacheSpec

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

the class LoadingCacheTest method refresh_conflict.

@Test(dataProvider = "caches")
@CacheSpec(population = Population.EMPTY, executor = CacheExecutor.THREADED, removalListener = Listener.CONSUMING)
public void refresh_conflict(CacheContext context) {
    AtomicBoolean refresh = new AtomicBoolean();
    Integer key = context.absentKey();
    Integer original = 1;
    Integer updated = 2;
    Integer refreshed = 3;
    LoadingCache<Integer, Integer> cache = context.build(k -> {
        await().untilTrue(refresh);
        return refreshed;
    });
    cache.put(key, original);
    cache.refresh(key);
    assertThat(cache.asMap().put(key, updated), is(original));
    refresh.set(true);
    await().until(() -> context.consumedNotifications().size(), is(2));
    List<Integer> removed = context.consumedNotifications().stream().map(RemovalNotification::getValue).collect(toList());
    assertThat(cache.getIfPresent(key), is(updated));
    assertThat(removed, containsInAnyOrder(original, refreshed));
    assertThat(cache, hasRemovalNotifications(context, 2, RemovalCause.REPLACED));
    assertThat(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 4 with CacheSpec

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

the class LoadingCacheTest method refresh_invalidate.

@Test(dataProvider = "caches")
@CacheSpec(population = Population.EMPTY, executor = CacheExecutor.THREADED, removalListener = Listener.CONSUMING)
public void refresh_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);
    cache.refresh(key);
    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 5 with CacheSpec

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

the class RefreshAfterWriteTest method put.

/* ---------------- put -------------- */
@Test(dataProvider = "caches")
@CacheSpec(population = Population.EMPTY, refreshAfterWrite = Expire.ONE_MINUTE, executor = CacheExecutor.THREADED, removalListener = Listener.CONSUMING)
public void put(CacheContext context) {
    AtomicBoolean refresh = new AtomicBoolean();
    Integer key = context.absentKey();
    Integer original = 1;
    Integer updated = 2;
    Integer refreshed = 3;
    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));
    assertThat(cache.asMap().put(key, updated), is(original));
    refresh.set(true);
    await().until(() -> context.consumedNotifications().size(), is(2));
    List<Integer> removed = context.consumedNotifications().stream().map(RemovalNotification::getValue).collect(toList());
    assertThat(cache.getIfPresent(key), is(updated));
    assertThat(removed, containsInAnyOrder(original, refreshed));
    assertThat(cache, hasRemovalNotifications(context, 2, RemovalCause.REPLACED));
    assertThat(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)

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