Java 类com.google.common.cache.TestingCacheLoaders.CountingLoader 实例源码

项目:guava-mock    文件:CacheLoadingTest.java   
public void testReloadAfterSimulatedValueReclamation() throws ExecutionException {
  CountingLoader countingLoader = new CountingLoader();
  LoadingCache<Object, Object> cache = CacheBuilder.newBuilder()
      .concurrencyLevel(1)
      .weakValues()
      .build(countingLoader);

  Object key = new Object();
  assertNotNull(cache.getUnchecked(key));

  CacheTesting.simulateValueReclamation(cache, key);

  // this blocks if computation can't deal with partially-collected values
  assertNotNull(cache.getUnchecked(key));
  assertEquals(1, cache.size());
  assertEquals(2, countingLoader.getCount());

  CacheTesting.simulateValueReclamation(cache, key);
  cache.refresh(key);
  checkNothingLogged();
  assertEquals(1, cache.size());
  assertEquals(3, countingLoader.getCount());
}
项目:guava-mock    文件:CacheLoadingTest.java   
public void testReloadAfterSimulatedKeyReclamation() throws ExecutionException {
  CountingLoader countingLoader = new CountingLoader();
  LoadingCache<Object, Object> cache = CacheBuilder.newBuilder()
      .concurrencyLevel(1)
      .weakKeys()
      .build(countingLoader);

  Object key = new Object();
  assertNotNull(cache.getUnchecked(key));
  assertEquals(1, cache.size());

  CacheTesting.simulateKeyReclamation(cache, key);

  // this blocks if computation can't deal with partially-collected values
  assertNotNull(cache.getUnchecked(key));
  assertEquals(2, countingLoader.getCount());

  CacheTesting.simulateKeyReclamation(cache, key);
  cache.refresh(key);
  checkNothingLogged();
  assertEquals(3, countingLoader.getCount());
}
项目:guava-mock    文件:LocalCacheTest.java   
@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());
}
项目:googles-monorepo-demo    文件:CacheLoadingTest.java   
public void testReloadAfterSimulatedValueReclamation() throws ExecutionException {
  CountingLoader countingLoader = new CountingLoader();
  LoadingCache<Object, Object> cache = CacheBuilder.newBuilder()
      .concurrencyLevel(1)
      .weakValues()
      .build(countingLoader);

  Object key = new Object();
  assertNotNull(cache.getUnchecked(key));

  CacheTesting.simulateValueReclamation(cache, key);

  // this blocks if computation can't deal with partially-collected values
  assertNotNull(cache.getUnchecked(key));
  assertEquals(1, cache.size());
  assertEquals(2, countingLoader.getCount());

  CacheTesting.simulateValueReclamation(cache, key);
  cache.refresh(key);
  checkNothingLogged();
  assertEquals(1, cache.size());
  assertEquals(3, countingLoader.getCount());
}
项目:googles-monorepo-demo    文件:CacheLoadingTest.java   
public void testReloadAfterSimulatedKeyReclamation() throws ExecutionException {
  CountingLoader countingLoader = new CountingLoader();
  LoadingCache<Object, Object> cache = CacheBuilder.newBuilder()
      .concurrencyLevel(1)
      .weakKeys()
      .build(countingLoader);

  Object key = new Object();
  assertNotNull(cache.getUnchecked(key));
  assertEquals(1, cache.size());

  CacheTesting.simulateKeyReclamation(cache, key);

  // this blocks if computation can't deal with partially-collected values
  assertNotNull(cache.getUnchecked(key));
  assertEquals(2, countingLoader.getCount());

  CacheTesting.simulateKeyReclamation(cache, key);
  cache.refresh(key);
  checkNothingLogged();
  assertEquals(3, countingLoader.getCount());
}
项目:googles-monorepo-demo    文件:LocalCacheTest.java   
@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());
}
项目:guava-libraries    文件:CacheLoadingTest.java   
public void testReloadAfterSimulatedValueReclamation() throws ExecutionException {
  CountingLoader countingLoader = new CountingLoader();
  LoadingCache<Object, Object> cache = CacheBuilder.newBuilder()
      .concurrencyLevel(1)
      .weakValues()
      .build(countingLoader);

  Object key = new Object();
  assertNotNull(cache.getUnchecked(key));

  CacheTesting.simulateValueReclamation(cache, key);

  // this blocks if computation can't deal with partially-collected values
  assertNotNull(cache.getUnchecked(key));
  assertEquals(1, cache.size());
  assertEquals(2, countingLoader.getCount());

  CacheTesting.simulateValueReclamation(cache, key);
  cache.refresh(key);
  checkNothingLogged();
  assertEquals(1, cache.size());
  assertEquals(3, countingLoader.getCount());
}
项目:guava-libraries    文件:CacheLoadingTest.java   
public void testReloadAfterSimulatedKeyReclamation() throws ExecutionException {
  CountingLoader countingLoader = new CountingLoader();
  LoadingCache<Object, Object> cache = CacheBuilder.newBuilder()
      .concurrencyLevel(1)
      .weakKeys()
      .build(countingLoader);

  Object key = new Object();
  assertNotNull(cache.getUnchecked(key));
  assertEquals(1, cache.size());

  CacheTesting.simulateKeyReclamation(cache, key);

  // this blocks if computation can't deal with partially-collected values
  assertNotNull(cache.getUnchecked(key));
  assertEquals(2, countingLoader.getCount());

  CacheTesting.simulateKeyReclamation(cache, key);
  cache.refresh(key);
  checkNothingLogged();
  assertEquals(3, countingLoader.getCount());
}
项目:guava    文件:CacheLoadingTest.java   
public void testReloadAfterSimulatedValueReclamation() throws ExecutionException {
  CountingLoader countingLoader = new CountingLoader();
  LoadingCache<Object, Object> cache =
      CacheBuilder.newBuilder().concurrencyLevel(1).weakValues().build(countingLoader);

  Object key = new Object();
  assertNotNull(cache.getUnchecked(key));

  CacheTesting.simulateValueReclamation(cache, key);

  // this blocks if computation can't deal with partially-collected values
  assertNotNull(cache.getUnchecked(key));
  assertEquals(1, cache.size());
  assertEquals(2, countingLoader.getCount());

  CacheTesting.simulateValueReclamation(cache, key);
  cache.refresh(key);
  checkNothingLogged();
  assertEquals(1, cache.size());
  assertEquals(3, countingLoader.getCount());
}
项目:guava    文件:CacheLoadingTest.java   
public void testReloadAfterSimulatedKeyReclamation() throws ExecutionException {
  CountingLoader countingLoader = new CountingLoader();
  LoadingCache<Object, Object> cache =
      CacheBuilder.newBuilder().concurrencyLevel(1).weakKeys().build(countingLoader);

  Object key = new Object();
  assertNotNull(cache.getUnchecked(key));
  assertEquals(1, cache.size());

  CacheTesting.simulateKeyReclamation(cache, key);

  // this blocks if computation can't deal with partially-collected values
  assertNotNull(cache.getUnchecked(key));
  assertEquals(2, countingLoader.getCount());

  CacheTesting.simulateKeyReclamation(cache, key);
  cache.refresh(key);
  checkNothingLogged();
  assertEquals(3, countingLoader.getCount());
}
项目:guava    文件:LocalCacheTest.java   
@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());
}
项目:guava    文件:CacheLoadingTest.java   
public void testReloadAfterSimulatedValueReclamation() throws ExecutionException {
  CountingLoader countingLoader = new CountingLoader();
  LoadingCache<Object, Object> cache =
      CacheBuilder.newBuilder().concurrencyLevel(1).weakValues().build(countingLoader);

  Object key = new Object();
  assertNotNull(cache.getUnchecked(key));

  CacheTesting.simulateValueReclamation(cache, key);

  // this blocks if computation can't deal with partially-collected values
  assertNotNull(cache.getUnchecked(key));
  assertEquals(1, cache.size());
  assertEquals(2, countingLoader.getCount());

  CacheTesting.simulateValueReclamation(cache, key);
  cache.refresh(key);
  checkNothingLogged();
  assertEquals(1, cache.size());
  assertEquals(3, countingLoader.getCount());
}
项目:guava    文件:CacheLoadingTest.java   
public void testReloadAfterSimulatedKeyReclamation() throws ExecutionException {
  CountingLoader countingLoader = new CountingLoader();
  LoadingCache<Object, Object> cache =
      CacheBuilder.newBuilder().concurrencyLevel(1).weakKeys().build(countingLoader);

  Object key = new Object();
  assertNotNull(cache.getUnchecked(key));
  assertEquals(1, cache.size());

  CacheTesting.simulateKeyReclamation(cache, key);

  // this blocks if computation can't deal with partially-collected values
  assertNotNull(cache.getUnchecked(key));
  assertEquals(2, countingLoader.getCount());

  CacheTesting.simulateKeyReclamation(cache, key);
  cache.refresh(key);
  checkNothingLogged();
  assertEquals(3, countingLoader.getCount());
}
项目:guava    文件:LocalCacheTest.java   
@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());
}
项目:guava-mock    文件:LocalCacheTest.java   
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());
}
项目:guava-mock    文件:LocalCacheTest.java   
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());
}
项目:guava-mock    文件:LocalCacheTest.java   
public void testComputePartiallyCollectedKey() throws ExecutionException {
  CacheBuilder<Object, Object> builder = createCacheBuilder().concurrencyLevel(1);
  CountingLoader loader = new CountingLoader();
  LocalCache<Object, Object> map = makeLocalCache(builder);
  Segment<Object, Object> segment = map.segments[0];
  AtomicReferenceArray<ReferenceEntry<Object, Object>> table = segment.table;
  assertEquals(0, loader.getCount());

  Object key = new Object();
  int hash = map.hash(key);
  Object value = new Object();
  int index = hash & (table.length() - 1);

  DummyEntry<Object, Object> entry = DummyEntry.create(key, hash, null);
  DummyValueReference<Object, Object> valueRef = DummyValueReference.create(value);
  entry.setValueReference(valueRef);
  table.set(index, entry);
  segment.count++;

  assertSame(value, map.get(key, loader));
  assertEquals(0, loader.getCount());
  assertEquals(1, segment.count);

  entry.clearKey();
  assertNotSame(value, map.get(key, loader));
  assertEquals(1, loader.getCount());
  assertEquals(2, segment.count);
}
项目:guava-mock    文件:LocalCacheTest.java   
public void testComputePartiallyCollectedValue() throws ExecutionException {
  CacheBuilder<Object, Object> builder = createCacheBuilder().concurrencyLevel(1);
  CountingLoader loader = new CountingLoader();
  LocalCache<Object, Object> map = makeLocalCache(builder);
  Segment<Object, Object> segment = map.segments[0];
  AtomicReferenceArray<ReferenceEntry<Object, Object>> table = segment.table;
  assertEquals(0, loader.getCount());

  Object key = new Object();
  int hash = map.hash(key);
  Object value = new Object();
  int index = hash & (table.length() - 1);

  DummyEntry<Object, Object> entry = DummyEntry.create(key, hash, null);
  DummyValueReference<Object, Object> valueRef = DummyValueReference.create(value);
  entry.setValueReference(valueRef);
  table.set(index, entry);
  segment.count++;

  assertSame(value, map.get(key, loader));
  assertEquals(0, loader.getCount());
  assertEquals(1, segment.count);

  valueRef.clear();
  assertNotSame(value, map.get(key, loader));
  assertEquals(1, loader.getCount());
  assertEquals(1, segment.count);
}
项目:googles-monorepo-demo    文件:LocalCacheTest.java   
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());
}
项目:googles-monorepo-demo    文件:LocalCacheTest.java   
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());
}
项目:googles-monorepo-demo    文件:LocalCacheTest.java   
public void testComputePartiallyCollectedKey() throws ExecutionException {
  CacheBuilder<Object, Object> builder = createCacheBuilder().concurrencyLevel(1);
  CountingLoader loader = new CountingLoader();
  LocalCache<Object, Object> map = makeLocalCache(builder);
  Segment<Object, Object> segment = map.segments[0];
  AtomicReferenceArray<ReferenceEntry<Object, Object>> table = segment.table;
  assertEquals(0, loader.getCount());

  Object key = new Object();
  int hash = map.hash(key);
  Object value = new Object();
  int index = hash & (table.length() - 1);

  DummyEntry<Object, Object> entry = DummyEntry.create(key, hash, null);
  DummyValueReference<Object, Object> valueRef = DummyValueReference.create(value);
  entry.setValueReference(valueRef);
  table.set(index, entry);
  segment.count++;

  assertSame(value, map.get(key, loader));
  assertEquals(0, loader.getCount());
  assertEquals(1, segment.count);

  entry.clearKey();
  assertNotSame(value, map.get(key, loader));
  assertEquals(1, loader.getCount());
  assertEquals(2, segment.count);
}
项目:googles-monorepo-demo    文件:LocalCacheTest.java   
public void testComputePartiallyCollectedValue() throws ExecutionException {
  CacheBuilder<Object, Object> builder = createCacheBuilder().concurrencyLevel(1);
  CountingLoader loader = new CountingLoader();
  LocalCache<Object, Object> map = makeLocalCache(builder);
  Segment<Object, Object> segment = map.segments[0];
  AtomicReferenceArray<ReferenceEntry<Object, Object>> table = segment.table;
  assertEquals(0, loader.getCount());

  Object key = new Object();
  int hash = map.hash(key);
  Object value = new Object();
  int index = hash & (table.length() - 1);

  DummyEntry<Object, Object> entry = DummyEntry.create(key, hash, null);
  DummyValueReference<Object, Object> valueRef = DummyValueReference.create(value);
  entry.setValueReference(valueRef);
  table.set(index, entry);
  segment.count++;

  assertSame(value, map.get(key, loader));
  assertEquals(0, loader.getCount());
  assertEquals(1, segment.count);

  valueRef.clear();
  assertNotSame(value, map.get(key, loader));
  assertEquals(1, loader.getCount());
  assertEquals(1, segment.count);
}
项目:guava-libraries    文件:LocalCacheTest.java   
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());
}
项目:guava-libraries    文件:LocalCacheTest.java   
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());
}
项目:guava-libraries    文件:LocalCacheTest.java   
public void testComputePartiallyCollectedKey() throws ExecutionException {
  CacheBuilder<Object, Object> builder = createCacheBuilder().concurrencyLevel(1);
  CountingLoader loader = new CountingLoader();
  LocalCache<Object, Object> map = makeLocalCache(builder);
  Segment<Object, Object> segment = map.segments[0];
  AtomicReferenceArray<ReferenceEntry<Object, Object>> table = segment.table;
  assertEquals(0, loader.getCount());

  Object key = new Object();
  int hash = map.hash(key);
  Object value = new Object();
  int index = hash & (table.length() - 1);

  DummyEntry<Object, Object> entry = DummyEntry.create(key, hash, null);
  DummyValueReference<Object, Object> valueRef = DummyValueReference.create(value);
  entry.setValueReference(valueRef);
  table.set(index, entry);
  segment.count++;

  assertSame(value, map.get(key, loader));
  assertEquals(0, loader.getCount());
  assertEquals(1, segment.count);

  entry.clearKey();
  assertNotSame(value, map.get(key, loader));
  assertEquals(1, loader.getCount());
  assertEquals(2, segment.count);
}
项目:guava-libraries    文件:LocalCacheTest.java   
public void testComputePartiallyCollectedValue() throws ExecutionException {
  CacheBuilder<Object, Object> builder = createCacheBuilder().concurrencyLevel(1);
  CountingLoader loader = new CountingLoader();
  LocalCache<Object, Object> map = makeLocalCache(builder);
  Segment<Object, Object> segment = map.segments[0];
  AtomicReferenceArray<ReferenceEntry<Object, Object>> table = segment.table;
  assertEquals(0, loader.getCount());

  Object key = new Object();
  int hash = map.hash(key);
  Object value = new Object();
  int index = hash & (table.length() - 1);

  DummyEntry<Object, Object> entry = DummyEntry.create(key, hash, null);
  DummyValueReference<Object, Object> valueRef = DummyValueReference.create(value);
  entry.setValueReference(valueRef);
  table.set(index, entry);
  segment.count++;

  assertSame(value, map.get(key, loader));
  assertEquals(0, loader.getCount());
  assertEquals(1, segment.count);

  valueRef.clear();
  assertNotSame(value, map.get(key, loader));
  assertEquals(1, loader.getCount());
  assertEquals(1, segment.count);
}
项目:guava-libraries    文件:LocalCacheTest.java   
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());
}
项目:guava    文件:LocalCacheTest.java   
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());
}
项目:guava    文件:LocalCacheTest.java   
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());
}
项目:guava    文件:LocalCacheTest.java   
public void testComputePartiallyCollectedKey() throws ExecutionException {
  CacheBuilder<Object, Object> builder = createCacheBuilder().concurrencyLevel(1);
  CountingLoader loader = new CountingLoader();
  LocalCache<Object, Object> map = makeLocalCache(builder);
  Segment<Object, Object> segment = map.segments[0];
  AtomicReferenceArray<ReferenceEntry<Object, Object>> table = segment.table;
  assertEquals(0, loader.getCount());

  Object key = new Object();
  int hash = map.hash(key);
  Object value = new Object();
  int index = hash & (table.length() - 1);

  DummyEntry<Object, Object> entry = DummyEntry.create(key, hash, null);
  DummyValueReference<Object, Object> valueRef = DummyValueReference.create(value);
  entry.setValueReference(valueRef);
  table.set(index, entry);
  segment.count++;

  assertSame(value, map.get(key, loader));
  assertEquals(0, loader.getCount());
  assertEquals(1, segment.count);

  entry.clearKey();
  assertNotSame(value, map.get(key, loader));
  assertEquals(1, loader.getCount());
  assertEquals(2, segment.count);
}
项目:guava    文件:LocalCacheTest.java   
public void testComputePartiallyCollectedValue() throws ExecutionException {
  CacheBuilder<Object, Object> builder = createCacheBuilder().concurrencyLevel(1);
  CountingLoader loader = new CountingLoader();
  LocalCache<Object, Object> map = makeLocalCache(builder);
  Segment<Object, Object> segment = map.segments[0];
  AtomicReferenceArray<ReferenceEntry<Object, Object>> table = segment.table;
  assertEquals(0, loader.getCount());

  Object key = new Object();
  int hash = map.hash(key);
  Object value = new Object();
  int index = hash & (table.length() - 1);

  DummyEntry<Object, Object> entry = DummyEntry.create(key, hash, null);
  DummyValueReference<Object, Object> valueRef = DummyValueReference.create(value);
  entry.setValueReference(valueRef);
  table.set(index, entry);
  segment.count++;

  assertSame(value, map.get(key, loader));
  assertEquals(0, loader.getCount());
  assertEquals(1, segment.count);

  valueRef.clear();
  assertNotSame(value, map.get(key, loader));
  assertEquals(1, loader.getCount());
  assertEquals(1, segment.count);
}
项目:guava    文件:LocalCacheTest.java   
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());
}
项目:guava    文件:LocalCacheTest.java   
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());
}
项目:guava    文件:LocalCacheTest.java   
public void testComputePartiallyCollectedKey() throws ExecutionException {
  CacheBuilder<Object, Object> builder = createCacheBuilder().concurrencyLevel(1);
  CountingLoader loader = new CountingLoader();
  LocalCache<Object, Object> map = makeLocalCache(builder);
  Segment<Object, Object> segment = map.segments[0];
  AtomicReferenceArray<ReferenceEntry<Object, Object>> table = segment.table;
  assertEquals(0, loader.getCount());

  Object key = new Object();
  int hash = map.hash(key);
  Object value = new Object();
  int index = hash & (table.length() - 1);

  DummyEntry<Object, Object> entry = DummyEntry.create(key, hash, null);
  DummyValueReference<Object, Object> valueRef = DummyValueReference.create(value);
  entry.setValueReference(valueRef);
  table.set(index, entry);
  segment.count++;

  assertSame(value, map.get(key, loader));
  assertEquals(0, loader.getCount());
  assertEquals(1, segment.count);

  entry.clearKey();
  assertNotSame(value, map.get(key, loader));
  assertEquals(1, loader.getCount());
  assertEquals(2, segment.count);
}
项目:guava    文件:LocalCacheTest.java   
public void testComputePartiallyCollectedValue() throws ExecutionException {
  CacheBuilder<Object, Object> builder = createCacheBuilder().concurrencyLevel(1);
  CountingLoader loader = new CountingLoader();
  LocalCache<Object, Object> map = makeLocalCache(builder);
  Segment<Object, Object> segment = map.segments[0];
  AtomicReferenceArray<ReferenceEntry<Object, Object>> table = segment.table;
  assertEquals(0, loader.getCount());

  Object key = new Object();
  int hash = map.hash(key);
  Object value = new Object();
  int index = hash & (table.length() - 1);

  DummyEntry<Object, Object> entry = DummyEntry.create(key, hash, null);
  DummyValueReference<Object, Object> valueRef = DummyValueReference.create(value);
  entry.setValueReference(valueRef);
  table.set(index, entry);
  segment.count++;

  assertSame(value, map.get(key, loader));
  assertEquals(0, loader.getCount());
  assertEquals(1, segment.count);

  valueRef.clear();
  assertNotSame(value, map.get(key, loader));
  assertEquals(1, loader.getCount());
  assertEquals(1, segment.count);
}