Skip to content

Commit

Permalink
minor additions to test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-manes committed Mar 18, 2023
1 parent 895a46d commit 0fd27bf
Show file tree
Hide file tree
Showing 12 changed files with 172 additions and 53 deletions.
4 changes: 0 additions & 4 deletions .lgtm.yml

This file was deleted.

7 changes: 2 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ apply plugin: 'io.github.gradle-nexus.publish-plugin'
apply plugin: 'io.snyk.gradle.plugin.snykplugin'
apply plugin: 'com.github.ben-manes.versions'
apply plugin: 'org.owasp.dependencycheck'
apply plugin: 'jvm-ecosystem'

apply from: "${rootDir}/gradle/coverage.gradle"

Expand Down Expand Up @@ -150,11 +151,7 @@ snyk {
autoDownload = true
autoUpdate = true
}
tasks.named('snyk-test').configure {
notCompatibleWithConfigurationCache(
"The ${name} task is not compatible with the configuration cache")
}
tasks.named('snyk-monitor').configure {
['snyk-check-binary', 'snyk-test', 'snyk-monitor'].collect { tasks.named(it) }*.configure {
notCompatibleWithConfigurationCache(
"The ${name} task is not compatible with the configuration cache")
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,13 @@ public static <K, V> SnapshotEntry<K, V> forEntry(K key, V value) {
public static <K, V> SnapshotEntry<K, V> forEntry(K key, V value,
long snapshot, int weight, long expiresAt, long refreshableAt) {
long unsetTicks = snapshot + Long.MAX_VALUE;
boolean refresh = (refreshableAt == unsetTicks);
boolean weights = (weight < 0) || (weight == 1);
boolean expires = (expiresAt == unsetTicks);
int features =
(refresh ? 0b000 : 0b100)
| (expires ? 0b000 : 0b010)
| (weights ? 0b000 : 0b001);
boolean refresh = (refreshableAt != unsetTicks);
boolean expires = (expiresAt != unsetTicks);
boolean weights = (weight != 1);
int features = // truth table
(weights ? 0b001 : 0b000)
| (expires ? 0b010 : 0b000)
| (refresh ? 0b100 : 0b000);
switch (features) { // optimized for common cases
case 0b000: return new SnapshotEntry<>(key, value, snapshot);
case 0b001: return new WeightedEntry<>(key, value, snapshot, weight);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
import com.google.common.primitives.Ints;

/**
Expand Down Expand Up @@ -259,12 +260,31 @@ public void getAll_absent_interrupted(AsyncLoadingCache<Int, Int> cache, CacheCo
@CacheSpec(loader = { Loader.NEGATIVE, Loader.BULK_NEGATIVE },
removalListener = { Listener.DISABLED, Listener.REJECTING })
public void getAll_absent(AsyncLoadingCache<Int, Int> cache, CacheContext context) {
var result = cache.getAll(context.absentKeys()).join();
var expect = Maps.toMap(context.absentKeys(), Int::negate);
var result = cache.getAll(expect.keySet()).join();
assertThat(result).isEqualTo(expect);

int misses = expect.size();
int loads = context.loader().isBulk() ? 1 : misses;
assertThat(context).stats().hits(0).misses(misses).success(loads).failures(0);
}

int count = context.absentKeys().size();
int loads = context.loader().isBulk() ? 1 : count;
assertThat(result).hasSize(count);
assertThat(context).stats().hits(0).misses(count).success(loads).failures(0);
@Test(dataProvider = "caches")
@CacheSpec(loader = { Loader.NEGATIVE, Loader.BULK_NEGATIVE },
population = { Population.SINGLETON, Population.PARTIAL, Population.FULL },
removalListener = { Listener.DISABLED, Listener.REJECTING })
public void getAll_absent_partial(AsyncLoadingCache<Int, Int> cache, CacheContext context) {
var expect = new ImmutableMap.Builder<Int, Int>()
.putAll(Maps.toMap(context.firstMiddleLastKeys(), Int::negate))
.putAll(Maps.toMap(context.absentKeys(), Int::negate))
.build();
var result = cache.getAll(expect.keySet()).join();
assertThat(result).isEqualTo(expect);

int misses = context.absentKeys().size();
int hits = context.firstMiddleLastKeys().size();
int loads = context.loader().isBulk() ? 1 : misses;
assertThat(context).stats().hits(hits).misses(misses).success(loads).failures(0);
}

@Test(dataProvider = "caches")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@
import com.github.benmanes.caffeine.cache.Policy.FixedExpiration;
import com.github.benmanes.caffeine.cache.Policy.VarExpiration;
import com.github.benmanes.caffeine.cache.References.WeakKeyReference;
import com.github.benmanes.caffeine.cache.SnapshotEntry.CompleteEntry;
import com.github.benmanes.caffeine.cache.SnapshotEntry.ExpirableEntry;
import com.github.benmanes.caffeine.cache.SnapshotEntry.ExpirableWeightedEntry;
import com.github.benmanes.caffeine.cache.SnapshotEntry.RefreshableExpirableEntry;
import com.github.benmanes.caffeine.cache.SnapshotEntry.WeightedEntry;
import com.github.benmanes.caffeine.cache.stats.StatsCounter;
import com.github.benmanes.caffeine.cache.testing.CacheContext;
import com.github.benmanes.caffeine.cache.testing.CacheProvider;
Expand Down Expand Up @@ -2532,7 +2537,7 @@ public void cleanupTask_ignore() {
task.complete(null);
}

@Test(dataProviderClass = CacheProvider.class, dataProvider = "caches")
@Test(dataProvider = "caches")
@CacheSpec(population = Population.SINGLETON, compute = Compute.SYNC,
initialCapacity = {InitialCapacity.DEFAULT, InitialCapacity.FULL})
public void node_string(BoundedLocalCache<Int, Int> cache, CacheContext context) {
Expand Down Expand Up @@ -2592,6 +2597,18 @@ public void policy_unsupported() {
assertThrows(type, () -> varExpiration.compute(Int.MAX_VALUE, (k, v) -> v, Duration.ZERO));
}

@Test(dataProvider = "caches")
@CacheSpec(population = Population.SINGLETON, expiryTime = Expire.ONE_MINUTE)
public void expireAfterRead_disabled(BoundedLocalCache<Int, Int> cache, CacheContext context) {
var node = Iterables.getOnlyElement(cache.data.values());
long duration = cache.expireAfterRead(node, node.getKey(), node.getValue(),
cache.expiry(), context.ticker().read());
var expiresAt = cache.expiresVariable()
? context.ticker().read() + context.expiryTime().timeNanos()
: 0;
assertThat(duration).isEqualTo(expiresAt);
}

@Test
public void fixedExpireAfterWrite() {
int key = 1;
Expand All @@ -2608,6 +2625,18 @@ public void fixedExpireAfterWrite() {
key, value, currentTime, currentDuration)).isEqualTo(currentDuration);
}

@Test
public void snapshotEntry() {
assertThat(SnapshotEntry.forEntry(1, 2)).isInstanceOf(SnapshotEntry.class);
assertThat(SnapshotEntry.forEntry(1, 2, 0, 1, 0, 0)).isInstanceOf(SnapshotEntry.class);
assertThat(SnapshotEntry.forEntry(1, 2, 0, 2, 0, 0)).isInstanceOf(WeightedEntry.class);
assertThat(SnapshotEntry.forEntry(1, 2, 0, 1, 3, 0)).isInstanceOf(ExpirableEntry.class);
assertThat(SnapshotEntry.forEntry(1, 2, 0, 2, 3, 0)).isInstanceOf(ExpirableWeightedEntry.class);
assertThat(SnapshotEntry.forEntry(1, 2, 0, 1, 3, 4))
.isInstanceOf(RefreshableExpirableEntry.class);
assertThat(SnapshotEntry.forEntry(1, 2, 0, 2, 3, 4)).isInstanceOf(CompleteEntry.class);
}

static <K, V> BoundedLocalCache<K, V> asBoundedLocalCache(Cache<K, V> cache) {
return (BoundedLocalCache<K, V>) cache.asMap();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,10 @@
import com.github.benmanes.caffeine.testing.Int;
import com.github.valfirst.slf4jtest.TestLoggerFactory;
import com.google.common.cache.CacheLoader.InvalidCacheLoadException;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
import com.google.common.primitives.Ints;

/**
Expand Down Expand Up @@ -276,12 +278,32 @@ public void getAll_absent_interrupted(LoadingCache<Int, Int> cache, CacheContext
@CacheSpec(loader = { Loader.NEGATIVE, Loader.BULK_NEGATIVE },
removalListener = { Listener.DISABLED, Listener.REJECTING })
public void getAll_absent(LoadingCache<Int, Int> cache, CacheContext context) {
var result = cache.getAll(context.absentKeys());
var expect = Maps.toMap(context.absentKeys(), Int::negate);
var result = cache.getAll(expect.keySet());
assertThat(result).isEqualTo(expect);

int count = context.absentKeys().size();
int loads = context.loader().isBulk() ? 1 : count;
assertThat(result).hasSize(count);
assertThat(context).stats().hits(0).misses(count).success(loads).failures(0);
int misses = expect.size();
int loads = context.loader().isBulk() ? 1 : misses;
assertThat(context).stats().hits(0).misses(misses).success(loads).failures(0);
}

@CheckNoEvictions
@Test(dataProvider = "caches")
@CacheSpec(loader = { Loader.NEGATIVE, Loader.BULK_NEGATIVE },
population = { Population.SINGLETON, Population.PARTIAL, Population.FULL },
removalListener = { Listener.DISABLED, Listener.REJECTING })
public void getAll_absent_partial(LoadingCache<Int, Int> cache, CacheContext context) {
var expect = new ImmutableMap.Builder<Int, Int>()
.putAll(Maps.toMap(context.firstMiddleLastKeys(), Int::negate))
.putAll(Maps.toMap(context.absentKeys(), Int::negate))
.build();
var result = cache.getAll(expect.keySet());
assertThat(result).isEqualTo(expect);

int misses = context.absentKeys().size();
int hits = context.firstMiddleLastKeys().size();
int loads = context.loader().isBulk() ? 1 : misses;
assertThat(context).stats().hits(hits).misses(misses).success(loads).failures(0);
}

@CheckNoEvictions
Expand All @@ -290,10 +312,7 @@ public void getAll_absent(LoadingCache<Int, Int> cache, CacheContext context) {
population = { Population.SINGLETON, Population.PARTIAL, Population.FULL },
removalListener = { Listener.DISABLED, Listener.REJECTING })
public void getAll_present_partial(LoadingCache<Int, Int> cache, CacheContext context) {
var expect = new HashMap<Int, Int>();
expect.put(context.firstKey(), context.firstKey().negate());
expect.put(context.middleKey(), context.middleKey().negate());
expect.put(context.lastKey(), context.lastKey().negate());
var expect = Maps.toMap(context.firstMiddleLastKeys(), Int::negate);
var result = cache.getAll(expect.keySet());

assertThat(result).containsExactlyEntriesIn(expect);
Expand Down
3 changes: 3 additions & 0 deletions config/spotbugs/exclude.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
CNC_COLLECTION_NAMING_CONFUSION,
DMI_RANDOM_USED_ONLY_ONCE,
EI_EXPOSE_REP, EI_EXPOSE_REP2,
ENMI_EQUALS_ON_ENUM,
ENMI_NULL_ENUM_VALUE,
ENMI_ONE_ENUM_VALUE,
EXS_EXCEPTION_SOFTENING_HAS_CHECKED,
EXS_EXCEPTION_SOFTENING_NO_CHECKED,
EXS_EXCEPTION_SOFTENING_NO_CONSTRAINTS,
Expand Down
24 changes: 13 additions & 11 deletions gradle/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ ext {
errorproneSupport: '0.8.0',
expiringMap: '0.5.10',
fastfilter: '1.0.2',
fastutil: '8.5.11',
fastutil: '8.5.12',
flipTables: '1.1.0',
googleJavaFormat: '1.16.0',
guava: '31.1-jre',
hazelcast: '5.2.2',
hazelcast: '5.3.0-BETA-1',
jackrabbit: '1.48.0',
jamm: '0.3.3',
javaObjectLayout: '0.17',
Expand All @@ -54,11 +54,11 @@ ext {
jmh: '1.36',
joor: '0.9.14',
jsr330: '1',
nullaway: '0.10.9',
nullaway: '0.10.10',
ohc: '0.6.1',
osgiComponentAnnotations: '1.5.1',
picocli: '4.7.1',
slf4j: '2.0.6',
slf4j: '2.0.7',
tcache: '2.0.1',
stream: '2.9.8',
univocityParsers: '2.9.1',
Expand All @@ -80,7 +80,7 @@ ext {
junit5: '5.9.2',
junitTestNG: '1.0.4',
lincheck: '2.16',
mockito: '5.1.1',
mockito: '5.2.0',
osgiUtilFunction: '1.2.0',
osgiUtilPromise: '1.3.0',
paxExam: '4.13.5',
Expand All @@ -90,8 +90,8 @@ ext {
]
pluginVersions = [
bnd: '6.4.0',
checkstyle: '10.8.0',
coveralls: '2.12.0',
checkstyle: '10.9.1',
coveralls: '2.12.2',
dependencyCheck: '8.1.2',
errorprone: '3.0.1',
findsecbugs: '1.12.0',
Expand All @@ -106,7 +106,7 @@ ext {
snyk: '0.4',
sonarqube: '4.0.0.2929',
spotbugs: '4.7.3',
spotbugsContrib: '7.4.7',
spotbugsContrib: '7.6.0',
spotbugsPlugin: '5.0.13',
versions: '0.46.0',
]
Expand Down Expand Up @@ -242,7 +242,9 @@ ext {
snyk: "gradle.plugin.io.snyk.gradle.plugin:snyk:${pluginVersions.snyk}",
sonarqube: "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:${pluginVersions.sonarqube}",
spotbugs: "com.github.spotbugs:spotbugs:${pluginVersions.spotbugs}",
spotbugsContrib: "com.mebigfatguy.sb-contrib:sb-contrib:${pluginVersions.spotbugsContrib}",
spotbugsContrib: dependencies.create("com.mebigfatguy.sb-contrib:sb-contrib:${pluginVersions.spotbugsContrib}") {
transitive = false
},
spotbugsPlugin: "com.github.spotbugs.snom:spotbugs-gradle-plugin:${pluginVersions.spotbugsPlugin}",
versions: "com.github.ben-manes:gradle-versions-plugin:${pluginVersions.versions}",
]
Expand All @@ -259,15 +261,15 @@ ext {
restrictions = [
'com.beust:jcommander': '1.82',
'com.fasterxml.jackson:jackson-bom': '2.14.2',
'com.google.protobuf:protobuf-java': '3.21.8',
'com.google.protobuf:protobuf-java': '3.22.2',
'com.thoughtworks.xstream:xstream': '1.4.20',
'net.sourceforge.nekohtml:nekohtml': '1.9.22',
'org.apache.bcel:bcel': '6.6.1',
'org.apache.commons:commons-text': '1.10.0',
'org.apache.httpcomponents:httpclient': '4.5.14',
'org.bouncycastle:bcprov-jdk15on': '1.70',
'org.jsoup:jsoup': '1.15.4',
'org.yaml:snakeyaml': '1.33',
'org.yaml:snakeyaml': '2.0',
'xerces:xercesImpl': '2.12.2',
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public boolean containsKey(K key) {
start = millis = 0L;
}

setAccessExpirationTime(key, expirable, millis);
setAccessExpireTime(key, expirable, millis);
V value = copyValue(expirable);
if (statsEnabled) {
statistics.recordHits(1L);
Expand Down Expand Up @@ -232,7 +232,7 @@ protected Map<K, Expirable<V>> getAndFilterExpiredEntries(
return true;
}
if (updateAccessTime) {
setAccessExpirationTime(entry.getKey(), entry.getValue(), millis[0]);
setAccessExpireTime(entry.getKey(), entry.getValue(), millis[0]);
}
return false;
});
Expand Down Expand Up @@ -580,7 +580,7 @@ public boolean remove(K key, V oldValue) {
removed[0] = true;
return null;
}
setAccessExpirationTime(key, expirable, millis);
setAccessExpireTime(key, expirable, millis);
return expirable;
});
dispatcher.awaitSynchronous();
Expand Down Expand Up @@ -657,7 +657,7 @@ public boolean replace(K key, V oldValue, V newValue) {
replaced[0] = true;
} else {
result = expirable;
setAccessExpirationTime(key, expirable, millis);
setAccessExpireTime(key, expirable, millis);
}
return result;
});
Expand Down Expand Up @@ -880,7 +880,7 @@ public <C extends Configuration<K, V>> C getConfiguration(Class<C> clazz) {
}
return expirable;
case READ: {
setAccessExpirationTime(entry.getKey(), expirable, 0L);
setAccessExpireTime(entry.getKey(), expirable, 0L);
return expirable;
}
case CREATED:
Expand Down Expand Up @@ -1164,7 +1164,7 @@ protected static long nanosToMillis(long nanos) {
* @param expirable the entry that was operated on
* @param currentTimeMS the current time, or 0 if not read yet
*/
protected final void setAccessExpirationTime(K key, Expirable<?> expirable, long currentTimeMS) {
protected final void setAccessExpireTime(K key, Expirable<?> expirable, long currentTimeMS) {
try {
Duration duration = expiry.getExpiryForAccess();
if (duration == null) {
Expand Down Expand Up @@ -1224,7 +1224,7 @@ public boolean hasNext() {
Map.Entry<K, Expirable<V>> entry = delegate.next();
long millis = entry.getValue().isEternal() ? 0L : currentTimeMillis();
if (!entry.getValue().hasExpired(millis)) {
setAccessExpirationTime(entry.getKey(), entry.getValue(), millis);
setAccessExpireTime(entry.getKey(), entry.getValue(), millis);
cursor = entry;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public LoadingCacheProxy(String name, Executor executor, CacheManager cacheManag

V value = null;
if (expirable != null) {
setAccessExpirationTime(key, expirable, millis);
setAccessExpireTime(key, expirable, millis);
value = copyValue(expirable);
}
if (statsEnabled) {
Expand Down

0 comments on commit 0fd27bf

Please sign in to comment.