Search in sources :

Example 1 with Condition

use of org.cache2k.test.util.Condition in project cache2k by cache2k.

the class CacheLoaderTest method prefetch.

@Test
public void prefetch() {
    final Cache<Integer, Integer> c = cacheWithLoader();
    c.prefetch(1);
    assertTrue(isLoadStarted(c));
    ConcurrencyHelper.await(new Condition() {

        @Override
        public boolean check() throws Exception {
            return c.containsKey(1);
        }
    });
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Condition(org.cache2k.test.util.Condition) Test(org.junit.Test)

Example 2 with Condition

use of org.cache2k.test.util.Condition in project cache2k by cache2k.

the class CacheLoaderTest method prefetchAll.

@Test
public void prefetchAll() {
    final Cache<Integer, Integer> c = cacheWithLoader();
    c.prefetchAll(toIterable(1, 2, 3), null);
    assertTrue(isLoadStarted(c));
    ConcurrencyHelper.await(new Condition() {

        @Override
        public boolean check() throws Exception {
            return c.containsKey(1);
        }
    });
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Condition(org.cache2k.test.util.Condition) Test(org.junit.Test)

Example 3 with Condition

use of org.cache2k.test.util.Condition in project cache2k by cache2k.

the class ListenerTest method manyAsyncUpdateListenerCalled.

/**
 * Check that we do not miss events.
 */
@Test(timeout = TestingParameters.MAX_FINISH_WAIT_MILLIS)
public void manyAsyncUpdateListenerCalled() {
    final AtomicInteger _callCount = new AtomicInteger();
    final ConcurrentMap<Integer, Integer> _seenValues = new ConcurrentHashMap<Integer, Integer>();
    Cache<Integer, Integer> c = target.cache(new CacheRule.Specialization<Integer, Integer>() {

        @Override
        public void extend(final Cache2kBuilder<Integer, Integer> b) {
            b.addAsyncListener(new CacheEntryUpdatedListener<Integer, Integer>() {

                @Override
                public void onEntryUpdated(final Cache<Integer, Integer> cache, final CacheEntry<Integer, Integer> currentEntry, final CacheEntry<Integer, Integer> entryWithNewData) {
                    _seenValues.put(entryWithNewData.getValue(), entryWithNewData.getValue());
                    _callCount.incrementAndGet();
                }
            });
        }
    });
    c.put(1, 2);
    assertEquals(0, _callCount.get());
    final int _UPDATE_COUNT = 123;
    for (int i = 0; i < _UPDATE_COUNT; i++) {
        c.put(1, i);
    }
    ConcurrencyHelper.await(new Condition() {

        @Override
        public boolean check() throws Exception {
            return _callCount.get() == _UPDATE_COUNT;
        }
    });
    assertEquals("Event dispatching is using copied events", 123, _seenValues.size());
}
Also used : Condition(org.cache2k.test.util.Condition) IntCacheRule(org.cache2k.test.util.IntCacheRule) CacheRule(org.cache2k.test.util.CacheRule) CacheEntry(org.cache2k.CacheEntry) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CacheEntryUpdatedListener(org.cache2k.event.CacheEntryUpdatedListener) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Cache(org.cache2k.Cache) Test(org.junit.Test)

Example 4 with Condition

use of org.cache2k.test.util.Condition in project cache2k by cache2k.

the class ListenerTest method asyncUpdateListenerCalled.

/**
 * If the listener is not executed in separate thread, this would block
 */
@Test(timeout = TestingParameters.MAX_FINISH_WAIT_MILLIS)
public void asyncUpdateListenerCalled() {
    final AtomicInteger _callCount = new AtomicInteger();
    final CountDownLatch _fire = new CountDownLatch(1);
    Cache<Integer, Integer> c = target.cache(new CacheRule.Specialization<Integer, Integer>() {

        @Override
        public void extend(final Cache2kBuilder<Integer, Integer> b) {
            b.addAsyncListener(new CacheEntryUpdatedListener<Integer, Integer>() {

                @Override
                public void onEntryUpdated(final Cache<Integer, Integer> cache, final CacheEntry<Integer, Integer> currentEntry, final CacheEntry<Integer, Integer> entryWithNewData) {
                    try {
                        _fire.await();
                    } catch (InterruptedException ignore) {
                    }
                    _callCount.incrementAndGet();
                }
            });
        }
    });
    c.put(1, 2);
    assertEquals(0, _callCount.get());
    c.put(1, 2);
    assertEquals(0, _callCount.get());
    _fire.countDown();
    ConcurrencyHelper.await(new Condition() {

        @Override
        public boolean check() throws Exception {
            return _callCount.get() == 1;
        }
    });
}
Also used : Condition(org.cache2k.test.util.Condition) CountDownLatch(java.util.concurrent.CountDownLatch) IntCacheRule(org.cache2k.test.util.IntCacheRule) CacheRule(org.cache2k.test.util.CacheRule) CacheEntry(org.cache2k.CacheEntry) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CacheEntryUpdatedListener(org.cache2k.event.CacheEntryUpdatedListener) Cache(org.cache2k.Cache) Test(org.junit.Test)

Example 5 with Condition

use of org.cache2k.test.util.Condition in project cache2k by cache2k.

the class ListenerTest method asyncHalfExpiredAfterUpdate.

/**
 * Expire time is load time if entry is modified, yields: Expiry listener is called. Entry
 * stays in the cache, we need to implement the removal
 */
@Test
public void asyncHalfExpiredAfterUpdate() {
    final AtomicInteger _expireCallCount = new AtomicInteger();
    final Cache<Integer, Integer> c = target.cache(new CacheRule.Specialization<Integer, Integer>() {

        @Override
        public void extend(final Cache2kBuilder<Integer, Integer> b) {
            b.addAsyncListener(new CacheEntryExpiredListener<Integer, Integer>() {

                @Override
                public void onEntryExpired(final Cache<Integer, Integer> c, final CacheEntry<Integer, Integer> e) {
                    _expireCallCount.incrementAndGet();
                }
            }).eternal(true).keepDataAfterExpired(false).expiryPolicy(new ExpiryPolicy<Integer, Integer>() {

                @Override
                public long calculateExpiryTime(final Integer key, final Integer value, final long loadTime, final CacheEntry<Integer, Integer> oldEntry) {
                    if (oldEntry != null) {
                        return loadTime;
                    }
                    return ETERNAL;
                }
            });
        }
    });
    c.put(1, 1);
    c.put(1, 2);
    ConcurrencyHelper.await(new Condition() {

        @Override
        public boolean check() throws Exception {
            return _expireCallCount.get() == 1;
        }
    });
    assertEquals(0, latestInfo(c).getSize());
    assertEquals(1, latestInfo(c).getExpiredCount());
}
Also used : Condition(org.cache2k.test.util.Condition) IntCacheRule(org.cache2k.test.util.IntCacheRule) CacheRule(org.cache2k.test.util.CacheRule) CacheEntry(org.cache2k.CacheEntry) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExpiryPolicy(org.cache2k.expiry.ExpiryPolicy) Test(org.junit.Test)

Aggregations

AtomicInteger (java.util.concurrent.atomic.AtomicInteger)11 Condition (org.cache2k.test.util.Condition)11 Test (org.junit.Test)11 CacheEntry (org.cache2k.CacheEntry)8 CacheRule (org.cache2k.test.util.CacheRule)8 IntCacheRule (org.cache2k.test.util.IntCacheRule)8 Cache (org.cache2k.Cache)6 CountDownLatch (java.util.concurrent.CountDownLatch)3 CacheEntryUpdatedListener (org.cache2k.event.CacheEntryUpdatedListener)3 ExpiryPolicy (org.cache2k.expiry.ExpiryPolicy)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Log (org.cache2k.core.util.Log)1 CacheEntryCreatedListener (org.cache2k.event.CacheEntryCreatedListener)1 CacheEntryExpiredListener (org.cache2k.event.CacheEntryExpiredListener)1 CacheEntryRemovedListener (org.cache2k.event.CacheEntryRemovedListener)1