Skip to content

Commit

Permalink
misc cleanups
Browse files Browse the repository at this point in the history
1. Fix flaky test cases where weak values are GC'd prior to the
assertion, causing the check to fail.
2. Guava adapter's asMap() should return the same instance rather
than a new forwarder each time.
3. Resolve Gradle 7 deprecations
4. Dependency updates
5. Minor formatting
  • Loading branch information
ben-manes committed Jun 7, 2021
1 parent a8af2a2 commit ff3bc33
Show file tree
Hide file tree
Showing 26 changed files with 442 additions and 401 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
distribution: 'zulu'
java-version: ${{ matrix.java }}
- name: Cache Gradle packages
uses: actions/[email protected].5
uses: actions/[email protected].6
with:
path: |
~/.gradle/wrapper
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
distribution: 'zulu'
java-version: ${{ matrix.java }}
- name: Cache Gradle packages
uses: actions/[email protected].5
uses: actions/[email protected].6
with:
path: |
~/.gradle/wrapper
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
distribution: 'zulu'
java-version: ${{ matrix.java }}
- name: Cache Gradle packages
uses: actions/[email protected].5
uses: actions/[email protected].6
with:
path: |
~/.gradle/wrapper
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/gradle-wrapper-validation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/[email protected]
- uses: gradle/[email protected].3
- uses: gradle/[email protected].4
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
distribution: 'zulu'
java-version: 11
- name: Cache Gradle packages
uses: actions/[email protected].5
uses: actions/[email protected].6
with:
path: |
~/.gradle/wrapper
Expand Down
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ subprojects {
// ensure tasks don't overwrite the default report directories used by the 'test' task
reports.html.destination = file("${buildDir}/reports/${name}")
reports.junitXml.destination = file("${buildDir}/reports/${name}/results")
binResultsDir = file("${buildDir}/reports/${name}/results/binary/${name}")
binaryResultsDirectory = file("${buildDir}/reports/${name}/results/binary/${name}")
}

if (project != project(':caffeine')) {
Expand Down Expand Up @@ -135,8 +135,8 @@ task jacocoRootReport(type: JacocoReport, group: 'Coverage reports') {
executionData jacocoMerge.destinationFile

reports {
html.enabled = true // human readable
xml.enabled = true // required by coveralls
html.required = true // human readable
xml.required = true // required by coveralls
}
}

Expand Down
8 changes: 4 additions & 4 deletions caffeine/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ compileCodeGenJava {
enabled = gradle.taskGraph.hasTask('uploadArchives')
}
dependsOn compileJava
destinationDir = compileJava.destinationDir
destinationDirectory = compileJava.destinationDirectory
classpath = sourceSets.main.runtimeClasspath + sourceSets.main.output
options.incremental = false
options.debug = false
Expand All @@ -97,7 +97,7 @@ sonarqube {
}

task generateLocalCaches(type: JavaExec) {
main = 'com.github.benmanes.caffeine.cache.LocalCacheFactoryGenerator'
mainClass = 'com.github.benmanes.caffeine.cache.LocalCacheFactoryGenerator'
classpath = sourceSets.javaPoet.runtimeClasspath
args "${buildDir}/generated-sources/"

Expand All @@ -109,7 +109,7 @@ compileJava.dependsOn(generateLocalCaches)
sourcesJar.dependsOn(generateLocalCaches)

task generateNodes(type: JavaExec) {
main = 'com.github.benmanes.caffeine.cache.NodeFactoryGenerator'
mainClass = 'com.github.benmanes.caffeine.cache.NodeFactoryGenerator'
classpath = sourceSets.javaPoet.runtimeClasspath
args "${buildDir}/generated-sources/"

Expand All @@ -123,5 +123,5 @@ sourcesJar.dependsOn(generateNodes)
task memoryOverhead(type: JavaExec, group: 'Benchmarks', description: 'Evaluates cache overhead') {
classpath sourceSets.jmh.runtimeClasspath
jvmArgs "-javaagent:${configurations.javaAgent.singleFile}"
main = 'com.github.benmanes.caffeine.cache.MemoryBenchmark'
mainClass = 'com.github.benmanes.caffeine.cache.MemoryBenchmark'
}
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,7 @@ public E poll() {
if (e == null) {
if (index != lvProducerIndex(this)) {
// poll() == null iff queue is empty, null element is not strong enough indicator, so we
// must
// check the producer index. If the queue is indeed not empty we spin until element is
// must check the producer index. If the queue is indeed not empty we spin until element is
// visible.
do {
e = lvElement(buffer, offset);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@
import com.github.benmanes.caffeine.cache.testing.CacheValidationListener;
import com.github.benmanes.caffeine.cache.testing.CheckNoStats;
import com.google.common.base.Functions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterators;
import com.google.common.collect.Maps;
Expand Down Expand Up @@ -921,11 +920,11 @@ public void iterators(Map<Integer, Integer> map, CacheContext context) {
expireAfterAccess = {Expire.DISABLED, Expire.ONE_MINUTE},
expireAfterWrite = {Expire.DISABLED, Expire.ONE_MINUTE})
public void putIfAbsent_weighted(Cache<Integer, List<Integer>> cache, CacheContext context) {
cache.put(1, ImmutableList.of(1));
cache.put(context.absentKey(), List.of(context.absentValue()));
context.ticker().advance(1, TimeUnit.MINUTES);
cache.asMap().putIfAbsent(1, ImmutableList.of(1, 2, 3));

assertThat(cache.policy().eviction().get().weightedSize().getAsLong(), is(3L));
cache.asMap().putIfAbsent(context.absentKey(), List.copyOf(context.absent().values()));
assertThat(cache.policy().eviction().get().weightedSize().getAsLong(),
is((long) context.absent().size()));
}

@Test(dataProvider = "caches")
Expand All @@ -936,11 +935,15 @@ public void putIfAbsent_weighted(Cache<Integer, List<Integer>> cache, CacheConte
expireAfterAccess = {Expire.DISABLED, Expire.ONE_MINUTE},
expireAfterWrite = {Expire.DISABLED, Expire.ONE_MINUTE})
public void put_weighted(Cache<Integer, List<Integer>> cache, CacheContext context) {
cache.put(1, ImmutableList.of(1));
cache.put(context.absentKey(), List.of(context.absentValue()));
assertThat(cache.policy().eviction().get().weightedSize().getAsLong(), is(1L));

context.ticker().advance(1, TimeUnit.MINUTES);
cache.put(1, ImmutableList.of(1, 2, 3));
assertThat(cache.policy().eviction().get().weightedSize().getAsLong(), is(1L));

assertThat(cache.policy().eviction().get().weightedSize().getAsLong(), is(3L));
cache.put(context.absentKey(), List.copyOf(context.absent().values()));
assertThat(cache.policy().eviction().get().weightedSize().getAsLong(),
is((long) context.absent().size()));
}

@Test(dataProvider = "caches")
Expand All @@ -951,11 +954,12 @@ public void put_weighted(Cache<Integer, List<Integer>> cache, CacheContext conte
expireAfterAccess = {Expire.DISABLED, Expire.ONE_MINUTE},
expireAfterWrite = {Expire.DISABLED, Expire.ONE_MINUTE})
public void computeIfAbsent_weighted(Cache<Integer, List<Integer>> cache, CacheContext context) {
cache.put(1, ImmutableList.of(1));
cache.put(context.absentKey(), List.of(context.absentValue()));
context.ticker().advance(1, TimeUnit.MINUTES);
cache.asMap().computeIfAbsent(1, k -> ImmutableList.of(1, 2, 3));
cache.asMap().computeIfAbsent(context.absentKey(), k -> List.copyOf(context.absent().values()));

assertThat(cache.policy().eviction().get().weightedSize().getAsLong(), is(3L));
assertThat(cache.policy().eviction().get().weightedSize().getAsLong(),
is((long) context.absent().size()));
}

@Test(dataProvider = "caches")
Expand All @@ -966,11 +970,12 @@ public void computeIfAbsent_weighted(Cache<Integer, List<Integer>> cache, CacheC
expireAfterAccess = {Expire.DISABLED, Expire.ONE_MINUTE},
expireAfterWrite = {Expire.DISABLED, Expire.ONE_MINUTE})
public void compute_weighted(Cache<Integer, List<Integer>> cache, CacheContext context) {
cache.put(1, ImmutableList.of(1));
cache.put(context.absentKey(), List.of(context.absentValue()));
context.ticker().advance(1, TimeUnit.MINUTES);
cache.asMap().compute(1, (k, v) -> ImmutableList.of(1, 2, 3));
cache.asMap().compute(context.absentKey(), (k, v) -> List.copyOf(context.absent().values()));

assertThat(cache.policy().eviction().get().weightedSize().getAsLong(), is(3L));
assertThat(cache.policy().eviction().get().weightedSize().getAsLong(),
is((long) context.absent().size()));
}

@Test(dataProvider = "caches")
Expand All @@ -981,13 +986,13 @@ public void compute_weighted(Cache<Integer, List<Integer>> cache, CacheContext c
expireAfterAccess = {Expire.DISABLED, Expire.ONE_MINUTE},
expireAfterWrite = {Expire.DISABLED, Expire.ONE_MINUTE})
public void merge_weighted(Cache<Integer, List<Integer>> cache, CacheContext context) {
cache.put(1, ImmutableList.of(1));
cache.put(context.absentKey(), List.of(context.absentValue()));
context.ticker().advance(1, TimeUnit.MINUTES);
cache.asMap().merge(1, ImmutableList.of(1, 2, 3), (oldValue, v) -> {
throw new AssertionError("Should never be called");
});
cache.asMap().merge(context.absentKey(), List.copyOf(context.absent().values()),
(oldValue, v) -> { throw new AssertionError("Should never be called"); });

assertThat(cache.policy().eviction().get().weightedSize().getAsLong(), is(3L));
assertThat(cache.policy().eviction().get().weightedSize().getAsLong(),
is((long) context.absent().size()));
}

@Test(dataProvider = "caches")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,8 @@ public void refresh_dedupe(LoadingCache<Integer, Integer> cache, CacheContext co
var future2 = cache.refresh(key);
assertThat(future1, is(sameInstance(future2)));

future1.complete(-key);
assertThat(cache.getIfPresent(key), is(-key));
future1.complete(context.absentValue());
assertThat(cache.getIfPresent(key), is(context.absentValue()));
}

@Test(dataProvider = "caches")
Expand Down
Loading

0 comments on commit ff3bc33

Please sign in to comment.