Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableTimeoutWithSelectorTest.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.util.*;
21 import java.util.concurrent.*;
22 import java.util.concurrent.atomic.*;
23
24 import org.junit.Test;
25 import org.mockito.InOrder;
26 import org.mockito.invocation.InvocationOnMock;
27 import org.mockito.stubbing.Answer;
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.operators.flowable.FlowableTimeout.TimeoutConsumer;
36 import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription;
37 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
38 import io.reactivex.rxjava3.processors.*;
39 import io.reactivex.rxjava3.schedulers.Schedulers;
40 import io.reactivex.rxjava3.subscribers.TestSubscriber;
41 import io.reactivex.rxjava3.testsupport.*;
42
43 public class FlowableTimeoutWithSelectorTest extends RxJavaTest {
44 @Test
45 public void timeoutSelectorNormal1() {
46 PublishProcessor<Integer> source = PublishProcessor.create();
47 final PublishProcessor<Integer> timeout = PublishProcessor.create();
48
49 Function<Integer, Flowable<Integer>> timeoutFunc = new Function<Integer, Flowable<Integer>>() {
50 @Override
51 public Flowable<Integer> apply(Integer t1) {
52 return timeout;
53 }
54 };
55
56 Flowable<Integer> other = Flowable.fromIterable(Arrays.asList(100));
57
58 Subscriber<Object> subscriber = TestHelper.mockSubscriber();
59 InOrder inOrder = inOrder(subscriber);
60
61 source.timeout(timeout, timeoutFunc, other).subscribe(subscriber);
62
63 source.onNext(1);
64 source.onNext(2);
65 source.onNext(3);
66 timeout.onNext(1);
67
68 inOrder.verify(subscriber).onNext(1);
69 inOrder.verify(subscriber).onNext(2);
70 inOrder.verify(subscriber).onNext(3);
71 inOrder.verify(subscriber).onNext(100);
72 inOrder.verify(subscriber).onComplete();
73 verify(subscriber, never()).onError(any(Throwable.class));
74
75 }
76
77 @Test
78 public void timeoutSelectorTimeoutFirst() throws InterruptedException {
79 Flowable<Integer> source = Flowable.<Integer>never();
80 final PublishProcessor<Integer> timeout = PublishProcessor.create();
81
82 Function<Integer, Flowable<Integer>> timeoutFunc = new Function<Integer, Flowable<Integer>>() {
83 @Override
84 public Flowable<Integer> apply(Integer t1) {
85 return timeout;
86 }
87 };
88
89 Flowable<Integer> other = Flowable.fromIterable(Arrays.asList(100));
90
91 Subscriber<Object> subscriber = TestHelper.mockSubscriber();
92 InOrder inOrder = inOrder(subscriber);
93
94 source.timeout(timeout, timeoutFunc, other).subscribe(subscriber);
95
96 timeout.onNext(1);
97
98 inOrder.verify(subscriber).onNext(100);
99 inOrder.verify(subscriber).onComplete();
100 verify(subscriber, never()).onError(any(Throwable.class));
101
102 }
103
104 @Test
105 public void timeoutSelectorFirstThrows() {
106 Flowable<Integer> source = Flowable.<Integer>never();
107 final PublishProcessor<Integer> timeout = PublishProcessor.create();
108
109 Function<Integer, Flowable<Integer>> timeoutFunc = new Function<Integer, Flowable<Integer>>() {
110 @Override
111 public Flowable<Integer> apply(Integer t1) {
112 return timeout;
113 }
114 };
115
116 Supplier<Flowable<Integer>> firstTimeoutFunc = new Supplier<Flowable<Integer>>() {
117 @Override
118 public Flowable<Integer> get() {
119 throw new TestException();
120 }
121 };
122
123 Flowable<Integer> other = Flowable.fromIterable(Arrays.asList(100));
124
125 Subscriber<Object> subscriber = TestHelper.mockSubscriber();
126
127 source.timeout(Flowable.defer(firstTimeoutFunc), timeoutFunc, other).subscribe(subscriber);
128
129 verify(subscriber).onError(any(TestException.class));
130 verify(subscriber, never()).onNext(any());
131 verify(subscriber, never()).onComplete();
132
133 }
134
135 @Test
136 public void timeoutSelectorSubsequentThrows() {
137 PublishProcessor<Integer> source = PublishProcessor.create();
138 final PublishProcessor<Integer> timeout = PublishProcessor.create();
139
140 Function<Integer, Flowable<Integer>> timeoutFunc = new Function<Integer, Flowable<Integer>>() {
141 @Override
142 public Flowable<Integer> apply(Integer t1) {
143 throw new TestException();
144 }
145 };
146
147 Flowable<Integer> other = Flowable.fromIterable(Arrays.asList(100));
148
149 Subscriber<Object> subscriber = TestHelper.mockSubscriber();
150 InOrder inOrder = inOrder(subscriber);
151
152 source.timeout(timeout, timeoutFunc, other).subscribe(subscriber);
153
154 source.onNext(1);
155
156 inOrder.verify(subscriber).onNext(1);
157 inOrder.verify(subscriber).onError(any(TestException.class));
158 verify(subscriber, never()).onComplete();
159
160 }
161
162 @Test
163 public void timeoutSelectorFirstFlowableThrows() {
164 PublishProcessor<Integer> source = PublishProcessor.create();
165 final PublishProcessor<Integer> timeout = PublishProcessor.create();
166
167 Function<Integer, Flowable<Integer>> timeoutFunc = new Function<Integer, Flowable<Integer>>() {
168 @Override
169 public Flowable<Integer> apply(Integer t1) {
170 return timeout;
171 }
172 };
173
174 Flowable<Integer> other = Flowable.fromIterable(Arrays.asList(100));
175
176 Subscriber<Object> subscriber = TestHelper.mockSubscriber();
177
178 source.timeout(Flowable.<Integer> error(new TestException()), timeoutFunc, other).subscribe(subscriber);
179
180 verify(subscriber).onError(any(TestException.class));
181 verify(subscriber, never()).onNext(any());
182 verify(subscriber, never()).onComplete();
183
184 }
185
186 @Test
187 public void timeoutSelectorSubsequentFlowableThrows() {
188 PublishProcessor<Integer> source = PublishProcessor.create();
189 final PublishProcessor<Integer> timeout = PublishProcessor.create();
190
191 Function<Integer, Flowable<Integer>> timeoutFunc = new Function<Integer, Flowable<Integer>>() {
192 @Override
193 public Flowable<Integer> apply(Integer t1) {
194 return Flowable.<Integer> error(new TestException());
195 }
196 };
197
198 Flowable<Integer> other = Flowable.fromIterable(Arrays.asList(100));
199
200 Subscriber<Object> subscriber = TestHelper.mockSubscriber();
201 InOrder inOrder = inOrder(subscriber);
202
203 source.timeout(timeout, timeoutFunc, other).subscribe(subscriber);
204
205 source.onNext(1);
206
207 inOrder.verify(subscriber).onNext(1);
208 inOrder.verify(subscriber).onError(any(TestException.class));
209 verify(subscriber, never()).onComplete();
210
211 }
212
213 @Test
214 public void timeoutSelectorWithFirstTimeoutFirstAndNoOtherFlowable() {
215 PublishProcessor<Integer> source = PublishProcessor.create();
216 final PublishProcessor<Integer> timeout = PublishProcessor.create();
217
218 Function<Integer, Flowable<Integer>> timeoutFunc = new Function<Integer, Flowable<Integer>>() {
219 @Override
220 public Flowable<Integer> apply(Integer t1) {
221 return PublishProcessor.create();
222 }
223 };
224
225 Subscriber<Object> subscriber = TestHelper.mockSubscriber();
226 source.timeout(timeout, timeoutFunc).subscribe(subscriber);
227
228 timeout.onNext(1);
229
230 InOrder inOrder = inOrder(subscriber);
231 inOrder.verify(subscriber).onError(isA(TimeoutException.class));
232 inOrder.verifyNoMoreInteractions();
233 }
234
235 @Test
236 public void timeoutSelectorWithTimeoutFirstAndNoOtherFlowable() {
237 PublishProcessor<Integer> source = PublishProcessor.create();
238 final PublishProcessor<Integer> timeout = PublishProcessor.create();
239
240 Function<Integer, Flowable<Integer>> timeoutFunc = new Function<Integer, Flowable<Integer>>() {
241 @Override
242 public Flowable<Integer> apply(Integer t1) {
243 return timeout;
244 }
245 };
246
247 Subscriber<Object> subscriber = TestHelper.mockSubscriber();
248 source.timeout(PublishProcessor.create(), timeoutFunc).subscribe(subscriber);
249 source.onNext(1);
250
251 timeout.onNext(1);
252
253 InOrder inOrder = inOrder(subscriber);
254 inOrder.verify(subscriber).onNext(1);
255 inOrder.verify(subscriber).onError(isA(TimeoutException.class));
256 inOrder.verifyNoMoreInteractions();
257 }
258
259 @Test
260 public void timeoutSelectorWithTimeoutAndOnNextRaceCondition() throws InterruptedException {
261 // Thread 1 Thread 2
262 //
263 // observer.onNext(1)
264 // start timeout
265 // unsubscribe timeout in thread 2 start to do some long-time work in "unsubscribe"
266 // observer.onNext(2)
267 // timeout.onNext(1)
268 // "unsubscribe" done
269 //
270 //
271 // In the above case, the timeout operator should ignore "timeout.onNext(1)"
272 // since "observer" has already seen 2.
273 final CountDownLatch observerReceivedTwo = new CountDownLatch(1);
274 final CountDownLatch timeoutEmittedOne = new CountDownLatch(1);
275 final CountDownLatch observerCompleted = new CountDownLatch(1);
276 final CountDownLatch enteredTimeoutOne = new CountDownLatch(1);
277 final AtomicBoolean latchTimeout = new AtomicBoolean(false);
278
279 final Function<Integer, Flowable<Integer>> timeoutFunc = new Function<Integer, Flowable<Integer>>() {
280 @Override
281 public Flowable<Integer> apply(Integer t1) {
282 if (t1 == 1) {
283 // Force "unsubscribe" run on another thread
284 return Flowable.unsafeCreate(new Publisher<Integer>() {
285 @Override
286 public void subscribe(Subscriber<? super Integer> subscriber) {
287 subscriber.onSubscribe(new BooleanSubscription());
288 enteredTimeoutOne.countDown();
289 // force the timeout message be sent after observer.onNext(2)
290 while (true) {
291 try {
292 if (!observerReceivedTwo.await(30, TimeUnit.SECONDS)) {
293 // CountDownLatch timeout
294 // There should be something wrong
295 latchTimeout.set(true);
296 }
297 break;
298 } catch (InterruptedException e) {
299 // Since we just want to emulate a busy method,
300 // we ignore the interrupt signal from Scheduler.
301 }
302 }
303 subscriber.onNext(1);
304 timeoutEmittedOne.countDown();
305 }
306 }).subscribeOn(Schedulers.newThread());
307 } else {
308 return PublishProcessor.create();
309 }
310 }
311 };
312
313 final Subscriber<Integer> subscriber = TestHelper.mockSubscriber();
314 doAnswer(new Answer<Void>() {
315
316 @Override
317 public Void answer(InvocationOnMock invocation) throws Throwable {
318 observerReceivedTwo.countDown();
319 return null;
320 }
321
322 }).when(subscriber).onNext(2);
323 doAnswer(new Answer<Void>() {
324
325 @Override
326 public Void answer(InvocationOnMock invocation) throws Throwable {
327 observerCompleted.countDown();
328 return null;
329 }
330
331 }).when(subscriber).onComplete();
332
333 final TestSubscriber<Integer> ts = new TestSubscriber<>(subscriber);
334
335 new Thread(new Runnable() {
336
337 @Override
338 public void run() {
339 PublishProcessor<Integer> source = PublishProcessor.create();
340 source.timeout(timeoutFunc, Flowable.just(3)).subscribe(ts);
341 source.onNext(1); // start timeout
342 try {
343 if (!enteredTimeoutOne.await(30, TimeUnit.SECONDS)) {
344 latchTimeout.set(true);
345 }
346 } catch (InterruptedException e) {
347 e.printStackTrace();
348 }
349 source.onNext(2); // disable timeout
350 try {
351 if (!timeoutEmittedOne.await(30, TimeUnit.SECONDS)) {
352 latchTimeout.set(true);
353 }
354 } catch (InterruptedException e) {
355 e.printStackTrace();
356 }
357 source.onComplete();
358 }
359
360 }).start();
361
362 if (!observerCompleted.await(30, TimeUnit.SECONDS)) {
363 latchTimeout.set(true);
364 }
365
366 assertFalse("CoundDownLatch timeout", latchTimeout.get());
367
368 InOrder inOrder = inOrder(subscriber);
369 inOrder.verify(subscriber).onSubscribe((Subscription)notNull());
370 inOrder.verify(subscriber).onNext(1);
371 inOrder.verify(subscriber).onNext(2);
372 inOrder.verify(subscriber, never()).onNext(3);
373 inOrder.verify(subscriber).onComplete();
374 inOrder.verifyNoMoreInteractions();
375 }
376
377 @Test
378 public void dispose() {
379 TestHelper.checkDisposed(PublishProcessor.create().timeout(Functions.justFunction(Flowable.never())));
380
381 TestHelper.checkDisposed(PublishProcessor.create().timeout(Functions.justFunction(Flowable.never()), Flowable.never()));
382 }
383
384 @Test
385 public void doubleOnSubscribe() {
386 TestHelper.checkDoubleOnSubscribeFlowable(new Function<Flowable<Object>, Flowable<Object>>() {
387 @Override
388 public Flowable<Object> apply(Flowable<Object> f) throws Exception {
389 return f.timeout(Functions.justFunction(Flowable.never()));
390 }
391 });
392
393 TestHelper.checkDoubleOnSubscribeFlowable(new Function<Flowable<Object>, Flowable<Object>>() {
394 @Override
395 public Flowable<Object> apply(Flowable<Object> f) throws Exception {
396 return f.timeout(Functions.justFunction(Flowable.never()), Flowable.never());
397 }
398 });
399 }
400
401 @Test
402 public void empty() {
403 Flowable.empty()
404 .timeout(Functions.justFunction(Flowable.never()))
405 .test()
406 .assertResult();
407 }
408
409 @Test
410 public void error() {
411 Flowable.error(new TestException())
412 .timeout(Functions.justFunction(Flowable.never()))
413 .test()
414 .assertFailure(TestException.class);
415 }
416
417 @Test
418 public void emptyInner() {
419 PublishProcessor<Integer> pp = PublishProcessor.create();
420
421 TestSubscriber<Integer> ts = pp
422 .timeout(Functions.justFunction(Flowable.empty()))
423 .test();
424
425 pp.onNext(1);
426
427 ts.assertFailure(TimeoutException.class, 1);
428 }
429
430 @Test
431 public void badInnerSource() {
432 List<Throwable> errors = TestHelper.trackPluginErrors();
433 try {
434 PublishProcessor<Integer> pp = PublishProcessor.create();
435
436 TestSubscriberEx<Integer> ts = pp
437 .timeout(Functions.justFunction(new Flowable<Integer>() {
438 @Override
439 protected void subscribeActual(Subscriber<? super Integer> subscriber) {
440 subscriber.onSubscribe(new BooleanSubscription());
441 subscriber.onError(new TestException("First"));
442 subscriber.onNext(2);
443 subscriber.onError(new TestException("Second"));
444 subscriber.onComplete();
445 }
446 }))
447 .to(TestHelper.<Integer>testConsumer());
448
449 pp.onNext(1);
450
451 ts.assertFailureAndMessage(TestException.class, "First", 1);
452
453 TestHelper.assertUndeliverable(errors, 0, TestException.class, "Second");
454 } finally {
455 RxJavaPlugins.reset();
456 }
457 }
458
459 @Test
460 public void badInnerSourceOther() {
461 List<Throwable> errors = TestHelper.trackPluginErrors();
462 try {
463 PublishProcessor<Integer> pp = PublishProcessor.create();
464
465 TestSubscriberEx<Integer> ts = pp
466 .timeout(Functions.justFunction(new Flowable<Integer>() {
467 @Override
468 protected void subscribeActual(Subscriber<? super Integer> subscriber) {
469 subscriber.onSubscribe(new BooleanSubscription());
470 subscriber.onError(new TestException("First"));
471 subscriber.onNext(2);
472 subscriber.onError(new TestException("Second"));
473 subscriber.onComplete();
474 }
475 }), Flowable.just(2))
476 .to(TestHelper.<Integer>testConsumer());
477
478 pp.onNext(1);
479
480 ts.assertFailureAndMessage(TestException.class, "First", 1);
481
482 TestHelper.assertUndeliverable(errors, 0, TestException.class, "Second");
483 } finally {
484 RxJavaPlugins.reset();
485 }
486 }
487
488 @Test
489 public void withOtherMainError() {
490 Flowable.error(new TestException())
491 .timeout(Functions.justFunction(Flowable.never()), Flowable.never())
492 .test()
493 .assertFailure(TestException.class);
494 }
495
496 @Test
497 public void badSourceTimeout() {
498 List<Throwable> errors = TestHelper.trackPluginErrors();
499 try {
500 new Flowable<Integer>() {
501 @Override
502 protected void subscribeActual(Subscriber<? super Integer> subscriber) {
503 subscriber.onSubscribe(new BooleanSubscription());
504 subscriber.onNext(1);
505 subscriber.onNext(2);
506 subscriber.onError(new TestException("First"));
507 subscriber.onNext(3);
508 subscriber.onComplete();
509 subscriber.onError(new TestException("Second"));
510 }
511 }
512 .timeout(Functions.justFunction(Flowable.never()), Flowable.<Integer>never())
513 .take(1)
514 .test()
515 .assertResult(1);
516
517 TestHelper.assertUndeliverable(errors, 0, TestException.class, "First");
518 TestHelper.assertUndeliverable(errors, 1, TestException.class, "Second");
519 } finally {
520 RxJavaPlugins.reset();
521 }
522 }
523
524 @Test
525 public void selectorTake() {
526 PublishProcessor<Integer> pp = PublishProcessor.create();
527
528 TestSubscriber<Integer> ts = pp
529 .timeout(Functions.justFunction(Flowable.never()))
530 .take(1)
531 .test();
532
533 assertTrue(pp.hasSubscribers());
534
535 pp.onNext(1);
536
537 assertFalse(pp.hasSubscribers());
538
539 ts.assertResult(1);
540 }
541
542 @Test
543 public void selectorFallbackTake() {
544 PublishProcessor<Integer> pp = PublishProcessor.create();
545
546 TestSubscriber<Integer> ts = pp
547 .timeout(Functions.justFunction(Flowable.never()), Flowable.just(2))
548 .take(1)
549 .test();
550
551 assertTrue(pp.hasSubscribers());
552
553 pp.onNext(1);
554
555 assertFalse(pp.hasSubscribers());
556
557 ts.assertResult(1);
558 }
559
560 @Test
561 public void lateOnTimeoutError() {
562 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
563 List<Throwable> errors = TestHelper.trackPluginErrors();
564 try {
565 final PublishProcessor<Integer> pp = PublishProcessor.create();
566
567 final Subscriber<?>[] sub = { null, null };
568
569 final Flowable<Integer> pp2 = new Flowable<Integer>() {
570
571 int count;
572
573 @Override
574 protected void subscribeActual(
575 Subscriber<? super Integer> s) {
576 s.onSubscribe(new BooleanSubscription());
577 sub[count++] = s;
578 }
579 };
580
581 TestSubscriber<Integer> ts = pp.timeout(Functions.justFunction(pp2)).test();
582
583 pp.onNext(0);
584
585 Runnable r1 = new Runnable() {
586 @Override
587 public void run() {
588 pp.onNext(1);
589 }
590 };
591
592 final Throwable ex = new TestException();
593
594 Runnable r2 = new Runnable() {
595 @Override
596 public void run() {
597 sub[0].onError(ex);
598 }
599 };
600
601 TestHelper.race(r1, r2);
602
603 ts.assertValueAt(0, 0);
604
605 if (!errors.isEmpty()) {
606 TestHelper.assertUndeliverable(errors, 0, TestException.class);
607 }
608 } finally {
609 RxJavaPlugins.reset();
610 }
611 }
612 }
613
614 @Test
615 public void lateOnTimeoutFallbackRace() {
616 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
617 List<Throwable> errors = TestHelper.trackPluginErrors();
618 try {
619 final PublishProcessor<Integer> pp = PublishProcessor.create();
620
621 final Subscriber<?>[] sub = { null, null };
622
623 final Flowable<Integer> pp2 = new Flowable<Integer>() {
624
625 int count;
626
627 @Override
628 protected void subscribeActual(
629 Subscriber<? super Integer> s) {
630 assertFalse(((Disposable)s).isDisposed());
631 s.onSubscribe(new BooleanSubscription());
632 sub[count++] = s;
633 }
634 };
635
636 TestSubscriber<Integer> ts = pp.timeout(Functions.justFunction(pp2), Flowable.<Integer>never()).test();
637
638 pp.onNext(0);
639
640 Runnable r1 = new Runnable() {
641 @Override
642 public void run() {
643 pp.onNext(1);
644 }
645 };
646
647 final Throwable ex = new TestException();
648
649 Runnable r2 = new Runnable() {
650 @Override
651 public void run() {
652 sub[0].onError(ex);
653 }
654 };
655
656 TestHelper.race(r1, r2);
657
658 ts.assertValueAt(0, 0);
659
660 if (!errors.isEmpty()) {
661 TestHelper.assertUndeliverable(errors, 0, TestException.class);
662 }
663 } finally {
664 RxJavaPlugins.reset();
665 }
666 }
667 }
668
669 @Test
670 public void onErrorOnTimeoutRace() {
671 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
672 List<Throwable> errors = TestHelper.trackPluginErrors();
673 try {
674 final PublishProcessor<Integer> pp = PublishProcessor.create();
675
676 final Subscriber<?>[] sub = { null, null };
677
678 final Flowable<Integer> pp2 = new Flowable<Integer>() {
679
680 int count;
681
682 @Override
683 protected void subscribeActual(
684 Subscriber<? super Integer> s) {
685 assertFalse(((Disposable)s).isDisposed());
686 s.onSubscribe(new BooleanSubscription());
687 sub[count++] = s;
688 }
689 };
690
691 TestSubscriber<Integer> ts = pp.timeout(Functions.justFunction(pp2)).test();
692
693 pp.onNext(0);
694
695 final Throwable ex = new TestException();
696
697 Runnable r1 = new Runnable() {
698 @Override
699 public void run() {
700 pp.onError(ex);
701 }
702 };
703
704 Runnable r2 = new Runnable() {
705 @Override
706 public void run() {
707 sub[0].onComplete();
708 }
709 };
710
711 TestHelper.race(r1, r2);
712
713 ts.assertValueAt(0, 0);
714
715 if (!errors.isEmpty()) {
716 TestHelper.assertUndeliverable(errors, 0, TestException.class);
717 }
718 } finally {
719 RxJavaPlugins.reset();
720 }
721 }
722 }
723
724 @Test
725 public void onCompleteOnTimeoutRace() {
726 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
727 List<Throwable> errors = TestHelper.trackPluginErrors();
728 try {
729 final PublishProcessor<Integer> pp = PublishProcessor.create();
730
731 final Subscriber<?>[] sub = { null, null };
732
733 final Flowable<Integer> pp2 = new Flowable<Integer>() {
734
735 int count;
736
737 @Override
738 protected void subscribeActual(
739 Subscriber<? super Integer> s) {
740 assertFalse(((Disposable)s).isDisposed());
741 s.onSubscribe(new BooleanSubscription());
742 sub[count++] = s;
743 }
744 };
745
746 TestSubscriber<Integer> ts = pp.timeout(Functions.justFunction(pp2)).test();
747
748 pp.onNext(0);
749
750 Runnable r1 = new Runnable() {
751 @Override
752 public void run() {
753 pp.onComplete();
754 }
755 };
756
757 Runnable r2 = new Runnable() {
758 @Override
759 public void run() {
760 sub[0].onComplete();
761 }
762 };
763
764 TestHelper.race(r1, r2);
765
766 ts.assertValueAt(0, 0);
767
768 if (!errors.isEmpty()) {
769 TestHelper.assertUndeliverable(errors, 0, TestException.class);
770 }
771 } finally {
772 RxJavaPlugins.reset();
773 }
774 }
775 }
776
777 @Test
778 public void onCompleteOnTimeoutRaceFallback() {
779 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
780 List<Throwable> errors = TestHelper.trackPluginErrors();
781 try {
782 final PublishProcessor<Integer> pp = PublishProcessor.create();
783
784 final Subscriber<?>[] sub = { null, null };
785
786 final Flowable<Integer> pp2 = new Flowable<Integer>() {
787
788 int count;
789
790 @Override
791 protected void subscribeActual(
792 Subscriber<? super Integer> s) {
793 assertFalse(((Disposable)s).isDisposed());
794 s.onSubscribe(new BooleanSubscription());
795 sub[count++] = s;
796 }
797 };
798
799 TestSubscriber<Integer> ts = pp.timeout(Functions.justFunction(pp2), Flowable.<Integer>never()).test();
800
801 pp.onNext(0);
802
803 Runnable r1 = new Runnable() {
804 @Override
805 public void run() {
806 pp.onComplete();
807 }
808 };
809
810 Runnable r2 = new Runnable() {
811 @Override
812 public void run() {
813 sub[0].onComplete();
814 }
815 };
816
817 TestHelper.race(r1, r2);
818
819 ts.assertValueAt(0, 0);
820
821 if (!errors.isEmpty()) {
822 TestHelper.assertUndeliverable(errors, 0, TestException.class);
823 }
824 } finally {
825 RxJavaPlugins.reset();
826 }
827 }
828 }
829
830 @Test
831 public void disposedUpfront() {
832 PublishProcessor<Integer> pp = PublishProcessor.create();
833 final AtomicInteger counter = new AtomicInteger();
834
835 Flowable<Object> timeoutAndFallback = Flowable.never().doOnSubscribe(new Consumer<Subscription>() {
836 @Override
837 public void accept(Subscription s) throws Exception {
838 counter.incrementAndGet();
839 }
840 });
841
842 pp
843 .timeout(timeoutAndFallback, Functions.justFunction(timeoutAndFallback))
844 .test(1, true)
845 .assertEmpty();
846
847 assertEquals(0, counter.get());
848 }
849
850 @Test
851 public void disposedUpfrontFallback() {
852 PublishProcessor<Object> pp = PublishProcessor.create();
853 final AtomicInteger counter = new AtomicInteger();
854
855 Flowable<Object> timeoutAndFallback = Flowable.never().doOnSubscribe(new Consumer<Subscription>() {
856 @Override
857 public void accept(Subscription s) throws Exception {
858 counter.incrementAndGet();
859 }
860 });
861
862 pp
863 .timeout(timeoutAndFallback, Functions.justFunction(timeoutAndFallback), timeoutAndFallback)
864 .test(1, true)
865 .assertEmpty();
866
867 assertEquals(0, counter.get());
868 }
869
870 @Test
871 public void timeoutConsumerDoubleOnSubscribe() {
872 List<Throwable> errors = TestHelper.trackPluginErrors();
873 try {
874 BehaviorProcessor.createDefault(1)
875 .timeout(Functions.justFunction(new Flowable<Integer>() {
876 @Override
877 protected void subscribeActual(Subscriber<? super Integer> s) {
878 BooleanSubscription bs1 = new BooleanSubscription();
879 s.onSubscribe(bs1);
880
881 BooleanSubscription bs2 = new BooleanSubscription();
882 s.onSubscribe(bs2);
883
884 assertFalse(bs1.isCancelled());
885 assertTrue(bs2.isCancelled());
886
887 s.onComplete();
888 }
889 }))
890 .test()
891 .assertFailure(TimeoutException.class, 1);
892
893 TestHelper.assertError(errors, 0, ProtocolViolationException.class);
894 } finally {
895 RxJavaPlugins.reset();
896 }
897 }
898
899 @Test
900 public void timeoutConsumerIsDisposed() {
901 TimeoutConsumer consumer = new TimeoutConsumer(0, null);
902
903 assertFalse(consumer.isDisposed());
904 consumer.dispose();
905 assertTrue(consumer.isDisposed());
906 }
907 }