Skip to content
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

1.x: Avoid to call next when Iterator is drained #3528

Merged
merged 1 commit into from
Nov 16, 2015
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
13 changes: 10 additions & 3 deletions src/main/java/rx/internal/operators/OperatorZipIterable.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,30 @@ public Subscriber<? super T1> call(final Subscriber<? super R> subscriber) {
return Subscribers.empty();
}
return new Subscriber<T1>(subscriber) {
boolean once;
boolean done;
@Override
public void onCompleted() {
if (once) {
if (done) {
return;
}
once = true;
done = true;
subscriber.onCompleted();
}

@Override
public void onError(Throwable e) {
if (done) {
return;
}
done = true;
subscriber.onError(e);
}

@Override
public void onNext(T1 t) {
if (done) {
return;
}
try {
subscriber.onNext(zipFunction.call(t, iterator.next()));
if (!iterator.hasNext()) {
Expand Down
21 changes: 21 additions & 0 deletions src/test/java/rx/internal/operators/OperatorZipIterableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import java.util.Arrays;
import java.util.Iterator;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import static org.junit.Assert.assertEquals;

Expand All @@ -37,6 +38,8 @@
import rx.functions.Func1;
import rx.functions.Func2;
import rx.functions.Func3;
import rx.observers.TestSubscriber;
import rx.schedulers.TestScheduler;
import rx.subjects.PublishSubject;

public class OperatorZipIterableTest {
Expand Down Expand Up @@ -378,4 +381,22 @@ public String call(Integer t1) {

assertEquals(2, squareStr.counter.get());
}

@Test
public void testZipIterableWithDelay() {
TestScheduler scheduler = new TestScheduler();
Observable<Integer> o = Observable.just(1, 2).zipWith(Arrays.asList(1), new Func2<Integer, Integer, Integer>() {
@Override
public Integer call(Integer v1, Integer v2) {
return v1;
}
}).delay(500, TimeUnit.MILLISECONDS, scheduler);

TestSubscriber<Integer> subscriber = new TestSubscriber<Integer>();
o.subscribe(subscriber);
scheduler.advanceTimeBy(1000, TimeUnit.MILLISECONDS);
subscriber.assertValue(1);
subscriber.assertNoErrors();
subscriber.assertCompleted();
}
}