Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableConcatTest.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.io.IOException;
21 import java.lang.reflect.Method;
22 import java.util.*;
23 import java.util.concurrent.*;
24 import java.util.concurrent.atomic.*;
25
26 import org.junit.Test;
27 import org.mockito.InOrder;
28 import org.reactivestreams.*;
29
30 import io.reactivex.rxjava3.core.*;
31 import io.reactivex.rxjava3.disposables.Disposable;
32 import io.reactivex.rxjava3.exceptions.*;
33 import io.reactivex.rxjava3.functions.*;
34 import io.reactivex.rxjava3.internal.functions.Functions;
35 import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription;
36 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
37 import io.reactivex.rxjava3.processors.*;
38 import io.reactivex.rxjava3.schedulers.*;
39 import io.reactivex.rxjava3.subscribers.*;
40 import io.reactivex.rxjava3.testsupport.*;
41
42 public class FlowableConcatTest {
43
44 @Test
45 public void concat() {
46 Subscriber<String> subscriber = TestHelper.mockSubscriber();
47
48 final String[] o = { "1", "3", "5", "7" };
49 final String[] e = { "2", "4", "6" };
50
51 final Flowable<String> odds = Flowable.fromArray(o);
52 final Flowable<String> even = Flowable.fromArray(e);
53
54 Flowable<String> concat = Flowable.concat(odds, even);
55 concat.subscribe(subscriber);
56
57 verify(subscriber, times(7)).onNext(anyString());
58 }
59
60 @Test
61 public void concatWithList() {
62 Subscriber<String> subscriber = TestHelper.mockSubscriber();
63
64 final String[] o = { "1", "3", "5", "7" };
65 final String[] e = { "2", "4", "6" };
66
67 final Flowable<String> odds = Flowable.fromArray(o);
68 final Flowable<String> even = Flowable.fromArray(e);
69 final List<Flowable<String>> list = new ArrayList<>();
70 list.add(odds);
71 list.add(even);
72 Flowable<String> concat = Flowable.concat(Flowable.fromIterable(list));
73 concat.subscribe(subscriber);
74
75 verify(subscriber, times(7)).onNext(anyString());
76 }
77
78 @Test
79 public void concatObservableOfObservables() {
80 Subscriber<String> subscriber = TestHelper.mockSubscriber();
81
82 final String[] o = { "1", "3", "5", "7" };
83 final String[] e = { "2", "4", "6" };
84
85 final Flowable<String> odds = Flowable.fromArray(o);
86 final Flowable<String> even = Flowable.fromArray(e);
87
88 Flowable<Flowable<String>> flowableOfFlowables = Flowable.unsafeCreate(new Publisher<Flowable<String>>() {
89
90 @Override
91 public void subscribe(Subscriber<? super Flowable<String>> subscriber) {
92 subscriber.onSubscribe(new BooleanSubscription());
93 // simulate what would happen in an observable
94 subscriber.onNext(odds);
95 subscriber.onNext(even);
96 subscriber.onComplete();
97 }
98
99 });
100 Flowable<String> concat = Flowable.concat(flowableOfFlowables);
101
102 concat.subscribe(subscriber);
103
104 verify(subscriber, times(7)).onNext(anyString());
105 }
106
107 /**
108 * Simple concat of 2 asynchronous observables ensuring it emits in correct order.
109 */
110 @Test
111 public void simpleAsyncConcat() {
112 Subscriber<String> subscriber = TestHelper.mockSubscriber();
113
114 TestObservable<String> o1 = new TestObservable<>("one", "two", "three");
115 TestObservable<String> o2 = new TestObservable<>("four", "five", "six");
116
117 Flowable.concat(Flowable.unsafeCreate(o1), Flowable.unsafeCreate(o2)).subscribe(subscriber);
118
119 try {
120 // wait for async observables to complete
121 o1.t.join();
122 o2.t.join();
123 } catch (Throwable e) {
124 throw new RuntimeException("failed waiting on threads");
125 }
126
127 InOrder inOrder = inOrder(subscriber);
128 inOrder.verify(subscriber, times(1)).onNext("one");
129 inOrder.verify(subscriber, times(1)).onNext("two");
130 inOrder.verify(subscriber, times(1)).onNext("three");
131 inOrder.verify(subscriber, times(1)).onNext("four");
132 inOrder.verify(subscriber, times(1)).onNext("five");
133 inOrder.verify(subscriber, times(1)).onNext("six");
134 }
135
136 @Test
137 public void nestedAsyncConcatLoop() throws Throwable {
138 for (int i = 0; i < 500; i++) {
139 if (i % 10 == 0) {
140 System.out.println("testNestedAsyncConcat >> " + i);
141 }
142 nestedAsyncConcat();
143 }
144 }
145
146 /**
147 * Test an async Flowable that emits more async Observables.
148 * @throws InterruptedException if the test is interrupted
149 */
150 @Test
151 public void nestedAsyncConcat() throws InterruptedException {
152 Subscriber<String> subscriber = TestHelper.mockSubscriber();
153
154 final TestObservable<String> o1 = new TestObservable<>("one", "two", "three");
155 final TestObservable<String> o2 = new TestObservable<>("four", "five", "six");
156 final TestObservable<String> o3 = new TestObservable<>("seven", "eight", "nine");
157 final CountDownLatch allowThird = new CountDownLatch(1);
158
159 final AtomicReference<Thread> parent = new AtomicReference<>();
160 final CountDownLatch parentHasStarted = new CountDownLatch(1);
161 final CountDownLatch parentHasFinished = new CountDownLatch(1);
162
163 Flowable<Flowable<String>> observableOfObservables = Flowable.unsafeCreate(new Publisher<Flowable<String>>() {
164
165 @Override
166 public void subscribe(final Subscriber<? super Flowable<String>> subscriber) {
167 final Disposable d = Disposable.empty();
168 subscriber.onSubscribe(new Subscription() {
169 @Override
170 public void request(long n) {
171
172 }
173
174 @Override
175 public void cancel() {
176 d.dispose();
177 }
178 });
179 parent.set(new Thread(new Runnable() {
180
181 @Override
182 public void run() {
183 try {
184 // emit first
185 if (!d.isDisposed()) {
186 System.out.println("Emit o1");
187 subscriber.onNext(Flowable.unsafeCreate(o1));
188 }
189 // emit second
190 if (!d.isDisposed()) {
191 System.out.println("Emit o2");
192 subscriber.onNext(Flowable.unsafeCreate(o2));
193 }
194
195 // wait until sometime later and emit third
196 try {
197 allowThird.await();
198 } catch (InterruptedException e) {
199 subscriber.onError(e);
200 }
201 if (!d.isDisposed()) {
202 System.out.println("Emit o3");
203 subscriber.onNext(Flowable.unsafeCreate(o3));
204 }
205
206 } catch (Throwable e) {
207 subscriber.onError(e);
208 } finally {
209 System.out.println("Done parent Flowable");
210 subscriber.onComplete();
211 parentHasFinished.countDown();
212 }
213 }
214 }));
215 parent.get().start();
216 parentHasStarted.countDown();
217 }
218 });
219
220 Flowable.concat(observableOfObservables).subscribe(subscriber);
221
222 // wait for parent to start
223 parentHasStarted.await();
224
225 try {
226 // wait for first 2 async observables to complete
227 System.out.println("Thread1 is starting ... waiting for it to complete ...");
228 o1.waitForThreadDone();
229 System.out.println("Thread2 is starting ... waiting for it to complete ...");
230 o2.waitForThreadDone();
231 } catch (Throwable e) {
232 throw new RuntimeException("failed waiting on threads", e);
233 }
234
235 InOrder inOrder = inOrder(subscriber);
236 inOrder.verify(subscriber, times(1)).onNext("one");
237 inOrder.verify(subscriber, times(1)).onNext("two");
238 inOrder.verify(subscriber, times(1)).onNext("three");
239 inOrder.verify(subscriber, times(1)).onNext("four");
240 inOrder.verify(subscriber, times(1)).onNext("five");
241 inOrder.verify(subscriber, times(1)).onNext("six");
242 // we shouldn't have the following 3 yet
243 inOrder.verify(subscriber, never()).onNext("seven");
244 inOrder.verify(subscriber, never()).onNext("eight");
245 inOrder.verify(subscriber, never()).onNext("nine");
246 // we should not be completed yet
247 verify(subscriber, never()).onComplete();
248 verify(subscriber, never()).onError(any(Throwable.class));
249
250 // now allow the third
251 allowThird.countDown();
252
253 try {
254 // wait for 3rd to complete
255 o3.waitForThreadDone();
256 } catch (Throwable e) {
257 throw new RuntimeException("failed waiting on threads", e);
258 }
259
260 try {
261 // wait for the parent to complete
262 if (!parentHasFinished.await(5, TimeUnit.SECONDS)) {
263 fail("Parent didn't finish within the time limit");
264 }
265 } catch (Throwable e) {
266 throw new RuntimeException("failed waiting on threads", e);
267 }
268
269 inOrder.verify(subscriber, times(1)).onNext("seven");
270 inOrder.verify(subscriber, times(1)).onNext("eight");
271 inOrder.verify(subscriber, times(1)).onNext("nine");
272
273 verify(subscriber, never()).onError(any(Throwable.class));
274 inOrder.verify(subscriber, times(1)).onComplete();
275 }
276
277 @Test
278 public void blockedObservableOfObservables() {
279 Subscriber<String> subscriber = TestHelper.mockSubscriber();
280
281 final String[] o = { "1", "3", "5", "7" };
282 final String[] e = { "2", "4", "6" };
283 final Flowable<String> odds = Flowable.fromArray(o);
284 final Flowable<String> even = Flowable.fromArray(e);
285 final CountDownLatch callOnce = new CountDownLatch(1);
286 final CountDownLatch okToContinue = new CountDownLatch(1);
287
288 TestObservable<Flowable<String>> observableOfObservables = new TestObservable<>(callOnce, okToContinue, odds, even);
289 Flowable<String> concatF = Flowable.concat(Flowable.unsafeCreate(observableOfObservables));
290 concatF.subscribe(subscriber);
291 try {
292 //Block main thread to allow observables to serve up o1.
293 callOnce.await();
294 } catch (Throwable ex) {
295 ex.printStackTrace();
296 fail(ex.getMessage());
297 }
298 // The concated observable should have served up all of the odds.
299 verify(subscriber, times(1)).onNext("1");
300 verify(subscriber, times(1)).onNext("3");
301 verify(subscriber, times(1)).onNext("5");
302 verify(subscriber, times(1)).onNext("7");
303
304 try {
305 // unblock observables so it can serve up o2 and complete
306 okToContinue.countDown();
307 observableOfObservables.t.join();
308 } catch (Throwable ex) {
309 ex.printStackTrace();
310 fail(ex.getMessage());
311 }
312 // The concatenated observable should now have served up all the evens.
313 verify(subscriber, times(1)).onNext("2");
314 verify(subscriber, times(1)).onNext("4");
315 verify(subscriber, times(1)).onNext("6");
316 }
317
318 @Test
319 public void concatConcurrentWithInfinity() {
320 final TestObservable<String> w1 = new TestObservable<>("one", "two", "three");
321 //This observable will send "hello" MAX_VALUE time.
322 final TestObservable<String> w2 = new TestObservable<>("hello", Integer.MAX_VALUE);
323
324 Subscriber<String> subscriber = TestHelper.mockSubscriber();
325
326 TestObservable<Flowable<String>> observableOfObservables = new TestObservable<>(Flowable.unsafeCreate(w1), Flowable.unsafeCreate(w2));
327 Flowable<String> concatF = Flowable.concat(Flowable.unsafeCreate(observableOfObservables));
328
329 concatF.take(50).subscribe(subscriber);
330
331 //Wait for the thread to start up.
332 try {
333 w1.waitForThreadDone();
334 w2.waitForThreadDone();
335 } catch (InterruptedException e) {
336 e.printStackTrace();
337 }
338
339 InOrder inOrder = inOrder(subscriber);
340 inOrder.verify(subscriber, times(1)).onNext("one");
341 inOrder.verify(subscriber, times(1)).onNext("two");
342 inOrder.verify(subscriber, times(1)).onNext("three");
343 inOrder.verify(subscriber, times(47)).onNext("hello");
344 verify(subscriber, times(1)).onComplete();
345 verify(subscriber, never()).onError(any(Throwable.class));
346 }
347
348 @Test
349 public void concatNonBlockingObservables() {
350
351 final CountDownLatch okToContinueW1 = new CountDownLatch(1);
352 final CountDownLatch okToContinueW2 = new CountDownLatch(1);
353
354 final TestObservable<String> w1 = new TestObservable<>(null, okToContinueW1, "one", "two", "three");
355 final TestObservable<String> w2 = new TestObservable<>(null, okToContinueW2, "four", "five", "six");
356
357 Subscriber<String> subscriber = TestHelper.mockSubscriber();
358
359 Flowable<Flowable<String>> observableOfObservables = Flowable.unsafeCreate(new Publisher<Flowable<String>>() {
360
361 @Override
362 public void subscribe(Subscriber<? super Flowable<String>> subscriber) {
363 subscriber.onSubscribe(new BooleanSubscription());
364 // simulate what would happen in an observable
365 subscriber.onNext(Flowable.unsafeCreate(w1));
366 subscriber.onNext(Flowable.unsafeCreate(w2));
367 subscriber.onComplete();
368 }
369
370 });
371 Flowable<String> concat = Flowable.concat(observableOfObservables);
372 concat.subscribe(subscriber);
373
374 verify(subscriber, times(0)).onComplete();
375
376 try {
377 // release both threads
378 okToContinueW1.countDown();
379 okToContinueW2.countDown();
380 // wait for both to finish
381 w1.t.join();
382 w2.t.join();
383 } catch (InterruptedException e) {
384 e.printStackTrace();
385 }
386
387 InOrder inOrder = inOrder(subscriber);
388 inOrder.verify(subscriber, times(1)).onNext("one");
389 inOrder.verify(subscriber, times(1)).onNext("two");
390 inOrder.verify(subscriber, times(1)).onNext("three");
391 inOrder.verify(subscriber, times(1)).onNext("four");
392 inOrder.verify(subscriber, times(1)).onNext("five");
393 inOrder.verify(subscriber, times(1)).onNext("six");
394 verify(subscriber, times(1)).onComplete();
395
396 }
397
398 /**
399 * Test unsubscribing the concatenated Flowable in a single thread.
400 */
401 @Test
402 public void concatUnsubscribe() {
403 final CountDownLatch callOnce = new CountDownLatch(1);
404 final CountDownLatch okToContinue = new CountDownLatch(1);
405 final TestObservable<String> w1 = new TestObservable<>("one", "two", "three");
406 final TestObservable<String> w2 = new TestObservable<>(callOnce, okToContinue, "four", "five", "six");
407
408 Subscriber<String> subscriber = TestHelper.mockSubscriber();
409 TestSubscriber<String> ts = new TestSubscriber<>(subscriber, 0L);
410
411 final Flowable<String> concat = Flowable.concat(Flowable.unsafeCreate(w1), Flowable.unsafeCreate(w2));
412
413 try {
414 // Subscribe
415 concat.subscribe(ts);
416 //Block main thread to allow observable "w1" to complete and observable "w2" to call onNext once.
417 callOnce.await();
418 // Unsubcribe
419 ts.cancel();
420 //Unblock the observable to continue.
421 okToContinue.countDown();
422 w1.t.join();
423 w2.t.join();
424 } catch (Throwable e) {
425 e.printStackTrace();
426 fail(e.getMessage());
427 }
428
429 InOrder inOrder = inOrder(subscriber);
430 inOrder.verify(subscriber, times(1)).onNext("one");
431 inOrder.verify(subscriber, times(1)).onNext("two");
432 inOrder.verify(subscriber, times(1)).onNext("three");
433 inOrder.verify(subscriber, times(1)).onNext("four");
434 inOrder.verify(subscriber, never()).onNext("five");
435 inOrder.verify(subscriber, never()).onNext("six");
436 inOrder.verify(subscriber, never()).onComplete();
437
438 }
439
440 /**
441 * All observables will be running in different threads so subscribe() is unblocked. CountDownLatch is only used in order to call unsubscribe() in a predictable manner.
442 */
443 @Test
444 public void concatUnsubscribeConcurrent() {
445 final CountDownLatch callOnce = new CountDownLatch(1);
446 final CountDownLatch okToContinue = new CountDownLatch(1);
447 final TestObservable<String> w1 = new TestObservable<>("one", "two", "three");
448 final TestObservable<String> w2 = new TestObservable<>(callOnce, okToContinue, "four", "five", "six");
449
450 Subscriber<String> subscriber = TestHelper.mockSubscriber();
451 TestSubscriber<String> ts = new TestSubscriber<>(subscriber, 0L);
452
453 TestObservable<Flowable<String>> observableOfObservables = new TestObservable<>(Flowable.unsafeCreate(w1), Flowable.unsafeCreate(w2));
454 Flowable<String> concatF = Flowable.concat(Flowable.unsafeCreate(observableOfObservables));
455
456 concatF.subscribe(ts);
457
458 try {
459 //Block main thread to allow observable "w1" to complete and observable "w2" to call onNext exactly once.
460 callOnce.await();
461 //"four" from w2 has been processed by onNext()
462 ts.cancel();
463 //"five" and "six" will NOT be processed by onNext()
464 //Unblock the observable to continue.
465 okToContinue.countDown();
466 w1.t.join();
467 w2.t.join();
468 } catch (Throwable e) {
469 e.printStackTrace();
470 fail(e.getMessage());
471 }
472
473 InOrder inOrder = inOrder(subscriber);
474 inOrder.verify(subscriber, times(1)).onNext("one");
475 inOrder.verify(subscriber, times(1)).onNext("two");
476 inOrder.verify(subscriber, times(1)).onNext("three");
477 inOrder.verify(subscriber, times(1)).onNext("four");
478 inOrder.verify(subscriber, never()).onNext("five");
479 inOrder.verify(subscriber, never()).onNext("six");
480 verify(subscriber, never()).onComplete();
481 verify(subscriber, never()).onError(any(Throwable.class));
482 }
483
484 private static class TestObservable<T> implements Publisher<T> {
485
486 private final Subscription s = new Subscription() {
487
488 @Override
489 public void request(long n) {
490
491 }
492
493 @Override
494 public void cancel() {
495 subscribed = false;
496 }
497 };
498 private final List<T> values;
499 private Thread t;
500 private int count;
501 private volatile boolean subscribed = true;
502 private final CountDownLatch once;
503 private final CountDownLatch okToContinue;
504 private final CountDownLatch threadHasStarted = new CountDownLatch(1);
505 private final T seed;
506 private final int size;
507
508 @SafeVarargs
509 TestObservable(T... values) {
510 this(null, null, values);
511 }
512
513 @SafeVarargs
514 TestObservable(CountDownLatch once, CountDownLatch okToContinue, T... values) {
515 this.values = Arrays.asList(values);
516 this.size = this.values.size();
517 this.once = once;
518 this.okToContinue = okToContinue;
519 this.seed = null;
520 }
521
522 TestObservable(T seed, int size) {
523 values = null;
524 once = null;
525 okToContinue = null;
526 this.seed = seed;
527 this.size = size;
528 }
529
530 @Override
531 public void subscribe(final Subscriber<? super T> subscriber) {
532 subscriber.onSubscribe(s);
533 t = new Thread(new Runnable() {
534
535 @Override
536 public void run() {
537 try {
538 while (count < size && subscribed) {
539 if (null != values) {
540 subscriber.onNext(values.get(count));
541 } else {
542 subscriber.onNext(seed);
543 }
544 count++;
545 //Unblock the main thread to call unsubscribe.
546 if (null != once) {
547 once.countDown();
548 }
549 //Block until the main thread has called unsubscribe.
550 if (null != okToContinue) {
551 okToContinue.await(5, TimeUnit.SECONDS);
552 }
553 }
554 if (subscribed) {
555 subscriber.onComplete();
556 }
557 } catch (InterruptedException e) {
558 e.printStackTrace();
559 fail(e.getMessage());
560 }
561 }
562
563 });
564 t.start();
565 threadHasStarted.countDown();
566 }
567
568 void waitForThreadDone() throws InterruptedException {
569 threadHasStarted.await();
570 t.join();
571 }
572 }
573
574 @Test
575 public void multipleObservers() {
576 Subscriber<Object> subscriber1 = TestHelper.mockSubscriber();
577 Subscriber<Object> subscriber2 = TestHelper.mockSubscriber();
578
579 TestScheduler s = new TestScheduler();
580
581 Flowable<Long> timer = Flowable.interval(500, TimeUnit.MILLISECONDS, s).take(2);
582 Flowable<Long> f = Flowable.concat(timer, timer);
583
584 f.subscribe(subscriber1);
585 f.subscribe(subscriber2);
586
587 InOrder inOrder1 = inOrder(subscriber1);
588 InOrder inOrder2 = inOrder(subscriber2);
589
590 s.advanceTimeBy(500, TimeUnit.MILLISECONDS);
591
592 inOrder1.verify(subscriber1, times(1)).onNext(0L);
593 inOrder2.verify(subscriber2, times(1)).onNext(0L);
594
595 s.advanceTimeBy(500, TimeUnit.MILLISECONDS);
596
597 inOrder1.verify(subscriber1, times(1)).onNext(1L);
598 inOrder2.verify(subscriber2, times(1)).onNext(1L);
599
600 s.advanceTimeBy(500, TimeUnit.MILLISECONDS);
601
602 inOrder1.verify(subscriber1, times(1)).onNext(0L);
603 inOrder2.verify(subscriber2, times(1)).onNext(0L);
604
605 s.advanceTimeBy(500, TimeUnit.MILLISECONDS);
606
607 inOrder1.verify(subscriber1, times(1)).onNext(1L);
608 inOrder2.verify(subscriber2, times(1)).onNext(1L);
609
610 inOrder1.verify(subscriber1, times(1)).onComplete();
611 inOrder2.verify(subscriber2, times(1)).onComplete();
612
613 verify(subscriber1, never()).onError(any(Throwable.class));
614 verify(subscriber2, never()).onError(any(Throwable.class));
615 }
616
617 @Test
618 public void concatVeryLongObservableOfObservables() {
619 final int n = 10000;
620 Flowable<Flowable<Integer>> source = Flowable.range(0, n).map(new Function<Integer, Flowable<Integer>>() {
621 @Override
622 public Flowable<Integer> apply(Integer v) {
623 return Flowable.just(v);
624 }
625 });
626
627 Single<List<Integer>> result = Flowable.concat(source).toList();
628
629 SingleObserver<List<Integer>> o = TestHelper.mockSingleObserver();
630 InOrder inOrder = inOrder(o);
631
632 result.subscribe(o);
633
634 List<Integer> list = new ArrayList<>(n);
635 for (int i = 0; i < n; i++) {
636 list.add(i);
637 }
638 inOrder.verify(o).onSuccess(list);
639 verify(o, never()).onError(any(Throwable.class));
640 }
641
642 @Test
643 public void concatVeryLongObservableOfObservablesTakeHalf() {
644 final int n = 10000;
645 Flowable<Flowable<Integer>> source = Flowable.range(0, n).map(new Function<Integer, Flowable<Integer>>() {
646 @Override
647 public Flowable<Integer> apply(Integer v) {
648 return Flowable.just(v);
649 }
650 });
651
652 Single<List<Integer>> result = Flowable.concat(source).take(n / 2).toList();
653
654 SingleObserver<List<Integer>> o = TestHelper.mockSingleObserver();
655 InOrder inOrder = inOrder(o);
656
657 result.subscribe(o);
658
659 List<Integer> list = new ArrayList<>(n);
660 for (int i = 0; i < n / 2; i++) {
661 list.add(i);
662 }
663 inOrder.verify(o).onSuccess(list);
664 verify(o, never()).onError(any(Throwable.class));
665 }
666
667 @Test
668 public void concatOuterBackpressure() {
669 assertEquals(1,
670 (int) Flowable.<Integer> empty()
671 .concatWith(Flowable.just(1))
672 .take(1)
673 .blockingSingle());
674 }
675
676 @Test
677 public void innerBackpressureWithAlignedBoundaries() {
678 TestSubscriber<Integer> ts = new TestSubscriber<>();
679 Flowable.range(0, Flowable.bufferSize() * 2)
680 .concatWith(Flowable.range(0, Flowable.bufferSize() * 2))
681 .observeOn(Schedulers.computation()) // observeOn has a backpressured RxRingBuffer
682 .subscribe(ts);
683
684 ts.awaitDone(5, TimeUnit.SECONDS);
685 ts.assertNoErrors();
686 assertEquals(Flowable.bufferSize() * 4, ts.values().size());
687 }
688
689 /*
690 * Testing without counts aligned with buffer sizes because concat must prevent the subscription
691 * to the next Flowable if request == 0 which can happen at the end of a subscription
692 * if the request size == emitted size. It needs to delay subscription until the next request when aligned,
693 * when not aligned, it just subscribesNext with the outstanding request amount.
694 */
695 @Test
696 public void innerBackpressureWithoutAlignedBoundaries() {
697 TestSubscriber<Integer> ts = new TestSubscriber<>();
698 Flowable.range(0, (Flowable.bufferSize() * 2) + 10)
699 .concatWith(Flowable.range(0, (Flowable.bufferSize() * 2) + 10))
700 .observeOn(Schedulers.computation()) // observeOn has a backpressured RxRingBuffer
701 .subscribe(ts);
702
703 ts.awaitDone(5, TimeUnit.SECONDS);
704 ts.assertNoErrors();
705 assertEquals((Flowable.bufferSize() * 4) + 20, ts.values().size());
706 }
707
708 // https://github.com/ReactiveX/RxJava/issues/1818
709 @Test
710 public void concatWithNonCompliantSourceDoubleOnComplete() {
711 Flowable<String> f = Flowable.unsafeCreate(new Publisher<String>() {
712
713 @Override
714 public void subscribe(Subscriber<? super String> s) {
715 s.onSubscribe(new BooleanSubscription());
716 s.onNext("hello");
717 s.onComplete();
718 s.onComplete();
719 }
720
721 });
722
723 TestSubscriberEx<String> ts = new TestSubscriberEx<>();
724 Flowable.concat(f, f).subscribe(ts);
725 ts.awaitDone(500, TimeUnit.MILLISECONDS);
726 ts.assertTerminated();
727 ts.assertNoErrors();
728 ts.assertValues("hello", "hello");
729 }
730
731 @Test
732 public void issue2890NoStackoverflow() throws InterruptedException, TimeoutException {
733 final ExecutorService executor = Executors.newFixedThreadPool(2);
734 final Scheduler sch = Schedulers.from(executor);
735
736 Function<Integer, Flowable<Integer>> func = new Function<Integer, Flowable<Integer>>() {
737 @Override
738 public Flowable<Integer> apply(Integer t) {
739 Flowable<Integer> flowable = Flowable.just(t)
740 .subscribeOn(sch)
741 ;
742 FlowableProcessor<Integer> processor = UnicastProcessor.create();
743 flowable.subscribe(processor);
744 return processor;
745 }
746 };
747
748 int n = 5000;
749 final AtomicInteger counter = new AtomicInteger();
750
751 Flowable.range(1, n).concatMap(func).subscribe(new DefaultSubscriber<Integer>() {
752 @Override
753 public void onNext(Integer t) {
754 // Consume after sleep for 1 ms
755 try {
756 Thread.sleep(1);
757 } catch (InterruptedException e) {
758 // ignored
759 }
760 if (counter.getAndIncrement() % 100 == 0) {
761 System.out.print("testIssue2890NoStackoverflow -> ");
762 System.out.println(counter.get());
763 };
764 }
765
766 @Override
767 public void onComplete() {
768 executor.shutdown();
769 }
770
771 @Override
772 public void onError(Throwable e) {
773 executor.shutdown();
774 }
775 });
776
777 long awaitTerminationTimeoutMillis = 100_000;
778 if (!executor.awaitTermination(awaitTerminationTimeoutMillis, TimeUnit.MILLISECONDS)) {
779 throw new TimeoutException("Completed " + counter.get() + "/" + n + " before timed out after "
780 + awaitTerminationTimeoutMillis + " milliseconds.");
781 }
782
783 assertEquals(n, counter.get());
784 }
785
786 @Test
787 public void requestOverflowDoesNotStallStream() {
788 Flowable<Integer> f1 = Flowable.just(1, 2, 3);
789 Flowable<Integer> f2 = Flowable.just(4, 5, 6);
790 final AtomicBoolean completed = new AtomicBoolean(false);
791 f1.concatWith(f2).subscribe(new DefaultSubscriber<Integer>() {
792
793 @Override
794 public void onComplete() {
795 completed.set(true);
796 }
797
798 @Override
799 public void onError(Throwable e) {
800
801 }
802
803 @Override
804 public void onNext(Integer t) {
805 request(2);
806 }});
807
808 assertTrue(completed.get());
809 }
810
811 @Test
812 public void concatMapRangeAsyncLoopIssue2876() {
813 final long durationSeconds = 2;
814 final long startTime = System.currentTimeMillis();
815 for (int i = 0;; i++) {
816 //only run this for a max of ten seconds
817 if (System.currentTimeMillis() - startTime > TimeUnit.SECONDS.toMillis(durationSeconds)) {
818 return;
819 }
820 if (i % 1000 == 0) {
821 System.out.println("concatMapRangeAsyncLoop > " + i);
822 }
823 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
824 Flowable.range(0, 1000)
825 .concatMap(new Function<Integer, Flowable<Integer>>() {
826 @Override
827 public Flowable<Integer> apply(Integer t) {
828 return Flowable.fromIterable(Arrays.asList(t));
829 }
830 })
831 .observeOn(Schedulers.computation()).subscribe(ts);
832
833 ts.awaitDone(2500, TimeUnit.MILLISECONDS);
834 ts.assertTerminated();
835 ts.assertNoErrors();
836 assertEquals(1000, ts.values().size());
837 assertEquals((Integer)999, ts.values().get(999));
838 }
839 }
840
841 @SuppressWarnings("unchecked")
842 @Test
843 public void arrayDelayError() {
844 Publisher<Integer>[] sources = new Publisher[] {
845 Flowable.just(1),
846 null,
847 Flowable.range(2, 3),
848 Flowable.error(new TestException()),
849 Flowable.empty()
850 };
851
852 TestSubscriberEx<Integer> ts = Flowable.concatArrayDelayError(sources).to(TestHelper.<Integer>testConsumer());
853
854 ts.assertFailure(CompositeException.class, 1, 2, 3, 4);
855
856 CompositeException composite = (CompositeException)ts.errors().get(0);
857 List<Throwable> list = composite.getExceptions();
858 assertTrue(list.get(0).toString(), list.get(0) instanceof NullPointerException);
859 assertTrue(list.get(1).toString(), list.get(1) instanceof TestException);
860 }
861
862 @Test
863 public void scalarAndRangeBackpressured() {
864 TestSubscriber<Integer> ts = TestSubscriber.create(0);
865
866 Flowable.just(1).concatWith(Flowable.range(2, 3)).subscribe(ts);
867
868 ts.assertNoValues();
869
870 ts.request(5);
871
872 ts.assertValues(1, 2, 3, 4);
873 ts.assertComplete();
874 ts.assertNoErrors();
875 }
876
877 @Test
878 public void scalarAndEmptyBackpressured() {
879 TestSubscriber<Integer> ts = TestSubscriber.create(0);
880
881 Flowable.just(1).concatWith(Flowable.<Integer>empty()).subscribe(ts);
882
883 ts.assertNoValues();
884
885 ts.request(5);
886
887 ts.assertValue(1);
888 ts.assertComplete();
889 ts.assertNoErrors();
890 }
891
892 @Test
893 public void rangeAndEmptyBackpressured() {
894 TestSubscriber<Integer> ts = TestSubscriber.create(0);
895
896 Flowable.range(1, 2).concatWith(Flowable.<Integer>empty()).subscribe(ts);
897
898 ts.assertNoValues();
899
900 ts.request(5);
901
902 ts.assertValues(1, 2);
903 ts.assertComplete();
904 ts.assertNoErrors();
905 }
906
907 @Test
908 public void emptyAndScalarBackpressured() {
909 TestSubscriber<Integer> ts = TestSubscriber.create(0);
910
911 Flowable.<Integer>empty().concatWith(Flowable.just(1)).subscribe(ts);
912
913 ts.assertNoValues();
914
915 ts.request(5);
916
917 ts.assertValue(1);
918 ts.assertComplete();
919 ts.assertNoErrors();
920 }
921
922 @SuppressWarnings("unchecked")
923 @Test
924 public void concatArray() throws Exception {
925 for (int i = 2; i < 10; i++) {
926 Flowable<Integer>[] obs = new Flowable[i];
927 Arrays.fill(obs, Flowable.just(1));
928
929 Integer[] expected = new Integer[i];
930 Arrays.fill(expected, 1);
931
932 Method m = Flowable.class.getMethod("concatArray", Publisher[].class);
933
934 TestSubscriber<Integer> ts = TestSubscriber.create();
935
936 ((Flowable<Integer>)m.invoke(null, new Object[]{obs})).subscribe(ts);
937
938 ts.assertValues(expected);
939 ts.assertNoErrors();
940 ts.assertComplete();
941 }
942 }
943
944 @SuppressWarnings({ "unchecked", "rawtypes" })
945 @Test
946 public void concatMapJustJust() {
947 TestSubscriber<Integer> ts = TestSubscriber.create();
948
949 Flowable.just(Flowable.just(1)).concatMap((Function)Functions.identity()).subscribe(ts);
950
951 ts.assertValue(1);
952 ts.assertNoErrors();
953 ts.assertComplete();
954 }
955
956 @SuppressWarnings({ "unchecked", "rawtypes" })
957 @Test
958 public void concatMapJustRange() {
959 TestSubscriber<Integer> ts = TestSubscriber.create();
960
961 Flowable.just(Flowable.range(1, 5)).concatMap((Function)Functions.identity()).subscribe(ts);
962
963 ts.assertValues(1, 2, 3, 4, 5);
964 ts.assertNoErrors();
965 ts.assertComplete();
966 }
967
968 @SuppressWarnings({ "unchecked", "rawtypes" })
969 @Test
970 public void concatMapDelayErrorJustJust() {
971 TestSubscriber<Integer> ts = TestSubscriber.create();
972
973 Flowable.just(Flowable.just(1)).concatMapDelayError((Function)Functions.identity()).subscribe(ts);
974
975 ts.assertValue(1);
976 ts.assertNoErrors();
977 ts.assertComplete();
978 }
979
980 @SuppressWarnings({ "unchecked", "rawtypes" })
981 @Test
982 public void concatMapDelayErrorJustRange() {
983 TestSubscriber<Integer> ts = TestSubscriber.create();
984
985 Flowable.just(Flowable.range(1, 5)).concatMapDelayError((Function)Functions.identity()).subscribe(ts);
986
987 ts.assertValues(1, 2, 3, 4, 5);
988 ts.assertNoErrors();
989 ts.assertComplete();
990 }
991
992 @SuppressWarnings("unchecked")
993 @Test
994 public void startWithArray() throws Exception {
995 for (int i = 2; i < 10; i++) {
996 Object[] obs = new Object[i];
997 Arrays.fill(obs, 1);
998
999 Integer[] expected = new Integer[i];
1000 Arrays.fill(expected, 1);
1001
1002 Method m = Flowable.class.getMethod("startWithArray", Object[].class);
1003
1004 TestSubscriber<Integer> ts = TestSubscriber.create();
1005
1006 ((Flowable<Integer>)m.invoke(Flowable.empty(), new Object[]{obs})).subscribe(ts);
1007
1008 ts.assertValues(expected);
1009 ts.assertNoErrors();
1010 ts.assertComplete();
1011 }
1012 }
1013
1014 static final class InfiniteIterator implements Iterator<Integer>, Iterable<Integer> {
1015
1016 int count;
1017
1018 @Override
1019 public boolean hasNext() {
1020 return true;
1021 }
1022
1023 @Override
1024 public Integer next() {
1025 return count++;
1026 }
1027
1028 @Override
1029 public void remove() {
1030 }
1031
1032 @Override
1033 public Iterator<Integer> iterator() {
1034 return this;
1035 }
1036 }
1037
1038 @Test
1039 public void veryLongTake() {
1040 Flowable.fromIterable(new InfiniteIterator()).concatWith(Flowable.<Integer>empty()).take(10)
1041 .test()
1042 .assertResult(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
1043 }
1044
1045 @Test
1046 public void concat3() {
1047 Flowable<Integer> source = Flowable.just(1);
1048
1049 Flowable.concat(source, source, source)
1050 .test()
1051 .assertResult(1, 1, 1);
1052 }
1053
1054 @Test
1055 public void concat4() {
1056 Flowable<Integer> source = Flowable.just(1);
1057
1058 Flowable.concat(source, source, source, source)
1059 .test()
1060 .assertResult(1, 1, 1, 1);
1061 }
1062
1063 @Test
1064 public void concatArrayDelayError() {
1065 Flowable.concatArrayDelayError(Flowable.just(1), Flowable.just(2),
1066 Flowable.just(3), Flowable.just(4))
1067 .test()
1068 .assertResult(1, 2, 3, 4);
1069 }
1070
1071 @Test
1072 public void concatArrayDelayErrorWithError() {
1073 Flowable.concatArrayDelayError(Flowable.just(1), Flowable.just(2),
1074 Flowable.just(3).concatWith(Flowable.<Integer>error(new TestException())),
1075 Flowable.just(4))
1076 .test()
1077 .assertFailure(TestException.class, 1, 2, 3, 4);
1078 }
1079
1080 @Test
1081 public void concatIterableDelayError() {
1082 Flowable.concatDelayError(
1083 Arrays.asList(Flowable.just(1), Flowable.just(2),
1084 Flowable.just(3), Flowable.just(4)))
1085 .test()
1086 .assertResult(1, 2, 3, 4);
1087 }
1088
1089 @Test
1090 public void concatIterableDelayErrorWithError() {
1091 Flowable.concatDelayError(
1092 Arrays.asList(Flowable.just(1), Flowable.just(2),
1093 Flowable.just(3).concatWith(Flowable.<Integer>error(new TestException())),
1094 Flowable.just(4)))
1095 .test()
1096 .assertFailure(TestException.class, 1, 2, 3, 4);
1097 }
1098
1099 @Test
1100 public void concatObservableDelayError() {
1101 Flowable.concatDelayError(
1102 Flowable.just(Flowable.just(1), Flowable.just(2),
1103 Flowable.just(3), Flowable.just(4)))
1104 .test()
1105 .assertResult(1, 2, 3, 4);
1106 }
1107
1108 @Test
1109 public void concatObservableDelayErrorWithError() {
1110 Flowable.concatDelayError(
1111 Flowable.just(Flowable.just(1), Flowable.just(2),
1112 Flowable.just(3).concatWith(Flowable.<Integer>error(new TestException())),
1113 Flowable.just(4)))
1114 .test()
1115 .assertFailure(TestException.class, 1, 2, 3, 4);
1116 }
1117
1118 @Test
1119 public void concatObservableDelayErrorBoundary() {
1120 Flowable.concatDelayError(
1121 Flowable.just(Flowable.just(1), Flowable.just(2),
1122 Flowable.just(3).concatWith(Flowable.<Integer>error(new TestException())),
1123 Flowable.just(4)), 2, false)
1124 .test()
1125 .assertFailure(TestException.class, 1, 2, 3);
1126 }
1127
1128 @Test
1129 public void concatObservableDelayErrorTillEnd() {
1130 Flowable.concatDelayError(
1131 Flowable.just(Flowable.just(1), Flowable.just(2),
1132 Flowable.just(3).concatWith(Flowable.<Integer>error(new TestException())),
1133 Flowable.just(4)), 2, true)
1134 .test()
1135 .assertFailure(TestException.class, 1, 2, 3, 4);
1136 }
1137
1138 @Test
1139 public void concatMapDelayError() {
1140 Flowable.just(Flowable.just(1), Flowable.just(2))
1141 .concatMapDelayError(Functions.<Flowable<Integer>>identity())
1142 .test()
1143 .assertResult(1, 2);
1144 }
1145
1146 @Test
1147 public void concatMapDelayErrorWithError() {
1148 Flowable.just(Flowable.just(1).concatWith(Flowable.<Integer>error(new TestException())), Flowable.just(2))
1149 .concatMapDelayError(Functions.<Flowable<Integer>>identity())
1150 .test()
1151 .assertFailure(TestException.class, 1, 2);
1152 }
1153
1154 @Test
1155 public void concatMapIterableBufferSize() {
1156
1157 Flowable.just(1, 2).concatMapIterable(new Function<Integer, Iterable<Integer>>() {
1158 @Override
1159 public Iterable<Integer> apply(Integer v) throws Exception {
1160 return Arrays.asList(1, 2, 3, 4, 5);
1161 }
1162 }, 1)
1163 .test()
1164 .assertResult(1, 2, 3, 4, 5, 1, 2, 3, 4, 5);
1165 }
1166
1167 @Test
1168 public void emptyArray() {
1169 assertSame(Flowable.empty(), Flowable.concatArrayDelayError());
1170 }
1171
1172 @Test
1173 public void singleElementArray() {
1174 assertSame(Flowable.never(), Flowable.concatArrayDelayError(Flowable.never()));
1175 }
1176
1177 @Test
1178 public void concatMapDelayErrorEmptySource() {
1179 assertSame(Flowable.empty(), Flowable.<Object>empty()
1180 .concatMapDelayError(new Function<Object, Flowable<Integer>>() {
1181 @Override
1182 public Flowable<Integer> apply(Object v) throws Exception {
1183 return Flowable.just(1);
1184 }
1185 }, true, 16));
1186 }
1187
1188 @Test
1189 public void concatMapDelayErrorJustSource() {
1190 Flowable.just(0)
1191 .concatMapDelayError(new Function<Object, Flowable<Integer>>() {
1192 @Override
1193 public Flowable<Integer> apply(Object v) throws Exception {
1194 return Flowable.just(1);
1195 }
1196 }, true, 16)
1197 .test()
1198 .assertResult(1);
1199
1200 }
1201
1202 @Test
1203 public void concatArrayEmpty() {
1204 assertSame(Flowable.empty(), Flowable.concatArray());
1205 }
1206
1207 @Test
1208 public void concatArraySingleElement() {
1209 assertSame(Flowable.never(), Flowable.concatArray(Flowable.never()));
1210 }
1211
1212 @Test
1213 public void concatMapErrorEmptySource() {
1214 assertSame(Flowable.empty(), Flowable.<Object>empty()
1215 .concatMap(new Function<Object, Flowable<Integer>>() {
1216 @Override
1217 public Flowable<Integer> apply(Object v) throws Exception {
1218 return Flowable.just(1);
1219 }
1220 }, 16));
1221 }
1222
1223 @Test
1224 public void concatMapJustSource() {
1225 Flowable.just(0).hide()
1226 .concatMap(new Function<Object, Flowable<Integer>>() {
1227 @Override
1228 public Flowable<Integer> apply(Object v) throws Exception {
1229 return Flowable.just(1);
1230 }
1231 }, 16)
1232 .test()
1233 .assertResult(1);
1234 }
1235
1236 @Test
1237 public void concatMapJustSourceDelayError() {
1238 Flowable.just(0).hide()
1239 .concatMapDelayError(new Function<Object, Flowable<Integer>>() {
1240 @Override
1241 public Flowable<Integer> apply(Object v) throws Exception {
1242 return Flowable.just(1);
1243 }
1244 }, false, 16)
1245 .test()
1246 .assertResult(1);
1247 }
1248
1249 @Test
1250 public void concatMapScalarBackpressured() {
1251 Flowable.just(1).hide()
1252 .concatMap(Functions.justFunction(Flowable.just(2)))
1253 .test(1L)
1254 .assertResult(2);
1255 }
1256
1257 @Test
1258 public void concatMapScalarBackpressuredDelayError() {
1259 Flowable.just(1).hide()
1260 .concatMapDelayError(Functions.justFunction(Flowable.just(2)))
1261 .test(1L)
1262 .assertResult(2);
1263 }
1264
1265 @Test
1266 public void concatMapEmpty() {
1267 Flowable.just(1).hide()
1268 .concatMap(Functions.justFunction(Flowable.empty()))
1269 .test()
1270 .assertResult();
1271 }
1272
1273 @Test
1274 public void concatMapEmptyDelayError() {
1275 Flowable.just(1).hide()
1276 .concatMapDelayError(Functions.justFunction(Flowable.empty()))
1277 .test()
1278 .assertResult();
1279 }
1280
1281 @Test
1282 public void ignoreBackpressure() {
1283 new Flowable<Integer>() {
1284 @Override
1285 protected void subscribeActual(Subscriber<? super Integer> s) {
1286 s.onSubscribe(new BooleanSubscription());
1287 for (int i = 0; i < 10; i++) {
1288 s.onNext(i);
1289 }
1290 }
1291 }
1292 .concatMap(Functions.justFunction(Flowable.just(2)), 8)
1293 .test(0L)
1294 .assertFailure(IllegalStateException.class);
1295 }
1296
1297 @Test
1298 public void doubleOnSubscribe() {
1299 TestHelper.checkDoubleOnSubscribeFlowable(new Function<Flowable<Object>, Publisher<Integer>>() {
1300 @Override
1301 public Publisher<Integer> apply(Flowable<Object> f) throws Exception {
1302 return f.concatMap(Functions.justFunction(Flowable.just(2)));
1303 }
1304 });
1305 TestHelper.checkDoubleOnSubscribeFlowable(new Function<Flowable<Object>, Publisher<Integer>>() {
1306 @Override
1307 public Publisher<Integer> apply(Flowable<Object> f) throws Exception {
1308 return f.concatMapDelayError(Functions.justFunction(Flowable.just(2)));
1309 }
1310 });
1311 }
1312
1313 @Test
1314 public void immediateInnerNextOuterError() {
1315 final PublishProcessor<Integer> pp = PublishProcessor.create();
1316
1317 final TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>() {
1318 @Override
1319 public void onNext(Integer t) {
1320 super.onNext(t);
1321 if (t == 1) {
1322 pp.onError(new TestException("First"));
1323 }
1324 }
1325 };
1326
1327 pp.concatMap(Functions.justFunction(Flowable.just(1)))
1328 .subscribe(ts);
1329
1330 pp.onNext(1);
1331
1332 assertFalse(pp.hasSubscribers());
1333
1334 ts.assertFailureAndMessage(TestException.class, "First", 1);
1335 }
1336
1337 @Test
1338 public void immediateInnerNextOuterError2() {
1339 final PublishProcessor<Integer> pp = PublishProcessor.create();
1340
1341 final TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>() {
1342 @Override
1343 public void onNext(Integer t) {
1344 super.onNext(t);
1345 if (t == 1) {
1346 pp.onError(new TestException("First"));
1347 }
1348 }
1349 };
1350
1351 pp.concatMap(Functions.justFunction(Flowable.just(1).hide()))
1352 .subscribe(ts);
1353
1354 pp.onNext(1);
1355
1356 assertFalse(pp.hasSubscribers());
1357
1358 ts.assertFailureAndMessage(TestException.class, "First", 1);
1359 }
1360
1361 @Test
1362 public void concatMapInnerError() {
1363 Flowable.just(1).hide()
1364 .concatMap(Functions.justFunction(Flowable.error(new TestException())))
1365 .test()
1366 .assertFailure(TestException.class);
1367 }
1368
1369 @Test
1370 public void concatMapInnerErrorDelayError() {
1371 Flowable.just(1).hide()
1372 .concatMapDelayError(Functions.justFunction(Flowable.error(new TestException())))
1373 .test()
1374 .assertFailure(TestException.class);
1375 }
1376
1377 @Test
1378 public void badSource() {
1379 TestHelper.checkBadSourceFlowable(new Function<Flowable<Integer>, Object>() {
1380 @Override
1381 public Object apply(Flowable<Integer> f) throws Exception {
1382 return f.concatMap(Functions.justFunction(Flowable.just(1).hide()));
1383 }
1384 }, true, 1, 1, 1);
1385 }
1386
1387 @Test
1388 public void badInnerSource() {
1389 @SuppressWarnings("rawtypes")
1390 final Subscriber[] ts0 = { null };
1391 TestSubscriberEx<Integer> ts = Flowable.just(1).hide().concatMap(Functions.justFunction(new Flowable<Integer>() {
1392 @Override
1393 protected void subscribeActual(Subscriber<? super Integer> s) {
1394 ts0[0] = s;
1395 s.onSubscribe(new BooleanSubscription());
1396 s.onError(new TestException("First"));
1397 }
1398 }))
1399 .to(TestHelper.<Integer>testConsumer());
1400
1401 ts.assertFailureAndMessage(TestException.class, "First");
1402
1403 List<Throwable> errors = TestHelper.trackPluginErrors();
1404 try {
1405 ts0[0].onError(new TestException("Second"));
1406
1407 TestHelper.assertUndeliverable(errors, 0, TestException.class);
1408 } finally {
1409 RxJavaPlugins.reset();
1410 }
1411 }
1412
1413 @Test
1414 public void badInnerSourceDelayError() {
1415 @SuppressWarnings("rawtypes")
1416 final Subscriber[] ts0 = { null };
1417 TestSubscriberEx<Integer> ts = Flowable.just(1).hide().concatMapDelayError(Functions.justFunction(new Flowable<Integer>() {
1418 @Override
1419 protected void subscribeActual(Subscriber<? super Integer> s) {
1420 ts0[0] = s;
1421 s.onSubscribe(new BooleanSubscription());
1422 s.onError(new TestException("First"));
1423 }
1424 }))
1425 .to(TestHelper.<Integer>testConsumer());
1426
1427 ts.assertFailureAndMessage(TestException.class, "First");
1428
1429 List<Throwable> errors = TestHelper.trackPluginErrors();
1430 try {
1431 ts0[0].onError(new TestException("Second"));
1432
1433 TestHelper.assertUndeliverable(errors, 0, TestException.class);
1434 } finally {
1435 RxJavaPlugins.reset();
1436 }
1437 }
1438
1439 @Test
1440 public void badSourceDelayError() {
1441 TestHelper.checkBadSourceFlowable(new Function<Flowable<Integer>, Object>() {
1442 @Override
1443 public Object apply(Flowable<Integer> f) throws Exception {
1444 return f.concatMapDelayError(Functions.justFunction(Flowable.just(1).hide()));
1445 }
1446 }, true, 1, 1, 1);
1447 }
1448
1449 @Test
1450 public void fusedCrash() {
1451 Flowable.range(1, 2)
1452 .map(new Function<Integer, Object>() {
1453 @Override
1454 public Object apply(Integer v) throws Exception { throw new TestException(); }
1455 })
1456 .concatMap(Functions.justFunction(Flowable.just(1)))
1457 .test()
1458 .assertFailure(TestException.class);
1459 }
1460
1461 @Test
1462 public void fusedCrashDelayError() {
1463 Flowable.range(1, 2)
1464 .map(new Function<Integer, Object>() {
1465 @Override
1466 public Object apply(Integer v) throws Exception { throw new TestException(); }
1467 })
1468 .concatMapDelayError(Functions.justFunction(Flowable.just(1)))
1469 .test()
1470 .assertFailure(TestException.class);
1471 }
1472
1473 @Test
1474 public void callableCrash() {
1475 Flowable.just(1).hide()
1476 .concatMap(Functions.justFunction(Flowable.fromCallable(new Callable<Object>() {
1477 @Override
1478 public Object call() throws Exception {
1479 throw new TestException();
1480 }
1481 })))
1482 .test()
1483 .assertFailure(TestException.class);
1484 }
1485
1486 @Test
1487 public void callableCrashDelayError() {
1488 Flowable.just(1).hide()
1489 .concatMapDelayError(Functions.justFunction(Flowable.fromCallable(new Callable<Object>() {
1490 @Override
1491 public Object call() throws Exception {
1492 throw new TestException();
1493 }
1494 })))
1495 .test()
1496 .assertFailure(TestException.class);
1497 }
1498
1499 @Test
1500 public void dispose() {
1501 TestHelper.checkDisposed(Flowable.range(1, 2)
1502 .concatMap(Functions.justFunction(Flowable.just(1))));
1503
1504 TestHelper.checkDisposed(Flowable.range(1, 2)
1505 .concatMapDelayError(Functions.justFunction(Flowable.just(1))));
1506 }
1507
1508 @Test
1509 public void notVeryEnd() {
1510 Flowable.range(1, 2)
1511 .concatMapDelayError(Functions.justFunction(Flowable.error(new TestException())), false, 16)
1512 .test()
1513 .assertFailure(TestException.class);
1514 }
1515
1516 @Test
1517 public void error() {
1518 Flowable.error(new TestException())
1519 .concatMapDelayError(Functions.justFunction(Flowable.just(2)), false, 16)
1520 .test()
1521 .assertFailure(TestException.class);
1522 }
1523
1524 @Test
1525 public void mapperThrows() {
1526 Flowable.range(1, 2)
1527 .concatMap(new Function<Integer, Publisher<Object>>() {
1528 @Override
1529 public Publisher<Object> apply(Integer v) throws Exception {
1530 throw new TestException();
1531 }
1532 })
1533 .test()
1534 .assertFailure(TestException.class);
1535 }
1536
1537 @Test
1538 public void noSubsequentSubscription() {
1539 final int[] calls = { 0 };
1540
1541 Flowable<Integer> source = Flowable.create(new FlowableOnSubscribe<Integer>() {
1542 @Override
1543 public void subscribe(FlowableEmitter<Integer> s) throws Exception {
1544 calls[0]++;
1545 s.onNext(1);
1546 s.onComplete();
1547 }
1548 }, BackpressureStrategy.MISSING);
1549
1550 Flowable.concatArray(source, source).firstElement()
1551 .test()
1552 .assertResult(1);
1553
1554 assertEquals(1, calls[0]);
1555 }
1556
1557 @Test
1558 public void noSubsequentSubscriptionDelayError() {
1559 final int[] calls = { 0 };
1560
1561 Flowable<Integer> source = Flowable.create(new FlowableOnSubscribe<Integer>() {
1562 @Override
1563 public void subscribe(FlowableEmitter<Integer> s) throws Exception {
1564 calls[0]++;
1565 s.onNext(1);
1566 s.onComplete();
1567 }
1568 }, BackpressureStrategy.MISSING);
1569
1570 Flowable.concatArrayDelayError(source, source).firstElement()
1571 .test()
1572 .assertResult(1);
1573
1574 assertEquals(1, calls[0]);
1575 }
1576
1577 @Test
1578 public void noSubsequentSubscriptionIterable() {
1579 final int[] calls = { 0 };
1580
1581 Flowable<Integer> source = Flowable.create(new FlowableOnSubscribe<Integer>() {
1582 @Override
1583 public void subscribe(FlowableEmitter<Integer> s) throws Exception {
1584 calls[0]++;
1585 s.onNext(1);
1586 s.onComplete();
1587 }
1588 }, BackpressureStrategy.MISSING);
1589
1590 Flowable.concat(Arrays.asList(source, source)).firstElement()
1591 .test()
1592 .assertResult(1);
1593
1594 assertEquals(1, calls[0]);
1595 }
1596
1597 @Test
1598 public void noSubsequentSubscriptionDelayErrorIterable() {
1599 final int[] calls = { 0 };
1600
1601 Flowable<Integer> source = Flowable.create(new FlowableOnSubscribe<Integer>() {
1602 @Override
1603 public void subscribe(FlowableEmitter<Integer> s) throws Exception {
1604 calls[0]++;
1605 s.onNext(1);
1606 s.onComplete();
1607 }
1608 }, BackpressureStrategy.MISSING);
1609
1610 Flowable.concatDelayError(Arrays.asList(source, source)).firstElement()
1611 .test()
1612 .assertResult(1);
1613
1614 assertEquals(1, calls[0]);
1615 }
1616
1617 @Test
1618 public void noCancelPreviousArray() {
1619 final AtomicInteger counter = new AtomicInteger();
1620
1621 Flowable<Integer> source = Flowable.just(1).doOnCancel(new Action() {
1622 @Override
1623 public void run() throws Exception {
1624 counter.getAndIncrement();
1625 }
1626 });
1627
1628 Flowable.concatArray(source, source, source, source, source)
1629 .test()
1630 .assertResult(1, 1, 1, 1, 1);
1631
1632 assertEquals(0, counter.get());
1633 }
1634
1635 @Test
1636 public void noCancelPreviousIterable() {
1637 final AtomicInteger counter = new AtomicInteger();
1638
1639 Flowable<Integer> source = Flowable.just(1).doOnCancel(new Action() {
1640 @Override
1641 public void run() throws Exception {
1642 counter.getAndIncrement();
1643 }
1644 });
1645
1646 Flowable.concat(Arrays.asList(source, source, source, source, source))
1647 .test()
1648 .assertResult(1, 1, 1, 1, 1);
1649
1650 assertEquals(0, counter.get());
1651 }
1652
1653 @Test
1654 public void arrayDelayErrorMultipleErrors() {
1655 TestSubscriberEx<Object> ts = new TestSubscriberEx<>();
1656
1657 Flowable.concatArrayDelayError(Flowable.error(new IOException()), Flowable.error(new TestException()))
1658 .subscribe(ts);
1659
1660 ts.assertFailure(CompositeException.class);
1661
1662 TestHelper.assertCompositeExceptions(ts, IOException.class, TestException.class);
1663 }
1664
1665 @Test
1666 public void arrayDelayErrorMultipleNullErrors() {
1667 TestSubscriberEx<Object> ts = new TestSubscriberEx<>();
1668
1669 Flowable.concatArrayDelayError(null, null)
1670 .subscribe(ts);
1671
1672 ts.assertFailure(CompositeException.class);
1673
1674 TestHelper.assertCompositeExceptions(ts, NullPointerException.class, NullPointerException.class);
1675 }
1676 }