Skip to content

Cache's computeIfPresent discards if error'd entry #5438

@ben-manes

Description

@ben-manes

It appears that we still have a bug in the compute logic. The test case below should retain the previous value when the compute throws an error. Instead the failed compute is left in the cache and the prior value can be read. When the next compute occurs, it treats the exception as a null old value, ignores the mappingFunction, and removes the entry.

public void testComputeIfPresent_error() {
  var cache = CacheBuilder.newBuilder().build();
  cache.put(key, "1");
  try {
    cache.asMap().computeIfPresent(key, (key, value) -> { throw new Error(); });
  } catch (Error e) {}
  assertEquals("1", cache.getIfPresent(key));
  assertEquals("2", cache.asMap().computeIfPresent(key, (k, v) -> "2")); // fails; removes mapping
}

Caffeine's test suite runs against a Guava shim in order to verify backwards compatibility. I was trying to update that to delegate to Guava's compute implementations after #5406.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions