Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/flowable/FlowablePublishTest.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.*;
19 import java.util.concurrent.*;
20 import java.util.concurrent.atomic.*;
21
22 import org.junit.Test;
23 import org.reactivestreams.*;
24
25 import io.reactivex.rxjava3.core.*;
26 import io.reactivex.rxjava3.disposables.Disposable;
27 import io.reactivex.rxjava3.exceptions.*;
28 import io.reactivex.rxjava3.flowables.ConnectableFlowable;
29 import io.reactivex.rxjava3.functions.*;
30 import io.reactivex.rxjava3.internal.functions.Functions;
31 import io.reactivex.rxjava3.internal.fuseable.HasUpstreamPublisher;
32 import io.reactivex.rxjava3.internal.operators.flowable.FlowablePublish.*;
33 import io.reactivex.rxjava3.internal.schedulers.ImmediateThinScheduler;
34 import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription;
35 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
36 import io.reactivex.rxjava3.processors.PublishProcessor;
37 import io.reactivex.rxjava3.schedulers.*;
38 import io.reactivex.rxjava3.subscribers.TestSubscriber;
39 import io.reactivex.rxjava3.testsupport.*;
40
41 public class FlowablePublishTest extends RxJavaTest {
42
43 @Test
44 public void publish() throws InterruptedException {
45 final AtomicInteger counter = new AtomicInteger();
46 ConnectableFlowable<String> f = Flowable.unsafeCreate(new Publisher<String>() {
47
48 @Override
49 public void subscribe(final Subscriber<? super String> subscriber) {
50 subscriber.onSubscribe(new BooleanSubscription());
51 new Thread(new Runnable() {
52
53 @Override
54 public void run() {
55 counter.incrementAndGet();
56 subscriber.onNext("one");
57 subscriber.onComplete();
58 }
59 }).start();
60 }
61 }).publish();
62
63 final CountDownLatch latch = new CountDownLatch(2);
64
65 // subscribe once
66 f.subscribe(new Consumer<String>() {
67
68 @Override
69 public void accept(String v) {
70 assertEquals("one", v);
71 latch.countDown();
72 }
73 });
74
75 // subscribe again
76 f.subscribe(new Consumer<String>() {
77
78 @Override
79 public void accept(String v) {
80 assertEquals("one", v);
81 latch.countDown();
82 }
83 });
84
85 Disposable connection = f.connect();
86 try {
87 if (!latch.await(1000, TimeUnit.MILLISECONDS)) {
88 fail("subscriptions did not receive values");
89 }
90 assertEquals(1, counter.get());
91 } finally {
92 connection.dispose();
93 }
94 }
95
96 @Test
97 public void backpressureFastSlow() {
98 ConnectableFlowable<Integer> is = Flowable.range(1, Flowable.bufferSize() * 2).publish();
99 Flowable<Integer> fast = is.observeOn(Schedulers.computation())
100 .doOnComplete(new Action() {
101 @Override
102 public void run() {
103 System.out.println("^^^^^^^^^^^^^ completed FAST");
104 }
105 });
106
107 Flowable<Integer> slow = is.observeOn(Schedulers.computation()).map(new Function<Integer, Integer>() {
108 int c;
109
110 @Override
111 public Integer apply(Integer i) {
112 if (c == 0) {
113 try {
114 Thread.sleep(500);
115 } catch (InterruptedException e) {
116 }
117 }
118 c++;
119 return i;
120 }
121
122 }).doOnComplete(new Action() {
123
124 @Override
125 public void run() {
126 System.out.println("^^^^^^^^^^^^^ completed SLOW");
127 }
128
129 });
130
131 TestSubscriber<Integer> ts = new TestSubscriber<>();
132 Flowable.merge(fast, slow).subscribe(ts);
133 is.connect();
134 ts.awaitDone(5, TimeUnit.SECONDS);
135 ts.assertNoErrors();
136 assertEquals(Flowable.bufferSize() * 4, ts.values().size());
137 }
138
139 // use case from https://github.com/ReactiveX/RxJava/issues/1732
140 @Test
141 public void takeUntilWithPublishedStreamUsingSelector() {
142 final AtomicInteger emitted = new AtomicInteger();
143 Flowable<Integer> xs = Flowable.range(0, Flowable.bufferSize() * 2).doOnNext(new Consumer<Integer>() {
144
145 @Override
146 public void accept(Integer t1) {
147 emitted.incrementAndGet();
148 }
149
150 });
151 TestSubscriber<Integer> ts = new TestSubscriber<>();
152 xs.publish(new Function<Flowable<Integer>, Flowable<Integer>>() {
153
154 @Override
155 public Flowable<Integer> apply(Flowable<Integer> xs) {
156 return xs.takeUntil(xs.skipWhile(new Predicate<Integer>() {
157
158 @Override
159 public boolean test(Integer i) {
160 return i <= 3;
161 }
162
163 }));
164 }
165
166 }).subscribe(ts);
167 ts.awaitDone(5, TimeUnit.SECONDS);
168 ts.assertNoErrors();
169 ts.assertValues(0, 1, 2, 3);
170 assertEquals(5, emitted.get());
171 System.out.println(ts.values());
172 }
173
174 // use case from https://github.com/ReactiveX/RxJava/issues/1732
175 @Test
176 public void takeUntilWithPublishedStream() {
177 Flowable<Integer> xs = Flowable.range(0, Flowable.bufferSize() * 2);
178 TestSubscriber<Integer> ts = new TestSubscriber<>();
179 ConnectableFlowable<Integer> xsp = xs.publish();
180 xsp.takeUntil(xsp.skipWhile(new Predicate<Integer>() {
181
182 @Override
183 public boolean test(Integer i) {
184 return i <= 3;
185 }
186
187 })).subscribe(ts);
188 xsp.connect();
189 System.out.println(ts.values());
190 }
191
192 @Test
193 public void backpressureTwoConsumers() {
194 final AtomicInteger sourceEmission = new AtomicInteger();
195 final AtomicBoolean sourceUnsubscribed = new AtomicBoolean();
196 final Flowable<Integer> source = Flowable.range(1, 100)
197 .doOnNext(new Consumer<Integer>() {
198 @Override
199 public void accept(Integer t1) {
200 sourceEmission.incrementAndGet();
201 }
202 })
203 .doOnCancel(new Action() {
204 @Override
205 public void run() {
206 sourceUnsubscribed.set(true);
207 }
208 }).share();
209 ;
210
211 final AtomicBoolean child1Unsubscribed = new AtomicBoolean();
212 final AtomicBoolean child2Unsubscribed = new AtomicBoolean();
213
214 final TestSubscriber<Integer> ts2 = new TestSubscriber<>();
215
216 final TestSubscriber<Integer> ts1 = new TestSubscriber<Integer>() {
217 @Override
218 public void onNext(Integer t) {
219 if (values().size() == 2) {
220 source.doOnCancel(new Action() {
221 @Override
222 public void run() {
223 child2Unsubscribed.set(true);
224 }
225 }).take(5).subscribe(ts2);
226 }
227 super.onNext(t);
228 }
229 };
230
231 source.doOnCancel(new Action() {
232 @Override
233 public void run() {
234 child1Unsubscribed.set(true);
235 }
236 }).take(5)
237 .subscribe(ts1);
238
239 ts1.awaitDone(5, TimeUnit.SECONDS);
240 ts2.awaitDone(5, TimeUnit.SECONDS);
241
242 ts1.assertNoErrors();
243 ts2.assertNoErrors();
244
245 assertTrue(sourceUnsubscribed.get());
246 assertTrue(child1Unsubscribed.get());
247 assertTrue(child2Unsubscribed.get());
248
249 ts1.assertValues(1, 2, 3, 4, 5);
250 ts2.assertValues(4, 5, 6, 7, 8);
251
252 assertEquals(8, sourceEmission.get());
253 }
254
255 @Test
256 public void connectWithNoSubscriber() {
257 TestScheduler scheduler = new TestScheduler();
258 ConnectableFlowable<Long> cf = Flowable.interval(10, 10, TimeUnit.MILLISECONDS, scheduler).take(3).publish();
259 cf.connect();
260 // Emit 0
261 scheduler.advanceTimeBy(15, TimeUnit.MILLISECONDS);
262 TestSubscriber<Long> subscriber = new TestSubscriber<>();
263 cf.subscribe(subscriber);
264 // Emit 1 and 2
265 scheduler.advanceTimeBy(50, TimeUnit.MILLISECONDS);
266 // 3.x: Flowable.publish no longer drains the input buffer if there are no subscribers
267 subscriber.assertResult(0L, 1L, 2L);
268 }
269
270 @Test
271 public void subscribeAfterDisconnectThenConnect() {
272 ConnectableFlowable<Integer> source = Flowable.just(1).publish();
273
274 TestSubscriberEx<Integer> ts1 = new TestSubscriberEx<>();
275
276 source.subscribe(ts1);
277
278 Disposable connection = source.connect();
279
280 ts1.assertValue(1);
281 ts1.assertNoErrors();
282 ts1.assertTerminated();
283
284 source.reset();
285
286 TestSubscriberEx<Integer> ts2 = new TestSubscriberEx<>();
287
288 source.subscribe(ts2);
289
290 Disposable connection2 = source.connect();
291
292 ts2.assertValue(1);
293 ts2.assertNoErrors();
294 ts2.assertTerminated();
295
296 System.out.println(connection);
297 System.out.println(connection2);
298 }
299
300 @Test
301 public void noSubscriberRetentionOnCompleted() {
302 FlowablePublish<Integer> source = (FlowablePublish<Integer>)Flowable.just(1).publish();
303
304 TestSubscriberEx<Integer> ts1 = new TestSubscriberEx<>();
305
306 source.subscribe(ts1);
307
308 ts1.assertNoValues();
309 ts1.assertNoErrors();
310 ts1.assertNotComplete();
311
312 source.connect();
313
314 ts1.assertValue(1);
315 ts1.assertNoErrors();
316 ts1.assertTerminated();
317
318 assertEquals(0, source.current.get().subscribers.get().length);
319 }
320
321 @Test
322 public void nonNullConnection() {
323 ConnectableFlowable<Object> source = Flowable.never().publish();
324
325 assertNotNull(source.connect());
326 assertNotNull(source.connect());
327 }
328
329 @Test
330 public void noDisconnectSomeoneElse() {
331 ConnectableFlowable<Object> source = Flowable.never().publish();
332
333 Disposable connection1 = source.connect();
334 Disposable connection2 = source.connect();
335
336 connection1.dispose();
337
338 Disposable connection3 = source.connect();
339
340 connection2.dispose();
341
342 assertTrue(checkPublishDisposed(connection1));
343 assertTrue(checkPublishDisposed(connection2));
344 assertFalse(checkPublishDisposed(connection3));
345 }
346
347 @SuppressWarnings("unchecked")
348 static boolean checkPublishDisposed(Disposable d) {
349 return ((FlowablePublish.PublishConnection<Object>)d).isDisposed();
350 }
351
352 @Test
353 public void zeroRequested() {
354 ConnectableFlowable<Integer> source = Flowable.just(1).publish();
355
356 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(0L);
357
358 source.subscribe(ts);
359
360 ts.assertNoValues();
361 ts.assertNoErrors();
362 ts.assertNotComplete();
363
364 source.connect();
365
366 ts.assertNoValues();
367 ts.assertNoErrors();
368 ts.assertNotComplete();
369
370 ts.request(5);
371
372 ts.assertValue(1);
373 ts.assertNoErrors();
374 ts.assertTerminated();
375 }
376
377 @Test
378 public void connectIsIdempotent() {
379 final AtomicInteger calls = new AtomicInteger();
380 Flowable<Integer> source = Flowable.unsafeCreate(new Publisher<Integer>() {
381 @Override
382 public void subscribe(Subscriber<? super Integer> t) {
383 t.onSubscribe(new BooleanSubscription());
384 calls.getAndIncrement();
385 }
386 });
387
388 ConnectableFlowable<Integer> conn = source.publish();
389
390 assertEquals(0, calls.get());
391
392 conn.connect();
393 conn.connect();
394
395 assertEquals(1, calls.get());
396
397 conn.connect().dispose();
398
399 conn.connect();
400 conn.connect();
401
402 assertEquals(2, calls.get());
403 }
404
405 @Test
406 public void syncFusedObserveOn() {
407 ConnectableFlowable<Integer> cf = Flowable.range(0, 1000).publish();
408 Flowable<Integer> obs = cf.observeOn(Schedulers.computation());
409 for (int i = 0; i < 1000; i++) {
410 for (int j = 1; j < 6; j++) {
411 List<TestSubscriberEx<Integer>> tss = new ArrayList<>();
412 for (int k = 1; k < j; k++) {
413 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
414 tss.add(ts);
415 obs.subscribe(ts);
416 }
417
418 Disposable connection = cf.connect();
419
420 for (TestSubscriberEx<Integer> ts : tss) {
421 ts.awaitDone(5, TimeUnit.SECONDS)
422 .assertSubscribed()
423 .assertValueCount(1000)
424 .assertNoErrors()
425 .assertComplete();
426 }
427 connection.dispose();
428 }
429 }
430 }
431
432 @Test
433 public void syncFusedObserveOn2() {
434 ConnectableFlowable<Integer> cf = Flowable.range(0, 1000).publish();
435 Flowable<Integer> obs = cf.observeOn(ImmediateThinScheduler.INSTANCE);
436 for (int i = 0; i < 1000; i++) {
437 for (int j = 1; j < 6; j++) {
438 List<TestSubscriberEx<Integer>> tss = new ArrayList<>();
439 for (int k = 1; k < j; k++) {
440 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
441 tss.add(ts);
442 obs.subscribe(ts);
443 }
444
445 Disposable connection = cf.connect();
446
447 for (TestSubscriberEx<Integer> ts : tss) {
448 ts.awaitDone(5, TimeUnit.SECONDS)
449 .assertSubscribed()
450 .assertValueCount(1000)
451 .assertNoErrors()
452 .assertComplete();
453 }
454 connection.dispose();
455 }
456 }
457 }
458
459 @Test
460 public void asyncFusedObserveOn() {
461 ConnectableFlowable<Integer> cf = Flowable.range(0, 1000).observeOn(ImmediateThinScheduler.INSTANCE).publish();
462 for (int i = 0; i < 1000; i++) {
463 for (int j = 1; j < 6; j++) {
464 List<TestSubscriberEx<Integer>> tss = new ArrayList<>();
465 for (int k = 1; k < j; k++) {
466 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
467 tss.add(ts);
468 cf.subscribe(ts);
469 }
470
471 Disposable connection = cf.connect();
472
473 for (TestSubscriberEx<Integer> ts : tss) {
474 ts.awaitDone(5, TimeUnit.SECONDS)
475 .assertSubscribed()
476 .assertValueCount(1000)
477 .assertNoErrors()
478 .assertComplete();
479 }
480 connection.dispose();
481 }
482 }
483 }
484
485 @Test
486 public void observeOn() {
487 ConnectableFlowable<Integer> cf = Flowable.range(0, 1000).hide().publish();
488 Flowable<Integer> obs = cf.observeOn(Schedulers.computation());
489 for (int i = 0; i < 1000; i++) {
490 for (int j = 1; j < 6; j++) {
491 List<TestSubscriberEx<Integer>> tss = new ArrayList<>();
492 for (int k = 1; k < j; k++) {
493 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
494 tss.add(ts);
495 obs.subscribe(ts);
496 }
497
498 Disposable connection = cf.connect();
499
500 for (TestSubscriberEx<Integer> ts : tss) {
501 ts.awaitDone(5, TimeUnit.SECONDS)
502 .assertSubscribed()
503 .assertValueCount(1000)
504 .assertNoErrors()
505 .assertComplete();
506 }
507 connection.dispose();
508 }
509 }
510 }
511
512 @Test
513 public void source() {
514 Flowable<Integer> f = Flowable.never();
515
516 assertSame(f, (((HasUpstreamPublisher<?>)f.publish()).source()));
517 }
518
519 @Test
520 public void connectThrows() {
521 ConnectableFlowable<Integer> cf = Flowable.<Integer>empty().publish();
522 try {
523 cf.connect(new Consumer<Disposable>() {
524 @Override
525 public void accept(Disposable d) throws Exception {
526 throw new TestException();
527 }
528 });
529 } catch (TestException ex) {
530 // expected
531 }
532 }
533
534 @Test
535 public void addRemoveRace() {
536 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
537
538 final ConnectableFlowable<Integer> cf = Flowable.<Integer>empty().publish();
539
540 final TestSubscriber<Integer> ts = cf.test();
541
542 final TestSubscriber<Integer> ts2 = new TestSubscriber<>();
543
544 Runnable r1 = new Runnable() {
545 @Override
546 public void run() {
547 cf.subscribe(ts2);
548 }
549 };
550
551 Runnable r2 = new Runnable() {
552 @Override
553 public void run() {
554 ts.cancel();
555 }
556 };
557
558 TestHelper.race(r1, r2);
559 }
560 }
561
562 @Test
563 public void disposeOnArrival() {
564 ConnectableFlowable<Integer> cf = Flowable.<Integer>empty().publish();
565
566 cf.test(Long.MAX_VALUE, true).assertEmpty();
567 }
568
569 @Test
570 public void disposeOnArrival2() {
571 Flowable<Integer> co = Flowable.<Integer>never().publish().autoConnect();
572
573 co.test(Long.MAX_VALUE, true).assertEmpty();
574 }
575
576 @Test
577 public void dispose() {
578 TestHelper.checkDisposed(Flowable.never().publish());
579
580 TestHelper.checkDisposed(Flowable.never().publish(Functions.<Flowable<Object>>identity()));
581 }
582
583 @Test
584 public void empty() {
585 ConnectableFlowable<Integer> cf = Flowable.<Integer>empty().publish();
586
587 cf.connect();
588 }
589
590 @Test
591 public void take() {
592 ConnectableFlowable<Integer> cf = Flowable.range(1, 2).publish();
593
594 TestSubscriber<Integer> ts = cf.take(1).test();
595
596 cf.connect();
597
598 ts.assertResult(1);
599 }
600
601 @Test
602 public void just() {
603 final PublishProcessor<Integer> pp = PublishProcessor.create();
604
605 ConnectableFlowable<Integer> cf = pp.publish();
606
607 TestSubscriber<Integer> ts = new TestSubscriber<Integer>() {
608 @Override
609 public void onNext(Integer t) {
610 super.onNext(t);
611 pp.onComplete();
612 }
613 };
614
615 cf.subscribe(ts);
616 cf.connect();
617
618 pp.onNext(1);
619
620 ts.assertResult(1);
621 }
622
623 @Test
624 public void nextCancelRace() {
625 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
626
627 final PublishProcessor<Integer> pp = PublishProcessor.create();
628
629 final ConnectableFlowable<Integer> cf = pp.publish();
630
631 final TestSubscriber<Integer> ts = cf.test();
632
633 Runnable r1 = new Runnable() {
634 @Override
635 public void run() {
636 pp.onNext(1);
637 }
638 };
639
640 Runnable r2 = new Runnable() {
641 @Override
642 public void run() {
643 ts.cancel();
644 }
645 };
646
647 TestHelper.race(r1, r2);
648 }
649 }
650
651 @Test
652 public void badSource() {
653 List<Throwable> errors = TestHelper.trackPluginErrors();
654 try {
655 new Flowable<Integer>() {
656 @Override
657 protected void subscribeActual(Subscriber<? super Integer> subscriber) {
658 subscriber.onSubscribe(new BooleanSubscription());
659 subscriber.onNext(1);
660 subscriber.onComplete();
661 subscriber.onNext(2);
662 subscriber.onError(new TestException());
663 subscriber.onComplete();
664 }
665 }
666 .publish()
667 .autoConnect()
668 .test()
669 .assertResult(1);
670
671 TestHelper.assertUndeliverable(errors, 0, TestException.class);
672 } finally {
673 RxJavaPlugins.reset();
674 }
675 }
676
677 @Test
678 public void noErrorLoss() {
679 ConnectableFlowable<Object> cf = Flowable.error(new TestException()).publish();
680
681 cf.connect();
682
683 // 3.x: terminal events are always kept until reset.
684 cf.test()
685 .assertFailure(TestException.class);
686 }
687
688 @Test
689 public void subscribeDisconnectRace() {
690 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
691
692 final PublishProcessor<Integer> pp = PublishProcessor.create();
693
694 final ConnectableFlowable<Integer> cf = pp.publish();
695
696 final Disposable d = cf.connect();
697 final TestSubscriber<Integer> ts = new TestSubscriber<>();
698
699 Runnable r1 = new Runnable() {
700 @Override
701 public void run() {
702 d.dispose();
703 }
704 };
705
706 Runnable r2 = new Runnable() {
707 @Override
708 public void run() {
709 cf.subscribe(ts);
710 }
711 };
712
713 TestHelper.race(r1, r2);
714 }
715 }
716
717 @Test
718 public void selectorDisconnectsIndependentSource() {
719 PublishProcessor<Integer> pp = PublishProcessor.create();
720
721 pp.publish(new Function<Flowable<Integer>, Flowable<Integer>>() {
722 @Override
723 public Flowable<Integer> apply(Flowable<Integer> v) throws Exception {
724 return Flowable.range(1, 2);
725 }
726 })
727 .test()
728 .assertResult(1, 2);
729
730 assertFalse(pp.hasSubscribers());
731 }
732
733 @Test
734 public void selectorLatecommer() {
735 Flowable.range(1, 5)
736 .publish(new Function<Flowable<Integer>, Flowable<Integer>>() {
737 @Override
738 public Flowable<Integer> apply(Flowable<Integer> v) throws Exception {
739 return v.concatWith(v);
740 }
741 })
742 .test()
743 .assertResult(1, 2, 3, 4, 5);
744 }
745
746 @Test
747 public void mainError() {
748 Flowable.error(new TestException())
749 .publish(Functions.<Flowable<Object>>identity())
750 .test()
751 .assertFailure(TestException.class);
752 }
753
754 @Test
755 public void selectorInnerError() {
756 PublishProcessor<Integer> pp = PublishProcessor.create();
757
758 pp.publish(new Function<Flowable<Integer>, Flowable<Integer>>() {
759 @Override
760 public Flowable<Integer> apply(Flowable<Integer> v) throws Exception {
761 return Flowable.error(new TestException());
762 }
763 })
764 .test()
765 .assertFailure(TestException.class);
766
767 assertFalse(pp.hasSubscribers());
768 }
769
770 @Test
771 public void preNextConnect() {
772 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
773
774 final ConnectableFlowable<Integer> cf = Flowable.<Integer>empty().publish();
775
776 cf.connect();
777
778 Runnable r1 = new Runnable() {
779 @Override
780 public void run() {
781 cf.test();
782 }
783 };
784
785 TestHelper.race(r1, r1);
786 }
787 }
788
789 @Test
790 public void connectRace() {
791 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
792
793 final ConnectableFlowable<Integer> cf = Flowable.<Integer>empty().publish();
794
795 Runnable r1 = new Runnable() {
796 @Override
797 public void run() {
798 cf.connect();
799 }
800 };
801
802 TestHelper.race(r1, r1);
803 }
804 }
805
806 @Test
807 public void selectorCrash() {
808 Flowable.just(1).publish(new Function<Flowable<Integer>, Flowable<Object>>() {
809 @Override
810 public Flowable<Object> apply(Flowable<Integer> v) throws Exception {
811 throw new TestException();
812 }
813 })
814 .test()
815 .assertFailure(TestException.class);
816 }
817
818 @Test
819 public void pollThrows() {
820 Flowable.just(1)
821 .map(new Function<Integer, Object>() {
822 @Override
823 public Object apply(Integer v) throws Exception {
824 throw new TestException();
825 }
826 })
827 .compose(TestHelper.flowableStripBoundary())
828 .publish()
829 .autoConnect()
830 .test()
831 .assertFailure(TestException.class);
832 }
833
834 @Test
835 public void pollThrowsNoSubscribers() {
836 ConnectableFlowable<Integer> cf = Flowable.just(1, 2)
837 .map(new Function<Integer, Integer>() {
838 @Override
839 public Integer apply(Integer v) throws Exception {
840 if (v == 2) {
841 throw new TestException();
842 }
843 return v;
844 }
845 })
846 .compose(TestHelper.<Integer>flowableStripBoundary())
847 .publish();
848
849 TestSubscriber<Integer> ts = cf.take(1)
850 .test();
851
852 cf.connect();
853
854 ts.assertResult(1);
855 }
856
857 @Test
858 public void dryRunCrash() {
859 final TestSubscriber<Object> ts = new TestSubscriber<Object>(1L) {
860 @Override
861 public void onNext(Object t) {
862 super.onNext(t);
863 onComplete();
864 cancel();
865 }
866 };
867
868 Flowable<Object> source = Flowable.range(1, 10)
869 .map(new Function<Integer, Object>() {
870 @Override
871 public Object apply(Integer v) throws Exception {
872 if (v == 2) {
873 throw new TestException();
874 }
875 return v;
876 }
877 })
878 .publish()
879 .autoConnect();
880
881 source.subscribe(ts);
882
883 ts
884 .assertResult(1);
885
886 // 3.x: terminal events remain observable until reset
887 source.test()
888 .assertFailure(TestException.class);
889 }
890
891 @Test
892 public void overflowQueue() {
893 List<Throwable> errors = TestHelper.trackPluginErrors();
894 try {
895 Flowable.create(new FlowableOnSubscribe<Object>() {
896 @Override
897 public void subscribe(FlowableEmitter<Object> s) throws Exception {
898 for (int i = 0; i < 10; i++) {
899 s.onNext(i);
900 }
901 }
902 }, BackpressureStrategy.MISSING)
903 .publish(8)
904 .autoConnect()
905 .test(0L)
906 // 3.x emits errors last, even the full queue errors
907 .requestMore(10)
908 .assertFailure(MissingBackpressureException.class, 0, 1, 2, 3, 4, 5, 6, 7);
909
910 TestHelper.assertError(errors, 0, MissingBackpressureException.class);
911 } finally {
912 RxJavaPlugins.reset();
913 }
914 }
915
916 @Test
917 public void delayedUpstreamOnSubscribe() {
918 final Subscriber<?>[] sub = { null };
919
920 new Flowable<Integer>() {
921 @Override
922 protected void subscribeActual(Subscriber<? super Integer> s) {
923 sub[0] = s;
924 }
925 }
926 .publish()
927 .connect()
928 .dispose();
929
930 BooleanSubscription bs = new BooleanSubscription();
931
932 sub[0].onSubscribe(bs);
933
934 assertTrue(bs.isCancelled());
935 }
936
937 @Test
938 public void disposeRace() {
939 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
940
941 final AtomicReference<Disposable> ref = new AtomicReference<>();
942
943 final ConnectableFlowable<Integer> cf = new Flowable<Integer>() {
944 @Override
945 protected void subscribeActual(Subscriber<? super Integer> s) {
946 s.onSubscribe(new BooleanSubscription());
947 ref.set((Disposable)s);
948 }
949 }.publish();
950
951 cf.connect();
952
953 Runnable r1 = new Runnable() {
954 @Override
955 public void run() {
956 ref.get().dispose();
957 }
958 };
959
960 TestHelper.race(r1, r1);
961 }
962 }
963
964 @Test
965 public void removeNotPresent() {
966 final AtomicReference<PublishConnection<Integer>> ref = new AtomicReference<>();
967
968 final ConnectableFlowable<Integer> cf = new Flowable<Integer>() {
969 @Override
970 @SuppressWarnings("unchecked")
971 protected void subscribeActual(Subscriber<? super Integer> s) {
972 s.onSubscribe(new BooleanSubscription());
973 ref.set((PublishConnection<Integer>)s);
974 }
975 }.publish();
976
977 cf.connect();
978
979 ref.get().add(new InnerSubscription<>(new TestSubscriber<>(), ref.get()));
980 ref.get().remove(null);
981 }
982
983 @Test
984 public void subscriberSwap() {
985 final ConnectableFlowable<Integer> cf = Flowable.range(1, 5).publish();
986
987 cf.connect();
988
989 TestSubscriber<Integer> ts1 = new TestSubscriber<Integer>() {
990 @Override
991 public void onNext(Integer t) {
992 super.onNext(t);
993 cancel();
994 onComplete();
995 }
996 };
997
998 cf.subscribe(ts1);
999
1000 ts1.assertResult(1);
1001
1002 TestSubscriber<Integer> ts2 = new TestSubscriber<>(0);
1003 cf.subscribe(ts2);
1004
1005 ts2
1006 .assertEmpty()
1007 .requestMore(4)
1008 .assertResult(2, 3, 4, 5);
1009 }
1010
1011 @Test
1012 public void subscriberLiveSwap() {
1013 final ConnectableFlowable<Integer> cf = Flowable.range(1, 5).publish();
1014
1015 final TestSubscriber<Integer> ts2 = new TestSubscriber<>(0);
1016
1017 TestSubscriber<Integer> ts1 = new TestSubscriber<Integer>() {
1018 @Override
1019 public void onNext(Integer t) {
1020 super.onNext(t);
1021 cancel();
1022 onComplete();
1023 cf.subscribe(ts2);
1024 }
1025 };
1026
1027 cf.subscribe(ts1);
1028
1029 cf.connect();
1030
1031 ts1.assertResult(1);
1032
1033 ts2
1034 .assertEmpty()
1035 .requestMore(4)
1036 .assertResult(2, 3, 4, 5);
1037 }
1038
1039 @Test
1040 public void selectorSubscriberSwap() {
1041 final AtomicReference<Flowable<Integer>> ref = new AtomicReference<>();
1042
1043 Flowable.range(1, 5).publish(new Function<Flowable<Integer>, Publisher<Integer>>() {
1044 @Override
1045 public Publisher<Integer> apply(Flowable<Integer> f) throws Exception {
1046 ref.set(f);
1047 return Flowable.never();
1048 }
1049 }).test();
1050
1051 ref.get().take(2).test().assertResult(1, 2);
1052
1053 ref.get()
1054 .test(0)
1055 .assertEmpty()
1056 .requestMore(2)
1057 .assertValuesOnly(3, 4)
1058 .requestMore(1)
1059 .assertResult(3, 4, 5);
1060 }
1061
1062 @Test
1063 public void leavingSubscriberOverrequests() {
1064 final AtomicReference<Flowable<Integer>> ref = new AtomicReference<>();
1065
1066 PublishProcessor<Integer> pp = PublishProcessor.create();
1067
1068 pp.publish(new Function<Flowable<Integer>, Publisher<Integer>>() {
1069 @Override
1070 public Publisher<Integer> apply(Flowable<Integer> f) throws Exception {
1071 ref.set(f);
1072 return Flowable.never();
1073 }
1074 }).test();
1075
1076 TestSubscriber<Integer> ts1 = ref.get().take(2).test();
1077
1078 pp.onNext(1);
1079 pp.onNext(2);
1080
1081 ts1.assertResult(1, 2);
1082
1083 pp.onNext(3);
1084 pp.onNext(4);
1085
1086 TestSubscriber<Integer> ts2 = ref.get().test(0L);
1087
1088 ts2.assertEmpty();
1089
1090 ts2.requestMore(2);
1091
1092 ts2.assertValuesOnly(3, 4);
1093 }
1094
1095 // call a transformer only if the input is non-empty
1096 @Test
1097 public void composeIfNotEmpty() {
1098 final FlowableTransformer<Integer, Integer> transformer = new FlowableTransformer<Integer, Integer>() {
1099 @Override
1100 public Publisher<Integer> apply(Flowable<Integer> g) {
1101 return g.map(new Function<Integer, Integer>() {
1102 @Override
1103 public Integer apply(Integer v) throws Exception {
1104 return v + 1;
1105 }
1106 });
1107 }
1108 };
1109
1110 final AtomicInteger calls = new AtomicInteger();
1111 Flowable.range(1, 5)
1112 .publish(new Function<Flowable<Integer>, Publisher<Integer>>() {
1113 @Override
1114 public Publisher<Integer> apply(final Flowable<Integer> shared)
1115 throws Exception {
1116 return shared.take(1).concatMap(new Function<Integer, Publisher<? extends Integer>>() {
1117 @Override
1118 public Publisher<? extends Integer> apply(Integer first)
1119 throws Exception {
1120 calls.incrementAndGet();
1121 return transformer.apply(Flowable.just(first).concatWith(shared));
1122 }
1123 });
1124 }
1125 })
1126 .test()
1127 .assertResult(2, 3, 4, 5, 6);
1128
1129 assertEquals(1, calls.get());
1130 }
1131
1132 // call a transformer only if the input is non-empty
1133 @Test
1134 public void composeIfNotEmptyNotFused() {
1135 final FlowableTransformer<Integer, Integer> transformer = new FlowableTransformer<Integer, Integer>() {
1136 @Override
1137 public Publisher<Integer> apply(Flowable<Integer> g) {
1138 return g.map(new Function<Integer, Integer>() {
1139 @Override
1140 public Integer apply(Integer v) throws Exception {
1141 return v + 1;
1142 }
1143 });
1144 }
1145 };
1146
1147 final AtomicInteger calls = new AtomicInteger();
1148 Flowable.range(1, 5).hide()
1149 .publish(new Function<Flowable<Integer>, Publisher<Integer>>() {
1150 @Override
1151 public Publisher<Integer> apply(final Flowable<Integer> shared)
1152 throws Exception {
1153 return shared.take(1).concatMap(new Function<Integer, Publisher<? extends Integer>>() {
1154 @Override
1155 public Publisher<? extends Integer> apply(Integer first)
1156 throws Exception {
1157 calls.incrementAndGet();
1158 return transformer.apply(Flowable.just(first).concatWith(shared));
1159 }
1160 });
1161 }
1162 })
1163 .test()
1164 .assertResult(2, 3, 4, 5, 6);
1165
1166 assertEquals(1, calls.get());
1167 }
1168
1169 // call a transformer only if the input is non-empty
1170 @Test
1171 public void composeIfNotEmptyIsEmpty() {
1172 final FlowableTransformer<Integer, Integer> transformer = new FlowableTransformer<Integer, Integer>() {
1173 @Override
1174 public Publisher<Integer> apply(Flowable<Integer> g) {
1175 return g.map(new Function<Integer, Integer>() {
1176 @Override
1177 public Integer apply(Integer v) throws Exception {
1178 return v + 1;
1179 }
1180 });
1181 }
1182 };
1183
1184 final AtomicInteger calls = new AtomicInteger();
1185 Flowable.<Integer>empty().hide()
1186 .publish(new Function<Flowable<Integer>, Publisher<Integer>>() {
1187 @Override
1188 public Publisher<Integer> apply(final Flowable<Integer> shared)
1189 throws Exception {
1190 return shared.take(1).concatMap(new Function<Integer, Publisher<? extends Integer>>() {
1191 @Override
1192 public Publisher<? extends Integer> apply(Integer first)
1193 throws Exception {
1194 calls.incrementAndGet();
1195 return transformer.apply(Flowable.just(first).concatWith(shared));
1196 }
1197 });
1198 }
1199 })
1200 .test()
1201 .assertResult();
1202
1203 assertEquals(0, calls.get());
1204 }
1205
1206 @Test
1207 public void publishFunctionCancelOuterAfterOneInner() {
1208 final AtomicReference<Flowable<Integer>> ref = new AtomicReference<>();
1209
1210 PublishProcessor<Integer> pp = PublishProcessor.create();
1211
1212 final TestSubscriber<Integer> ts = pp.publish(new Function<Flowable<Integer>, Publisher<Integer>>() {
1213 @Override
1214 public Publisher<Integer> apply(Flowable<Integer> f) throws Exception {
1215 ref.set(f);
1216 return Flowable.never();
1217 }
1218 }).test();
1219
1220 ref.get().subscribe(new TestSubscriber<Integer>() {
1221 @Override
1222 public void onNext(Integer t) {
1223 super.onNext(t);
1224 onComplete();
1225 ts.cancel();
1226 }
1227 });
1228
1229 pp.onNext(1);
1230 }
1231
1232 @Test
1233 public void publishFunctionCancelOuterAfterOneInnerBackpressured() {
1234 final AtomicReference<Flowable<Integer>> ref = new AtomicReference<>();
1235
1236 PublishProcessor<Integer> pp = PublishProcessor.create();
1237
1238 final TestSubscriber<Integer> ts = pp.publish(new Function<Flowable<Integer>, Publisher<Integer>>() {
1239 @Override
1240 public Publisher<Integer> apply(Flowable<Integer> f) throws Exception {
1241 ref.set(f);
1242 return Flowable.never();
1243 }
1244 }).test();
1245
1246 ref.get().subscribe(new TestSubscriber<Integer>(1L) {
1247 @Override
1248 public void onNext(Integer t) {
1249 super.onNext(t);
1250 onComplete();
1251 ts.cancel();
1252 }
1253 });
1254
1255 pp.onNext(1);
1256 }
1257
1258 @Test
1259 public void publishCancelOneAsync() {
1260 for (int i = 0; i < TestHelper.RACE_LONG_LOOPS; i++) {
1261
1262 final PublishProcessor<Integer> pp = PublishProcessor.create();
1263
1264 final AtomicReference<Flowable<Integer>> ref = new AtomicReference<>();
1265
1266 pp.publish(new Function<Flowable<Integer>, Publisher<Integer>>() {
1267 @Override
1268 public Publisher<Integer> apply(Flowable<Integer> f) throws Exception {
1269 ref.set(f);
1270 return Flowable.never();
1271 }
1272 }).test();
1273
1274 final TestSubscriber<Integer> ts1 = ref.get().test();
1275 TestSubscriber<Integer> ts2 = ref.get().test();
1276
1277 Runnable r1 = new Runnable() {
1278 @Override
1279 public void run() {
1280 pp.onNext(1);
1281 }
1282 };
1283
1284 Runnable r2 = new Runnable() {
1285 @Override
1286 public void run() {
1287 ts1.cancel();
1288 }
1289 };
1290
1291 TestHelper.race(r1, r2);
1292
1293 ts2.assertValuesOnly(1);
1294 }
1295 }
1296
1297 @Test
1298 public void publishCancelOneAsync2() {
1299 final PublishProcessor<Integer> pp = PublishProcessor.create();
1300
1301 ConnectableFlowable<Integer> cf = pp.publish();
1302
1303 final TestSubscriber<Integer> ts1 = new TestSubscriber<>();
1304
1305 final AtomicReference<InnerSubscription<Integer>> ref = new AtomicReference<>();
1306
1307 cf.subscribe(new FlowableSubscriber<Integer>() {
1308 @SuppressWarnings("unchecked")
1309 @Override
1310 public void onSubscribe(Subscription s) {
1311 ts1.onSubscribe(new BooleanSubscription());
1312 // pretend to be cancelled without removing it from the subscriber list
1313 ref.set((InnerSubscription<Integer>)s);
1314 }
1315
1316 @Override
1317 public void onNext(Integer t) {
1318 ts1.onNext(t);
1319 }
1320
1321 @Override
1322 public void onError(Throwable t) {
1323 ts1.onError(t);
1324 }
1325
1326 @Override
1327 public void onComplete() {
1328 ts1.onComplete();
1329 }
1330 });
1331 TestSubscriber<Integer> ts2 = cf.test();
1332
1333 cf.connect();
1334
1335 ref.get().set(Long.MIN_VALUE);
1336
1337 pp.onNext(1);
1338
1339 ts1.assertEmpty();
1340 ts2.assertValuesOnly(1);
1341 }
1342
1343 @Test
1344 public void boundaryFusion() {
1345 Flowable.range(1, 10000)
1346 .observeOn(Schedulers.single())
1347 .map(new Function<Integer, String>() {
1348 @Override
1349 public String apply(Integer t) throws Exception {
1350 String name = Thread.currentThread().getName();
1351 if (name.contains("RxSingleScheduler")) {
1352 return "RxSingleScheduler";
1353 }
1354 return name;
1355 }
1356 })
1357 .share()
1358 .observeOn(Schedulers.computation())
1359 .distinct()
1360 .test()
1361 .awaitDone(5, TimeUnit.SECONDS)
1362 .assertResult("RxSingleScheduler");
1363 }
1364
1365 @Test
1366 public void badRequest() {
1367 TestHelper.assertBadRequestReported(Flowable.range(1, 5).publish());
1368 }
1369
1370 @Test
1371 public void splitCombineSubscriberChangeAfterOnNext() {
1372 Flowable<Integer> source = Flowable.range(0, 20)
1373 .doOnSubscribe(new Consumer<Subscription>() {
1374 @Override
1375 public void accept(Subscription v) throws Exception {
1376 System.out.println("Subscribed");
1377 }
1378 })
1379 .publish(10)
1380 .refCount()
1381 ;
1382
1383 Flowable<Integer> evenNumbers = source.filter(new Predicate<Integer>() {
1384 @Override
1385 public boolean test(Integer v) throws Exception {
1386 return v % 2 == 0;
1387 }
1388 });
1389
1390 Flowable<Integer> oddNumbers = source.filter(new Predicate<Integer>() {
1391 @Override
1392 public boolean test(Integer v) throws Exception {
1393 return v % 2 != 0;
1394 }
1395 });
1396
1397 final Single<Integer> getNextOdd = oddNumbers.first(0);
1398
1399 TestSubscriber<List<Integer>> ts = evenNumbers.concatMap(new Function<Integer, Publisher<List<Integer>>>() {
1400 @Override
1401 public Publisher<List<Integer>> apply(Integer v) throws Exception {
1402 return Single.zip(
1403 Single.just(v), getNextOdd,
1404 new BiFunction<Integer, Integer, List<Integer>>() {
1405 @Override
1406 public List<Integer> apply(Integer a, Integer b) throws Exception {
1407 return Arrays.asList( a, b );
1408 }
1409 }
1410 )
1411 .toFlowable();
1412 }
1413 })
1414 .takeWhile(new Predicate<List<Integer>>() {
1415 @Override
1416 public boolean test(List<Integer> v) throws Exception {
1417 return v.get(0) < 20;
1418 }
1419 })
1420 .test();
1421
1422 ts
1423 .assertResult(
1424 Arrays.asList(0, 1),
1425 Arrays.asList(2, 3),
1426 Arrays.asList(4, 5),
1427 Arrays.asList(6, 7),
1428 Arrays.asList(8, 9),
1429 Arrays.asList(10, 11),
1430 Arrays.asList(12, 13),
1431 Arrays.asList(14, 15),
1432 Arrays.asList(16, 17),
1433 Arrays.asList(18, 19)
1434 );
1435 }
1436
1437 @Test
1438 public void splitCombineSubscriberChangeAfterOnNextFused() {
1439 Flowable<Integer> source = Flowable.range(0, 20)
1440 .publish(10)
1441 .refCount()
1442 ;
1443
1444 Flowable<Integer> evenNumbers = source.filter(new Predicate<Integer>() {
1445 @Override
1446 public boolean test(Integer v) throws Exception {
1447 return v % 2 == 0;
1448 }
1449 });
1450
1451 Flowable<Integer> oddNumbers = source.filter(new Predicate<Integer>() {
1452 @Override
1453 public boolean test(Integer v) throws Exception {
1454 return v % 2 != 0;
1455 }
1456 });
1457
1458 final Single<Integer> getNextOdd = oddNumbers.first(0);
1459
1460 TestSubscriber<List<Integer>> ts = evenNumbers.concatMap(new Function<Integer, Publisher<List<Integer>>>() {
1461 @Override
1462 public Publisher<List<Integer>> apply(Integer v) throws Exception {
1463 return Single.zip(
1464 Single.just(v), getNextOdd,
1465 new BiFunction<Integer, Integer, List<Integer>>() {
1466 @Override
1467 public List<Integer> apply(Integer a, Integer b) throws Exception {
1468 return Arrays.asList( a, b );
1469 }
1470 }
1471 )
1472 .toFlowable();
1473 }
1474 })
1475 .takeWhile(new Predicate<List<Integer>>() {
1476 @Override
1477 public boolean test(List<Integer> v) throws Exception {
1478 return v.get(0) < 20;
1479 }
1480 })
1481 .test();
1482
1483 ts
1484 .assertResult(
1485 Arrays.asList(0, 1),
1486 Arrays.asList(2, 3),
1487 Arrays.asList(4, 5),
1488 Arrays.asList(6, 7),
1489 Arrays.asList(8, 9),
1490 Arrays.asList(10, 11),
1491 Arrays.asList(12, 13),
1492 Arrays.asList(14, 15),
1493 Arrays.asList(16, 17),
1494 Arrays.asList(18, 19)
1495 );
1496 }
1497
1498 @Test
1499 public void altConnectCrash() {
1500 try {
1501 new FlowablePublish<>(Flowable.<Integer>empty(), 128)
1502 .connect(new Consumer<Disposable>() {
1503 @Override
1504 public void accept(Disposable t) throws Exception {
1505 throw new TestException();
1506 }
1507 });
1508 fail("Should have thrown");
1509 } catch (TestException expected) {
1510 // expected
1511 }
1512 }
1513
1514 @Test
1515 public void altConnectRace() {
1516 for (int i = 0; i < TestHelper.RACE_LONG_LOOPS; i++) {
1517 final ConnectableFlowable<Integer> cf =
1518 new FlowablePublish<>(Flowable.<Integer>never(), 128);
1519
1520 Runnable r = new Runnable() {
1521 @Override
1522 public void run() {
1523 cf.connect();
1524 }
1525 };
1526
1527 TestHelper.race(r, r);
1528 }
1529 }
1530
1531 @Test
1532 public void fusedPollCrash() {
1533 Flowable.range(1, 5)
1534 .map(new Function<Integer, Object>() {
1535 @Override
1536 public Object apply(Integer v) throws Exception {
1537 throw new TestException();
1538 }
1539 })
1540 .compose(TestHelper.flowableStripBoundary())
1541 .publish()
1542 .refCount()
1543 .test()
1544 .assertFailure(TestException.class);
1545 }
1546
1547 @Test
1548 public void syncFusedNoRequest() {
1549 Flowable.range(1, 5)
1550 .publish(1)
1551 .refCount()
1552 .test()
1553 .assertResult(1, 2, 3, 4, 5);
1554 }
1555
1556 @Test
1557 public void normalBackpressuredPolls() {
1558 Flowable.range(1, 5)
1559 .hide()
1560 .publish(1)
1561 .refCount()
1562 .test()
1563 .assertResult(1, 2, 3, 4, 5);
1564 }
1565
1566 @Test
1567 public void emptyHidden() {
1568 Flowable.empty()
1569 .hide()
1570 .publish(1)
1571 .refCount()
1572 .test()
1573 .assertResult();
1574 }
1575
1576 @Test
1577 public void emptyFused() {
1578 Flowable.empty()
1579 .publish(1)
1580 .refCount()
1581 .test()
1582 .assertResult();
1583 }
1584
1585 @Test
1586 public void overflowQueueRefCount() {
1587 new Flowable<Integer>() {
1588 @Override
1589 protected void subscribeActual(Subscriber<? super Integer> s) {
1590 s.onSubscribe(new BooleanSubscription());
1591 s.onNext(1);
1592 s.onNext(2);
1593 }
1594 }
1595 .publish(1)
1596 .refCount()
1597 .test(0)
1598 .requestMore(1)
1599 .assertFailure(MissingBackpressureException.class, 1);
1600 }
1601
1602 @Test
1603 public void doubleErrorRefCount() {
1604 List<Throwable> errors = TestHelper.trackPluginErrors();
1605 try {
1606 new Flowable<Integer>() {
1607 @Override
1608 protected void subscribeActual(Subscriber<? super Integer> s) {
1609 s.onSubscribe(new BooleanSubscription());
1610 s.onError(new TestException("one"));
1611 s.onError(new TestException("two"));
1612 }
1613 }
1614 .publish(1)
1615 .refCount()
1616 .to(TestHelper.<Integer>testSubscriber(0L))
1617 .assertFailureAndMessage(TestException.class, "one");
1618
1619 TestHelper.assertUndeliverable(errors, 0, TestException.class, "two");
1620 assertEquals(1, errors.size());
1621 } finally {
1622 RxJavaPlugins.reset();
1623 }
1624 }
1625
1626 @Test
1627 public void onCompleteAvailableUntilReset() {
1628 ConnectableFlowable<Integer> cf = Flowable.just(1).publish();
1629
1630 TestSubscriber<Integer> ts = cf.test();
1631 ts.assertEmpty();
1632
1633 cf.connect();
1634
1635 ts.assertResult(1);
1636
1637 cf.test().assertResult();
1638
1639 cf.reset();
1640
1641 ts = cf.test();
1642 ts.assertEmpty();
1643
1644 cf.connect();
1645
1646 ts.assertResult(1);
1647 }
1648
1649 @Test
1650 public void onErrorAvailableUntilReset() {
1651 ConnectableFlowable<Integer> cf = Flowable.just(1)
1652 .concatWith(Flowable.<Integer>error(new TestException()))
1653 .publish();
1654
1655 TestSubscriber<Integer> ts = cf.test();
1656 ts.assertEmpty();
1657
1658 cf.connect();
1659
1660 ts.assertFailure(TestException.class, 1);
1661
1662 cf.test().assertFailure(TestException.class);
1663
1664 cf.reset();
1665
1666 ts = cf.test();
1667 ts.assertEmpty();
1668
1669 cf.connect();
1670
1671 ts.assertFailure(TestException.class, 1);
1672 }
1673
1674 @Test
1675 public void disposeResets() {
1676 PublishProcessor<Integer> pp = PublishProcessor.create();
1677
1678 ConnectableFlowable<Integer> cf = pp.publish();
1679
1680 assertFalse(pp.hasSubscribers());
1681
1682 Disposable d = cf.connect();
1683
1684 assertTrue(pp.hasSubscribers());
1685
1686 d.dispose();
1687
1688 assertFalse(pp.hasSubscribers());
1689
1690 TestSubscriber<Integer> ts = cf.test();
1691
1692 cf.connect();
1693
1694 assertTrue(pp.hasSubscribers());
1695
1696 pp.onNext(1);
1697
1698 ts.assertValuesOnly(1);
1699 }
1700
1701 @Test(expected = TestException.class)
1702 public void connectDisposeCrash() {
1703 ConnectableFlowable<Object> cf = Flowable.never().publish();
1704
1705 cf.connect();
1706
1707 cf.connect(d -> { throw new TestException(); });
1708 }
1709
1710 @Test
1711 public void resetWhileNotConnectedIsNoOp() {
1712 ConnectableFlowable<Object> cf = Flowable.never().publish();
1713
1714 cf.reset();
1715 }
1716
1717 @Test
1718 public void resetWhileActiveIsNoOp() {
1719 ConnectableFlowable<Object> cf = Flowable.never().publish();
1720
1721 cf.connect();
1722
1723 cf.reset();
1724 }
1725
1726 @Test
1727 public void crossCancelOnComplete() {
1728 TestSubscriber<Integer> ts1 = new TestSubscriber<>();
1729 TestSubscriber<Integer> ts2 = new TestSubscriber<Integer>() {
1730 @Override
1731 public void onComplete() {
1732 super.onComplete();
1733 ts1.cancel();
1734 }
1735 };
1736
1737 PublishProcessor<Integer> pp = PublishProcessor.create();
1738
1739 ConnectableFlowable<Integer> cf = pp.publish();
1740
1741 cf.subscribe(ts2);
1742 cf.subscribe(ts1);
1743
1744 cf.connect();
1745
1746 pp.onComplete();
1747
1748 ts2.assertResult();
1749
1750 ts1.assertEmpty();
1751 }
1752
1753 @Test
1754 public void crossCancelOnError() {
1755 TestSubscriber<Integer> ts1 = new TestSubscriber<>();
1756 TestSubscriber<Integer> ts2 = new TestSubscriber<Integer>() {
1757 @Override
1758 public void onError(Throwable t) {
1759 super.onError(t);
1760 ts1.cancel();
1761 }
1762 };
1763
1764 PublishProcessor<Integer> pp = PublishProcessor.create();
1765
1766 ConnectableFlowable<Integer> cf = pp.publish();
1767
1768 cf.subscribe(ts2);
1769 cf.subscribe(ts1);
1770
1771 cf.connect();
1772
1773 pp.onError(new TestException());
1774
1775 ts2.assertFailure(TestException.class);
1776
1777 ts1.assertEmpty();
1778 }
1779
1780 @Test
1781 public void disposeNoNeedForReset() {
1782 PublishProcessor<Integer> pp = PublishProcessor.create();
1783
1784 ConnectableFlowable<Integer> cf = pp.publish();
1785
1786 TestSubscriber<Integer> ts = cf.test();
1787
1788 Disposable d = cf.connect();
1789
1790 pp.onNext(1);
1791
1792 d.dispose();
1793
1794 ts = cf.test();
1795
1796 ts.assertEmpty();
1797
1798 cf.connect();
1799
1800 ts.assertEmpty();
1801
1802 pp.onNext(2);
1803
1804 ts.assertValuesOnly(2);
1805 }
1806 }