Search in sources :

Example 6 with CompletionListenerFuture

use of javax.cache.integration.CompletionListenerFuture in project cache2k by cache2k.

the class CacheLoaderWithoutReadThroughTest method shouldLoadSingleExistingEntryUsingLoadAll.

/**
 * Ensure that {@link Cache#loadAll(java.util.Set, boolean, javax.cache.integration.CompletionListener)}
 * for an existing single entry will cause it to be reloaded.
 */
@Test
public void shouldLoadSingleExistingEntryUsingLoadAll() throws Exception {
    RecordingCacheLoader<String> cacheLoader = new RecordingCacheLoader<String>();
    cacheLoaderServer.setCacheLoader(cacheLoader);
    String key = "message";
    HashSet<String> keys = new HashSet<>();
    keys.add(key);
    assertThat(cache.containsKey(key), is(false));
    String value = "other";
    cache.put(key, value);
    assertThat(cache.containsKey(key), is(true));
    assertThat(cache.get(key), is(value));
    CompletionListenerFuture future = new CompletionListenerFuture();
    cache.loadAll(keys, true, future);
    // wait for the load to complete
    future.get();
    assertThat(future.isDone(), is(true));
    assertThat(cache.get(key), is(equalTo(key)));
    assertThat(cacheLoader.getLoadCount(), is(1));
    assertThat(cacheLoader.hasLoaded(key), is(true));
}
Also used : CompletionListenerFuture(javax.cache.integration.CompletionListenerFuture) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 7 with CompletionListenerFuture

use of javax.cache.integration.CompletionListenerFuture in project cache2k by cache2k.

the class CacheLoaderWithoutReadThroughTest method shouldNotLoadWithNullKeyUsingLoadAll.

/**
 * Ensure that {@link Cache#loadAll(java.util.Set, boolean, javax.cache.integration.CompletionListener)} )}
 * using a <code>null</code> key will raise an exception
 */
@Test
public void shouldNotLoadWithNullKeyUsingLoadAll() throws Exception {
    RecordingCacheLoader<String> cacheLoader = new RecordingCacheLoader<String>();
    cacheLoaderServer.setCacheLoader(cacheLoader);
    HashSet<String> keys = new HashSet<>();
    keys.add(null);
    try {
        CompletionListenerFuture future = new CompletionListenerFuture();
        cache.loadAll(keys, false, future);
        fail("Expected a NullPointerException");
    } catch (NullPointerException e) {
    // SKIP: expected
    } finally {
        assertThat(cacheLoader.getLoadCount(), is(0));
    }
}
Also used : CompletionListenerFuture(javax.cache.integration.CompletionListenerFuture) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 8 with CompletionListenerFuture

use of javax.cache.integration.CompletionListenerFuture in project cache2k by cache2k.

the class CacheLoaderWriterTest method shouldNotWriteThroughUsingLoadAll.

@Test
public void shouldNotWriteThroughUsingLoadAll() throws Exception {
    final int NUMBER_OF_KEYS = 10;
    assertEquals(0, recordingCacheWriter.getWriteCount());
    assertEquals(0, recordingCacheWriter.getDeleteCount());
    assertEquals(0, recordingCacheLoader.getLoadCount());
    Set<String> keys = new HashSet<>();
    for (int key = 1; key <= NUMBER_OF_KEYS; key++) {
        keys.add(Integer.toString(key));
    }
    assertEquals(0, recordingCacheWriter.getWriteCount());
    assertEquals(0, recordingCacheWriter.getDeleteCount());
    CompletionListenerFuture future = new CompletionListenerFuture();
    cache.loadAll(keys, true, future);
    // wait for the load to complete
    future.get();
    assertThat(recordingCacheLoader.getLoadCount(), is(keys.size()));
    for (String key : keys) {
        assertThat(recordingCacheLoader.hasLoaded(key), is(true));
        assertThat(cache.containsKey(key), is(true));
    }
    assertEquals(0, recordingCacheWriter.getWriteCount());
    assertEquals(0, recordingCacheWriter.getDeleteCount());
}
Also used : CompletionListenerFuture(javax.cache.integration.CompletionListenerFuture) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 9 with CompletionListenerFuture

use of javax.cache.integration.CompletionListenerFuture in project cache2k by cache2k.

the class CacheLoaderWriterTest method shouldLoadSingleMissingEntryUsingLoadAll.

/**
 * Ensure that {@link Cache#loadAll(java.util.Set, boolean, javax.cache.integration.CompletionListener)}
 * for a non-existent single value will cause it to be loaded and not written.
 */
@Test
public void shouldLoadSingleMissingEntryUsingLoadAll() throws Exception {
    String key = "message";
    HashSet<String> keys = new HashSet<>();
    keys.add(key);
    assertThat(cache.containsKey(key), is(false));
    CompletionListenerFuture future = new CompletionListenerFuture();
    cache.loadAll(keys, false, future);
    // wait for the load to complete
    future.get();
    assertThat(future.isDone(), is(true));
    assertThat(cache.get(key), is(equalTo(key)));
    assertThat(recordingCacheLoader.getLoadCount(), is(1));
    assertThat(recordingCacheLoader.hasLoaded(key), is(true));
    // ensure nothing has been written
    assertThat(recordingCacheWriter.getWriteCount(), is(0L));
    assertThat(recordingCacheWriter.getDeleteCount(), is(0L));
}
Also used : CompletionListenerFuture(javax.cache.integration.CompletionListenerFuture) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 10 with CompletionListenerFuture

use of javax.cache.integration.CompletionListenerFuture in project cache2k by cache2k.

the class CacheLoaderWriterTest method shouldLoadMultipleNonExistingEntryUsingLoadAll.

/**
 * Ensure that {@link Cache#loadAll(java.util.Set, boolean, javax.cache.integration.CompletionListener)} )}
 * for multiple non-existing entries will be loaded or written.
 */
@Test
public void shouldLoadMultipleNonExistingEntryUsingLoadAll() throws Exception {
    HashSet<String> keys = new HashSet<>();
    keys.add("gudday");
    keys.add("hello");
    keys.add("howdy");
    keys.add("bonjour");
    for (String key : keys) {
        assertThat(cache.containsKey(key), is(false));
    }
    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()));
    for (String key : keys) {
        assertThat(cache.get(key), is(equalTo(key)));
        assertThat(recordingCacheLoader.hasLoaded(key), is(true));
    }
    // ensure nothing has been written
    assertThat(recordingCacheWriter.getWriteCount(), is(0L));
    assertThat(recordingCacheWriter.getDeleteCount(), is(0L));
}
Also used : CompletionListenerFuture(javax.cache.integration.CompletionListenerFuture) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

CompletionListenerFuture (javax.cache.integration.CompletionListenerFuture)32 Test (org.junit.Test)30 HashSet (java.util.HashSet)26 IgniteCacheAbstractTest (org.apache.ignite.internal.processors.cache.IgniteCacheAbstractTest)3 ExecutionException (java.util.concurrent.ExecutionException)2 Duration (javax.cache.expiry.Duration)2 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)2 TimeStep (com.hazelcast.simulator.test.annotations.TimeStep)1 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 MutableConfiguration (javax.cache.configuration.MutableConfiguration)1 CreatedExpiryPolicy (javax.cache.expiry.CreatedExpiryPolicy)1 ExpiryPolicy (javax.cache.expiry.ExpiryPolicy)1 Affinity (org.apache.ignite.cache.affinity.Affinity)1 SqlQuery (org.apache.ignite.cache.query.SqlQuery)1 GridCacheAbstractSelfTest (org.apache.ignite.internal.processors.cache.GridCacheAbstractSelfTest)1 Transaction (org.apache.ignite.transactions.Transaction)1 TransactionConcurrency (org.apache.ignite.transactions.TransactionConcurrency)1 TransactionIsolation (org.apache.ignite.transactions.TransactionIsolation)1