public void testDrainValueReferenceQueueOnWrite() { for (MapMaker maker : allWeakValueStrengthMakers()) { MapMakerInternalMap<Object, Object, ?, ?> map = makeMap(maker.concurrencyLevel(1)); if (maker.getValueStrength() == Strength.WEAK) { 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); @SuppressWarnings("unchecked") WeakValueEntry<Object, Object, ?> entry = (WeakValueEntry<Object, Object, ?>) segment.getEntry(keyOne, hashOne); WeakValueReference<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.getValueReferenceQueueForTesting().poll()); } } }
public void testDrainValueReferenceQueueOnRead() { for (MapMaker maker : allWeakValueStrengthMakers()) { MapMakerInternalMap<Object, Object, ?, ?> map = makeMap(maker.concurrencyLevel(1)); if (maker.getValueStrength() == Strength.WEAK) { 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); @SuppressWarnings("unchecked") WeakValueEntry<Object, Object, ?> entry = (WeakValueEntry<Object, Object, ?>) segment.getEntry(keyOne, hashOne); WeakValueReference<Object, Object, ?> valueReference = entry.getValueReference(); @SuppressWarnings("unchecked") Reference<Object> reference = (Reference) valueReference; reference.enqueue(); for (int i = 0; i < SMALL_MAX_SIZE; i++) { Object unused = map.get(keyTwo); } assertFalse(map.containsKey(keyOne)); assertFalse(map.containsValue(valueOne)); assertNull(map.get(keyOne)); assertEquals(0, map.size()); assertNull(segment.getValueReferenceQueueForTesting().poll()); } } }