private void assertMapSize(Map<?, ?> map, int size) { assertEquals(size, map.size()); if (size > 0) { assertFalse(map.isEmpty()); } else { assertTrue(map.isEmpty()); } assertCollectionSize(map.keySet(), size); assertCollectionSize(map.entrySet(), size); assertCollectionSize(map.values(), size); }
public void testSize_populated() { for (LoadingCache<Object, Object> cache : caches()) { // don't let the entries get GCed List<Entry<Object, Object>> warmed = warmUp(cache); assertEquals(WARMUP_SIZE, cache.size()); assertMapSize(cache.asMap(), WARMUP_SIZE); checkValidState(cache); } }
public void testPutIfAbsent_populated() { for (LoadingCache<Object, Object> cache : caches()) { // don't let the entries get GCed List<Entry<Object, Object>> warmed = warmUp(cache); for (int i = WARMUP_MIN; i < WARMUP_MAX; i++) { Entry<Object, Object> entry = warmed.get(i - WARMUP_MIN); Object newValue = new Object(); assertSame(entry.getValue(), cache.asMap().putIfAbsent(entry.getKey(), newValue)); Object newKey = new Object(); assertNull(cache.asMap().putIfAbsent(newKey, entry.getValue())); // this getUnchecked() call shouldn't be a cache miss; verified below assertEquals(entry.getValue(), cache.getUnchecked(entry.getKey())); assertEquals(entry.getValue(), cache.getUnchecked(newKey)); // don't let the new entry get GCed warmed.add(entryOf(newKey, entry.getValue())); } assertEquals(WARMUP_SIZE, cache.stats().missCount()); checkValidState(cache); } }
private void assertCollectionSize(Collection<?> collection, int size) { assertEquals(size, collection.size()); if (size > 0) { assertFalse(collection.isEmpty()); } else { assertTrue(collection.isEmpty()); } assertEquals(size, Iterables.size(collection)); assertEquals(size, Iterators.size(collection.iterator())); } }
public void testRemove_byKeyAndValue() { for (LoadingCache<Object, Object> cache : caches()) { // don't let the entries get GCed List<Entry<Object, Object>> warmed = warmUp(cache); for (int i = WARMUP_MIN; i < WARMUP_MAX; i++) { Object key = warmed.get(i - WARMUP_MIN).getKey(); Object value = warmed.get(i - WARMUP_MIN).getValue(); assertFalse(cache.asMap().remove(key, -1)); assertTrue(cache.asMap().remove(key, value)); assertFalse(cache.asMap().remove(key, -1)); assertFalse(cache.asMap().containsKey(key)); } checkEmpty(cache); } }
public void testReplace_populated() { for (LoadingCache<Object, Object> cache : caches()) { // don't let the entries get GCed List<Entry<Object, Object>> warmed = warmUp(cache); for (int i = WARMUP_MIN; i < WARMUP_MAX; i++) { Entry<Object, Object> entry = warmed.get(i - WARMUP_MIN); Object newValue = new Object(); assertSame(entry.getValue(), cache.asMap().replace(entry.getKey(), newValue)); assertTrue(cache.asMap().replace(entry.getKey(), newValue, entry.getValue())); Object newKey = new Object(); assertNull(cache.asMap().replace(newKey, entry.getValue())); assertFalse(cache.asMap().replace(newKey, entry.getValue(), newValue)); // this getUnchecked() call shouldn't be a cache miss; verified below assertEquals(entry.getValue(), cache.getUnchecked(entry.getKey())); assertFalse(cache.asMap().containsKey(newKey)); } assertEquals(WARMUP_SIZE, cache.stats().missCount()); checkValidState(cache); } }
public void testRemove_byKey() { for (LoadingCache<Object, Object> cache : caches()) { // don't let the entries get GCed List<Entry<Object, Object>> warmed = warmUp(cache); for (int i = WARMUP_MIN; i < WARMUP_MAX; i++) { Entry<Object, Object> entry = warmed.get(i - WARMUP_MIN); Object key = entry.getKey(); assertEquals(entry.getValue(), cache.asMap().remove(key)); assertNull(cache.asMap().remove(key)); assertFalse(cache.asMap().containsKey(key)); } checkEmpty(cache); } }
public void testPutAll_populated() { for (LoadingCache<Object, Object> cache : caches()) { // don't let the entries get GCed List<Entry<Object, Object>> warmed = warmUp(cache); Object newKey = new Object(); Object newValue = new Object(); cache.asMap().putAll(ImmutableMap.of(newKey, newValue)); // this getUnchecked() call shouldn't be a cache miss; verified below assertEquals(newValue, cache.getUnchecked(newKey)); assertEquals(WARMUP_SIZE, cache.stats().missCount()); checkValidState(cache); } }
public void testWriteThroughEntry() { for (LoadingCache<Object, Object> cache : caches()) { cache.getUnchecked(1); Entry<Object, Object> entry = Iterables.getOnlyElement(cache.asMap().entrySet()); cache.invalidate(1); assertEquals(0, cache.size()); entry.setValue(3); assertEquals(1, cache.size()); assertEquals(3, cache.getIfPresent(1)); checkValidState(cache); try { entry.setValue(null); fail(); } catch (NullPointerException expected) { } checkValidState(cache); } }
/** Most of the tests in this class run against every one of these caches. */ private Iterable<LoadingCache<Object, Object>> caches() { // lots of different ways to configure a LoadingCache CacheBuilderFactory factory = cacheFactory(); return Iterables.transform( factory.buildAllPermutations(), new Function<CacheBuilder<Object, Object>, LoadingCache<Object, Object>>() { @Override public LoadingCache<Object, Object> apply(CacheBuilder<Object, Object> builder) { return builder.recordStats().build(identityLoader()); } }); }
/** * Returns the entries that were added to the map, so they won't fall out of a map with weak or * soft references until the caller drops the reference to the returned entries. */ private List<Entry<Object, Object>> warmUp( LoadingCache<Object, Object> cache, int minimum, int maximum) { List<Entry<Object, Object>> entries = Lists.newArrayList(); for (int i = minimum; i < maximum; i++) { Object key = i; Object value = cache.getUnchecked(key); entries.add(entryOf(key, value)); } return entries; }
public void testContainsKey_found() { for (LoadingCache<Object, Object> cache : caches()) { // don't let the entries get GCed List<Entry<Object, Object>> warmed = warmUp(cache); for (int i = WARMUP_MIN; i < WARMUP_MAX; i++) { Entry<Object, Object> entry = warmed.get(i - WARMUP_MIN); assertTrue(cache.asMap().containsKey(entry.getKey())); assertTrue(cache.asMap().containsValue(entry.getValue())); // this getUnchecked() call shouldn't be a cache miss; verified below assertEquals(entry.getValue(), cache.getUnchecked(entry.getKey())); } assertEquals(WARMUP_SIZE, cache.stats().missCount()); checkValidState(cache); } }
public void testReplace_populated() { for (LoadingCache<Object, Object> cache : caches()) { // don't let the entries get GCed List<Entry<Object, Object>> warmed = warmUp(cache); for (int i = WARMUP_MIN; i < WARMUP_MAX; i++) { Entry<Object, Object> entry = warmed.get(i - WARMUP_MIN); Object newValue = new Object(); assertSame(entry.getValue(), cache.asMap().replace(entry.getKey(), newValue)); assertTrue(cache.asMap().replace(entry.getKey(), newValue, entry.getValue())); Object newKey = new Object(); assertNull(cache.asMap().replace(newKey, entry.getValue())); assertFalse(cache.asMap().replace(newKey, entry.getValue(), newValue)); // this getUnchecked() call shouldn't be a cache miss; verified below assertEquals(entry.getValue(), cache.getUnchecked(entry.getKey())); assertFalse(cache.asMap().containsKey(newKey)); } assertEquals(WARMUP_SIZE, cache.stats().missCount()); checkValidState(cache); } }
public void testRemove_byKey() { for (LoadingCache<Object, Object> cache : caches()) { // don't let the entries get GCed List<Entry<Object, Object>> warmed = warmUp(cache); for (int i = WARMUP_MIN; i < WARMUP_MAX; i++) { Entry<Object, Object> entry = warmed.get(i - WARMUP_MIN); Object key = entry.getKey(); assertEquals(entry.getValue(), cache.asMap().remove(key)); assertNull(cache.asMap().remove(key)); assertFalse(cache.asMap().containsKey(key)); } checkEmpty(cache); } }
private void assertCollectionSize(Collection<?> collection, int size) { assertEquals(size, collection.size()); if (size > 0) { assertFalse(collection.isEmpty()); } else { assertTrue(collection.isEmpty()); } assertEquals(size, Iterables.size(collection)); assertEquals(size, Iterators.size(collection.iterator())); } }
public void testPutAll_populated() { for (LoadingCache<Object, Object> cache : caches()) { // don't let the entries get GCed @SuppressWarnings("unused") List<Entry<Object, Object>> warmed = warmUp(cache); Object newKey = new Object(); Object newValue = new Object(); cache.asMap().putAll(ImmutableMap.of(newKey, newValue)); // this getUnchecked() call shouldn't be a cache miss; verified below assertEquals(newValue, cache.getUnchecked(newKey)); assertEquals(WARMUP_SIZE, cache.stats().missCount()); checkValidState(cache); } }
public void testWriteThroughEntry() { for (LoadingCache<Object, Object> cache : caches()) { cache.getUnchecked(1); Entry<Object, Object> entry = Iterables.getOnlyElement(cache.asMap().entrySet()); cache.invalidate(1); assertEquals(0, cache.size()); entry.setValue(3); assertEquals(1, cache.size()); assertEquals(3, cache.getIfPresent(1)); checkValidState(cache); try { entry.setValue(null); fail(); } catch (NullPointerException expected) { } checkValidState(cache); } }
/** * Most of the tests in this class run against every one of these caches. */ private Iterable<LoadingCache<Object, Object>> caches() { // lots of different ways to configure a LoadingCache CacheBuilderFactory factory = cacheFactory(); return Iterables.transform(factory.buildAllPermutations(), new Function<Caffeine<Object, Object>, LoadingCache<Object, Object>>() { @Override public LoadingCache<Object, Object> apply( Caffeine<Object, Object> builder) { return CaffeinatedGuava.build(builder.recordStats(), identityLoader()); } }); }
/** * Returns the entries that were added to the map, so they won't fall out of a map with weak or * soft references until the caller drops the reference to the returned entries. */ private List<Map.Entry<Object, Object>> warmUp( LoadingCache<Object, Object> cache, int minimum, int maximum) { List<Map.Entry<Object, Object>> entries = Lists.newArrayList(); for (int i = minimum; i < maximum; i++) { Object key = i; Object value = cache.getUnchecked(key); entries.add(entryOf(key, value)); } return entries; }
public void testRemove_byKeyAndValue() { for (LoadingCache<Object, Object> cache : caches()) { // don't let the entries get GCed List<Entry<Object, Object>> warmed = warmUp(cache); for (int i = WARMUP_MIN; i < WARMUP_MAX; i++) { Object key = warmed.get(i - WARMUP_MIN).getKey(); Object value = warmed.get(i - WARMUP_MIN).getValue(); assertFalse(cache.asMap().remove(key, -1)); assertTrue(cache.asMap().remove(key, value)); assertFalse(cache.asMap().remove(key, -1)); assertFalse(cache.asMap().containsKey(key)); } checkEmpty(cache); } }