Debian Codegraph @

src/test/java/io/reactivex/rxjava3/processors/PublishProcessorTest.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 import static org.mockito.ArgumentMatchers.any;
18 import static org.mockito.Mockito.*;
19
20 import java.util.ArrayList;
21 import java.util.concurrent.TimeUnit;
22 import java.util.concurrent.atomic.AtomicInteger;
23
24 import org.junit.Test;
25 import org.mockito.*;
26 import org.reactivestreams.*;
27
28 import io.reactivex.rxjava3.core.*;
29 import io.reactivex.rxjava3.exceptions.*;
30 import io.reactivex.rxjava3.functions.*;
31 import io.reactivex.rxjava3.schedulers.Schedulers;
32 import io.reactivex.rxjava3.subscribers.*;
33 import io.reactivex.rxjava3.testsupport.*;
34
35 public class PublishProcessorTest extends FlowableProcessorTest<Object> {
36
37 @Override
38 protected FlowableProcessor<Object> create() {
39 return PublishProcessor.create();
40 }
41
42 @Test
43 @SuppressUndeliverable
44 public void completed() {
45 PublishProcessor<String> processor = PublishProcessor.create();
46
47 Subscriber<String> subscriber = TestHelper.mockSubscriber();
48 processor.subscribe(subscriber);
49
50 processor.onNext("one");
51 processor.onNext("two");
52 processor.onNext("three");
53 processor.onComplete();
54
55 Subscriber<String> anotherSubscriber = TestHelper.mockSubscriber();
56 processor.subscribe(anotherSubscriber);
57
58 processor.onNext("four");
59 processor.onComplete();
60 processor.onError(new Throwable());
61
62 assertCompletedSubscriber(subscriber);
63 // todo bug? assertNeverSubscriber(anotherSubscriber);
64 }
65
66 @Test
67 public void completedStopsEmittingData() {
68 PublishProcessor<Object> channel = PublishProcessor.create();
69 Subscriber<Object> observerA = TestHelper.mockSubscriber();
70 Subscriber<Object> observerB = TestHelper.mockSubscriber();
71 Subscriber<Object> observerC = TestHelper.mockSubscriber();
72
73 TestSubscriber<Object> ts = new TestSubscriber<>(observerA);
74
75 channel.subscribe(ts);
76 channel.subscribe(observerB);
77
78 InOrder inOrderA = inOrder(observerA);
79 InOrder inOrderB = inOrder(observerB);
80 InOrder inOrderC = inOrder(observerC);
81
82 channel.onNext(42);
83
84 inOrderA.verify(observerA).onNext(42);
85 inOrderB.verify(observerB).onNext(42);
86
87 ts.cancel();
88 inOrderA.verifyNoMoreInteractions();
89
90 channel.onNext(4711);
91
92 inOrderB.verify(observerB).onNext(4711);
93
94 channel.onComplete();
95
96 inOrderB.verify(observerB).onComplete();
97
98 channel.subscribe(observerC);
99
100 inOrderC.verify(observerC).onComplete();
101
102 channel.onNext(13);
103
104 inOrderB.verifyNoMoreInteractions();
105 inOrderC.verifyNoMoreInteractions();
106 }
107
108 private void assertCompletedSubscriber(Subscriber<String> subscriber) {
109 verify(subscriber, times(1)).onNext("one");
110 verify(subscriber, times(1)).onNext("two");
111 verify(subscriber, times(1)).onNext("three");
112 verify(subscriber, Mockito.never()).onError(any(Throwable.class));
113 verify(subscriber, times(1)).onComplete();
114 }
115
116 @Test
117 @SuppressUndeliverable
118 public void error() {
119 PublishProcessor<String> processor = PublishProcessor.create();
120
121 Subscriber<String> subscriber = TestHelper.mockSubscriber();
122 processor.subscribe(subscriber);
123
124 processor.onNext("one");
125 processor.onNext("two");
126 processor.onNext("three");
127 processor.onError(testException);
128
129 Subscriber<String> anotherSubscriber = TestHelper.mockSubscriber();
130 processor.subscribe(anotherSubscriber);
131
132 processor.onNext("four");
133 processor.onError(new Throwable());
134 processor.onComplete();
135
136 assertErrorSubscriber(subscriber);
137 // todo bug? assertNeverSubscriber(anotherSubscriber);
138 }
139
140 private void assertErrorSubscriber(Subscriber<String> subscriber) {
141 verify(subscriber, times(1)).onNext("one");
142 verify(subscriber, times(1)).onNext("two");
143 verify(subscriber, times(1)).onNext("three");
144 verify(subscriber, times(1)).onError(testException);
145 verify(subscriber, Mockito.never()).onComplete();
146 }
147
148 @Test
149 public void subscribeMidSequence() {
150 PublishProcessor<String> processor = PublishProcessor.create();
151
152 Subscriber<String> subscriber = TestHelper.mockSubscriber();
153 processor.subscribe(subscriber);
154
155 processor.onNext("one");
156 processor.onNext("two");
157
158 assertObservedUntilTwo(subscriber);
159
160 Subscriber<String> anotherSubscriber = TestHelper.mockSubscriber();
161 processor.subscribe(anotherSubscriber);
162
163 processor.onNext("three");
164 processor.onComplete();
165
166 assertCompletedSubscriber(subscriber);
167 assertCompletedStartingWithThreeSubscriber(anotherSubscriber);
168 }
169
170 private void assertCompletedStartingWithThreeSubscriber(Subscriber<String> subscriber) {
171 verify(subscriber, Mockito.never()).onNext("one");
172 verify(subscriber, Mockito.never()).onNext("two");
173 verify(subscriber, times(1)).onNext("three");
174 verify(subscriber, Mockito.never()).onError(any(Throwable.class));
175 verify(subscriber, times(1)).onComplete();
176 }
177
178 @Test
179 public void unsubscribeFirstSubscriber() {
180 PublishProcessor<String> processor = PublishProcessor.create();
181
182 Subscriber<String> subscriber = TestHelper.mockSubscriber();
183 TestSubscriber<String> ts = new TestSubscriber<>(subscriber);
184 processor.subscribe(ts);
185
186 processor.onNext("one");
187 processor.onNext("two");
188
189 ts.cancel();
190 assertObservedUntilTwo(subscriber);
191
192 Subscriber<String> anotherSubscriber = TestHelper.mockSubscriber();
193 processor.subscribe(anotherSubscriber);
194
195 processor.onNext("three");
196 processor.onComplete();
197
198 assertObservedUntilTwo(subscriber);
199 assertCompletedStartingWithThreeSubscriber(anotherSubscriber);
200 }
201
202 private void assertObservedUntilTwo(Subscriber<String> subscriber) {
203 verify(subscriber, times(1)).onNext("one");
204 verify(subscriber, times(1)).onNext("two");
205 verify(subscriber, Mockito.never()).onNext("three");
206 verify(subscriber, Mockito.never()).onError(any(Throwable.class));
207 verify(subscriber, Mockito.never()).onComplete();
208 }
209
210 @Test
211 public void nestedSubscribe() {
212 final PublishProcessor<Integer> s = PublishProcessor.create();
213
214 final AtomicInteger countParent = new AtomicInteger();
215 final AtomicInteger countChildren = new AtomicInteger();
216 final AtomicInteger countTotal = new AtomicInteger();
217
218 final ArrayList<String> list = new ArrayList<>();
219
220 s.flatMap(new Function<Integer, Flowable<String>>() {
221
222 @Override
223 public Flowable<String> apply(final Integer v) {
224 countParent.incrementAndGet();
225
226 // then subscribe to processor again (it will not receive the previous value)
227 return s.map(new Function<Integer, String>() {
228
229 @Override
230 public String apply(Integer v2) {
231 countChildren.incrementAndGet();
232 return "Parent: " + v + " Child: " + v2;
233 }
234
235 });
236 }
237
238 }).subscribe(new Consumer<String>() {
239
240 @Override
241 public void accept(String v) {
242 countTotal.incrementAndGet();
243 list.add(v);
244 }
245
246 });
247
248 for (int i = 0; i < 10; i++) {
249 s.onNext(i);
250 }
251 s.onComplete();
252
253 // System.out.println("countParent: " + countParent.get());
254 // System.out.println("countChildren: " + countChildren.get());
255 // System.out.println("countTotal: " + countTotal.get());
256
257 // 9+8+7+6+5+4+3+2+1+0 == 45
258 assertEquals(45, list.size());
259 }
260
261 /**
262 * Should be able to unsubscribe all Subscribers, have it stop emitting, then subscribe new ones and it start emitting again.
263 */
264 @Test
265 public void reSubscribe() {
266 final PublishProcessor<Integer> pp = PublishProcessor.create();
267
268 Subscriber<Integer> subscriber1 = TestHelper.mockSubscriber();
269 TestSubscriber<Integer> ts = new TestSubscriber<>(subscriber1);
270 pp.subscribe(ts);
271
272 // emit
273 pp.onNext(1);
274
275 // validate we got it
276 InOrder inOrder1 = inOrder(subscriber1);
277 inOrder1.verify(subscriber1, times(1)).onNext(1);
278 inOrder1.verifyNoMoreInteractions();
279
280 // unsubscribe
281 ts.cancel();
282
283 // emit again but nothing will be there to receive it
284 pp.onNext(2);
285
286 Subscriber<Integer> subscriber2 = TestHelper.mockSubscriber();
287 TestSubscriber<Integer> ts2 = new TestSubscriber<>(subscriber2);
288 pp.subscribe(ts2);
289
290 // emit
291 pp.onNext(3);
292
293 // validate we got it
294 InOrder inOrder2 = inOrder(subscriber2);
295 inOrder2.verify(subscriber2, times(1)).onNext(3);
296 inOrder2.verifyNoMoreInteractions();
297
298 ts2.cancel();
299 }
300
301 private final Throwable testException = new Throwable();
302
303 @Test
304 public void unsubscriptionCase() {
305 PublishProcessor<String> src = PublishProcessor.create();
306
307 for (int i = 0; i < 10; i++) {
308 final Subscriber<Object> subscriber = TestHelper.mockSubscriber();
309 InOrder inOrder = inOrder(subscriber);
310 String v = "" + i;
311 System.out.printf("Turn: %d%n", i);
312 src.firstElement().toFlowable()
313 .flatMap(new Function<String, Flowable<String>>() {
314
315 @Override
316 public Flowable<String> apply(String t1) {
317 return Flowable.just(t1 + ", " + t1);
318 }
319 })
320 .subscribe(new DefaultSubscriber<String>() {
321 @Override
322 public void onNext(String t) {
323 subscriber.onNext(t);
324 }
325
326 @Override
327 public void onError(Throwable e) {
328 subscriber.onError(e);
329 }
330
331 @Override
332 public void onComplete() {
333 subscriber.onComplete();
334 }
335 });
336 src.onNext(v);
337
338 inOrder.verify(subscriber).onNext(v + ", " + v);
339 inOrder.verify(subscriber).onComplete();
340 verify(subscriber, never()).onError(any(Throwable.class));
341 }
342 }
343
344 @Test
345 public void currentStateMethodsNormal() {
346 PublishProcessor<Object> as = PublishProcessor.create();
347
348 assertFalse(as.hasThrowable());
349 assertFalse(as.hasComplete());
350 assertNull(as.getThrowable());
351
352 as.onNext(1);
353
354 assertFalse(as.hasThrowable());
355 assertFalse(as.hasComplete());
356 assertNull(as.getThrowable());
357
358 as.onComplete();
359
360 assertFalse(as.hasThrowable());
361 assertTrue(as.hasComplete());
362 assertNull(as.getThrowable());
363 }
364
365 @Test
366 public void currentStateMethodsEmpty() {
367 PublishProcessor<Object> as = PublishProcessor.create();
368
369 assertFalse(as.hasThrowable());
370 assertFalse(as.hasComplete());
371 assertNull(as.getThrowable());
372
373 as.onComplete();
374
375 assertFalse(as.hasThrowable());
376 assertTrue(as.hasComplete());
377 assertNull(as.getThrowable());
378 }
379
380 @Test
381 public void currentStateMethodsError() {
382 PublishProcessor<Object> as = PublishProcessor.create();
383
384 assertFalse(as.hasThrowable());
385 assertFalse(as.hasComplete());
386 assertNull(as.getThrowable());
387
388 as.onError(new TestException());
389
390 assertTrue(as.hasThrowable());
391 assertFalse(as.hasComplete());
392 assertTrue(as.getThrowable() instanceof TestException);
393 }
394
395 @Test
396 public void subscribeTo() {
397 PublishProcessor<Integer> pp = PublishProcessor.create();
398
399 Flowable.range(1, 10).subscribe(pp);
400
401 assertTrue(pp.hasComplete());
402
403 PublishProcessor<Integer> pp2 = PublishProcessor.create();
404
405 pp2.subscribe(pp);
406
407 assertFalse(pp2.hasSubscribers());
408 }
409
410 @Test
411 public void requestValidation() {
412 TestHelper.assertBadRequestReported(PublishProcessor.create());
413 }
414
415 @Test
416 public void crossCancel() {
417 final TestSubscriber<Integer> ts1 = new TestSubscriber<>();
418 TestSubscriber<Integer> ts2 = new TestSubscriber<Integer>() {
419 @Override
420 public void onNext(Integer t) {
421 super.onNext(t);
422 ts1.cancel();
423 }
424 };
425
426 PublishProcessor<Integer> pp = PublishProcessor.create();
427
428 pp.subscribe(ts2);
429 pp.subscribe(ts1);
430
431 pp.onNext(1);
432
433 ts2.assertValue(1);
434
435 ts1.assertNoValues();
436 }
437
438 @Test
439 @SuppressUndeliverable
440 public void crossCancelOnError() {
441 final TestSubscriber<Integer> ts1 = new TestSubscriber<>();
442 TestSubscriber<Integer> ts2 = new TestSubscriber<Integer>() {
443 @Override
444 public void onError(Throwable t) {
445 super.onError(t);
446 ts1.cancel();
447 }
448 };
449
450 PublishProcessor<Integer> pp = PublishProcessor.create();
451
452 pp.subscribe(ts2);
453 pp.subscribe(ts1);
454
455 pp.onError(new TestException());
456
457 ts2.assertError(TestException.class);
458
459 ts1.assertNoErrors();
460 }
461
462 @Test
463 public void crossCancelOnComplete() {
464 final TestSubscriber<Integer> ts1 = new TestSubscriber<>();
465 TestSubscriber<Integer> ts2 = new TestSubscriber<Integer>() {
466 @Override
467 public void onComplete() {
468 super.onComplete();
469 ts1.cancel();
470 }
471 };
472
473 PublishProcessor<Integer> pp = PublishProcessor.create();
474
475 pp.subscribe(ts2);
476 pp.subscribe(ts1);
477
478 pp.onComplete();
479
480 ts2.assertComplete();
481
482 ts1.assertNotComplete();
483 }
484
485 @Test
486 public void backpressureOverflow() {
487
488 PublishProcessor<Integer> pp = PublishProcessor.create();
489
490 TestSubscriber<Integer> ts = pp.test(0L);
491
492 pp.onNext(1);
493
494 ts.assertNoValues()
495 .assertNotComplete()
496 .assertError(MissingBackpressureException.class)
497 ;
498 }
499
500 @Test
501 public void onSubscribeCancelsImmediately() {
502 PublishProcessor<Integer> pp = PublishProcessor.create();
503
504 TestSubscriber<Integer> ts = pp.test();
505
506 pp.subscribe(new FlowableSubscriber<Integer>() {
507
508 @Override
509 public void onSubscribe(Subscription s) {
510 s.cancel();
511 }
512
513 @Override
514 public void onNext(Integer t) {
515
516 }
517
518 @Override
519 public void onError(Throwable t) {
520
521 }
522
523 @Override
524 public void onComplete() {
525
526 }
527
528 });
529
530 ts.cancel();
531
532 assertFalse(pp.hasSubscribers());
533 }
534
535 @Test
536 public void terminateRace() throws Exception {
537
538 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
539 final PublishProcessor<Integer> pp = PublishProcessor.create();
540
541 TestSubscriber<Integer> ts = pp.test();
542
543 Runnable task = new Runnable() {
544 @Override
545 public void run() {
546 pp.onComplete();
547 }
548 };
549
550 TestHelper.race(task, task);
551
552 ts
553 .awaitDone(5, TimeUnit.SECONDS)
554 .assertResult();
555 }
556 }
557
558 @Test
559 public void addRemoveRance() throws Exception {
560
561 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
562 final PublishProcessor<Integer> pp = PublishProcessor.create();
563
564 final TestSubscriber<Integer> ts = pp.test();
565
566 Runnable r1 = new Runnable() {
567 @Override
568 public void run() {
569 pp.subscribe();
570 }
571 };
572 Runnable r2 = new Runnable() {
573 @Override
574 public void run() {
575 ts.cancel();
576 }
577 };
578
579 TestHelper.race(r1, r2);
580 }
581 }
582
583 @Test
584 public void offer() {
585 PublishProcessor<Integer> pp = PublishProcessor.create();
586
587 TestSubscriber<Integer> ts = pp.test(0);
588
589 assertFalse(pp.offer(1));
590
591 ts.request(1);
592
593 assertTrue(pp.offer(1));
594
595 assertFalse(pp.offer(2));
596
597 ts.cancel();
598
599 assertTrue(pp.offer(2));
600
601 ts = pp.test(0);
602
603 try {
604 pp.offer(null);
605 fail("Should have thrown NPE!");
606 } catch (NullPointerException expected) {
607 // expected
608 }
609
610 ts.assertEmpty();
611 }
612
613 @Test
614 public void offerAsync() throws Exception {
615 final PublishProcessor<Integer> pp = PublishProcessor.create();
616
617 Schedulers.single().scheduleDirect(new Runnable() {
618 @Override
619 public void run() {
620 while (!pp.hasSubscribers()) {
621 try {
622 Thread.sleep(1);
623 } catch (InterruptedException ex) {
624 return;
625 }
626 }
627
628 for (int i = 1; i <= 10; i++) {
629 while (!pp.offer(i)) { }
630 }
631 pp.onComplete();
632 }
633 });
634
635 Thread.sleep(1);
636
637 pp.test()
638 .awaitDone(5, TimeUnit.SECONDS)
639 .assertResult(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
640 }
641
642 @Test
643 public void subscriberCancelOfferRace() {
644 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
645 final PublishProcessor<Integer> pp = PublishProcessor.create();
646
647 final TestSubscriber<Integer> ts = pp.test(1);
648
649 Runnable r1 = new Runnable() {
650 @Override
651 public void run() {
652 for (int i = 0; i < 2; i++) {
653 while (!pp.offer(i)) { }
654 }
655 }
656 };
657
658 Runnable r2 = new Runnable() {
659 @Override
660 public void run() {
661 ts.cancel();
662 }
663 };
664
665 TestHelper.race(r1, r2);
666
667 if (ts.values().size() > 0) {
668 ts.assertValuesOnly(0);
669 } else {
670 ts.assertEmpty();
671 }
672 }
673 }
674 }