public void testSetExpireAfterAccess() { long duration = 42; TimeUnit unit = TimeUnit.SECONDS; LocalCache<Object, Object> map = makeLocalCache(createCacheBuilder().expireAfterAccess(duration, unit)); assertEquals(unit.toNanos(duration), map.expireAfterAccessNanos); }
public void testSetExpireAfterWrite() { long duration = 42; TimeUnit unit = TimeUnit.SECONDS; LocalCache<Object, Object> map = makeLocalCache(createCacheBuilder().expireAfterWrite(duration, unit)); assertEquals(unit.toNanos(duration), map.expireAfterWriteNanos); }
private static void checkConcurrencyLevel(int concurrencyLevel, int segmentCount) { LocalCache<Object, Object> map = makeLocalCache(createCacheBuilder().concurrencyLevel(concurrencyLevel)); assertThat(map.segments).hasLength(segmentCount); }
public void testSetRefresh() { long duration = 42; TimeUnit unit = TimeUnit.SECONDS; LocalCache<Object, Object> map = makeLocalCache(createCacheBuilder().refreshAfterWrite(duration, unit)); assertEquals(unit.toNanos(duration), map.refreshNanos); }
public void testSetRemovalListener() { RemovalListener<Object, Object> testListener = TestingRemovalListeners.nullRemovalListener(); LocalCache<Object, Object> map = makeLocalCache(createCacheBuilder().removalListener(testListener)); assertSame(testListener, map.removalListener); }
private static void checkInitialCapacity( int concurrencyLevel, int initialCapacity, int segmentSize) { LocalCache<Object, Object> map = makeLocalCache( createCacheBuilder() .concurrencyLevel(concurrencyLevel) .initialCapacity(initialCapacity)); for (int i = 0; i < map.segments.length; i++) { assertEquals(segmentSize, map.segments[i].table.length()); } }
public void testSetTicker() { Ticker testTicker = new Ticker() { @Override public long read() { return 0; } }; LocalCache<Object, Object> map = makeLocalCache(createCacheBuilder().ticker(testTicker)); assertSame(testTicker, map.ticker); }
public void testNullParameters() throws Exception { NullPointerTester tester = new NullPointerTester(); tester.testAllPublicInstanceMethods(makeLocalCache(createCacheBuilder())); CacheLoader<Object, Object> loader = identityLoader(); tester.testAllPublicInstanceMethods(makeLocalCache(createCacheBuilder())); }
public void testSegmentPut_expand() { LocalCache<Object, Object> map = makeLocalCache(createCacheBuilder().concurrencyLevel(1).initialCapacity(1)); Segment<Object, Object> segment = map.segments[0]; assertEquals(1, segment.table.length()); int count = 1024; for (int i = 0; i < count; i++) { Object key = new Object(); Object value = new Object(); int hash = map.hash(key); assertNull(segment.put(key, hash, value, false)); assertTrue(segment.table.length() > i); } }
public void testSetSoftValues() { LocalCache<Object, Object> map = makeLocalCache(createCacheBuilder().softValues()); checkStrength(map, Strength.STRONG, Strength.SOFT); assertSame(EntryFactory.STRONG, map.entryFactory); }
public void testSetWeakValues() { LocalCache<Object, Object> map = makeLocalCache(createCacheBuilder().weakValues()); checkStrength(map, Strength.STRONG, Strength.WEAK); assertSame(EntryFactory.STRONG, map.entryFactory); }
public void testSetWeakKeys() { LocalCache<Object, Object> map = makeLocalCache(createCacheBuilder().weakKeys()); checkStrength(map, Strength.WEAK, Strength.STRONG); assertSame(EntryFactory.WEAK, map.entryFactory); }
public void testSetWeigher() { Weigher<Object, Object> testWeigher = new Weigher<Object, Object>() { @Override public int weigh(Object key, Object value) { return 42; } }; LocalCache<Object, Object> map = makeLocalCache(createCacheBuilder().maximumWeight(1).weigher(testWeigher)); assertSame(testWeigher, map.weigher); }
public void testRemoveIfWithConcurrentRemoval() { LocalCache<Integer, Integer> map = makeLocalCache(createCacheBuilder().concurrencyLevel(1).initialCapacity(1)); map.put(0, 1); map.put(1, 1); map.put(2, 1); map.entrySet() .removeIf( entry -> { assertThat(entry.getValue()).isNotNull(); map.remove((entry.getKey() + 1) % 3); return false; }); assertEquals(1, map.size()); }
public void testComputeExistingEntry() throws ExecutionException { CountingLoader loader = new CountingLoader(); LocalCache<Object, Object> map = makeLocalCache(createCacheBuilder()); assertEquals(0, loader.getCount()); Object key = new Object(); Object value = new Object(); map.put(key, value); assertEquals(value, map.get(key, loader)); assertEquals(0, loader.getCount()); }
public void testValues() { LocalCache<Object, Object> map = makeLocalCache(createCacheBuilder()); map.put("foo", "bar"); map.put("baz", "bar"); map.put("quux", "quux"); assertFalse(map.values() instanceof Set); assertTrue(map.values().removeAll(ImmutableSet.of("bar"))); assertEquals(1, map.size()); }
public void testSegmentRefresh_duplicate() throws ExecutionException { LocalCache<Object, Object> map = makeLocalCache(createCacheBuilder().concurrencyLevel(1)); Segment<Object, Object> segment = map.segments[0]; Object key = new Object(); int hash = map.hash(key); AtomicReferenceArray<ReferenceEntry<Object, Object>> table = segment.table; int index = hash & (table.length() - 1); // already loading DummyEntry<Object, Object> entry = DummyEntry.create(key, hash, null); DummyValueReference<Object, Object> valueRef = DummyValueReference.create(null); valueRef.setLoading(true); entry.setValueReference(valueRef); table.set(index, entry); assertNull(segment.refresh(key, hash, identityLoader(), false)); }
@AndroidIncompatible // Perhaps emulator clock does not update between the two get() calls? public void testComputeExpiredEntry() throws ExecutionException { CacheBuilder<Object, Object> builder = createCacheBuilder().expireAfterWrite(1, TimeUnit.NANOSECONDS); CountingLoader loader = new CountingLoader(); LocalCache<Object, Object> map = makeLocalCache(builder); assertEquals(0, loader.getCount()); Object key = new Object(); Object one = map.get(key, loader); assertEquals(1, loader.getCount()); Object two = map.get(key, loader); assertNotSame(one, two); assertEquals(2, loader.getCount()); }
public void testCompute() throws ExecutionException { CountingLoader loader = new CountingLoader(); LocalCache<Object, Object> map = makeLocalCache(createCacheBuilder()); assertEquals(0, loader.getCount()); Object key = new Object(); Object value = map.get(key, loader); assertEquals(1, loader.getCount()); assertEquals(value, map.get(key, loader)); assertEquals(1, loader.getCount()); }
public void testGetOrDefault() { LocalCache<Object, Object> map = makeLocalCache(createCacheBuilder().concurrencyLevel(1).initialCapacity(1)); map.put(1, 1); assertEquals(1, map.getOrDefault(1, 2)); assertEquals(2, map.getOrDefault(2, 2)); }