Skip to content

Commit

Permalink
dependency upgrades and related cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-manes committed Apr 27, 2024
1 parent 4ba734a commit 6b44488
Show file tree
Hide file tree
Showing 43 changed files with 119 additions and 115 deletions.
24 changes: 10 additions & 14 deletions caffeine/build.gradle.kts
Expand Up @@ -256,6 +256,7 @@ tasks.register<JavaExec>("memoryOverhead") {
tasks.register<Stress>("stress") {
group = "Cache tests"
description = "Executes a stress test"
mainClass = "com.github.benmanes.caffeine.cache.Stresser"
classpath = sourceSets["codeGen"].runtimeClasspath + sourceSets["test"].runtimeClasspath
outputs.upToDateWhen { false }
dependsOn(tasks.compileTestJava)
Expand Down Expand Up @@ -313,23 +314,18 @@ idea.module {
scopes["PROVIDED"]!!["plus"]!!.add(configurations["javaPoetCompileClasspath"])
}

abstract class Stress @Inject constructor(@Internal val external: ExecOperations) : DefaultTask() {
@get:Input @get:Optional @get:Option(option = "workload", description = "The workload type")
abstract val operation: Property<String>
@get:InputFiles @get:Classpath
abstract val classpath: Property<FileCollection>
abstract class Stress : JavaExec() {
@Input @Option(option = "workload", description = "The workload type")
var operation: String = ""

@TaskAction
fun run() {
external.javaexec {
mainClass = "com.github.benmanes.caffeine.cache.Stresser"
classpath(this@Stress.classpath)
if (operation.isPresent) {
args("--workload", operation.get())
} else {
args("--help")
}
override fun exec() {
if (operation.isNotEmpty()) {
args("--workload", operation)
} else {
args("--help")
}
super.exec()
}
}

Expand Down
Expand Up @@ -1881,7 +1881,7 @@ public void keySet_removeAll_self(Map<Int, Int> map, CacheContext context) {
@CacheSpec(population = Population.FULL, implementation = Implementation.Caffeine)
public void keySet_removeAll_byCollection(Map<Int, Int> map, CacheContext context) {
var delegate = Sets.union(context.original().keySet(), context.absentKeys());
var keys = Mockito.mock(Collection.class);
Collection<Int> keys = Mockito.mock();
when(keys.iterator()).thenReturn(delegate.iterator());

assertThat(map.keySet().removeAll(keys)).isTrue();
Expand All @@ -1894,10 +1894,9 @@ public void keySet_removeAll_byCollection(Map<Int, Int> map, CacheContext contex
@CacheSpec(population = Population.FULL, implementation = Implementation.Caffeine)
public void keySet_removeAll_bySet(Map<Int, Int> map, CacheContext context) {
var delegate = Sets.union(context.original().keySet(), context.absentKeys());
var keys = Mockito.mock(Set.class);
Set<Int> keys = Mockito.mock();
when(keys.size()).thenReturn(delegate.size());
when(keys.contains(any())).thenAnswer(invocation ->
delegate.contains(invocation.getArgument(0)));
when(keys.contains(any())).then(invocation -> delegate.contains(invocation.getArgument(0)));

assertThat(map.keySet().removeAll(keys)).isTrue();
verify(keys).size();
Expand Down Expand Up @@ -2726,7 +2725,7 @@ public void entrySet_removeAll_self(Map<Int, Int> map, CacheContext context) {
@CacheSpec(population = Population.FULL, implementation = Implementation.Caffeine)
public void entrySet_removeAll_byCollection(Map<Int, Int> map, CacheContext context) {
var delegate = Sets.union(context.original().entrySet(), context.absent().entrySet());
var entries = Mockito.mock(Collection.class);
Collection<Map.Entry<Int, Int>> entries = Mockito.mock();
when(entries.iterator()).thenReturn(delegate.iterator());

assertThat(map.entrySet().removeAll(entries)).isTrue();
Expand All @@ -2739,10 +2738,9 @@ public void entrySet_removeAll_byCollection(Map<Int, Int> map, CacheContext cont
@CacheSpec(population = Population.FULL, implementation = Implementation.Caffeine)
public void entrySet_removeAll_bySet(Map<Int, Int> map, CacheContext context) {
var delegate = Sets.union(context.original().entrySet(), context.absent().entrySet());
var entries = Mockito.mock(Set.class);
Set<Map.Entry<Int, Int>> entries = Mockito.mock();
when(entries.size()).thenReturn(delegate.size());
when(entries.contains(any())).thenAnswer(invocation ->
delegate.contains(invocation.getArgument(0)));
when(entries.contains(any())).then(invocation -> delegate.contains(invocation.getArgument(0)));

assertThat(map.entrySet().removeAll(entries)).isTrue();
verify(entries).size();
Expand Down
Expand Up @@ -1599,7 +1599,7 @@ public void keySet_removeAll_self(AsyncCache<Int, Int> cache, CacheContext conte
@CacheSpec(population = Population.FULL, implementation = Implementation.Caffeine)
public void keySet_removeAll_byCollection(AsyncCache<Int, Int> cache, CacheContext context) {
var delegate = Sets.union(context.original().keySet(), context.absentKeys());
var keys = Mockito.mock(Collection.class);
Collection<Int> keys = Mockito.mock();
when(keys.iterator()).thenReturn(delegate.iterator());

assertThat(cache.asMap().keySet().removeAll(keys)).isTrue();
Expand All @@ -1612,10 +1612,9 @@ public void keySet_removeAll_byCollection(AsyncCache<Int, Int> cache, CacheConte
@CacheSpec(population = Population.FULL, implementation = Implementation.Caffeine)
public void keySet_removeAll_bySet(AsyncCache<Int, Int> cache, CacheContext context) {
var delegate = Sets.union(context.original().keySet(), context.absentKeys());
var keys = Mockito.mock(Set.class);
Set<Int> keys = Mockito.mock();
when(keys.size()).thenReturn(delegate.size());
when(keys.contains(any())).thenAnswer(invocation ->
delegate.contains(invocation.getArgument(0)));
when(keys.contains(any())).then(invocation -> delegate.contains(invocation.getArgument(0)));

assertThat(cache.asMap().keySet().removeAll(keys)).isTrue();
verify(keys).size();
Expand Down Expand Up @@ -2480,7 +2479,7 @@ public void entrySet_removeAll_self(AsyncCache<Int, Int> cache, CacheContext con
public void entrySet_removeAll_byCollection(AsyncCache<Int, Int> cache, CacheContext context) {
var delegate = Sets.union(cache.asMap().entrySet(),
Maps.transformValues(context.absent(), Int::asFuture).entrySet());
var entries = Mockito.mock(Collection.class);
Collection<Map.Entry<Int, CompletableFuture<Int>>> entries = Mockito.mock();
when(entries.iterator()).thenReturn(delegate.iterator());

assertThat(cache.asMap().entrySet().removeAll(entries)).isTrue();
Expand All @@ -2494,10 +2493,9 @@ public void entrySet_removeAll_byCollection(AsyncCache<Int, Int> cache, CacheCon
public void entrySet_removeAll_bySet(AsyncCache<Int, Int> cache, CacheContext context) {
var delegate = Sets.union(cache.asMap().entrySet(),
Maps.transformValues(context.absent(), Int::asFuture).entrySet());
var entries = Mockito.mock(Set.class);
Set<Map.Entry<Int, CompletableFuture<Int>>> entries = Mockito.mock();
when(entries.size()).thenReturn(delegate.size());
when(entries.contains(any())).thenAnswer(invocation ->
delegate.contains(invocation.getArgument(0)));
when(entries.contains(any())).then(invocation -> delegate.contains(invocation.getArgument(0)));

assertThat(cache.asMap().entrySet().removeAll(entries)).isTrue();
verify(entries).size();
Expand Down
Expand Up @@ -56,7 +56,6 @@
import static org.mockito.Mockito.CALLS_REAL_METHODS;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doCallRealMethod;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.verify;
Expand Down Expand Up @@ -251,7 +250,7 @@ public void cleanupTask_allowGc() {
@CheckMaxLogLevel(ERROR)
public void cleanupTask_exception() {
var expected = new RuntimeException();
var cache = Mockito.mock(BoundedLocalCache.class);
BoundedLocalCache<?, ?> cache = Mockito.mock();
doThrow(expected).when(cache).performCleanUp(any());
var task = new PerformCleanupTask(cache);
assertThat(task.exec()).isFalse();
Expand All @@ -267,7 +266,7 @@ public void cleanupTask_exception() {
@CheckMaxLogLevel(ERROR)
public void cleanup_exception() {
var expected = new RuntimeException();
var cache = Mockito.mock(BoundedLocalCache.class);
BoundedLocalCache<?, ?> cache = Mockito.mock();
doThrow(expected).when(cache).performCleanUp(any());
doCallRealMethod().when(cache).cleanUp();
cache.cleanUp();
Expand Down Expand Up @@ -312,7 +311,7 @@ public void scheduleAfterWrite_invalidDrainStatus() {

@Test
public void scheduleDrainBuffers() {
var executor = Mockito.mock(Executor.class);
Executor executor = Mockito.mock();
var cache = new BoundedLocalCache<Object, Object>(
Caffeine.newBuilder().executor(executor), /* loader */ null, /* async */ false) {};
var transitions = Map.of(
Expand Down Expand Up @@ -2006,8 +2005,8 @@ public void putIfAbsent_expireAfterRead(BoundedLocalCache<Int, Int> cache, Cache
expireAfterAccess = {Expire.DISABLED, Expire.ONE_MINUTE},
expireAfterWrite = {Expire.DISABLED, Expire.ONE_MINUTE})
public void unschedule_cleanUp(BoundedLocalCache<Int, Int> cache, CacheContext context) {
var future = Mockito.mock(Future.class);
doReturn(future).when(context.scheduler()).schedule(any(), any(), anyLong(), any());
Future<?> future = Mockito.mock();
when(context.scheduler().schedule(any(), any(), anyLong(), any())).then(invocation -> future);

for (int i = 0; i < 10; i++) {
var value = cache.put(Int.valueOf(i), Int.valueOf(-i));
Expand All @@ -2032,8 +2031,8 @@ public void unschedule_cleanUp(BoundedLocalCache<Int, Int> cache, CacheContext c
expireAfterAccess = {Expire.DISABLED, Expire.ONE_MINUTE},
expireAfterWrite = {Expire.DISABLED, Expire.ONE_MINUTE})
public void unschedule_invalidateAll(BoundedLocalCache<Int, Int> cache, CacheContext context) {
var future = Mockito.mock(Future.class);
doReturn(future).when(context.scheduler()).schedule(any(), any(), anyLong(), any());
Future<?> future = Mockito.mock();
when(context.scheduler().schedule(any(), any(), anyLong(), any())).then(invocation -> future);

for (int i = 0; i < 10; i++) {
var value = cache.put(Int.valueOf(i), Int.valueOf(-i));
Expand Down Expand Up @@ -2200,7 +2199,7 @@ public void expirationDelay_varTime(BoundedLocalCache<Int, Int> cache, CacheCont
refreshAfterWrite = Expire.ONE_MINUTE, executor = CacheExecutor.THREADED,
compute = Compute.SYNC, stats = Stats.DISABLED)
public void refreshIfNeeded_liveliness(CacheContext context) {
var stats = Mockito.mock(StatsCounter.class);
StatsCounter stats = Mockito.mock();
context.caffeine().recordStats(() -> stats);

// Capture the refresh parameters, should not be retired/dead sentinel entry
Expand Down Expand Up @@ -2300,13 +2299,13 @@ public CompletableFuture<Int> asyncReload(Int key, Int oldValue, Executor execut
@CacheSpec(population = Population.EMPTY, executor = CacheExecutor.THREADED,
compute = Compute.ASYNC, stats = Stats.DISABLED)
public void refresh_startReloadBeforeLoadCompletion(CacheContext context) {
var stats = Mockito.mock(StatsCounter.class);
var beganLoadSuccess = new AtomicBoolean();
var endLoadSuccess = new CountDownLatch(1);
var beganReloading = new AtomicBoolean();
var beganLoading = new AtomicBoolean();
var endReloading = new AtomicBoolean();
var endLoading = new AtomicBoolean();
StatsCounter stats = Mockito.mock();

context.ticker().setAutoIncrementStep(Duration.ofSeconds(1));
context.caffeine().recordStats(() -> stats);
Expand Down
Expand Up @@ -35,7 +35,6 @@
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.verifyNoMoreInteractions;
Expand Down Expand Up @@ -142,8 +141,8 @@ public void expire_evictionListener_failure(Cache<Int, Int> cache, CacheContext
public void schedule(Cache<Int, Int> cache, CacheContext context) {
var delay = ArgumentCaptor.forClass(long.class);
var task = ArgumentCaptor.forClass(Runnable.class);
doReturn(DisabledFuture.INSTANCE).when(context.scheduler()).schedule(
eq(context.executor()), task.capture(), delay.capture(), eq(TimeUnit.NANOSECONDS));
when(context.scheduler().schedule(eq(context.executor()), task.capture(), delay.capture(),
eq(TimeUnit.NANOSECONDS))).then(invocation -> DisabledFuture.INSTANCE);

cache.put(context.absentKey(), context.absentValue());

Expand Down Expand Up @@ -172,10 +171,10 @@ public void schedule(Cache<Int, Int> cache, CacheContext context) {
expireAfterAccess = {Expire.DISABLED, Expire.ONE_MINUTE}, expiryTime = Expire.ONE_MINUTE,
expireAfterWrite = {Expire.DISABLED, Expire.ONE_MINUTE})
public void schedule_immediate(Cache<Int, Int> cache, CacheContext context) {
doAnswer(invocation -> {
when(context.scheduler().schedule(any(), any(), anyLong(), any())).then(invocation -> {
invocation.<Runnable>getArgument(1).run();
return new CompletableFuture<>();
}).when(context.scheduler()).schedule(any(), any(), anyLong(), any());
});

cache.put(context.absentKey(), context.absentValue());
verify(context.scheduler()).schedule(any(), any(), anyLong(), any());
Expand Down
Expand Up @@ -16,11 +16,10 @@
package com.github.benmanes.caffeine.cache;

import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;

import java.util.Random;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -60,8 +59,8 @@ public void beforeMethod() throws Exception {
@Test
public void schedule_initialize() {
long delay = random.nextInt(Ints.saturatedCast(Pacer.TOLERANCE));
doReturn(future)
.when(scheduler).schedule(executor, command, Pacer.TOLERANCE, TimeUnit.NANOSECONDS);
when(scheduler.schedule(executor, command, Pacer.TOLERANCE, TimeUnit.NANOSECONDS))
.then(invocation -> future);
pacer.schedule(executor, command, NOW, delay);

assertThat(pacer.isScheduled()).isTrue();
Expand All @@ -72,12 +71,13 @@ public void schedule_initialize() {
@Test
public void schedule_initialize_recurse() {
long delay = random.nextInt(Ints.saturatedCast(Pacer.TOLERANCE));
doAnswer(invocation -> {
assertThat(pacer.future).isNull();
assertThat(pacer.nextFireTime).isNotEqualTo(0);
pacer.schedule(executor, command, NOW, delay);
return future;
}).when(scheduler).schedule(executor, command, Pacer.TOLERANCE, TimeUnit.NANOSECONDS);
when(scheduler.schedule(executor, command, Pacer.TOLERANCE, TimeUnit.NANOSECONDS))
.then(invocation -> {
assertThat(pacer.future).isNull();
assertThat(pacer.nextFireTime).isNotEqualTo(0);
pacer.schedule(executor, command, NOW, delay);
return future;
});

pacer.schedule(executor, command, NOW, delay);

Expand All @@ -90,8 +90,8 @@ public void schedule_initialize_recurse() {
public void schedule_cancel_schedule() {
long fireTime = NOW + Pacer.TOLERANCE;
long delay = random.nextInt(Ints.saturatedCast(Pacer.TOLERANCE));
doReturn(future)
.when(scheduler).schedule(executor, command, Pacer.TOLERANCE, TimeUnit.NANOSECONDS);
when(scheduler.schedule(executor, command, Pacer.TOLERANCE, TimeUnit.NANOSECONDS))
.then(invocation -> future);

pacer.schedule(executor, command, NOW, delay);
assertThat(pacer.nextFireTime).isEqualTo(fireTime);
Expand Down Expand Up @@ -146,8 +146,8 @@ public void schedule_beforeNextFireTime_minimumDelay() {
pacer.future = future;

long delay = random.nextInt(Ints.saturatedCast(Pacer.TOLERANCE));
doReturn(future)
.when(scheduler).schedule(executor, command, Pacer.TOLERANCE, TimeUnit.NANOSECONDS);
when(scheduler.schedule(executor, command, Pacer.TOLERANCE, TimeUnit.NANOSECONDS))
.then(invocation -> future);
pacer.schedule(executor, command, NOW, delay);

verify(future).cancel(false);
Expand All @@ -167,8 +167,8 @@ public void schedule_beforeNextFireTime_customDelay() {
pacer.future = future;

long delay = (Pacer.TOLERANCE + Math.max(1, random.nextInt()));
doReturn(future)
.when(scheduler).schedule(executor, command, delay, TimeUnit.NANOSECONDS);
when(scheduler.schedule(executor, command, delay, TimeUnit.NANOSECONDS))
.then(invocation -> future);
pacer.schedule(executor, command, NOW, delay);

verify(future).cancel(false);
Expand Down
Expand Up @@ -129,9 +129,9 @@ public void guardedScheduler_null() {

@Test
public void guardedScheduler_nullFuture() {
var scheduledExecutor = Mockito.mock(ScheduledExecutorService.class);
ScheduledExecutorService scheduledExecutor = Mockito.mock();
var scheduler = Scheduler.forScheduledExecutorService(scheduledExecutor);
var executor = Mockito.mock(Executor.class);
Executor executor = Mockito.mock();
Runnable command = () -> {};

var future = Scheduler.guardedScheduler(scheduler)
Expand Down Expand Up @@ -169,9 +169,9 @@ public void scheduledExecutorService_null() {

@Test
public void scheduledExecutorService_schedule() {
var scheduledExecutor = Mockito.mock(ScheduledExecutorService.class);
ScheduledExecutorService scheduledExecutor = Mockito.mock();
var task = ArgumentCaptor.forClass(Runnable.class);
var executor = Mockito.mock(Executor.class);
Executor executor = Mockito.mock();
Runnable command = () -> {};

var scheduler = Scheduler.forScheduledExecutorService(scheduledExecutor);
Expand All @@ -189,8 +189,8 @@ public void scheduledExecutorService_schedule() {

@Test
public void scheduledExecutorService_shutdown() {
var scheduledExecutor = Mockito.mock(ScheduledExecutorService.class);
var executor = Mockito.mock(Executor.class);
ScheduledExecutorService scheduledExecutor = Mockito.mock();
Executor executor = Mockito.mock();

when(scheduledExecutor.isShutdown()).thenReturn(true);
var scheduler = Scheduler.forScheduledExecutorService(scheduledExecutor);
Expand Down
Expand Up @@ -402,7 +402,7 @@ public void deschedule_fuzzy(long clock, long nanos, long[] times) {

@Test(dataProvider = "clock")
public void expire_reschedule(long clock) {
when(cache.evictEntry(captor.capture(), any(), anyLong())).thenAnswer(invocation -> {
when(cache.evictEntry(captor.capture(), any(), anyLong())).then(invocation -> {
Timer timer = invocation.getArgument(0);
timer.setVariableTime(timerWheel.nanos + 100);
return false;
Expand Down
Expand Up @@ -112,7 +112,7 @@ public void guarded_sameInstance() {

@Test
public void guarded_exception() {
var statsCounter = Mockito.mock(StatsCounter.class);
StatsCounter statsCounter = Mockito.mock();
when(statsCounter.snapshot()).thenThrow(new NullPointerException());
doThrow(NullPointerException.class).when(statsCounter).recordHits(anyInt());
doThrow(NullPointerException.class).when(statsCounter).recordMisses(anyInt());
Expand Down
Expand Up @@ -203,7 +203,7 @@ enum CacheWeigher {
/** A flag indicating that the entry's weight records interactions. */
@SuppressWarnings("unchecked")
MOCKITO(() -> {
var weigher = Mockito.mock(Weigher.class);
Weigher<Object, Object> weigher = Mockito.mock();
when(weigher.weigh(any(), any())).thenReturn(1);
return weigher;
}, 1);
Expand Down Expand Up @@ -391,7 +391,7 @@ enum Listener {
/** A {@link ConsumingRemovalListener} retains all notifications for evaluation by the test. */
CONSUMING(RemovalListeners::consuming),
/** A removal listener that records interactions. */
MOCKITO(() -> Mockito.mock(RemovalListener.class));
MOCKITO(() -> Mockito.mock());

private final Supplier<RemovalListener<Object, Object>> factory;

Expand Down Expand Up @@ -796,7 +796,7 @@ enum CacheScheduler {
DISABLED(() -> null),
SYSTEM(Scheduler::systemScheduler),
THREADED(() -> Scheduler.forScheduledExecutorService(scheduledExecutor)),
MOCKITO(() -> Mockito.mock(Scheduler.class));
MOCKITO(() -> Mockito.mock());

private final Supplier<Scheduler> scheduler;

Expand Down

0 comments on commit 6b44488

Please sign in to comment.