Single.never not singleton

Signed-off-by: Daniel Kec <daniel.kec@oracle.com>
This commit is contained in:
Daniel Kec
2020-09-10 18:35:25 +02:00
committed by Santiago Pericasgeertsen
parent 2a23a68e20
commit 85a77ca335
3 changed files with 22 additions and 14 deletions

View File

@@ -228,7 +228,7 @@ public interface Single<T> extends Subscribable<T>, CompletionStage<T>, Awaitabl
* @return Single
*/
static <T> Single<T> never() {
return SingleNever.instance();
return new SingleNever<T>();
}
/**

View File

@@ -21,24 +21,16 @@ import java.util.concurrent.Flow.Subscriber;
* Implementation of {@link Single} that never invokes
* {@link Subscriber#onComplete()} or
* {@link Subscriber#onError(java.lang.Throwable)}.
*
* @param <T> item type
*/
final class SingleNever extends CompletionSingle<Object> {
final class SingleNever<T> extends CompletionSingle<T> {
/**
* Singleton instance.
*/
private static final SingleNever INSTANCE = new SingleNever();
private SingleNever() {
SingleNever() {
}
@Override
public void subscribe(Subscriber<? super Object> actual) {
public void subscribe(Subscriber<? super T> actual) {
actual.onSubscribe(EmptySubscription.INSTANCE);
}
@SuppressWarnings("unchecked")
static <T> Single<T> instance() {
return (Single<T>) INSTANCE;
}
}

View File

@@ -157,6 +157,22 @@ public class SingleTest {
assertThat(subscriber.getItems(), is(empty()));
}
@Test
public void testNeverIsNotSingleton() throws InterruptedException, TimeoutException, ExecutionException {
CompletableFuture<Void> cf1 = new CompletableFuture<>();
CompletableFuture<Void> cf2 = new CompletableFuture<>();
Single<Object> never1 = Single.never();
Single<Object> never2 = Single.never();
never1.onCancel(() -> cf1.complete(null));
never2.onCancel(() -> cf2.complete(null));
never1.cancel();
cf1.get(100, TimeUnit.MILLISECONDS);
assertThat("First Single.never should be cancelled!", cf1.isDone());
assertThat("Other Single.never should NOT be cancelled!", !cf2.isDone());
}
@Test
public void testMap() {
SingleTestSubscriber<String> subscriber = new SingleTestSubscriber<>();