public void testDrainRecencyQueueOnWrite() { for (CacheBuilder<Object, Object> builder : allEvictingMakers()) { LocalCache<Object, Object> map = makeLocalCache(builder.concurrencyLevel(1)); Segment<Object, Object> segment = map.segments[0]; if (segment.recencyQueue != DISCARDING_QUEUE) { Object keyOne = new Object(); Object valueOne = new Object(); Object keyTwo = new Object(); Object valueTwo = new Object(); map.put(keyOne, valueOne); assertTrue(segment.recencyQueue.isEmpty()); for (int i = 0; i < DRAIN_THRESHOLD / 2; i++) { map.get(keyOne); } assertFalse(segment.recencyQueue.isEmpty()); map.put(keyTwo, valueTwo); assertTrue(segment.recencyQueue.isEmpty()); } } }
map.get(keyOne); assertFalse(segment.recencyQueue.isEmpty()); map.get(keyOne); assertFalse(segment.recencyQueue.isEmpty());
public void testRemoveEntry() { LocalCache<Object, Object> map = makeLocalCache( createCacheBuilder() .concurrencyLevel(1) .initialCapacity(1) .maximumSize(SMALL_MAX_SIZE) .expireAfterWrite(99999, SECONDS) .removalListener(countingRemovalListener())); Segment<Object, Object> segment = map.segments[0]; AtomicReferenceArray<ReferenceEntry<Object, Object>> table = segment.table; assertEquals(1, table.length()); Object key = new Object(); Object value = new Object(); int hash = map.hash(key); DummyEntry<Object, Object> entry = createDummyEntry(key, hash, value, null); // remove absent assertFalse(segment.removeEntry(entry, hash, RemovalCause.COLLECTED)); // remove live segment.recordWrite(entry, 1, map.ticker.read()); table.set(0, entry); segment.count = 1; assertTrue(segment.removeEntry(entry, hash, RemovalCause.COLLECTED)); assertNotificationEnqueued(map, key, value, hash); assertTrue(map.removalNotificationQueue.isEmpty()); assertFalse(segment.accessQueue.contains(entry)); assertFalse(segment.writeQueue.contains(entry)); assertEquals(0, segment.count); assertNull(table.get(0)); }
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 testDefaults() { LocalCache<Object, Object> map = makeLocalCache(createCacheBuilder()); assertSame(Strength.STRONG, map.keyStrength); assertSame(Strength.STRONG, map.valueStrength); assertSame(map.keyStrength.defaultEquivalence(), map.keyEquivalence); assertSame(map.valueStrength.defaultEquivalence(), map.valueEquivalence); assertEquals(0, map.expireAfterAccessNanos); assertEquals(0, map.expireAfterWriteNanos); assertEquals(0, map.refreshNanos); assertEquals(CacheBuilder.UNSET_INT, map.maxWeight); assertSame(EntryFactory.STRONG, map.entryFactory); assertSame(CacheBuilder.NullListener.INSTANCE, map.removalListener); assertSame(DISCARDING_QUEUE, map.removalNotificationQueue); assertSame(NULL_TICKER, map.ticker); assertEquals(4, map.concurrencyLevel); // concurrency level assertThat(map.segments).hasLength(4); // initial capacity / concurrency level assertEquals(16 / map.segments.length, map.segments[0].table.length()); assertFalse(map.evictsBySize()); assertFalse(map.expires()); assertFalse(map.expiresAfterWrite()); assertFalse(map.expiresAfterAccess()); assertFalse(map.refreshes()); }
public void testRemoveIfWithConcurrentModification() { LocalCache<Integer, Integer> map = makeLocalCache(createCacheBuilder().concurrencyLevel(1).initialCapacity(1)); map.put(1, 1); map.put(2, 1); map.put(3, 1); map.entrySet() .removeIf( entry -> { if (entry.getValue().equals(1)) { map.put(entry.getKey(), 2); return true; } else { return false; } }); assertEquals(3, map.size()); assertFalse(map.containsValue(1)); }
public void testDrainKeyReferenceQueueOnWrite() { for (CacheBuilder<Object, Object> builder : allKeyValueStrengthMakers()) { LocalCache<Object, Object> map = makeLocalCache(builder.concurrencyLevel(1)); if (map.usesKeyReferences()) { Segment<Object, Object> segment = map.segments[0]; Object keyOne = new Object(); int hashOne = map.hash(keyOne); Object valueOne = new Object(); Object keyTwo = new Object(); Object valueTwo = new Object(); map.put(keyOne, valueOne); ReferenceEntry<Object, Object> entry = segment.getEntry(keyOne, hashOne); @SuppressWarnings("unchecked") Reference<Object> reference = (Reference) entry; reference.enqueue(); map.put(keyTwo, valueTwo); assertFalse(map.containsKey(keyOne)); assertFalse(map.containsValue(valueOne)); assertNull(map.get(keyOne)); assertEquals(1, map.size()); assertNull(segment.keyReferenceQueue.poll()); } } }
public void testDrainValueReferenceQueueOnWrite() { for (CacheBuilder<Object, Object> builder : allKeyValueStrengthMakers()) { LocalCache<Object, Object> map = makeLocalCache(builder.concurrencyLevel(1)); if (map.usesValueReferences()) { Segment<Object, Object> segment = map.segments[0]; Object keyOne = new Object(); int hashOne = map.hash(keyOne); Object valueOne = new Object(); Object keyTwo = new Object(); Object valueTwo = new Object(); map.put(keyOne, valueOne); ReferenceEntry<Object, Object> entry = segment.getEntry(keyOne, hashOne); ValueReference<Object, Object> valueReference = entry.getValueReference(); @SuppressWarnings("unchecked") Reference<Object> reference = (Reference) valueReference; reference.enqueue(); map.put(keyTwo, valueTwo); assertFalse(map.containsKey(keyOne)); assertFalse(map.containsValue(valueOne)); assertNull(map.get(keyOne)); assertEquals(1, map.size()); assertNull(segment.valueReferenceQueue.poll()); } } }
public void testDrainKeyReferenceQueueOnRead() { for (CacheBuilder<Object, Object> builder : allKeyValueStrengthMakers()) { LocalCache<Object, Object> map = makeLocalCache(builder.concurrencyLevel(1)); if (map.usesKeyReferences()) { Segment<Object, Object> segment = map.segments[0]; Object keyOne = new Object(); int hashOne = map.hash(keyOne); Object valueOne = new Object(); Object keyTwo = new Object(); map.put(keyOne, valueOne); ReferenceEntry<Object, Object> entry = segment.getEntry(keyOne, hashOne); @SuppressWarnings("unchecked") Reference<Object> reference = (Reference) entry; reference.enqueue(); for (int i = 0; i < SMALL_MAX_SIZE; i++) { map.get(keyTwo); } assertFalse(map.containsKey(keyOne)); assertFalse(map.containsValue(valueOne)); assertNull(map.get(keyOne)); assertEquals(0, map.size()); assertNull(segment.keyReferenceQueue.poll()); } } }
public void testDrainValueReferenceQueueOnRead() { for (CacheBuilder<Object, Object> builder : allKeyValueStrengthMakers()) { LocalCache<Object, Object> map = makeLocalCache(builder.concurrencyLevel(1)); if (map.usesValueReferences()) { Segment<Object, Object> segment = map.segments[0]; Object keyOne = new Object(); int hashOne = map.hash(keyOne); Object valueOne = new Object(); Object keyTwo = new Object(); map.put(keyOne, valueOne); ReferenceEntry<Object, Object> entry = segment.getEntry(keyOne, hashOne); ValueReference<Object, Object> valueReference = entry.getValueReference(); @SuppressWarnings("unchecked") Reference<Object> reference = (Reference) valueReference; reference.enqueue(); for (int i = 0; i < SMALL_MAX_SIZE; i++) { map.get(keyTwo); } assertFalse(map.containsKey(keyOne)); assertFalse(map.containsValue(valueOne)); assertNull(map.get(keyOne)); assertEquals(0, map.size()); assertNull(segment.valueReferenceQueue.poll()); } } }
assertFalse(segment.reclaimKey(entryOne, hashOne)); assertEquals(0, listener.getCount()); table.set(0, entryOne); assertFalse(segment.reclaimKey(entryTwo, hashTwo)); assertEquals(0, listener.getCount()); table.set(0, entryTwo); assertFalse(segment.reclaimKey(entryThree, hashThree)); assertEquals(0, listener.getCount()); assertSame(valueOne, listener.getLastEvictedValue()); assertTrue(map.removalNotificationQueue.isEmpty()); assertFalse(segment.accessQueue.contains(entryOne)); assertFalse(segment.writeQueue.contains(entryOne)); assertEquals(0, segment.count); assertNull(table.get(0));
LoadingValueReference<Object, Object> valueReference = (LoadingValueReference) newEntry.getValueReference(); assertFalse(valueReference.futureValue.isDone()); startSignal.countDown();
assertFalse(segment.reclaimValue(key, hash, valueRef)); assertSame(value, listener.getLastEvictedValue()); assertTrue(map.removalNotificationQueue.isEmpty()); assertFalse(segment.accessQueue.contains(entry)); assertFalse(segment.writeQueue.contains(entry)); assertEquals(0, segment.count); assertNull(table.get(0)); DummyValueReference<Object, Object> otherValueRef = DummyValueReference.create(value); entry.setValueReference(otherValueRef); assertFalse(segment.reclaimValue(key, hash, valueRef)); assertEquals(1, listener.getCount()); assertTrue(segment.reclaimValue(key, hash, otherValueRef));
assertFalse(segment.replace(key, hash, oldValue, newValue)); assertEquals(0, segment.count); assertFalse(segment.replace(key, hash, oldValue, newValue)); assertEquals(1, segment.count); assertSame(newValue, segment.get(key, hash)); assertSame(oldValue, segment.get(key, hash)); oldValueRef.clear(); assertFalse(segment.replace(key, hash, oldValue, newValue)); assertEquals(0, segment.count); assertNull(segment.get(key, hash));
assertFalse(segment.removeLoadingValue(key, hash, valueRef)); DummyValueReference<Object, Object> otherValueRef = DummyValueReference.create(value); entry.setValueReference(otherValueRef); assertFalse(segment.removeLoadingValue(key, hash, valueRef)); entry.setValueReference(valueRef); assertTrue(segment.removeLoadingValue(key, hash, valueRef));
assertFalse(map.containsKey(key)); assertNull(map.get(key)); segment.expireEntries(ticker.read()); assertFalse(map.containsKey(key)); assertNull(map.get(key)); assertTrue(segment.accessQueue.isEmpty());
assertEquals(1, segment.count); assertSame(oldValue, segment.get(key, hash)); assertFalse(segment.remove(key, hash, newValue)); assertEquals(1, segment.count); assertSame(oldValue, segment.get(key, hash)); assertFalse(segment.remove(key, hash, oldValue)); assertEquals(0, segment.count); assertNull(segment.get(key, hash));
assertFalse(one.asMap().isEmpty()); LocalManualCache<Object, Object> two = SerializableTester.reserialize(one); assertEquals(0, two.size());
assertFalse(one.asMap().isEmpty()); LocalLoadingCache<Object, Object> two = SerializableTester.reserialize(one); assertEquals(0, two.size());
assertFalse(segment.storeLoadedValue(key, hash, valueRef, value2)); assertEquals(1, segment.count); assertSame(value, segment.get(key, hash));