Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableWithLatestFromTest.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.util.*;
21
22 import org.junit.Test;
23 import org.mockito.InOrder;
24 import org.reactivestreams.Subscriber;
25
26 import io.reactivex.rxjava3.core.*;
27 import io.reactivex.rxjava3.exceptions.TestException;
28 import io.reactivex.rxjava3.functions.*;
29 import io.reactivex.rxjava3.internal.functions.Functions;
30 import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription;
31 import io.reactivex.rxjava3.internal.util.CrashingMappedIterable;
32 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
33 import io.reactivex.rxjava3.processors.PublishProcessor;
34 import io.reactivex.rxjava3.subscribers.TestSubscriber;
35 import io.reactivex.rxjava3.testsupport.*;
36
37 public class FlowableWithLatestFromTest extends RxJavaTest {
38 static final BiFunction<Integer, Integer, Integer> COMBINER = new BiFunction<Integer, Integer, Integer>() {
39 @Override
40 public Integer apply(Integer t1, Integer t2) {
41 return (t1 << 8) + t2;
42 }
43 };
44 static final BiFunction<Integer, Integer, Integer> COMBINER_ERROR = new BiFunction<Integer, Integer, Integer>() {
45 @Override
46 public Integer apply(Integer t1, Integer t2) {
47 throw new TestException("Forced failure");
48 }
49 };
50 @Test
51 public void simple() {
52 PublishProcessor<Integer> source = PublishProcessor.create();
53 PublishProcessor<Integer> other = PublishProcessor.create();
54
55 Subscriber<Integer> subscriber = TestHelper.mockSubscriber();
56 InOrder inOrder = inOrder(subscriber);
57
58 Flowable<Integer> result = source.withLatestFrom(other, COMBINER);
59
60 result.subscribe(subscriber);
61
62 source.onNext(1);
63 inOrder.verify(subscriber, never()).onNext(anyInt());
64
65 other.onNext(1);
66 inOrder.verify(subscriber, never()).onNext(anyInt());
67
68 source.onNext(2);
69 inOrder.verify(subscriber).onNext((2 << 8) + 1);
70
71 other.onNext(2);
72 inOrder.verify(subscriber, never()).onNext(anyInt());
73
74 other.onComplete();
75 inOrder.verify(subscriber, never()).onComplete();
76
77 source.onNext(3);
78 inOrder.verify(subscriber).onNext((3 << 8) + 2);
79
80 source.onComplete();
81 inOrder.verify(subscriber).onComplete();
82
83 verify(subscriber, never()).onError(any(Throwable.class));
84 }
85
86 @Test
87 public void emptySource() {
88 PublishProcessor<Integer> source = PublishProcessor.create();
89 PublishProcessor<Integer> other = PublishProcessor.create();
90
91 Flowable<Integer> result = source.withLatestFrom(other, COMBINER);
92
93 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
94
95 result.subscribe(ts);
96
97 assertTrue(source.hasSubscribers());
98 assertTrue(other.hasSubscribers());
99
100 other.onNext(1);
101
102 source.onComplete();
103
104 ts.assertNoErrors();
105 ts.assertTerminated();
106 ts.assertNoValues();
107
108 assertFalse(source.hasSubscribers());
109 assertFalse(other.hasSubscribers());
110 }
111
112 @Test
113 public void emptyOther() {
114 PublishProcessor<Integer> source = PublishProcessor.create();
115 PublishProcessor<Integer> other = PublishProcessor.create();
116
117 Flowable<Integer> result = source.withLatestFrom(other, COMBINER);
118
119 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
120
121 result.subscribe(ts);
122
123 assertTrue(source.hasSubscribers());
124 assertTrue(other.hasSubscribers());
125
126 source.onNext(1);
127
128 source.onComplete();
129
130 ts.assertNoErrors();
131 ts.assertTerminated();
132 ts.assertNoValues();
133
134 assertFalse(source.hasSubscribers());
135 assertFalse(other.hasSubscribers());
136 }
137
138 @Test
139 public void unsubscription() {
140 PublishProcessor<Integer> source = PublishProcessor.create();
141 PublishProcessor<Integer> other = PublishProcessor.create();
142
143 Flowable<Integer> result = source.withLatestFrom(other, COMBINER);
144
145 TestSubscriber<Integer> ts = new TestSubscriber<>();
146
147 result.subscribe(ts);
148
149 assertTrue(source.hasSubscribers());
150 assertTrue(other.hasSubscribers());
151
152 other.onNext(1);
153 source.onNext(1);
154
155 ts.cancel();
156
157 ts.assertValue((1 << 8) + 1);
158 ts.assertNoErrors();
159 ts.assertNotComplete();
160
161 assertFalse(source.hasSubscribers());
162 assertFalse(other.hasSubscribers());
163 }
164
165 @Test
166 public void sourceThrows() {
167 PublishProcessor<Integer> source = PublishProcessor.create();
168 PublishProcessor<Integer> other = PublishProcessor.create();
169
170 Flowable<Integer> result = source.withLatestFrom(other, COMBINER);
171
172 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
173
174 result.subscribe(ts);
175
176 assertTrue(source.hasSubscribers());
177 assertTrue(other.hasSubscribers());
178
179 other.onNext(1);
180 source.onNext(1);
181
182 source.onError(new TestException());
183
184 ts.assertTerminated();
185 ts.assertValue((1 << 8) + 1);
186 ts.assertError(TestException.class);
187 ts.assertNotComplete();
188
189 assertFalse(source.hasSubscribers());
190 assertFalse(other.hasSubscribers());
191 }
192
193 @Test
194 public void otherThrows() {
195 PublishProcessor<Integer> source = PublishProcessor.create();
196 PublishProcessor<Integer> other = PublishProcessor.create();
197
198 Flowable<Integer> result = source.withLatestFrom(other, COMBINER);
199
200 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
201
202 result.subscribe(ts);
203
204 assertTrue(source.hasSubscribers());
205 assertTrue(other.hasSubscribers());
206
207 other.onNext(1);
208 source.onNext(1);
209
210 other.onError(new TestException());
211
212 ts.assertTerminated();
213 ts.assertValue((1 << 8) + 1);
214 ts.assertNotComplete();
215 ts.assertError(TestException.class);
216
217 assertFalse(source.hasSubscribers());
218 assertFalse(other.hasSubscribers());
219 }
220
221 @Test
222 public void functionThrows() {
223 PublishProcessor<Integer> source = PublishProcessor.create();
224 PublishProcessor<Integer> other = PublishProcessor.create();
225
226 Flowable<Integer> result = source.withLatestFrom(other, COMBINER_ERROR);
227
228 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
229
230 result.subscribe(ts);
231
232 assertTrue(source.hasSubscribers());
233 assertTrue(other.hasSubscribers());
234
235 other.onNext(1);
236 source.onNext(1);
237
238 ts.assertTerminated();
239 ts.assertNotComplete();
240 ts.assertNoValues();
241 ts.assertError(TestException.class);
242
243 assertFalse(source.hasSubscribers());
244 assertFalse(other.hasSubscribers());
245 }
246
247 @Test
248 public void noDownstreamUnsubscribe() {
249 PublishProcessor<Integer> source = PublishProcessor.create();
250 PublishProcessor<Integer> other = PublishProcessor.create();
251
252 Flowable<Integer> result = source.withLatestFrom(other, COMBINER);
253
254 TestSubscriber<Integer> ts = new TestSubscriber<>();
255
256 result.subscribe(ts);
257
258 source.onComplete();
259
260 assertFalse(ts.isCancelled());
261 }
262
263 @Test
264 public void backpressure() {
265 PublishProcessor<Integer> source = PublishProcessor.create();
266 PublishProcessor<Integer> other = PublishProcessor.create();
267
268 Flowable<Integer> result = source.withLatestFrom(other, COMBINER);
269
270 TestSubscriber<Integer> ts = new TestSubscriber<>(0L);
271
272 result.subscribe(ts);
273
274 assertTrue("Other has no observers!", other.hasSubscribers());
275
276 ts.request(1);
277
278 source.onNext(1);
279
280 assertTrue("Other has no observers!", other.hasSubscribers());
281
282 ts.assertNoValues();
283
284 other.onNext(1);
285
286 source.onNext(2);
287
288 ts.assertValue((2 << 8) + 1);
289
290 ts.request(5);
291 source.onNext(3);
292 source.onNext(4);
293 source.onNext(5);
294 source.onNext(6);
295 source.onNext(7);
296 ts.assertValues(
297 (2 << 8) + 1, (3 << 8) + 1, (4 << 8) + 1, (5 << 8) + 1,
298 (6 << 8) + 1, (7 << 8) + 1
299 );
300
301 ts.cancel();
302
303 assertFalse("Other has observers!", other.hasSubscribers());
304
305 ts.assertNoErrors();
306 }
307
308 static final Function<Object[], String> toArray = new Function<Object[], String>() {
309 @Override
310 public String apply(Object[] args) {
311 return Arrays.toString(args);
312 }
313 };
314
315 @Test
316 public void manySources() {
317 PublishProcessor<String> pp1 = PublishProcessor.create();
318 PublishProcessor<String> pp2 = PublishProcessor.create();
319 PublishProcessor<String> pp3 = PublishProcessor.create();
320 PublishProcessor<String> main = PublishProcessor.create();
321
322 TestSubscriber<String> ts = new TestSubscriber<>();
323
324 main.withLatestFrom(new Flowable[] { pp1, pp2, pp3 }, toArray)
325 .subscribe(ts);
326
327 main.onNext("1");
328 ts.assertNoValues();
329 pp1.onNext("a");
330 ts.assertNoValues();
331 pp2.onNext("A");
332 ts.assertNoValues();
333 pp3.onNext("=");
334 ts.assertNoValues();
335
336 main.onNext("2");
337 ts.assertValues("[2, a, A, =]");
338
339 pp2.onNext("B");
340
341 ts.assertValues("[2, a, A, =]");
342
343 pp3.onComplete();
344 ts.assertValues("[2, a, A, =]");
345
346 pp1.onNext("b");
347
348 main.onNext("3");
349
350 ts.assertValues("[2, a, A, =]", "[3, b, B, =]");
351
352 main.onComplete();
353 ts.assertValues("[2, a, A, =]", "[3, b, B, =]");
354 ts.assertNoErrors();
355 ts.assertComplete();
356
357 assertFalse("ps1 has subscribers?", pp1.hasSubscribers());
358 assertFalse("ps2 has subscribers?", pp2.hasSubscribers());
359 assertFalse("ps3 has subscribers?", pp3.hasSubscribers());
360 }
361
362 @Test
363 public void manySourcesIterable() {
364 PublishProcessor<String> pp1 = PublishProcessor.create();
365 PublishProcessor<String> pp2 = PublishProcessor.create();
366 PublishProcessor<String> pp3 = PublishProcessor.create();
367 PublishProcessor<String> main = PublishProcessor.create();
368
369 TestSubscriber<String> ts = new TestSubscriber<>();
370
371 main.withLatestFrom(Arrays.<Flowable<?>>asList(pp1, pp2, pp3), toArray)
372 .subscribe(ts);
373
374 main.onNext("1");
375 ts.assertNoValues();
376 pp1.onNext("a");
377 ts.assertNoValues();
378 pp2.onNext("A");
379 ts.assertNoValues();
380 pp3.onNext("=");
381 ts.assertNoValues();
382
383 main.onNext("2");
384 ts.assertValues("[2, a, A, =]");
385
386 pp2.onNext("B");
387
388 ts.assertValues("[2, a, A, =]");
389
390 pp3.onComplete();
391 ts.assertValues("[2, a, A, =]");
392
393 pp1.onNext("b");
394
395 main.onNext("3");
396
397 ts.assertValues("[2, a, A, =]", "[3, b, B, =]");
398
399 main.onComplete();
400 ts.assertValues("[2, a, A, =]", "[3, b, B, =]");
401 ts.assertNoErrors();
402 ts.assertComplete();
403
404 assertFalse("ps1 has subscribers?", pp1.hasSubscribers());
405 assertFalse("ps2 has subscribers?", pp2.hasSubscribers());
406 assertFalse("ps3 has subscribers?", pp3.hasSubscribers());
407 }
408
409 @Test
410 public void manySourcesIterableSweep() {
411 for (String val : new String[] { "1" /*, null*/ }) {
412 int n = 35;
413 for (int i = 0; i < n; i++) {
414 List<Flowable<?>> sources = new ArrayList<>();
415 List<String> expected = new ArrayList<>();
416 expected.add(val);
417
418 for (int j = 0; j < i; j++) {
419 sources.add(Flowable.just(val));
420 expected.add(String.valueOf(val));
421 }
422
423 TestSubscriber<String> ts = new TestSubscriber<>();
424
425 PublishProcessor<String> main = PublishProcessor.create();
426
427 main.withLatestFrom(sources, toArray).subscribe(ts);
428
429 ts.assertNoValues();
430
431 main.onNext(val);
432 main.onComplete();
433
434 ts.assertValue(expected.toString());
435 ts.assertNoErrors();
436 ts.assertComplete();
437 }
438 }
439 }
440
441 @Test
442 public void backpressureNoSignal() {
443 PublishProcessor<String> pp1 = PublishProcessor.create();
444 PublishProcessor<String> pp2 = PublishProcessor.create();
445
446 TestSubscriber<String> ts = new TestSubscriber<>(0);
447
448 Flowable.range(1, 10).withLatestFrom(new Flowable<?>[] { pp1, pp2 }, toArray)
449 .subscribe(ts);
450
451 ts.assertNoValues();
452
453 ts.request(1);
454
455 ts.assertNoValues();
456 ts.assertNoErrors();
457 ts.assertComplete();
458
459 assertFalse("ps1 has subscribers?", pp1.hasSubscribers());
460 assertFalse("ps2 has subscribers?", pp2.hasSubscribers());
461 }
462
463 @Test
464 public void backpressureWithSignal() {
465 PublishProcessor<String> pp1 = PublishProcessor.create();
466 PublishProcessor<String> pp2 = PublishProcessor.create();
467
468 TestSubscriber<String> ts = new TestSubscriber<>(0);
469
470 Flowable.range(1, 3).withLatestFrom(new Flowable<?>[] { pp1, pp2 }, toArray)
471 .subscribe(ts);
472
473 ts.assertNoValues();
474
475 pp1.onNext("1");
476 pp2.onNext("1");
477
478 ts.request(1);
479
480 ts.assertValue("[1, 1, 1]");
481
482 ts.request(1);
483
484 ts.assertValues("[1, 1, 1]", "[2, 1, 1]");
485
486 ts.request(1);
487
488 ts.assertValues("[1, 1, 1]", "[2, 1, 1]", "[3, 1, 1]");
489 ts.assertNoErrors();
490 ts.assertComplete();
491
492 assertFalse("ps1 has subscribers?", pp1.hasSubscribers());
493 assertFalse("ps2 has subscribers?", pp2.hasSubscribers());
494 }
495
496 @Test
497 public void withEmpty() {
498 TestSubscriber<String> ts = new TestSubscriber<>(0);
499
500 Flowable.range(1, 3).withLatestFrom(
501 new Flowable<?>[] { Flowable.just(1), Flowable.empty() }, toArray)
502 .subscribe(ts);
503
504 ts.assertNoValues();
505 ts.assertNoErrors();
506 ts.assertComplete();
507 }
508
509 @Test
510 public void withError() {
511 TestSubscriber<String> ts = new TestSubscriber<>(0);
512
513 Flowable.range(1, 3).withLatestFrom(
514 new Flowable<?>[] { Flowable.just(1), Flowable.error(new TestException()) }, toArray)
515 .subscribe(ts);
516
517 ts.assertNoValues();
518 ts.assertError(TestException.class);
519 ts.assertNotComplete();
520 }
521
522 @Test
523 public void withMainError() {
524 TestSubscriber<String> ts = new TestSubscriber<>(0);
525
526 Flowable.error(new TestException()).withLatestFrom(
527 new Flowable<?>[] { Flowable.just(1), Flowable.just(1) }, toArray)
528 .subscribe(ts);
529
530 ts.assertNoValues();
531 ts.assertError(TestException.class);
532 ts.assertNotComplete();
533 }
534
535 @Test
536 public void with2Others() {
537 Flowable<Integer> just = Flowable.just(1);
538
539 TestSubscriber<List<Integer>> ts = new TestSubscriber<>();
540
541 just.withLatestFrom(just, just, new Function3<Integer, Integer, Integer, List<Integer>>() {
542 @Override
543 public List<Integer> apply(Integer a, Integer b, Integer c) {
544 return Arrays.asList(a, b, c);
545 }
546 })
547 .subscribe(ts);
548
549 ts.assertValue(Arrays.asList(1, 1, 1));
550 ts.assertNoErrors();
551 ts.assertComplete();
552 }
553
554 @Test
555 public void with3Others() {
556 Flowable<Integer> just = Flowable.just(1);
557
558 TestSubscriber<List<Integer>> ts = new TestSubscriber<>();
559
560 just.withLatestFrom(just, just, just, new Function4<Integer, Integer, Integer, Integer, List<Integer>>() {
561 @Override
562 public List<Integer> apply(Integer a, Integer b, Integer c, Integer d) {
563 return Arrays.asList(a, b, c, d);
564 }
565 })
566 .subscribe(ts);
567
568 ts.assertValue(Arrays.asList(1, 1, 1, 1));
569 ts.assertNoErrors();
570 ts.assertComplete();
571 }
572
573 @Test
574 public void with4Others() {
575 Flowable<Integer> just = Flowable.just(1);
576
577 TestSubscriber<List<Integer>> ts = new TestSubscriber<>();
578
579 just.withLatestFrom(just, just, just, just, new Function5<Integer, Integer, Integer, Integer, Integer, List<Integer>>() {
580 @Override
581 public List<Integer> apply(Integer a, Integer b, Integer c, Integer d, Integer e) {
582 return Arrays.asList(a, b, c, d, e);
583 }
584 })
585 .subscribe(ts);
586
587 ts.assertValue(Arrays.asList(1, 1, 1, 1, 1));
588 ts.assertNoErrors();
589 ts.assertComplete();
590 }
591
592 @Test
593 public void dispose() {
594 TestHelper.checkDisposed(Flowable.just(1).withLatestFrom(Flowable.just(2), new BiFunction<Integer, Integer, Object>() {
595 @Override
596 public Object apply(Integer a, Integer b) throws Exception {
597 return a;
598 }
599 }));
600
601 TestHelper.checkDisposed(Flowable.just(1).withLatestFrom(Flowable.just(2), Flowable.just(3), new Function3<Integer, Integer, Integer, Object>() {
602 @Override
603 public Object apply(Integer a, Integer b, Integer c) throws Exception {
604 return a;
605 }
606 }));
607 }
608
609 @Test
610 public void manyIteratorThrows() {
611 Flowable.just(1)
612 .withLatestFrom(new CrashingMappedIterable<>(1, 100, 100, new Function<Integer, Flowable<Integer>>() {
613 @Override
614 public Flowable<Integer> apply(Integer v) throws Exception {
615 return Flowable.just(2);
616 }
617 }), new Function<Object[], Object>() {
618 @Override
619 public Object apply(Object[] a) throws Exception {
620 return a;
621 }
622 })
623 .to(TestHelper.testConsumer())
624 .assertFailureAndMessage(TestException.class, "iterator()");
625 }
626
627 @Test
628 public void manyCombinerThrows() {
629 Flowable.just(1).withLatestFrom(Flowable.just(2), Flowable.just(3), new Function3<Integer, Integer, Integer, Object>() {
630 @Override
631 public Object apply(Integer a, Integer b, Integer c) throws Exception {
632 throw new TestException();
633 }
634 })
635 .test()
636 .assertFailure(TestException.class);
637 }
638
639 @Test
640 public void manyErrors() {
641 List<Throwable> errors = TestHelper.trackPluginErrors();
642 try {
643 new Flowable<Integer>() {
644 @Override
645 protected void subscribeActual(Subscriber<? super Integer> subscriber) {
646 subscriber.onSubscribe(new BooleanSubscription());
647 subscriber.onError(new TestException("First"));
648 subscriber.onNext(1);
649 subscriber.onError(new TestException("Second"));
650 subscriber.onComplete();
651 }
652 }.withLatestFrom(Flowable.just(2), Flowable.just(3), new Function3<Integer, Integer, Integer, Object>() {
653 @Override
654 public Object apply(Integer a, Integer b, Integer c) throws Exception {
655 return a;
656 }
657 })
658 .to(TestHelper.testConsumer())
659 .assertFailureAndMessage(TestException.class, "First");
660
661 TestHelper.assertUndeliverable(errors, 0, TestException.class, "Second");
662 } finally {
663 RxJavaPlugins.reset();
664 }
665 }
666
667 @Test
668 public void otherErrors() {
669 List<Throwable> errors = TestHelper.trackPluginErrors();
670 try {
671 Flowable.just(1)
672 .withLatestFrom(new Flowable<Integer>() {
673 @Override
674 protected void subscribeActual(Subscriber<? super Integer> s) {
675 s.onSubscribe(new BooleanSubscription());
676 s.onError(new TestException("First"));
677 s.onError(new TestException("Second"));
678 }
679 }, new BiFunction<Integer, Integer, Integer>() {
680 @Override
681 public Integer apply(Integer a, Integer b) throws Exception {
682 return a + b;
683 }
684 })
685 .to(TestHelper.<Integer>testConsumer())
686 .assertFailureAndMessage(TestException.class, "First");
687
688 TestHelper.assertUndeliverable(errors, 0, TestException.class, "Second");
689 } finally {
690 RxJavaPlugins.reset();
691 }
692 }
693
694 @Test
695 public void combineToNull1() {
696 Flowable.just(1)
697 .withLatestFrom(Flowable.just(2), new BiFunction<Integer, Integer, Object>() {
698 @Override
699 public Object apply(Integer a, Integer b) throws Exception {
700 return null;
701 }
702 })
703 .test()
704 .assertFailure(NullPointerException.class);
705 }
706
707 @Test
708 public void combineToNull2() {
709 Flowable.just(1)
710 .withLatestFrom(Arrays.asList(Flowable.just(2), Flowable.just(3)), new Function<Object[], Object>() {
711 @Override
712 public Object apply(Object[] o) throws Exception {
713 return null;
714 }
715 })
716 .test()
717 .assertFailure(NullPointerException.class);
718 }
719
720 @Test
721 public void zeroOtherCombinerReturnsNull() {
722 Flowable.just(1)
723 .withLatestFrom(new Flowable[0], Functions.justFunction(null))
724 .to(TestHelper.testConsumer())
725 .assertFailureAndMessage(NullPointerException.class, "The combiner returned a null value");
726 }
727
728 @Test
729 public void singleRequestNotForgottenWhenNoData() {
730 PublishProcessor<Integer> source = PublishProcessor.create();
731 PublishProcessor<Integer> other = PublishProcessor.create();
732
733 Flowable<Integer> result = source.withLatestFrom(other, COMBINER);
734
735 TestSubscriber<Integer> ts = new TestSubscriber<>(0L);
736
737 result.subscribe(ts);
738
739 ts.request(1);
740
741 source.onNext(1);
742
743 ts.assertNoValues();
744
745 other.onNext(1);
746
747 ts.assertNoValues();
748
749 source.onNext(2);
750
751 ts.assertValue((2 << 8) + 1);
752 }
753
754 @Test
755 public void coldSourceConsumedWithoutOther() {
756 Flowable.range(1, 10).withLatestFrom(Flowable.never(),
757 new BiFunction<Integer, Object, Object>() {
758 @Override
759 public Object apply(Integer a, Object b) throws Exception {
760 return a;
761 }
762 })
763 .test(1)
764 .assertResult();
765 }
766
767 @Test
768 public void coldSourceConsumedWithoutManyOthers() {
769 Flowable.range(1, 10).withLatestFrom(Flowable.never(), Flowable.never(), Flowable.never(),
770 new Function4<Integer, Object, Object, Object, Object>() {
771 @Override
772 public Object apply(Integer a, Object b, Object c, Object d) throws Exception {
773 return a;
774 }
775 })
776 .test(1)
777 .assertResult();
778 }
779
780 @Test
781 public void otherOnSubscribeRace() {
782 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
783 final PublishProcessor<Integer> pp0 = PublishProcessor.create();
784 final PublishProcessor<Integer> pp1 = PublishProcessor.create();
785 final PublishProcessor<Integer> pp2 = PublishProcessor.create();
786 final PublishProcessor<Integer> pp3 = PublishProcessor.create();
787
788 final Flowable<Object> source = pp0.withLatestFrom(pp1, pp2, pp3, new Function4<Object, Integer, Integer, Integer, Object>() {
789 @Override
790 public Object apply(Object a, Integer b, Integer c, Integer d)
791 throws Exception {
792 return a;
793 }
794 });
795
796 final TestSubscriber<Object> ts = new TestSubscriber<>();
797
798 Runnable r1 = new Runnable() {
799 @Override
800 public void run() {
801 source.subscribe(ts);
802 }
803 };
804
805 Runnable r2 = new Runnable() {
806 @Override
807 public void run() {
808 ts.cancel();
809 }
810 };
811
812 TestHelper.race(r1, r2);
813
814 ts.assertEmpty();
815
816 assertFalse(pp0.hasSubscribers());
817 assertFalse(pp1.hasSubscribers());
818 assertFalse(pp2.hasSubscribers());
819 assertFalse(pp3.hasSubscribers());
820 }
821 }
822
823 @Test
824 public void otherCompleteRace() {
825 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
826 final PublishProcessor<Integer> pp0 = PublishProcessor.create();
827 final PublishProcessor<Integer> pp1 = PublishProcessor.create();
828 final PublishProcessor<Integer> pp2 = PublishProcessor.create();
829 final PublishProcessor<Integer> pp3 = PublishProcessor.create();
830
831 final Flowable<Object> source = pp0.withLatestFrom(pp1, pp2, pp3, new Function4<Object, Integer, Integer, Integer, Object>() {
832 @Override
833 public Object apply(Object a, Integer b, Integer c, Integer d)
834 throws Exception {
835 return a;
836 }
837 });
838
839 final TestSubscriber<Object> ts = new TestSubscriber<>();
840
841 Runnable r1 = new Runnable() {
842 @Override
843 public void run() {
844 source.subscribe(ts);
845 }
846 };
847
848 Runnable r2 = new Runnable() {
849 @Override
850 public void run() {
851 pp1.onComplete();
852 }
853 };
854
855 TestHelper.race(r1, r2);
856
857 ts.assertResult();
858
859 assertFalse(pp0.hasSubscribers());
860 assertFalse(pp1.hasSubscribers());
861 assertFalse(pp2.hasSubscribers());
862 assertFalse(pp3.hasSubscribers());
863 }
864 }
865 }