Skip to content

Commit 13dc137

Browse files
Merge pull request #1254 from akarnokd/ZipWithIterableFix
Merge pull request #1254
2 parents 84b7030 + 803efa4 commit 13dc137

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

rxjava-core/src/main/java/rx/operators/OperatorZipIterable.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,14 @@ public Subscriber<? super T1> call(final Subscriber<? super R> subscriber) {
4343
} catch (Throwable e) {
4444
subscriber.onError(e);
4545
}
46-
return new Subscriber<T1>() {
47-
46+
return new Subscriber<T1>(subscriber) {
47+
boolean once;
4848
@Override
4949
public void onCompleted() {
50+
if (once) {
51+
return;
52+
}
53+
once = true;
5054
subscriber.onCompleted();
5155
}
5256

rxjava-core/src/test/java/rx/operators/OperatorZipIterableTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
import java.util.Arrays;
2525
import java.util.Iterator;
26+
import java.util.concurrent.atomic.AtomicInteger;
27+
import static org.junit.Assert.assertEquals;
2628

2729
import org.junit.Before;
2830
import org.junit.Test;
@@ -31,6 +33,8 @@
3133
import rx.Observable;
3234
import rx.Observer;
3335
import rx.exceptions.TestException;
36+
import rx.functions.Action1;
37+
import rx.functions.Func1;
3438
import rx.functions.Func2;
3539
import rx.functions.Func3;
3640
import rx.subjects.PublishSubject;
@@ -346,4 +350,32 @@ public void remove() {
346350
verify(o, never()).onCompleted();
347351

348352
}
353+
354+
Action1<String> printer = new Action1<String>() {
355+
@Override
356+
public void call(String t1) {
357+
System.out.println(t1);
358+
}
359+
};
360+
361+
static final class SquareStr implements Func1<Integer, String> {
362+
final AtomicInteger counter = new AtomicInteger();
363+
@Override
364+
public String call(Integer t1) {
365+
counter.incrementAndGet();
366+
System.out.println("Omg I'm calculating so hard: " + t1 + "*" + t1 + "=" + (t1*t1));
367+
return " " + (t1*t1);
368+
}
369+
}
370+
371+
@Test public void testTake2() {
372+
Observable<Integer> o = Observable.from(1, 2, 3, 4, 5);
373+
Iterable<String> it = Arrays.asList("a", "b", "c", "d", "e");
374+
375+
SquareStr squareStr = new SquareStr();
376+
377+
o.map(squareStr).zip(it, concat2Strings).take(2).subscribe(printer);
378+
379+
assertEquals(2, squareStr.counter.get());
380+
}
349381
}

0 commit comments

Comments
 (0)