Revise contribution

See gh-35380
This commit is contained in:
Sam Brannen
2025-10-30 16:13:38 +01:00
parent a41af448ec
commit 2d3c2e3feb
3 changed files with 63 additions and 49 deletions
@@ -17,9 +17,7 @@
package org.springframework.util.function;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicInteger;
@@ -29,123 +27,129 @@ import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
/**
* Tests for {@link SingletonSupplier}.
*
* @author Dmytro Nosan
* @author Sam Brannen
* @since 7.0
*/
class SingletonSupplierTests {
@Test
void shouldReturnDefaultWhenInstanceSupplierReturnsNull() {
SingletonSupplier<String> singletonSupplier = new SingletonSupplier<>(() -> null, () -> "Default");
var singletonSupplier = new SingletonSupplier<>(() -> null, () -> "Default");
assertThat(singletonSupplier.get()).isEqualTo("Default");
}
@Test
void shouldReturnNullForOfNullableWithNullInstance() {
SingletonSupplier<String> singletonSupplier = SingletonSupplier.ofNullable((String) null);
var singletonSupplier = SingletonSupplier.ofNullable((String) null);
assertThat(singletonSupplier).isNull();
}
@Test
void shouldReturnNullForOfNullableWithNullSupplier() {
SingletonSupplier<String> singletonSupplier = SingletonSupplier.ofNullable((Supplier<String>) null);
var singletonSupplier = SingletonSupplier.ofNullable((Supplier<String>) null);
assertThat(singletonSupplier).isNull();
}
@Test
void shouldReturnNullWhenAllSuppliersReturnNull() {
SingletonSupplier<String> singletonSupplier = new SingletonSupplier<>(() -> null, () -> null);
var singletonSupplier = new SingletonSupplier<>(() -> null, () -> null);
assertThat(singletonSupplier.get()).isNull();
}
@Test
void shouldReturnNullWhenNoInstanceOrDefaultSupplier() {
SingletonSupplier<String> singletonSupplier = new SingletonSupplier<>((String) null, null);
var singletonSupplier = new SingletonSupplier<>((String) null, null);
assertThat(singletonSupplier.get()).isNull();
}
@Test
void shouldReturnSingletonInstanceOnMultipleCalls() {
SingletonSupplier<String> singletonSupplier = SingletonSupplier.of("Hello");
var singletonSupplier = SingletonSupplier.of("Hello");
assertThat(singletonSupplier.get()).isEqualTo("Hello");
assertThat(singletonSupplier.get()).isEqualTo("Hello");
}
@Test
void shouldReturnSingletonInstanceOnMultipleSupplierCalls() {
SingletonSupplier<String> singletonSupplier = SingletonSupplier.of(new HelloStringSupplier());
var singletonSupplier = SingletonSupplier.of(new HelloStringSupplier());
assertThat(singletonSupplier.get()).isEqualTo("Hello 0");
assertThat(singletonSupplier.get()).isEqualTo("Hello 0");
}
@Test
void shouldReturnSupplierForOfNullableWithNonNullInstance() {
SingletonSupplier<String> singletonSupplier = SingletonSupplier.ofNullable("Hello");
var singletonSupplier = SingletonSupplier.ofNullable("Hello");
assertThat(singletonSupplier).isNotNull();
assertThat(singletonSupplier.get()).isEqualTo("Hello");
}
@Test
void shouldReturnSupplierForOfNullableWithNonNullSupplier() {
SingletonSupplier<String> singletonSupplier = SingletonSupplier.ofNullable(() -> "Hello");
var singletonSupplier = SingletonSupplier.ofNullable(() -> "Hello");
assertThat(singletonSupplier).isNotNull();
assertThat(singletonSupplier.get()).isEqualTo("Hello");
}
@Test
void shouldThrowWhenObtainCalledAndNoInstanceAvailable() {
SingletonSupplier<String> singletonSupplier = new SingletonSupplier<>((String) null, null);
assertThatThrownBy(singletonSupplier::obtain).isInstanceOf(IllegalStateException.class)
.hasMessage("No instance from Supplier");
var singletonSupplier = new SingletonSupplier<>((String) null, null);
assertThatIllegalStateException()
.isThrownBy(singletonSupplier::obtain)
.withMessage("No instance from Supplier");
}
@Test
void shouldUseDefaultSupplierWhenInstanceIsNull() {
SingletonSupplier<String> singletonSupplier = new SingletonSupplier<>((String) null, () -> "defaultSupplier");
var singletonSupplier = new SingletonSupplier<>((String) null, () -> "defaultSupplier");
assertThat(singletonSupplier.get()).isEqualTo("defaultSupplier");
}
@Test
void shouldUseDefaultSupplierWhenInstanceSupplierReturnsNull() {
SingletonSupplier<String> singletonSupplier = new SingletonSupplier<>((Supplier<String>) null, () -> "defaultSupplier");
var singletonSupplier = new SingletonSupplier<>((Supplier<String>) null, () -> "defaultSupplier");
assertThat(singletonSupplier.get()).isEqualTo("defaultSupplier");
}
@Test
void shouldUseInstanceSupplierWhenProvidedAndIgnoreDefaultSupplier() {
AtomicInteger defaultValue = new AtomicInteger();
SingletonSupplier<Integer> singletonSupplier = new SingletonSupplier<>(() -> -1, defaultValue::incrementAndGet);
var defaultValue = new AtomicInteger();
var singletonSupplier = new SingletonSupplier<>(() -> -1, defaultValue::incrementAndGet);
assertThat(singletonSupplier.get()).isEqualTo(-1);
assertThat(defaultValue.get()).isEqualTo(0);
assertThat(defaultValue.get()).isZero();
}
@Test
void shouldUseInstanceWhenProvidedAndIgnoreDefaultSupplier() {
AtomicInteger defaultValue = new AtomicInteger();
SingletonSupplier<Integer> singletonSupplier = new SingletonSupplier<>(-1, defaultValue::incrementAndGet);
var defaultValue = new AtomicInteger();
var singletonSupplier = new SingletonSupplier<>(-1, defaultValue::incrementAndGet);
assertThat(singletonSupplier.get()).isEqualTo(-1);
assertThat(defaultValue.get()).isEqualTo(0);
assertThat(defaultValue.get()).isZero();
}
@Test
void shouldReturnConsistentlyNullSingletonInstanceOnMultipleSupplierCalls() {
SingletonSupplier<String> singletonSupplier = SingletonSupplier.of(new Supplier<>() {
int count = 0;
@Override
public String get() {
if (this.count++ == 0) {
return null;
}
return "Hello";
}
});
var count = new AtomicInteger();
var singletonSupplier = SingletonSupplier.of(() -> (count.getAndIncrement() == 0 ? null : "Hello"));
assertThat(singletonSupplier.get()).isNull();
assertThat(singletonSupplier.get()).isNull();
@@ -154,11 +158,12 @@ class SingletonSupplierTests {
@RepeatedTest(100)
void shouldReturnSingletonInstanceOnMultipleConcurrentSupplierCalls() throws Exception {
int numberOfThreads = 4;
CountDownLatch ready = new CountDownLatch(numberOfThreads);
CountDownLatch start = new CountDownLatch(1);
List<Future<String>> futures = new ArrayList<>();
SingletonSupplier<String> singletonSupplier = SingletonSupplier.of(new HelloStringSupplier());
ExecutorService executorService = Executors.newFixedThreadPool(numberOfThreads);
var ready = new CountDownLatch(numberOfThreads);
var start = new CountDownLatch(1);
var futures = new ArrayList<Future<String>>();
var singletonSupplier = SingletonSupplier.of(new HelloStringSupplier());
var executorService = Executors.newFixedThreadPool(numberOfThreads);
try {
for (int i = 0; i < numberOfThreads; i++) {
futures.add(executorService.submit(() -> {
@@ -179,7 +184,7 @@ class SingletonSupplierTests {
private static final class HelloStringSupplier implements Supplier<String> {
private final AtomicInteger count = new AtomicInteger();
final AtomicInteger count = new AtomicInteger();
@Override
public String get() {