Skip to content

Commit

Permalink
Port Guava's cache weight test addition (b3855ef)
Browse files Browse the repository at this point in the history
Guava added two tests to verify the maximum weight. Only one of these
makes sense. The unported test validates segment bounding which does
not allow the cache to reach its maximum. If fails for Caffeine which
correctly holds the value as it does not split the maximum size
threshold across segments
  • Loading branch information
ben-manes committed Mar 24, 2015
1 parent cacd8e1 commit 0874855
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions guava/src/test/java/com/google/common/cache/CacheEvictionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import junit.framework.TestCase;

import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.Weigher;
import com.github.benmanes.caffeine.guava.CaffeinatedGuava;
import com.google.common.cache.CacheTesting.Receiver;
import com.google.common.cache.LocalCache.ReferenceEntry;
Expand Down Expand Up @@ -104,6 +105,56 @@ public void testEviction_maxWeight() {
CacheTesting.checkValidState(cache);
}

/**
* With an unlimited-size cache with maxWeight of 0, entries weighing 0 should still be cached.
* Entries with positive weight should not be cached (nor dump existing cache).
*/
public void testEviction_maxWeight_zero() {
CountingRemovalListener<Integer, Integer> removalListener = countingRemovalListener();
IdentityLoader<Integer> loader = identityLoader();

// Even numbers are free, odd are too expensive
Weigher<Integer, Integer> evensOnly = (k, v) -> k % 2;

LoadingCache<Integer, Integer> cache = CaffeinatedGuava.build(Caffeine.newBuilder()
.maximumWeight(0)
.weigher(evensOnly)
.removalListener(removalListener),
loader);

// 1 won't be cached
assertThat(cache.getUnchecked(1)).isEqualTo(1);
assertThat(cache.asMap().keySet()).isEmpty();

cache.cleanUp();
assertThat(removalListener.getCount()).isEqualTo(1);

// 2 will be cached
assertThat(cache.getUnchecked(2)).isEqualTo(2);
assertThat(cache.asMap().keySet()).containsExactly(2);

cache.cleanUp();
CacheTesting.checkValidState(cache);
assertThat(removalListener.getCount()).isEqualTo(1);

// 4 will be cached
assertThat(cache.getUnchecked(4)).isEqualTo(4);
assertThat(cache.asMap().keySet()).containsExactly(2, 4);

cache.cleanUp();
assertThat(removalListener.getCount()).isEqualTo(1);

// 5 won't be cached, won't dump cache
assertThat(cache.getUnchecked(5)).isEqualTo(5);
assertThat(cache.asMap().keySet()).containsExactly(2, 4);

cache.cleanUp();
assertThat(removalListener.getCount()).isEqualTo(2);

// Should we pepper more of these calls throughout the above? Where?
CacheTesting.checkValidState(cache);
}

public void testEviction_overflow() {
CountingRemovalListener<Object, Object> removalListener = countingRemovalListener();
IdentityLoader<Object> loader = identityLoader();
Expand Down

0 comments on commit 0874855

Please sign in to comment.