Debian Codegraph @

src/test/java/io/reactivex/rxjava3/processors/MulticastProcessorTest.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.processors;
15
16 import static org.junit.Assert.*;
17
18 import java.io.IOException;
19 import java.util.List;
20 import java.util.concurrent.TimeUnit;
21
22 import org.junit.Test;
23 import org.reactivestreams.Subscription;
24
25 import io.reactivex.rxjava3.core.*;
26 import io.reactivex.rxjava3.exceptions.*;
27 import io.reactivex.rxjava3.functions.Function;
28 import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription;
29 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
30 import io.reactivex.rxjava3.subscribers.TestSubscriber;
31 import io.reactivex.rxjava3.testsupport.TestHelper;
32
33 public class MulticastProcessorTest extends RxJavaTest {
34
35 @Test
36 public void complete() {
37 MulticastProcessor<Integer> mp = MulticastProcessor.create();
38 mp.start();
39
40 assertFalse(mp.hasSubscribers());
41 assertFalse(mp.hasComplete());
42 assertFalse(mp.hasThrowable());
43 assertNull(mp.getThrowable());
44
45 TestSubscriber<Integer> ts = mp.test();
46
47 assertTrue(mp.hasSubscribers());
48 assertFalse(mp.hasComplete());
49 assertFalse(mp.hasThrowable());
50 assertNull(mp.getThrowable());
51
52 mp.onNext(1);
53 mp.onComplete();
54
55 ts.assertResult(1);
56
57 assertFalse(mp.hasSubscribers());
58 assertTrue(mp.hasComplete());
59 assertFalse(mp.hasThrowable());
60 assertNull(mp.getThrowable());
61
62 mp.test().assertResult();
63 }
64
65 @Test
66 public void error() {
67 MulticastProcessor<Integer> mp = MulticastProcessor.create();
68 mp.start();
69
70 assertFalse(mp.hasSubscribers());
71 assertFalse(mp.hasComplete());
72 assertFalse(mp.hasThrowable());
73 assertNull(mp.getThrowable());
74
75 TestSubscriber<Integer> ts = mp.test();
76
77 assertTrue(mp.hasSubscribers());
78 assertFalse(mp.hasComplete());
79 assertFalse(mp.hasThrowable());
80 assertNull(mp.getThrowable());
81
82 mp.onNext(1);
83 mp.onError(new IOException());
84
85 ts.assertFailure(IOException.class, 1);
86
87 assertFalse(mp.hasSubscribers());
88 assertFalse(mp.hasComplete());
89 assertTrue(mp.hasThrowable());
90 assertNotNull(mp.getThrowable());
91 assertTrue("" + mp.getThrowable(), mp.getThrowable() instanceof IOException);
92
93 mp.test().assertFailure(IOException.class);
94 }
95
96 @Test
97 public void overflow() {
98 MulticastProcessor<Integer> mp = MulticastProcessor.create(1);
99 mp.start();
100
101 TestSubscriber<Integer> ts = mp.test(0);
102
103 assertTrue(mp.offer(1));
104 assertFalse(mp.offer(2));
105
106 mp.onNext(3);
107
108 ts.assertEmpty();
109
110 ts.request(1);
111
112 ts.assertFailure(MissingBackpressureException.class, 1);
113
114 mp.test().assertFailure(MissingBackpressureException.class);
115 }
116
117 @Test
118 public void backpressure() {
119 MulticastProcessor<Integer> mp = MulticastProcessor.create(16, false);
120 mp.start();
121
122 for (int i = 0; i < 10; i++) {
123 mp.onNext(i);
124 }
125 mp.onComplete();
126
127 mp.test(0)
128 .assertEmpty()
129 .requestMore(1)
130 .assertValuesOnly(0)
131 .requestMore(2)
132 .assertValuesOnly(0, 1, 2)
133 .requestMore(3)
134 .assertValuesOnly(0, 1, 2, 3, 4, 5)
135 .requestMore(4)
136 .assertResult(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
137 }
138
139 @Test
140 public void refCounted() {
141 MulticastProcessor<Integer> mp = MulticastProcessor.create(true);
142 BooleanSubscription bs = new BooleanSubscription();
143
144 mp.onSubscribe(bs);
145
146 assertFalse(bs.isCancelled());
147
148 mp.test().cancel();
149
150 assertTrue(bs.isCancelled());
151
152 assertFalse(mp.hasSubscribers());
153 assertTrue(mp.hasComplete());
154 assertFalse(mp.hasThrowable());
155 assertNull(mp.getThrowable());
156
157 mp.test().assertResult();
158 }
159
160 @Test
161 public void refCounted2() {
162 MulticastProcessor<Integer> mp = MulticastProcessor.create(16, true);
163 BooleanSubscription bs = new BooleanSubscription();
164
165 mp.onSubscribe(bs);
166
167 assertFalse(bs.isCancelled());
168
169 mp.test(1, true);
170
171 assertTrue(bs.isCancelled());
172
173 assertFalse(mp.hasSubscribers());
174 assertTrue(mp.hasComplete());
175 assertFalse(mp.hasThrowable());
176 assertNull(mp.getThrowable());
177
178 mp.test().assertResult();
179 }
180
181 @Test
182 public void longRunning() {
183 MulticastProcessor<Integer> mp = MulticastProcessor.create(16);
184 Flowable.range(1, 1000).subscribe(mp);
185
186 mp.test().assertValueCount(1000).assertNoErrors().assertComplete();
187 }
188
189 @Test
190 public void oneByOne() {
191 MulticastProcessor<Integer> mp = MulticastProcessor.create(16);
192 Flowable.range(1, 1000).subscribe(mp);
193
194 mp
195 .rebatchRequests(1)
196 .test()
197 .assertValueCount(1000)
198 .assertNoErrors()
199 .assertComplete();
200 }
201
202 @Test
203 public void take() {
204 MulticastProcessor<Integer> mp = MulticastProcessor.create(16);
205 Flowable.range(1, 1000).subscribe(mp);
206
207 mp.take(10).test().assertResult(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
208 }
209
210 @Test
211 public void takeRefCount() {
212 MulticastProcessor<Integer> mp = MulticastProcessor.create(16, true);
213 Flowable.range(1, 1000).subscribe(mp);
214
215 mp.take(10).test().assertResult(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
216 }
217
218 @Test
219 public void takeRefCountExact() {
220 MulticastProcessor<Integer> mp = MulticastProcessor.create(16, true);
221 Flowable.range(1, 10).subscribe(mp);
222
223 mp
224 .rebatchRequests(10)
225 .take(10)
226 .test().assertResult(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
227 }
228
229 @Test
230 public void crossCancel() {
231
232 final TestSubscriber<Integer> ts1 = new TestSubscriber<>();
233
234 TestSubscriber<Integer> ts2 = new TestSubscriber<Integer>() {
235 @Override
236 public void onNext(Integer t) {
237 super.onNext(t);
238 ts1.cancel();
239 ts1.onComplete();
240 }
241 };
242
243 MulticastProcessor<Integer> mp = MulticastProcessor.create(false);
244
245 mp.subscribe(ts2);
246 mp.subscribe(ts1);
247
248 mp.start();
249
250 mp.onNext(1);
251 mp.onComplete();
252
253 ts1.assertResult();
254 ts2.assertResult(1);
255 }
256
257 @Test
258 public void crossCancelError() {
259
260 final TestSubscriber<Integer> ts1 = new TestSubscriber<>();
261
262 TestSubscriber<Integer> ts2 = new TestSubscriber<Integer>() {
263 @Override
264 public void onError(Throwable t) {
265 super.onError(t);
266 ts1.cancel();
267 ts1.onComplete();
268 }
269 };
270
271 MulticastProcessor<Integer> mp = MulticastProcessor.create(false);
272
273 mp.subscribe(ts2);
274 mp.subscribe(ts1);
275
276 mp.start();
277
278 mp.onNext(1);
279 mp.onError(new IOException());
280
281 ts1.assertResult(1);
282 ts2.assertFailure(IOException.class, 1);
283 }
284
285 @Test
286 public void crossCancelComplete() {
287
288 final TestSubscriber<Integer> ts1 = new TestSubscriber<>();
289
290 TestSubscriber<Integer> ts2 = new TestSubscriber<Integer>() {
291 @Override
292 public void onComplete() {
293 super.onComplete();
294 ts1.cancel();
295 ts1.onNext(2);
296 ts1.onComplete();
297 }
298 };
299
300 MulticastProcessor<Integer> mp = MulticastProcessor.create(false);
301
302 mp.subscribe(ts2);
303 mp.subscribe(ts1);
304
305 mp.start();
306
307 mp.onNext(1);
308 mp.onComplete();
309
310 ts1.assertResult(1, 2);
311 ts2.assertResult(1);
312 }
313
314 @Test
315 public void crossCancel1() {
316
317 final TestSubscriber<Integer> ts1 = new TestSubscriber<>(1);
318
319 TestSubscriber<Integer> ts2 = new TestSubscriber<Integer>(1) {
320 @Override
321 public void onNext(Integer t) {
322 super.onNext(t);
323 ts1.cancel();
324 ts1.onComplete();
325 }
326 };
327
328 MulticastProcessor<Integer> mp = MulticastProcessor.create(false);
329
330 mp.subscribe(ts2);
331 mp.subscribe(ts1);
332
333 mp.start();
334
335 mp.onNext(1);
336 mp.onComplete();
337
338 ts1.assertResult();
339 ts2.assertResult(1);
340 }
341
342 @Test
343 public void requestCancel() {
344 List<Throwable> errors = TestHelper.trackPluginErrors();
345 try {
346 MulticastProcessor<Integer> mp = MulticastProcessor.create(false);
347
348 mp.subscribe(new FlowableSubscriber<Integer>() {
349
350 @Override
351 public void onNext(Integer t) {
352 }
353
354 @Override
355 public void onError(Throwable t) {
356 }
357
358 @Override
359 public void onComplete() {
360 }
361
362 @Override
363 public void onSubscribe(Subscription t) {
364 t.request(-1);
365 t.request(1);
366 t.request(Long.MAX_VALUE);
367 t.request(Long.MAX_VALUE);
368 t.cancel();
369 t.cancel();
370 t.request(2);
371 }
372 });
373
374 TestHelper.assertError(errors, 0, IllegalArgumentException.class);
375 } finally {
376 RxJavaPlugins.reset();
377 }
378 }
379
380 @Test
381 public void unbounded() {
382 MulticastProcessor<Integer> mp = MulticastProcessor.create(4, false);
383 mp.startUnbounded();
384
385 for (int i = 0; i < 10; i++) {
386 assertTrue(mp.offer(i));
387 }
388 mp.onComplete();
389
390 mp.test().assertResult(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
391 }
392
393 @Test
394 public void multiStart() {
395 List<Throwable> errors = TestHelper.trackPluginErrors();
396 try {
397 MulticastProcessor<Integer> mp = MulticastProcessor.create(4, false);
398
399 mp.start();
400 mp.start();
401 mp.startUnbounded();
402 BooleanSubscription bs = new BooleanSubscription();
403 mp.onSubscribe(bs);
404
405 assertTrue(bs.isCancelled());
406
407 TestHelper.assertError(errors, 0, ProtocolViolationException.class);
408 TestHelper.assertError(errors, 1, ProtocolViolationException.class);
409 TestHelper.assertError(errors, 2, ProtocolViolationException.class);
410 } finally {
411 RxJavaPlugins.reset();
412 }
413 }
414
415 @Test(expected = NullPointerException.class)
416 public void onNextNull() {
417 MulticastProcessor<Integer> mp = MulticastProcessor.create(4, false);
418 mp.start();
419 mp.onNext(null);
420 }
421
422 @Test(expected = NullPointerException.class)
423 public void onOfferNull() {
424 MulticastProcessor<Integer> mp = MulticastProcessor.create(4, false);
425 mp.start();
426 mp.offer(null);
427 }
428
429 @Test(expected = NullPointerException.class)
430 public void onErrorNull() {
431 MulticastProcessor<Integer> mp = MulticastProcessor.create(4, false);
432 mp.start();
433 mp.onError(null);
434 }
435
436 @Test
437 public void afterTerminated() {
438 List<Throwable> errors = TestHelper.trackPluginErrors();
439 try {
440 MulticastProcessor<Integer> mp = MulticastProcessor.create();
441 mp.start();
442 mp.onComplete();
443 mp.onComplete();
444 mp.onError(new IOException());
445 mp.onNext(1);
446 mp.offer(1);
447
448 mp.test().assertResult();
449
450 TestHelper.assertUndeliverable(errors, 0, IOException.class);
451 } finally {
452 RxJavaPlugins.reset();
453 }
454 }
455
456 @Test
457 public void asyncFused() {
458 UnicastProcessor<Integer> up = UnicastProcessor.create();
459 MulticastProcessor<Integer> mp = MulticastProcessor.create(4);
460
461 up.subscribe(mp);
462
463 TestSubscriber<Integer> ts = mp.test();
464
465 for (int i = 0; i < 10; i++) {
466 up.onNext(i);
467 }
468
469 try {
470 mp.offer(10);
471 fail("Should have thrown IllegalStateException");
472 } catch (IllegalStateException expected) {
473 // expected
474 }
475
476 up.onComplete();
477
478 ts.assertResult(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
479 }
480
481 @Test
482 public void fusionCrash() {
483 MulticastProcessor<Integer> mp = Flowable.range(1, 5)
484 .map(new Function<Integer, Integer>() {
485 @Override
486 public Integer apply(Integer v) throws Exception {
487 throw new IOException();
488 }
489 })
490 .subscribeWith(MulticastProcessor.<Integer>create());
491
492 mp.test().assertFailure(IOException.class);
493 }
494
495 @Test
496 public void lockstep() {
497 MulticastProcessor<Integer> mp = MulticastProcessor.create();
498
499 TestSubscriber<Integer> ts1 = mp.test();
500 mp.start();
501
502 mp.onNext(1);
503 mp.onNext(2);
504
505 ts1.assertValues(1, 2);
506
507 TestSubscriber<Integer> ts2 = mp.test(0);
508
509 ts2.assertEmpty();
510
511 mp.onNext(3);
512
513 ts1.assertValues(1, 2);
514 ts2.assertEmpty();
515
516 mp.onComplete();
517
518 ts1.assertValues(1, 2);
519 ts2.assertEmpty();
520
521 ts2.request(1);
522
523 ts1.assertResult(1, 2, 3);
524 ts2.assertResult(3);
525 }
526
527 @Test
528 public void rejectedFusion() {
529
530 MulticastProcessor<Integer> mp = MulticastProcessor.create();
531
532 TestHelper.<Integer>rejectFlowableFusion()
533 .subscribe(mp);
534
535 mp.test().assertEmpty();
536 }
537
538 @Test
539 public void addRemoveRaceNoRefCount() {
540 for (int i = 0; i < TestHelper.RACE_LONG_LOOPS; i++) {
541 final MulticastProcessor<Integer> mp = MulticastProcessor.create();
542
543 final TestSubscriber<Integer> ts = mp.test();
544 final TestSubscriber<Integer> ts2 = new TestSubscriber<>();
545
546 Runnable r1 = new Runnable() {
547 @Override
548 public void run() {
549 ts.cancel();
550 }
551 };
552
553 Runnable r2 = new Runnable() {
554 @Override
555 public void run() {
556 mp.subscribe(ts2);
557 }
558 };
559
560 TestHelper.race(r1, r2);
561
562 assertTrue(mp.hasSubscribers());
563 }
564 }
565
566 @Test
567 public void addRemoveRaceNoRefCountNonEmpty() {
568 for (int i = 0; i < TestHelper.RACE_LONG_LOOPS; i++) {
569 final MulticastProcessor<Integer> mp = MulticastProcessor.create();
570
571 mp.test();
572 final TestSubscriber<Integer> ts = mp.test();
573 final TestSubscriber<Integer> ts2 = new TestSubscriber<>();
574
575 Runnable r1 = new Runnable() {
576 @Override
577 public void run() {
578 ts.cancel();
579 }
580 };
581
582 Runnable r2 = new Runnable() {
583 @Override
584 public void run() {
585 mp.subscribe(ts2);
586 }
587 };
588
589 TestHelper.race(r1, r2);
590
591 assertTrue(mp.hasSubscribers());
592 }
593 }
594
595 @Test
596 public void addRemoveRaceWitRefCount() {
597 for (int i = 0; i < TestHelper.RACE_LONG_LOOPS; i++) {
598 final MulticastProcessor<Integer> mp = MulticastProcessor.create(true);
599
600 final TestSubscriber<Integer> ts = mp.test();
601 final TestSubscriber<Integer> ts2 = new TestSubscriber<>();
602
603 Runnable r1 = new Runnable() {
604 @Override
605 public void run() {
606 ts.cancel();
607 }
608 };
609
610 Runnable r2 = new Runnable() {
611 @Override
612 public void run() {
613 mp.subscribe(ts2);
614 }
615 };
616
617 TestHelper.race(r1, r2);
618 }
619 }
620
621 @Test
622 public void cancelUpfront() {
623 MulticastProcessor<Integer> mp = MulticastProcessor.create();
624
625 mp.test(0, true).assertEmpty();
626
627 assertFalse(mp.hasSubscribers());
628 }
629
630 @Test
631 public void cancelUpfrontOtherConsumersPresent() {
632 MulticastProcessor<Integer> mp = MulticastProcessor.create();
633
634 mp.test();
635
636 mp.test(0, true).assertEmpty();
637
638 assertTrue(mp.hasSubscribers());
639 }
640
641 @Test
642 public void consumerRequestRace() {
643 for (int i = 0; i < TestHelper.RACE_LONG_LOOPS; i++) {
644 final MulticastProcessor<Integer> mp = MulticastProcessor.create(true);
645 mp.startUnbounded();
646 mp.onNext(1);
647 mp.onNext(2);
648
649 final TestSubscriber<Integer> ts = mp.test();
650
651 Runnable r1 = new Runnable() {
652 @Override
653 public void run() {
654 ts.request(1);
655 }
656 };
657
658 Runnable r2 = new Runnable() {
659 @Override
660 public void run() {
661 ts.request(1);
662 }
663 };
664
665 TestHelper.race(r1, r2);
666
667 ts.assertValuesOnly(1, 2);
668 }
669 }
670
671 @Test
672 public void consumerUpstreamRace() {
673 for (int i = 0; i < TestHelper.RACE_LONG_LOOPS; i++) {
674 final MulticastProcessor<Integer> mp = MulticastProcessor.create(true);
675
676 final Flowable<Integer> source = Flowable.range(1, 5);
677
678 final TestSubscriber<Integer> ts = mp.test();
679
680 Runnable r1 = new Runnable() {
681 @Override
682 public void run() {
683 ts.request(5);
684 }
685 };
686
687 Runnable r2 = new Runnable() {
688 @Override
689 public void run() {
690 source.subscribe(mp);
691 }
692 };
693
694 TestHelper.race(r1, r2);
695
696 ts
697 .awaitDone(5, TimeUnit.SECONDS)
698 .assertResult(1, 2, 3, 4, 5);
699 }
700 }
701
702 @Test
703 public void emitCancelRace() {
704 for (int i = 0; i < TestHelper.RACE_LONG_LOOPS; i++) {
705 final MulticastProcessor<Integer> mp = MulticastProcessor.create(true);
706 mp.startUnbounded();
707
708 final TestSubscriber<Integer> ts = mp.test();
709
710 Runnable r1 = new Runnable() {
711 @Override
712 public void run() {
713 ts.cancel();
714 }
715 };
716
717 Runnable r2 = new Runnable() {
718 @Override
719 public void run() {
720 mp.onNext(1);
721 }
722 };
723
724 TestHelper.race(r1, r2);
725 }
726 }
727
728 @Test
729 public void cancelCancelDrain() {
730 for (int i = 0; i < TestHelper.RACE_LONG_LOOPS; i++) {
731 final MulticastProcessor<Integer> mp = MulticastProcessor.create(true);
732
733 final TestSubscriber<Integer> ts1 = mp.test();
734 final TestSubscriber<Integer> ts2 = mp.test();
735
736 mp.test();
737
738 Runnable r1 = new Runnable() {
739 @Override
740 public void run() {
741 ts1.cancel();
742 }
743 };
744
745 Runnable r2 = new Runnable() {
746 @Override
747 public void run() {
748 ts2.cancel();
749 }
750 };
751
752 TestHelper.race(r1, r2);
753 }
754 }
755
756 @Test
757 public void requestCancelRace() {
758 for (int i = 0; i < TestHelper.RACE_LONG_LOOPS; i++) {
759 final MulticastProcessor<Integer> mp = MulticastProcessor.create(true);
760
761 final TestSubscriber<Integer> ts1 = mp.test();
762
763 Runnable r1 = new Runnable() {
764 @Override
765 public void run() {
766 ts1.cancel();
767 }
768 };
769
770 Runnable r2 = new Runnable() {
771 @Override
772 public void run() {
773 ts1.request(1);
774 }
775 };
776
777 TestHelper.race(r1, r2);
778 }
779 }
780
781 @Test
782 public void noUpstream() {
783 MulticastProcessor<Integer> mp = MulticastProcessor.create();
784
785 TestSubscriber<Integer> ts = mp.test(0);
786
787 ts.request(1);
788
789 assertTrue(mp.hasSubscribers());
790 }
791
792 @Test
793 public void requestUpstreamPrefetchNonFused() {
794 for (int j = 1; j < 12; j++) {
795 MulticastProcessor<Integer> mp = MulticastProcessor.create(j, true);
796
797 TestSubscriber<Integer> ts = mp.test(0).withTag("Prefetch: " + j);
798
799 Flowable.range(1, 10).hide().subscribe(mp);
800
801 ts.assertEmpty()
802 .requestMore(3)
803 .assertValuesOnly(1, 2, 3)
804 .requestMore(3)
805 .assertValuesOnly(1, 2, 3, 4, 5, 6)
806 .requestMore(4)
807 .assertResult(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
808 }
809 }
810
811 @Test
812 public void requestUpstreamPrefetchNonFused2() {
813 for (int j = 1; j < 12; j++) {
814 MulticastProcessor<Integer> mp = MulticastProcessor.create(j, true);
815
816 TestSubscriber<Integer> ts = mp.test(0).withTag("Prefetch: " + j);
817
818 Flowable.range(1, 10).hide().subscribe(mp);
819
820 ts.assertEmpty()
821 .requestMore(2)
822 .assertValuesOnly(1, 2)
823 .requestMore(2)
824 .assertValuesOnly(1, 2, 3, 4)
825 .requestMore(6)
826 .assertResult(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
827 }
828 }
829 }