From e654e9b0274521a45abfff9719ec74509009594b Mon Sep 17 00:00:00 2001 From: Niklas Baudy Date: Fri, 7 Oct 2016 11:48:01 +0200 Subject: [PATCH] 2.x: Fix Flowable + Single elementAt and elementAtOrError operators on empty sources (#4681) --- .../flowable/FlowableElementAtMaybe.java | 2 +- .../flowable/FlowableElementAtSingle.java | 2 +- .../observable/ObservableElementAtMaybe.java | 2 +- .../observable/ObservableElementAtSingle.java | 2 +- .../flowable/FlowableElementAtTest.java | 40 +++++++++++++++++++ .../observable/ObservableElementAtTest.java | 40 +++++++++++++++++++ 6 files changed, 84 insertions(+), 4 deletions(-) diff --git a/src/main/java/io/reactivex/internal/operators/flowable/FlowableElementAtMaybe.java b/src/main/java/io/reactivex/internal/operators/flowable/FlowableElementAtMaybe.java index bac3e7a982..b2ee24f2c0 100644 --- a/src/main/java/io/reactivex/internal/operators/flowable/FlowableElementAtMaybe.java +++ b/src/main/java/io/reactivex/internal/operators/flowable/FlowableElementAtMaybe.java @@ -97,7 +97,7 @@ public void onError(Throwable t) { @Override public void onComplete() { s = SubscriptionHelper.CANCELLED; - if (index <= count && !done) { + if (!done) { done = true; actual.onComplete(); } diff --git a/src/main/java/io/reactivex/internal/operators/flowable/FlowableElementAtSingle.java b/src/main/java/io/reactivex/internal/operators/flowable/FlowableElementAtSingle.java index 4816d67a82..a53c6421d3 100644 --- a/src/main/java/io/reactivex/internal/operators/flowable/FlowableElementAtSingle.java +++ b/src/main/java/io/reactivex/internal/operators/flowable/FlowableElementAtSingle.java @@ -103,7 +103,7 @@ public void onError(Throwable t) { @Override public void onComplete() { s = SubscriptionHelper.CANCELLED; - if (index <= count && !done) { + if (!done) { done = true; T v = defaultValue; diff --git a/src/main/java/io/reactivex/internal/operators/observable/ObservableElementAtMaybe.java b/src/main/java/io/reactivex/internal/operators/observable/ObservableElementAtMaybe.java index 1eb6063445..5a5dc024bb 100644 --- a/src/main/java/io/reactivex/internal/operators/observable/ObservableElementAtMaybe.java +++ b/src/main/java/io/reactivex/internal/operators/observable/ObservableElementAtMaybe.java @@ -98,7 +98,7 @@ public void onError(Throwable t) { @Override public void onComplete() { - if (index <= count && !done) { + if (!done) { done = true; actual.onComplete(); } diff --git a/src/main/java/io/reactivex/internal/operators/observable/ObservableElementAtSingle.java b/src/main/java/io/reactivex/internal/operators/observable/ObservableElementAtSingle.java index dcc61de06a..a13a9a9843 100644 --- a/src/main/java/io/reactivex/internal/operators/observable/ObservableElementAtSingle.java +++ b/src/main/java/io/reactivex/internal/operators/observable/ObservableElementAtSingle.java @@ -99,7 +99,7 @@ public void onError(Throwable t) { @Override public void onComplete() { - if (index <= count && !done) { + if (!done) { done = true; T v = defaultValue; diff --git a/src/test/java/io/reactivex/internal/operators/flowable/FlowableElementAtTest.java b/src/test/java/io/reactivex/internal/operators/flowable/FlowableElementAtTest.java index aa9b72465d..f3ed4ec1f7 100644 --- a/src/test/java/io/reactivex/internal/operators/flowable/FlowableElementAtTest.java +++ b/src/test/java/io/reactivex/internal/operators/flowable/FlowableElementAtTest.java @@ -135,4 +135,44 @@ public void elementAtOrErrorError() { .assertErrorMessage("error") .assertError(RuntimeException.class); } + + @Test + public void elementAtIndex0OnEmptySource() { + Flowable.empty() + .elementAt(0) + .test() + .assertResult(); + } + + @Test + public void elementAtIndex0WithDefaultOnEmptySource() { + Flowable.empty() + .elementAt(0, 5) + .test() + .assertResult(5); + } + + @Test + public void elementAtIndex1OnEmptySource() { + Flowable.empty() + .elementAt(1) + .test() + .assertResult(); + } + + @Test + public void elementAtIndex1WithDefaultOnEmptySource() { + Flowable.empty() + .elementAt(1, 10) + .test() + .assertResult(10); + } + + @Test + public void elementAtOrErrorIndex1OnEmptySource() { + Flowable.empty() + .elementAtOrError(1) + .test() + .assertFailure(NoSuchElementException.class); + } } diff --git a/src/test/java/io/reactivex/internal/operators/observable/ObservableElementAtTest.java b/src/test/java/io/reactivex/internal/operators/observable/ObservableElementAtTest.java index c37d48b31d..4df2b6c6a6 100644 --- a/src/test/java/io/reactivex/internal/operators/observable/ObservableElementAtTest.java +++ b/src/test/java/io/reactivex/internal/operators/observable/ObservableElementAtTest.java @@ -126,4 +126,44 @@ public void elementAtOrErrorError() { .assertErrorMessage("error") .assertError(RuntimeException.class); } + + @Test + public void elementAtIndex0OnEmptySource() { + Observable.empty() + .elementAt(0) + .test() + .assertResult(); + } + + @Test + public void elementAtIndex0WithDefaultOnEmptySource() { + Observable.empty() + .elementAt(0, 5) + .test() + .assertResult(5); + } + + @Test + public void elementAtIndex1OnEmptySource() { + Observable.empty() + .elementAt(1) + .test() + .assertResult(); + } + + @Test + public void elementAtIndex1WithDefaultOnEmptySource() { + Observable.empty() + .elementAt(1, 10) + .test() + .assertResult(10); + } + + @Test + public void elementAtOrErrorIndex1OnEmptySource() { + Observable.empty() + .elementAtOrError(1) + .test() + .assertFailure(NoSuchElementException.class); + } }