Skip to content

Commit

Permalink
Improved Guava view (entrySet#iterator) compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-manes committed Apr 23, 2016
1 parent 61dc09a commit f11108a
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,8 @@ public void evict_weighted_async(AsyncLoadingCache<Integer, Integer> cache,

ready.set(true);
Awaits.await().untilTrue(done);
Awaits.await().until(() -> eviction.weightedSize().getAsLong(), is(10L));
Awaits.await().until(() -> cache.synchronous().estimatedSize(), is(2L));
Awaits.await().until(() -> eviction.weightedSize().getAsLong(), is(10L));
assertThat(context, hasRemovalNotifications(context, 1, RemovalCause.SIZE));
verifyWriter(context, (verifier, writer) -> verifier.deletions(1, RemovalCause.SIZE));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import com.google.common.cache.CacheStats;
import com.google.common.collect.ForwardingCollection;
import com.google.common.collect.ForwardingConcurrentMap;
import com.google.common.collect.ForwardingIterator;
import com.google.common.collect.ForwardingMapEntry;
import com.google.common.collect.ForwardingSet;
import com.google.common.collect.ImmutableMap;
Expand Down Expand Up @@ -199,17 +200,23 @@ public ConcurrentMap<K, V> asMap() {
}
@Override
public Iterator<Entry<K, V>> iterator() {
return delegate().stream().map(entry -> {
Entry<K, V> e = new ForwardingMapEntry<K, V>() {
@Override public V setValue(V value) {
throw new UnsupportedOperationException();
}
@Override protected Entry<K, V> delegate() {
return entry;
}
};
return e;
}).iterator();
Iterator<Entry<K, V>> iterator = delegate().iterator();
return new ForwardingIterator<Entry<K, V>>() {
@Override public Entry<K, V> next() {
Entry<K, V> entry = delegate().next();
return new ForwardingMapEntry<K, V>() {
@Override public V setValue(V value) {
throw new UnsupportedOperationException();
}
@Override protected Entry<K, V> delegate() {
return entry;
}
};
}
@Override protected Iterator<Entry<K, V>> delegate() {
return iterator;
}
};
}
@Override protected Set<Entry<K, V>> delegate() {
return cache.asMap().entrySet();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright 2015 Ben Manes. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.benmanes.caffeine.cache;

import com.github.benmanes.caffeine.guava.CaffeinatedGuava;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

/**
* Guava testlib map tests for the {@link Cache#asMap()} view.
*
* @author ben.manes@gmail.com (Ben Manes)
*/
public final class CaffeineMapTests extends TestCase {

public static Test suite() throws Exception {
TestSuite suite = new TestSuite();
addGuavaViewTests(suite);
addUnboundedTests(suite);
addBoundedTests(suite);
return suite;
}

private static void addUnboundedTests(TestSuite suite) throws Exception {
suite.addTest(MapTestFactory.suite("UnboundedCache", () -> {
Cache<String, String> cache = Caffeine.newBuilder().build();
return cache.asMap();
}));
suite.addTest(MapTestFactory.suite("UnboundedAsyncCache", () -> {
AsyncLoadingCache<String, String> cache = Caffeine.newBuilder().buildAsync(key -> null);
return cache.synchronous().asMap();
}));
}

private static void addBoundedTests(TestSuite suite) throws Exception {
suite.addTest(MapTestFactory.suite("BoundedCache", () -> {
Cache<String, String> cache = Caffeine.newBuilder().maximumSize(Long.MAX_VALUE).build();
return cache.asMap();
}));
suite.addTest(MapTestFactory.suite("BoundedAsyncCache", () -> {
AsyncLoadingCache<String, String> cache = Caffeine.newBuilder()
.maximumSize(Long.MAX_VALUE)
.buildAsync(key -> null);
return cache.synchronous().asMap();
}));
}

private static void addGuavaViewTests(TestSuite suite) throws Exception {
suite.addTest(MapTestFactory.suite("GuavaView", () -> {
com.google.common.cache.Cache<String, String> cache = CaffeinatedGuava.build(
Caffeine.newBuilder().maximumSize(Long.MAX_VALUE));
return cache.asMap();
}));
suite.addTest(MapTestFactory.suite("GuavaLoadingView", () -> {
com.google.common.cache.Cache<String, String> cache = CaffeinatedGuava.build(
Caffeine.newBuilder().maximumSize(Long.MAX_VALUE), key -> null);
return cache.asMap();
}));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.util.Map;
import java.util.Map.Entry;
import java.util.function.Supplier;

import com.google.common.collect.testing.MapTestSuiteBuilder;
import com.google.common.collect.testing.TestStringMapGenerator;
Expand All @@ -39,18 +40,15 @@ private MapTestFactory() {}
* Returns a test suite.
*
* @param name the name of the cache type under test
* @param builder the preconfigured cache builder
* @param async if the cache is asynchronous
* @param supplier the cache as a map
* @return a suite of tests
*/
protected static Test suite(String name, Caffeine<Object, Object> builder, boolean async)
protected static Test suite(String name, Supplier<Map<String, String>> supplier)
throws NoSuchMethodException, SecurityException {
return MapTestSuiteBuilder
.using(new TestStringMapGenerator() {
@Override protected Map<String, String> create(Entry<String, String>[] entries) {
Map<String, String> map = async
? builder.<String, String>buildAsync(key -> null).synchronous().asMap()
: builder.<String, String>build().asMap();
Map<String, String> map = supplier.get();
for (Entry<String, String> entry : entries) {
map.put(entry.getKey(), entry.getValue());
}
Expand All @@ -60,6 +58,7 @@ protected static Test suite(String name, Caffeine<Object, Object> builder, boole
.named(name)
.withFeatures(
MapFeature.GENERAL_PURPOSE,
MapFeature.ALLOWS_NULL_ENTRY_QUERIES,
CollectionFeature.SUPPORTS_ITERATOR_REMOVE,
CollectionSize.ANY)
.createTestSuite();
Expand Down

This file was deleted.

0 comments on commit f11108a

Please sign in to comment.