Search in sources :

Example 1 with CheckNoWriter

use of com.github.benmanes.caffeine.cache.testing.CheckNoWriter 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 2 with CheckNoWriter

use of com.github.benmanes.caffeine.cache.testing.CheckNoWriter 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 3 with CheckNoWriter

use of com.github.benmanes.caffeine.cache.testing.CheckNoWriter 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 4 with CheckNoWriter

use of com.github.benmanes.caffeine.cache.testing.CheckNoWriter 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)

Example 5 with CheckNoWriter

use of com.github.benmanes.caffeine.cache.testing.CheckNoWriter 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)

Aggregations

CacheSpec (com.github.benmanes.caffeine.cache.testing.CacheSpec)5 CheckNoWriter (com.github.benmanes.caffeine.cache.testing.CheckNoWriter)5 Test (org.testng.annotations.Test)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 CacheStats (com.github.benmanes.caffeine.cache.stats.CacheStats)1 CheckNoStats (com.github.benmanes.caffeine.cache.testing.CheckNoStats)1