@Override protected void insert(Cache<Number, CharSequence> testCache, Map<Number, CharSequence> entries) { testCache.putAll(entries); } }
@Test public void putAll() { Map<Integer, String> vals = new HashMap<>(); vals.put(1, "a"); vals.put(2, "b"); cache.putAll(vals); changesOf(0, 0, 2, 0); vals.put(1, "c"); vals.put(2, "d"); vals.put(3, "e"); cache.putAll(vals); changesOf(0, 0, 3, 0); // FIXME: No way to track update correctly in OnHeapStore.compute }
@SuppressWarnings({ "rawtypes", "unchecked" }) @Test public void testPutAllWithWriterException() throws Exception { doThrow(new Exception("Mock Exception: cannot write 1")).when(cacheLoaderWriter).writeAll(ArgumentMatchers.<Iterable>any()); Map<Integer, String> values = new HashMap<>(); values.put(1, "one"); values.put(2, "two"); try { testCache.putAll(values); fail("expected CacheWritingException"); } catch (CacheWritingException ex) { // expected } } }
@Test public void testBulkOps() throws Exception { List<Cache<Long, String>> caches = new ArrayList<>(); caches.add(CACHE1); caches.add(CACHE2); Map<Long, String> entriesMap = new HashMap<>(); entriesMap.put(1L, "one"); entriesMap.put(2L, "two"); entriesMap.put(3L, "three"); entriesMap.put(4L, "four"); entriesMap.put(5L, "five"); entriesMap.put(6L, "six"); caches.forEach(cache -> cache.putAll(entriesMap)); CLUSTER.getClusterControl().terminateActive(); Set<Long> keySet = entriesMap.keySet(); caches.forEach(cache -> { Map<Long, String> all = cache.getAll(keySet); assertThat(all.get(1L), is("one")); assertThat(all.get(2L), is("two")); assertThat(all.get(3L), is("three")); assertThat(all.get(4L), is("four")); assertThat(all.get(5L), is("five")); assertThat(all.get(6L), is("six")); }); }
@Test public void testSimplePutAllWithEviction() throws Exception { Cache<Number, CharSequence> testCache = cacheManager.createCache("testCache", CacheConfigurationBuilder.newCacheConfigurationBuilder(Number.class, CharSequence.class, heap(2)) .build()); Map<Integer, String> values = new HashMap<>(); values.put(1, "one"); values.put(2, "two"); values.put(3, "three"); values.put(4, "four"); testCache.putAll(values); int count = 0; for (@SuppressWarnings("unused") Cache.Entry<Number, CharSequence> entry : testCache) { count++; } assertThat(count, is(2)); }
@Test public void testSimplePutAll() throws Exception { Cache<Number, CharSequence> testCache = cacheManager.createCache("testCache", newCacheConfigurationBuilder(Number.class, CharSequence.class, heap(10))); Map<Integer, String> values = new HashMap<>(); values.put(1, "one"); values.put(2, "two"); values.put(3, "three"); testCache.putAll(values); assertThat(testCache.get(1), Matchers.<CharSequence>equalTo("one")); assertThat(testCache.get(2), Matchers.<CharSequence>equalTo("two")); assertThat(testCache.get(3), Matchers.<CharSequence>equalTo("three")); }
@Test public void testClear() throws Exception { List<Cache<Long, String>> caches = new ArrayList<>(); caches.add(CACHE1); caches.add(CACHE2); Map<Long, String> entriesMap = new HashMap<>(); entriesMap.put(1L, "one"); entriesMap.put(2L, "two"); entriesMap.put(3L, "three"); entriesMap.put(4L, "four"); entriesMap.put(5L, "five"); entriesMap.put(6L, "six"); caches.forEach(cache -> cache.putAll(entriesMap)); Set<Long> keySet = entriesMap.keySet(); caches.forEach(cache -> { Map<Long, String> all = cache.getAll(keySet); assertThat(all.get(1L), is("one")); assertThat(all.get(2L), is("two")); assertThat(all.get(3L), is("three")); assertThat(all.get(4L), is("four")); assertThat(all.get(5L), is("five")); assertThat(all.get(6L), is("six")); }); CACHE1.clear(); CACHE2.clear(); CLUSTER.getClusterControl().terminateActive(); keySet.forEach(x -> assertThat(CACHE1.get(x), nullValue())); keySet.forEach(x -> assertThat(CACHE2.get(x), nullValue())); }
@Test(timeout=180000) @Ignore //TODO: FIXME: FIX THIS RANDOMLY FAILING TEST public void testBulkOps() throws Exception { List<Cache<Long, BlobValue>> caches = new ArrayList<>(); caches.add(CACHE1); caches.add(CACHE2); Map<Long, BlobValue> entriesMap = new HashMap<>(); Random random = new Random(); LongStream longStream = random.longs(1000); longStream.forEach(x -> entriesMap.put(x, new BlobValue())); caches.forEach(cache -> cache.putAll(entriesMap)); Set<Long> keySet = entriesMap.keySet(); Set<Long> readKeysByCache2BeforeFailOver = new HashSet<>(); keySet.forEach(x -> { if (CACHE2.get(x) != null) { readKeysByCache2BeforeFailOver.add(x); } }); CLUSTER.getClusterControl().terminateActive(); Set<Long> readKeysByCache1AfterFailOver = new HashSet<>(); keySet.forEach(x -> { if (CACHE1.get(x) != null) { readKeysByCache1AfterFailOver.add(x); } }); assertThat(readKeysByCache2BeforeFailOver.size(), greaterThanOrEqualTo(readKeysByCache1AfterFailOver.size())); readKeysByCache1AfterFailOver.stream().filter(readKeysByCache2BeforeFailOver::contains).forEach(y -> assertThat(CACHE2.get(y), notNullValue())); }
@Test(timeout=180000) public void testClear() throws Exception { List<Cache<Long, BlobValue>> caches = new ArrayList<>(); caches.add(CACHE1); caches.add(CACHE2); Map<Long, BlobValue> entriesMap = new HashMap<>(); Random random = new Random(); LongStream longStream = random.longs(1000); longStream.forEach(x -> entriesMap.put(x, new BlobValue())); caches.forEach(cache -> cache.putAll(entriesMap)); Set<Long> keySet = entriesMap.keySet(); Set<Long> readKeysByCache2BeforeFailOver = new HashSet<>(); keySet.forEach(x -> { if (CACHE2.get(x) != null) { readKeysByCache2BeforeFailOver.add(x); } }); CACHE1.clear(); CLUSTER.getClusterControl().terminateActive(); if (cacheConsistency == Consistency.STRONG) { readKeysByCache2BeforeFailOver.forEach(x -> assertThat(CACHE2.get(x), nullValue())); } else { readKeysByCache2BeforeFailOver.forEach(x -> assertThat(CACHE1.get(x), nullValue())); } }
Map<Long, BlobValue> map = random.longs().limit(JOB_SIZE).collect(HashMap::new, (hashMap, x) -> hashMap.put(x, new BlobValue()), HashMap::putAll); futures.add(executorService.submit(() -> { cache.putAll(map); universalSet.addAll(map.keySet()); }));
@Test public void testPutAll_with_cache_writer_that_throws_exception() throws Exception { CacheConfigurationBuilder cacheConfigurationBuilder = CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, String.class, heap(100)); CacheLoaderWriterProvider cacheLoaderWriterProvider = mock(CacheLoaderWriterProvider.class); CacheLoaderWriter cacheLoaderWriterThatThrows = mock(CacheLoaderWriter.class); doThrow(new RuntimeException("We should not have called .write() but .writeAll()")).when(cacheLoaderWriterThatThrows).write(ArgumentMatchers .any(), ArgumentMatchers.any()); doThrow(new Exception("Simulating an exception from the cache writer")).when(cacheLoaderWriterThatThrows).writeAll(ArgumentMatchers.any(Iterable.class)); when(cacheLoaderWriterProvider.createCacheLoaderWriter(anyString(), ArgumentMatchers.any(CacheConfiguration.class))).thenReturn(cacheLoaderWriterThatThrows); CacheManagerBuilder<CacheManager> managerBuilder = CacheManagerBuilder.newCacheManagerBuilder().using(cacheLoaderWriterProvider); CacheConfiguration<String, String> cacheConfiguration = cacheConfigurationBuilder.withLoaderWriter(cacheLoaderWriterThatThrows).build(); CacheManager cacheManager = managerBuilder.withCache("myCache", cacheConfiguration).build(true); Cache<String, String> myCache = cacheManager.getCache("myCache", String.class, String.class); HashMap<String, String> stringStringHashMap = new HashMap<>(); for (int i = 0; i < 3; i++) { stringStringHashMap.put("key" + i, "value" + i); } // the call to putAll try { myCache.putAll(stringStringHashMap); fail(); } catch (BulkCacheWritingException bcwe) { assertThat(bcwe.getFailures().size(), is(3)); assertThat(bcwe.getSuccesses().size(), is(0)); } }
@Test public void testBulkOps() { TestCacheLoaderWriter loaderWriter = new TestCacheLoaderWriter(); CacheConfiguration<Long, String> cacheConfiguration = getCacheConfiguration(loaderWriter); CacheManager cacheManager = CacheManagerBuilder .newCacheManagerBuilder() .with(cluster(CLUSTER_URI).autoCreate()) .withCache("cache-1", cacheConfiguration) .build(true); Cache<Long, String> cache = cacheManager.getCache("cache-1", Long.class, String.class); Map<Long, String> mappings = new HashMap<>(); for (int i = 1; i <= 5; i++) { mappings.put((long) i, "" + i); } cache.putAll(mappings); assertThat(loaderWriter.storeMap.keySet(), containsInAnyOrder(mappings.keySet().toArray())); cache.clear(); Map<Long, String> loadedData = cache.getAll(mappings.keySet()); assertThat(mappings.keySet(), containsInAnyOrder(loadedData.keySet().toArray())); cache.removeAll(mappings.keySet()); assertThat(loaderWriter.storeMap.isEmpty(), is(true)); }
myCache.putAll(stringStringHashMap);
@Ignore("This is currently unstable as if the clear does not complete before the failover," + "there is no future operation that will trigger the code in ClusterTierActiveEntity.invokeServerStoreOperation" + "dealing with in-flight invalidation reconstructed from reconnect data") @Test(timeout=180000) public void testClear() throws Exception { List<Future<?>> futures = new ArrayList<>(); Set<Long> universalSet = ConcurrentHashMap.newKeySet(); caches.forEach(cache -> { for (int i = 0; i < NUM_OF_THREADS; i++) { Map<Long, BlobValue> map = random.longs().limit(JOB_SIZE).collect(HashMap::new, (hashMap, x) -> hashMap.put(x, new BlobValue()), HashMap::putAll); futures.add(executorService.submit(() -> { cache.putAll(map); universalSet.addAll(map.keySet()); })); } }); drainTasks(futures); universalSet.forEach(x -> { CACHE1.get(x); CACHE2.get(x); }); Future<?> clearFuture = executorService.submit(() -> CACHE1.clear()); CLUSTER.getClusterControl().terminateActive(); clearFuture.get(); universalSet.forEach(x -> assertThat(CACHE2.get(x), nullValue())); }
myCache.putAll(stringStringHashMap);
entriesMap.put(2L, "two"); entriesMap.put(3L, "three"); cache1.putAll(entriesMap);
@Test public void testPutAll_without_cache_writer() throws Exception { CacheConfigurationBuilder cacheConfigurationBuilder = CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, String.class, heap(100)); CacheConfiguration<String, String> cacheConfiguration = cacheConfigurationBuilder.build(); CacheManagerBuilder<CacheManager> managerBuilder = CacheManagerBuilder.newCacheManagerBuilder(); CacheManager cacheManager = managerBuilder.withCache("myCache", cacheConfiguration).build(true); Cache<String, String> myCache = cacheManager.getCache("myCache", String.class, String.class); HashMap<String, String> stringStringHashMap = new HashMap<>(); for (int i = 0; i < 3; i++) { stringStringHashMap.put("key" + i, "value" + i); } // the call to putAll myCache.putAll(stringStringHashMap); for (int i = 0; i < 3; i++) { assertThat(myCache.get("key" + i), is("value" + i)); } }
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(asSet(2)); // one remove changesOf(1, 4, 3, 2); cacheStatistics.clear(); changesOf(-1, -4, -3, -2); }
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(asSet(2)); // one remove changesOf(1, 4, 3, 2); tierStatistics.clear(); changesOf(-1, -4, -3, -2); }