Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableFlatMapCompletableTest.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
18 import java.util.List;
19 import java.util.concurrent.*;
20
21 import org.junit.Test;
22 import org.reactivestreams.Subscription;
23
24 import io.reactivex.rxjava3.core.*;
25 import io.reactivex.rxjava3.disposables.Disposable;
26 import io.reactivex.rxjava3.exceptions.*;
27 import io.reactivex.rxjava3.functions.*;
28 import io.reactivex.rxjava3.internal.functions.Functions;
29 import io.reactivex.rxjava3.observers.TestObserver;
30 import io.reactivex.rxjava3.operators.QueueFuseable;
31 import io.reactivex.rxjava3.operators.QueueSubscription;
32 import io.reactivex.rxjava3.processors.PublishProcessor;
33 import io.reactivex.rxjava3.schedulers.Schedulers;
34 import io.reactivex.rxjava3.subscribers.TestSubscriber;
35 import io.reactivex.rxjava3.testsupport.*;
36
37 public class FlowableFlatMapCompletableTest extends RxJavaTest {
38
39 @Test
40 public void normalFlowable() {
41 Flowable.range(1, 10)
42 .flatMapCompletable(new Function<Integer, CompletableSource>() {
43 @Override
44 public CompletableSource apply(Integer v) throws Exception {
45 return Completable.complete();
46 }
47 }).toFlowable()
48 .test()
49 .assertResult();
50 }
51
52 @Test
53 public void mapperThrowsFlowable() {
54 PublishProcessor<Integer> pp = PublishProcessor.create();
55
56 TestSubscriber<Integer> ts = pp
57 .flatMapCompletable(new Function<Integer, CompletableSource>() {
58 @Override
59 public CompletableSource apply(Integer v) throws Exception {
60 throw new TestException();
61 }
62 }).<Integer>toFlowable()
63 .test();
64
65 assertTrue(pp.hasSubscribers());
66
67 pp.onNext(1);
68
69 ts.assertFailure(TestException.class);
70
71 assertFalse(pp.hasSubscribers());
72 }
73
74 @Test
75 public void mapperReturnsNullFlowable() {
76 PublishProcessor<Integer> pp = PublishProcessor.create();
77
78 TestSubscriber<Integer> ts = pp
79 .flatMapCompletable(new Function<Integer, CompletableSource>() {
80 @Override
81 public CompletableSource apply(Integer v) throws Exception {
82 return null;
83 }
84 }).<Integer>toFlowable()
85 .test();
86
87 assertTrue(pp.hasSubscribers());
88
89 pp.onNext(1);
90
91 ts.assertFailure(NullPointerException.class);
92
93 assertFalse(pp.hasSubscribers());
94 }
95
96 @Test
97 public void normalDelayErrorFlowable() {
98 Flowable.range(1, 10)
99 .flatMapCompletable(new Function<Integer, CompletableSource>() {
100 @Override
101 public CompletableSource apply(Integer v) throws Exception {
102 return Completable.complete();
103 }
104 }, true, Integer.MAX_VALUE).toFlowable()
105 .test()
106 .assertResult();
107 }
108
109 @Test
110 public void normalAsyncFlowable() {
111 Flowable.range(1, 1000)
112 .flatMapCompletable(new Function<Integer, CompletableSource>() {
113 @Override
114 public CompletableSource apply(Integer v) throws Exception {
115 return Flowable.range(1, 100).subscribeOn(Schedulers.computation()).ignoreElements();
116 }
117 }).toFlowable()
118 .test()
119 .awaitDone(5, TimeUnit.SECONDS)
120 .assertResult();
121 }
122
123 @Test
124 public void normalAsyncFlowableMaxConcurrency() {
125 Flowable.range(1, 1000)
126 .flatMapCompletable(new Function<Integer, CompletableSource>() {
127 @Override
128 public CompletableSource apply(Integer v) throws Exception {
129 return Flowable.range(1, 100).subscribeOn(Schedulers.computation()).ignoreElements();
130 }
131 }, false, 3).toFlowable()
132 .test()
133 .awaitDone(5, TimeUnit.SECONDS)
134 .assertResult();
135 }
136
137 @Test
138 public void normalDelayErrorAllFlowable() {
139 TestSubscriberEx<Integer> ts = Flowable.range(1, 10).concatWith(Flowable.<Integer>error(new TestException()))
140 .flatMapCompletable(new Function<Integer, CompletableSource>() {
141 @Override
142 public CompletableSource apply(Integer v) throws Exception {
143 return Completable.error(new TestException());
144 }
145 }, true, Integer.MAX_VALUE).<Integer>toFlowable()
146 .to(TestHelper.<Integer>testConsumer())
147 .assertFailure(CompositeException.class);
148
149 List<Throwable> errors = TestHelper.compositeList(ts.errors().get(0));
150
151 for (int i = 0; i < 11; i++) {
152 TestHelper.assertError(errors, i, TestException.class);
153 }
154 }
155
156 @Test
157 public void normalDelayInnerErrorAllFlowable() {
158 TestSubscriberEx<Integer> ts = Flowable.range(1, 10)
159 .flatMapCompletable(new Function<Integer, CompletableSource>() {
160 @Override
161 public CompletableSource apply(Integer v) throws Exception {
162 return Completable.error(new TestException());
163 }
164 }, true, Integer.MAX_VALUE).<Integer>toFlowable()
165 .to(TestHelper.<Integer>testConsumer())
166 .assertFailure(CompositeException.class);
167
168 List<Throwable> errors = TestHelper.compositeList(ts.errors().get(0));
169
170 for (int i = 0; i < 10; i++) {
171 TestHelper.assertError(errors, i, TestException.class);
172 }
173 }
174
175 @Test
176 public void normalNonDelayErrorOuterFlowable() {
177 Flowable.range(1, 10).concatWith(Flowable.<Integer>error(new TestException()))
178 .flatMapCompletable(new Function<Integer, CompletableSource>() {
179 @Override
180 public CompletableSource apply(Integer v) throws Exception {
181 return Completable.complete();
182 }
183 }, false, Integer.MAX_VALUE).toFlowable()
184 .test()
185 .assertFailure(TestException.class);
186 }
187
188 @Test
189 public void fusedFlowable() {
190 TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.ANY);
191
192 Flowable.range(1, 10)
193 .flatMapCompletable(new Function<Integer, CompletableSource>() {
194 @Override
195 public CompletableSource apply(Integer v) throws Exception {
196 return Completable.complete();
197 }
198 }).<Integer>toFlowable()
199 .subscribe(ts);
200
201 ts
202 .assertFuseable()
203 .assertFusionMode(QueueFuseable.ASYNC)
204 .assertResult();
205 }
206
207 @Test
208 public void normal() {
209 Flowable.range(1, 10)
210 .flatMapCompletable(new Function<Integer, CompletableSource>() {
211 @Override
212 public CompletableSource apply(Integer v) throws Exception {
213 return Completable.complete();
214 }
215 })
216 .test()
217 .assertResult();
218 }
219
220 @Test
221 public void mapperThrows() {
222 PublishProcessor<Integer> pp = PublishProcessor.create();
223
224 TestObserver<Void> to = pp
225 .flatMapCompletable(new Function<Integer, CompletableSource>() {
226 @Override
227 public CompletableSource apply(Integer v) throws Exception {
228 throw new TestException();
229 }
230 })
231 .test();
232
233 assertTrue(pp.hasSubscribers());
234
235 pp.onNext(1);
236
237 to.assertFailure(TestException.class);
238
239 assertFalse(pp.hasSubscribers());
240 }
241
242 @Test
243 public void mapperReturnsNull() {
244 PublishProcessor<Integer> pp = PublishProcessor.create();
245
246 TestObserver<Void> to = pp
247 .flatMapCompletable(new Function<Integer, CompletableSource>() {
248 @Override
249 public CompletableSource apply(Integer v) throws Exception {
250 return null;
251 }
252 })
253 .test();
254
255 assertTrue(pp.hasSubscribers());
256
257 pp.onNext(1);
258
259 to.assertFailure(NullPointerException.class);
260
261 assertFalse(pp.hasSubscribers());
262 }
263
264 @Test
265 public void normalDelayError() {
266 Flowable.range(1, 10)
267 .flatMapCompletable(new Function<Integer, CompletableSource>() {
268 @Override
269 public CompletableSource apply(Integer v) throws Exception {
270 return Completable.complete();
271 }
272 }, true, Integer.MAX_VALUE)
273 .test()
274 .assertResult();
275 }
276
277 @Test
278 public void normalAsync() {
279 Flowable.range(1, 1000)
280 .flatMapCompletable(new Function<Integer, CompletableSource>() {
281 @Override
282 public CompletableSource apply(Integer v) throws Exception {
283 return Flowable.range(1, 100).subscribeOn(Schedulers.computation()).ignoreElements();
284 }
285 })
286 .test()
287 .awaitDone(5, TimeUnit.SECONDS)
288 .assertResult();
289 }
290
291 @Test
292 public void normalDelayErrorAll() {
293 TestObserverEx<Void> to = Flowable.range(1, 10).concatWith(Flowable.<Integer>error(new TestException()))
294 .flatMapCompletable(new Function<Integer, CompletableSource>() {
295 @Override
296 public CompletableSource apply(Integer v) throws Exception {
297 return Completable.error(new TestException());
298 }
299 }, true, Integer.MAX_VALUE)
300 .to(TestHelper.<Integer>testConsumer())
301 .assertFailure(CompositeException.class);
302
303 List<Throwable> errors = TestHelper.compositeList(to.errors().get(0));
304
305 for (int i = 0; i < 11; i++) {
306 TestHelper.assertError(errors, i, TestException.class);
307 }
308 }
309
310 @Test
311 public void normalDelayInnerErrorAll() {
312 TestObserverEx<Void> to = Flowable.range(1, 10)
313 .flatMapCompletable(new Function<Integer, CompletableSource>() {
314 @Override
315 public CompletableSource apply(Integer v) throws Exception {
316 return Completable.error(new TestException());
317 }
318 }, true, Integer.MAX_VALUE)
319 .to(TestHelper.<Integer>testConsumer())
320 .assertFailure(CompositeException.class);
321
322 List<Throwable> errors = TestHelper.compositeList(to.errors().get(0));
323
324 for (int i = 0; i < 10; i++) {
325 TestHelper.assertError(errors, i, TestException.class);
326 }
327 }
328
329 @Test
330 public void normalNonDelayErrorOuter() {
331 Flowable.range(1, 10).concatWith(Flowable.<Integer>error(new TestException()))
332 .flatMapCompletable(new Function<Integer, CompletableSource>() {
333 @Override
334 public CompletableSource apply(Integer v) throws Exception {
335 return Completable.complete();
336 }
337 }, false, Integer.MAX_VALUE)
338 .test()
339 .assertFailure(TestException.class);
340 }
341
342 @Test
343 public void fused() {
344 TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.ANY);
345
346 Flowable.range(1, 10)
347 .flatMapCompletable(new Function<Integer, CompletableSource>() {
348 @Override
349 public CompletableSource apply(Integer v) throws Exception {
350 return Completable.complete();
351 }
352 })
353 .<Integer>toFlowable()
354 .subscribe(ts);
355
356 ts
357 .assertFuseable()
358 .assertFusionMode(QueueFuseable.ASYNC)
359 .assertResult();
360 }
361
362 @Test
363 public void disposed() {
364 TestHelper.checkDisposed(Flowable.range(1, 10)
365 .flatMapCompletable(new Function<Integer, CompletableSource>() {
366 @Override
367 public CompletableSource apply(Integer v) throws Exception {
368 return Completable.complete();
369 }
370 }));
371 }
372
373 @Test
374 public void normalAsyncMaxConcurrency() {
375 Flowable.range(1, 1000)
376 .flatMapCompletable(new Function<Integer, CompletableSource>() {
377 @Override
378 public CompletableSource apply(Integer v) throws Exception {
379 return Flowable.range(1, 100).subscribeOn(Schedulers.computation()).ignoreElements();
380 }
381 }, false, 3)
382 .test()
383 .awaitDone(5, TimeUnit.SECONDS)
384 .assertResult();
385 }
386
387 @Test
388 public void disposedFlowable() {
389 TestHelper.checkDisposed(Flowable.range(1, 10)
390 .flatMapCompletable(new Function<Integer, CompletableSource>() {
391 @Override
392 public CompletableSource apply(Integer v) throws Exception {
393 return Completable.complete();
394 }
395 }).toFlowable());
396 }
397
398 @Test
399 public void badSource() {
400 TestHelper.checkBadSourceFlowable(new Function<Flowable<Integer>, Object>() {
401 @Override
402 public Object apply(Flowable<Integer> f) throws Exception {
403 return f.flatMapCompletable(new Function<Integer, CompletableSource>() {
404 @Override
405 public CompletableSource apply(Integer v) throws Exception {
406 return Completable.complete();
407 }
408 });
409 }
410 }, false, 1, null);
411 }
412
413 @Test
414 public void fusedInternalsFlowable() {
415 Flowable.range(1, 10)
416 .flatMapCompletable(new Function<Integer, CompletableSource>() {
417 @Override
418 public CompletableSource apply(Integer v) throws Exception {
419 return Completable.complete();
420 }
421 })
422 .toFlowable()
423 .subscribe(new FlowableSubscriber<Object>() {
424 @Override
425 public void onSubscribe(Subscription s) {
426 QueueSubscription<?> qs = (QueueSubscription<?>)s;
427 try {
428 assertNull(qs.poll());
429 } catch (Throwable ex) {
430 throw new RuntimeException(ex);
431 }
432 assertTrue(qs.isEmpty());
433 qs.clear();
434 }
435
436 @Override
437 public void onNext(Object t) {
438 }
439
440 @Override
441 public void onError(Throwable t) {
442 }
443
444 @Override
445 public void onComplete() {
446 }
447 });
448 }
449
450 @Test
451 public void innerObserverFlowable() {
452 Flowable.range(1, 3)
453 .flatMapCompletable(new Function<Integer, CompletableSource>() {
454 @Override
455 public CompletableSource apply(Integer v) throws Exception {
456 return new Completable() {
457 @Override
458 protected void subscribeActual(CompletableObserver observer) {
459 observer.onSubscribe(Disposable.empty());
460
461 assertFalse(((Disposable)observer).isDisposed());
462
463 ((Disposable)observer).dispose();
464
465 assertTrue(((Disposable)observer).isDisposed());
466 }
467 };
468 }
469 })
470 .toFlowable()
471 .test();
472 }
473
474 @Test
475 public void badSourceFlowable() {
476 TestHelper.checkBadSourceFlowable(new Function<Flowable<Integer>, Object>() {
477 @Override
478 public Object apply(Flowable<Integer> f) throws Exception {
479 return f.flatMapCompletable(new Function<Integer, CompletableSource>() {
480 @Override
481 public CompletableSource apply(Integer v) throws Exception {
482 return Completable.complete();
483 }
484 }).toFlowable();
485 }
486 }, false, 1, null);
487 }
488
489 @Test
490 public void innerObserver() {
491 Flowable.range(1, 3)
492 .flatMapCompletable(new Function<Integer, CompletableSource>() {
493 @Override
494 public CompletableSource apply(Integer v) throws Exception {
495 return new Completable() {
496 @Override
497 protected void subscribeActual(CompletableObserver observer) {
498 observer.onSubscribe(Disposable.empty());
499
500 assertFalse(((Disposable)observer).isDisposed());
501
502 ((Disposable)observer).dispose();
503
504 assertTrue(((Disposable)observer).isDisposed());
505 }
506 };
507 }
508 })
509 .test();
510 }
511
512 @Test
513 public void delayErrorMaxConcurrency() {
514 Flowable.range(1, 3)
515 .flatMapCompletable(new Function<Integer, CompletableSource>() {
516 @Override
517 public CompletableSource apply(Integer v) throws Exception {
518 if (v == 2) {
519 return Completable.error(new TestException());
520 }
521 return Completable.complete();
522 }
523 }, true, 1)
524 .toFlowable()
525 .test()
526 .assertFailure(TestException.class);
527 }
528
529 @Test
530 public void delayErrorMaxConcurrencyCompletable() {
531 Flowable.range(1, 3)
532 .flatMapCompletable(new Function<Integer, CompletableSource>() {
533 @Override
534 public CompletableSource apply(Integer v) throws Exception {
535 if (v == 2) {
536 return Completable.error(new TestException());
537 }
538 return Completable.complete();
539 }
540 }, true, 1)
541 .test()
542 .assertFailure(TestException.class);
543 }
544
545 @Test
546 public void asyncMaxConcurrency() {
547 for (int itemCount = 1; itemCount <= 100000; itemCount *= 10) {
548 for (int concurrency = 1; concurrency <= 256; concurrency *= 2) {
549 Flowable.range(1, itemCount)
550 .flatMapCompletable(
551 Functions.justFunction(Completable.complete()
552 .subscribeOn(Schedulers.computation()))
553 , false, concurrency)
554 .test()
555 .withTag("itemCount=" + itemCount + ", concurrency=" + concurrency)
556 .awaitDone(5, TimeUnit.SECONDS)
557 .assertResult();
558 }
559 }
560 }
561
562 @Test
563 public void undeliverableUponCancel() {
564 TestHelper.checkUndeliverableUponCancel(new FlowableConverter<Integer, Completable>() {
565 @Override
566 public Completable apply(Flowable<Integer> upstream) {
567 return upstream.flatMapCompletable(new Function<Integer, Completable>() {
568 @Override
569 public Completable apply(Integer v) throws Throwable {
570 return Completable.complete().hide();
571 }
572 });
573 }
574 });
575 }
576
577 @Test
578 public void undeliverableUponCancelDelayError() {
579 TestHelper.checkUndeliverableUponCancel(new FlowableConverter<Integer, Completable>() {
580 @Override
581 public Completable apply(Flowable<Integer> upstream) {
582 return upstream.flatMapCompletable(new Function<Integer, Completable>() {
583 @Override
584 public Completable apply(Integer v) throws Throwable {
585 return Completable.complete().hide();
586 }
587 }, true, 2);
588 }
589 });
590 }
591
592 @Test
593 public void doubleOnSubscribe() {
594 TestHelper.checkDoubleOnSubscribeFlowable(f -> f.flatMapCompletable(v -> Completable.never()).toFlowable());
595 }
596
597 @Test
598 public void doubleOnSubscribeCompletable() {
599 TestHelper.checkDoubleOnSubscribeFlowableToCompletable(f -> f.flatMapCompletable(v -> Completable.never()));
600 }
601
602 @Test
603 public void cancelWhileMapping() throws Throwable {
604 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
605 PublishProcessor<Integer> pp1 = PublishProcessor.create();
606
607 TestSubscriber<Object> ts = new TestSubscriber<>();
608 CountDownLatch cdl = new CountDownLatch(1);
609
610 pp1.flatMapCompletable(v -> {
611 TestHelper.raceOther(() -> {
612 ts.cancel();
613 }, cdl);
614 return Completable.complete();
615 })
616 .toFlowable()
617 .subscribe(ts);
618
619 pp1.onNext(1);
620
621 cdl.await();
622 }
623 }
624
625 @Test
626 public void cancelWhileMappingCompletable() throws Throwable {
627 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
628 PublishProcessor<Integer> pp1 = PublishProcessor.create();
629
630 TestObserver<Void> to = new TestObserver<>();
631 CountDownLatch cdl = new CountDownLatch(1);
632
633 pp1.flatMapCompletable(v -> {
634 TestHelper.raceOther(() -> {
635 to.dispose();
636 }, cdl);
637 return Completable.complete();
638 })
639 .subscribe(to);
640
641 pp1.onNext(1);
642
643 cdl.await();
644 }
645 }
646 }