forked from ReactiveX/RxJava
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFlowableCombineLatestTest.java
1589 lines (1312 loc) · 54.8 KB
/
FlowableCombineLatestTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright (c) 2016-present, RxJava Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
* the License for the specific language governing permissions and limitations under the License.
*/
package io.reactivex.internal.operators.flowable;
import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import java.lang.reflect.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import org.junit.*;
import org.mockito.*;
import org.reactivestreams.*;
import io.reactivex.*;
import io.reactivex.exceptions.*;
import io.reactivex.functions.*;
import io.reactivex.internal.functions.Functions;
import io.reactivex.internal.fuseable.QueueFuseable;
import io.reactivex.internal.operators.flowable.FlowableZipTest.ArgsToString;
import io.reactivex.plugins.RxJavaPlugins;
import io.reactivex.processors.PublishProcessor;
import io.reactivex.schedulers.*;
import io.reactivex.subscribers.*;
import io.reactivex.testsupport.*;
public class FlowableCombineLatestTest {
@Test
public void combineLatestWithFunctionThatThrowsAnException() {
Subscriber<String> w = TestHelper.mockSubscriber();
PublishProcessor<String> w1 = PublishProcessor.create();
PublishProcessor<String> w2 = PublishProcessor.create();
Flowable<String> combined = Flowable.combineLatest(w1, w2, new BiFunction<String, String, String>() {
@Override
public String apply(String v1, String v2) {
throw new RuntimeException("I don't work.");
}
});
combined.subscribe(w);
w1.onNext("first value of w1");
w2.onNext("first value of w2");
verify(w, never()).onNext(anyString());
verify(w, never()).onComplete();
verify(w, times(1)).onError(Mockito.<RuntimeException> any());
}
@Test
public void combineLatestDifferentLengthFlowableSequences1() {
Subscriber<String> w = TestHelper.mockSubscriber();
PublishProcessor<String> w1 = PublishProcessor.create();
PublishProcessor<String> w2 = PublishProcessor.create();
PublishProcessor<String> w3 = PublishProcessor.create();
Flowable<String> combineLatestW = Flowable.combineLatest(w1, w2, w3,
getConcat3StringsCombineLatestFunction());
combineLatestW.subscribe(w);
/* simulate sending data */
// once for w1
w1.onNext("1a");
w2.onNext("2a");
w3.onNext("3a");
w1.onComplete();
// twice for w2
w2.onNext("2b");
w2.onComplete();
// 4 times for w3
w3.onNext("3b");
w3.onNext("3c");
w3.onNext("3d");
w3.onComplete();
/* we should have been called 4 times on the Observer */
InOrder inOrder = inOrder(w);
inOrder.verify(w).onNext("1a2a3a");
inOrder.verify(w).onNext("1a2b3a");
inOrder.verify(w).onNext("1a2b3b");
inOrder.verify(w).onNext("1a2b3c");
inOrder.verify(w).onNext("1a2b3d");
inOrder.verify(w, never()).onNext(anyString());
inOrder.verify(w, times(1)).onComplete();
}
@Test
public void combineLatestDifferentLengthFlowableSequences2() {
Subscriber<String> w = TestHelper.mockSubscriber();
PublishProcessor<String> w1 = PublishProcessor.create();
PublishProcessor<String> w2 = PublishProcessor.create();
PublishProcessor<String> w3 = PublishProcessor.create();
Flowable<String> combineLatestW = Flowable.combineLatest(w1, w2, w3, getConcat3StringsCombineLatestFunction());
combineLatestW.subscribe(w);
/* simulate sending data */
// 4 times for w1
w1.onNext("1a");
w1.onNext("1b");
w1.onNext("1c");
w1.onNext("1d");
w1.onComplete();
// twice for w2
w2.onNext("2a");
w2.onNext("2b");
w2.onComplete();
// 1 times for w3
w3.onNext("3a");
w3.onComplete();
/* 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 */
InOrder inOrder = inOrder(w);
inOrder.verify(w, times(1)).onNext("1d2b3a");
inOrder.verify(w, never()).onNext(anyString());
inOrder.verify(w, times(1)).onComplete();
}
@Test
public void combineLatestWithInterleavingSequences() {
Subscriber<String> w = TestHelper.mockSubscriber();
PublishProcessor<String> w1 = PublishProcessor.create();
PublishProcessor<String> w2 = PublishProcessor.create();
PublishProcessor<String> w3 = PublishProcessor.create();
Flowable<String> combineLatestW = Flowable.combineLatest(w1, w2, w3, getConcat3StringsCombineLatestFunction());
combineLatestW.subscribe(w);
/* simulate sending data */
w1.onNext("1a");
w2.onNext("2a");
w2.onNext("2b");
w3.onNext("3a");
w1.onNext("1b");
w2.onNext("2c");
w2.onNext("2d");
w3.onNext("3b");
w1.onComplete();
w2.onComplete();
w3.onComplete();
/* we should have been called 5 times on the Observer */
InOrder inOrder = inOrder(w);
inOrder.verify(w).onNext("1a2b3a");
inOrder.verify(w).onNext("1b2b3a");
inOrder.verify(w).onNext("1b2c3a");
inOrder.verify(w).onNext("1b2d3a");
inOrder.verify(w).onNext("1b2d3b");
inOrder.verify(w, never()).onNext(anyString());
inOrder.verify(w, times(1)).onComplete();
}
@Test
public void combineLatest2Types() {
BiFunction<String, Integer, String> combineLatestFunction = getConcatStringIntegerCombineLatestFunction();
/* define an Observer to receive aggregated events */
Subscriber<String> subscriber = TestHelper.mockSubscriber();
Flowable<String> w = Flowable.combineLatest(Flowable.just("one", "two"), Flowable.just(2, 3, 4), combineLatestFunction);
w.subscribe(subscriber);
verify(subscriber, never()).onError(any(Throwable.class));
verify(subscriber, times(1)).onComplete();
verify(subscriber, times(1)).onNext("two2");
verify(subscriber, times(1)).onNext("two3");
verify(subscriber, times(1)).onNext("two4");
}
@Test
public void combineLatest3TypesA() {
Function3<String, Integer, int[], String> combineLatestFunction = getConcatStringIntegerIntArrayCombineLatestFunction();
/* define an Observer to receive aggregated events */
Subscriber<String> subscriber = TestHelper.mockSubscriber();
Flowable<String> w = Flowable.combineLatest(Flowable.just("one", "two"), Flowable.just(2), Flowable.just(new int[] { 4, 5, 6 }), combineLatestFunction);
w.subscribe(subscriber);
verify(subscriber, never()).onError(any(Throwable.class));
verify(subscriber, times(1)).onComplete();
verify(subscriber, times(1)).onNext("two2[4, 5, 6]");
}
@Test
public void combineLatest3TypesB() {
Function3<String, Integer, int[], String> combineLatestFunction = getConcatStringIntegerIntArrayCombineLatestFunction();
/* define an Observer to receive aggregated events */
Subscriber<String> subscriber = TestHelper.mockSubscriber();
Flowable<String> w = Flowable.combineLatest(Flowable.just("one"), Flowable.just(2), Flowable.just(new int[] { 4, 5, 6 }, new int[] { 7, 8 }), combineLatestFunction);
w.subscribe(subscriber);
verify(subscriber, never()).onError(any(Throwable.class));
verify(subscriber, times(1)).onComplete();
verify(subscriber, times(1)).onNext("one2[4, 5, 6]");
verify(subscriber, times(1)).onNext("one2[7, 8]");
}
private Function3<String, String, String, String> getConcat3StringsCombineLatestFunction() {
Function3<String, String, String, String> combineLatestFunction = new Function3<String, String, String, String>() {
@Override
public String apply(String a1, String a2, String a3) {
if (a1 == null) {
a1 = "";
}
if (a2 == null) {
a2 = "";
}
if (a3 == null) {
a3 = "";
}
return a1 + a2 + a3;
}
};
return combineLatestFunction;
}
private BiFunction<String, Integer, String> getConcatStringIntegerCombineLatestFunction() {
BiFunction<String, Integer, String> combineLatestFunction = new BiFunction<String, Integer, String>() {
@Override
public String apply(String s, Integer i) {
return getStringValue(s) + getStringValue(i);
}
};
return combineLatestFunction;
}
private Function3<String, Integer, int[], String> getConcatStringIntegerIntArrayCombineLatestFunction() {
return new Function3<String, Integer, int[], String>() {
@Override
public String apply(String s, Integer i, int[] iArray) {
return getStringValue(s) + getStringValue(i) + getStringValue(iArray);
}
};
}
private static String getStringValue(Object o) {
if (o == null) {
return "";
} else {
if (o instanceof int[]) {
return Arrays.toString((int[]) o);
} else {
return String.valueOf(o);
}
}
}
BiFunction<Integer, Integer, Integer> or = new BiFunction<Integer, Integer, Integer>() {
@Override
public Integer apply(Integer t1, Integer t2) {
return t1 | t2;
}
};
@Test
public void combineSimple() {
PublishProcessor<Integer> a = PublishProcessor.create();
PublishProcessor<Integer> b = PublishProcessor.create();
Flowable<Integer> source = Flowable.combineLatest(a, b, or);
Subscriber<Object> subscriber = TestHelper.mockSubscriber();
InOrder inOrder = inOrder(subscriber);
source.subscribe(subscriber);
a.onNext(1);
inOrder.verify(subscriber, never()).onNext(any());
a.onNext(2);
inOrder.verify(subscriber, never()).onNext(any());
b.onNext(0x10);
inOrder.verify(subscriber, times(1)).onNext(0x12);
b.onNext(0x20);
inOrder.verify(subscriber, times(1)).onNext(0x22);
b.onComplete();
inOrder.verify(subscriber, never()).onComplete();
a.onComplete();
inOrder.verify(subscriber, times(1)).onComplete();
a.onNext(3);
b.onNext(0x30);
a.onComplete();
b.onComplete();
inOrder.verifyNoMoreInteractions();
verify(subscriber, never()).onError(any(Throwable.class));
}
@Test
public void combineMultipleObservers() {
PublishProcessor<Integer> a = PublishProcessor.create();
PublishProcessor<Integer> b = PublishProcessor.create();
Flowable<Integer> source = Flowable.combineLatest(a, b, or);
Subscriber<Object> subscriber1 = TestHelper.mockSubscriber();
Subscriber<Object> subscriber2 = TestHelper.mockSubscriber();
InOrder inOrder1 = inOrder(subscriber1);
InOrder inOrder2 = inOrder(subscriber2);
source.subscribe(subscriber1);
source.subscribe(subscriber2);
a.onNext(1);
inOrder1.verify(subscriber1, never()).onNext(any());
inOrder2.verify(subscriber2, never()).onNext(any());
a.onNext(2);
inOrder1.verify(subscriber1, never()).onNext(any());
inOrder2.verify(subscriber2, never()).onNext(any());
b.onNext(0x10);
inOrder1.verify(subscriber1, times(1)).onNext(0x12);
inOrder2.verify(subscriber2, times(1)).onNext(0x12);
b.onNext(0x20);
inOrder1.verify(subscriber1, times(1)).onNext(0x22);
inOrder2.verify(subscriber2, times(1)).onNext(0x22);
b.onComplete();
inOrder1.verify(subscriber1, never()).onComplete();
inOrder2.verify(subscriber2, never()).onComplete();
a.onComplete();
inOrder1.verify(subscriber1, times(1)).onComplete();
inOrder2.verify(subscriber2, times(1)).onComplete();
a.onNext(3);
b.onNext(0x30);
a.onComplete();
b.onComplete();
inOrder1.verifyNoMoreInteractions();
inOrder2.verifyNoMoreInteractions();
verify(subscriber1, never()).onError(any(Throwable.class));
verify(subscriber2, never()).onError(any(Throwable.class));
}
@Test
public void firstNeverProduces() {
PublishProcessor<Integer> a = PublishProcessor.create();
PublishProcessor<Integer> b = PublishProcessor.create();
Flowable<Integer> source = Flowable.combineLatest(a, b, or);
Subscriber<Object> subscriber = TestHelper.mockSubscriber();
InOrder inOrder = inOrder(subscriber);
source.subscribe(subscriber);
b.onNext(0x10);
b.onNext(0x20);
a.onComplete();
inOrder.verify(subscriber, times(1)).onComplete();
verify(subscriber, never()).onNext(any());
verify(subscriber, never()).onError(any(Throwable.class));
}
@Test
public void secondNeverProduces() {
PublishProcessor<Integer> a = PublishProcessor.create();
PublishProcessor<Integer> b = PublishProcessor.create();
Flowable<Integer> source = Flowable.combineLatest(a, b, or);
Subscriber<Object> subscriber = TestHelper.mockSubscriber();
InOrder inOrder = inOrder(subscriber);
source.subscribe(subscriber);
a.onNext(0x1);
a.onNext(0x2);
b.onComplete();
a.onComplete();
inOrder.verify(subscriber, times(1)).onComplete();
verify(subscriber, never()).onNext(any());
verify(subscriber, never()).onError(any(Throwable.class));
}
@Test
public void oneToNSources() {
int n = 30;
Function<Object[], List<Object>> func = new Function<Object[], List<Object>>() {
@Override
public List<Object> apply(Object[] args) {
return Arrays.asList(args);
}
};
for (int i = 1; i <= n; i++) {
System.out.println("test1ToNSources: " + i + " sources");
List<Flowable<Integer>> sources = new ArrayList<Flowable<Integer>>();
List<Object> values = new ArrayList<Object>();
for (int j = 0; j < i; j++) {
sources.add(Flowable.just(j));
values.add(j);
}
Flowable<List<Object>> result = Flowable.combineLatest(sources, func);
Subscriber<List<Object>> subscriber = TestHelper.mockSubscriber();
result.subscribe(subscriber);
verify(subscriber).onNext(values);
verify(subscriber).onComplete();
verify(subscriber, never()).onError(any(Throwable.class));
}
}
@Test(timeout = 5000)
public void oneToNSourcesScheduled() throws InterruptedException {
int n = 10;
Function<Object[], List<Object>> func = new Function<Object[], List<Object>>() {
@Override
public List<Object> apply(Object[] args) {
return Arrays.asList(args);
}
};
for (int i = 1; i <= n; i++) {
System.out.println("test1ToNSourcesScheduled: " + i + " sources");
List<Flowable<Integer>> sources = new ArrayList<Flowable<Integer>>();
List<Object> values = new ArrayList<Object>();
for (int j = 0; j < i; j++) {
sources.add(Flowable.just(j).subscribeOn(Schedulers.io()));
values.add(j);
}
Flowable<List<Object>> result = Flowable.combineLatest(sources, func);
final Subscriber<List<Object>> subscriber = TestHelper.mockSubscriber();
final CountDownLatch cdl = new CountDownLatch(1);
Subscriber<List<Object>> s = new DefaultSubscriber<List<Object>>() {
@Override
public void onNext(List<Object> t) {
subscriber.onNext(t);
}
@Override
public void onError(Throwable e) {
subscriber.onError(e);
cdl.countDown();
}
@Override
public void onComplete() {
subscriber.onComplete();
cdl.countDown();
}
};
result.subscribe(s);
cdl.await();
verify(subscriber).onNext(values);
verify(subscriber).onComplete();
verify(subscriber, never()).onError(any(Throwable.class));
}
}
@Test
public void twoSourcesOverload() {
Flowable<Integer> s1 = Flowable.just(1);
Flowable<Integer> s2 = Flowable.just(2);
Flowable<List<Integer>> result = Flowable.combineLatest(s1, s2,
new BiFunction<Integer, Integer, List<Integer>>() {
@Override
public List<Integer> apply(Integer t1, Integer t2) {
return Arrays.asList(t1, t2);
}
});
Subscriber<Object> subscriber = TestHelper.mockSubscriber();
result.subscribe(subscriber);
verify(subscriber).onNext(Arrays.asList(1, 2));
verify(subscriber).onComplete();
verify(subscriber, never()).onError(any(Throwable.class));
}
@Test
public void threeSourcesOverload() {
Flowable<Integer> s1 = Flowable.just(1);
Flowable<Integer> s2 = Flowable.just(2);
Flowable<Integer> s3 = Flowable.just(3);
Flowable<List<Integer>> result = Flowable.combineLatest(s1, s2, s3,
new Function3<Integer, Integer, Integer, List<Integer>>() {
@Override
public List<Integer> apply(Integer t1, Integer t2, Integer t3) {
return Arrays.asList(t1, t2, t3);
}
});
Subscriber<Object> subscriber = TestHelper.mockSubscriber();
result.subscribe(subscriber);
verify(subscriber).onNext(Arrays.asList(1, 2, 3));
verify(subscriber).onComplete();
verify(subscriber, never()).onError(any(Throwable.class));
}
@Test
public void fourSourcesOverload() {
Flowable<Integer> s1 = Flowable.just(1);
Flowable<Integer> s2 = Flowable.just(2);
Flowable<Integer> s3 = Flowable.just(3);
Flowable<Integer> s4 = Flowable.just(4);
Flowable<List<Integer>> result = Flowable.combineLatest(s1, s2, s3, s4,
new Function4<Integer, Integer, Integer, Integer, List<Integer>>() {
@Override
public List<Integer> apply(Integer t1, Integer t2, Integer t3, Integer t4) {
return Arrays.asList(t1, t2, t3, t4);
}
});
Subscriber<Object> subscriber = TestHelper.mockSubscriber();
result.subscribe(subscriber);
verify(subscriber).onNext(Arrays.asList(1, 2, 3, 4));
verify(subscriber).onComplete();
verify(subscriber, never()).onError(any(Throwable.class));
}
@Test
public void fiveSourcesOverload() {
Flowable<Integer> s1 = Flowable.just(1);
Flowable<Integer> s2 = Flowable.just(2);
Flowable<Integer> s3 = Flowable.just(3);
Flowable<Integer> s4 = Flowable.just(4);
Flowable<Integer> s5 = Flowable.just(5);
Flowable<List<Integer>> result = Flowable.combineLatest(s1, s2, s3, s4, s5,
new Function5<Integer, Integer, Integer, Integer, Integer, List<Integer>>() {
@Override
public List<Integer> apply(Integer t1, Integer t2, Integer t3, Integer t4, Integer t5) {
return Arrays.asList(t1, t2, t3, t4, t5);
}
});
Subscriber<Object> subscriber = TestHelper.mockSubscriber();
result.subscribe(subscriber);
verify(subscriber).onNext(Arrays.asList(1, 2, 3, 4, 5));
verify(subscriber).onComplete();
verify(subscriber, never()).onError(any(Throwable.class));
}
@Test
public void sixSourcesOverload() {
Flowable<Integer> s1 = Flowable.just(1);
Flowable<Integer> s2 = Flowable.just(2);
Flowable<Integer> s3 = Flowable.just(3);
Flowable<Integer> s4 = Flowable.just(4);
Flowable<Integer> s5 = Flowable.just(5);
Flowable<Integer> s6 = Flowable.just(6);
Flowable<List<Integer>> result = Flowable.combineLatest(s1, s2, s3, s4, s5, s6,
new Function6<Integer, Integer, Integer, Integer, Integer, Integer, List<Integer>>() {
@Override
public List<Integer> apply(Integer t1, Integer t2, Integer t3, Integer t4, Integer t5, Integer t6) {
return Arrays.asList(t1, t2, t3, t4, t5, t6);
}
});
Subscriber<Object> subscriber = TestHelper.mockSubscriber();
result.subscribe(subscriber);
verify(subscriber).onNext(Arrays.asList(1, 2, 3, 4, 5, 6));
verify(subscriber).onComplete();
verify(subscriber, never()).onError(any(Throwable.class));
}
@Test
public void sevenSourcesOverload() {
Flowable<Integer> s1 = Flowable.just(1);
Flowable<Integer> s2 = Flowable.just(2);
Flowable<Integer> s3 = Flowable.just(3);
Flowable<Integer> s4 = Flowable.just(4);
Flowable<Integer> s5 = Flowable.just(5);
Flowable<Integer> s6 = Flowable.just(6);
Flowable<Integer> s7 = Flowable.just(7);
Flowable<List<Integer>> result = Flowable.combineLatest(s1, s2, s3, s4, s5, s6, s7,
new Function7<Integer, Integer, Integer, Integer, Integer, Integer, Integer, List<Integer>>() {
@Override
public List<Integer> apply(Integer t1, Integer t2, Integer t3, Integer t4, Integer t5, Integer t6, Integer t7) {
return Arrays.asList(t1, t2, t3, t4, t5, t6, t7);
}
});
Subscriber<Object> subscriber = TestHelper.mockSubscriber();
result.subscribe(subscriber);
verify(subscriber).onNext(Arrays.asList(1, 2, 3, 4, 5, 6, 7));
verify(subscriber).onComplete();
verify(subscriber, never()).onError(any(Throwable.class));
}
@Test
public void eightSourcesOverload() {
Flowable<Integer> s1 = Flowable.just(1);
Flowable<Integer> s2 = Flowable.just(2);
Flowable<Integer> s3 = Flowable.just(3);
Flowable<Integer> s4 = Flowable.just(4);
Flowable<Integer> s5 = Flowable.just(5);
Flowable<Integer> s6 = Flowable.just(6);
Flowable<Integer> s7 = Flowable.just(7);
Flowable<Integer> s8 = Flowable.just(8);
Flowable<List<Integer>> result = Flowable.combineLatest(s1, s2, s3, s4, s5, s6, s7, s8,
new Function8<Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, List<Integer>>() {
@Override
public List<Integer> apply(Integer t1, Integer t2, Integer t3, Integer t4, Integer t5, Integer t6, Integer t7, Integer t8) {
return Arrays.asList(t1, t2, t3, t4, t5, t6, t7, t8);
}
});
Subscriber<Object> subscriber = TestHelper.mockSubscriber();
result.subscribe(subscriber);
verify(subscriber).onNext(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8));
verify(subscriber).onComplete();
verify(subscriber, never()).onError(any(Throwable.class));
}
@Test
public void nineSourcesOverload() {
Flowable<Integer> s1 = Flowable.just(1);
Flowable<Integer> s2 = Flowable.just(2);
Flowable<Integer> s3 = Flowable.just(3);
Flowable<Integer> s4 = Flowable.just(4);
Flowable<Integer> s5 = Flowable.just(5);
Flowable<Integer> s6 = Flowable.just(6);
Flowable<Integer> s7 = Flowable.just(7);
Flowable<Integer> s8 = Flowable.just(8);
Flowable<Integer> s9 = Flowable.just(9);
Flowable<List<Integer>> result = Flowable.combineLatest(s1, s2, s3, s4, s5, s6, s7, s8, s9,
new Function9<Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, List<Integer>>() {
@Override
public List<Integer> apply(Integer t1, Integer t2, Integer t3, Integer t4, Integer t5, Integer t6, Integer t7, Integer t8, Integer t9) {
return Arrays.asList(t1, t2, t3, t4, t5, t6, t7, t8, t9);
}
});
Subscriber<Object> subscriber = TestHelper.mockSubscriber();
result.subscribe(subscriber);
verify(subscriber).onNext(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9));
verify(subscriber).onComplete();
verify(subscriber, never()).onError(any(Throwable.class));
}
@Test
public void zeroSources() {
Flowable<Object> result = Flowable.combineLatest(
Collections.<Flowable<Object>> emptyList(), new Function<Object[], Object>() {
@Override
public Object apply(Object[] args) {
return args;
}
});
Subscriber<Object> subscriber = TestHelper.mockSubscriber();
result.subscribe(subscriber);
verify(subscriber).onComplete();
verify(subscriber, never()).onNext(any());
verify(subscriber, never()).onError(any(Throwable.class));
}
@Test
public void backpressureLoop() {
for (int i = 0; i < 5000; i++) {
backpressure();
}
}
@Test//(timeout = 2000)
public void backpressure() {
BiFunction<String, Integer, String> combineLatestFunction = getConcatStringIntegerCombineLatestFunction();
int num = Flowable.bufferSize() * 4;
TestSubscriber<String> ts = new TestSubscriber<String>();
Flowable.combineLatest(
Flowable.just("one", "two"),
Flowable.range(2, num),
combineLatestFunction
)
.observeOn(Schedulers.computation())
.subscribe(ts);
ts.awaitDone(5, TimeUnit.SECONDS);
ts.assertNoErrors();
List<String> events = ts.values();
assertEquals("two2", events.get(0));
assertEquals("two3", events.get(1));
assertEquals("two4", events.get(2));
assertEquals(num, events.size());
}
@Test
public void withCombineLatestIssue1717() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
final AtomicInteger count = new AtomicInteger();
final int SIZE = 2000;
Flowable<Long> timer = Flowable.interval(0, 1, TimeUnit.MILLISECONDS)
.observeOn(Schedulers.newThread())
.doOnEach(new Consumer<Notification<Long>>() {
@Override
public void accept(Notification<Long> n) {
// System.out.println(n);
if (count.incrementAndGet() >= SIZE) {
latch.countDown();
}
}
}).take(SIZE);
TestSubscriber<Long> ts = new TestSubscriber<Long>();
Flowable.combineLatest(timer, Flowable.<Integer> never(), new BiFunction<Long, Integer, Long>() {
@Override
public Long apply(Long t1, Integer t2) {
return t1;
}
}).subscribe(ts);
if (!latch.await(SIZE + 2000, TimeUnit.MILLISECONDS)) {
fail("timed out");
}
assertEquals(SIZE, count.get());
}
@Test(timeout = 10000)
public void combineLatestRequestOverflow() throws InterruptedException {
@SuppressWarnings("unchecked")
List<Flowable<Integer>> sources = Arrays.asList(Flowable.fromArray(1, 2, 3, 4),
Flowable.fromArray(5, 6, 7, 8));
Flowable<Integer> f = Flowable.combineLatest(sources, new Function<Object[], Integer>() {
@Override
public Integer apply(Object[] args) {
return (Integer) args[0];
}});
//should get at least 4
final CountDownLatch latch = new CountDownLatch(4);
f.subscribeOn(Schedulers.computation()).subscribe(new DefaultSubscriber<Integer>() {
@Override
public void onStart() {
request(2);
}
@Override
public void onComplete() {
//ignore
}
@Override
public void onError(Throwable e) {
throw new RuntimeException(e);
}
@Override
public void onNext(Integer t) {
latch.countDown();
request(Long.MAX_VALUE - 1);
}});
assertTrue(latch.await(10, TimeUnit.SECONDS));
}
private static final Function<Object[], Integer> THROW_NON_FATAL = new Function<Object[], Integer>() {
@Override
public Integer apply(Object[] args) {
throw new RuntimeException();
}
};
@Test
public void nonFatalExceptionThrownByCombinatorForSingleSourceIsNotReportedByUpstreamOperator() {
final AtomicBoolean errorOccurred = new AtomicBoolean(false);
TestSubscriber<Integer> ts = TestSubscriber.create(1);
Flowable<Integer> source = Flowable.just(1)
// if haven't caught exception in combineLatest operator then would incorrectly
// be picked up by this call to doOnError
.doOnError(new Consumer<Throwable>() {
@Override
public void accept(Throwable t) {
errorOccurred.set(true);
}
});
Flowable
.combineLatest(Collections.singletonList(source), THROW_NON_FATAL)
.subscribe(ts);
assertFalse(errorOccurred.get());
}
@Ignore("Nulls are not allowed")
@Test
public void combineManyNulls() {
int n = Flowable.bufferSize() * 3;
Flowable<Integer> source = Flowable.just((Integer)null);
List<Flowable<Integer>> sources = new ArrayList<Flowable<Integer>>();
for (int i = 0; i < n; i++) {
sources.add(source);
}
TestSubscriber<Integer> ts = TestSubscriber.create();
Flowable.combineLatest(sources, new Function<Object[], Integer>() {
@Override
public Integer apply(Object[] args) {
int sum = 0;
for (Object o : args) {
if (o == null) {
sum ++;
}
}
return sum;
}
}).subscribe(ts);
ts.assertValue(n);
ts.assertNoErrors();
ts.assertComplete();
}
@SuppressWarnings("unchecked")
@Test
public void combineLatestIterable() {
Flowable<Integer> source = Flowable.just(1);
TestSubscriber<Integer> ts = TestSubscriber.create();
Flowable.combineLatest(Arrays.asList(source, source),
new Function<Object[], Integer>() {
@Override
public Integer apply(Object[] args) {
return (Integer)args[0] + (Integer)args[1];
}
})
.subscribe(ts);
ts.assertValue(2);
ts.assertNoErrors();
ts.assertComplete();
}
@Test
public void combineMany() {
int n = Flowable.bufferSize() * 3;
List<Flowable<Integer>> sources = new ArrayList<Flowable<Integer>>();
StringBuilder expected = new StringBuilder(n * 2);
for (int i = 0; i < n; i++) {
sources.add(Flowable.just(i));
expected.append(i);
}
TestSubscriber<String> ts = TestSubscriber.create();
Flowable.combineLatest(sources, new Function<Object[], String>() {
@Override
public String apply(Object[] args) {
StringBuilder b = new StringBuilder();
for (Object o : args) {
b.append(o);
}
return b.toString();
}
}).subscribe(ts);
ts.assertNoErrors();
ts.assertValue(expected.toString());
ts.assertComplete();
}
@SuppressWarnings("unchecked")
@Test
public void firstJustError() {
TestSubscriber<Integer> ts = TestSubscriber.create();
Flowable.combineLatestDelayError(
Arrays.asList(Flowable.just(1), Flowable.<Integer>error(new TestException())),
new Function<Object[], Integer>() {
@Override
public Integer apply(Object[] args) {
return ((Integer)args[0]) + ((Integer)args[1]);
}
}
).subscribe(ts);
ts.assertNoValues();
ts.assertError(TestException.class);
ts.assertNotComplete();
}
@SuppressWarnings("unchecked")
@Test
public void secondJustError() {
TestSubscriber<Integer> ts = TestSubscriber.create();
Flowable.combineLatestDelayError(
Arrays.asList(Flowable.<Integer>error(new TestException()), Flowable.just(1)),
new Function<Object[], Integer>() {
@Override
public Integer apply(Object[] args) {
return ((Integer)args[0]) + ((Integer)args[1]);
}
}
).subscribe(ts);
ts.assertNoValues();
ts.assertError(TestException.class);
ts.assertNotComplete();
}
@SuppressWarnings("unchecked")
@Test
public void oneErrors() {
TestSubscriber<Integer> ts = TestSubscriber.create();
Flowable.combineLatestDelayError(
Arrays.asList(Flowable.just(10).concatWith(Flowable.<Integer>error(new TestException())), Flowable.just(1)),
new Function<Object[], Integer>() {
@Override