Skip to content

Commit

Permalink
minor touchups
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-manes committed Dec 31, 2024
1 parent 02349cf commit f6820e8
Show file tree
Hide file tree
Showing 17 changed files with 374 additions and 346 deletions.
9 changes: 2 additions & 7 deletions caffeine/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ dependencies {
api(libs.jspecify)
api(libs.errorprone.annotations)

testImplementation(libs.joor)
testImplementation(libs.ycsb) {
isTransitive = false
}
Expand All @@ -45,17 +44,13 @@ dependencies {
testImplementation(libs.bundles.slf4j.test)
testImplementation(libs.commons.collections4)
testImplementation(libs.commons.collections4) {
artifact {
classifier = "tests"
}
artifact { classifier = "tests" }
}
testImplementation(sourceSets["codeGen"].output)
testImplementation(libs.eclipse.collections.testutils)

collections4Sources(libs.commons.collections4) {
artifact {
classifier = "test-sources"
}
artifact { classifier = "test-sources" }
}

jammAgent(libs.jamm) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ public void getAll_absent_partial(AsyncLoadingCache<Int, Int> cache, CacheContex
var expect = new ImmutableMap.Builder<Int, Int>()
.putAll(Maps.toMap(context.firstMiddleLastKeys(), Int::negate))
.putAll(Maps.toMap(context.absentKeys(), Int::negate))
.build();
.buildOrThrow();
var result = cache.getAll(expect.keySet()).join();
assertThat(result).isEqualTo(expect);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ public void getAll_exceeds(Cache<Int, Int> cache, CacheContext context) {
var expected = new ImmutableMap.Builder<Int, Int>()
.putAll(context.original())
.putAll(context.absent())
.build();
.buildOrThrow();
assertThat(cache).containsExactlyEntriesIn(expected);
assertThat(context).stats().hits(0).misses(result.size()).success(1).failures(0);
assertThat(result).containsExactlyEntriesIn(Map.of(context.absentKey(), context.absentValue()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ public void getAll_absent_partial(LoadingCache<Int, Int> cache, CacheContext con
var expect = new ImmutableMap.Builder<Int, Int>()
.putAll(Maps.toMap(context.firstMiddleLastKeys(), Int::negate))
.putAll(Maps.toMap(context.absentKeys(), Int::negate))
.build();
.buildOrThrow();
var result = cache.getAll(expect.keySet());
assertThat(result).isEqualTo(expect);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ private void checkEvictionDeque(BoundedLocalCache<Object, Object> bounded) {
.put("probation", mainProbation, bounded.accessOrderProbationDeque())
.put("protected", bounded.mainProtectedWeightedSize(),
bounded.accessOrderProtectedDeque())
.build();
.buildOrThrow();
checkLinks(bounded, deques);
check("accessOrderWindowDeque()").about(deque())
.that(bounded.accessOrderWindowDeque()).isValid();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,48 +20,34 @@
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
import static org.mockito.quality.Strictness.STRICT_STUBS;

import java.util.Random;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

import org.mockito.Mock;
import org.mockito.testng.MockitoSettings;
import org.mockito.testng.MockitoTestNGListener;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Listeners;
import org.mockito.Mockito;
import org.testng.annotations.Test;

import com.google.common.primitives.Ints;

/**
* @author [email protected] (Ben Manes)
*/
@Test(singleThreaded = true)
@Listeners(MockitoTestNGListener.class)
@MockitoSettings(strictness = STRICT_STUBS)
public final class PacerTest {
private static final long ONE_MINUTE_IN_NANOS = TimeUnit.MINUTES.toNanos(1);
private static final Random random = new Random();
private static final long NOW = random.nextLong();

@Mock Scheduler scheduler;
@Mock Executor executor;
@Mock Runnable command;
@Mock Future<?> future;

Pacer pacer;

@BeforeMethod
public void beforeMethod() {
pacer = new Pacer(scheduler);
}

@Test
public void schedule_initialize() {
Scheduler scheduler = Mockito.mock();
Executor executor = Mockito.mock();
Runnable command = Mockito.mock();
Future<?> future = Mockito.mock();
var pacer = new Pacer(scheduler);

long delay = random.nextInt(Ints.saturatedCast(Pacer.TOLERANCE));
when(scheduler.schedule(executor, command, Pacer.TOLERANCE, TimeUnit.NANOSECONDS))
.then(invocation -> future);
Expand All @@ -74,6 +60,12 @@ public void schedule_initialize() {

@Test
public void schedule_initialize_recurse() {
Scheduler scheduler = Mockito.mock();
Executor executor = Mockito.mock();
Runnable command = Mockito.mock();
Future<?> future = Mockito.mock();
var pacer = new Pacer(scheduler);

long delay = random.nextInt(Ints.saturatedCast(Pacer.TOLERANCE));
when(scheduler.schedule(executor, command, Pacer.TOLERANCE, TimeUnit.NANOSECONDS))
.then(invocation -> {
Expand All @@ -92,6 +84,12 @@ public void schedule_initialize_recurse() {

@Test
public void schedule_cancel_schedule() {
Scheduler scheduler = Mockito.mock();
Executor executor = Mockito.mock();
Runnable command = Mockito.mock();
Future<?> future = Mockito.mock();
var pacer = new Pacer(scheduler);

long fireTime = NOW + Pacer.TOLERANCE;
long delay = random.nextInt(Ints.saturatedCast(Pacer.TOLERANCE));
when(scheduler.schedule(executor, command, Pacer.TOLERANCE, TimeUnit.NANOSECONDS))
Expand All @@ -116,6 +114,12 @@ public void schedule_cancel_schedule() {

@Test
public void scheduled_afterNextFireTime_skip() {
Scheduler scheduler = Mockito.mock();
Executor executor = Mockito.mock();
Runnable command = Mockito.mock();
Future<?> future = Mockito.mock();
var pacer = new Pacer(scheduler);

pacer.nextFireTime = NOW + ONE_MINUTE_IN_NANOS;
pacer.future = future;

Expand All @@ -130,6 +134,12 @@ public void scheduled_afterNextFireTime_skip() {

@Test
public void schedule_beforeNextFireTime_skip() {
Scheduler scheduler = Mockito.mock();
Executor executor = Mockito.mock();
Runnable command = Mockito.mock();
Future<?> future = Mockito.mock();
var pacer = new Pacer(scheduler);

pacer.nextFireTime = NOW + ONE_MINUTE_IN_NANOS;
pacer.future = future;

Expand All @@ -146,6 +156,12 @@ public void schedule_beforeNextFireTime_skip() {

@Test
public void schedule_beforeNextFireTime_minimumDelay() {
Scheduler scheduler = Mockito.mock();
Executor executor = Mockito.mock();
Runnable command = Mockito.mock();
Future<?> future = Mockito.mock();
var pacer = new Pacer(scheduler);

pacer.nextFireTime = NOW + ONE_MINUTE_IN_NANOS;
pacer.future = future;

Expand All @@ -167,6 +183,12 @@ public void schedule_beforeNextFireTime_minimumDelay() {

@Test
public void schedule_beforeNextFireTime_customDelay() {
Scheduler scheduler = Mockito.mock();
Executor executor = Mockito.mock();
Runnable command = Mockito.mock();
Future<?> future = Mockito.mock();
var pacer = new Pacer(scheduler);

pacer.nextFireTime = NOW + ONE_MINUTE_IN_NANOS;
pacer.future = future;

Expand All @@ -188,6 +210,8 @@ public void schedule_beforeNextFireTime_customDelay() {

@Test
public void cancel_initialize() {
var pacer = new Pacer(Mockito.mock());

pacer.cancel();
assertThat(pacer.nextFireTime).isEqualTo(0);
assertThat(pacer.isScheduled()).isFalse();
Expand All @@ -196,6 +220,9 @@ public void cancel_initialize() {

@Test
public void cancel_scheduled() {
Future<?> future = Mockito.mock();
var pacer = new Pacer(Mockito.mock());

pacer.nextFireTime = NOW + ONE_MINUTE_IN_NANOS;
pacer.future = future;

Expand All @@ -208,18 +235,24 @@ public void cancel_scheduled() {

@Test
public void isScheduled_nullFuture() {
var pacer = new Pacer(Mockito.mock());

pacer.future = null;
assertThat(pacer.isScheduled()).isFalse();
}

@Test
public void isScheduled_doneFuture() {
var pacer = new Pacer(Mockito.mock());

pacer.future = DisabledFuture.instance();
assertThat(pacer.isScheduled()).isFalse();
}

@Test
public void isScheduled_inFlight() {
var pacer = new Pacer(Mockito.mock());

pacer.future = new CompletableFuture<>();
assertThat(pacer.isScheduled()).isTrue();
}
Expand Down
Loading

0 comments on commit f6820e8

Please sign in to comment.