Skip to content

Commit

Permalink
Enable CheckReturnValue in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-manes committed Feb 20, 2023
1 parent 1803278 commit b4c92e4
Show file tree
Hide file tree
Showing 62 changed files with 729 additions and 269 deletions.
8 changes: 0 additions & 8 deletions .github/workflows/qodana.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,3 @@ jobs:
uses: github/codeql-action/upload-sarif@v2
with:
sarif_file: ${{ runner.temp }}/qodana/results/qodana.sarif.json
- name: View Instructions
run: |
echo "Download and extract the report artifact"
echo "Start a http server"
echo "... python2 -m SimpleHTTPServer"
echo "... python3 -m http.server"
echo "The report is available at http://localhost:8000"
echo "For more details see https://www.jetbrains.com/help/qodana"
1 change: 0 additions & 1 deletion caffeine/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ tasks.named('compileJava').configure {
}
tasks.named('compileTestJava').configure {
dependsOn jar, compileCodeGenJava
options.errorprone.disable('CheckReturnValue')
}
tasks.named('sourcesJar').configure {
dependsOn generateLocalCaches, generateNodes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
* @param <K> the type of keys maintained by this map
* @param <V> the type of mapped values
*/
@SuppressWarnings({"all", "deprecation", "CheckReturnValue", "JdkObsolete", "rawtypes", "serial",
@SuppressWarnings({"all", "CheckReturnValue", "deprecation", "JdkObsolete", "rawtypes", "serial",
"unchecked", "UnnecessaryParentheses", "UnusedNestedClass", "UnusedVariable", "YodaCondition"})
public class ConcurrentHashMapV7<K, V> extends AbstractMap<K, V>
implements ConcurrentMap<K, V>, Serializable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public abstract class ProfilerHook {
calls = new LongAdder();
}

@SuppressWarnings("CheckReturnValue")
public final void run() {
scheduleStatusTask();
ConcurrentTestHarness.timeTasks(NUM_THREADS, this::profile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2220,8 +2220,8 @@ public boolean containsValue(Object value) {

@Override
public Map<K, V> getAllPresent(Iterable<? extends K> keys) {
var result = new LinkedHashMap<Object, Object>(calculateHashMapCapacity(keys));
for (Object key : keys) {
var result = new LinkedHashMap<K, V>(calculateHashMapCapacity(keys));
for (K key : keys) {
result.put(key, null);
}

Expand All @@ -2235,9 +2235,7 @@ public Map<K, V> getAllPresent(Iterable<? extends K> keys) {
iter.remove();
} else {
if (!isComputingAsync(node)) {
@SuppressWarnings("unchecked")
K castedKey = (K) entry.getKey();
tryExpireAfterRead(node, castedKey, value, expiry(), now);
tryExpireAfterRead(node, entry.getKey(), value, expiry(), now);
setAccessTime(node, now);
}
V refreshed = afterRead(node, now, /* recordHit */ false);
Expand All @@ -2247,9 +2245,7 @@ public Map<K, V> getAllPresent(Iterable<? extends K> keys) {
statsCounter().recordHits(result.size());
statsCounter().recordMisses(uniqueKeys - result.size());

@SuppressWarnings("unchecked")
Map<K, V> castedResult = (Map<K, V>) result;
return Collections.unmodifiableMap(castedResult);
return Collections.unmodifiableMap(result);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -486,17 +486,17 @@ abstract class AbstractCacheView<K, V> implements Cache<K, V>, Serializable {

@Override
public Map<K, V> getAllPresent(Iterable<? extends K> keys) {
var result = new LinkedHashMap<Object, Object>(calculateHashMapCapacity(keys));
for (Object key : keys) {
var result = new LinkedHashMap<K, V>(calculateHashMapCapacity(keys));
for (K key : keys) {
result.put(key, null);
}

int uniqueKeys = result.size();
for (var iter = result.entrySet().iterator(); iter.hasNext();) {
Map.Entry<Object, Object> entry = iter.next();
Map.Entry<K, V> entry = iter.next();

CompletableFuture<V> future = asyncCache().cache().get(entry.getKey());
Object value = Async.getIfReady(future);
V value = Async.getIfReady(future);
if (value == null) {
iter.remove();
} else {
Expand All @@ -506,9 +506,7 @@ public Map<K, V> getAllPresent(Iterable<? extends K> keys) {
asyncCache().cache().statsCounter().recordHits(result.size());
asyncCache().cache().statsCounter().recordMisses(uniqueKeys - result.size());

@SuppressWarnings("unchecked")
var castedResult = (Map<K, V>) result;
return Collections.unmodifiableMap(castedResult);
return Collections.unmodifiableMap(result);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +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;
int features = 0
| ((refreshableAt == unsetTicks) ? 0b000 : 0b100)
| ((expiresAt == unsetTicks) ? 0b000 : 0b010)
| ((weight < 0) || (weight == 1) ? 0b000 : 0b001);
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);
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 @@ -145,15 +145,15 @@ public long estimatedSize() {

@Override
public Map<K, V> getAllPresent(Iterable<? extends K> keys) {
var result = new LinkedHashMap<Object, Object>(calculateHashMapCapacity(keys));
for (Object key : keys) {
var result = new LinkedHashMap<K, V>(calculateHashMapCapacity(keys));
for (K key : keys) {
result.put(key, null);
}

int uniqueKeys = result.size();
for (var iter = result.entrySet().iterator(); iter.hasNext();) {
Map.Entry<Object, Object> entry = iter.next();
Object value = data.get(entry.getKey());
Map.Entry<K, V> entry = iter.next();
V value = data.get(entry.getKey());
if (value == null) {
iter.remove();
} else {
Expand All @@ -163,9 +163,7 @@ public Map<K, V> getAllPresent(Iterable<? extends K> keys) {
statsCounter.recordHits(result.size());
statsCounter.recordMisses(uniqueKeys - result.size());

@SuppressWarnings("unchecked")
var castedResult = (Map<K, V>) result;
return Collections.unmodifiableMap(castedResult);
return Collections.unmodifiableMap(result);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public final class AsyncCacheTest {

/* --------------- getIfPresent --------------- */

@SuppressWarnings("CheckReturnValue")
@CacheSpec(removalListener = { Listener.DISABLED, Listener.REJECTING })
@Test(dataProvider = "caches", expectedExceptions = NullPointerException.class)
public void getIfPresent_nullKey(AsyncCache<Int, Int> cache, CacheContext context) {
Expand All @@ -107,18 +108,21 @@ public void getIfPresent_present(AsyncCache<Int, Int> cache, CacheContext contex
/* --------------- getFunc --------------- */

@CacheSpec
@SuppressWarnings("CheckReturnValue")
@Test(dataProvider = "caches", expectedExceptions = NullPointerException.class)
public void getFunc_nullKey(AsyncCache<Int, Int> cache, CacheContext context) {
cache.get(null, key -> null);
}

@CacheSpec
@SuppressWarnings("CheckReturnValue")
@Test(dataProvider = "caches", expectedExceptions = NullPointerException.class)
public void getFunc_nullLoader(AsyncCache<Int, Int> cache, CacheContext context) {
cache.get(context.absentKey(), (Function<Int, Int>) null);
}

@CacheSpec
@SuppressWarnings("CheckReturnValue")
@Test(dataProvider = "caches", expectedExceptions = NullPointerException.class)
public void getFunc_nullKeyAndLoader(AsyncCache<Int, Int> cache, CacheContext context) {
cache.get(null, (Function<Int, Int>) null);
Expand Down Expand Up @@ -227,26 +231,30 @@ public void getFunc_present(AsyncCache<Int, Int> cache, CacheContext context) {
/* --------------- getBiFunc --------------- */

@CacheSpec
@SuppressWarnings("CheckReturnValue")
@Test(dataProvider = "caches", expectedExceptions = NullPointerException.class)
public void getBiFunc_nullKey(AsyncCache<Int, Int> cache, CacheContext context) {
cache.get(null, (key, executor) -> CompletableFuture.completedFuture(null));
}

@CacheSpec
@SuppressWarnings("CheckReturnValue")
@Test(dataProvider = "caches", expectedExceptions = NullPointerException.class)
public void getBiFunc_nullLoader(AsyncCache<Int, Int> cache, CacheContext context) {
BiFunction<Int, Executor, CompletableFuture<Int>> mappingFunction = null;
cache.get(context.absentKey(), mappingFunction);
}

@CacheSpec
@SuppressWarnings("CheckReturnValue")
@Test(dataProvider = "caches", expectedExceptions = NullPointerException.class)
public void getBiFunc_nullKeyAndLoader(AsyncCache<Int, Int> cache, CacheContext context) {
BiFunction<Int, Executor, CompletableFuture<Int>> mappingFunction = null;
cache.get(null, mappingFunction);
}

@CacheSpec
@SuppressWarnings("CheckReturnValue")
@Test(dataProvider = "caches", expectedExceptions = IllegalStateException.class)
public void getBiFunc_throwsException(AsyncCache<Int, Int> cache, CacheContext context) {
try {
Expand All @@ -258,6 +266,7 @@ public void getBiFunc_throwsException(AsyncCache<Int, Int> cache, CacheContext c
}

@CacheSpec
@SuppressWarnings("CheckReturnValue")
@Test(dataProvider = "caches", expectedExceptions = UnknownError.class)
public void getBiFunc_throwsError(AsyncCache<Int, Int> cache, CacheContext context) {
try {
Expand All @@ -269,6 +278,7 @@ public void getBiFunc_throwsError(AsyncCache<Int, Int> cache, CacheContext conte
}

@CacheSpec
@SuppressWarnings("CheckReturnValue")
@Test(dataProvider = "caches", expectedExceptions = NullPointerException.class)
public void getBiFunc_absent_null(AsyncCache<Int, Int> cache, CacheContext context) {
cache.get(context.absentKey(), (k, executor) -> null);
Expand Down Expand Up @@ -304,6 +314,7 @@ public void getBiFunc_absent_failure_after(AsyncCache<Int, Int> cache, CacheCont

@CacheSpec
@Test(dataProvider = "caches")
@SuppressWarnings("CheckReturnValue")
public void getBiFunc_absent_cancelled(AsyncCache<Int, Int> cache, CacheContext context) {
var cancelledFuture = new CompletableFuture<Int>();
cache.get(context.absentKey(), (k, executor) -> cancelledFuture);
Expand Down Expand Up @@ -339,13 +350,15 @@ public void getBiFunc_present(AsyncCache<Int, Int> cache, CacheContext context)
/* --------------- getAllFunc --------------- */

@CheckNoStats
@SuppressWarnings("CheckReturnValue")
@Test(dataProvider = "caches", expectedExceptions = NullPointerException.class)
@CacheSpec(removalListener = { Listener.DISABLED, Listener.REJECTING })
public void getAllFunction_nullKeys(AsyncCache<Int, Int> cache, CacheContext context) {
cache.getAll(null, keys -> { throw new AssertionError(); });
}

@CheckNoStats
@SuppressWarnings("CheckReturnValue")
@Test(dataProvider = "caches", expectedExceptions = NullPointerException.class)
@CacheSpec(removalListener = { Listener.DISABLED, Listener.REJECTING })
public void getAllFunction_nullKeys_nullFunction(
Expand All @@ -354,13 +367,15 @@ public void getAllFunction_nullKeys_nullFunction(
}

@CheckNoStats
@SuppressWarnings("CheckReturnValue")
@Test(dataProvider = "caches", expectedExceptions = NullPointerException.class)
@CacheSpec(removalListener = { Listener.DISABLED, Listener.REJECTING })
public void getAllFunction_nullFunction(AsyncCache<Int, Int> cache, CacheContext context) {
cache.getAll(context.original().keySet(), (Function<Set<? extends Int>, Map<Int, Int>>) null);
}

@CheckNoStats
@SuppressWarnings("CheckReturnValue")
@Test(dataProvider = "caches", expectedExceptions = NullPointerException.class)
@CacheSpec(removalListener = { Listener.DISABLED, Listener.REJECTING })
public void getAllFunction_nullKey(AsyncCache<Int, Int> cache, CacheContext context) {
Expand Down Expand Up @@ -564,13 +579,15 @@ public void getAllFunction_badLoader(AsyncCache<Int, Int> cache, CacheContext co
/* --------------- getAllBiFunc --------------- */

@CheckNoStats
@SuppressWarnings("CheckReturnValue")
@Test(dataProvider = "caches", expectedExceptions = NullPointerException.class)
@CacheSpec(removalListener = { Listener.DISABLED, Listener.REJECTING })
public void getAllBifunction_nullKeys(AsyncCache<Int, Int> cache, CacheContext context) {
cache.getAll(null, (keys, executor) -> { throw new AssertionError(); });
}

@CheckNoStats
@SuppressWarnings("CheckReturnValue")
@Test(dataProvider = "caches", expectedExceptions = NullPointerException.class)
@CacheSpec(removalListener = { Listener.DISABLED, Listener.REJECTING })
public void getAllBifunction_nullKeys_nullBifunction(
Expand All @@ -580,6 +597,7 @@ public void getAllBifunction_nullKeys_nullBifunction(
}

@CheckNoStats
@SuppressWarnings("CheckReturnValue")
@Test(dataProvider = "caches", expectedExceptions = NullPointerException.class)
@CacheSpec(removalListener = { Listener.DISABLED, Listener.REJECTING })
public void getAllBifunction_nullBifunction(AsyncCache<Int, Int> cache, CacheContext context) {
Expand All @@ -588,6 +606,7 @@ public void getAllBifunction_nullBifunction(AsyncCache<Int, Int> cache, CacheCon
}

@CheckNoStats
@SuppressWarnings("CheckReturnValue")
@Test(dataProvider = "caches", expectedExceptions = NullPointerException.class)
@CacheSpec(removalListener = { Listener.DISABLED, Listener.REJECTING })
public void getAllBifunction_nullKey(AsyncCache<Int, Int> cache, CacheContext context) {
Expand Down Expand Up @@ -615,6 +634,7 @@ public void getAllBiFunction_nullLookup(AsyncCache<Int, Int> cache, CacheContext
}

@CacheSpec
@SuppressWarnings("CheckReturnValue")
@Test(dataProvider = "caches", expectedExceptions = UnsupportedOperationException.class)
public void getAllBifunction_immutable_keys(AsyncCache<Int, Int> cache, CacheContext context) {
cache.getAll(context.absentKeys(), (keys, executor) -> {
Expand Down Expand Up @@ -642,6 +662,7 @@ public void getAllBifunction_absent_null(AsyncCache<Int, Int> cache, CacheContex
}

@CacheSpec
@SuppressWarnings("CheckReturnValue")
@Test(dataProvider = "caches", expectedExceptions = NullPointerException.class)
public void getAllBifunction_absent_nullValue(AsyncCache<Int, Int> cache, CacheContext context) {
cache.getAll(context.absentKeys(), (keys, executor) -> null);
Expand All @@ -658,6 +679,7 @@ public void getAllBifunction_absent_failure(AsyncCache<Int, Int> cache, CacheCon
}

@CacheSpec
@SuppressWarnings("CheckReturnValue")
@Test(dataProvider = "caches", expectedExceptions = IllegalStateException.class)
public void getAllBifunction_absent_throwsException(
AsyncCache<Int, Int> cache, CacheContext context) {
Expand All @@ -672,6 +694,7 @@ public void getAllBifunction_absent_throwsException(
}

@CacheSpec
@SuppressWarnings("CheckReturnValue")
@Test(dataProvider = "caches", expectedExceptions = UnknownError.class)
public void getAllBifunction_absent_throwsError(
AsyncCache<Int, Int> cache, CacheContext context) {
Expand Down Expand Up @@ -829,6 +852,7 @@ public void getAllBifunction_present_ordered_exceeds(
}

@Test(dataProvider = "caches")
@SuppressWarnings("CheckReturnValue")
@CacheSpec(removalListener = { Listener.DISABLED, Listener.REJECTING })
public void getAllBifunction_badLoader(AsyncCache<Int, Int> cache, CacheContext context) {
try {
Expand Down
Loading

0 comments on commit b4c92e4

Please sign in to comment.