Skip to content

Commit 7bdcb59

Browse files
authored
2.x: Fix Flowable.singleOrError().toFlowable() not signalling NSEE (#5904)
1 parent 5e5d5a2 commit 7bdcb59

File tree

5 files changed

+36
-6
lines changed

5 files changed

+36
-6
lines changed

src/main/java/io/reactivex/internal/operators/flowable/FlowableSingle.java

+16-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
package io.reactivex.internal.operators.flowable;
1515

16+
import java.util.NoSuchElementException;
17+
1618
import org.reactivestreams.*;
1719

1820
import io.reactivex.*;
@@ -23,14 +25,17 @@ public final class FlowableSingle<T> extends AbstractFlowableWithUpstream<T, T>
2325

2426
final T defaultValue;
2527

26-
public FlowableSingle(Flowable<T> source, T defaultValue) {
28+
final boolean failOnEmpty;
29+
30+
public FlowableSingle(Flowable<T> source, T defaultValue, boolean failOnEmpty) {
2731
super(source);
2832
this.defaultValue = defaultValue;
33+
this.failOnEmpty = failOnEmpty;
2934
}
3035

3136
@Override
3237
protected void subscribeActual(Subscriber<? super T> s) {
33-
source.subscribe(new SingleElementSubscriber<T>(s, defaultValue));
38+
source.subscribe(new SingleElementSubscriber<T>(s, defaultValue, failOnEmpty));
3439
}
3540

3641
static final class SingleElementSubscriber<T> extends DeferredScalarSubscription<T>
@@ -40,13 +45,16 @@ static final class SingleElementSubscriber<T> extends DeferredScalarSubscription
4045

4146
final T defaultValue;
4247

48+
final boolean failOnEmpty;
49+
4350
Subscription s;
4451

4552
boolean done;
4653

47-
SingleElementSubscriber(Subscriber<? super T> actual, T defaultValue) {
54+
SingleElementSubscriber(Subscriber<? super T> actual, T defaultValue, boolean failOnEmpty) {
4855
super(actual);
4956
this.defaultValue = defaultValue;
57+
this.failOnEmpty = failOnEmpty;
5058
}
5159

5260
@Override
@@ -94,7 +102,11 @@ public void onComplete() {
94102
v = defaultValue;
95103
}
96104
if (v == null) {
97-
actual.onComplete();
105+
if (failOnEmpty) {
106+
actual.onError(new NoSuchElementException());
107+
} else {
108+
actual.onComplete();
109+
}
98110
} else {
99111
complete(v);
100112
}

src/main/java/io/reactivex/internal/operators/flowable/FlowableSingleMaybe.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ protected void subscribeActual(MaybeObserver<? super T> s) {
3636

3737
@Override
3838
public Flowable<T> fuseToFlowable() {
39-
return RxJavaPlugins.onAssembly(new FlowableSingle<T>(source, null));
39+
return RxJavaPlugins.onAssembly(new FlowableSingle<T>(source, null, false));
4040
}
4141

4242
static final class SingleElementSubscriber<T>

src/main/java/io/reactivex/internal/operators/flowable/FlowableSingleSingle.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ protected void subscribeActual(SingleObserver<? super T> s) {
4141

4242
@Override
4343
public Flowable<T> fuseToFlowable() {
44-
return RxJavaPlugins.onAssembly(new FlowableSingle<T>(source, defaultValue));
44+
return RxJavaPlugins.onAssembly(new FlowableSingle<T>(source, defaultValue, true));
4545
}
4646

4747
static final class SingleElementSubscriber<T>

src/test/java/io/reactivex/internal/operators/flowable/FlowableSingleTest.java

+9
Original file line numberDiff line numberDiff line change
@@ -796,4 +796,13 @@ public void cancelAsFlowable() {
796796

797797
assertFalse(pp.hasSubscribers());
798798
}
799+
800+
@Test
801+
public void singleOrError() {
802+
Flowable.empty()
803+
.singleOrError()
804+
.toFlowable()
805+
.test()
806+
.assertFailure(NoSuchElementException.class);
807+
}
799808
}

src/test/java/io/reactivex/internal/operators/observable/ObservableSingleTest.java

+9
Original file line numberDiff line numberDiff line change
@@ -556,4 +556,13 @@ public MaybeSource<Object> apply(Observable<Object> o) throws Exception {
556556
}
557557
});
558558
}
559+
560+
@Test
561+
public void singleOrError() {
562+
Observable.empty()
563+
.singleOrError()
564+
.toObservable()
565+
.test()
566+
.assertFailure(NoSuchElementException.class);
567+
}
559568
}

0 commit comments

Comments
 (0)