Search in sources :

Example 1 with GetEntryProcessor

use of org.jsr107.tck.processor.GetEntryProcessor 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 GetEntryProcessor

use of org.jsr107.tck.processor.GetEntryProcessor in project cache2k by cache2k.

the class CacheExpiryTest method invokeGetValueShouldCallGetExpiry.

@Test
public void invokeGetValueShouldCallGetExpiry() {
    CountingExpiryPolicy expiryPolicy = new CountingExpiryPolicy();
    expiryPolicyServer.setExpiryPolicy(expiryPolicy);
    MutableConfiguration<Integer, Integer> config = new MutableConfiguration<>();
    config.setExpiryPolicyFactory(FactoryBuilder.factoryOf(expiryPolicyClient));
    Cache<Integer, Integer> cache = getCacheManager().createCache(getTestCacheName(), config);
    final Integer key = 123;
    final Integer setValue = 456;
    // verify non-access to non-existent entry does not call getExpiryForAccessedEntry. no read-through scenario.
    Integer resultValue = cache.invoke(key, new GetEntryProcessor<Integer, Integer>());
    assertEquals(null, resultValue);
    assertThat(expiryPolicy.getCreationCount(), is(0));
    assertThat(expiryPolicy.getAccessCount(), is(0));
    assertThat(expiryPolicy.getUpdatedCount(), is(0));
    // verify access to existing entry.
    resultValue = cache.invoke(key, new SetEntryProcessor<Integer, Integer>(setValue));
    assertEquals(resultValue, setValue);
    assertThat(expiryPolicy.getCreationCount(), greaterThanOrEqualTo(1));
    assertThat(expiryPolicy.getAccessCount(), is(0));
    assertThat(expiryPolicy.getUpdatedCount(), is(0));
    expiryPolicy.resetCount();
    resultValue = cache.invoke(key, new GetEntryProcessor<Integer, Integer>());
    assertEquals(setValue, resultValue);
    assertThat(expiryPolicy.getCreationCount(), is(0));
    assertThat(expiryPolicy.getAccessCount(), greaterThanOrEqualTo(1));
    assertThat(expiryPolicy.getUpdatedCount(), is(0));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GetEntryProcessor(org.jsr107.tck.processor.GetEntryProcessor) SetEntryProcessor(org.jsr107.tck.processor.SetEntryProcessor) MutableConfiguration(javax.cache.configuration.MutableConfiguration) Test(org.junit.Test)

Example 3 with GetEntryProcessor

use of org.jsr107.tck.processor.GetEntryProcessor 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 4 with GetEntryProcessor

use of org.jsr107.tck.processor.GetEntryProcessor in project cache2k by cache2k.

the class CacheExpiryTest method invokeMultiSetValueShouldCallGetExpiry.

@Test
public void invokeMultiSetValueShouldCallGetExpiry() {
    CountingExpiryPolicy expiryPolicy = new CountingExpiryPolicy();
    expiryPolicyServer.setExpiryPolicy(expiryPolicy);
    MutableConfiguration<Integer, Integer> config = new MutableConfiguration<>();
    config.setExpiryPolicyFactory(FactoryBuilder.factoryOf(expiryPolicyClient));
    Cache<Integer, Integer> cache = getCacheManager().createCache(getTestCacheName(), config);
    final Integer key = 123;
    final Integer setValue = 456;
    final Integer modifySetValue = 789;
    // verify create
    EntryProcessor[] processors = new EntryProcessor[] { new AssertNotPresentEntryProcessor(null), new SetEntryProcessor<Integer, Integer>(111), new SetEntryProcessor<Integer, Integer>(setValue), new GetEntryProcessor<Integer, Integer>() };
    Object[] result = (Object[]) cache.invoke(key, new CombineEntryProcessor(processors));
    assertEquals(result[1], 111);
    assertEquals(result[2], setValue);
    assertEquals(result[3], setValue);
    // expiry called should be for create, not for the get or modify.
    // Operations get combined in entry processor and only net result should be expiryPolicy method called.
    assertThat(expiryPolicy.getCreationCount(), greaterThanOrEqualTo(1));
    assertThat(expiryPolicy.getAccessCount(), greaterThanOrEqualTo(0));
    assertThat(expiryPolicy.getUpdatedCount(), greaterThanOrEqualTo(0));
}
Also used : AssertNotPresentEntryProcessor(org.jsr107.tck.processor.AssertNotPresentEntryProcessor) CombineEntryProcessor(org.jsr107.tck.processor.CombineEntryProcessor) MutableConfiguration(javax.cache.configuration.MutableConfiguration) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GetEntryProcessor(org.jsr107.tck.processor.GetEntryProcessor) SetEntryProcessor(org.jsr107.tck.processor.SetEntryProcessor) EntryProcessor(javax.cache.processor.EntryProcessor) CombineEntryProcessor(org.jsr107.tck.processor.CombineEntryProcessor) GetEntryProcessor(org.jsr107.tck.processor.GetEntryProcessor) AssertNotPresentEntryProcessor(org.jsr107.tck.processor.AssertNotPresentEntryProcessor) SetEntryProcessor(org.jsr107.tck.processor.SetEntryProcessor) Test(org.junit.Test)

Example 5 with GetEntryProcessor

use of org.jsr107.tck.processor.GetEntryProcessor in project cache2k by cache2k.

the class CacheExpiryTest method invokeSetValueShouldCallGetExpiry.

// skipping test for removeAll and removeAll specified keys for now. negative test of remove seems enough.
@Test
public void invokeSetValueShouldCallGetExpiry() {
    CountingExpiryPolicy expiryPolicy = new CountingExpiryPolicy();
    expiryPolicyServer.setExpiryPolicy(expiryPolicy);
    MutableConfiguration<Integer, Integer> config = new MutableConfiguration<>();
    config.setExpiryPolicyFactory(FactoryBuilder.factoryOf(expiryPolicyClient));
    Cache<Integer, Integer> cache = getCacheManager().createCache(getTestCacheName(), config);
    final Integer key = 123;
    final Integer setValue = 456;
    final Integer modifySetValue = 789;
    // verify create
    EntryProcessor[] processors = new EntryProcessor[] { new AssertNotPresentEntryProcessor(null), new SetEntryProcessor<Integer, Integer>(setValue), new GetEntryProcessor<Integer, Integer>() };
    Object[] result = (Object[]) cache.invoke(key, new CombineEntryProcessor(processors));
    assertEquals(result[1], setValue);
    assertEquals(result[2], setValue);
    // expiry called should be for create, not for the get or modify.
    // Operations get combined in entry processor and only net result should be expiryPolicy method called.
    assertThat(expiryPolicy.getCreationCount(), greaterThanOrEqualTo(1));
    assertThat(expiryPolicy.getAccessCount(), is(0));
    assertThat(expiryPolicy.getUpdatedCount(), is(0));
    expiryPolicy.resetCount();
    // verify modify
    Integer resultValue = cache.invoke(key, new SetEntryProcessor<Integer, Integer>(modifySetValue));
    assertEquals(modifySetValue, resultValue);
    assertThat(expiryPolicy.getCreationCount(), is(0));
    assertThat(expiryPolicy.getAccessCount(), is(0));
    assertThat(expiryPolicy.getUpdatedCount(), greaterThanOrEqualTo(1));
}
Also used : AssertNotPresentEntryProcessor(org.jsr107.tck.processor.AssertNotPresentEntryProcessor) CombineEntryProcessor(org.jsr107.tck.processor.CombineEntryProcessor) MutableConfiguration(javax.cache.configuration.MutableConfiguration) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GetEntryProcessor(org.jsr107.tck.processor.GetEntryProcessor) SetEntryProcessor(org.jsr107.tck.processor.SetEntryProcessor) EntryProcessor(javax.cache.processor.EntryProcessor) CombineEntryProcessor(org.jsr107.tck.processor.CombineEntryProcessor) GetEntryProcessor(org.jsr107.tck.processor.GetEntryProcessor) AssertNotPresentEntryProcessor(org.jsr107.tck.processor.AssertNotPresentEntryProcessor) SetEntryProcessor(org.jsr107.tck.processor.SetEntryProcessor) Test(org.junit.Test)

Aggregations

AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 MutableConfiguration (javax.cache.configuration.MutableConfiguration)5 Test (org.junit.Test)5 GetEntryProcessor (org.jsr107.tck.processor.GetEntryProcessor)3 SetEntryProcessor (org.jsr107.tck.processor.SetEntryProcessor)3 EntryProcessor (javax.cache.processor.EntryProcessor)2 CacheLoaderClient (org.jsr107.tck.integration.CacheLoaderClient)2 CacheLoaderServer (org.jsr107.tck.integration.CacheLoaderServer)2 RecordingCacheLoader (org.jsr107.tck.integration.RecordingCacheLoader)2 AssertNotPresentEntryProcessor (org.jsr107.tck.processor.AssertNotPresentEntryProcessor)2 CombineEntryProcessor (org.jsr107.tck.processor.CombineEntryProcessor)2 HashSet (java.util.HashSet)1