Skip to content

Commit

Permalink
delete redundant tests and add missing ones
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-manes committed Jun 9, 2024
1 parent 7e3fe4e commit ca69499
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
import com.github.benmanes.caffeine.cache.testing.CheckNoStats;
import com.github.benmanes.caffeine.cache.testing.RemovalListeners.RejectingRemovalListener;
import com.github.benmanes.caffeine.testing.Int;
import com.google.common.collect.HashBiMap;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Maps;
import com.google.common.collect.Range;
Expand Down Expand Up @@ -325,6 +326,46 @@ public void getAll_weigherFails_async(AsyncCache<Int, Int> cache, CacheContext c
.hasSize(context.absentKeys().size());
}

@Test(dataProvider = "caches")
@CacheSpec(population = Population.FULL,
maximumSize = Maximum.FULL, weigher = CacheWeigher.MOCKITO)
public void getAll_weigherFails_newEntries(Cache<Int, Int> cache, CacheContext context) {
when(context.weigher().weigh(any(), any())).thenThrow(IllegalStateException.class);
assertThrows(IllegalStateException.class, () ->
cache.getAll(context.absentKeys(), keys -> HashBiMap.create(context.original()).inverse()));
assertThat(cache).containsExactlyEntriesIn(context.original());
if (context.isCaffeine()) {
assertThat(logEvents()
.withMessage("Exception thrown during asynchronous load")
.withThrowable(IllegalStateException.class)
.withLevel(WARN)
.exclusively())
.hasSize(context.isAsync() ? context.original().size() : 0);
}
}

@Test(dataProvider = "caches")
@CacheSpec(population = Population.FULL,
maximumSize = Maximum.FULL, weigher = CacheWeigher.MOCKITO)
public void getAll_weigherFails_newEntries_async(
AsyncCache<Int, Int> cache, CacheContext context) {
when(context.weigher().weigh(any(), any())).thenThrow(IllegalStateException.class);
var future = new CompletableFuture<Map<Int, Int>>();
var result = cache.getAll(context.absentKeys(), (keys, executor) -> future);
future.complete(HashBiMap.create(context.original()).inverse());

assertThat(context).stats().failures(1);
assertThat(result).failsWith(CompletionException.class)
.hasCauseThat().isInstanceOf(IllegalStateException.class);
assertThat(cache).containsExactlyEntriesIn(context.original());
assertThat(logEvents()
.withMessage("Exception thrown during asynchronous load")
.withThrowable(IllegalStateException.class)
.withLevel(WARN)
.exclusively())
.hasSize(context.original().size());
}

@Test(dataProvider = "caches")
@CacheSpec(population = Population.EMPTY,
maximumSize = Maximum.FULL, weigher = CacheWeigher.NEGATIVE)
Expand Down Expand Up @@ -563,49 +604,17 @@ public void replaceConditionally_weigherFails_absent(
public void replaceConditionally_weigherFails_presentKey(
Cache<Int, Int> cache, CacheContext context, Eviction<Int, Int> eviction) {
when(context.weigher().weigh(any(), any())).thenThrow(IllegalStateException.class);
assertThrows(IllegalStateException.class, () -> cache.asMap().replace(
context.firstKey(), context.original().get(context.firstKey()), context.absentValue()));
assertThat(cache).containsExactlyEntriesIn(context.original());
assertThat(eviction.weightOf(context.firstKey())).hasValue(1);
}

@Test(dataProvider = "caches")
@CacheSpec(population = Population.FULL,
maximumSize = Maximum.FULL, weigher = CacheWeigher.MOCKITO)
public void replaceConditionally_weigherFails_presentKey_async(
AsyncCache<Int, Int> cache, CacheContext context) {
when(context.weigher().weigh(any(), any())).thenThrow(IllegalStateException.class);
var future = new CompletableFuture<Int>();
cache.asMap().replace(context.firstKey(), cache.getIfPresent(context.firstKey()), future);
future.complete(context.absentValue());
assertThat(context).stats().failures(1);
assertThat(cache).doesNotContainKey(context.absentKey());
assertThat(logEvents()
.withMessage("Exception thrown during asynchronous load")
.withThrowable(IllegalStateException.class)
.withLevel(WARN)
.exclusively())
.hasSize(1);
}

@Test(dataProvider = "caches")
@CacheSpec(population = Population.FULL,
maximumSize = Maximum.FULL, weigher = CacheWeigher.MOCKITO)
public void replaceConditionally_weigherFails_presentKeyAndValue(
Cache<Int, Int> cache, CacheContext context, Eviction<Int, Int> eviction) {
when(context.weigher().weigh(any(), any())).thenThrow(IllegalStateException.class);
assertThrows(IllegalStateException.class, () -> {
assertThrows(IllegalStateException.class, () ->
cache.asMap().replace(context.firstKey(),
context.original().get(context.firstKey()), context.absentValue());
});
context.original().get(context.firstKey()), context.absentValue()));
assertThat(cache).containsExactlyEntriesIn(context.original());
assertThat(eviction.weightOf(context.firstKey())).hasValue(1);
}

@Test(dataProvider = "caches")
@CacheSpec(population = Population.FULL,
maximumSize = Maximum.FULL, weigher = CacheWeigher.MOCKITO)
public void replaceConditionally_weigherFails_presentKeyAndValue_async(
public void replaceConditionally_weigherFails_presentKey_async(
AsyncCache<Int, Int> cache, CacheContext context) {
when(context.weigher().weigh(any(), any())).thenThrow(IllegalStateException.class);
var future = new CompletableFuture<Int>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
import com.github.benmanes.caffeine.cache.testing.CheckNoStats;
import com.github.benmanes.caffeine.testing.ConcurrentTestHarness;
import com.github.benmanes.caffeine.testing.Int;
import com.google.common.collect.HashBiMap;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Maps;
import com.google.common.testing.SerializableTester;
Expand Down Expand Up @@ -412,6 +413,14 @@ public void getAll_expiryFails(Cache<Int, Int> cache, CacheContext context) {
cache.getAll(context.absentKeys(), keys -> Maps.toMap(keys, Int::negate)));
context.ticker().advance(Duration.ofHours(-1));
assertThat(cache).containsExactlyEntriesIn(context.original());
if (context.isCaffeine()) {
assertThat(logEvents()
.withMessage("Exception thrown during asynchronous load")
.withThrowable(ExpirationException.class)
.withLevel(WARN)
.exclusively())
.hasSize(context.isAsync() ? context.absentKeys().size() : 0);
}
}

@Test(dataProvider = "caches")
Expand All @@ -434,6 +443,47 @@ public void getAll_expiryFails_async(AsyncCache<Int, Int> cache, CacheContext co
.hasSize(context.absentKeys().size());
}

@Test(dataProvider = "caches")
@CacheSpec(population = Population.FULL, expiry = CacheExpiry.MOCKITO)
public void getAll_expiryFails_newEntries(Cache<Int, Int> cache, CacheContext context) {
context.ticker().advance(Duration.ofHours(1));
when(context.expiry().expireAfterCreate(any(), any(), anyLong()))
.thenThrow(ExpirationException.class);
assertThrows(ExpirationException.class, () ->
cache.getAll(context.absentKeys(), keys -> HashBiMap.create(context.original()).inverse()));
context.ticker().advance(Duration.ofHours(-1));
assertThat(cache).containsExactlyEntriesIn(context.original());
if (context.isCaffeine()) {
assertThat(logEvents()
.withMessage("Exception thrown during asynchronous load")
.withThrowable(ExpirationException.class)
.withLevel(WARN)
.exclusively())
.hasSize(context.isAsync() ? context.original().size() : 0);
}
}

@Test(dataProvider = "caches")
@CacheSpec(population = Population.FULL, expiry = CacheExpiry.MOCKITO)
public void getAll_expiryFails_newEntries_async(
AsyncCache<Int, Int> cache, CacheContext context) {
context.ticker().advance(Duration.ofHours(1));
when(context.expiry().expireAfterCreate(any(), any(), anyLong()))
.thenThrow(ExpirationException.class);
var future = new CompletableFuture<Map<Int, Int>>();
var result = cache.getAll(context.absentKeys(), (keys, executor) -> future);
future.complete(HashBiMap.create(context.original()).inverse());
assertThat(result).failsWith(CompletionException.class)
.hasCauseThat().isInstanceOf(ExpirationException.class);
assertThat(cache).containsExactlyEntriesIn(context.original());
assertThat(logEvents()
.withMessage("Exception thrown during asynchronous load")
.withThrowable(ExpirationException.class)
.withLevel(WARN)
.exclusively())
.hasSize(context.original().size());
}

@Test(dataProvider = "caches")
@CacheSpec(population = Population.FULL, expiry = CacheExpiry.MOCKITO)
public void getAllPresent_expiryFails(Cache<Int, Int> cache, CacheContext context) {
Expand Down

0 comments on commit ca69499

Please sign in to comment.