Debian Codegraph @

src/test/java/io/reactivex/rxjava3/core/XFlatMapTest.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.core;
15
16 import static org.junit.Assert.assertTrue;
17
18 import java.util.List;
19 import java.util.concurrent.CyclicBarrier;
20
21 import org.junit.*;
22 import org.reactivestreams.Publisher;
23
24 import io.reactivex.rxjava3.exceptions.TestException;
25 import io.reactivex.rxjava3.functions.*;
26 import io.reactivex.rxjava3.observers.TestObserver;
27 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
28 import io.reactivex.rxjava3.schedulers.Schedulers;
29 import io.reactivex.rxjava3.subscribers.TestSubscriber;
30 import io.reactivex.rxjava3.testsupport.TestHelper;
31
32 public class XFlatMapTest extends RxJavaTest {
33
34 @Rule
35 public Retry retry = new Retry(5, 1000, true);
36
37 static final int SLEEP_AFTER_CANCEL = 500;
38
39 final CyclicBarrier cb = new CyclicBarrier(2);
40
41 void sleep() throws Exception {
42 cb.await();
43 try {
44 long before = System.currentTimeMillis();
45 Thread.sleep(5000);
46 throw new IllegalStateException("Was not interrupted in time?! " + (System.currentTimeMillis() - before));
47 } catch (InterruptedException ex) {
48 // ignored here
49 }
50 }
51
52 void beforeCancelSleep(TestSubscriber<?> ts) throws Exception {
53 long before = System.currentTimeMillis();
54 Thread.sleep(50);
55 if (System.currentTimeMillis() - before > 100) {
56 ts.cancel();
57 throw new IllegalStateException("Overslept?" + (System.currentTimeMillis() - before));
58 }
59 }
60
61 void beforeCancelSleep(TestObserver<?> to) throws Exception {
62 long before = System.currentTimeMillis();
63 Thread.sleep(50);
64 if (System.currentTimeMillis() - before > 100) {
65 to.dispose();
66 throw new IllegalStateException("Overslept?" + (System.currentTimeMillis() - before));
67 }
68 }
69
70 @Test
71 public void flowableFlowable() throws Exception {
72 List<Throwable> errors = TestHelper.trackPluginErrors();
73 try {
74 TestSubscriber<Integer> ts = Flowable.just(1)
75 .subscribeOn(Schedulers.io())
76 .flatMap(new Function<Integer, Publisher<Integer>>() {
77 @Override
78 public Publisher<Integer> apply(Integer v) throws Exception {
79 sleep();
80 return Flowable.<Integer>error(new TestException());
81 }
82 })
83 .test();
84
85 cb.await();
86
87 beforeCancelSleep(ts);
88
89 ts.cancel();
90
91 Thread.sleep(SLEEP_AFTER_CANCEL);
92
93 ts.assertEmpty();
94
95 assertTrue(errors.toString(), errors.isEmpty());
96 } finally {
97 RxJavaPlugins.reset();
98 }
99 }
100
101 @Test
102 public void flowableSingle() throws Exception {
103 List<Throwable> errors = TestHelper.trackPluginErrors();
104 try {
105 TestSubscriber<Integer> ts = Flowable.just(1)
106 .subscribeOn(Schedulers.io())
107 .flatMapSingle(new Function<Integer, Single<Integer>>() {
108 @Override
109 public Single<Integer> apply(Integer v) throws Exception {
110 sleep();
111 return Single.<Integer>error(new TestException());
112 }
113 })
114 .test();
115
116 cb.await();
117
118 beforeCancelSleep(ts);
119
120 ts.cancel();
121
122 Thread.sleep(SLEEP_AFTER_CANCEL);
123
124 ts.assertEmpty();
125
126 assertTrue(errors.toString(), errors.isEmpty());
127 } finally {
128 RxJavaPlugins.reset();
129 }
130 }
131
132 @Test
133 public void flowableMaybe() throws Exception {
134 List<Throwable> errors = TestHelper.trackPluginErrors();
135 try {
136 TestSubscriber<Integer> ts = Flowable.just(1)
137 .subscribeOn(Schedulers.io())
138 .flatMapMaybe(new Function<Integer, Maybe<Integer>>() {
139 @Override
140 public Maybe<Integer> apply(Integer v) throws Exception {
141 sleep();
142 return Maybe.<Integer>error(new TestException());
143 }
144 })
145 .test();
146
147 cb.await();
148
149 beforeCancelSleep(ts);
150
151 ts.cancel();
152
153 Thread.sleep(SLEEP_AFTER_CANCEL);
154
155 ts.assertEmpty();
156
157 assertTrue(errors.toString(), errors.isEmpty());
158 } finally {
159 RxJavaPlugins.reset();
160 }
161 }
162
163 @Test
164 public void flowableCompletable() throws Exception {
165 List<Throwable> errors = TestHelper.trackPluginErrors();
166 try {
167 TestObserver<Void> to = Flowable.just(1)
168 .subscribeOn(Schedulers.io())
169 .flatMapCompletable(new Function<Integer, Completable>() {
170 @Override
171 public Completable apply(Integer v) throws Exception {
172 sleep();
173 return Completable.error(new TestException());
174 }
175 })
176 .test();
177
178 cb.await();
179
180 beforeCancelSleep(to);
181
182 to.dispose();
183
184 Thread.sleep(SLEEP_AFTER_CANCEL);
185
186 to.assertEmpty();
187
188 assertTrue(errors.toString(), errors.isEmpty());
189 } finally {
190 RxJavaPlugins.reset();
191 }
192 }
193
194 @Test
195 public void flowableCompletable2() throws Exception {
196 List<Throwable> errors = TestHelper.trackPluginErrors();
197 try {
198 TestSubscriber<Void> ts = Flowable.just(1)
199 .subscribeOn(Schedulers.io())
200 .flatMapCompletable(new Function<Integer, Completable>() {
201 @Override
202 public Completable apply(Integer v) throws Exception {
203 sleep();
204 return Completable.error(new TestException());
205 }
206 })
207 .<Void>toFlowable()
208 .test();
209
210 cb.await();
211
212 beforeCancelSleep(ts);
213
214 ts.cancel();
215
216 Thread.sleep(SLEEP_AFTER_CANCEL);
217
218 ts.assertEmpty();
219
220 assertTrue(errors.toString(), errors.isEmpty());
221 } finally {
222 RxJavaPlugins.reset();
223 }
224 }
225
226 @Test
227 public void observableObservable() throws Exception {
228 List<Throwable> errors = TestHelper.trackPluginErrors();
229 try {
230 TestObserver<Integer> to = Observable.just(1)
231 .subscribeOn(Schedulers.io())
232 .flatMap(new Function<Integer, Observable<Integer>>() {
233 @Override
234 public Observable<Integer> apply(Integer v) throws Exception {
235 sleep();
236 return Observable.<Integer>error(new TestException());
237 }
238 })
239 .test();
240
241 cb.await();
242
243 beforeCancelSleep(to);
244
245 to.dispose();
246
247 Thread.sleep(SLEEP_AFTER_CANCEL);
248
249 to.assertEmpty();
250
251 assertTrue(errors.toString(), errors.isEmpty());
252 } finally {
253 RxJavaPlugins.reset();
254 }
255 }
256
257 @Test
258 public void observerSingle() throws Exception {
259 List<Throwable> errors = TestHelper.trackPluginErrors();
260 try {
261 TestObserver<Integer> to = Observable.just(1)
262 .subscribeOn(Schedulers.io())
263 .flatMapSingle(new Function<Integer, Single<Integer>>() {
264 @Override
265 public Single<Integer> apply(Integer v) throws Exception {
266 sleep();
267 return Single.<Integer>error(new TestException());
268 }
269 })
270 .test();
271
272 cb.await();
273
274 beforeCancelSleep(to);
275
276 to.dispose();
277
278 Thread.sleep(SLEEP_AFTER_CANCEL);
279
280 to.assertEmpty();
281
282 assertTrue(errors.toString(), errors.isEmpty());
283 } finally {
284 RxJavaPlugins.reset();
285 }
286 }
287
288 @Test
289 public void observerMaybe() throws Exception {
290 List<Throwable> errors = TestHelper.trackPluginErrors();
291 try {
292 TestObserver<Integer> to = Observable.just(1)
293 .subscribeOn(Schedulers.io())
294 .flatMapMaybe(new Function<Integer, Maybe<Integer>>() {
295 @Override
296 public Maybe<Integer> apply(Integer v) throws Exception {
297 sleep();
298 return Maybe.<Integer>error(new TestException());
299 }
300 })
301 .test();
302
303 cb.await();
304
305 beforeCancelSleep(to);
306
307 to.dispose();
308
309 Thread.sleep(SLEEP_AFTER_CANCEL);
310
311 to.assertEmpty();
312
313 assertTrue(errors.toString(), errors.isEmpty());
314 } finally {
315 RxJavaPlugins.reset();
316 }
317 }
318
319 @Test
320 public void observerCompletable() throws Exception {
321 List<Throwable> errors = TestHelper.trackPluginErrors();
322 try {
323 TestObserver<Void> to = Observable.just(1)
324 .subscribeOn(Schedulers.io())
325 .flatMapCompletable(new Function<Integer, Completable>() {
326 @Override
327 public Completable apply(Integer v) throws Exception {
328 sleep();
329 return Completable.error(new TestException());
330 }
331 })
332 .test();
333
334 cb.await();
335
336 beforeCancelSleep(to);
337
338 to.dispose();
339
340 Thread.sleep(SLEEP_AFTER_CANCEL);
341
342 to.assertEmpty();
343
344 assertTrue(errors.toString(), errors.isEmpty());
345 } finally {
346 RxJavaPlugins.reset();
347 }
348 }
349
350 @Test
351 public void observerCompletable2() throws Exception {
352 List<Throwable> errors = TestHelper.trackPluginErrors();
353 try {
354 TestObserver<Void> to = Observable.just(1)
355 .subscribeOn(Schedulers.io())
356 .flatMapCompletable(new Function<Integer, Completable>() {
357 @Override
358 public Completable apply(Integer v) throws Exception {
359 sleep();
360 return Completable.error(new TestException());
361 }
362 })
363 .<Void>toObservable()
364 .test();
365
366 cb.await();
367
368 beforeCancelSleep(to);
369
370 to.dispose();
371
372 Thread.sleep(SLEEP_AFTER_CANCEL);
373
374 to.assertEmpty();
375
376 assertTrue(errors.toString(), errors.isEmpty());
377 } finally {
378 RxJavaPlugins.reset();
379 }
380 }
381
382 @Test
383 public void singleSingle() throws Exception {
384 List<Throwable> errors = TestHelper.trackPluginErrors();
385 try {
386 TestObserver<Integer> to = Single.just(1)
387 .subscribeOn(Schedulers.io())
388 .flatMap(new Function<Integer, Single<Integer>>() {
389 @Override
390 public Single<Integer> apply(Integer v) throws Exception {
391 sleep();
392 return Single.<Integer>error(new TestException());
393 }
394 })
395 .test();
396
397 cb.await();
398
399 beforeCancelSleep(to);
400
401 to.dispose();
402
403 Thread.sleep(SLEEP_AFTER_CANCEL);
404
405 to.assertEmpty();
406
407 assertTrue(errors.toString(), errors.isEmpty());
408 } finally {
409 RxJavaPlugins.reset();
410 }
411 }
412
413 @Test
414 public void singleMaybe() throws Exception {
415 List<Throwable> errors = TestHelper.trackPluginErrors();
416 try {
417 TestObserver<Integer> to = Single.just(1)
418 .subscribeOn(Schedulers.io())
419 .flatMapMaybe(new Function<Integer, Maybe<Integer>>() {
420 @Override
421 public Maybe<Integer> apply(Integer v) throws Exception {
422 sleep();
423 return Maybe.<Integer>error(new TestException());
424 }
425 })
426 .test();
427
428 cb.await();
429
430 beforeCancelSleep(to);
431
432 to.dispose();
433
434 Thread.sleep(SLEEP_AFTER_CANCEL);
435
436 to.assertEmpty();
437
438 assertTrue(errors.toString(), errors.isEmpty());
439 } finally {
440 RxJavaPlugins.reset();
441 }
442 }
443
444 @Test
445 public void singleCompletable() throws Exception {
446 List<Throwable> errors = TestHelper.trackPluginErrors();
447 try {
448 TestObserver<Void> to = Single.just(1)
449 .subscribeOn(Schedulers.io())
450 .flatMapCompletable(new Function<Integer, Completable>() {
451 @Override
452 public Completable apply(Integer v) throws Exception {
453 sleep();
454 return Completable.error(new TestException());
455 }
456 })
457 .test();
458
459 cb.await();
460
461 beforeCancelSleep(to);
462
463 to.dispose();
464
465 Thread.sleep(SLEEP_AFTER_CANCEL);
466
467 to.assertEmpty();
468
469 assertTrue(errors.toString(), errors.isEmpty());
470 } finally {
471 RxJavaPlugins.reset();
472 }
473 }
474
475 @Test
476 public void singleCompletable2() throws Exception {
477 List<Throwable> errors = TestHelper.trackPluginErrors();
478 try {
479 TestObserver<Integer> to = Single.just(1)
480 .subscribeOn(Schedulers.io())
481 .flatMapCompletable(new Function<Integer, Completable>() {
482 @Override
483 public Completable apply(Integer v) throws Exception {
484 sleep();
485 return Completable.error(new TestException());
486 }
487 })
488 .toSingleDefault(0)
489 .test();
490
491 cb.await();
492
493 beforeCancelSleep(to);
494
495 to.dispose();
496
497 Thread.sleep(SLEEP_AFTER_CANCEL);
498
499 to.assertEmpty();
500
501 assertTrue(errors.toString(), errors.isEmpty());
502 } finally {
503 RxJavaPlugins.reset();
504 }
505 }
506
507 @Test
508 public void singlePublisher() throws Exception {
509 List<Throwable> errors = TestHelper.trackPluginErrors();
510 try {
511 TestSubscriber<Integer> ts = Single.just(1)
512 .subscribeOn(Schedulers.io())
513 .flatMapPublisher(new Function<Integer, Publisher<Integer>>() {
514 @Override
515 public Publisher<Integer> apply(Integer v) throws Exception {
516 sleep();
517 return Flowable.<Integer>error(new TestException());
518 }
519 })
520 .test();
521
522 cb.await();
523
524 beforeCancelSleep(ts);
525
526 ts.cancel();
527
528 Thread.sleep(SLEEP_AFTER_CANCEL);
529
530 ts.assertEmpty();
531
532 assertTrue(errors.toString(), errors.isEmpty());
533 } finally {
534 RxJavaPlugins.reset();
535 }
536 }
537
538 @Test
539 public void singleCombiner() throws Exception {
540 List<Throwable> errors = TestHelper.trackPluginErrors();
541 try {
542 TestObserver<Integer> to = Single.just(1)
543 .subscribeOn(Schedulers.io())
544 .flatMap(new Function<Integer, Single<Integer>>() {
545 @Override
546 public Single<Integer> apply(Integer v) throws Exception {
547 sleep();
548 return Single.<Integer>error(new TestException());
549 }
550 }, (a, b) -> a + b)
551 .test();
552
553 cb.await();
554
555 beforeCancelSleep(to);
556
557 to.dispose();
558
559 Thread.sleep(SLEEP_AFTER_CANCEL);
560
561 to.assertEmpty();
562
563 assertTrue(errors.toString(), errors.isEmpty());
564 } finally {
565 RxJavaPlugins.reset();
566 }
567 }
568
569 @Test
570 public void singleObservable() throws Exception {
571 List<Throwable> errors = TestHelper.trackPluginErrors();
572 try {
573 TestObserver<Integer> to = Single.just(1)
574 .subscribeOn(Schedulers.io())
575 .flatMapObservable(new Function<Integer, Observable<Integer>>() {
576 @Override
577 public Observable<Integer> apply(Integer v) throws Exception {
578 sleep();
579 return Observable.<Integer>error(new TestException());
580 }
581 })
582 .test();
583
584 cb.await();
585
586 beforeCancelSleep(to);
587
588 to.dispose();
589
590 Thread.sleep(SLEEP_AFTER_CANCEL);
591
592 to.assertEmpty();
593
594 assertTrue(errors.toString(), errors.isEmpty());
595 } finally {
596 RxJavaPlugins.reset();
597 }
598 }
599
600 @Test
601 public void singleNotificationSuccess() throws Exception {
602 List<Throwable> errors = TestHelper.trackPluginErrors();
603 try {
604 TestObserver<Integer> to = Single.just(1)
605 .subscribeOn(Schedulers.io())
606 .flatMap(
607 new Function<Integer, Single<Integer>>() {
608 @Override
609 public Single<Integer> apply(Integer v) throws Exception {
610 sleep();
611 return Single.<Integer>error(new TestException());
612 }
613 },
614 new Function<Throwable, Single<Integer>>() {
615 @Override
616 public Single<Integer> apply(Throwable v) throws Exception {
617 sleep();
618 return Single.<Integer>error(new TestException());
619 }
620 }
621 )
622 .test();
623
624 cb.await();
625
626 beforeCancelSleep(to);
627
628 to.dispose();
629
630 Thread.sleep(SLEEP_AFTER_CANCEL);
631
632 to.assertEmpty();
633
634 assertTrue(errors.toString(), errors.isEmpty());
635 } finally {
636 RxJavaPlugins.reset();
637 }
638 }
639
640 @Test
641 public void singleNotificationError() throws Exception {
642 List<Throwable> errors = TestHelper.trackPluginErrors();
643 try {
644 TestObserver<Integer> to = Single.<Integer>error(new TestException())
645 .subscribeOn(Schedulers.io())
646 .flatMap(
647 new Function<Integer, Single<Integer>>() {
648 @Override
649 public Single<Integer> apply(Integer v) throws Exception {
650 sleep();
651 return Single.<Integer>error(new TestException());
652 }
653 },
654 new Function<Throwable, Single<Integer>>() {
655 @Override
656 public Single<Integer> apply(Throwable v) throws Exception {
657 sleep();
658 return Single.<Integer>error(new TestException());
659 }
660 }
661 )
662 .test();
663
664 cb.await();
665
666 beforeCancelSleep(to);
667
668 to.dispose();
669
670 Thread.sleep(SLEEP_AFTER_CANCEL);
671
672 to.assertEmpty();
673
674 assertTrue(errors.toString(), errors.isEmpty());
675 } finally {
676 RxJavaPlugins.reset();
677 }
678 }
679
680 @Test
681 public void maybeSingle() throws Exception {
682 List<Throwable> errors = TestHelper.trackPluginErrors();
683 try {
684 TestObserver<Integer> to = Maybe.just(1)
685 .subscribeOn(Schedulers.io())
686 .flatMapSingle(new Function<Integer, Single<Integer>>() {
687 @Override
688 public Single<Integer> apply(Integer v) throws Exception {
689 sleep();
690 return Single.<Integer>error(new TestException());
691 }
692 })
693 .toSingle()
694 .test();
695
696 cb.await();
697
698 beforeCancelSleep(to);
699
700 to.dispose();
701
702 Thread.sleep(SLEEP_AFTER_CANCEL);
703
704 to.assertEmpty();
705
706 assertTrue(errors.toString(), errors.isEmpty());
707 } finally {
708 RxJavaPlugins.reset();
709 }
710 }
711
712 @Test
713 public void maybeSingle2() throws Exception {
714 List<Throwable> errors = TestHelper.trackPluginErrors();
715 try {
716 TestObserver<Integer> to = Maybe.just(1)
717 .subscribeOn(Schedulers.io())
718 .flatMapSingle(new Function<Integer, Single<Integer>>() {
719 @Override
720 public Single<Integer> apply(Integer v) throws Exception {
721 sleep();
722 return Single.<Integer>error(new TestException());
723 }
724 })
725 .test();
726
727 cb.await();
728
729 beforeCancelSleep(to);
730
731 to.dispose();
732
733 Thread.sleep(SLEEP_AFTER_CANCEL);
734
735 to.assertEmpty();
736
737 assertTrue(errors.toString(), errors.isEmpty());
738 } finally {
739 RxJavaPlugins.reset();
740 }
741 }
742
743 @Test
744 public void maybeMaybe() throws Exception {
745 List<Throwable> errors = TestHelper.trackPluginErrors();
746 try {
747 TestObserver<Integer> to = Maybe.just(1)
748 .subscribeOn(Schedulers.io())
749 .flatMap(new Function<Integer, Maybe<Integer>>() {
750 @Override
751 public Maybe<Integer> apply(Integer v) throws Exception {
752 sleep();
753 return Maybe.<Integer>error(new TestException());
754 }
755 })
756 .test();
757
758 cb.await();
759
760 beforeCancelSleep(to);
761
762 to.dispose();
763
764 Thread.sleep(SLEEP_AFTER_CANCEL);
765
766 to.assertEmpty();
767
768 assertTrue(errors.toString(), errors.isEmpty());
769 } finally {
770 RxJavaPlugins.reset();
771 }
772 }
773
774 @Test
775 public void maybePublisher() throws Exception {
776 List<Throwable> errors = TestHelper.trackPluginErrors();
777 try {
778 TestSubscriber<Integer> ts = Maybe.just(1)
779 .subscribeOn(Schedulers.io())
780 .flatMapPublisher(new Function<Integer, Publisher<Integer>>() {
781 @Override
782 public Publisher<Integer> apply(Integer v) throws Exception {
783 sleep();
784 return Flowable.<Integer>error(new TestException());
785 }
786 })
787 .test();
788
789 cb.await();
790
791 beforeCancelSleep(ts);
792
793 ts.cancel();
794
795 Thread.sleep(SLEEP_AFTER_CANCEL);
796
797 ts.assertEmpty();
798
799 assertTrue(errors.toString(), errors.isEmpty());
800 } finally {
801 RxJavaPlugins.reset();
802 }
803 }
804
805 @Test
806 public void maybeObservable() throws Exception {
807 List<Throwable> errors = TestHelper.trackPluginErrors();
808 try {
809 TestObserver<Integer> to = Maybe.just(1)
810 .subscribeOn(Schedulers.io())
811 .flatMapObservable(new Function<Integer, Observable<Integer>>() {
812 @Override
813 public Observable<Integer> apply(Integer v) throws Exception {
814 sleep();
815 return Observable.<Integer>error(new TestException());
816 }
817 })
818 .test();
819
820 cb.await();
821
822 beforeCancelSleep(to);
823
824 to.dispose();
825
826 Thread.sleep(SLEEP_AFTER_CANCEL);
827
828 to.assertEmpty();
829
830 assertTrue(errors.toString(), errors.isEmpty());
831 } finally {
832 RxJavaPlugins.reset();
833 }
834 }
835
836 @Test
837 public void maybeNotificationSuccess() throws Exception {
838 List<Throwable> errors = TestHelper.trackPluginErrors();
839 try {
840 TestObserver<Integer> to = Maybe.just(1)
841 .subscribeOn(Schedulers.io())
842 .flatMap(
843 new Function<Integer, Maybe<Integer>>() {
844 @Override
845 public Maybe<Integer> apply(Integer v) throws Exception {
846 sleep();
847 return Maybe.<Integer>error(new TestException());
848 }
849 },
850 new Function<Throwable, Maybe<Integer>>() {
851 @Override
852 public Maybe<Integer> apply(Throwable v) throws Exception {
853 sleep();
854 return Maybe.<Integer>error(new TestException());
855 }
856 },
857 new Supplier<Maybe<Integer>>() {
858 @Override
859 public Maybe<Integer> get() throws Exception {
860 sleep();
861 return Maybe.<Integer>error(new TestException());
862 }
863 }
864 )
865 .test();
866
867 cb.await();
868
869 beforeCancelSleep(to);
870
871 to.dispose();
872
873 Thread.sleep(SLEEP_AFTER_CANCEL);
874
875 to.assertEmpty();
876
877 assertTrue(errors.toString(), errors.isEmpty());
878 } finally {
879 RxJavaPlugins.reset();
880 }
881 }
882
883 @Test
884 public void maybeNotificationError() throws Exception {
885 List<Throwable> errors = TestHelper.trackPluginErrors();
886 try {
887 TestObserver<Integer> to = Maybe.<Integer>error(new TestException())
888 .subscribeOn(Schedulers.io())
889 .flatMap(
890 new Function<Integer, Maybe<Integer>>() {
891 @Override
892 public Maybe<Integer> apply(Integer v) throws Exception {
893 sleep();
894 return Maybe.<Integer>error(new TestException());
895 }
896 },
897 new Function<Throwable, Maybe<Integer>>() {
898 @Override
899 public Maybe<Integer> apply(Throwable v) throws Exception {
900 sleep();
901 return Maybe.<Integer>error(new TestException());
902 }
903 },
904 new Supplier<Maybe<Integer>>() {
905 @Override
906 public Maybe<Integer> get() throws Exception {
907 sleep();
908 return Maybe.<Integer>error(new TestException());
909 }
910 }
911 )
912 .test();
913
914 cb.await();
915
916 beforeCancelSleep(to);
917
918 to.dispose();
919
920 Thread.sleep(SLEEP_AFTER_CANCEL);
921
922 to.assertEmpty();
923
924 assertTrue(errors.toString(), errors.isEmpty());
925 } finally {
926 RxJavaPlugins.reset();
927 }
928 }
929
930 @Test
931 public void maybeNotificationEmpty() throws Exception {
932 List<Throwable> errors = TestHelper.trackPluginErrors();
933 try {
934 TestObserver<Integer> to = Maybe.<Integer>empty()
935 .subscribeOn(Schedulers.io())
936 .flatMap(
937 new Function<Integer, Maybe<Integer>>() {
938 @Override
939 public Maybe<Integer> apply(Integer v) throws Exception {
940 sleep();
941 return Maybe.<Integer>error(new TestException());
942 }
943 },
944 new Function<Throwable, Maybe<Integer>>() {
945 @Override
946 public Maybe<Integer> apply(Throwable v) throws Exception {
947 sleep();
948 return Maybe.<Integer>error(new TestException());
949 }
950 },
951 new Supplier<Maybe<Integer>>() {
952 @Override
953 public Maybe<Integer> get() throws Exception {
954 sleep();
955 return Maybe.<Integer>error(new TestException());
956 }
957 }
958 )
959 .test();
960
961 cb.await();
962
963 beforeCancelSleep(to);
964
965 to.dispose();
966
967 Thread.sleep(SLEEP_AFTER_CANCEL);
968
969 to.assertEmpty();
970
971 assertTrue(errors.toString(), errors.isEmpty());
972 } finally {
973 RxJavaPlugins.reset();
974 }
975 }
976
977 @Test
978 public void maybeCombiner() throws Exception {
979 List<Throwable> errors = TestHelper.trackPluginErrors();
980 try {
981 TestObserver<Integer> to = Maybe.just(1)
982 .subscribeOn(Schedulers.io())
983 .flatMap(new Function<Integer, Maybe<Integer>>() {
984 @Override
985 public Maybe<Integer> apply(Integer v) throws Exception {
986 sleep();
987 return Maybe.<Integer>error(new TestException());
988 }
989 }, (a, b) -> a + b)
990 .test();
991
992 cb.await();
993
994 beforeCancelSleep(to);
995
996 to.dispose();
997
998 Thread.sleep(SLEEP_AFTER_CANCEL);
999
1000 to.assertEmpty();
1001
1002 assertTrue(errors.toString(), errors.isEmpty());
1003 } finally {
1004 RxJavaPlugins.reset();
1005 }
1006 }
1007
1008 @Test
1009 public void maybeCompletable() throws Exception {
1010 List<Throwable> errors = TestHelper.trackPluginErrors();
1011 try {
1012 TestObserver<Void> to = Maybe.just(1)
1013 .subscribeOn(Schedulers.io())
1014 .flatMapCompletable(new Function<Integer, Completable>() {
1015 @Override
1016 public Completable apply(Integer v) throws Exception {
1017 sleep();
1018 return Completable.error(new TestException());
1019 }
1020 })
1021 .test();
1022
1023 cb.await();
1024
1025 beforeCancelSleep(to);
1026
1027 to.dispose();
1028
1029 Thread.sleep(SLEEP_AFTER_CANCEL);
1030
1031 to.assertEmpty();
1032
1033 assertTrue(errors.toString(), errors.isEmpty());
1034 } finally {
1035 RxJavaPlugins.reset();
1036 }
1037 }
1038
1039 @Test
1040 public void maybeCompletable2() throws Exception {
1041 List<Throwable> errors = TestHelper.trackPluginErrors();
1042 try {
1043 TestObserver<Void> to = Maybe.just(1)
1044 .subscribeOn(Schedulers.io())
1045 .flatMapCompletable(new Function<Integer, Completable>() {
1046 @Override
1047 public Completable apply(Integer v) throws Exception {
1048 sleep();
1049 return Completable.error(new TestException());
1050 }
1051 })
1052 .<Void>toMaybe()
1053 .test();
1054
1055 cb.await();
1056
1057 beforeCancelSleep(to);
1058
1059 to.dispose();
1060
1061 Thread.sleep(SLEEP_AFTER_CANCEL);
1062
1063 to.assertEmpty();
1064
1065 assertTrue(errors.toString(), errors.isEmpty());
1066 } finally {
1067 RxJavaPlugins.reset();
1068 }
1069 }
1070 }