Search in sources :

Example 1 with CacheLoaderServer

use of org.jsr107.tck.integration.CacheLoaderServer in project cache2k by cache2k.

the class CacheExpiryTest method invokeGetValueWithReadThroughForNonExistentEntryShouldCallGetExpiryForCreatedEntry.

@Test
public void invokeGetValueWithReadThroughForNonExistentEntryShouldCallGetExpiryForCreatedEntry() throws IOException {
    // establish and open a CacheLoaderServer to handle cache
    // cache loading requests from a CacheLoaderClient
    // this cacheLoader just returns the key as the value.
    RecordingCacheLoader<Integer> recordingCacheLoader = new RecordingCacheLoader<>();
    try (CacheLoaderServer<Integer, Integer> cacheLoaderServer = new CacheLoaderServer<>(10000, recordingCacheLoader)) {
        cacheLoaderServer.open();
        // establish a CacheLoaderClient that a Cache can use for loading entries
        // (via the CacheLoaderServer)
        CacheLoaderClient<Integer, Integer> cacheLoader = new CacheLoaderClient<>(cacheLoaderServer.getInetAddress(), cacheLoaderServer.getPort());
        CountingExpiryPolicy expiryPolicy = new CountingExpiryPolicy();
        expiryPolicyServer.setExpiryPolicy(expiryPolicy);
        MutableConfiguration<Integer, Integer> config = new MutableConfiguration<>();
        config.setExpiryPolicyFactory(FactoryBuilder.factoryOf(expiryPolicyClient));
        config.setCacheLoaderFactory(FactoryBuilder.factoryOf(cacheLoader));
        config.setReadThrough(true);
        Cache<Integer, Integer> cache = getCacheManager().createCache(getTestCacheName(), config);
        final Integer key = 123;
        final Integer recordingCacheLoaderValue = key;
        // verify create when read through is enabled and entry was non-existent in cache.
        Integer resultValue = cache.invoke(key, new GetEntryProcessor<Integer, Integer>());
        assertEquals(recordingCacheLoaderValue, resultValue);
        assertTrue(recordingCacheLoader.hasLoaded(key));
        assertThat(expiryPolicy.getCreationCount(), greaterThanOrEqualTo(1));
        assertThat(expiryPolicy.getAccessCount(), is(0));
        assertThat(expiryPolicy.getUpdatedCount(), is(0));
        closeTestCache();
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CacheLoaderServer(org.jsr107.tck.integration.CacheLoaderServer) CacheLoaderClient(org.jsr107.tck.integration.CacheLoaderClient) RecordingCacheLoader(org.jsr107.tck.integration.RecordingCacheLoader) MutableConfiguration(javax.cache.configuration.MutableConfiguration) Test(org.junit.Test)

Example 2 with CacheLoaderServer

use of org.jsr107.tck.integration.CacheLoaderServer in project cache2k by cache2k.

the class CacheExpiryTest method invokeAllReadThroughEnabledGetOnNonExistentEntry.

@Test
public void invokeAllReadThroughEnabledGetOnNonExistentEntry() throws IOException {
    // establish and open a CacheLoaderServer to handle cache
    // cache loading requests from a CacheLoaderClient
    // this cacheLoader just returns the key as the value.
    RecordingCacheLoader<Integer> recordingCacheLoader = new RecordingCacheLoader<>();
    try (CacheLoaderServer<Integer, Integer> cacheLoaderServer = new CacheLoaderServer<>(10000, recordingCacheLoader)) {
        cacheLoaderServer.open();
        // establish a CacheLoaderClient that a Cache can use for loading entries
        // (via the CacheLoaderServer)
        CacheLoaderClient<Integer, Integer> cacheLoader = new CacheLoaderClient<>(cacheLoaderServer.getInetAddress(), cacheLoaderServer.getPort());
        CountingExpiryPolicy expiryPolicy = new CountingExpiryPolicy();
        expiryPolicyServer.setExpiryPolicy(expiryPolicy);
        MutableConfiguration<Integer, Integer> config = new MutableConfiguration<>();
        config.setExpiryPolicyFactory(FactoryBuilder.factoryOf(expiryPolicyClient));
        config.setCacheLoaderFactory(FactoryBuilder.factoryOf(cacheLoader));
        config.setReadThrough(true);
        Cache<Integer, Integer> cache = getCacheManager().createCache(getTestCacheName(), config);
        final Integer INITIAL_KEY = 123;
        final Integer MAX_KEY_VALUE = INITIAL_KEY + 4;
        // set keys to read through
        Set<Integer> keys = new HashSet<>();
        for (int key = INITIAL_KEY; key <= MAX_KEY_VALUE; key++) {
            keys.add(key);
        }
        // verify read-through of getValue of non-existent entries
        cache.invokeAll(keys, new GetEntryProcessor<Integer, Integer>());
        assertThat(expiryPolicy.getCreationCount(), greaterThanOrEqualTo(keys.size()));
        assertThat(expiryPolicy.getAccessCount(), is(0));
        assertThat(expiryPolicy.getUpdatedCount(), is(0));
        expiryPolicy.resetCount();
        closeTestCache();
    }
}
Also used : CacheLoaderServer(org.jsr107.tck.integration.CacheLoaderServer) CacheLoaderClient(org.jsr107.tck.integration.CacheLoaderClient) MutableConfiguration(javax.cache.configuration.MutableConfiguration) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RecordingCacheLoader(org.jsr107.tck.integration.RecordingCacheLoader) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with CacheLoaderServer

use of org.jsr107.tck.integration.CacheLoaderServer in project cache2k by cache2k.

the class CacheExpiryTest method loadAllWithReadThroughEnabledShouldCallGetExpiryForCreatedEntry.

@Test
public void loadAllWithReadThroughEnabledShouldCallGetExpiryForCreatedEntry() throws IOException, ExecutionException, InterruptedException {
    // establish and open a CacheLoaderServer to handle cache
    // cache loading requests from a CacheLoaderClient
    // this cacheLoader just returns the key as the value.
    RecordingCacheLoader<Integer> recordingCacheLoader = new RecordingCacheLoader<>();
    try (CacheLoaderServer<Integer, Integer> cacheLoaderServer = new CacheLoaderServer<>(10000, recordingCacheLoader)) {
        cacheLoaderServer.open();
        // establish a CacheLoaderClient that a Cache can use for loading entries
        // (via the CacheLoaderServer)
        CacheLoaderClient<Integer, Integer> cacheLoader = new CacheLoaderClient<>(cacheLoaderServer.getInetAddress(), cacheLoaderServer.getPort());
        CountingExpiryPolicy expiryPolicy = new CountingExpiryPolicy();
        expiryPolicyServer.setExpiryPolicy(expiryPolicy);
        MutableConfiguration<Integer, Integer> config = new MutableConfiguration<>();
        config.setExpiryPolicyFactory(FactoryBuilder.factoryOf(expiryPolicyClient));
        config.setCacheLoaderFactory(FactoryBuilder.factoryOf(cacheLoader));
        config.setReadThrough(true);
        Cache<Integer, Integer> cache = getCacheManager().createCache(getTestCacheName(), config);
        final Integer INITIAL_KEY = 123;
        final Integer MAX_KEY_VALUE = INITIAL_KEY + 4;
        // set half of the keys so half of loadAdd will be loaded
        Set<Integer> keys = new HashSet<>();
        for (int key = INITIAL_KEY; key <= MAX_KEY_VALUE; key++) {
            keys.add(key);
        }
        // verify read-through of getValue of non-existent entries
        CompletionListenerFuture future = new CompletionListenerFuture();
        cache.loadAll(keys, false, future);
        // wait for the load to complete
        future.get();
        assertThat(future.isDone(), is(true));
        assertThat(recordingCacheLoader.getLoadCount(), is(keys.size()));
        assertThat(expiryPolicy.getCreationCount(), greaterThanOrEqualTo(keys.size()));
        assertThat(expiryPolicy.getAccessCount(), is(0));
        assertThat(expiryPolicy.getUpdatedCount(), is(0));
        expiryPolicy.resetCount();
        for (Integer key : keys) {
            assertThat(recordingCacheLoader.hasLoaded(key), is(true));
            assertThat(cache.get(key), is(equalTo(key)));
        }
        assertThat(expiryPolicy.getAccessCount(), greaterThanOrEqualTo(keys.size()));
        expiryPolicy.resetCount();
        // verify read-through of getValue for existing entries AND replaceExistingValues is true.
        final boolean REPLACE_EXISTING_VALUES = true;
        future = new CompletionListenerFuture();
        cache.loadAll(keys, REPLACE_EXISTING_VALUES, future);
        // wait for the load to complete
        future.get();
        assertThat(future.isDone(), is(true));
        assertThat(recordingCacheLoader.getLoadCount(), is(keys.size() * 2));
        assertThat(expiryPolicy.getCreationCount(), is(0));
        assertThat(expiryPolicy.getAccessCount(), is(0));
        assertThat(expiryPolicy.getUpdatedCount(), greaterThanOrEqualTo(keys.size()));
        expiryPolicy.resetCount();
        for (Integer key : keys) {
            assertThat(recordingCacheLoader.hasLoaded(key), is(true));
            assertThat(cache.get(key), is(equalTo(key)));
        }
        closeTestCache();
    }
}
Also used : CacheLoaderServer(org.jsr107.tck.integration.CacheLoaderServer) CacheLoaderClient(org.jsr107.tck.integration.CacheLoaderClient) MutableConfiguration(javax.cache.configuration.MutableConfiguration) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RecordingCacheLoader(org.jsr107.tck.integration.RecordingCacheLoader) CompletionListenerFuture(javax.cache.integration.CompletionListenerFuture) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 MutableConfiguration (javax.cache.configuration.MutableConfiguration)3 CacheLoaderClient (org.jsr107.tck.integration.CacheLoaderClient)3 CacheLoaderServer (org.jsr107.tck.integration.CacheLoaderServer)3 RecordingCacheLoader (org.jsr107.tck.integration.RecordingCacheLoader)3 Test (org.junit.Test)3 HashSet (java.util.HashSet)2 CompletionListenerFuture (javax.cache.integration.CompletionListenerFuture)1