Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableCombineLatestTest.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.*;
24
25 import org.junit.Test;
26 import org.mockito.*;
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.operators.flowable.FlowableZipTest.ArgsToString;
34 import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription;
35 import io.reactivex.rxjava3.operators.QueueFuseable;
36 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
37 import io.reactivex.rxjava3.processors.PublishProcessor;
38 import io.reactivex.rxjava3.schedulers.*;
39 import io.reactivex.rxjava3.subscribers.*;
40 import io.reactivex.rxjava3.testsupport.*;
41
42 public class FlowableCombineLatestTest extends RxJavaTest {
43
44 @Test
45 public void combineLatestWithFunctionThatThrowsAnException() {
46 Subscriber<String> w = TestHelper.mockSubscriber();
47
48 PublishProcessor<String> w1 = PublishProcessor.create();
49 PublishProcessor<String> w2 = PublishProcessor.create();
50
51 Flowable<String> combined = Flowable.combineLatest(w1, w2, new BiFunction<String, String, String>() {
52 @Override
53 public String apply(String v1, String v2) {
54 throw new RuntimeException("I don't work.");
55 }
56 });
57 combined.subscribe(w);
58
59 w1.onNext("first value of w1");
60 w2.onNext("first value of w2");
61
62 verify(w, never()).onNext(anyString());
63 verify(w, never()).onComplete();
64 verify(w, times(1)).onError(Mockito.<RuntimeException> any());
65 }
66
67 @Test
68 public void combineLatestDifferentLengthFlowableSequences1() {
69 Subscriber<String> w = TestHelper.mockSubscriber();
70
71 PublishProcessor<String> w1 = PublishProcessor.create();
72 PublishProcessor<String> w2 = PublishProcessor.create();
73 PublishProcessor<String> w3 = PublishProcessor.create();
74
75 Flowable<String> combineLatestW = Flowable.combineLatest(w1, w2, w3,
76 getConcat3StringsCombineLatestFunction());
77 combineLatestW.subscribe(w);
78
79 /* simulate sending data */
80 // once for w1
81 w1.onNext("1a");
82 w2.onNext("2a");
83 w3.onNext("3a");
84 w1.onComplete();
85 // twice for w2
86 w2.onNext("2b");
87 w2.onComplete();
88 // 4 times for w3
89 w3.onNext("3b");
90 w3.onNext("3c");
91 w3.onNext("3d");
92 w3.onComplete();
93
94 /* we should have been called 4 times on the Observer */
95 InOrder inOrder = inOrder(w);
96 inOrder.verify(w).onNext("1a2a3a");
97 inOrder.verify(w).onNext("1a2b3a");
98 inOrder.verify(w).onNext("1a2b3b");
99 inOrder.verify(w).onNext("1a2b3c");
100 inOrder.verify(w).onNext("1a2b3d");
101 inOrder.verify(w, never()).onNext(anyString());
102 inOrder.verify(w, times(1)).onComplete();
103 }
104
105 @Test
106 public void combineLatestDifferentLengthFlowableSequences2() {
107 Subscriber<String> w = TestHelper.mockSubscriber();
108
109 PublishProcessor<String> w1 = PublishProcessor.create();
110 PublishProcessor<String> w2 = PublishProcessor.create();
111 PublishProcessor<String> w3 = PublishProcessor.create();
112
113 Flowable<String> combineLatestW = Flowable.combineLatest(w1, w2, w3, getConcat3StringsCombineLatestFunction());
114 combineLatestW.subscribe(w);
115
116 /* simulate sending data */
117 // 4 times for w1
118 w1.onNext("1a");
119 w1.onNext("1b");
120 w1.onNext("1c");
121 w1.onNext("1d");
122 w1.onComplete();
123 // twice for w2
124 w2.onNext("2a");
125 w2.onNext("2b");
126 w2.onComplete();
127 // 1 times for w3
128 w3.onNext("3a");
129 w3.onComplete();
130
131 /* we should have been called 1 time only on the Observer since we only combine the "latest" we don't go back and loop through others once completed */
132 InOrder inOrder = inOrder(w);
133 inOrder.verify(w, times(1)).onNext("1d2b3a");
134 inOrder.verify(w, never()).onNext(anyString());
135
136 inOrder.verify(w, times(1)).onComplete();
137
138 }
139
140 @Test
141 public void combineLatestWithInterleavingSequences() {
142 Subscriber<String> w = TestHelper.mockSubscriber();
143
144 PublishProcessor<String> w1 = PublishProcessor.create();
145 PublishProcessor<String> w2 = PublishProcessor.create();
146 PublishProcessor<String> w3 = PublishProcessor.create();
147
148 Flowable<String> combineLatestW = Flowable.combineLatest(w1, w2, w3, getConcat3StringsCombineLatestFunction());
149 combineLatestW.subscribe(w);
150
151 /* simulate sending data */
152 w1.onNext("1a");
153 w2.onNext("2a");
154 w2.onNext("2b");
155 w3.onNext("3a");
156
157 w1.onNext("1b");
158 w2.onNext("2c");
159 w2.onNext("2d");
160 w3.onNext("3b");
161
162 w1.onComplete();
163 w2.onComplete();
164 w3.onComplete();
165
166 /* we should have been called 5 times on the Observer */
167 InOrder inOrder = inOrder(w);
168 inOrder.verify(w).onNext("1a2b3a");
169 inOrder.verify(w).onNext("1b2b3a");
170 inOrder.verify(w).onNext("1b2c3a");
171 inOrder.verify(w).onNext("1b2d3a");
172 inOrder.verify(w).onNext("1b2d3b");
173
174 inOrder.verify(w, never()).onNext(anyString());
175 inOrder.verify(w, times(1)).onComplete();
176 }
177
178 @Test
179 public void combineLatest2Types() {
180 BiFunction<String, Integer, String> combineLatestFunction = getConcatStringIntegerCombineLatestFunction();
181
182 /* define an Observer to receive aggregated events */
183 Subscriber<String> subscriber = TestHelper.mockSubscriber();
184
185 Flowable<String> w = Flowable.combineLatest(Flowable.just("one", "two"), Flowable.just(2, 3, 4), combineLatestFunction);
186 w.subscribe(subscriber);
187
188 verify(subscriber, never()).onError(any(Throwable.class));
189 verify(subscriber, times(1)).onComplete();
190 verify(subscriber, times(1)).onNext("two2");
191 verify(subscriber, times(1)).onNext("two3");
192 verify(subscriber, times(1)).onNext("two4");
193 }
194
195 @Test
196 public void combineLatest3TypesA() {
197 Function3<String, Integer, int[], String> combineLatestFunction = getConcatStringIntegerIntArrayCombineLatestFunction();
198
199 /* define an Observer to receive aggregated events */
200 Subscriber<String> subscriber = TestHelper.mockSubscriber();
201
202 Flowable<String> w = Flowable.combineLatest(Flowable.just("one", "two"), Flowable.just(2), Flowable.just(new int[] { 4, 5, 6 }), combineLatestFunction);
203 w.subscribe(subscriber);
204
205 verify(subscriber, never()).onError(any(Throwable.class));
206 verify(subscriber, times(1)).onComplete();
207 verify(subscriber, times(1)).onNext("two2[4, 5, 6]");
208 }
209
210 @Test
211 public void combineLatest3TypesB() {
212 Function3<String, Integer, int[], String> combineLatestFunction = getConcatStringIntegerIntArrayCombineLatestFunction();
213
214 /* define an Observer to receive aggregated events */
215 Subscriber<String> subscriber = TestHelper.mockSubscriber();
216
217 Flowable<String> w = Flowable.combineLatest(Flowable.just("one"), Flowable.just(2), Flowable.just(new int[] { 4, 5, 6 }, new int[] { 7, 8 }), combineLatestFunction);
218 w.subscribe(subscriber);
219
220 verify(subscriber, never()).onError(any(Throwable.class));
221 verify(subscriber, times(1)).onComplete();
222 verify(subscriber, times(1)).onNext("one2[4, 5, 6]");
223 verify(subscriber, times(1)).onNext("one2[7, 8]");
224 }
225
226 private Function3<String, String, String, String> getConcat3StringsCombineLatestFunction() {
227 Function3<String, String, String, String> combineLatestFunction = new Function3<String, String, String, String>() {
228 @Override
229 public String apply(String a1, String a2, String a3) {
230 if (a1 == null) {
231 a1 = "";
232 }
233 if (a2 == null) {
234 a2 = "";
235 }
236 if (a3 == null) {
237 a3 = "";
238 }
239 return a1 + a2 + a3;
240 }
241 };
242 return combineLatestFunction;
243 }
244
245 private BiFunction<String, Integer, String> getConcatStringIntegerCombineLatestFunction() {
246 BiFunction<String, Integer, String> combineLatestFunction = new BiFunction<String, Integer, String>() {
247 @Override
248 public String apply(String s, Integer i) {
249 return getStringValue(s) + getStringValue(i);
250 }
251 };
252 return combineLatestFunction;
253 }
254
255 private Function3<String, Integer, int[], String> getConcatStringIntegerIntArrayCombineLatestFunction() {
256 return new Function3<String, Integer, int[], String>() {
257 @Override
258 public String apply(String s, Integer i, int[] iArray) {
259 return getStringValue(s) + getStringValue(i) + getStringValue(iArray);
260 }
261 };
262 }
263
264 private static String getStringValue(Object o) {
265 if (o == null) {
266 return "";
267 } else {
268 if (o instanceof int[]) {
269 return Arrays.toString((int[]) o);
270 } else {
271 return String.valueOf(o);
272 }
273 }
274 }
275
276 BiFunction<Integer, Integer, Integer> or = new BiFunction<Integer, Integer, Integer>() {
277 @Override
278 public Integer apply(Integer t1, Integer t2) {
279 return t1 | t2;
280 }
281 };
282
283 @Test
284 public void combineSimple() {
285 PublishProcessor<Integer> a = PublishProcessor.create();
286 PublishProcessor<Integer> b = PublishProcessor.create();
287
288 Flowable<Integer> source = Flowable.combineLatest(a, b, or);
289
290 Subscriber<Object> subscriber = TestHelper.mockSubscriber();
291 InOrder inOrder = inOrder(subscriber);
292
293 source.subscribe(subscriber);
294
295 a.onNext(1);
296
297 inOrder.verify(subscriber, never()).onNext(any());
298
299 a.onNext(2);
300
301 inOrder.verify(subscriber, never()).onNext(any());
302
303 b.onNext(0x10);
304
305 inOrder.verify(subscriber, times(1)).onNext(0x12);
306
307 b.onNext(0x20);
308 inOrder.verify(subscriber, times(1)).onNext(0x22);
309
310 b.onComplete();
311
312 inOrder.verify(subscriber, never()).onComplete();
313
314 a.onComplete();
315
316 inOrder.verify(subscriber, times(1)).onComplete();
317
318 a.onNext(3);
319 b.onNext(0x30);
320 a.onComplete();
321 b.onComplete();
322
323 inOrder.verifyNoMoreInteractions();
324 verify(subscriber, never()).onError(any(Throwable.class));
325 }
326
327 @Test
328 public void combineMultipleObservers() {
329 PublishProcessor<Integer> a = PublishProcessor.create();
330 PublishProcessor<Integer> b = PublishProcessor.create();
331
332 Flowable<Integer> source = Flowable.combineLatest(a, b, or);
333
334 Subscriber<Object> subscriber1 = TestHelper.mockSubscriber();
335
336 Subscriber<Object> subscriber2 = TestHelper.mockSubscriber();
337
338 InOrder inOrder1 = inOrder(subscriber1);
339 InOrder inOrder2 = inOrder(subscriber2);
340
341 source.subscribe(subscriber1);
342 source.subscribe(subscriber2);
343
344 a.onNext(1);
345
346 inOrder1.verify(subscriber1, never()).onNext(any());
347 inOrder2.verify(subscriber2, never()).onNext(any());
348
349 a.onNext(2);
350
351 inOrder1.verify(subscriber1, never()).onNext(any());
352 inOrder2.verify(subscriber2, never()).onNext(any());
353
354 b.onNext(0x10);
355
356 inOrder1.verify(subscriber1, times(1)).onNext(0x12);
357 inOrder2.verify(subscriber2, times(1)).onNext(0x12);
358
359 b.onNext(0x20);
360 inOrder1.verify(subscriber1, times(1)).onNext(0x22);
361 inOrder2.verify(subscriber2, times(1)).onNext(0x22);
362
363 b.onComplete();
364
365 inOrder1.verify(subscriber1, never()).onComplete();
366 inOrder2.verify(subscriber2, never()).onComplete();
367
368 a.onComplete();
369
370 inOrder1.verify(subscriber1, times(1)).onComplete();
371 inOrder2.verify(subscriber2, times(1)).onComplete();
372
373 a.onNext(3);
374 b.onNext(0x30);
375 a.onComplete();
376 b.onComplete();
377
378 inOrder1.verifyNoMoreInteractions();
379 inOrder2.verifyNoMoreInteractions();
380 verify(subscriber1, never()).onError(any(Throwable.class));
381 verify(subscriber2, never()).onError(any(Throwable.class));
382 }
383
384 @Test
385 public void firstNeverProduces() {
386 PublishProcessor<Integer> a = PublishProcessor.create();
387 PublishProcessor<Integer> b = PublishProcessor.create();
388
389 Flowable<Integer> source = Flowable.combineLatest(a, b, or);
390
391 Subscriber<Object> subscriber = TestHelper.mockSubscriber();
392 InOrder inOrder = inOrder(subscriber);
393
394 source.subscribe(subscriber);
395
396 b.onNext(0x10);
397 b.onNext(0x20);
398
399 a.onComplete();
400
401 inOrder.verify(subscriber, times(1)).onComplete();
402 verify(subscriber, never()).onNext(any());
403 verify(subscriber, never()).onError(any(Throwable.class));
404 }
405
406 @Test
407 public void secondNeverProduces() {
408 PublishProcessor<Integer> a = PublishProcessor.create();
409 PublishProcessor<Integer> b = PublishProcessor.create();
410
411 Flowable<Integer> source = Flowable.combineLatest(a, b, or);
412
413 Subscriber<Object> subscriber = TestHelper.mockSubscriber();
414 InOrder inOrder = inOrder(subscriber);
415
416 source.subscribe(subscriber);
417
418 a.onNext(0x1);
419 a.onNext(0x2);
420
421 b.onComplete();
422 a.onComplete();
423
424 inOrder.verify(subscriber, times(1)).onComplete();
425 verify(subscriber, never()).onNext(any());
426 verify(subscriber, never()).onError(any(Throwable.class));
427 }
428
429 @Test
430 public void oneToNSources() {
431 int n = 30;
432 Function<Object[], List<Object>> func = new Function<Object[], List<Object>>() {
433
434 @Override
435 public List<Object> apply(Object[] args) {
436 return Arrays.asList(args);
437 }
438 };
439 for (int i = 1; i <= n; i++) {
440 System.out.println("test1ToNSources: " + i + " sources");
441 List<Flowable<Integer>> sources = new ArrayList<>();
442 List<Object> values = new ArrayList<>();
443 for (int j = 0; j < i; j++) {
444 sources.add(Flowable.just(j));
445 values.add(j);
446 }
447
448 Flowable<List<Object>> result = Flowable.combineLatest(sources, func);
449
450 Subscriber<List<Object>> subscriber = TestHelper.mockSubscriber();
451
452 result.subscribe(subscriber);
453
454 verify(subscriber).onNext(values);
455 verify(subscriber).onComplete();
456 verify(subscriber, never()).onError(any(Throwable.class));
457 }
458 }
459
460 @Test
461 public void oneToNSourcesScheduled() throws InterruptedException {
462 int n = 10;
463 Function<Object[], List<Object>> func = new Function<Object[], List<Object>>() {
464
465 @Override
466 public List<Object> apply(Object[] args) {
467 return Arrays.asList(args);
468 }
469 };
470 for (int i = 1; i <= n; i++) {
471 System.out.println("test1ToNSourcesScheduled: " + i + " sources");
472 List<Flowable<Integer>> sources = new ArrayList<>();
473 List<Object> values = new ArrayList<>();
474 for (int j = 0; j < i; j++) {
475 sources.add(Flowable.just(j).subscribeOn(Schedulers.io()));
476 values.add(j);
477 }
478
479 Flowable<List<Object>> result = Flowable.combineLatest(sources, func);
480
481 final Subscriber<List<Object>> subscriber = TestHelper.mockSubscriber();
482
483 final CountDownLatch cdl = new CountDownLatch(1);
484
485 Subscriber<List<Object>> s = new DefaultSubscriber<List<Object>>() {
486
487 @Override
488 public void onNext(List<Object> t) {
489 subscriber.onNext(t);
490 }
491
492 @Override
493 public void onError(Throwable e) {
494 subscriber.onError(e);
495 cdl.countDown();
496 }
497
498 @Override
499 public void onComplete() {
500 subscriber.onComplete();
501 cdl.countDown();
502 }
503 };
504
505 result.subscribe(s);
506
507 cdl.await();
508
509 verify(subscriber).onNext(values);
510 verify(subscriber).onComplete();
511 verify(subscriber, never()).onError(any(Throwable.class));
512 }
513 }
514
515 @Test
516 public void twoSourcesOverload() {
517 Flowable<Integer> s1 = Flowable.just(1);
518 Flowable<Integer> s2 = Flowable.just(2);
519
520 Flowable<List<Integer>> result = Flowable.combineLatest(s1, s2,
521 new BiFunction<Integer, Integer, List<Integer>>() {
522 @Override
523 public List<Integer> apply(Integer t1, Integer t2) {
524 return Arrays.asList(t1, t2);
525 }
526 });
527
528 Subscriber<Object> subscriber = TestHelper.mockSubscriber();
529
530 result.subscribe(subscriber);
531
532 verify(subscriber).onNext(Arrays.asList(1, 2));
533 verify(subscriber).onComplete();
534 verify(subscriber, never()).onError(any(Throwable.class));
535 }
536
537 @Test
538 public void threeSourcesOverload() {
539 Flowable<Integer> s1 = Flowable.just(1);
540 Flowable<Integer> s2 = Flowable.just(2);
541 Flowable<Integer> s3 = Flowable.just(3);
542
543 Flowable<List<Integer>> result = Flowable.combineLatest(s1, s2, s3,
544 new Function3<Integer, Integer, Integer, List<Integer>>() {
545 @Override
546 public List<Integer> apply(Integer t1, Integer t2, Integer t3) {
547 return Arrays.asList(t1, t2, t3);
548 }
549 });
550
551 Subscriber<Object> subscriber = TestHelper.mockSubscriber();
552
553 result.subscribe(subscriber);
554
555 verify(subscriber).onNext(Arrays.asList(1, 2, 3));
556 verify(subscriber).onComplete();
557 verify(subscriber, never()).onError(any(Throwable.class));
558 }
559
560 @Test
561 public void fourSourcesOverload() {
562 Flowable<Integer> s1 = Flowable.just(1);
563 Flowable<Integer> s2 = Flowable.just(2);
564 Flowable<Integer> s3 = Flowable.just(3);
565 Flowable<Integer> s4 = Flowable.just(4);
566
567 Flowable<List<Integer>> result = Flowable.combineLatest(s1, s2, s3, s4,
568 new Function4<Integer, Integer, Integer, Integer, List<Integer>>() {
569 @Override
570 public List<Integer> apply(Integer t1, Integer t2, Integer t3, Integer t4) {
571 return Arrays.asList(t1, t2, t3, t4);
572 }
573 });
574
575 Subscriber<Object> subscriber = TestHelper.mockSubscriber();
576
577 result.subscribe(subscriber);
578
579 verify(subscriber).onNext(Arrays.asList(1, 2, 3, 4));
580 verify(subscriber).onComplete();
581 verify(subscriber, never()).onError(any(Throwable.class));
582 }
583
584 @Test
585 public void fiveSourcesOverload() {
586 Flowable<Integer> s1 = Flowable.just(1);
587 Flowable<Integer> s2 = Flowable.just(2);
588 Flowable<Integer> s3 = Flowable.just(3);
589 Flowable<Integer> s4 = Flowable.just(4);
590 Flowable<Integer> s5 = Flowable.just(5);
591
592 Flowable<List<Integer>> result = Flowable.combineLatest(s1, s2, s3, s4, s5,
593 new Function5<Integer, Integer, Integer, Integer, Integer, List<Integer>>() {
594 @Override
595 public List<Integer> apply(Integer t1, Integer t2, Integer t3, Integer t4, Integer t5) {
596 return Arrays.asList(t1, t2, t3, t4, t5);
597 }
598 });
599
600 Subscriber<Object> subscriber = TestHelper.mockSubscriber();
601
602 result.subscribe(subscriber);
603
604 verify(subscriber).onNext(Arrays.asList(1, 2, 3, 4, 5));
605 verify(subscriber).onComplete();
606 verify(subscriber, never()).onError(any(Throwable.class));
607 }
608
609 @Test
610 public void sixSourcesOverload() {
611 Flowable<Integer> s1 = Flowable.just(1);
612 Flowable<Integer> s2 = Flowable.just(2);
613 Flowable<Integer> s3 = Flowable.just(3);
614 Flowable<Integer> s4 = Flowable.just(4);
615 Flowable<Integer> s5 = Flowable.just(5);
616 Flowable<Integer> s6 = Flowable.just(6);
617
618 Flowable<List<Integer>> result = Flowable.combineLatest(s1, s2, s3, s4, s5, s6,
619 new Function6<Integer, Integer, Integer, Integer, Integer, Integer, List<Integer>>() {
620 @Override
621 public List<Integer> apply(Integer t1, Integer t2, Integer t3, Integer t4, Integer t5, Integer t6) {
622 return Arrays.asList(t1, t2, t3, t4, t5, t6);
623 }
624 });
625
626 Subscriber<Object> subscriber = TestHelper.mockSubscriber();
627
628 result.subscribe(subscriber);
629
630 verify(subscriber).onNext(Arrays.asList(1, 2, 3, 4, 5, 6));
631 verify(subscriber).onComplete();
632 verify(subscriber, never()).onError(any(Throwable.class));
633 }
634
635 @Test
636 public void sevenSourcesOverload() {
637 Flowable<Integer> s1 = Flowable.just(1);
638 Flowable<Integer> s2 = Flowable.just(2);
639 Flowable<Integer> s3 = Flowable.just(3);
640 Flowable<Integer> s4 = Flowable.just(4);
641 Flowable<Integer> s5 = Flowable.just(5);
642 Flowable<Integer> s6 = Flowable.just(6);
643 Flowable<Integer> s7 = Flowable.just(7);
644
645 Flowable<List<Integer>> result = Flowable.combineLatest(s1, s2, s3, s4, s5, s6, s7,
646 new Function7<Integer, Integer, Integer, Integer, Integer, Integer, Integer, List<Integer>>() {
647 @Override
648 public List<Integer> apply(Integer t1, Integer t2, Integer t3, Integer t4, Integer t5, Integer t6, Integer t7) {
649 return Arrays.asList(t1, t2, t3, t4, t5, t6, t7);
650 }
651 });
652
653 Subscriber<Object> subscriber = TestHelper.mockSubscriber();
654
655 result.subscribe(subscriber);
656
657 verify(subscriber).onNext(Arrays.asList(1, 2, 3, 4, 5, 6, 7));
658 verify(subscriber).onComplete();
659 verify(subscriber, never()).onError(any(Throwable.class));
660 }
661
662 @Test
663 public void eightSourcesOverload() {
664 Flowable<Integer> s1 = Flowable.just(1);
665 Flowable<Integer> s2 = Flowable.just(2);
666 Flowable<Integer> s3 = Flowable.just(3);
667 Flowable<Integer> s4 = Flowable.just(4);
668 Flowable<Integer> s5 = Flowable.just(5);
669 Flowable<Integer> s6 = Flowable.just(6);
670 Flowable<Integer> s7 = Flowable.just(7);
671 Flowable<Integer> s8 = Flowable.just(8);
672
673 Flowable<List<Integer>> result = Flowable.combineLatest(s1, s2, s3, s4, s5, s6, s7, s8,
674 new Function8<Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, List<Integer>>() {
675 @Override
676 public List<Integer> apply(Integer t1, Integer t2, Integer t3, Integer t4, Integer t5, Integer t6, Integer t7, Integer t8) {
677 return Arrays.asList(t1, t2, t3, t4, t5, t6, t7, t8);
678 }
679 });
680
681 Subscriber<Object> subscriber = TestHelper.mockSubscriber();
682
683 result.subscribe(subscriber);
684
685 verify(subscriber).onNext(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8));
686 verify(subscriber).onComplete();
687 verify(subscriber, never()).onError(any(Throwable.class));
688 }
689
690 @Test
691 public void nineSourcesOverload() {
692 Flowable<Integer> s1 = Flowable.just(1);
693 Flowable<Integer> s2 = Flowable.just(2);
694 Flowable<Integer> s3 = Flowable.just(3);
695 Flowable<Integer> s4 = Flowable.just(4);
696 Flowable<Integer> s5 = Flowable.just(5);
697 Flowable<Integer> s6 = Flowable.just(6);
698 Flowable<Integer> s7 = Flowable.just(7);
699 Flowable<Integer> s8 = Flowable.just(8);
700 Flowable<Integer> s9 = Flowable.just(9);
701
702 Flowable<List<Integer>> result = Flowable.combineLatest(s1, s2, s3, s4, s5, s6, s7, s8, s9,
703 new Function9<Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, List<Integer>>() {
704 @Override
705 public List<Integer> apply(Integer t1, Integer t2, Integer t3, Integer t4, Integer t5, Integer t6, Integer t7, Integer t8, Integer t9) {
706 return Arrays.asList(t1, t2, t3, t4, t5, t6, t7, t8, t9);
707 }
708 });
709
710 Subscriber<Object> subscriber = TestHelper.mockSubscriber();
711
712 result.subscribe(subscriber);
713
714 verify(subscriber).onNext(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9));
715 verify(subscriber).onComplete();
716 verify(subscriber, never()).onError(any(Throwable.class));
717 }
718
719 @Test
720 public void zeroSources() {
721 Flowable<Object> result = Flowable.combineLatest(
722 Collections.<Flowable<Object>> emptyList(), new Function<Object[], Object>() {
723
724 @Override
725 public Object apply(Object[] args) {
726 return args;
727 }
728
729 });
730
731 Subscriber<Object> subscriber = TestHelper.mockSubscriber();
732
733 result.subscribe(subscriber);
734
735 verify(subscriber).onComplete();
736 verify(subscriber, never()).onNext(any());
737 verify(subscriber, never()).onError(any(Throwable.class));
738
739 }
740
741 @Test
742 public void backpressureLoop() {
743 for (int i = 0; i < 5000; i++) {
744 backpressure();
745 }
746 }
747
748 @Test
749 public void backpressure() {
750 BiFunction<String, Integer, String> combineLatestFunction = getConcatStringIntegerCombineLatestFunction();
751
752 int num = Flowable.bufferSize() * 4;
753 TestSubscriber<String> ts = new TestSubscriber<>();
754 Flowable.combineLatest(
755 Flowable.just("one", "two"),
756 Flowable.range(2, num),
757 combineLatestFunction
758 )
759 .observeOn(Schedulers.computation())
760 .subscribe(ts);
761
762 ts.awaitDone(5, TimeUnit.SECONDS);
763 ts.assertNoErrors();
764 List<String> events = ts.values();
765 assertEquals("two2", events.get(0));
766 assertEquals("two3", events.get(1));
767 assertEquals("two4", events.get(2));
768 assertEquals(num, events.size());
769 }
770
771 @Test
772 public void withCombineLatestIssue1717() throws InterruptedException {
773 final CountDownLatch latch = new CountDownLatch(1);
774 final AtomicInteger count = new AtomicInteger();
775 final int SIZE = 2000;
776 Flowable<Long> timer = Flowable.interval(0, 1, TimeUnit.MILLISECONDS)
777 .observeOn(Schedulers.newThread())
778 .doOnEach(new Consumer<Notification<Long>>() {
779 @Override
780 public void accept(Notification<Long> n) {
781 // System.out.println(n);
782 if (count.incrementAndGet() >= SIZE) {
783 latch.countDown();
784 }
785 }
786 }).take(SIZE);
787
788 TestSubscriber<Long> ts = new TestSubscriber<>();
789
790 Flowable.combineLatest(timer, Flowable.<Integer> never(), new BiFunction<Long, Integer, Long>() {
791 @Override
792 public Long apply(Long t1, Integer t2) {
793 return t1;
794 }
795 }).subscribe(ts);
796
797 if (!latch.await(SIZE + 2000, TimeUnit.MILLISECONDS)) {
798 fail("timed out");
799 }
800
801 assertEquals(SIZE, count.get());
802 }
803
804 @Test
805 public void combineLatestRequestOverflow() throws InterruptedException {
806 List<Flowable<Integer>> sources = Arrays.asList(Flowable.fromArray(1, 2, 3, 4),
807 Flowable.fromArray(5, 6, 7, 8));
808 Flowable<Integer> f = Flowable.combineLatest(sources, new Function<Object[], Integer>() {
809 @Override
810 public Integer apply(Object[] args) {
811 return (Integer) args[0];
812 }});
813 //should get at least 4
814 final CountDownLatch latch = new CountDownLatch(4);
815 f.subscribeOn(Schedulers.computation()).subscribe(new DefaultSubscriber<Integer>() {
816
817 @Override
818 public void onStart() {
819 request(2);
820 }
821
822 @Override
823 public void onComplete() {
824 //ignore
825 }
826
827 @Override
828 public void onError(Throwable e) {
829 throw new RuntimeException(e);
830 }
831
832 @Override
833 public void onNext(Integer t) {
834 latch.countDown();
835 request(Long.MAX_VALUE - 1);
836 }});
837 assertTrue(latch.await(10, TimeUnit.SECONDS));
838 }
839
840 private static final Function<Object[], Integer> THROW_NON_FATAL = new Function<Object[], Integer>() {
841 @Override
842 public Integer apply(Object[] args) {
843 throw new RuntimeException();
844 }
845
846 };
847
848 @Test
849 public void nonFatalExceptionThrownByCombinatorForSingleSourceIsNotReportedByUpstreamOperator() {
850 final AtomicBoolean errorOccurred = new AtomicBoolean(false);
851 TestSubscriber<Integer> ts = TestSubscriber.create(1);
852 Flowable<Integer> source = Flowable.just(1)
853 // if haven't caught exception in combineLatest operator then would incorrectly
854 // be picked up by this call to doOnError
855 .doOnError(new Consumer<Throwable>() {
856 @Override
857 public void accept(Throwable t) {
858 errorOccurred.set(true);
859 }
860 });
861 Flowable
862 .combineLatest(Collections.singletonList(source), THROW_NON_FATAL)
863 .subscribe(ts);
864 assertFalse(errorOccurred.get());
865 }
866
867 @Test
868 public void combineLatestIterable() {
869 Flowable<Integer> source = Flowable.just(1);
870
871 TestSubscriber<Integer> ts = TestSubscriber.create();
872
873 Flowable.combineLatest(Arrays.asList(source, source),
874 new Function<Object[], Integer>() {
875 @Override
876 public Integer apply(Object[] args) {
877 return (Integer)args[0] + (Integer)args[1];
878 }
879 })
880 .subscribe(ts);
881
882 ts.assertValue(2);
883 ts.assertNoErrors();
884 ts.assertComplete();
885 }
886
887 @Test
888 public void combineMany() {
889 int n = Flowable.bufferSize() * 3;
890
891 List<Flowable<Integer>> sources = new ArrayList<>();
892
893 StringBuilder expected = new StringBuilder(n * 2);
894
895 for (int i = 0; i < n; i++) {
896 sources.add(Flowable.just(i));
897 expected.append(i);
898 }
899
900 TestSubscriber<String> ts = TestSubscriber.create();
901
902 Flowable.combineLatest(sources, new Function<Object[], String>() {
903 @Override
904 public String apply(Object[] args) {
905 StringBuilder b = new StringBuilder();
906 for (Object o : args) {
907 b.append(o);
908 }
909 return b.toString();
910 }
911 }).subscribe(ts);
912
913 ts.assertNoErrors();
914 ts.assertValue(expected.toString());
915 ts.assertComplete();
916 }
917
918 @Test
919 public void firstJustError() {
920 TestSubscriber<Integer> ts = TestSubscriber.create();
921
922 Flowable.combineLatestDelayError(
923 Arrays.asList(Flowable.just(1), Flowable.<Integer>error(new TestException())),
924 new Function<Object[], Integer>() {
925 @Override
926 public Integer apply(Object[] args) {
927 return ((Integer)args[0]) + ((Integer)args[1]);
928 }
929 }
930 ).subscribe(ts);
931
932 ts.assertNoValues();
933 ts.assertError(TestException.class);
934 ts.assertNotComplete();
935 }
936
937 @Test
938 public void secondJustError() {
939 TestSubscriber<Integer> ts = TestSubscriber.create();
940
941 Flowable.combineLatestDelayError(
942 Arrays.asList(Flowable.<Integer>error(new TestException()), Flowable.just(1)),
943 new Function<Object[], Integer>() {
944 @Override
945 public Integer apply(Object[] args) {
946 return ((Integer)args[0]) + ((Integer)args[1]);
947 }
948 }
949 ).subscribe(ts);
950
951 ts.assertNoValues();
952 ts.assertError(TestException.class);
953 ts.assertNotComplete();
954 }
955
956 @Test
957 public void oneErrors() {
958 TestSubscriber<Integer> ts = TestSubscriber.create();
959
960 Flowable.combineLatestDelayError(
961 Arrays.asList(Flowable.just(10).concatWith(Flowable.<Integer>error(new TestException())), Flowable.just(1)),
962 new Function<Object[], Integer>() {
963 @Override
964 public Integer apply(Object[] args) {
965 return ((Integer)args[0]) + ((Integer)args[1]);
966 }
967 }
968 ).subscribe(ts);
969
970 ts.assertValues(11);
971 ts.assertError(TestException.class);
972 ts.assertNotComplete();
973 }
974
975 @Test
976 public void twoErrors() {
977 TestSubscriber<Integer> ts = TestSubscriber.create();
978
979 Flowable.combineLatestDelayError(
980 Arrays.asList(Flowable.just(1), Flowable.just(10).concatWith(Flowable.<Integer>error(new TestException()))),
981 new Function<Object[], Integer>() {
982 @Override
983 public Integer apply(Object[] args) {
984 return ((Integer)args[0]) + ((Integer)args[1]);
985 }
986 }
987 ).subscribe(ts);
988
989 ts.assertValues(11);
990 ts.assertError(TestException.class);
991 ts.assertNotComplete();
992 }
993
994 @Test
995 public void bothError() {
996 TestSubscriber<Integer> ts = TestSubscriber.create();
997
998 Flowable.combineLatestDelayError(
999 Arrays.asList(Flowable.just(1).concatWith(Flowable.<Integer>error(new TestException())),
1000 Flowable.just(10).concatWith(Flowable.<Integer>error(new TestException()))),
1001 new Function<Object[], Integer>() {
1002 @Override
1003 public Integer apply(Object[] args) {
1004 return ((Integer)args[0]) + ((Integer)args[1]);
1005 }
1006 }
1007 ).subscribe(ts);
1008
1009 ts.assertValues(11);
1010 ts.assertError(CompositeException.class);
1011 ts.assertNotComplete();
1012 }
1013
1014 @SuppressWarnings({ "unchecked", "rawtypes" })
1015 @Test
1016 public void combineLatestNArguments() throws Exception {
1017 Flowable source = Flowable.just(1);
1018
1019 for (int i = 2; i < 10; i++) {
1020 Class<?>[] types = new Class[i + 1];
1021 Arrays.fill(types, Publisher.class);
1022 types[i] = i == 2 ? BiFunction.class : Class.forName("io.reactivex.rxjava3.functions.Function" + i);
1023
1024 Method m = Flowable.class.getMethod("combineLatest", types);
1025
1026 Object[] params = new Object[i + 1];
1027 Arrays.fill(params, source);
1028 params[i] = ArgsToString.INSTANCE;
1029
1030 StringBuilder b = new StringBuilder();
1031 for (int j = 0; j < i; j++) {
1032 b.append('1');
1033 }
1034
1035 ((Flowable)m.invoke(null, params)).test().assertResult(b.toString());
1036
1037 for (int j = 0; j < params.length; j++) {
1038 Object[] params0 = params.clone();
1039 params0[j] = null;
1040
1041 try {
1042 m.invoke(null, params0);
1043 fail("Should have thrown @ " + m);
1044 } catch (InvocationTargetException ex) {
1045 assertTrue(ex.toString(), ex.getCause() instanceof NullPointerException);
1046
1047 if (j < i) {
1048 assertEquals("source" + (j + 1) + " is null", ex.getCause().getMessage());
1049 } else {
1050 assertEquals("combiner is null", ex.getCause().getMessage());
1051 }
1052 }
1053 }
1054 }
1055 }
1056
1057 @SuppressWarnings("unchecked")
1058 @Test
1059 public void combineLatestArrayNSources() {
1060 for (int i = 1; i < 100; i++) {
1061 Flowable<Integer>[] sources = new Flowable[i];
1062 Arrays.fill(sources, Flowable.just(1));
1063 List<Object> expected = new ArrayList<>(i);
1064 for (int j = 1; j <= i; j++) {
1065 expected.add(1);
1066 }
1067
1068 Flowable.combineLatestArray(sources, new Function<Object[], List<Object>>() {
1069 @Override
1070 public List<Object> apply(Object[] t) throws Exception {
1071 return Arrays.asList(t);
1072 }
1073 })
1074 .test()
1075 .assertResult(expected);
1076
1077 Flowable.combineLatestArrayDelayError(sources, new Function<Object[], List<Object>>() {
1078 @Override
1079 public List<Object> apply(Object[] t) throws Exception {
1080 return Arrays.asList(t);
1081 }
1082 })
1083 .test()
1084 .assertResult(expected);
1085 }
1086 }
1087
1088 @SuppressWarnings("unchecked")
1089 @Test
1090 public void combineLatestArrayOfSources() {
1091
1092 Flowable.combineLatestArray(new Flowable[] {
1093 Flowable.just(1), Flowable.just(2)
1094 }, new Function<Object[], Object>() {
1095 @Override
1096 public Object apply(Object[] a) throws Exception {
1097 return Arrays.toString(a);
1098 }
1099 })
1100 .test()
1101 .assertResult("[1, 2]");
1102 }
1103
1104 @Test
1105 @SuppressWarnings("unchecked")
1106 public void combineLatestDelayErrorArrayOfSources() {
1107
1108 Flowable.combineLatestArrayDelayError(new Flowable[] {
1109 Flowable.just(1), Flowable.just(2)
1110 }, new Function<Object[], Object>() {
1111 @Override
1112 public Object apply(Object[] a) throws Exception {
1113 return Arrays.toString(a);
1114 }
1115 })
1116 .test()
1117 .assertResult("[1, 2]");
1118 }
1119
1120 @Test
1121 @SuppressWarnings("unchecked")
1122 public void combineLatestDelayErrorArrayOfSourcesWithError() {
1123
1124 Flowable.combineLatestArrayDelayError(new Flowable[] {
1125 Flowable.just(1), Flowable.just(2).concatWith(Flowable.<Integer>error(new TestException()))
1126 }, new Function<Object[], Object>() {
1127 @Override
1128 public Object apply(Object[] a) throws Exception {
1129 return Arrays.toString(a);
1130 }
1131 })
1132 .test()
1133 .assertFailure(TestException.class, "[1, 2]");
1134 }
1135
1136 @Test
1137 public void combineLatestDelayErrorIterableOfSources() {
1138
1139 Flowable.combineLatestDelayError(Arrays.asList(
1140 Flowable.just(1), Flowable.just(2)
1141 ), new Function<Object[], Object>() {
1142 @Override
1143 public Object apply(Object[] a) throws Exception {
1144 return Arrays.toString(a);
1145 }
1146 })
1147 .test()
1148 .assertResult("[1, 2]");
1149 }
1150
1151 @Test
1152 public void combineLatestDelayErrorIterableOfSourcesWithError() {
1153
1154 Flowable.combineLatestDelayError(Arrays.asList(
1155 Flowable.just(1), Flowable.just(2).concatWith(Flowable.<Integer>error(new TestException()))
1156 ), new Function<Object[], Object>() {
1157 @Override
1158 public Object apply(Object[] a) throws Exception {
1159 return Arrays.toString(a);
1160 }
1161 })
1162 .test()
1163 .assertFailure(TestException.class, "[1, 2]");
1164 }
1165
1166 @SuppressWarnings("unchecked")
1167 @Test
1168 public void combineLatestArrayEmpty() {
1169 assertSame(Flowable.empty(), Flowable.combineLatestArray(new Flowable[0], Functions.<Object[]>identity(), 16));
1170 }
1171
1172 @SuppressWarnings("unchecked")
1173 @Test
1174 public void combineLatestDelayErrorEmpty() {
1175 assertSame(Flowable.empty(), Flowable.combineLatestArrayDelayError(new Flowable[0], Functions.<Object[]>identity(), 16));
1176 }
1177
1178 @Test
1179 public void error() {
1180 Flowable.combineLatest(Flowable.never(), Flowable.error(new TestException()), new BiFunction<Object, Object, Object>() {
1181 @Override
1182 public Object apply(Object a, Object b) throws Exception {
1183 return a;
1184 }
1185 })
1186 .test()
1187 .assertFailure(TestException.class);
1188 }
1189
1190 @Test
1191 public void disposed() {
1192 TestHelper.checkDisposed(Flowable.combineLatest(Flowable.never(), Flowable.never(), new BiFunction<Object, Object, Object>() {
1193 @Override
1194 public Object apply(Object a, Object b) throws Exception {
1195 return a;
1196 }
1197 }));
1198 }
1199
1200 @Test
1201 public void cancelWhileSubscribing() {
1202 final TestSubscriber<Object> ts = new TestSubscriber<>();
1203
1204 Flowable.combineLatest(
1205 Flowable.just(1)
1206 .doOnNext(new Consumer<Integer>() {
1207 @Override
1208 public void accept(Integer v) throws Exception {
1209 ts.cancel();
1210 }
1211 }),
1212 Flowable.never(),
1213 new BiFunction<Object, Object, Object>() {
1214 @Override
1215 public Object apply(Object a, Object b) throws Exception {
1216 return a;
1217 }
1218 })
1219 .subscribe(ts);
1220 }
1221
1222 @Test
1223 public void onErrorRace() {
1224 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
1225 List<Throwable> errors = TestHelper.trackPluginErrors();
1226 try {
1227 final PublishProcessor<Integer> pp1 = PublishProcessor.create();
1228 final PublishProcessor<Integer> pp2 = PublishProcessor.create();
1229
1230 TestSubscriberEx<Integer> ts = Flowable.combineLatest(pp1, pp2, new BiFunction<Integer, Integer, Integer>() {
1231 @Override
1232 public Integer apply(Integer a, Integer b) throws Exception {
1233 return a;
1234 }
1235 }).to(TestHelper.<Integer>testConsumer());
1236
1237 final TestException ex1 = new TestException();
1238 final TestException ex2 = new TestException();
1239
1240 Runnable r1 = new Runnable() {
1241 @Override
1242 public void run() {
1243 pp1.onError(ex1);
1244 }
1245 };
1246 Runnable r2 = new Runnable() {
1247 @Override
1248 public void run() {
1249 pp2.onError(ex2);
1250 }
1251 };
1252
1253 TestHelper.race(r1, r2);
1254
1255 if (ts.errors().size() != 0) {
1256 if (ts.errors().get(0) instanceof CompositeException) {
1257 ts.assertSubscribed()
1258 .assertNotComplete()
1259 .assertNoValues();
1260
1261 for (Throwable e : TestHelper.errorList(ts)) {
1262 assertTrue(e.toString(), e instanceof TestException);
1263 }
1264
1265 } else {
1266 ts.assertFailure(TestException.class);
1267 }
1268 }
1269
1270 for (Throwable e : errors) {
1271 assertTrue(e.toString(), e.getCause() instanceof TestException);
1272 }
1273 } finally {
1274 RxJavaPlugins.reset();
1275 }
1276 }
1277 }
1278
1279 @Test
1280 public void combineAsync() {
1281 Flowable<Integer> source = Flowable.range(1, 1000).subscribeOn(Schedulers.computation());
1282
1283 Flowable.combineLatest(source, source, new BiFunction<Object, Object, Object>() {
1284 @Override
1285 public Object apply(Object a, Object b) throws Exception {
1286 return a;
1287 }
1288 })
1289 .take(500)
1290 .test()
1291 .awaitDone(5, TimeUnit.SECONDS)
1292 .assertNoErrors()
1293 .assertComplete();
1294 }
1295
1296 @SuppressWarnings("unchecked")
1297 @Test
1298 public void errorDelayed() {
1299 Flowable.combineLatestArrayDelayError(
1300 new Publisher[] { Flowable.error(new TestException()), Flowable.just(1) },
1301 new Function<Object[], Object>() {
1302 @Override
1303 public Object apply(Object[] a) throws Exception {
1304 return a;
1305 }
1306 },
1307 128
1308 )
1309 .test()
1310 .assertFailure(TestException.class);
1311 }
1312
1313 @SuppressWarnings("unchecked")
1314 @Test
1315 public void errorDelayed2() {
1316 Flowable.combineLatestArrayDelayError(
1317 new Publisher[] { Flowable.error(new TestException()).startWithItem(1), Flowable.empty() },
1318 new Function<Object[], Object>() {
1319 @Override
1320 public Object apply(Object[] a) throws Exception {
1321 return a;
1322 }
1323 },
1324 128
1325 )
1326 .test()
1327 .assertFailure(TestException.class);
1328 }
1329
1330 @Test
1331 public void dontSubscribeIfDone() {
1332 List<Throwable> errors = TestHelper.trackPluginErrors();
1333 try {
1334 final int[] count = { 0 };
1335
1336 Flowable.combineLatest(Flowable.empty(),
1337 Flowable.error(new TestException())
1338 .doOnSubscribe(new Consumer<Subscription>() {
1339 @Override
1340 public void accept(Subscription s) throws Exception {
1341 count[0]++;
1342 }
1343 }),
1344 new BiFunction<Object, Object, Object>() {
1345 @Override
1346 public Object apply(Object a, Object b) throws Exception {
1347 return 0;
1348 }
1349 })
1350 .test()
1351 .assertResult();
1352
1353 assertEquals(0, count[0]);
1354
1355 assertTrue(errors.toString(), errors.isEmpty());
1356 } finally {
1357 RxJavaPlugins.reset();
1358 }
1359 }
1360
1361 @Test
1362 public void dontSubscribeIfDone2() {
1363 List<Throwable> errors = TestHelper.trackPluginErrors();
1364 try {
1365 final int[] count = { 0 };
1366
1367 Flowable.combineLatestDelayError(
1368 Arrays.asList(Flowable.empty(),
1369 Flowable.error(new TestException())
1370 .doOnSubscribe(new Consumer<Subscription>() {
1371 @Override
1372 public void accept(Subscription s) throws Exception {
1373 count[0]++;
1374 }
1375 })
1376 ),
1377 new Function<Object[], Object>() {
1378 @Override
1379 public Object apply(Object[] a) throws Exception {
1380 return 0;
1381 }
1382 })
1383 .test()
1384 .assertResult();
1385
1386 assertEquals(0, count[0]);
1387
1388 assertTrue(errors.toString(), errors.isEmpty());
1389 } finally {
1390 RxJavaPlugins.reset();
1391 }
1392 }
1393
1394 @Test
1395 public void combine2Flowable2Errors() throws Exception {
1396 List<Throwable> errors = TestHelper.trackPluginErrors();
1397 try {
1398 TestSubscriber<Object> testSubscriber = TestSubscriber.create();
1399
1400 TestScheduler testScheduler = new TestScheduler();
1401
1402 Flowable<Integer> emptyFlowable = Flowable.timer(10, TimeUnit.MILLISECONDS, testScheduler)
1403 .flatMap(new Function<Long, Publisher<Integer>>() {
1404 @Override
1405 public Publisher<Integer> apply(Long aLong) throws Exception {
1406 return Flowable.error(new Exception());
1407 }
1408 });
1409 Flowable<Object> errorFlowable = Flowable.timer(100, TimeUnit.MILLISECONDS, testScheduler).map(new Function<Long, Object>() {
1410 @Override
1411 public Object apply(Long aLong) throws Exception {
1412 throw new Exception();
1413 }
1414 });
1415
1416 Flowable.combineLatestDelayError(
1417 Arrays.asList(
1418 emptyFlowable
1419 .doOnEach(new Consumer<Notification<Integer>>() {
1420 @Override
1421 public void accept(Notification<Integer> integerNotification) throws Exception {
1422 System.out.println("emptyFlowable: " + integerNotification);
1423 }
1424 })
1425 .doFinally(new Action() {
1426 @Override
1427 public void run() throws Exception {
1428 System.out.println("emptyFlowable: doFinally");
1429 }
1430 }),
1431 errorFlowable
1432 .doOnEach(new Consumer<Notification<Object>>() {
1433 @Override
1434 public void accept(Notification<Object> integerNotification) throws Exception {
1435 System.out.println("errorFlowable: " + integerNotification);
1436 }
1437 })
1438 .doFinally(new Action() {
1439 @Override
1440 public void run() throws Exception {
1441 System.out.println("errorFlowable: doFinally");
1442 }
1443 })),
1444 new Function<Object[], Object>() {
1445 @Override
1446 public Object apply(Object[] objects) throws Exception {
1447 return 0;
1448 }
1449 }
1450 )
1451 .doOnEach(new Consumer<Notification<Object>>() {
1452 @Override
1453 public void accept(Notification<Object> integerNotification) throws Exception {
1454 System.out.println("combineLatestDelayError: " + integerNotification);
1455 }
1456 })
1457 .doFinally(new Action() {
1458 @Override
1459 public void run() throws Exception {
1460 System.out.println("combineLatestDelayError: doFinally");
1461 }
1462 })
1463 .subscribe(testSubscriber);
1464
1465 testScheduler.advanceTimeBy(100, TimeUnit.MILLISECONDS);
1466
1467 testSubscriber.awaitDone(5, TimeUnit.SECONDS);
1468
1469 assertTrue(errors.toString(), errors.isEmpty());
1470 } finally {
1471 RxJavaPlugins.reset();
1472 }
1473 }
1474
1475 @Test
1476 public void eagerDispose() {
1477 final PublishProcessor<Integer> pp1 = PublishProcessor.create();
1478 final PublishProcessor<Integer> pp2 = PublishProcessor.create();
1479
1480 TestSubscriber<Integer> ts = new TestSubscriber<Integer>() {
1481 @Override
1482 public void onNext(Integer t) {
1483 super.onNext(t);
1484 cancel();
1485 if (pp1.hasSubscribers()) {
1486 onError(new IllegalStateException("pp1 not disposed"));
1487 } else
1488 if (pp2.hasSubscribers()) {
1489 onError(new IllegalStateException("pp2 not disposed"));
1490 } else {
1491 onComplete();
1492 }
1493 }
1494 };
1495
1496 Flowable.combineLatest(pp1, pp2, new BiFunction<Integer, Integer, Integer>() {
1497 @Override
1498 public Integer apply(Integer t1, Integer t2) throws Exception {
1499 return t1 + t2;
1500 }
1501 })
1502 .subscribe(ts);
1503
1504 pp1.onNext(1);
1505 pp2.onNext(2);
1506 ts.assertResult(3);
1507 }
1508
1509 @Test
1510 public void fusedNullCheck() {
1511 TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.ASYNC);
1512
1513 Flowable.combineLatest(Flowable.just(1), Flowable.just(2), new BiFunction<Integer, Integer, Integer>() {
1514 @Override
1515 public Integer apply(Integer t1, Integer t2) throws Exception {
1516 return null;
1517 }
1518 })
1519 .subscribe(ts);
1520
1521 ts
1522 .assertFusionMode(QueueFuseable.ASYNC)
1523 .assertFailureAndMessage(NullPointerException.class, "The combiner returned a null value");
1524 }
1525
1526 @Test
1527 public void syncFirstErrorsAfterItemDelayError() {
1528 Flowable.combineLatestDelayError(Arrays.asList(
1529 Flowable.just(21).concatWith(Flowable.<Integer>error(new TestException())),
1530 Flowable.just(21).delay(100, TimeUnit.MILLISECONDS)
1531 ),
1532 new Function<Object[], Object>() {
1533 @Override
1534 public Object apply(Object[] a) throws Exception {
1535 return (Integer)a[0] + (Integer)a[1];
1536 }
1537 }
1538 )
1539 .test()
1540 .awaitDone(5, TimeUnit.SECONDS)
1541 .assertFailure(TestException.class, 42);
1542 }
1543
1544 @Test
1545 public void publishersInIterable() {
1546 Publisher<Integer> source = new Publisher<Integer>() {
1547 @Override
1548 public void subscribe(Subscriber<? super Integer> subscriber) {
1549 Flowable.just(1).subscribe(subscriber);
1550 }
1551 };
1552
1553 Flowable.combineLatest(Arrays.asList(source, source), new Function<Object[], Integer>() {
1554 @Override
1555 public Integer apply(Object[] t) throws Throwable {
1556 return 2;
1557 }
1558 })
1559 .test()
1560 .assertResult(2);
1561 }
1562
1563 @Test
1564 public void FlowableSourcesInIterable() {
1565 Flowable<Integer> source = new Flowable<Integer>() {
1566 @Override
1567 public void subscribeActual(Subscriber<? super Integer> s) {
1568 Flowable.just(1).subscribe(s);
1569 }
1570 };
1571
1572 Flowable.combineLatest(Arrays.asList(source, source), new Function<Object[], Integer>() {
1573 @Override
1574 public Integer apply(Object[] t) throws Throwable {
1575 return 2;
1576 }
1577 })
1578 .test()
1579 .assertResult(2);
1580 }
1581
1582 @Test
1583 public void onCompleteDisposeRace() {
1584 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
1585
1586 TestSubscriber<Integer> ts = new TestSubscriber<>();
1587 PublishProcessor<Integer> pp = PublishProcessor.create();
1588
1589 Flowable.combineLatest(pp, Flowable.never(), (a, b) -> a)
1590 .subscribe(ts);
1591
1592 TestHelper.race(() -> pp.onComplete(), () -> ts.cancel());
1593 }
1594 }
1595
1596 @Test
1597 public void onErrorDisposeDelayErrorRace() throws Throwable {
1598 TestHelper.withErrorTracking(errors -> {
1599 TestException ex = new TestException();
1600
1601 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
1602
1603 TestSubscriberEx<Object[]> ts = new TestSubscriberEx<>();
1604 AtomicReference<Subscriber<? super Object>> ref = new AtomicReference<>();
1605 Flowable<Object> f = new Flowable<Object>() {
1606 @Override
1607 public void subscribeActual(Subscriber<? super Object> s) {
1608 ref.set(s);
1609 }
1610 };
1611
1612 Flowable.combineLatestDelayError(Arrays.asList(f, Flowable.never()), (a) -> a)
1613 .subscribe(ts);
1614
1615 ref.get().onSubscribe(new BooleanSubscription());
1616
1617 TestHelper.race(() -> ref.get().onError(ex), () -> ts.cancel());
1618
1619 if (ts.errors().isEmpty()) {
1620 TestHelper.assertUndeliverable(errors, 0, TestException.class);
1621 }
1622 }
1623 });
1624 }
1625
1626 @Test
1627 public void doneButNotEmpty() {
1628 PublishProcessor<Integer> pp1 = PublishProcessor.create();
1629 PublishProcessor<Integer> pp2 = PublishProcessor.create();
1630
1631 TestSubscriber<Integer> ts = Flowable.combineLatest(pp1, pp2, (a, b) -> a + b)
1632 .doOnNext(v -> {
1633 if (v == 2) {
1634 pp2.onNext(3);
1635 pp2.onComplete();
1636 pp1.onComplete();
1637 }
1638 })
1639 .test();
1640
1641 pp1.onNext(1);
1642 pp2.onNext(1);
1643
1644 ts.assertResult(2, 4);
1645 }
1646
1647 @Test
1648 public void iterableNullPublisher() {
1649 Flowable.combineLatest(Arrays.asList(Flowable.never(), null), (a) -> a)
1650 .test()
1651 .assertFailure(NullPointerException.class);
1652 }
1653
1654 @Test
1655 public void badRequest() {
1656 TestHelper.assertBadRequestReported(Flowable.combineLatest(Flowable.never(), Flowable.never(), (a, b) -> a));
1657 }
1658
1659 @Test
1660 public void syncFusionRejected() {
1661 TestSubscriberEx<Object> ts = new TestSubscriberEx<>();
1662 ts.setInitialFusionMode(QueueFuseable.SYNC);
1663
1664 Flowable.combineLatest(Flowable.never(), Flowable.never(), (a, b) -> a)
1665 .subscribe(ts);
1666
1667 ts.assertFuseable()
1668 .assertFusionMode(QueueFuseable.NONE);
1669 }
1670
1671 @Test
1672 public void bounderyFusionRejected() {
1673 TestSubscriberEx<Object> ts = new TestSubscriberEx<>();
1674 ts.setInitialFusionMode(QueueFuseable.ANY | QueueFuseable.BOUNDARY);
1675
1676 Flowable.combineLatest(Flowable.never(), Flowable.never(), (a, b) -> a)
1677 .subscribe(ts);
1678
1679 ts.assertFuseable()
1680 .assertFusionMode(QueueFuseable.NONE);
1681 }
1682
1683 @Test
1684 public void fusedNormal() {
1685 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
1686 ts.setInitialFusionMode(QueueFuseable.ANY);
1687
1688 Flowable.combineLatest(Flowable.just(1), Flowable.just(2), (a, b) -> a + b)
1689 .subscribeWith(ts)
1690 .assertFuseable()
1691 .assertFusionMode(QueueFuseable.ASYNC)
1692 .assertResult(3);
1693 }
1694
1695 @Test
1696 public void fusedToParallel() {
1697 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
1698 ts.setInitialFusionMode(QueueFuseable.ANY);
1699
1700 Flowable.combineLatest(Flowable.just(1), Flowable.just(2), (a, b) -> a + b)
1701 .parallel()
1702 .sequential()
1703 .subscribeWith(ts)
1704 .assertResult(3);
1705 }
1706
1707 @Test
1708 public void fusedToParallel2() {
1709 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
1710 ts.setInitialFusionMode(QueueFuseable.ANY);
1711
1712 Flowable.combineLatest(Flowable.just(1), Flowable.just(2), (a, b) -> a + b)
1713 .compose(TestHelper.flowableStripBoundary())
1714 .parallel()
1715 .sequential()
1716 .subscribeWith(ts)
1717 .assertResult(3);
1718 }
1719
1720 @Test
1721 public void fusedError() {
1722 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
1723 ts.setInitialFusionMode(QueueFuseable.ANY);
1724
1725 Flowable.combineLatest(Flowable.just(1), Flowable.<Integer>error(new TestException()), (a, b) -> a + b)
1726 .subscribeWith(ts)
1727 .assertFuseable()
1728 .assertFusionMode(QueueFuseable.ASYNC)
1729 .assertFailure(TestException.class);
1730 }
1731
1732 @Test
1733 public void nonFusedMoreWorkBeforeTermination() {
1734 PublishProcessor<Integer> pp = PublishProcessor.create();
1735
1736 TestSubscriber<Integer> ts = Flowable.combineLatest(pp, Flowable.just(1), (a, b) -> a + b)
1737 .doOnNext(v -> {
1738 if (v == 1) {
1739 pp.onNext(2);
1740 pp.onComplete();
1741 }
1742 })
1743 .test();
1744
1745 pp.onNext(0);
1746
1747 ts.assertResult(1, 3);
1748 }
1749
1750 @Test
1751 public void nonFusedDelayErrorMoreWorkBeforeTermination() {
1752 PublishProcessor<Integer> pp = PublishProcessor.create();
1753
1754 TestSubscriber<List<Object>> ts = Flowable.combineLatestDelayError(Arrays.asList(pp, Flowable.just(1)), a -> Arrays.asList(a))
1755 .doOnNext(v -> {
1756 if (((Integer)v.get(0)) == 0) {
1757 pp.onNext(2);
1758 pp.onComplete();
1759 }
1760 })
1761 .test();
1762
1763 pp.onNext(0);
1764
1765 ts.assertResult(Arrays.asList(0, 1), Arrays.asList(2, 1));
1766 }
1767
1768 @Test
1769 public void fusedCombinerCrashError() {
1770 TestSubscriberEx<Object> ts = new TestSubscriberEx<>();
1771 ts.setInitialFusionMode(QueueFuseable.ANY);
1772
1773 Flowable.combineLatest(Flowable.just(1), Flowable.just(1), (a, b) -> { throw new TestException(); })
1774 .subscribeWith(ts)
1775 .assertFuseable()
1776 .assertFusionMode(QueueFuseable.ASYNC)
1777 .assertFailure(TestException.class);
1778 }
1779
1780 @Test
1781 public void fusedCombinerCrashError2() {
1782 Flowable.combineLatest(Flowable.just(1), Flowable.just(1), (a, b) -> { throw new TestException(); })
1783 .compose(TestHelper.flowableStripBoundary())
1784 .rebatchRequests(10)
1785 .test()
1786 .assertFailure(TestException.class);
1787 }
1788 }