@Test public void factory() { Cache<Integer, Integer> cache = cacheManager.getCache("guice"); Map<Integer, Integer> result = cache.getAll(ImmutableSet.of(1, 2, 3)); assertThat(result, is(ImmutableMap.of(1, 1, 2, 2, 3, 3))); }
/** * @param threads Threads count. * @param recsPerThread initial records per thread. * @param restartedCache cache to obtain data from. */ private void verifyByChunk(int threads, int recsPerThread, Cache<Integer, HugeIndexedObject> restartedCache) { int verifyChunk = 100; int totalRecsToVerify = recsPerThread * threads; int chunks = totalRecsToVerify / verifyChunk; for (int c = 0; c < chunks; c++) { Set<Integer> keys = new TreeSet<>(); for (int i = 0; i < verifyChunk; i++) keys.add(i + c * verifyChunk); Map<Integer, HugeIndexedObject> values = restartedCache.getAll(keys); for (Map.Entry<Integer, HugeIndexedObject> next : values.entrySet()) { Integer key = next.getKey(); int actVal = values.get(next.getKey()).iVal; int i = key; Assert.assertEquals(i, actVal); if (i % 1000 == 0) X.println(" >> Verified: " + i); } } }
@Test public void test_getCacheHitsAndMisses() { heapCache.put("key1", "value1"); heapCache.put("key3", "value3"); heapCache.put("key5", "value5"); HashSet<String> keys = new HashSet<>(5); for (int i = 1; i <= 5; i++) { keys.add("key" + i); } heapCache.getAll(keys); assertThat(heapStatistics.getCacheHits(), is(3L)); assertThat(heapStatistics.getCacheMisses(), is(2L)); }
private void innerClear() { cache.get(1); // one miss cache.getAll(asSet(1, 2, 3)); // 3 misses cache.put(1, "a"); // one put cache.put(1, "b"); // one put and update cache.putAll(Collections.singletonMap(2, "b")); // 1 put cache.get(1); // one hit cache.remove(1); // one remove cache.removeAll(); // one remove changesOf(1, 4, 3, 2); cacheStatistics.clear(); changesOf(-1, -4, -3, -2); } }
@Override void execute(Cache<Object, Object> cache, Exchange exchange) { exchange.getIn().setBody( cache.getAll( exchange.getIn().getHeader(JCacheConstants.KEYS, Set.class)) ); } },
@Override public Map<K, V> getAll(Set<? extends K> keys) { Map<K0, V0> m = cache.getAll(compactBoundedKeys(keys)); return expandMap(m); }
@Test public void getAll_NullKey() { HashSet<Long> keys = new HashSet<Long>(); keys.add(1L); keys.add(null); keys.add(2L); try { cache.getAll(keys); fail("should have thrown an exception - null key in keys not allowed"); } catch (NullPointerException e) { //expected } }
@Test public void getAll_Null() { try { cache.getAll(null); fail("should have thrown an exception - null keys not allowed"); } catch (NullPointerException e) { //good } }
/** * Ensure that a {@link javax.cache.integration.CacheLoader} that returns <code>null</code> values * aren't placed in the cache. */ @Test public void shouldNotLoadNullValues() { NullValueCacheLoader<String, String> cacheLoader = new NullValueCacheLoader<>(); cacheLoaderServer.setCacheLoader(cacheLoader); //construct a set of keys HashSet<String> keys = new HashSet<String>(); keys.add("gudday"); keys.add("hello"); keys.add("howdy"); keys.add("bonjour"); //attempt to get the keys Map<String, String> map = cache.getAll(keys); //nothing should have been loaded assertThat(map.size(), is(0)); }
/** * Ensure that a {@link CacheLoader} that returns <code>null</code> values * aren't placed in the cache. */ @Test public void shouldNotLoadNullValues() { NullValueCacheLoader<String, String> cacheLoader = new NullValueCacheLoader<>(); cacheLoaderServer.setCacheLoader(cacheLoader); //construct a set of keys HashSet<String> keys = new HashSet<String>(); keys.add("gudday"); keys.add("hello"); keys.add("howdy"); keys.add("bonjour"); //attempt to get the keys Map<String, String> map = cache.getAll(keys); //nothing should have been loaded assertThat(map.size(), is(0)); }
/** * Ensure that a {@link javax.cache.integration.CacheLoader} that returns <code>null</code> entries * aren't placed in the cache. */ @Test public void shouldNotLoadNullEntries() { NullValueCacheLoader<String, String> nullCacheLoader = new NullValueCacheLoader<>(); cacheLoaderServer.setCacheLoader(nullCacheLoader); //construct a set of keys HashSet<String> keys = new HashSet<String>(); keys.add("gudday"); keys.add("hello"); keys.add("howdy"); keys.add("bonjour"); //attempt to get the keys Map<String, String> map = cache.getAll(keys); //nothing should have been loaded assertThat(map.size(), is(0)); }
@Test public void getAll() { ArrayList<Long> keysInCache = new ArrayList<Long>(); keysInCache.add(1L); keysInCache.add(2L); for (Long k : keysInCache) { cache.put(k, "value" + k); } HashSet<Long> keysToGet = new HashSet<Long>(); keysToGet.add(2L); keysToGet.add(3L); HashSet<Long> keysExpected = new HashSet<Long>(); keysExpected.add(2L); Map<Long, String> map = cache.getAll(keysToGet); assertEquals("size", keysExpected.size(), map.size()); for (Long key : keysExpected) { assertTrue(map.containsKey(key)); assertEquals("key : key=" + key, cache.get(key), map.get(key)); assertEquals("value: key=" + key, "value" + key, map.get(key)); } }
@Test public void getAll_Closed() { cache.close(); try { cache.getAll(null); fail("should have thrown an exception - cache closed"); } catch (IllegalStateException e) { //good } }
/** * Ensure that a {@link javax.cache.integration.CacheLoader} that returns <code>null</code> values * aren't placed in the cache or written */ @Test public void shouldNotLoadNullValues() { NullValueCacheLoader<String, String> cacheLoader = new NullValueCacheLoader<>(); cacheLoaderServer.setCacheLoader(cacheLoader); //construct a set of keys HashSet<String> keys = new HashSet<String>(); keys.add("gudday"); keys.add("hello"); keys.add("howdy"); keys.add("bonjour"); //attempt to get the keys Map<String, String> map = cache.getAll(keys); //nothing should have been loaded assertThat(map.size(), is(0)); //ensure nothing has been written assertThat(recordingCacheWriter.getWriteCount(), is(0L)); assertThat(recordingCacheWriter.getDeleteCount(), is(0L)); }
/** * Ensure that a {@link Cache#getAll(java.util.Set)} will not load entries. */ @Test public void shouldNotLoadUsingGetAll() { RecordingCacheLoader<String> cacheLoader = new RecordingCacheLoader<String>(); cacheLoaderServer.setCacheLoader(cacheLoader); //construct a set of keys HashSet<String> keys = new HashSet<String>(); keys.add("gudday"); keys.add("hello"); keys.add("howdy"); keys.add("bonjour"); //get the keys Map<String, String> map = cache.getAll(keys); //assert that the map content is as expected assertThat(map.size(), is(0)); for (String key : keys) { assertThat(map.containsKey(key), is(false)); } //assert that the loader state is as expected assertThat(cacheLoader.getLoadCount(), is(0)); for (String key : keys) { assertThat(cacheLoader.hasLoaded(key), is(false)); } //attempting to load the same keys should not result in loading cache.getAll(keys); assertThat(cacheLoader.getLoadCount(), is(0)); }
/** * Ensure that a {@link Cache#getAll(java.util.Set)} using one or more * <code>null</code> keys will not load anything. */ @Test public void shouldNotLoadWithNullKeyUsingGetAll() { RecordingCacheLoader<String> cacheLoader = new RecordingCacheLoader<String>(); cacheLoaderServer.setCacheLoader(cacheLoader); //construct a set of keys HashSet<String> keys = new HashSet<String>(); keys.add("gudday"); keys.add("hello"); keys.add("howdy"); keys.add("bonjour"); keys.add(null); //attempt to get the keys try { Map<String, String> map = cache.getAll(keys); fail("Should have thrown a NullPointerException"); } catch (NullPointerException e) { //assert that nothing was actually loaded assertThat(cacheLoader.getLoadCount(), is(0)); for (String key : keys) { assertThat(cacheLoader.hasLoaded(key), is(false)); } } }
/** * Ensure that a {@link Cache#getAll(java.util.Set)} using one or more * <code>null</code> keys will not load anything. */ @Test public void shouldNotLoadWithNullKeyUsingGetAll() { RecordingCacheLoader<String> cacheLoader = new RecordingCacheLoader<String>(); cacheLoaderServer.setCacheLoader(cacheLoader); //construct a set of keys HashSet<String> keys = new HashSet<String>(); keys.add("gudday"); keys.add("hello"); keys.add("howdy"); keys.add("bonjour"); keys.add(null); //attempt to get the keys try { Map<String, String> map = cache.getAll(keys); fail("Should have thrown a NullPointerException"); } catch (NullPointerException e) { //assert that nothing was actually loaded assertThat(cacheLoader.getLoadCount(), is(0)); for (String key : keys) { assertThat(cacheLoader.hasLoaded(key), is(false)); } } }
@Test public void shouldNotInvokeWriteThroughCallingGetAll() { int NUM_KEYS = 4; Set<Integer> keys = new HashSet<>(); for (int i = 1; i <= NUM_KEYS; i++) { keys.add(i); } // getAll returns null case. assertEquals(0, cacheWriter.getWriteCount()); assertEquals(0, cacheWriter.getDeleteCount()); Map<Integer, String> map = cache.getAll(keys); assertTrue(map.size() == 0); assertEquals(0, cacheWriter.getWriteCount()); assertEquals(0, cacheWriter.getDeleteCount()); // getAll returns non-null case. for (Integer key : keys) { cache.put(key, "value" + key); } assertEquals(NUM_KEYS, cacheWriter.getWriteCount()); assertEquals(0, cacheWriter.getDeleteCount()); map = cache.getAll(keys); assertEquals(keys.size(), map.size()); assertEquals(NUM_KEYS, cacheWriter.getWriteCount()); assertEquals(0, cacheWriter.getDeleteCount()); }
@Test public void getAllShouldCallGetExpiryForAccessedEntry() { 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); Set<Integer> keys = new HashSet<>(); keys.add(1); keys.add(2); // when getting a non-existent entry, getExpiryForAccessedEntry is not called. cache.getAll(keys); assertThat(expiryPolicy.getCreationCount(), is(0)); assertThat(expiryPolicy.getAccessCount(), is(0)); assertThat(expiryPolicy.getUpdatedCount(), is(0)); cache.put(1, 1); cache.put(2, 2); assertThat(expiryPolicy.getCreationCount(), greaterThanOrEqualTo(2)); assertThat(expiryPolicy.getAccessCount(), is(0)); assertThat(expiryPolicy.getUpdatedCount(), is(0)); expiryPolicy.resetCount(); // when getting an existing entry, getExpiryForAccessedEntry is called. cache.get(1); cache.get(2); assertThat(expiryPolicy.getCreationCount(), is(0)); assertThat(expiryPolicy.getAccessCount(), greaterThanOrEqualTo(2)); assertThat(expiryPolicy.getUpdatedCount(), is(0)); expiryPolicy.resetCount(); }