use of javax.cache.integration.CompletionListenerFuture 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();
}
}
use of javax.cache.integration.CompletionListenerFuture in project cache2k by cache2k.
the class CacheLoaderTest method shouldLoadMultipleNonExistingEntryUsingLoadAll.
/**
* Ensure that {@link Cache#loadAll(Set, boolean, javax.cache.integration.CompletionListener)} )}
* for multiple non-existing entries will be loaded.
*/
@Test
public void shouldLoadMultipleNonExistingEntryUsingLoadAll() 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");
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(cacheLoader.getLoadCount(), is(keys.size()));
for (String key : keys) {
assertThat(cache.get(key), is(equalTo(key)));
assertThat(cacheLoader.hasLoaded(key), is(true));
}
}
use of javax.cache.integration.CompletionListenerFuture in project cache2k by cache2k.
the class CacheLoaderTest method shouldLoadMultipleExistingEntryUsingLoadAll.
/**
* Ensure that {@link Cache#loadAll(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));
}
}
use of javax.cache.integration.CompletionListenerFuture in project cache2k by cache2k.
the class CacheLoaderTest method shouldNotLoadWithNullKeysUsingLoadAll.
/**
* 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 shouldNotLoadWithNullKeysUsingLoadAll() throws Exception {
RecordingCacheLoader<String> cacheLoader = new RecordingCacheLoader<String>();
cacheLoaderServer.setCacheLoader(cacheLoader);
try {
CompletionListenerFuture future = new CompletionListenerFuture();
cache.loadAll(null, false, future);
fail("Expected a NullPointerException");
} catch (NullPointerException e) {
// SKIP: expected
} finally {
assertThat(cacheLoader.getLoadCount(), is(0));
}
}
use of javax.cache.integration.CompletionListenerFuture in project cache2k by cache2k.
the class CacheLoaderTest 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));
}
}
Aggregations