Skip to content

Commit

Permalink
2.x: Fix Flowable + Single elementAt and elementAtOrError operators o…
Browse files Browse the repository at this point in the history
…n empty sources (#4681)
  • Loading branch information
vanniktech authored and akarnokd committed Oct 7, 2016
1 parent 27e8dad commit e654e9b
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public void onError(Throwable t) {

@Override
public void onComplete() {
if (index <= count && !done) {
if (!done) {
done = true;
actual.onComplete();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public void onError(Throwable t) {

@Override
public void onComplete() {
if (index <= count && !done) {
if (!done) {
done = true;

T v = defaultValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit e654e9b

Please sign in to comment.