Skip to content

2.x: Fix Flowable.elementAt on empty sources. Plus sync tests #4707

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void onError(Throwable t) {

@Override
public void onComplete() {
if (index <= count && !done) {
if (!done) {
done = true;
T v = defaultValue;
if (v == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@

import static org.junit.Assert.*;

import io.reactivex.*;
import io.reactivex.exceptions.TestException;
import io.reactivex.functions.Function;
import io.reactivex.internal.subscriptions.BooleanSubscription;
import io.reactivex.plugins.RxJavaPlugins;
import java.util.List;
import java.util.NoSuchElementException;
import org.junit.Test;

import io.reactivex.Flowable;
import org.reactivestreams.*;

public class FlowableElementAtTest {

Expand Down Expand Up @@ -175,4 +180,59 @@ public void elementAtOrErrorIndex1OnEmptySource() {
.test()
.assertFailure(NoSuchElementException.class);
}


@Test
public void doubleOnSubscribe() {
TestHelper.checkDoubleOnSubscribeFlowable(new Function<Flowable<Object>, Publisher<Object>>() {
@Override
public Publisher<Object> apply(Flowable<Object> o) throws Exception {
return o.elementAt(0).toFlowable();
}
});
}

@Test
public void elementAtIndex1WithDefaultOnEmptySourceObservable() {
Flowable.empty()
.elementAt(1, 10)
.toFlowable()
.test()
.assertResult(10);
}

@Test
public void errorFlowable() {
Flowable.error(new TestException())
.elementAt(1, 10)
.toFlowable()
.test()
.assertFailure(TestException.class);
}

@Test
public void badSource() {
List<Throwable> errors = TestHelper.trackPluginErrors();
try {
new Flowable<Integer>() {
@Override
protected void subscribeActual(Subscriber<? super Integer> subscriber) {
subscriber.onSubscribe(new BooleanSubscription());

subscriber.onNext(1);
subscriber.onNext(2);
subscriber.onError(new TestException());
subscriber.onComplete();
}
}
.elementAt(0)
.toFlowable()
.test()
.assertResult(1);

TestHelper.assertError(errors, 0, TestException.class);
} finally {
RxJavaPlugins.reset();
}
}
}