Skip to content

Commit

Permalink
[test] Add nullability checks in tests (#279)
Browse files Browse the repository at this point in the history
Signed-off-by: Violeta Georgieva <[email protected]>
  • Loading branch information
violetagg authored Feb 12, 2025
1 parent 5758316 commit 08f2482
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.function.Consumer;

import org.assertj.core.data.Offset;
import org.jspecify.annotations.Nullable;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
Expand Down Expand Up @@ -110,7 +111,7 @@ static final PoolBuilder<PoolableTest, PoolConfig<PoolableTest>> poolableTestBui
@Test
void demonstrateAcquireInScopePipeline() throws InterruptedException {
AtomicInteger counter = new AtomicInteger();
AtomicReference<String> releaseRef = new AtomicReference<>();
AtomicReference<@Nullable String> releaseRef = new AtomicReference<>();

InstrumentedPool<String> pool =
from(Mono.just("Hello Reactive World"))
Expand Down
43 changes: 26 additions & 17 deletions reactor-pool/src/test/java/reactor/pool/CommonPoolTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.assertj.core.data.Offset;
import org.awaitility.Awaitility;
import org.hamcrest.CoreMatchers;
import org.jspecify.annotations.Nullable;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.params.provider.EnumSource;
Expand Down Expand Up @@ -1799,7 +1800,8 @@ void acquireTimeout(PoolStyle configAdjuster) {
uniqueElement.release().block();
assertThat(uniqueElement.metadata().acquireCount()).as("acquireCount post timeout-then-release").isOne();

assertThat(pool.acquire().block()).isNotNull();
PooledRef<Integer> ref = pool.acquire().block();
assertThat(ref).isNotNull();
assertThat(allocCounter).as("final allocation count").hasValue(1);
assertThat(didReset).hasValue(1);
}
Expand Down Expand Up @@ -2123,7 +2125,7 @@ void raceShutdownAndAcquireInvalidate(PoolStyle configAdjuster) {
.destroyHandler(resource -> Mono.fromRunnable(resource::decrementAndGet))
.sizeBetween(0, 1);

AtomicReference<Throwable> errorRef = new AtomicReference<>();
AtomicReference<@Nullable Throwable> errorRef = new AtomicReference<>();
for (int i = 0; i < 100_000; i++) {
errorRef.set(null);
InstrumentedPool<AtomicInteger> pool = configAdjuster.apply(configBuilder);
Expand All @@ -2146,10 +2148,11 @@ void raceShutdownAndAcquireInvalidate(PoolStyle configAdjuster) {
() -> pool.disposeLater().subscribe(v -> {}, errorRef::set)
);
}
if (errorRef.get() != null) {
errorRef.get().printStackTrace();
Throwable t = errorRef.get();
if (t != null) {
t.printStackTrace();
}
assertThat(errorRef.get()).as("exception in " + configAdjuster.toString() + " iteration " + i).isNull();
assertThat(t).as("exception in " + configAdjuster.toString() + " iteration " + i).isNull();
}
assertThat(ai).as("creates and destroys stabilizes to 0").hasValue(0);
}
Expand All @@ -2168,7 +2171,7 @@ void raceShutdownAndPreAcquiredInvalidate(PoolStyle configAdjuster) {
.destroyHandler(resource -> Mono.fromRunnable(resource::decrementAndGet))
.sizeBetween(0, 1);

AtomicReference<Throwable> errorRef = new AtomicReference<>();
AtomicReference<@Nullable Throwable> errorRef = new AtomicReference<>();
for (int i = 0; i < 100_000; i++) {
errorRef.set(null);
InstrumentedPool<AtomicInteger> pool = configAdjuster.apply(configBuilder);
Expand All @@ -2191,10 +2194,11 @@ void raceShutdownAndPreAcquiredInvalidate(PoolStyle configAdjuster) {
() -> pool.disposeLater().subscribe(v -> {}, errorRef::set)
);
}
if (errorRef.get() != null) {
errorRef.get().printStackTrace();
Throwable t = errorRef.get();
if (t != null) {
t.printStackTrace();
}
assertThat(errorRef.get()).as("exception in " + configAdjuster.toString() + " iteration " + i).isNull();
assertThat(t).as("exception in " + configAdjuster.toString() + " iteration " + i).isNull();
}
assertThat(ai).as("creates and destroys stabilizes to 0").hasValue(0);
}
Expand All @@ -2213,7 +2217,7 @@ void raceShutdownAndPreAcquiredReleaseWithEviction(PoolStyle configAdjuster) {
.destroyHandler(resource -> Mono.fromRunnable(resource::decrementAndGet))
.sizeBetween(0, 1);

AtomicReference<Throwable> errorRef = new AtomicReference<>();
AtomicReference<@Nullable Throwable> errorRef = new AtomicReference<>();
for (int i = 0; i < 100_000; i++) {
errorRef.set(null);
InstrumentedPool<AtomicInteger> pool = configAdjuster.apply(configBuilder);
Expand All @@ -2236,10 +2240,11 @@ void raceShutdownAndPreAcquiredReleaseWithEviction(PoolStyle configAdjuster) {
() -> pool.disposeLater().subscribe(v -> {}, errorRef::set)
);
}
if (errorRef.get() != null) {
errorRef.get().printStackTrace();
Throwable t = errorRef.get();
if (t != null) {
t.printStackTrace();
}
assertThat(errorRef.get()).as("exception in " + configAdjuster.toString() + " iteration " + i).isNull();
assertThat(t).as("exception in " + configAdjuster.toString() + " iteration " + i).isNull();
}
assertThat(ai).as("creates and destroys stabilizes to 0").hasValue(0);
}
Expand All @@ -2258,13 +2263,15 @@ void maxPendingZero(PoolStyle configAdjuster) {
assertThat(pool.warmup().block(Duration.ofSeconds(1))).as("warmup").isOne();

//there is one idle resource
assertThat(pool.acquire().block(Duration.ofSeconds(1)))
PooledRef<Integer> ref1 = pool.acquire().block(Duration.ofSeconds(1));
assertThat(ref1)
.as("acquire on idle")
.isNotNull()
.returns(1 , PooledRef::poolable);

//there is now idle resource, but still capacity
assertThat(pool.acquire().block(Duration.ofSeconds(1)))
PooledRef<Integer> ref2 = pool.acquire().block(Duration.ofSeconds(1));
assertThat(ref2)
.as("acquire on allocate")
.isNotNull()
.returns(2 , PooledRef::poolable);
Expand All @@ -2291,13 +2298,15 @@ void maxPendingOne(PoolStyle configAdjuster)
assertThat(pool.warmup().block(Duration.ofSeconds(1))).as("warmup").isOne();

//there is one idle resource
assertThat(pool.acquire().block(Duration.ofSeconds(1)))
PooledRef<Integer> ref1 = pool.acquire().block(Duration.ofSeconds(1));
assertThat(ref1)
.as("acquire on idle")
.isNotNull()
.returns(1 , PooledRef::poolable);

//there is now idle resource, but still capacity
assertThat(pool.acquire().block(Duration.ofSeconds(1)))
PooledRef<Integer> ref2 = pool.acquire().block(Duration.ofSeconds(1));
assertThat(ref2)
.as("acquire on allocate")
.isNotNull()
.returns(2 , PooledRef::poolable);
Expand Down
5 changes: 3 additions & 2 deletions reactor-pool/src/test/java/reactor/pool/PoolWarmupTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2023 VMware Inc. or its affiliates, All Rights Reserved.
* Copyright (c) 2019-2025 VMware Inc. or its affiliates, All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,6 +16,7 @@

package reactor.pool;

import org.jspecify.annotations.Nullable;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
Expand Down Expand Up @@ -94,7 +95,7 @@ protected static Stream<Arguments> warmupTestArgs() {
* Each DBConnection will use one of the following DBConnection Executor
*/
final static class DBConnectionThread implements Executor {
final static ThreadLocal<DBConnectionThread> current = ThreadLocal.withInitial(() -> null);
final static ThreadLocal<@Nullable DBConnectionThread> current = ThreadLocal.withInitial(() -> null);

final ExecutorService dbThread;
final AtomicBoolean used = new AtomicBoolean(false);
Expand Down

0 comments on commit 08f2482

Please sign in to comment.