assertSame(two, one.getNextInAccessQueue());
}
}
public void testSegmentGetAndContains() {
FakeTicker ticker = new FakeTicker();
LocalCache<Object, Object> map = makeLocalCache(createCacheBuilder()
.concurrencyLevel(1)
.ticker(ticker)
.expireAfterAccess(1, TimeUnit.NANOSECONDS));
Segment<Object, Object> segment = map.segments[0];
// TODO(fry): check recency ordering
Object key = new Object();
int hash = map.hash(key);
Object value = new Object();
AtomicReferenceArray<ReferenceEntry<Object, Object>> table = segment.table;
int index = hash & (table.length() - 1);
ReferenceEntry<Object, Object> entry = map.newEntry(key, hash, null);
ValueReference<Object, Object> valueRef = map.newValueReference(entry, value, 1);
entry.setValueReference(valueRef);
assertNull(segment.get(key, hash));
// count == 0
table.set(index, entry);
assertNull(segment.get(key, hash));
assertFalse(segment.containsKey(key, hash));
assertFalse(segment.containsValue(value));
// count == 1
segment.count++;
assertSame(value, segment.get(key, hash));
assertTrue(segment.containsKey(key, hash));
assertTrue(segment.containsValue(value));
// don't see absent values now that count > 0
assertNull(segment.get(new Object(), hash));
// null key
DummyEntry<Object, Object> nullEntry = DummyEntry.create(null, hash, entry);
Object nullValue = new Object();
ValueReference<Object, Object> nullValueRef = map.newValueReference(nullEntry, nullValue, 1);
nullEntry.setValueReference(nullValueRef);
table.set(index, nullEntry);
// skip the null key
assertSame(value, segment.get(key, hash));
assertTrue(segment.containsKey(key, hash));
assertTrue(segment.containsValue(value));
assertFalse(segment.containsValue(nullValue));
// hash collision
DummyEntry<Object, Object> dummy = DummyEntry.create(new Object(), hash, entry);
Object dummyValue = new Object();
ValueReference<Object, Object> dummyValueRef = map.newValueReference(dummy, dummyValue, 1);
dummy.setValueReference(dummyValueRef);
table.set(index, dummy);
assertSame(value, segment.get(key, hash));
assertTrue(segment.containsKey(key, hash));
assertTrue(segment.containsValue(value));
assertTrue(segment.containsValue(dummyValue));
// key collision
dummy = DummyEntry.create(key, hash, entry);
dummyValue = new Object();
dummyValueRef = map.newValueReference(dummy, dummyValue, 1);
dummy.setValueReference(dummyValueRef);
table.set(index, dummy);
// returns the most recent entry
assertSame(dummyValue, segment.get(key, hash));
assertTrue(segment.containsKey(key, hash));
assertTrue(segment.containsValue(value));
assertTrue(segment.containsValue(dummyValue));
// expired
dummy.setAccessTime(ticker.read() - 2);
assertNull(segment.get(key, hash));
assertFalse(segment.containsKey(key, hash));
assertTrue(segment.containsValue(value));
assertFalse(segment.containsValue(dummyValue));
}