Search in sources :

Example 71 with MutableConfiguration

use of javax.cache.configuration.MutableConfiguration 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();
        setupExpiryPolicyClientServer(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 72 with MutableConfiguration

use of javax.cache.configuration.MutableConfiguration in project cache2k by cache2k.

the class CacheExpiryTest method invokeGetValueWithReadThroughForNonExistentEntryShouldCallGetExpiryForCreatedEntry.

@Test
public void invokeGetValueWithReadThroughForNonExistentEntryShouldCallGetExpiryForCreatedEntry() throws IOException {
    CountingExpiryPolicy expiryPolicy = new CountingExpiryPolicy();
    setupExpiryPolicyClientServer(expiryPolicy);
    // 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());
        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 73 with MutableConfiguration

use of javax.cache.configuration.MutableConfiguration in project cache2k by cache2k.

the class CacheExpiryTest method invokeAllSetValueShouldCallGetExpiry.

@Test
public void invokeAllSetValueShouldCallGetExpiry() {
    CountingExpiryPolicy expiryPolicy = new CountingExpiryPolicy();
    setupExpiryPolicyClientServer(expiryPolicy);
    MutableConfiguration<Integer, Integer> config = new MutableConfiguration<>();
    config.setExpiryPolicyFactory(FactoryBuilder.factoryOf(expiryPolicyClient));
    Cache<Integer, Integer> cache = getCacheManager().createCache(getTestCacheName(), config);
    final Integer INITIAL_KEY = 123;
    final Integer MAX_KEY_VALUE = INITIAL_KEY + 4;
    final Integer setValue = 456;
    final Integer modifySetValue = 789;
    // set half of the keys so half of invokeAll will be modify and rest will be create.
    Set<Integer> keys = new HashSet<>();
    int createdCount = 0;
    for (int key = INITIAL_KEY; key <= MAX_KEY_VALUE; key++) {
        keys.add(key);
        if (key <= MAX_KEY_VALUE - 2) {
            cache.put(key, setValue);
            createdCount++;
        }
    }
    assertThat(expiryPolicy.getCreationCount(), greaterThanOrEqualTo(createdCount));
    assertThat(expiryPolicy.getAccessCount(), is(0));
    assertThat(expiryPolicy.getUpdatedCount(), is(0));
    expiryPolicy.resetCount();
    // verify modify or create
    cache.invokeAll(keys, new SetEntryProcessor<Integer, Integer>(setValue));
    assertThat(expiryPolicy.getCreationCount(), greaterThanOrEqualTo(keys.size() - createdCount));
    assertThat(expiryPolicy.getAccessCount(), is(0));
    assertThat(expiryPolicy.getUpdatedCount(), greaterThanOrEqualTo(createdCount));
    expiryPolicy.resetCount();
    // verify accessed
    cache.invokeAll(keys, new GetEntryProcessor<Integer, Integer>());
    assertThat(expiryPolicy.getCreationCount(), is(0));
    assertThat(expiryPolicy.getAccessCount(), greaterThanOrEqualTo(keys.size()));
    assertThat(expiryPolicy.getUpdatedCount(), is(0));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MutableConfiguration(javax.cache.configuration.MutableConfiguration) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 74 with MutableConfiguration

use of javax.cache.configuration.MutableConfiguration in project cache2k by cache2k.

the class TypesTest method genericsEnforcementAndStricterTypeEnforcement.

/**
 * What happens when you:
 *
 * 1) declare using generics with a super class
 * 2) declare using configuration with a sub class
 *
 * Set generics to Identifier and Dog
 * Bypass generics with a raw MutableConfiguration but set runtime to Identifier and Hound
 *
 * The configuration checking gets done on put.
 */
@Test
public void genericsEnforcementAndStricterTypeEnforcement() {
    // configure the cache
    MutableConfiguration config = new MutableConfiguration<>();
    config.setTypes(Identifier.class, Hound.class);
    Cache<Identifier, Dog> cache = cacheManager.createCache(cacheName, config);
    // Types are restricted and types are enforced
    // Cannot put in wrong types
    // cache.put(1, "something");
    // can put in
    cache.put(pistachio.getName(), pistachio);
    // can put in with generics but possibly not with configuration as not a hound
    try {
        cache.put(tonto.getName(), tonto);
    } catch (ClassCastException e) {
    // expected but not mandatory. The RI throws these.
    }
    // cannot get out wrong key types
    // assertNotNull(cache.get(1));
    assertNotNull(cache.get(pistachio.getName()));
    // not necessarily
    // assertNotNull(cache.get(tonto.getName()));
    // cannot remove wrong key types
    // assertTrue(cache.remove(1));
    assertTrue(cache.remove(pistachio.getName()));
// not necessarily
// assertTrue(cache.remove(tonto.getName()));
}
Also used : Identifier(domain.Identifier) Dog(domain.Dog) MutableConfiguration(javax.cache.configuration.MutableConfiguration) Test(org.junit.Test)

Example 75 with MutableConfiguration

use of javax.cache.configuration.MutableConfiguration in project cache2k by cache2k.

the class TypesTest method simpleAPINoGenericsAndNoTypeEnforcementStoreByReference.

/**
 * What happens when you:
 *
 * 1) don't declare using generics and
 * 2) don't specify types during configuration.
 */
@Test
public void simpleAPINoGenericsAndNoTypeEnforcementStoreByReference() {
    MutableConfiguration config = new MutableConfiguration().setStoreByValue(false);
    Cache cache = cacheManager.createCache(cacheName, config);
    Identifier2 one = new Identifier2("1");
    // can put different things in
    cache.put(one, "something");
    cache.put(pistachio.getName(), pistachio);
    cache.put(tonto.getName(), tonto);
    cache.put(bonzo.getName(), bonzo);
    cache.put(juno.getName(), juno);
    cache.put(talker.getName(), talker);
    try {
        cache.put(skinny.getName(), skinny);
    } catch (Exception e) {
    // not serializable expected
    }
    // can get them out
    Identifier2 one_ = new Identifier2("1");
    Assert.assertEquals(one, one_);
    Assert.assertEquals(one.hashCode(), one_.hashCode());
    assertNotNull(cache.get(one_));
    assertNotNull(cache.get(one));
    assertNotNull(cache.get(pistachio.getName()));
    // can remove them
    assertTrue(cache.remove(one));
    assertTrue(cache.remove(pistachio.getName()));
}
Also used : MutableConfiguration(javax.cache.configuration.MutableConfiguration) Cache(javax.cache.Cache) Identifier2(domain.Identifier2) Test(org.junit.Test)

Aggregations

MutableConfiguration (javax.cache.configuration.MutableConfiguration)146 Test (org.junit.Test)97 CacheManager (javax.cache.CacheManager)76 CachingProvider (javax.cache.spi.CachingProvider)35 Cache (javax.cache.Cache)32 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)21 QuickTest (com.hazelcast.test.annotation.QuickTest)16 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)15 Test (org.junit.jupiter.api.Test)12 CacheTestSupport.createServerCachingProvider (com.hazelcast.cache.CacheTestSupport.createServerCachingProvider)11 URI (java.net.URI)11 CreatedExpiryPolicy (javax.cache.expiry.CreatedExpiryPolicy)11 MutableCacheEntryListenerConfiguration (javax.cache.configuration.MutableCacheEntryListenerConfiguration)10 Duration (javax.cache.expiry.Duration)10 CountDownLatch (java.util.concurrent.CountDownLatch)8 ExpiryPolicy (javax.cache.expiry.ExpiryPolicy)7 ExtendedMutableConfiguration (org.cache2k.jcache.ExtendedMutableConfiguration)7 AssertTask (com.hazelcast.test.AssertTask)6 BaseTest (org.redisson.BaseTest)6 RedisRunner (org.redisson.RedisRunner)6