Search in sources :

Example 26 with CompletionListenerFuture

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

the class CacheLoaderWithoutReadThroughTest method shouldPropagateExceptionUsingLoadAll.

/**
 * Ensure that {@link Cache#loadAll(java.util.Set, boolean, javax.cache.integration.CompletionListener)}  )}
 * will propagate an exception from a {@link javax.cache.integration.CacheLoader}.
 */
@Test
public void shouldPropagateExceptionUsingLoadAll() throws Exception {
    FailingCacheLoader<String, String> cacheLoader = new FailingCacheLoader<>();
    cacheLoaderServer.setCacheLoader(cacheLoader);
    HashSet<String> keys = new HashSet<>();
    keys.add("gudday");
    keys.add("hello");
    keys.add("howdy");
    keys.add("bonjour");
    CompletionListenerFuture future = new CompletionListenerFuture();
    cache.loadAll(keys, false, future);
    // wait for the load to complete
    try {
        future.get();
        // new since TCK 1.1
        fail();
    } catch (ExecutionException e) {
        assertThat(e.getCause(), instanceOf(CacheLoaderException.class));
    }
}
Also used : ExecutionException(java.util.concurrent.ExecutionException) CompletionListenerFuture(javax.cache.integration.CompletionListenerFuture) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 27 with CompletionListenerFuture

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

the class CacheLoaderWithoutReadThroughTest method shouldNotLoadMultipleNullEntriesUsingLoadAll.

/**
 * Ensure that {@link Cache#loadAll(java.util.Set, boolean, javax.cache.integration.CompletionListener)}
 * won't load <code>null</code> entries.
 */
@Test
public void shouldNotLoadMultipleNullEntriesUsingLoadAll() throws Exception {
    NullValueCacheLoader<String, String> cacheLoader = new NullValueCacheLoader<>();
    cacheLoaderServer.setCacheLoader(cacheLoader);
    HashSet<String> keys = new HashSet<>();
    keys.add("gudday");
    keys.add("hello");
    keys.add("howdy");
    keys.add("bonjour");
    CompletionListenerFuture future = new CompletionListenerFuture();
    cache.loadAll(keys, false, future);
    // wait for the load to complete
    future.get();
    assertThat(future.isDone(), is(true));
    for (String key : keys) {
        assertThat(cache.containsKey(key), is(false));
    }
}
Also used : CompletionListenerFuture(javax.cache.integration.CompletionListenerFuture) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 28 with CompletionListenerFuture

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

the class CacheLoaderWithoutReadThroughTest method shouldLoadMultipleExistingEntryUsingLoadAll.

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

Example 29 with CompletionListenerFuture

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

the class CacheLoaderWriterTest 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 or written.
 */
@Test
public void shouldLoadSingleExistingEntryUsingLoadAll() throws Exception {
    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(recordingCacheLoader.getLoadCount(), is(1));
    assertThat(recordingCacheLoader.hasLoaded(key), is(true));
    // ensure nothing has been written
    assertThat(recordingCacheWriter.getWriteCount(), is(1L));
    assertThat(recordingCacheWriter.getDeleteCount(), is(0L));
}
Also used : CompletionListenerFuture(javax.cache.integration.CompletionListenerFuture) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 30 with CompletionListenerFuture

use of javax.cache.integration.CompletionListenerFuture in project ignite by apache.

the class IgniteCacheExpiryStoreLoadSelfTest method testLoadAllWithExpiry.

/**
 * @throws Exception If failed.
 */
@Test
public void testLoadAllWithExpiry() throws Exception {
    IgniteCache<Integer, Integer> cache = ignite(0).<Integer, Integer>cache(DEFAULT_CACHE_NAME).withExpiryPolicy(new CreatedExpiryPolicy(new Duration(MILLISECONDS, TIME_TO_LIVE)));
    Set<Integer> keys = new HashSet<>();
    keys.add(primaryKey(jcache(0)));
    keys.add(primaryKey(jcache(1)));
    keys.add(primaryKey(jcache(2)));
    CompletionListenerFuture fut = new CompletionListenerFuture();
    cache.loadAll(keys, false, fut);
    fut.get();
    assertEquals(3, cache.size(CachePeekMode.PRIMARY));
    Thread.sleep(TIME_TO_LIVE + WAIT_TIME);
    assertEquals(0, cache.size());
}
Also used : Duration(javax.cache.expiry.Duration) CreatedExpiryPolicy(javax.cache.expiry.CreatedExpiryPolicy) CompletionListenerFuture(javax.cache.integration.CompletionListenerFuture) HashSet(java.util.HashSet) Test(org.junit.Test) GridCacheAbstractSelfTest(org.apache.ignite.internal.processors.cache.GridCacheAbstractSelfTest)

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