Skip to content

Commit

Permalink
add test coverage for minor edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-manes committed Mar 18, 2023
1 parent 0fd27bf commit 7a67449
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -986,25 +986,42 @@ public void evict_retired_victim(BoundedLocalCache<Int, Int> cache, CacheContext
@Test(dataProvider = "caches")
@CacheSpec(compute = Compute.SYNC, population = Population.EMPTY,
maximumSize = Maximum.FULL, weigher = CacheWeigher.VALUE)
public void evict_zeroWeight(BoundedLocalCache<Int, Int> cache, CacheContext context) {
public void evict_zeroWeight_candidate(BoundedLocalCache<Int, Int> cache, CacheContext context) {
for (int i = 0; i < context.maximumSize(); i++) {
var oldValue = intern(cache.put(Int.valueOf(i), Int.valueOf(1)));
assertThat(oldValue).isNull();

var value = cache.get(Int.valueOf(i - 1));
assertThat(value).isAnyOf(Int.valueOf(1), null);
assertThat(cache.put(Int.valueOf(i), Int.valueOf(1))).isNull();
}

var window = cache.put(cache.accessOrderWindowDeque().peekFirst().getKey(), Int.valueOf(0));
assertThat(window).isNotNull();
var candidate = cache.accessOrderWindowDeque().peekFirst();
cache.setWindowWeightedSize(cache.windowWeightedSize() - candidate.getWeight());
cache.setWeightedSize(cache.weightedSize() - candidate.getWeight());
candidate.setPolicyWeight(0);
candidate.setWeight(0);

cache.setMaximumSize(0);
cache.evictEntries();

assertThat(cache).containsExactly(candidate.getKey(), candidate.getValue());
assertThat(cache.weightedSize()).isEqualTo(0);
}

@Test(dataProvider = "caches")
@CacheSpec(compute = Compute.SYNC, population = Population.EMPTY,
maximumSize = Maximum.FULL, weigher = CacheWeigher.VALUE)
public void evict_zeroWeight_victim(BoundedLocalCache<Int, Int> cache, CacheContext context) {
for (int i = 0; i < context.maximumSize(); i++) {
assertThat(cache.put(Int.valueOf(i), Int.valueOf(1))).isNull();
}

var main = cache.put(cache.accessOrderProbationDeque().peekFirst().getKey(), Int.valueOf(0));
assertThat(main).isNotNull();
var victim = cache.accessOrderProbationDeque().peekFirst();
cache.setWeightedSize(cache.weightedSize() - victim.getWeight());
victim.setPolicyWeight(0);
victim.setWeight(0);

cache.setMaximumSize(0);
cache.evictEntries();
assertThat(cache).hasSize(2);

assertThat(cache.weightedSize()).isEqualTo(0);
assertThat(cache).containsExactly(victim.getKey(), victim.getValue());
}

@Test(dataProvider = "caches")
Expand Down Expand Up @@ -1446,6 +1463,25 @@ public void evict_collected_victim(BoundedLocalCache<Int, Int> cache, CacheConte
.contains(null, value).exclusively();
}

@CheckNoEvictions
@Test(dataProvider = "caches")
@CacheSpec(compute = Compute.SYNC, population = Population.FULL, maximumSize = Maximum.FULL)
public void evictEntry_absent(BoundedLocalCache<Int, Int> cache, CacheContext context) {
var absent = cache.nodeFactory.newNode(context.absentKey(), cache.keyReferenceQueue(),
context.absentValue(), cache.valueReferenceQueue(), 0, context.ticker().read());
var first = cache.nodeFactory.newNode(context.firstKey(), cache.keyReferenceQueue(),
context.absentValue(), cache.valueReferenceQueue(), 0, context.ticker().read());
absent.die();
first.die();

// This can occur due to a node being concurrently removed, but the operation is pending in the
// write buffer. The stale node is unlinked and treated as if evicted to allow further
// evictions, but the removal notification and stats are skipped as already handled.
assertThat(cache.evictEntry(absent, SIZE, context.ticker().read())).isTrue();
assertThat(cache.evictEntry(first, SIZE, context.ticker().read())).isTrue();
assertThat(cache).containsExactlyEntriesIn(context.original());
}

@Test(dataProvider = "caches")
@CacheSpec(compute = Compute.SYNC, population = Population.FULL, maximumSize = Maximum.FULL)
public void updateRecency_onGet(BoundedLocalCache<Int, Int> cache, CacheContext context) {
Expand Down
2 changes: 1 addition & 1 deletion gradle/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ ext {
]
pluginVersions = [
bnd: '6.4.0',
checkstyle: '10.9.1',
checkstyle: '10.9.2',
coveralls: '2.12.2',
dependencyCheck: '8.1.2',
errorprone: '3.0.1',
Expand Down

0 comments on commit 7a67449

Please sign in to comment.