Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableTimeoutTests.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 io.reactivex.rxjava3.internal.util.ExceptionHelper.timeoutMessage;
17 import static org.junit.Assert.*;
18 import static org.mockito.ArgumentMatchers.any;
19 import static org.mockito.Mockito.*;
20
21 import java.util.List;
22 import java.util.concurrent.*;
23
24 import org.junit.*;
25 import org.mockito.InOrder;
26 import org.reactivestreams.*;
27
28 import io.reactivex.rxjava3.core.*;
29 import io.reactivex.rxjava3.exceptions.TestException;
30 import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription;
31 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
32 import io.reactivex.rxjava3.processors.PublishProcessor;
33 import io.reactivex.rxjava3.schedulers.*;
34 import io.reactivex.rxjava3.subscribers.TestSubscriber;
35 import io.reactivex.rxjava3.testsupport.*;
36
37 public class FlowableTimeoutTests extends RxJavaTest {
38 private PublishProcessor<String> underlyingSubject;
39 private TestScheduler testScheduler;
40 private Flowable<String> withTimeout;
41 private static final long TIMEOUT = 3;
42 private static final TimeUnit TIME_UNIT = TimeUnit.SECONDS;
43
44 @Before
45 public void setUp() {
46
47 underlyingSubject = PublishProcessor.create();
48 testScheduler = new TestScheduler();
49 withTimeout = underlyingSubject.timeout(TIMEOUT, TIME_UNIT, testScheduler);
50 }
51
52 @Test
53 public void shouldNotTimeoutIfOnNextWithinTimeout() {
54 Subscriber<String> subscriber = TestHelper.mockSubscriber();
55 TestSubscriber<String> ts = new TestSubscriber<>(subscriber);
56
57 withTimeout.subscribe(ts);
58
59 testScheduler.advanceTimeBy(2, TimeUnit.SECONDS);
60 underlyingSubject.onNext("One");
61 verify(subscriber).onNext("One");
62 testScheduler.advanceTimeBy(2, TimeUnit.SECONDS);
63 verify(subscriber, never()).onError(any(Throwable.class));
64 ts.cancel();
65 }
66
67 @Test
68 public void shouldNotTimeoutIfSecondOnNextWithinTimeout() {
69 Subscriber<String> subscriber = TestHelper.mockSubscriber();
70 TestSubscriber<String> ts = new TestSubscriber<>(subscriber);
71
72 withTimeout.subscribe(ts);
73
74 testScheduler.advanceTimeBy(2, TimeUnit.SECONDS);
75 underlyingSubject.onNext("One");
76 testScheduler.advanceTimeBy(2, TimeUnit.SECONDS);
77 underlyingSubject.onNext("Two");
78 verify(subscriber).onNext("Two");
79 testScheduler.advanceTimeBy(2, TimeUnit.SECONDS);
80 verify(subscriber, never()).onError(any(Throwable.class));
81 ts.cancel();
82 }
83
84 @Test
85 public void shouldTimeoutIfOnNextNotWithinTimeout() {
86 TestSubscriberEx<String> subscriber = new TestSubscriberEx<>();
87
88 withTimeout.subscribe(subscriber);
89
90 testScheduler.advanceTimeBy(TIMEOUT + 1, TimeUnit.SECONDS);
91 subscriber.assertFailureAndMessage(TimeoutException.class, timeoutMessage(TIMEOUT, TIME_UNIT));
92 }
93
94 @Test
95 public void shouldTimeoutIfSecondOnNextNotWithinTimeout() {
96 TestSubscriberEx<String> subscriber = new TestSubscriberEx<>();
97 TestSubscriber<String> ts = new TestSubscriber<>(subscriber);
98 withTimeout.subscribe(subscriber);
99 testScheduler.advanceTimeBy(2, TimeUnit.SECONDS);
100 underlyingSubject.onNext("One");
101 subscriber.assertValue("One");
102 testScheduler.advanceTimeBy(TIMEOUT + 1, TimeUnit.SECONDS);
103 subscriber.assertFailureAndMessage(TimeoutException.class, timeoutMessage(TIMEOUT, TIME_UNIT), "One");
104 ts.cancel();
105 }
106
107 @Test
108 public void shouldCompleteIfUnderlyingComletes() {
109 Subscriber<String> subscriber = TestHelper.mockSubscriber();
110 TestSubscriber<String> ts = new TestSubscriber<>(subscriber);
111 withTimeout.subscribe(subscriber);
112 testScheduler.advanceTimeBy(2, TimeUnit.SECONDS);
113 underlyingSubject.onComplete();
114 testScheduler.advanceTimeBy(2, TimeUnit.SECONDS);
115 verify(subscriber).onComplete();
116 verify(subscriber, never()).onError(any(Throwable.class));
117 ts.cancel();
118 }
119
120 @Test
121 public void shouldErrorIfUnderlyingErrors() {
122 Subscriber<String> subscriber = TestHelper.mockSubscriber();
123 TestSubscriber<String> ts = new TestSubscriber<>(subscriber);
124 withTimeout.subscribe(subscriber);
125 testScheduler.advanceTimeBy(2, TimeUnit.SECONDS);
126 underlyingSubject.onError(new UnsupportedOperationException());
127 testScheduler.advanceTimeBy(2, TimeUnit.SECONDS);
128 verify(subscriber).onError(any(UnsupportedOperationException.class));
129 ts.cancel();
130 }
131
132 @Test
133 public void shouldSwitchToOtherIfOnNextNotWithinTimeout() {
134 Flowable<String> other = Flowable.just("a", "b", "c");
135 Flowable<String> source = underlyingSubject.timeout(TIMEOUT, TIME_UNIT, testScheduler, other);
136
137 Subscriber<String> subscriber = TestHelper.mockSubscriber();
138 TestSubscriber<String> ts = new TestSubscriber<>(subscriber);
139 source.subscribe(ts);
140
141 testScheduler.advanceTimeBy(2, TimeUnit.SECONDS);
142 underlyingSubject.onNext("One");
143 testScheduler.advanceTimeBy(4, TimeUnit.SECONDS);
144 underlyingSubject.onNext("Two");
145 InOrder inOrder = inOrder(subscriber);
146 inOrder.verify(subscriber, times(1)).onNext("One");
147 inOrder.verify(subscriber, times(1)).onNext("a");
148 inOrder.verify(subscriber, times(1)).onNext("b");
149 inOrder.verify(subscriber, times(1)).onNext("c");
150 inOrder.verify(subscriber, times(1)).onComplete();
151 inOrder.verifyNoMoreInteractions();
152 ts.cancel();
153 }
154
155 @Test
156 public void shouldSwitchToOtherIfOnErrorNotWithinTimeout() {
157 Flowable<String> other = Flowable.just("a", "b", "c");
158 Flowable<String> source = underlyingSubject.timeout(TIMEOUT, TIME_UNIT, testScheduler, other);
159
160 Subscriber<String> subscriber = TestHelper.mockSubscriber();
161 TestSubscriber<String> ts = new TestSubscriber<>(subscriber);
162 source.subscribe(ts);
163
164 testScheduler.advanceTimeBy(2, TimeUnit.SECONDS);
165 underlyingSubject.onNext("One");
166 testScheduler.advanceTimeBy(4, TimeUnit.SECONDS);
167 underlyingSubject.onError(new UnsupportedOperationException());
168 InOrder inOrder = inOrder(subscriber);
169 inOrder.verify(subscriber, times(1)).onNext("One");
170 inOrder.verify(subscriber, times(1)).onNext("a");
171 inOrder.verify(subscriber, times(1)).onNext("b");
172 inOrder.verify(subscriber, times(1)).onNext("c");
173 inOrder.verify(subscriber, times(1)).onComplete();
174 inOrder.verifyNoMoreInteractions();
175 ts.cancel();
176 }
177
178 @Test
179 public void shouldSwitchToOtherIfOnCompletedNotWithinTimeout() {
180 Flowable<String> other = Flowable.just("a", "b", "c");
181 Flowable<String> source = underlyingSubject.timeout(TIMEOUT, TIME_UNIT, testScheduler, other);
182
183 Subscriber<String> subscriber = TestHelper.mockSubscriber();
184 TestSubscriber<String> ts = new TestSubscriber<>(subscriber);
185 source.subscribe(ts);
186
187 testScheduler.advanceTimeBy(2, TimeUnit.SECONDS);
188 underlyingSubject.onNext("One");
189 testScheduler.advanceTimeBy(4, TimeUnit.SECONDS);
190 underlyingSubject.onComplete();
191 InOrder inOrder = inOrder(subscriber);
192 inOrder.verify(subscriber, times(1)).onNext("One");
193 inOrder.verify(subscriber, times(1)).onNext("a");
194 inOrder.verify(subscriber, times(1)).onNext("b");
195 inOrder.verify(subscriber, times(1)).onNext("c");
196 inOrder.verify(subscriber, times(1)).onComplete();
197 inOrder.verifyNoMoreInteractions();
198 ts.cancel();
199 }
200
201 @Test
202 public void shouldSwitchToOtherAndCanBeUnsubscribedIfOnNextNotWithinTimeout() {
203 PublishProcessor<String> other = PublishProcessor.create();
204 Flowable<String> source = underlyingSubject.timeout(TIMEOUT, TIME_UNIT, testScheduler, other);
205
206 Subscriber<String> subscriber = TestHelper.mockSubscriber();
207 TestSubscriber<String> ts = new TestSubscriber<>(subscriber);
208 source.subscribe(ts);
209
210 testScheduler.advanceTimeBy(2, TimeUnit.SECONDS);
211 underlyingSubject.onNext("One");
212 testScheduler.advanceTimeBy(4, TimeUnit.SECONDS);
213 underlyingSubject.onNext("Two");
214
215 other.onNext("a");
216 other.onNext("b");
217 ts.cancel();
218
219 // The following messages should not be delivered.
220 other.onNext("c");
221 other.onNext("d");
222 other.onComplete();
223
224 InOrder inOrder = inOrder(subscriber);
225 inOrder.verify(subscriber, times(1)).onNext("One");
226 inOrder.verify(subscriber, times(1)).onNext("a");
227 inOrder.verify(subscriber, times(1)).onNext("b");
228 inOrder.verifyNoMoreInteractions();
229 }
230
231 @Test
232 public void shouldTimeoutIfSynchronizedFlowableEmitFirstOnNextNotWithinTimeout()
233 throws InterruptedException {
234 final CountDownLatch exit = new CountDownLatch(1);
235 final CountDownLatch timeoutSetuped = new CountDownLatch(1);
236
237 final TestSubscriberEx<String> subscriber = new TestSubscriberEx<>();
238
239 new Thread(new Runnable() {
240
241 @Override
242 public void run() {
243 Flowable.unsafeCreate(new Publisher<String>() {
244
245 @Override
246 public void subscribe(Subscriber<? super String> subscriber) {
247 subscriber.onSubscribe(new BooleanSubscription());
248 try {
249 timeoutSetuped.countDown();
250 exit.await();
251 } catch (InterruptedException e) {
252 e.printStackTrace();
253 }
254 subscriber.onNext("a");
255 subscriber.onComplete();
256 }
257
258 }).timeout(1, TimeUnit.SECONDS, testScheduler)
259 .subscribe(subscriber);
260 }
261 }).start();
262
263 timeoutSetuped.await();
264 testScheduler.advanceTimeBy(2, TimeUnit.SECONDS);
265
266 subscriber.assertFailureAndMessage(TimeoutException.class, timeoutMessage(1, TimeUnit.SECONDS));
267
268 exit.countDown(); // exit the thread
269 }
270
271 @Test
272 public void shouldUnsubscribeFromUnderlyingSubscriptionOnTimeout() throws InterruptedException {
273 // From https://github.com/ReactiveX/RxJava/pull/951
274 final Subscription s = mock(Subscription.class);
275
276 Flowable<String> never = Flowable.unsafeCreate(new Publisher<String>() {
277 @Override
278 public void subscribe(Subscriber<? super String> subscriber) {
279 subscriber.onSubscribe(s);
280 }
281 });
282
283 TestScheduler testScheduler = new TestScheduler();
284 Flowable<String> observableWithTimeout = never.timeout(1000, TimeUnit.MILLISECONDS, testScheduler);
285
286 TestSubscriberEx<String> subscriber = new TestSubscriberEx<>();
287 observableWithTimeout.subscribe(subscriber);
288
289 testScheduler.advanceTimeBy(2000, TimeUnit.MILLISECONDS);
290
291 subscriber.assertFailureAndMessage(TimeoutException.class, timeoutMessage(1000, TimeUnit.MILLISECONDS));
292
293 verify(s, times(1)).cancel();
294 }
295
296 @Test
297 public void shouldUnsubscribeFromUnderlyingSubscriptionOnDispose() {
298 final PublishProcessor<String> processor = PublishProcessor.create();
299 final TestScheduler scheduler = new TestScheduler();
300
301 final TestSubscriber<String> subscriber = processor
302 .timeout(100, TimeUnit.MILLISECONDS, scheduler)
303 .test();
304
305 assertTrue(processor.hasSubscribers());
306
307 subscriber.cancel();
308
309 assertFalse(processor.hasSubscribers());
310 }
311
312 @Test
313 public void timedAndOther() {
314 Flowable.never().timeout(100, TimeUnit.MILLISECONDS, Flowable.just(1))
315 .test()
316 .awaitDone(5, TimeUnit.SECONDS)
317 .assertResult(1);
318 }
319
320 @Test
321 public void disposed() {
322 TestHelper.checkDisposed(PublishProcessor.create().timeout(1, TimeUnit.DAYS));
323
324 TestHelper.checkDisposed(PublishProcessor.create().timeout(1, TimeUnit.DAYS, Flowable.just(1)));
325 }
326
327 @Test
328 public void timedErrorOther() {
329 Flowable.error(new TestException())
330 .timeout(1, TimeUnit.DAYS, Flowable.just(1))
331 .test()
332 .assertFailure(TestException.class);
333 }
334
335 @Test
336 public void timedError() {
337 Flowable.error(new TestException())
338 .timeout(1, TimeUnit.DAYS)
339 .test()
340 .assertFailure(TestException.class);
341 }
342
343 @Test
344 public void timedEmptyOther() {
345 Flowable.empty()
346 .timeout(1, TimeUnit.DAYS, Flowable.just(1))
347 .test()
348 .assertResult();
349 }
350
351 @Test
352 public void timedEmpty() {
353 Flowable.empty()
354 .timeout(1, TimeUnit.DAYS)
355 .test()
356 .assertResult();
357 }
358
359 @Test
360 public void badSource() {
361 List<Throwable> errors = TestHelper.trackPluginErrors();
362 try {
363 new Flowable<Integer>() {
364 @Override
365 protected void subscribeActual(Subscriber<? super Integer> subscriber) {
366 subscriber.onSubscribe(new BooleanSubscription());
367
368 subscriber.onNext(1);
369 subscriber.onComplete();
370 subscriber.onNext(2);
371 subscriber.onError(new TestException());
372 subscriber.onComplete();
373 }
374 }
375 .timeout(1, TimeUnit.DAYS)
376 .test()
377 .assertResult(1);
378
379 TestHelper.assertUndeliverable(errors, 0, TestException.class);
380 } finally {
381 RxJavaPlugins.reset();
382 }
383 }
384
385 @Test
386 public void badSourceOther() {
387 List<Throwable> errors = TestHelper.trackPluginErrors();
388 try {
389 new Flowable<Integer>() {
390 @Override
391 protected void subscribeActual(Subscriber<? super Integer> subscriber) {
392 subscriber.onSubscribe(new BooleanSubscription());
393
394 subscriber.onNext(1);
395 subscriber.onComplete();
396 subscriber.onNext(2);
397 subscriber.onError(new TestException());
398 subscriber.onComplete();
399 }
400 }
401 .timeout(1, TimeUnit.DAYS, Flowable.just(3))
402 .test()
403 .assertResult(1);
404
405 TestHelper.assertUndeliverable(errors, 0, TestException.class);
406 } finally {
407 RxJavaPlugins.reset();
408 }
409 }
410
411 @Test
412 public void timedTake() {
413 PublishProcessor<Integer> pp = PublishProcessor.create();
414
415 TestSubscriber<Integer> ts = pp.timeout(1, TimeUnit.DAYS)
416 .take(1)
417 .test();
418
419 assertTrue(pp.hasSubscribers());
420
421 pp.onNext(1);
422
423 assertFalse(pp.hasSubscribers());
424
425 ts.assertResult(1);
426 }
427
428 @Test
429 public void timedFallbackTake() {
430 PublishProcessor<Integer> pp = PublishProcessor.create();
431
432 TestSubscriber<Integer> ts = pp.timeout(1, TimeUnit.DAYS, Flowable.just(2))
433 .take(1)
434 .test();
435
436 assertTrue(pp.hasSubscribers());
437
438 pp.onNext(1);
439
440 assertFalse(pp.hasSubscribers());
441
442 ts.assertResult(1);
443 }
444
445 @Test
446 public void fallbackErrors() {
447 Flowable.never()
448 .timeout(1, TimeUnit.MILLISECONDS, Flowable.error(new TestException()))
449 .test()
450 .awaitDone(5, TimeUnit.SECONDS)
451 .assertFailure(TestException.class);
452 }
453
454 @Test
455 public void onNextOnTimeoutRace() {
456 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
457 final TestScheduler sch = new TestScheduler();
458
459 final PublishProcessor<Integer> pp = PublishProcessor.create();
460
461 TestSubscriberEx<Integer> ts = pp.timeout(1, TimeUnit.SECONDS, sch).to(TestHelper.<Integer>testConsumer());
462
463 Runnable r1 = new Runnable() {
464 @Override
465 public void run() {
466 pp.onNext(1);
467 }
468 };
469
470 Runnable r2 = new Runnable() {
471 @Override
472 public void run() {
473 sch.advanceTimeBy(1, TimeUnit.SECONDS);
474 }
475 };
476
477 TestHelper.race(r1, r2);
478
479 if (ts.values().size() != 0) {
480 if (ts.errors().size() != 0) {
481 ts.assertFailure(TimeoutException.class, 1);
482 ts.assertErrorMessage(timeoutMessage(1, TimeUnit.SECONDS));
483 } else {
484 ts.assertValuesOnly(1);
485 }
486 } else {
487 ts.assertFailure(TimeoutException.class);
488 ts.assertErrorMessage(timeoutMessage(1, TimeUnit.SECONDS));
489 }
490 }
491 }
492
493 @Test
494 public void onNextOnTimeoutRaceFallback() {
495 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
496 final TestScheduler sch = new TestScheduler();
497
498 final PublishProcessor<Integer> pp = PublishProcessor.create();
499
500 TestSubscriberEx<Integer> ts = pp.timeout(1, TimeUnit.SECONDS, sch, Flowable.just(2)).to(TestHelper.<Integer>testConsumer());
501
502 Runnable r1 = new Runnable() {
503 @Override
504 public void run() {
505 pp.onNext(1);
506 }
507 };
508
509 Runnable r2 = new Runnable() {
510 @Override
511 public void run() {
512 sch.advanceTimeBy(1, TimeUnit.SECONDS);
513 }
514 };
515
516 TestHelper.race(r1, r2);
517
518 if (ts.isTerminated()) {
519 int c = ts.values().size();
520 if (c == 1) {
521 int v = ts.values().get(0);
522 assertTrue("" + v, v == 1 || v == 2);
523 } else {
524 ts.assertResult(1, 2);
525 }
526 } else {
527 ts.assertValuesOnly(1);
528 }
529 }
530 }
531
532 @Test
533 public void doubleOnSubscribeFallback() {
534 TestHelper.checkDoubleOnSubscribeFlowable(f -> f.timeout(1, TimeUnit.MINUTES, Flowable.<Object>never()));
535 }
536 }