Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableZipTest.java

raw
1 /*
2 * Copyright (c) 2016-present, RxJava Contributors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 * compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11 * the License for the specific language governing permissions and limitations under the License.
12 */
13
14 package io.reactivex.rxjava3.internal.operators.flowable;
15
16 import static org.junit.Assert.*;
17 import static org.mockito.ArgumentMatchers.*;
18 import static org.mockito.Mockito.*;
19
20 import java.lang.reflect.*;
21 import java.util.*;
22 import java.util.concurrent.*;
23 import java.util.concurrent.atomic.AtomicInteger;
24
25 import org.junit.*;
26 import org.mockito.InOrder;
27 import org.reactivestreams.*;
28
29 import io.reactivex.rxjava3.core.*;
30 import io.reactivex.rxjava3.exceptions.*;
31 import io.reactivex.rxjava3.functions.*;
32 import io.reactivex.rxjava3.internal.functions.Functions;
33 import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription;
34 import io.reactivex.rxjava3.operators.QueueSubscription;
35 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
36 import io.reactivex.rxjava3.processors.PublishProcessor;
37 import io.reactivex.rxjava3.schedulers.Schedulers;
38 import io.reactivex.rxjava3.subscribers.*;
39 import io.reactivex.rxjava3.testsupport.*;
40
41 public class FlowableZipTest extends RxJavaTest {
42 BiFunction<String, String, String> concat2Strings;
43 PublishProcessor<String> s1;
44 PublishProcessor<String> s2;
45 Flowable<String> zipped;
46
47 Subscriber<String> subscriber;
48 InOrder inOrder;
49
50 @Before
51 public void setUp() {
52 concat2Strings = new BiFunction<String, String, String>() {
53 @Override
54 public String apply(String t1, String t2) {
55 return t1 + "-" + t2;
56 }
57 };
58
59 s1 = PublishProcessor.create();
60 s2 = PublishProcessor.create();
61 zipped = Flowable.zip(s1, s2, concat2Strings);
62
63 subscriber = TestHelper.mockSubscriber();
64 inOrder = inOrder(subscriber);
65
66 zipped.subscribe(subscriber);
67 }
68
69 @SuppressWarnings("unchecked")
70 @Test
71 public void collectionSizeDifferentThanFunction() {
72 Function<Object[], String> zipr = Functions.toFunction(getConcatStringIntegerIntArrayZipr());
73 //Function3<String, Integer, int[], String>
74
75 /* define a Subscriber to receive aggregated events */
76 Subscriber<String> subscriber = TestHelper.mockSubscriber();
77
78 @SuppressWarnings("rawtypes")
79 Collection ws = java.util.Collections.singleton(Flowable.just("one", "two"));
80 Flowable<String> w = Flowable.zip(ws, zipr);
81 w.subscribe(subscriber);
82
83 verify(subscriber, times(1)).onError(any(Throwable.class));
84 verify(subscriber, never()).onComplete();
85 verify(subscriber, never()).onNext(any(String.class));
86 }
87
88 @Test
89 public void startpingDifferentLengthFlowableSequences1() {
90 Subscriber<String> w = TestHelper.mockSubscriber();
91
92 TestFlowable w1 = new TestFlowable();
93 TestFlowable w2 = new TestFlowable();
94 TestFlowable w3 = new TestFlowable();
95
96 Flowable<String> zipW = Flowable.zip(
97 Flowable.unsafeCreate(w1), Flowable.unsafeCreate(w2),
98 Flowable.unsafeCreate(w3), getConcat3StringsZipr());
99 zipW.subscribe(w);
100
101 /* simulate sending data */
102 // once for w1
103 w1.subscriber.onNext("1a");
104 w1.subscriber.onComplete();
105 // twice for w2
106 w2.subscriber.onNext("2a");
107 w2.subscriber.onNext("2b");
108 w2.subscriber.onComplete();
109 // 4 times for w3
110 w3.subscriber.onNext("3a");
111 w3.subscriber.onNext("3b");
112 w3.subscriber.onNext("3c");
113 w3.subscriber.onNext("3d");
114 w3.subscriber.onComplete();
115
116 /* we should have been called 1 time on the Subscriber */
117 InOrder io = inOrder(w);
118 io.verify(w).onNext("1a2a3a");
119
120 io.verify(w, times(1)).onComplete();
121 }
122
123 @Test
124 public void startpingDifferentLengthFlowableSequences2() {
125 Subscriber<String> w = TestHelper.mockSubscriber();
126
127 TestFlowable w1 = new TestFlowable();
128 TestFlowable w2 = new TestFlowable();
129 TestFlowable w3 = new TestFlowable();
130
131 Flowable<String> zipW = Flowable.zip(Flowable.unsafeCreate(w1), Flowable.unsafeCreate(w2), Flowable.unsafeCreate(w3), getConcat3StringsZipr());
132 zipW.subscribe(w);
133
134 /* simulate sending data */
135 // 4 times for w1
136 w1.subscriber.onNext("1a");
137 w1.subscriber.onNext("1b");
138 w1.subscriber.onNext("1c");
139 w1.subscriber.onNext("1d");
140 w1.subscriber.onComplete();
141 // twice for w2
142 w2.subscriber.onNext("2a");
143 w2.subscriber.onNext("2b");
144 w2.subscriber.onComplete();
145 // 1 times for w3
146 w3.subscriber.onNext("3a");
147 w3.subscriber.onComplete();
148
149 /* we should have been called 1 time on the Subscriber */
150 InOrder io = inOrder(w);
151 io.verify(w).onNext("1a2a3a");
152
153 io.verify(w, times(1)).onComplete();
154
155 }
156
157 BiFunction<Object, Object, String> zipr2 = new BiFunction<Object, Object, String>() {
158
159 @Override
160 public String apply(Object t1, Object t2) {
161 return "" + t1 + t2;
162 }
163
164 };
165 Function3<Object, Object, Object, String> zipr3 = new Function3<Object, Object, Object, String>() {
166
167 @Override
168 public String apply(Object t1, Object t2, Object t3) {
169 return "" + t1 + t2 + t3;
170 }
171
172 };
173
174 /**
175 * Testing internal private logic due to the complexity so I want to use TDD to test as a I build it rather than relying purely on the overall functionality expected by the public methods.
176 */
177 @Test
178 public void aggregatorSimple() {
179 PublishProcessor<String> r1 = PublishProcessor.create();
180 PublishProcessor<String> r2 = PublishProcessor.create();
181 /* define a Subscriber to receive aggregated events */
182 Subscriber<String> subscriber = TestHelper.mockSubscriber();
183
184 Flowable.zip(r1, r2, zipr2).subscribe(subscriber);
185
186 /* simulate the Flowables pushing data into the aggregator */
187 r1.onNext("hello");
188 r2.onNext("world");
189
190 InOrder inOrder = inOrder(subscriber);
191
192 verify(subscriber, never()).onError(any(Throwable.class));
193 verify(subscriber, never()).onComplete();
194 inOrder.verify(subscriber, times(1)).onNext("helloworld");
195
196 r1.onNext("hello ");
197 r2.onNext("again");
198
199 verify(subscriber, never()).onError(any(Throwable.class));
200 verify(subscriber, never()).onComplete();
201 inOrder.verify(subscriber, times(1)).onNext("hello again");
202
203 r1.onComplete();
204 r2.onComplete();
205
206 inOrder.verify(subscriber, never()).onNext(anyString());
207 verify(subscriber, times(1)).onComplete();
208 }
209
210 @Test
211 public void aggregatorDifferentSizedResultsWithOnComplete() {
212 /* create the aggregator which will execute the zip function when all Flowables provide values */
213 /* define a Subscriber to receive aggregated events */
214 PublishProcessor<String> r1 = PublishProcessor.create();
215 PublishProcessor<String> r2 = PublishProcessor.create();
216 /* define a Subscriber to receive aggregated events */
217 Subscriber<String> subscriber = TestHelper.mockSubscriber();
218 Flowable.zip(r1, r2, zipr2).subscribe(subscriber);
219
220 /* simulate the Flowables pushing data into the aggregator */
221 r1.onNext("hello");
222 r2.onNext("world");
223 r2.onComplete();
224
225 InOrder inOrder = inOrder(subscriber);
226
227 inOrder.verify(subscriber, never()).onError(any(Throwable.class));
228 inOrder.verify(subscriber, times(1)).onNext("helloworld");
229 inOrder.verify(subscriber, times(1)).onComplete();
230
231 r1.onNext("hi");
232 r1.onComplete();
233
234 inOrder.verify(subscriber, never()).onError(any(Throwable.class));
235 inOrder.verify(subscriber, never()).onComplete();
236 inOrder.verify(subscriber, never()).onNext(anyString());
237 }
238
239 @Test
240 public void aggregateMultipleTypes() {
241 PublishProcessor<String> r1 = PublishProcessor.create();
242 PublishProcessor<Integer> r2 = PublishProcessor.create();
243 /* define a Subscriber to receive aggregated events */
244 Subscriber<String> subscriber = TestHelper.mockSubscriber();
245
246 Flowable.zip(r1, r2, zipr2).subscribe(subscriber);
247
248 /* simulate the Flowables pushing data into the aggregator */
249 r1.onNext("hello");
250 r2.onNext(1);
251 r2.onComplete();
252
253 InOrder inOrder = inOrder(subscriber);
254
255 inOrder.verify(subscriber, never()).onError(any(Throwable.class));
256 inOrder.verify(subscriber, times(1)).onNext("hello1");
257 inOrder.verify(subscriber, times(1)).onComplete();
258
259 r1.onNext("hi");
260 r1.onComplete();
261
262 inOrder.verify(subscriber, never()).onError(any(Throwable.class));
263 inOrder.verify(subscriber, never()).onComplete();
264 inOrder.verify(subscriber, never()).onNext(anyString());
265 }
266
267 @Test
268 public void aggregate3Types() {
269 PublishProcessor<String> r1 = PublishProcessor.create();
270 PublishProcessor<Integer> r2 = PublishProcessor.create();
271 PublishProcessor<List<Integer>> r3 = PublishProcessor.create();
272 /* define a Subscriber to receive aggregated events */
273 Subscriber<String> subscriber = TestHelper.mockSubscriber();
274
275 Flowable.zip(r1, r2, r3, zipr3).subscribe(subscriber);
276
277 /* simulate the Flowables pushing data into the aggregator */
278 r1.onNext("hello");
279 r2.onNext(2);
280 r3.onNext(Arrays.asList(5, 6, 7));
281
282 verify(subscriber, never()).onError(any(Throwable.class));
283 verify(subscriber, never()).onComplete();
284 verify(subscriber, times(1)).onNext("hello2[5, 6, 7]");
285 }
286
287 @Test
288 public void aggregatorsWithDifferentSizesAndTiming() {
289 PublishProcessor<String> r1 = PublishProcessor.create();
290 PublishProcessor<String> r2 = PublishProcessor.create();
291 /* define a Subscriber to receive aggregated events */
292 Subscriber<String> subscriber = TestHelper.mockSubscriber();
293
294 Flowable.zip(r1, r2, zipr2).subscribe(subscriber);
295
296 /* simulate the Flowables pushing data into the aggregator */
297 r1.onNext("one");
298 r1.onNext("two");
299 r1.onNext("three");
300 r2.onNext("A");
301
302 verify(subscriber, never()).onError(any(Throwable.class));
303 verify(subscriber, never()).onComplete();
304 verify(subscriber, times(1)).onNext("oneA");
305
306 r1.onNext("four");
307 r1.onComplete();
308 r2.onNext("B");
309 verify(subscriber, times(1)).onNext("twoB");
310 r2.onNext("C");
311 verify(subscriber, times(1)).onNext("threeC");
312 r2.onNext("D");
313 verify(subscriber, times(1)).onNext("fourD");
314 r2.onNext("E");
315 verify(subscriber, never()).onNext("E");
316 r2.onComplete();
317
318 verify(subscriber, never()).onError(any(Throwable.class));
319 verify(subscriber, times(1)).onComplete();
320 }
321
322 @Test
323 public void aggregatorError() {
324 PublishProcessor<String> r1 = PublishProcessor.create();
325 PublishProcessor<String> r2 = PublishProcessor.create();
326 /* define a Subscriber to receive aggregated events */
327 Subscriber<String> subscriber = TestHelper.mockSubscriber();
328
329 Flowable.zip(r1, r2, zipr2).subscribe(subscriber);
330
331 /* simulate the Flowables pushing data into the aggregator */
332 r1.onNext("hello");
333 r2.onNext("world");
334
335 verify(subscriber, never()).onError(any(Throwable.class));
336 verify(subscriber, never()).onComplete();
337 verify(subscriber, times(1)).onNext("helloworld");
338
339 r1.onError(new RuntimeException(""));
340 r1.onNext("hello");
341 r2.onNext("again");
342
343 verify(subscriber, times(1)).onError(any(Throwable.class));
344 verify(subscriber, never()).onComplete();
345 // we don't want to be called again after an error
346 verify(subscriber, times(0)).onNext("helloagain");
347 }
348
349 @Test
350 public void aggregatorUnsubscribe() {
351 PublishProcessor<String> r1 = PublishProcessor.create();
352 PublishProcessor<String> r2 = PublishProcessor.create();
353 /* define a Subscriber to receive aggregated events */
354 Subscriber<String> subscriber = TestHelper.mockSubscriber();
355 TestSubscriber<String> ts = new TestSubscriber<>(subscriber);
356
357 Flowable.zip(r1, r2, zipr2).subscribe(ts);
358
359 /* simulate the Flowables pushing data into the aggregator */
360 r1.onNext("hello");
361 r2.onNext("world");
362
363 verify(subscriber, never()).onError(any(Throwable.class));
364 verify(subscriber, never()).onComplete();
365 verify(subscriber, times(1)).onNext("helloworld");
366
367 ts.cancel();
368 r1.onNext("hello");
369 r2.onNext("again");
370
371 verify(subscriber, times(0)).onError(any(Throwable.class));
372 verify(subscriber, never()).onComplete();
373 // we don't want to be called again after an error
374 verify(subscriber, times(0)).onNext("helloagain");
375 }
376
377 @Test
378 public void aggregatorEarlyCompletion() {
379 PublishProcessor<String> r1 = PublishProcessor.create();
380 PublishProcessor<String> r2 = PublishProcessor.create();
381 /* define a Subscriber to receive aggregated events */
382 Subscriber<String> subscriber = TestHelper.mockSubscriber();
383
384 Flowable.zip(r1, r2, zipr2).subscribe(subscriber);
385
386 /* simulate the Flowables pushing data into the aggregator */
387 r1.onNext("one");
388 r1.onNext("two");
389 r1.onComplete();
390 r2.onNext("A");
391
392 InOrder inOrder = inOrder(subscriber);
393
394 inOrder.verify(subscriber, never()).onError(any(Throwable.class));
395 inOrder.verify(subscriber, never()).onComplete();
396 inOrder.verify(subscriber, times(1)).onNext("oneA");
397
398 r2.onComplete();
399
400 inOrder.verify(subscriber, never()).onError(any(Throwable.class));
401 inOrder.verify(subscriber, times(1)).onComplete();
402 inOrder.verify(subscriber, never()).onNext(anyString());
403 }
404
405 @Test
406 public void start2Types() {
407 BiFunction<String, Integer, String> zipr = getConcatStringIntegerZipr();
408
409 /* define a Subscriber to receive aggregated events */
410 Subscriber<String> subscriber = TestHelper.mockSubscriber();
411
412 Flowable<String> w = Flowable.zip(Flowable.just("one", "two"), Flowable.just(2, 3, 4), zipr);
413 w.subscribe(subscriber);
414
415 verify(subscriber, never()).onError(any(Throwable.class));
416 verify(subscriber, times(1)).onComplete();
417 verify(subscriber, times(1)).onNext("one2");
418 verify(subscriber, times(1)).onNext("two3");
419 verify(subscriber, never()).onNext("4");
420 }
421
422 @Test
423 public void start3Types() {
424 Function3<String, Integer, int[], String> zipr = getConcatStringIntegerIntArrayZipr();
425
426 /* define a Subscriber to receive aggregated events */
427 Subscriber<String> subscriber = TestHelper.mockSubscriber();
428
429 Flowable<String> w = Flowable.zip(Flowable.just("one", "two"), Flowable.just(2), Flowable.just(new int[] { 4, 5, 6 }), zipr);
430 w.subscribe(subscriber);
431
432 verify(subscriber, never()).onError(any(Throwable.class));
433 verify(subscriber, times(1)).onComplete();
434 verify(subscriber, times(1)).onNext("one2[4, 5, 6]");
435 verify(subscriber, never()).onNext("two");
436 }
437
438 @Test
439 public void onNextExceptionInvokesOnError() {
440 BiFunction<Integer, Integer, Integer> zipr = getDivideZipr();
441
442 Subscriber<Integer> subscriber = TestHelper.mockSubscriber();
443
444 Flowable<Integer> w = Flowable.zip(Flowable.just(10, 20, 30), Flowable.just(0, 1, 2), zipr);
445 w.subscribe(subscriber);
446
447 verify(subscriber, times(1)).onError(any(Throwable.class));
448 }
449
450 @Test
451 public void onFirstCompletion() {
452 PublishProcessor<String> oA = PublishProcessor.create();
453 PublishProcessor<String> oB = PublishProcessor.create();
454
455 Subscriber<String> obs = TestHelper.mockSubscriber();
456
457 Flowable<String> f = Flowable.zip(oA, oB, getConcat2Strings());
458 f.subscribe(obs);
459
460 InOrder io = inOrder(obs);
461
462 oA.onNext("a1");
463 io.verify(obs, never()).onNext(anyString());
464 oB.onNext("b1");
465 io.verify(obs, times(1)).onNext("a1-b1");
466 oB.onNext("b2");
467 io.verify(obs, never()).onNext(anyString());
468 oA.onNext("a2");
469 io.verify(obs, times(1)).onNext("a2-b2");
470
471 oA.onNext("a3");
472 oA.onNext("a4");
473 oA.onNext("a5");
474 oA.onComplete();
475
476 // SHOULD ONCOMPLETE BE EMITTED HERE INSTEAD OF WAITING
477 // FOR B3, B4, B5 TO BE EMITTED?
478
479 oB.onNext("b3");
480 oB.onNext("b4");
481 oB.onNext("b5");
482
483 io.verify(obs, times(1)).onNext("a3-b3");
484 io.verify(obs, times(1)).onNext("a4-b4");
485 io.verify(obs, times(1)).onNext("a5-b5");
486
487 // WE RECEIVE THE ONCOMPLETE HERE
488 io.verify(obs, times(1)).onComplete();
489
490 oB.onNext("b6");
491 oB.onNext("b7");
492 oB.onNext("b8");
493 oB.onNext("b9");
494 // never completes (infinite stream for example)
495
496 // we should receive nothing else despite oB continuing after oA completed
497 io.verifyNoMoreInteractions();
498 }
499
500 @Test
501 public void onErrorTermination() {
502 PublishProcessor<String> oA = PublishProcessor.create();
503 PublishProcessor<String> oB = PublishProcessor.create();
504
505 Subscriber<String> obs = TestHelper.mockSubscriber();
506
507 Flowable<String> f = Flowable.zip(oA, oB, getConcat2Strings());
508 f.subscribe(obs);
509
510 InOrder io = inOrder(obs);
511
512 oA.onNext("a1");
513 io.verify(obs, never()).onNext(anyString());
514 oB.onNext("b1");
515 io.verify(obs, times(1)).onNext("a1-b1");
516 oB.onNext("b2");
517 io.verify(obs, never()).onNext(anyString());
518 oA.onNext("a2");
519 io.verify(obs, times(1)).onNext("a2-b2");
520
521 oA.onNext("a3");
522 oA.onNext("a4");
523 oA.onNext("a5");
524 oA.onError(new RuntimeException("forced failure"));
525
526 // it should emit failure immediately
527 io.verify(obs, times(1)).onError(any(RuntimeException.class));
528
529 oB.onNext("b3");
530 oB.onNext("b4");
531 oB.onNext("b5");
532 oB.onNext("b6");
533 oB.onNext("b7");
534 oB.onNext("b8");
535 oB.onNext("b9");
536 // never completes (infinite stream for example)
537
538 // we should receive nothing else despite oB continuing after oA completed
539 io.verifyNoMoreInteractions();
540 }
541
542 private BiFunction<String, String, String> getConcat2Strings() {
543 return new BiFunction<String, String, String>() {
544
545 @Override
546 public String apply(String t1, String t2) {
547 return t1 + "-" + t2;
548 }
549 };
550 }
551
552 private BiFunction<Integer, Integer, Integer> getDivideZipr() {
553 BiFunction<Integer, Integer, Integer> zipr = new BiFunction<Integer, Integer, Integer>() {
554
555 @Override
556 public Integer apply(Integer i1, Integer i2) {
557 return i1 / i2;
558 }
559
560 };
561 return zipr;
562 }
563
564 private Function3<String, String, String, String> getConcat3StringsZipr() {
565 Function3<String, String, String, String> zipr = new Function3<String, String, String, String>() {
566
567 @Override
568 public String apply(String a1, String a2, String a3) {
569 if (a1 == null) {
570 a1 = "";
571 }
572 if (a2 == null) {
573 a2 = "";
574 }
575 if (a3 == null) {
576 a3 = "";
577 }
578 return a1 + a2 + a3;
579 }
580
581 };
582 return zipr;
583 }
584
585 private BiFunction<String, Integer, String> getConcatStringIntegerZipr() {
586 BiFunction<String, Integer, String> zipr = new BiFunction<String, Integer, String>() {
587
588 @Override
589 public String apply(String s, Integer i) {
590 return getStringValue(s) + getStringValue(i);
591 }
592
593 };
594 return zipr;
595 }
596
597 private Function3<String, Integer, int[], String> getConcatStringIntegerIntArrayZipr() {
598 Function3<String, Integer, int[], String> zipr = new Function3<String, Integer, int[], String>() {
599
600 @Override
601 public String apply(String s, Integer i, int[] iArray) {
602 return getStringValue(s) + getStringValue(i) + getStringValue(iArray);
603 }
604
605 };
606 return zipr;
607 }
608
609 private static String getStringValue(Object o) {
610 if (o == null) {
611 return "";
612 } else {
613 if (o instanceof int[]) {
614 return Arrays.toString((int[]) o);
615 } else {
616 return String.valueOf(o);
617 }
618 }
619 }
620
621 private static class TestFlowable implements Publisher<String> {
622
623 Subscriber<? super String> subscriber;
624
625 @Override
626 public void subscribe(Subscriber<? super String> subscriber) {
627 // just store the variable where it can be accessed so we can manually trigger it
628 this.subscriber = subscriber;
629 subscriber.onSubscribe(new BooleanSubscription());
630 }
631
632 }
633
634 @Test
635 public void firstCompletesThenSecondInfinite() {
636 s1.onNext("a");
637 s1.onNext("b");
638 s1.onComplete();
639 s2.onNext("1");
640 inOrder.verify(subscriber, times(1)).onNext("a-1");
641 s2.onNext("2");
642 inOrder.verify(subscriber, times(1)).onNext("b-2");
643 inOrder.verify(subscriber, times(1)).onComplete();
644 inOrder.verifyNoMoreInteractions();
645 }
646
647 @Test
648 public void secondInfiniteThenFirstCompletes() {
649 s2.onNext("1");
650 s2.onNext("2");
651 s1.onNext("a");
652 inOrder.verify(subscriber, times(1)).onNext("a-1");
653 s1.onNext("b");
654 inOrder.verify(subscriber, times(1)).onNext("b-2");
655 s1.onComplete();
656 inOrder.verify(subscriber, times(1)).onComplete();
657 inOrder.verifyNoMoreInteractions();
658 }
659
660 @Test
661 public void secondCompletesThenFirstInfinite() {
662 s2.onNext("1");
663 s2.onNext("2");
664 s2.onComplete();
665 s1.onNext("a");
666 inOrder.verify(subscriber, times(1)).onNext("a-1");
667 s1.onNext("b");
668 inOrder.verify(subscriber, times(1)).onNext("b-2");
669 inOrder.verify(subscriber, times(1)).onComplete();
670 inOrder.verifyNoMoreInteractions();
671 }
672
673 @Test
674 public void firstInfiniteThenSecondCompletes() {
675 s1.onNext("a");
676 s1.onNext("b");
677 s2.onNext("1");
678 inOrder.verify(subscriber, times(1)).onNext("a-1");
679 s2.onNext("2");
680 inOrder.verify(subscriber, times(1)).onNext("b-2");
681 s2.onComplete();
682 inOrder.verify(subscriber, times(1)).onComplete();
683 inOrder.verifyNoMoreInteractions();
684 }
685
686 @Test
687 public void firstFails() {
688 s2.onNext("a");
689 s1.onError(new RuntimeException("Forced failure"));
690
691 inOrder.verify(subscriber, times(1)).onError(any(RuntimeException.class));
692
693 s2.onNext("b");
694 s1.onNext("1");
695 s1.onNext("2");
696
697 inOrder.verify(subscriber, never()).onComplete();
698 inOrder.verify(subscriber, never()).onNext(any(String.class));
699 inOrder.verifyNoMoreInteractions();
700 }
701
702 @Test
703 public void secondFails() {
704 s1.onNext("a");
705 s1.onNext("b");
706 s2.onError(new RuntimeException("Forced failure"));
707
708 inOrder.verify(subscriber, times(1)).onError(any(RuntimeException.class));
709
710 s2.onNext("1");
711 s2.onNext("2");
712
713 inOrder.verify(subscriber, never()).onComplete();
714 inOrder.verify(subscriber, never()).onNext(any(String.class));
715 inOrder.verifyNoMoreInteractions();
716 }
717
718 @Test
719 public void startWithOnCompletedTwice() {
720 // issue: https://groups.google.com/forum/#!topic/rxjava/79cWTv3TFp0
721 // The problem is the original "zip" implementation does not wrap
722 // an internal subscriber with a SafeSubscriber. However, in the "zip",
723 // it may calls "onComplete" twice. That breaks the Rx contract.
724
725 // This test tries to emulate this case.
726 // As "TestHelper.mockSubscriber()" will create an instance in the package "rx",
727 // we need to wrap "TestHelper.mockSubscriber()" with an subscriber instance
728 // which is in the package "rx.operators".
729 final Subscriber<Integer> subscriber = TestHelper.mockSubscriber();
730
731 Flowable.zip(Flowable.just(1),
732 Flowable.just(1), new BiFunction<Integer, Integer, Integer>() {
733 @Override
734 public Integer apply(Integer a, Integer b) {
735 return a + b;
736 }
737 }).subscribe(new DefaultSubscriber<Integer>() {
738
739 @Override
740 public void onComplete() {
741 subscriber.onComplete();
742 }
743
744 @Override
745 public void onError(Throwable e) {
746 subscriber.onError(e);
747 }
748
749 @Override
750 public void onNext(Integer args) {
751 subscriber.onNext(args);
752 }
753
754 });
755
756 InOrder inOrder = inOrder(subscriber);
757 inOrder.verify(subscriber, times(1)).onNext(2);
758 inOrder.verify(subscriber, times(1)).onComplete();
759 inOrder.verifyNoMoreInteractions();
760 }
761
762 @Test
763 public void start() {
764 Flowable<String> os = OBSERVABLE_OF_5_INTEGERS
765 .zipWith(OBSERVABLE_OF_5_INTEGERS, new BiFunction<Integer, Integer, String>() {
766
767 @Override
768 public String apply(Integer a, Integer b) {
769 return a + "-" + b;
770 }
771 });
772
773 final ArrayList<String> list = new ArrayList<>();
774 os.subscribe(new Consumer<String>() {
775
776 @Override
777 public void accept(String s) {
778 System.out.println(s);
779 list.add(s);
780 }
781 });
782
783 assertEquals(5, list.size());
784 assertEquals("1-1", list.get(0));
785 assertEquals("2-2", list.get(1));
786 assertEquals("5-5", list.get(4));
787 }
788
789 @Test
790 public void startAsync() throws InterruptedException {
791 Flowable<String> os = ASYNC_OBSERVABLE_OF_INFINITE_INTEGERS(new CountDownLatch(1)).onBackpressureBuffer()
792 .zipWith(ASYNC_OBSERVABLE_OF_INFINITE_INTEGERS(new CountDownLatch(1)).onBackpressureBuffer(), new BiFunction<Integer, Integer, String>() {
793
794 @Override
795 public String apply(Integer a, Integer b) {
796 return a + "-" + b;
797 }
798 }).take(5);
799
800 TestSubscriber<String> ts = new TestSubscriber<>();
801 os.subscribe(ts);
802
803 ts.awaitDone(5, TimeUnit.SECONDS);
804 ts.assertNoErrors();
805
806 assertEquals(5, ts.values().size());
807 assertEquals("1-1", ts.values().get(0));
808 assertEquals("2-2", ts.values().get(1));
809 assertEquals("5-5", ts.values().get(4));
810 }
811
812 @Test
813 public void startInfiniteAndFinite() throws InterruptedException {
814 final CountDownLatch latch = new CountDownLatch(1);
815 final CountDownLatch infiniteFlowable = new CountDownLatch(1);
816 Flowable<String> os = OBSERVABLE_OF_5_INTEGERS
817 .zipWith(ASYNC_OBSERVABLE_OF_INFINITE_INTEGERS(infiniteFlowable), new BiFunction<Integer, Integer, String>() {
818
819 @Override
820 public String apply(Integer a, Integer b) {
821 return a + "-" + b;
822 }
823 });
824
825 final ArrayList<String> list = new ArrayList<>();
826 os.subscribe(new DefaultSubscriber<String>() {
827
828 @Override
829 public void onComplete() {
830 latch.countDown();
831 }
832
833 @Override
834 public void onError(Throwable e) {
835 e.printStackTrace();
836 latch.countDown();
837 }
838
839 @Override
840 public void onNext(String s) {
841 System.out.println(s);
842 list.add(s);
843 }
844 });
845
846 latch.await(1000, TimeUnit.MILLISECONDS);
847 if (!infiniteFlowable.await(2000, TimeUnit.MILLISECONDS)) {
848 throw new RuntimeException("didn't unsubscribe");
849 }
850
851 assertEquals(5, list.size());
852 assertEquals("1-1", list.get(0));
853 assertEquals("2-2", list.get(1));
854 assertEquals("5-5", list.get(4));
855 }
856
857 @SuppressWarnings("rawtypes")
858 static String kind(Notification notification) {
859 if (notification.isOnError()) {
860 return "OnError";
861 }
862 if (notification.isOnNext()) {
863 return "OnNext";
864 }
865 return "OnComplete";
866 }
867
868 @SuppressWarnings("rawtypes")
869 static String value(Notification notification) {
870 if (notification.isOnNext()) {
871 return String.valueOf(notification.getValue());
872 }
873 return "null";
874 }
875
876 @Test
877 public void emitMaterializedNotifications() {
878 Flowable<Notification<Integer>> oi = Flowable.just(1, 2, 3).materialize();
879 Flowable<Notification<String>> os = Flowable.just("a", "b", "c").materialize();
880 Flowable<String> f = Flowable.zip(oi, os, new BiFunction<Notification<Integer>, Notification<String>, String>() {
881
882 @Override
883 public String apply(Notification<Integer> t1, Notification<String> t2) {
884 return kind(t1) + "_" + value(t1) + "-" + kind(t2) + "_" + value(t2);
885 }
886
887 });
888
889 final ArrayList<String> list = new ArrayList<>();
890 f.subscribe(new Consumer<String>() {
891
892 @Override
893 public void accept(String s) {
894 System.out.println(s);
895 list.add(s);
896 }
897 });
898
899 assertEquals(4, list.size());
900 assertEquals("OnNext_1-OnNext_a", list.get(0));
901 assertEquals("OnNext_2-OnNext_b", list.get(1));
902 assertEquals("OnNext_3-OnNext_c", list.get(2));
903 assertEquals("OnComplete_null-OnComplete_null", list.get(3));
904 }
905
906 @Test
907 public void startEmptyFlowables() {
908
909 Flowable<String> f = Flowable.zip(Flowable.<Integer> empty(), Flowable.<String> empty(), new BiFunction<Integer, String, String>() {
910
911 @Override
912 public String apply(Integer t1, String t2) {
913 return t1 + "-" + t2;
914 }
915
916 });
917
918 final ArrayList<String> list = new ArrayList<>();
919 f.subscribe(new Consumer<String>() {
920
921 @Override
922 public void accept(String s) {
923 System.out.println(s);
924 list.add(s);
925 }
926 });
927
928 assertEquals(0, list.size());
929 }
930
931 @Test
932 public void startEmptyList() {
933
934 final Object invoked = new Object();
935 Collection<Flowable<Object>> observables = Collections.emptyList();
936
937 Flowable<Object> f = Flowable.zip(observables, new Function<Object[], Object>() {
938 @Override
939 public Object apply(final Object[] args) {
940 assertEquals("No argument should have been passed", 0, args.length);
941 return invoked;
942 }
943 });
944
945 TestSubscriber<Object> ts = new TestSubscriber<>();
946 f.subscribe(ts);
947 ts.awaitDone(200, TimeUnit.MILLISECONDS);
948 ts.assertNoValues();
949 }
950
951 /**
952 * Expect NoSuchElementException instead of blocking forever as zip should emit onComplete and no onNext
953 * and last() expects at least a single response.
954 */
955 @Test(expected = NoSuchElementException.class)
956 public void startEmptyListBlocking() {
957
958 final Object invoked = new Object();
959 Collection<Flowable<Object>> observables = Collections.emptyList();
960
961 Flowable<Object> f = Flowable.zip(observables, new Function<Object[], Object>() {
962 @Override
963 public Object apply(final Object[] args) {
964 assertEquals("No argument should have been passed", 0, args.length);
965 return invoked;
966 }
967 });
968
969 f.blockingLast();
970 }
971
972 @Test
973 public void backpressureSync() {
974 AtomicInteger generatedA = new AtomicInteger();
975 AtomicInteger generatedB = new AtomicInteger();
976 Flowable<Integer> f1 = createInfiniteFlowable(generatedA);
977 Flowable<Integer> f2 = createInfiniteFlowable(generatedB);
978
979 TestSubscriber<String> ts = new TestSubscriber<>();
980 Flowable.zip(f1, f2, new BiFunction<Integer, Integer, String>() {
981
982 @Override
983 public String apply(Integer t1, Integer t2) {
984 return t1 + "-" + t2;
985 }
986
987 }).take(Flowable.bufferSize() * 2).subscribe(ts);
988
989 ts.awaitDone(5, TimeUnit.SECONDS);
990 ts.assertNoErrors();
991 assertEquals(Flowable.bufferSize() * 2, ts.values().size());
992 assertTrue(generatedA.get() < (Flowable.bufferSize() * 3));
993 assertTrue(generatedB.get() < (Flowable.bufferSize() * 3));
994 }
995
996 @Test
997 public void backpressureAsync() {
998 AtomicInteger generatedA = new AtomicInteger();
999 AtomicInteger generatedB = new AtomicInteger();
1000 Flowable<Integer> f1 = createInfiniteFlowable(generatedA).subscribeOn(Schedulers.computation());
1001 Flowable<Integer> f2 = createInfiniteFlowable(generatedB).subscribeOn(Schedulers.computation());
1002
1003 TestSubscriber<String> ts = new TestSubscriber<>();
1004 Flowable.zip(f1, f2, new BiFunction<Integer, Integer, String>() {
1005
1006 @Override
1007 public String apply(Integer t1, Integer t2) {
1008 return t1 + "-" + t2;
1009 }
1010
1011 }).take(Flowable.bufferSize() * 2).subscribe(ts);
1012
1013 ts.awaitDone(5, TimeUnit.SECONDS);
1014 ts.assertNoErrors();
1015 assertEquals(Flowable.bufferSize() * 2, ts.values().size());
1016 assertTrue(generatedA.get() < (Flowable.bufferSize() * 3));
1017 assertTrue(generatedB.get() < (Flowable.bufferSize() * 3));
1018 }
1019
1020 @Test
1021 public void downstreamBackpressureRequestsWithFiniteSyncFlowables() {
1022 AtomicInteger generatedA = new AtomicInteger();
1023 AtomicInteger generatedB = new AtomicInteger();
1024 Flowable<Integer> f1 = createInfiniteFlowable(generatedA).take(Flowable.bufferSize() * 2);
1025 Flowable<Integer> f2 = createInfiniteFlowable(generatedB).take(Flowable.bufferSize() * 2);
1026
1027 TestSubscriber<String> ts = new TestSubscriber<>();
1028 Flowable.zip(f1, f2, new BiFunction<Integer, Integer, String>() {
1029
1030 @Override
1031 public String apply(Integer t1, Integer t2) {
1032 return t1 + "-" + t2;
1033 }
1034
1035 }).observeOn(Schedulers.computation()).take(Flowable.bufferSize() * 2).subscribe(ts);
1036
1037 ts.awaitDone(5, TimeUnit.SECONDS);
1038 ts.assertNoErrors();
1039 assertEquals(Flowable.bufferSize() * 2, ts.values().size());
1040 System.out.println("Generated => A: " + generatedA.get() + " B: " + generatedB.get());
1041 assertTrue(generatedA.get() < (Flowable.bufferSize() * 3));
1042 assertTrue(generatedB.get() < (Flowable.bufferSize() * 3));
1043 }
1044
1045 @Test
1046 public void downstreamBackpressureRequestsWithInfiniteAsyncFlowables() {
1047 AtomicInteger generatedA = new AtomicInteger();
1048 AtomicInteger generatedB = new AtomicInteger();
1049 Flowable<Integer> f1 = createInfiniteFlowable(generatedA).subscribeOn(Schedulers.computation());
1050 Flowable<Integer> f2 = createInfiniteFlowable(generatedB).subscribeOn(Schedulers.computation());
1051
1052 TestSubscriber<String> ts = new TestSubscriber<>();
1053 Flowable.zip(f1, f2, new BiFunction<Integer, Integer, String>() {
1054
1055 @Override
1056 public String apply(Integer t1, Integer t2) {
1057 return t1 + "-" + t2;
1058 }
1059
1060 }).observeOn(Schedulers.computation()).take(Flowable.bufferSize() * 2).subscribe(ts);
1061
1062 ts.awaitDone(5, TimeUnit.SECONDS);
1063 ts.assertNoErrors();
1064 assertEquals(Flowable.bufferSize() * 2, ts.values().size());
1065 System.out.println("Generated => A: " + generatedA.get() + " B: " + generatedB.get());
1066 assertTrue(generatedA.get() < (Flowable.bufferSize() * 4));
1067 assertTrue(generatedB.get() < (Flowable.bufferSize() * 4));
1068 }
1069
1070 @Test
1071 public void downstreamBackpressureRequestsWithInfiniteSyncFlowables() {
1072 AtomicInteger generatedA = new AtomicInteger();
1073 AtomicInteger generatedB = new AtomicInteger();
1074 Flowable<Integer> f1 = createInfiniteFlowable(generatedA);
1075 Flowable<Integer> f2 = createInfiniteFlowable(generatedB);
1076
1077 TestSubscriber<String> ts = new TestSubscriber<>();
1078 Flowable.zip(f1, f2, new BiFunction<Integer, Integer, String>() {
1079
1080 @Override
1081 public String apply(Integer t1, Integer t2) {
1082 return t1 + "-" + t2;
1083 }
1084
1085 }).observeOn(Schedulers.computation()).take(Flowable.bufferSize() * 2).subscribe(ts);
1086
1087 ts.awaitDone(5, TimeUnit.SECONDS);
1088 ts.assertNoErrors();
1089 assertEquals(Flowable.bufferSize() * 2, ts.values().size());
1090 System.out.println("Generated => A: " + generatedA.get() + " B: " + generatedB.get());
1091 assertTrue(generatedA.get() < (Flowable.bufferSize() * 4));
1092 assertTrue(generatedB.get() < (Flowable.bufferSize() * 4));
1093 }
1094
1095 private Flowable<Integer> createInfiniteFlowable(final AtomicInteger generated) {
1096 Flowable<Integer> flowable = Flowable.fromIterable(new Iterable<Integer>() {
1097 @Override
1098 public Iterator<Integer> iterator() {
1099 return new Iterator<Integer>() {
1100
1101 @Override
1102 public void remove() {
1103 }
1104
1105 @Override
1106 public Integer next() {
1107 return generated.getAndIncrement();
1108 }
1109
1110 @Override
1111 public boolean hasNext() {
1112 return true;
1113 }
1114 };
1115 }
1116 });
1117 return flowable;
1118 }
1119
1120 Flowable<Integer> OBSERVABLE_OF_5_INTEGERS = OBSERVABLE_OF_5_INTEGERS(new AtomicInteger());
1121
1122 Flowable<Integer> OBSERVABLE_OF_5_INTEGERS(final AtomicInteger numEmitted) {
1123 return Flowable.unsafeCreate(new Publisher<Integer>() {
1124
1125 @Override
1126 public void subscribe(final Subscriber<? super Integer> subscriber) {
1127 BooleanSubscription bs = new BooleanSubscription();
1128 subscriber.onSubscribe(bs);
1129 for (int i = 1; i <= 5; i++) {
1130 if (bs.isCancelled()) {
1131 break;
1132 }
1133 numEmitted.incrementAndGet();
1134 subscriber.onNext(i);
1135 Thread.yield();
1136 }
1137 subscriber.onComplete();
1138 }
1139
1140 });
1141 }
1142
1143 Flowable<Integer> ASYNC_OBSERVABLE_OF_INFINITE_INTEGERS(final CountDownLatch latch) {
1144 return Flowable.unsafeCreate(new Publisher<Integer>() {
1145
1146 @Override
1147 public void subscribe(final Subscriber<? super Integer> subscriber) {
1148 final BooleanSubscription bs = new BooleanSubscription();
1149 subscriber.onSubscribe(bs);
1150 Thread t = new Thread(new Runnable() {
1151
1152 @Override
1153 public void run() {
1154 System.out.println("-------> subscribe to infinite sequence");
1155 System.out.println("Starting thread: " + Thread.currentThread());
1156 int i = 1;
1157 while (!bs.isCancelled()) {
1158 subscriber.onNext(i++);
1159 Thread.yield();
1160 }
1161 subscriber.onComplete();
1162 latch.countDown();
1163 System.out.println("Ending thread: " + Thread.currentThread());
1164 }
1165 });
1166 t.start();
1167
1168 }
1169
1170 });
1171 }
1172
1173 @Test
1174 public void issue1812() {
1175 // https://github.com/ReactiveX/RxJava/issues/1812
1176 Flowable<Integer> zip1 = Flowable.zip(Flowable.range(0, 1026), Flowable.range(0, 1026),
1177 new BiFunction<Integer, Integer, Integer>() {
1178
1179 @Override
1180 public Integer apply(Integer i1, Integer i2) {
1181 return i1 + i2;
1182 }
1183 });
1184 Flowable<Integer> zip2 = Flowable.zip(zip1, Flowable.range(0, 1026),
1185 new BiFunction<Integer, Integer, Integer>() {
1186
1187 @Override
1188 public Integer apply(Integer i1, Integer i2) {
1189 return i1 + i2;
1190 }
1191 });
1192 List<Integer> expected = new ArrayList<>();
1193 for (int i = 0; i < 1026; i++) {
1194 expected.add(i * 3);
1195 }
1196 assertEquals(expected, zip2.toList().blockingGet());
1197 }
1198
1199 @Test
1200 public void unboundedDownstreamOverrequesting() {
1201 Flowable<Integer> source = Flowable.range(1, 2).zipWith(Flowable.range(1, 2), new BiFunction<Integer, Integer, Integer>() {
1202 @Override
1203 public Integer apply(Integer t1, Integer t2) {
1204 return t1 + 10 * t2;
1205 }
1206 });
1207
1208 TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>() {
1209 @Override
1210 public void onNext(Integer t) {
1211 super.onNext(t);
1212 request(5);
1213 }
1214 };
1215
1216 source.subscribe(ts);
1217
1218 ts.assertNoErrors();
1219 ts.assertTerminated();
1220 ts.assertValues(11, 22);
1221 }
1222
1223 @Test
1224 public void zipRace() {
1225 long startTime = System.currentTimeMillis();
1226 Flowable<Integer> src = Flowable.just(1).subscribeOn(Schedulers.computation());
1227
1228 // now try and generate a hang by zipping src with itself repeatedly. A
1229 // time limit of 9 seconds ( 1 second less than the test timeout) is
1230 // used so that this test will not timeout on slow machines.
1231 int i = 0;
1232 while (System.currentTimeMillis() - startTime < 9000 && i++ < 100000) {
1233 int value = Flowable.zip(src, src, new BiFunction<Integer, Integer, Integer>() {
1234 @Override
1235 public Integer apply(Integer t1, Integer t2) {
1236 return t1 + t2 * 10;
1237 }
1238 }).blockingSingle(0);
1239
1240 Assert.assertEquals(11, value);
1241 }
1242 }
1243 /**
1244 * Request only a single value and don't wait for another request just
1245 * to emit an onComplete.
1246 */
1247 @Test
1248 public void zipRequest1() {
1249 Flowable<Integer> src = Flowable.just(1).subscribeOn(Schedulers.computation());
1250 TestSubscriber<Integer> ts = new TestSubscriber<>(1L);
1251
1252 Flowable.zip(src, src, new BiFunction<Integer, Integer, Integer>() {
1253 @Override
1254 public Integer apply(Integer t1, Integer t2) {
1255 return t1 + t2 * 10;
1256 }
1257 }).subscribe(ts);
1258
1259 ts.awaitDone(1, TimeUnit.SECONDS);
1260 ts.assertNoErrors();
1261 ts.assertValue(11);
1262 }
1263
1264 @SuppressWarnings({ "unchecked", "rawtypes" })
1265 @Test
1266 public void zipNArguments() throws Exception {
1267 Flowable source = Flowable.just(1);
1268
1269 for (int i = 2; i < 10; i++) {
1270 Class<?>[] types = new Class[i + 1];
1271 Arrays.fill(types, Publisher.class);
1272 types[i] = i == 2 ? BiFunction.class : Class.forName("io.reactivex.rxjava3.functions.Function" + i);
1273
1274 Method m = Flowable.class.getMethod("zip", types);
1275
1276 Object[] params = new Object[i + 1];
1277 Arrays.fill(params, source);
1278 params[i] = ArgsToString.INSTANCE;
1279
1280 StringBuilder b = new StringBuilder();
1281 for (int j = 0; j < i; j++) {
1282 b.append('1');
1283 }
1284
1285 ((Flowable)m.invoke(null, params)).test().assertResult(b.toString());
1286
1287 for (int j = 0; j < params.length; j++) {
1288 Object[] params0 = params.clone();
1289 params0[j] = null;
1290
1291 try {
1292 m.invoke(null, params0);
1293 fail("Should have thrown @ " + m);
1294 } catch (InvocationTargetException ex) {
1295 assertTrue(ex.toString(), ex.getCause() instanceof NullPointerException);
1296
1297 if (j < i) {
1298 assertEquals("source" + (j + 1) + " is null", ex.getCause().getMessage());
1299 } else {
1300 assertEquals("zipper is null", ex.getCause().getMessage());
1301 }
1302 }
1303 }
1304 }
1305 }
1306
1307 /**
1308 * Implements all Function types which return a String concatenating their inputs.
1309 */
1310 @SuppressWarnings("rawtypes")
1311 public enum ArgsToString implements Function, BiFunction, Function3, Function4, Function5, Function6, Function7, Function8, Function9 {
1312 INSTANCE;
1313
1314 @Override
1315 public Object apply(Object t1, Object t2, Object t3, Object t4, Object t5, Object t6, Object t7, Object t8,
1316 Object t9) throws Exception {
1317 return "" + t1 + t2 + t3 + t4 + t5 + t6 + t7 + t8 + t9;
1318 }
1319
1320 @Override
1321 public Object apply(Object t1, Object t2, Object t3, Object t4, Object t5, Object t6, Object t7, Object t8)
1322 throws Exception {
1323 return "" + t1 + t2 + t3 + t4 + t5 + t6 + t7 + t8;
1324 }
1325
1326 @Override
1327 public Object apply(Object t1, Object t2, Object t3, Object t4, Object t5, Object t6, Object t7)
1328 throws Exception {
1329 return "" + t1 + t2 + t3 + t4 + t5 + t6 + t7;
1330 }
1331
1332 @Override
1333 public Object apply(Object t1, Object t2, Object t3, Object t4, Object t5, Object t6) throws Exception {
1334 return "" + t1 + t2 + t3 + t4 + t5 + t6;
1335 }
1336
1337 @Override
1338 public Object apply(Object t1, Object t2, Object t3, Object t4, Object t5) throws Exception {
1339 return "" + t1 + t2 + t3 + t4 + t5;
1340 }
1341
1342 @Override
1343 public Object apply(Object t1, Object t2, Object t3, Object t4) throws Exception {
1344 return "" + t1 + t2 + t3 + t4;
1345 }
1346
1347 @Override
1348 public Object apply(Object t1, Object t2, Object t3) throws Exception {
1349 return "" + t1 + t2 + t3;
1350 }
1351
1352 @Override
1353 public Object apply(Object t1, Object t2) throws Exception {
1354 return "" + t1 + t2;
1355 }
1356
1357 @Override
1358 public Object apply(Object t1) throws Exception {
1359 return "" + t1;
1360 }
1361 }
1362
1363 @Test
1364 public void zip2DelayError() {
1365 Flowable<Integer> error1 = Flowable.error(new TestException("One"));
1366 Flowable<Integer> source1 = Flowable.range(1, 3).concatWith(error1);
1367
1368 Flowable<Integer> error2 = Flowable.error(new TestException("Two"));
1369 Flowable<Integer> source2 = Flowable.range(1, 2).concatWith(error2);
1370
1371 TestSubscriberEx<Object> ts = Flowable.zip(source1, source2, new BiFunction<Integer, Integer, Object>() {
1372 @Override
1373 public Object apply(Integer a, Integer b) throws Exception {
1374 return "" + a + b;
1375 }
1376 }, true)
1377 .to(TestHelper.<Object>testConsumer())
1378 .assertFailure(CompositeException.class, "11", "22");
1379
1380 List<Throwable> errors = TestHelper.compositeList(ts.errors().get(0));
1381 TestHelper.assertError(errors, 0, TestException.class, "One");
1382 TestHelper.assertError(errors, 1, TestException.class, "Two");
1383 assertEquals(2, errors.size());
1384 }
1385
1386 @Test
1387 public void zip2DelayErrorPrefetch() {
1388 Flowable<Integer> error1 = Flowable.error(new TestException("One"));
1389 Flowable<Integer> source1 = Flowable.range(1, 3).concatWith(error1);
1390
1391 Flowable<Integer> error2 = Flowable.error(new TestException("Two"));
1392 Flowable<Integer> source2 = Flowable.range(1, 2).concatWith(error2);
1393
1394 TestSubscriberEx<Object> ts = Flowable.zip(source1, source2, new BiFunction<Integer, Integer, Object>() {
1395 @Override
1396 public Object apply(Integer a, Integer b) throws Exception {
1397 return "" + a + b;
1398 }
1399 }, true, 1)
1400 .to(TestHelper.<Object>testConsumer())
1401 .assertFailure(CompositeException.class, "11", "22");
1402
1403 List<Throwable> errors = TestHelper.compositeList(ts.errors().get(0));
1404 TestHelper.assertError(errors, 0, TestException.class, "One");
1405 TestHelper.assertError(errors, 1, TestException.class, "Two");
1406 assertEquals(2, errors.size());
1407 }
1408
1409 @Test
1410 public void zip2Prefetch() {
1411 Flowable.zip(Flowable.range(1, 9),
1412 Flowable.range(21, 9),
1413 new BiFunction<Integer, Integer, Object>() {
1414 @Override
1415 public Object apply(Integer a, Integer b) throws Exception {
1416 return "" + a + b;
1417 }
1418 }, false, 2
1419 )
1420 .takeLast(1)
1421 .test()
1422 .assertResult("929");
1423 }
1424
1425 @Test
1426 public void zipArrayEmpty() {
1427 assertSame(Flowable.empty(), Flowable.zipArray(Functions.<Object[]>identity(), false, 16));
1428 }
1429
1430 @Test
1431 public void zip2() {
1432 Flowable.zip(Flowable.just(1),
1433 Flowable.just(2),
1434 new BiFunction<Integer, Integer, Object>() {
1435 @Override
1436 public Object apply(Integer a, Integer b) throws Exception {
1437 return "" + a + b;
1438 }
1439 }
1440 )
1441 .test()
1442 .assertResult("12");
1443 }
1444
1445 @Test
1446 public void zip3() {
1447 Flowable.zip(Flowable.just(1),
1448 Flowable.just(2), Flowable.just(3),
1449 new Function3<Integer, Integer, Integer, Object>() {
1450 @Override
1451 public Object apply(Integer a, Integer b, Integer c) throws Exception {
1452 return "" + a + b + c;
1453 }
1454 }
1455 )
1456 .test()
1457 .assertResult("123");
1458 }
1459
1460 @Test
1461 public void zip4() {
1462 Flowable.zip(Flowable.just(1),
1463 Flowable.just(2), Flowable.just(3),
1464 Flowable.just(4),
1465 new Function4<Integer, Integer, Integer, Integer, Object>() {
1466 @Override
1467 public Object apply(Integer a, Integer b, Integer c, Integer d) throws Exception {
1468 return "" + a + b + c + d;
1469 }
1470 }
1471 )
1472 .test()
1473 .assertResult("1234");
1474 }
1475
1476 @Test
1477 public void zip5() {
1478 Flowable.zip(Flowable.just(1),
1479 Flowable.just(2), Flowable.just(3),
1480 Flowable.just(4), Flowable.just(5),
1481 new Function5<Integer, Integer, Integer, Integer, Integer, Object>() {
1482 @Override
1483 public Object apply(Integer a, Integer b, Integer c, Integer d, Integer e) throws Exception {
1484 return "" + a + b + c + d + e;
1485 }
1486 }
1487 )
1488 .test()
1489 .assertResult("12345");
1490 }
1491
1492 @Test
1493 public void zip6() {
1494 Flowable.zip(Flowable.just(1),
1495 Flowable.just(2), Flowable.just(3),
1496 Flowable.just(4), Flowable.just(5),
1497 Flowable.just(6),
1498 new Function6<Integer, Integer, Integer, Integer, Integer, Integer, Object>() {
1499 @Override
1500 public Object apply(Integer a, Integer b, Integer c, Integer d, Integer e, Integer f) throws Exception {
1501 return "" + a + b + c + d + e + f;
1502 }
1503 }
1504 )
1505 .test()
1506 .assertResult("123456");
1507 }
1508
1509 @Test
1510 public void zip7() {
1511 Flowable.zip(Flowable.just(1),
1512 Flowable.just(2), Flowable.just(3),
1513 Flowable.just(4), Flowable.just(5),
1514 Flowable.just(6), Flowable.just(7),
1515 new Function7<Integer, Integer, Integer, Integer, Integer, Integer, Integer, Object>() {
1516 @Override
1517 public Object apply(Integer a, Integer b, Integer c, Integer d, Integer e, Integer f, Integer g)
1518 throws Exception {
1519 return "" + a + b + c + d + e + f + g;
1520 }
1521 }
1522 )
1523 .test()
1524 .assertResult("1234567");
1525 }
1526
1527 @Test
1528 public void zip8() {
1529 Flowable.zip(Flowable.just(1),
1530 Flowable.just(2), Flowable.just(3),
1531 Flowable.just(4), Flowable.just(5),
1532 Flowable.just(6), Flowable.just(7),
1533 Flowable.just(8),
1534 new Function8<Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Object>() {
1535 @Override
1536 public Object apply(Integer a, Integer b, Integer c, Integer d, Integer e, Integer f, Integer g,
1537 Integer h) throws Exception {
1538 return "" + a + b + c + d + e + f + g + h;
1539 }
1540 }
1541 )
1542 .test()
1543 .assertResult("12345678");
1544 }
1545
1546 @Test
1547 public void zip9() {
1548 Flowable.zip(Flowable.just(1),
1549 Flowable.just(2), Flowable.just(3),
1550 Flowable.just(4), Flowable.just(5),
1551 Flowable.just(6), Flowable.just(7),
1552 Flowable.just(8), Flowable.just(9),
1553 new Function9<Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Object>() {
1554 @Override
1555 public Object apply(Integer a, Integer b, Integer c, Integer d, Integer e, Integer f, Integer g,
1556 Integer h, Integer i) throws Exception {
1557 return "" + a + b + c + d + e + f + g + h + i;
1558 }
1559 }
1560 )
1561 .test()
1562 .assertResult("123456789");
1563 }
1564
1565 @Test
1566 public void zipArrayMany() {
1567 @SuppressWarnings("unchecked")
1568 Flowable<Integer>[] arr = new Flowable[10];
1569
1570 Arrays.fill(arr, Flowable.just(1));
1571
1572 Flowable.zip(Arrays.asList(arr), new Function<Object[], Object>() {
1573 @Override
1574 public Object apply(Object[] a) throws Exception {
1575 return Arrays.toString(a);
1576 }
1577 })
1578 .test()
1579 .assertResult("[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]");
1580 }
1581
1582 @Test
1583 public void dispose() {
1584 TestHelper.checkDisposed(Flowable.zip(Flowable.just(1), Flowable.just(1), new BiFunction<Integer, Integer, Object>() {
1585 @Override
1586 public Object apply(Integer a, Integer b) throws Exception {
1587 return a + b;
1588 }
1589 }));
1590 }
1591
1592 @Test
1593 public void badRequest() {
1594 TestHelper.assertBadRequestReported(Flowable.zip(Flowable.just(1), Flowable.just(1), new BiFunction<Integer, Integer, Object>() {
1595 @Override
1596 public Object apply(Integer a, Integer b) throws Exception {
1597 return a + b;
1598 }
1599 }));
1600 }
1601
1602 @Test
1603 public void multiError() {
1604 List<Throwable> errors = TestHelper.trackPluginErrors();
1605 try {
1606 PublishProcessor<Object> pp = PublishProcessor.create();
1607
1608 @SuppressWarnings("rawtypes")
1609 final Subscriber[] sub = { null };
1610 TestSubscriberEx<Object> ts = Flowable.zip(pp, new Flowable<Object>() {
1611 @Override
1612 protected void subscribeActual(Subscriber<? super Object> s) {
1613 sub[0] = s;
1614 }
1615 }, new BiFunction<Object, Object, Object>() {
1616 @Override
1617 public Object apply(Object a, Object b) throws Exception {
1618 return a;
1619 }
1620 })
1621 .to(TestHelper.<Object>testConsumer());
1622
1623 pp.onError(new TestException("First"));
1624
1625 ts
1626 .assertFailureAndMessage(TestException.class, "First");
1627
1628 sub[0].onError(new TestException("Second"));
1629
1630 TestHelper.assertUndeliverable(errors, 0, TestException.class, "Second");
1631 } finally {
1632 RxJavaPlugins.reset();
1633 }
1634 }
1635
1636 @Test
1637 public void singleErrorDelayed() {
1638 PublishProcessor<Object> pp1 = PublishProcessor.create();
1639 PublishProcessor<Object> pp2 = PublishProcessor.create();
1640
1641 TestSubscriberEx<Object> ts = Flowable.zip(pp1, pp2, new BiFunction<Object, Object, Object>() {
1642 @Override
1643 public Object apply(Object a, Object b) throws Exception {
1644 return a;
1645 }
1646 }, true)
1647 .to(TestHelper.<Object>testConsumer());
1648
1649 pp1.onError(new TestException("First"));
1650 pp2.onComplete();
1651
1652 ts
1653 .assertFailureAndMessage(TestException.class, "First");
1654 }
1655
1656 @Test
1657 public void singleErrorDelayedBackpressured() {
1658 PublishProcessor<Object> pp1 = PublishProcessor.create();
1659 PublishProcessor<Object> pp2 = PublishProcessor.create();
1660
1661 TestSubscriberEx<Object> ts = Flowable.zip(pp1, pp2, new BiFunction<Object, Object, Object>() {
1662 @Override
1663 public Object apply(Object a, Object b) throws Exception {
1664 return a;
1665 }
1666 })
1667 .to(TestHelper.<Object>testSubscriber(0L));
1668
1669 pp1.onError(new TestException("First"));
1670 pp2.onComplete();
1671
1672 ts
1673 .assertFailureAndMessage(TestException.class, "First");
1674 }
1675
1676 @Test
1677 public void fusedInputThrows() {
1678 Flowable.zip(Flowable.just(1).map(new Function<Integer, Integer>() {
1679 @Override
1680 public Integer apply(Integer v) throws Exception {
1681 throw new TestException();
1682 }
1683 }), Flowable.just(2), new BiFunction<Integer, Integer, Integer>() {
1684 @Override
1685 public Integer apply(Integer a, Integer b) throws Exception {
1686 return a + b;
1687 }
1688 })
1689 .test()
1690 .assertFailure(TestException.class);
1691 }
1692
1693 @Test
1694 public void fusedInputThrowsDelayError() {
1695 Flowable.zip(Flowable.just(1).map(new Function<Integer, Integer>() {
1696 @Override
1697 public Integer apply(Integer v) throws Exception {
1698 throw new TestException();
1699 }
1700 }), Flowable.just(2), new BiFunction<Integer, Integer, Integer>() {
1701 @Override
1702 public Integer apply(Integer a, Integer b) throws Exception {
1703 return a + b;
1704 }
1705 }, true)
1706 .test()
1707 .assertFailure(TestException.class);
1708 }
1709
1710 @Test
1711 public void fusedInputThrowsBackpressured() {
1712 Flowable.zip(Flowable.just(1).map(new Function<Integer, Integer>() {
1713 @Override
1714 public Integer apply(Integer v) throws Exception {
1715 throw new TestException();
1716 }
1717 }), Flowable.just(2), new BiFunction<Integer, Integer, Integer>() {
1718 @Override
1719 public Integer apply(Integer a, Integer b) throws Exception {
1720 return a + b;
1721 }
1722 })
1723 .test(0L)
1724 .assertFailure(TestException.class);
1725 }
1726
1727 @Test
1728 public void fusedInputThrowsDelayErrorBackpressured() {
1729 Flowable.zip(Flowable.just(1).map(new Function<Integer, Integer>() {
1730 @Override
1731 public Integer apply(Integer v) throws Exception {
1732 throw new TestException();
1733 }
1734 }), Flowable.just(2), new BiFunction<Integer, Integer, Integer>() {
1735 @Override
1736 public Integer apply(Integer a, Integer b) throws Exception {
1737 return a + b;
1738 }
1739 }, true)
1740 .test(0L)
1741 .assertFailure(TestException.class);
1742 }
1743
1744 @Test
1745 public void noCrossBoundaryFusion() {
1746 for (int i = 0; i < 500; i++) {
1747 TestSubscriber<List<Object>> ts = Flowable.zip(
1748 Flowable.just(1).observeOn(Schedulers.single()).map(new Function<Integer, Object>() {
1749 @Override
1750 public Object apply(Integer v) throws Exception {
1751 return Thread.currentThread().getName().substring(0, 4);
1752 }
1753 }),
1754 Flowable.just(1).observeOn(Schedulers.computation()).map(new Function<Integer, Object>() {
1755 @Override
1756 public Object apply(Integer v) throws Exception {
1757 return Thread.currentThread().getName().substring(0, 4);
1758 }
1759 }),
1760 new BiFunction<Object, Object, List<Object>>() {
1761 @Override
1762 public List<Object> apply(Object t1, Object t2) throws Exception {
1763 return Arrays.asList(t1, t2);
1764 }
1765 }
1766 )
1767 .test()
1768 .awaitDone(5, TimeUnit.SECONDS)
1769 .assertValueCount(1);
1770
1771 List<Object> list = ts.values().get(0);
1772
1773 assertTrue(list.toString(), list.contains("RxSi"));
1774 assertTrue(list.toString(), list.contains("RxCo"));
1775 }
1776 }
1777
1778 static final class ThrowingQueueSubscription implements QueueSubscription<Integer>, Publisher<Integer> {
1779
1780 @Override
1781 public int requestFusion(int mode) {
1782 return mode & SYNC;
1783 }
1784
1785 @Override
1786 public boolean offer(Integer value) {
1787 throw new UnsupportedOperationException();
1788 }
1789
1790 @Override
1791 public boolean offer(Integer v1, Integer v2) {
1792 throw new UnsupportedOperationException();
1793 }
1794
1795 @Override
1796 public Integer poll() throws Exception {
1797 throw new TestException();
1798 }
1799
1800 @Override
1801 public boolean isEmpty() {
1802 return false;
1803 }
1804
1805 @Override
1806 public void clear() {
1807 }
1808
1809 @Override
1810 public void request(long n) {
1811 }
1812
1813 @Override
1814 public void cancel() {
1815 }
1816
1817 @Override
1818 public void subscribe(Subscriber<? super Integer> s) {
1819 s.onSubscribe(this);
1820 }
1821 }
1822
1823 @Test
1824 public void fusedInputThrows2() {
1825 Flowable.zip(new ThrowingQueueSubscription(), Flowable.just(1), new BiFunction<Integer, Integer, Integer>() {
1826 @Override
1827 public Integer apply(Integer a, Integer b) throws Exception {
1828 return a + b;
1829 }
1830 })
1831 .test()
1832 .assertFailure(TestException.class);
1833 }
1834
1835 @Test
1836 public void fusedInputThrows2Backpressured() {
1837 Flowable.zip(new ThrowingQueueSubscription(), Flowable.just(1), new BiFunction<Integer, Integer, Integer>() {
1838 @Override
1839 public Integer apply(Integer a, Integer b) throws Exception {
1840 return a + b;
1841 }
1842 })
1843 .test(0)
1844 .assertFailure(TestException.class);
1845 }
1846
1847 @Test
1848 public void cancelOnBackpressureBoundary() {
1849 TestSubscriber<Integer> ts = new TestSubscriber<Integer>(1L) {
1850 @Override
1851 public void onNext(Integer t) {
1852 super.onNext(t);
1853 cancel();
1854 onComplete();
1855 }
1856 };
1857
1858 Flowable.zip(Flowable.range(1, 2), Flowable.range(3, 2), new BiFunction<Integer, Integer, Integer>() {
1859 @Override
1860 public Integer apply(Integer a, Integer b) throws Exception {
1861 return a + b;
1862 }
1863 })
1864 .subscribe(ts);
1865
1866 ts.assertResult(4);
1867 }
1868
1869 @Test
1870 public void firstErrorPreventsSecondSubscription() {
1871 final AtomicInteger counter = new AtomicInteger();
1872
1873 List<Flowable<?>> flowableList = new ArrayList<>();
1874 flowableList.add(Flowable.create(new FlowableOnSubscribe<Object>() {
1875 @Override
1876 public void subscribe(FlowableEmitter<Object> e)
1877 throws Exception { throw new TestException(); }
1878 }, BackpressureStrategy.MISSING));
1879 flowableList.add(Flowable.create(new FlowableOnSubscribe<Object>() {
1880 @Override
1881 public void subscribe(FlowableEmitter<Object> e)
1882 throws Exception { counter.getAndIncrement(); }
1883 }, BackpressureStrategy.MISSING));
1884
1885 Flowable.zip(flowableList,
1886 new Function<Object[], Object>() {
1887 @Override
1888 public Object apply(Object[] a) throws Exception {
1889 return a;
1890 }
1891 })
1892 .test()
1893 .assertFailure(TestException.class)
1894 ;
1895
1896 assertEquals(0, counter.get());
1897 }
1898
1899 @Test
1900 public void publishersInIterable() {
1901 Publisher<Integer> source = new Publisher<Integer>() {
1902 @Override
1903 public void subscribe(Subscriber<? super Integer> subscriber) {
1904 Flowable.just(1).subscribe(subscriber);
1905 }
1906 };
1907
1908 Flowable.zip(Arrays.asList(source, source), new Function<Object[], Integer>() {
1909 @Override
1910 public Integer apply(Object[] t) throws Throwable {
1911 return 2;
1912 }
1913 })
1914 .test()
1915 .assertResult(2);
1916 }
1917
1918 @Test
1919 public void fusedInnerPollCrashDelayError() {
1920 Flowable.zip(
1921 Flowable.range(1, 5),
1922 Flowable.just(1)
1923 .<Integer>map(v -> { throw new TestException(); })
1924 .compose(TestHelper.flowableStripBoundary()),
1925 (a, b) -> a + b, true
1926 )
1927 .test()
1928 .assertFailure(TestException.class);
1929 }
1930
1931 @Test
1932 public void fusedInnerPollCrashRequestBoundaryDelayError() {
1933 Flowable.zip(
1934 Flowable.range(1, 5),
1935 Flowable.just(1)
1936 .<Integer>map(v -> { throw new TestException(); })
1937 .compose(TestHelper.flowableStripBoundary()),
1938 (a, b) -> a + b, true
1939 )
1940 .test(0L)
1941 .assertFailure(TestException.class);
1942 }
1943 }