Search in sources :

Example 1 with Listener

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

the class BoundedLocalCacheTest method clear_update.

@Test(groups = "slow")
public void clear_update() {
    Integer key = 0;
    Integer oldValue = 1;
    Integer newValue = 2;
    Thread invalidator = Thread.currentThread();
    AtomicBoolean started = new AtomicBoolean();
    AtomicBoolean writing = new AtomicBoolean();
    AtomicInteger clearedValue = new AtomicInteger();
    AtomicInteger removedValues = new AtomicInteger();
    AtomicInteger previousValue = new AtomicInteger();
    CacheWriter<Integer, Integer> writer = new CacheWriter<Integer, Integer>() {

        @Override
        public void write(Integer key, Integer value) {
            if (started.get()) {
                writing.set(true);
                await().until(invalidator::getState, is(Thread.State.BLOCKED));
            }
        }

        @Override
        public void delete(Integer key, Integer value, RemovalCause cause) {
            clearedValue.set(value);
        }
    };
    RemovalListener<Integer, Integer> listener = (k, v, cause) -> removedValues.addAndGet(v);
    Cache<Integer, Integer> cache = Caffeine.newBuilder().removalListener(listener).executor(Runnable::run).maximumSize(100).writer(writer).build();
    cache.put(key, oldValue);
    started.set(true);
    ConcurrentTestHarness.execute(() -> previousValue.set(cache.asMap().put(key, newValue)));
    await().untilTrue(writing);
    cache.asMap().clear();
    await().untilAtomic(clearedValue, is(newValue));
    await().untilAtomic(previousValue, is(oldValue));
    await().untilAtomic(removedValues, is(oldValue + newValue));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CacheContext(com.github.benmanes.caffeine.cache.testing.CacheContext) Listeners(org.testng.annotations.Listeners) Matchers.not(org.hamcrest.Matchers.not) Population(com.github.benmanes.caffeine.cache.testing.CacheSpec.Population) PROCESSING_TO_REQUIRED(com.github.benmanes.caffeine.cache.BLCHeader.DrainStatusRef.PROCESSING_TO_REQUIRED) Test(org.testng.annotations.Test) CacheSpec(com.github.benmanes.caffeine.cache.testing.CacheSpec) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Arrays.asList(java.util.Arrays.asList) Matchers.nullValue(org.hamcrest.Matchers.nullValue) Map(java.util.Map) Maximum(com.github.benmanes.caffeine.cache.testing.CacheSpec.Maximum) HasStats.hasEvictionCount(com.github.benmanes.caffeine.cache.testing.HasStats.hasEvictionCount) ExecutorFailure(com.github.benmanes.caffeine.cache.testing.CacheSpec.ExecutorFailure) ImmutableMap(com.google.common.collect.ImmutableMap) Implementation(com.github.benmanes.caffeine.cache.testing.CacheSpec.Implementation) Executors(java.util.concurrent.Executors) ReferenceType(com.github.benmanes.caffeine.cache.testing.CacheSpec.ReferenceType) List(java.util.List) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) REQUIRED(com.github.benmanes.caffeine.cache.BLCHeader.DrainStatusRef.REQUIRED) Eviction(com.github.benmanes.caffeine.cache.Policy.Eviction) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Entry(java.util.Map.Entry) CacheProvider(com.github.benmanes.caffeine.cache.testing.CacheProvider) Matchers.is(org.hamcrest.Matchers.is) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) Iterables(com.google.common.collect.Iterables) CacheExecutor(com.github.benmanes.caffeine.cache.testing.CacheSpec.CacheExecutor) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Awaits(com.github.benmanes.caffeine.testing.Awaits) ConcurrentTestHarness(com.github.benmanes.caffeine.testing.ConcurrentTestHarness) CacheValidationListener(com.github.benmanes.caffeine.cache.testing.CacheValidationListener) ImmutableList(com.google.common.collect.ImmutableList) WeakKeyReference(com.github.benmanes.caffeine.cache.References.WeakKeyReference) CacheWeigher(com.github.benmanes.caffeine.cache.testing.CacheSpec.CacheWeigher) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) ReentrantLock(java.util.concurrent.locks.ReentrantLock) PROCESSING_TO_IDLE(com.github.benmanes.caffeine.cache.BLCHeader.DrainStatusRef.PROCESSING_TO_IDLE) Executor(java.util.concurrent.Executor) HasRemovalNotifications.hasRemovalNotifications(com.github.benmanes.caffeine.cache.testing.HasRemovalNotifications.hasRemovalNotifications) Expire(com.github.benmanes.caffeine.cache.testing.CacheSpec.Expire) Mockito(org.mockito.Mockito) Listener(com.github.benmanes.caffeine.cache.testing.CacheSpec.Listener) Lock(java.util.concurrent.locks.Lock) Compute(com.github.benmanes.caffeine.cache.testing.CacheSpec.Compute) InitialCapacity(com.github.benmanes.caffeine.cache.testing.CacheSpec.InitialCapacity) Awaits.await(com.github.benmanes.caffeine.testing.Awaits.await) IDLE(com.github.benmanes.caffeine.cache.BLCHeader.DrainStatusRef.IDLE) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.testng.annotations.Test)

Example 2 with Listener

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

the class BoundedLocalCacheTest method evict_update.

@Test(groups = "slow")
public void evict_update() {
    Integer key = 0;
    Integer oldValue = 1;
    Integer newValue = 2;
    Thread evictor = Thread.currentThread();
    AtomicBoolean started = new AtomicBoolean();
    AtomicBoolean writing = new AtomicBoolean();
    AtomicInteger evictedValue = new AtomicInteger();
    AtomicInteger removedValues = new AtomicInteger();
    AtomicInteger previousValue = new AtomicInteger();
    CacheWriter<Integer, Integer> writer = new CacheWriter<Integer, Integer>() {

        @Override
        public void write(Integer key, Integer value) {
            if (started.get()) {
                writing.set(true);
                await().until(evictor::getState, is(Thread.State.BLOCKED));
            }
        }

        @Override
        public void delete(Integer key, Integer value, RemovalCause cause) {
            evictedValue.set(value);
        }
    };
    RemovalListener<Integer, Integer> listener = (k, v, cause) -> removedValues.addAndGet(v);
    Cache<Integer, Integer> cache = Caffeine.newBuilder().removalListener(listener).executor(Runnable::run).maximumSize(100).writer(writer).build();
    BoundedLocalCache<Integer, Integer> localCache = asBoundedLocalCache(cache);
    cache.put(key, oldValue);
    started.set(true);
    ConcurrentTestHarness.execute(() -> previousValue.set(localCache.put(key, newValue)));
    await().untilTrue(writing);
    Node<Integer, Integer> node = localCache.data.values().iterator().next();
    localCache.evictEntry(node, RemovalCause.SIZE, 0L);
    await().untilAtomic(evictedValue, is(newValue));
    await().untilAtomic(previousValue, is(oldValue));
    await().untilAtomic(removedValues, is(oldValue + newValue));
}
Also used : CacheContext(com.github.benmanes.caffeine.cache.testing.CacheContext) Listeners(org.testng.annotations.Listeners) Matchers.not(org.hamcrest.Matchers.not) Population(com.github.benmanes.caffeine.cache.testing.CacheSpec.Population) PROCESSING_TO_REQUIRED(com.github.benmanes.caffeine.cache.BLCHeader.DrainStatusRef.PROCESSING_TO_REQUIRED) Test(org.testng.annotations.Test) CacheSpec(com.github.benmanes.caffeine.cache.testing.CacheSpec) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Arrays.asList(java.util.Arrays.asList) Matchers.nullValue(org.hamcrest.Matchers.nullValue) Map(java.util.Map) Maximum(com.github.benmanes.caffeine.cache.testing.CacheSpec.Maximum) HasStats.hasEvictionCount(com.github.benmanes.caffeine.cache.testing.HasStats.hasEvictionCount) ExecutorFailure(com.github.benmanes.caffeine.cache.testing.CacheSpec.ExecutorFailure) ImmutableMap(com.google.common.collect.ImmutableMap) Implementation(com.github.benmanes.caffeine.cache.testing.CacheSpec.Implementation) Executors(java.util.concurrent.Executors) ReferenceType(com.github.benmanes.caffeine.cache.testing.CacheSpec.ReferenceType) List(java.util.List) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) REQUIRED(com.github.benmanes.caffeine.cache.BLCHeader.DrainStatusRef.REQUIRED) Eviction(com.github.benmanes.caffeine.cache.Policy.Eviction) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Entry(java.util.Map.Entry) CacheProvider(com.github.benmanes.caffeine.cache.testing.CacheProvider) Matchers.is(org.hamcrest.Matchers.is) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) Iterables(com.google.common.collect.Iterables) CacheExecutor(com.github.benmanes.caffeine.cache.testing.CacheSpec.CacheExecutor) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Awaits(com.github.benmanes.caffeine.testing.Awaits) ConcurrentTestHarness(com.github.benmanes.caffeine.testing.ConcurrentTestHarness) CacheValidationListener(com.github.benmanes.caffeine.cache.testing.CacheValidationListener) ImmutableList(com.google.common.collect.ImmutableList) WeakKeyReference(com.github.benmanes.caffeine.cache.References.WeakKeyReference) CacheWeigher(com.github.benmanes.caffeine.cache.testing.CacheSpec.CacheWeigher) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) ReentrantLock(java.util.concurrent.locks.ReentrantLock) PROCESSING_TO_IDLE(com.github.benmanes.caffeine.cache.BLCHeader.DrainStatusRef.PROCESSING_TO_IDLE) Executor(java.util.concurrent.Executor) HasRemovalNotifications.hasRemovalNotifications(com.github.benmanes.caffeine.cache.testing.HasRemovalNotifications.hasRemovalNotifications) Expire(com.github.benmanes.caffeine.cache.testing.CacheSpec.Expire) Mockito(org.mockito.Mockito) Listener(com.github.benmanes.caffeine.cache.testing.CacheSpec.Listener) Lock(java.util.concurrent.locks.Lock) Compute(com.github.benmanes.caffeine.cache.testing.CacheSpec.Compute) InitialCapacity(com.github.benmanes.caffeine.cache.testing.CacheSpec.InitialCapacity) Awaits.await(com.github.benmanes.caffeine.testing.Awaits.await) IDLE(com.github.benmanes.caffeine.cache.BLCHeader.DrainStatusRef.IDLE) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.testng.annotations.Test)

Aggregations

IDLE (com.github.benmanes.caffeine.cache.BLCHeader.DrainStatusRef.IDLE)2 PROCESSING_TO_IDLE (com.github.benmanes.caffeine.cache.BLCHeader.DrainStatusRef.PROCESSING_TO_IDLE)2 PROCESSING_TO_REQUIRED (com.github.benmanes.caffeine.cache.BLCHeader.DrainStatusRef.PROCESSING_TO_REQUIRED)2 REQUIRED (com.github.benmanes.caffeine.cache.BLCHeader.DrainStatusRef.REQUIRED)2 Eviction (com.github.benmanes.caffeine.cache.Policy.Eviction)2 WeakKeyReference (com.github.benmanes.caffeine.cache.References.WeakKeyReference)2 CacheContext (com.github.benmanes.caffeine.cache.testing.CacheContext)2 CacheProvider (com.github.benmanes.caffeine.cache.testing.CacheProvider)2 CacheSpec (com.github.benmanes.caffeine.cache.testing.CacheSpec)2 CacheExecutor (com.github.benmanes.caffeine.cache.testing.CacheSpec.CacheExecutor)2 CacheWeigher (com.github.benmanes.caffeine.cache.testing.CacheSpec.CacheWeigher)2 Compute (com.github.benmanes.caffeine.cache.testing.CacheSpec.Compute)2 ExecutorFailure (com.github.benmanes.caffeine.cache.testing.CacheSpec.ExecutorFailure)2 Expire (com.github.benmanes.caffeine.cache.testing.CacheSpec.Expire)2 Implementation (com.github.benmanes.caffeine.cache.testing.CacheSpec.Implementation)2 InitialCapacity (com.github.benmanes.caffeine.cache.testing.CacheSpec.InitialCapacity)2 Listener (com.github.benmanes.caffeine.cache.testing.CacheSpec.Listener)2 Maximum (com.github.benmanes.caffeine.cache.testing.CacheSpec.Maximum)2 Population (com.github.benmanes.caffeine.cache.testing.CacheSpec.Population)2 ReferenceType (com.github.benmanes.caffeine.cache.testing.CacheSpec.ReferenceType)2