Search in sources :

Example 6 with Condition

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

the class ListenerTest method asyncExpiredListenerCalledSharpExpiry.

@Test
public void asyncExpiredListenerCalledSharpExpiry() {
    final AtomicInteger _callCount = new AtomicInteger();
    final long _EXPIRY_MILLIS = TestingParameters.MINIMAL_TICK_MILLIS;
    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) {
                    _callCount.incrementAndGet();
                }
            }).expireAfterWrite(_EXPIRY_MILLIS, TimeUnit.MILLISECONDS).sharpExpiry(true);
        }
    });
    final int ANY_KEY = 1;
    TimeBox.millis(_EXPIRY_MILLIS).work(new Runnable() {

        @Override
        public void run() {
            c.put(ANY_KEY, 4711);
        }
    }).check(new Runnable() {

        @Override
        public void run() {
            assertEquals(0, _callCount.get());
            assertTrue(c.containsKey(ANY_KEY));
        }
    });
    ConcurrencyHelper.await(new Condition() {

        @Override
        public boolean check() throws Exception {
            return _callCount.get() == 1;
        }
    });
}
Also used : CacheEntryExpiredListener(org.cache2k.event.CacheEntryExpiredListener) 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) Cache(org.cache2k.Cache) Test(org.junit.Test)

Example 7 with Condition

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

the class ListenerTest method asyncUpdateListenerException.

@Test
public void asyncUpdateListenerException() {
    String _logName = getClass().getName() + ".asyncUpdateListenerException";
    final Log.SuppressionCounter _suppressionCounter = new Log.SuppressionCounter();
    Log.registerSuppression("org.cache2k.Cache/default:" + _logName, _suppressionCounter);
    Cache<Integer, Integer> c = Cache2kBuilder.of(Integer.class, Integer.class).name(_logName).eternal(true).addAsyncListener(new CacheEntryUpdatedListener<Integer, Integer>() {

        @Override
        public void onEntryUpdated(final Cache<Integer, Integer> cache, final CacheEntry<Integer, Integer> currentEntry, final CacheEntry<Integer, Integer> entryWithNewData) {
            throw new RuntimeException("ouch");
        }
    }).build();
    c.put(1, 2);
    c.put(1, 2);
    ConcurrencyHelper.await(new Condition() {

        @Override
        public boolean check() throws Exception {
            return _suppressionCounter.getWarnCount() == 1;
        }
    });
    c.close();
}
Also used : Condition(org.cache2k.test.util.Condition) Log(org.cache2k.core.util.Log) CacheEntry(org.cache2k.CacheEntry) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CacheEntryUpdatedListener(org.cache2k.event.CacheEntryUpdatedListener) Cache(org.cache2k.Cache) Test(org.junit.Test)

Example 8 with Condition

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

the class ListenerTest method asyncCreatedListenerCalled.

/**
 * If the listener is not executed in separate thread, this would block
 */
@Test(timeout = TestingParameters.MAX_FINISH_WAIT_MILLIS)
public void asyncCreatedListenerCalled() {
    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 CacheEntryCreatedListener<Integer, Integer>() {

                @Override
                public void onEntryCreated(final Cache<Integer, Integer> c, final CacheEntry<Integer, Integer> e) {
                    try {
                        _fire.await();
                    } catch (InterruptedException ignore) {
                    }
                    _callCount.incrementAndGet();
                }
            });
        }
    });
    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) CacheEntryCreatedListener(org.cache2k.event.CacheEntryCreatedListener) Cache(org.cache2k.Cache) Test(org.junit.Test)

Example 9 with Condition

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

the class ListenerTest method asyncExpiredListenerCalled.

@Test
public void asyncExpiredListenerCalled() {
    final AtomicInteger _callCount = new AtomicInteger();
    final long _EXPIRY_MILLIS = TestingParameters.MINIMAL_TICK_MILLIS;
    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) {
                    _callCount.incrementAndGet();
                }
            }).expireAfterWrite(_EXPIRY_MILLIS, TimeUnit.MILLISECONDS);
        }
    });
    final int ANY_KEY = 1;
    TimeBox.millis(_EXPIRY_MILLIS).work(new Runnable() {

        @Override
        public void run() {
            c.put(ANY_KEY, 4711);
        }
    }).check(new Runnable() {

        @Override
        public void run() {
            assertEquals(0, _callCount.get());
            assertTrue(c.containsKey(ANY_KEY));
        }
    });
    ConcurrencyHelper.await(new Condition() {

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

Example 10 with Condition

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

the class ListenerTest method asyncRemovedListenerCalled.

/**
 * If the listener is not executed in separate thread, this would block
 */
@Test(timeout = TestingParameters.MAX_FINISH_WAIT_MILLIS)
public void asyncRemovedListenerCalled() {
    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 CacheEntryRemovedListener<Integer, Integer>() {

                @Override
                public void onEntryRemoved(final Cache<Integer, Integer> c, final CacheEntry<Integer, Integer> e) {
                    try {
                        _fire.await();
                    } catch (InterruptedException ignore) {
                    }
                    _callCount.incrementAndGet();
                }
            });
        }
    });
    c.put(1, 2);
    assertEquals(0, _callCount.get());
    c.put(1, 2);
    assertEquals(0, _callCount.get());
    c.remove(1);
    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) CacheEntryRemovedListener(org.cache2k.event.CacheEntryRemovedListener) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Cache(org.cache2k.Cache) 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