Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableRetryWithPredicateTest.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.assertEquals;
17 import static org.mockito.ArgumentMatchers.any;
18 import static org.mockito.Mockito.*;
19
20 import java.io.IOException;
21 import java.util.*;
22 import java.util.concurrent.*;
23 import java.util.concurrent.atomic.*;
24
25 import org.junit.Test;
26 import org.mockito.InOrder;
27 import org.reactivestreams.*;
28
29 import io.reactivex.rxjava3.core.*;
30 import io.reactivex.rxjava3.disposables.Disposable;
31 import io.reactivex.rxjava3.exceptions.*;
32 import io.reactivex.rxjava3.functions.*;
33 import io.reactivex.rxjava3.internal.functions.Functions;
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.subscribers.TestSubscriber;
38 import io.reactivex.rxjava3.testsupport.*;
39
40 public class FlowableRetryWithPredicateTest extends RxJavaTest {
41 BiPredicate<Integer, Throwable> retryTwice = new BiPredicate<Integer, Throwable>() {
42 @Override
43 public boolean test(Integer t1, Throwable t2) {
44 return t1 <= 2;
45 }
46 };
47 BiPredicate<Integer, Throwable> retry5 = new BiPredicate<Integer, Throwable>() {
48 @Override
49 public boolean test(Integer t1, Throwable t2) {
50 return t1 <= 5;
51 }
52 };
53 BiPredicate<Integer, Throwable> retryOnTestException = new BiPredicate<Integer, Throwable>() {
54 @Override
55 public boolean test(Integer t1, Throwable t2) {
56 return t2 instanceof IOException;
57 }
58 };
59 @Test
60 public void withNothingToRetry() {
61 Flowable<Integer> source = Flowable.range(0, 3);
62
63 Subscriber<Integer> subscriber = TestHelper.mockSubscriber();
64 InOrder inOrder = inOrder(subscriber);
65
66 source.retry(retryTwice).subscribe(subscriber);
67
68 inOrder.verify(subscriber).onNext(0);
69 inOrder.verify(subscriber).onNext(1);
70 inOrder.verify(subscriber).onNext(2);
71 inOrder.verify(subscriber).onComplete();
72 verify(subscriber, never()).onError(any(Throwable.class));
73 }
74
75 @Test
76 public void retryTwice() {
77 Flowable<Integer> source = Flowable.unsafeCreate(new Publisher<Integer>() {
78 int count;
79 @Override
80 public void subscribe(Subscriber<? super Integer> t1) {
81 t1.onSubscribe(new BooleanSubscription());
82 count++;
83 t1.onNext(0);
84 t1.onNext(1);
85 if (count == 1) {
86 t1.onError(new TestException());
87 return;
88 }
89 t1.onNext(2);
90 t1.onNext(3);
91 t1.onComplete();
92 }
93 });
94
95 Subscriber<Integer> subscriber = TestHelper.mockSubscriber();
96 InOrder inOrder = inOrder(subscriber);
97
98 source.retry(retryTwice).subscribe(subscriber);
99
100 inOrder.verify(subscriber).onNext(0);
101 inOrder.verify(subscriber).onNext(1);
102 inOrder.verify(subscriber).onNext(0);
103 inOrder.verify(subscriber).onNext(1);
104 inOrder.verify(subscriber).onNext(2);
105 inOrder.verify(subscriber).onNext(3);
106 inOrder.verify(subscriber).onComplete();
107 verify(subscriber, never()).onError(any(Throwable.class));
108
109 }
110
111 @Test
112 public void retryTwiceAndGiveUp() {
113 Flowable<Integer> source = Flowable.unsafeCreate(new Publisher<Integer>() {
114 @Override
115 public void subscribe(Subscriber<? super Integer> t1) {
116 t1.onSubscribe(new BooleanSubscription());
117 t1.onNext(0);
118 t1.onNext(1);
119 t1.onError(new TestException());
120 }
121 });
122
123 Subscriber<Integer> subscriber = TestHelper.mockSubscriber();
124 InOrder inOrder = inOrder(subscriber);
125
126 source.retry(retryTwice).subscribe(subscriber);
127
128 inOrder.verify(subscriber).onNext(0);
129 inOrder.verify(subscriber).onNext(1);
130 inOrder.verify(subscriber).onNext(0);
131 inOrder.verify(subscriber).onNext(1);
132 inOrder.verify(subscriber).onNext(0);
133 inOrder.verify(subscriber).onNext(1);
134 inOrder.verify(subscriber).onError(any(TestException.class));
135 verify(subscriber, never()).onComplete();
136
137 }
138
139 @Test
140 public void retryOnSpecificException() {
141 Flowable<Integer> source = Flowable.unsafeCreate(new Publisher<Integer>() {
142 int count;
143 @Override
144 public void subscribe(Subscriber<? super Integer> t1) {
145 t1.onSubscribe(new BooleanSubscription());
146 count++;
147 t1.onNext(0);
148 t1.onNext(1);
149 if (count == 1) {
150 t1.onError(new IOException());
151 return;
152 }
153 t1.onNext(2);
154 t1.onNext(3);
155 t1.onComplete();
156 }
157 });
158
159 Subscriber<Integer> subscriber = TestHelper.mockSubscriber();
160 InOrder inOrder = inOrder(subscriber);
161
162 source.retry(retryOnTestException).subscribe(subscriber);
163
164 inOrder.verify(subscriber).onNext(0);
165 inOrder.verify(subscriber).onNext(1);
166 inOrder.verify(subscriber).onNext(0);
167 inOrder.verify(subscriber).onNext(1);
168 inOrder.verify(subscriber).onNext(2);
169 inOrder.verify(subscriber).onNext(3);
170 inOrder.verify(subscriber).onComplete();
171 verify(subscriber, never()).onError(any(Throwable.class));
172 }
173
174 @Test
175 public void retryOnSpecificExceptionAndNotOther() {
176 final IOException ioe = new IOException();
177 final TestException te = new TestException();
178 Flowable<Integer> source = Flowable.unsafeCreate(new Publisher<Integer>() {
179 int count;
180 @Override
181 public void subscribe(Subscriber<? super Integer> t1) {
182 t1.onSubscribe(new BooleanSubscription());
183 count++;
184 t1.onNext(0);
185 t1.onNext(1);
186 if (count == 1) {
187 t1.onError(ioe);
188 return;
189 }
190 t1.onNext(2);
191 t1.onNext(3);
192 t1.onError(te);
193 }
194 });
195
196 Subscriber<Integer> subscriber = TestHelper.mockSubscriber();
197 InOrder inOrder = inOrder(subscriber);
198
199 source.retry(retryOnTestException).subscribe(subscriber);
200
201 inOrder.verify(subscriber).onNext(0);
202 inOrder.verify(subscriber).onNext(1);
203 inOrder.verify(subscriber).onNext(0);
204 inOrder.verify(subscriber).onNext(1);
205 inOrder.verify(subscriber).onNext(2);
206 inOrder.verify(subscriber).onNext(3);
207 inOrder.verify(subscriber).onError(te);
208 verify(subscriber, never()).onError(ioe);
209 verify(subscriber, never()).onComplete();
210 }
211
212 @Test
213 public void unsubscribeFromRetry() {
214 PublishProcessor<Integer> processor = PublishProcessor.create();
215 final AtomicInteger count = new AtomicInteger(0);
216 Disposable sub = processor.retry(retryTwice).subscribe(new Consumer<Integer>() {
217 @Override
218 public void accept(Integer n) {
219 count.incrementAndGet();
220 }
221 });
222 processor.onNext(1);
223 sub.dispose();
224 processor.onNext(2);
225 assertEquals(1, count.get());
226 }
227
228 @Test
229 public void unsubscribeAfterError() {
230
231 Subscriber<Long> subscriber = TestHelper.mockSubscriber();
232
233 // Flowable that always fails after 100ms
234 FlowableRetryTest.SlowFlowable so = new FlowableRetryTest.SlowFlowable(100, 0, "testUnsubscribeAfterError");
235 Flowable<Long> f = Flowable
236 .unsafeCreate(so)
237 .retry(retry5);
238
239 FlowableRetryTest.AsyncSubscriber<Long> async = new FlowableRetryTest.AsyncSubscriber<>(subscriber);
240
241 f.subscribe(async);
242
243 async.await();
244
245 InOrder inOrder = inOrder(subscriber);
246 // Should fail once
247 inOrder.verify(subscriber, times(1)).onError(any(Throwable.class));
248 inOrder.verify(subscriber, never()).onComplete();
249
250 assertEquals("Start 6 threads, retry 5 then fail on 6", 6, so.efforts.get());
251 assertEquals("Only 1 active subscription", 1, so.maxActive.get());
252 }
253
254 @Test
255 public void timeoutWithRetry() {
256
257 Subscriber<Long> subscriber = TestHelper.mockSubscriber();
258
259 // Flowable that sends every 100ms (timeout fails instead)
260 FlowableRetryTest.SlowFlowable so = new FlowableRetryTest.SlowFlowable(100, 10, "testTimeoutWithRetry");
261 Flowable<Long> f = Flowable
262 .unsafeCreate(so)
263 .timeout(80, TimeUnit.MILLISECONDS)
264 .retry(retry5);
265
266 FlowableRetryTest.AsyncSubscriber<Long> async = new FlowableRetryTest.AsyncSubscriber<>(subscriber);
267
268 f.subscribe(async);
269
270 async.await();
271
272 InOrder inOrder = inOrder(subscriber);
273 // Should fail once
274 inOrder.verify(subscriber, times(1)).onError(any(Throwable.class));
275 inOrder.verify(subscriber, never()).onComplete();
276
277 assertEquals("Start 6 threads, retry 5 then fail on 6", 6, so.efforts.get());
278 }
279
280 @Test
281 public void issue2826() {
282 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
283 final RuntimeException e = new RuntimeException("You shall not pass");
284 final AtomicInteger c = new AtomicInteger();
285 Flowable.just(1).map(new Function<Integer, Integer>() {
286 @Override
287 public Integer apply(Integer t1) {
288 c.incrementAndGet();
289 throw e;
290 }
291 }).retry(retry5).subscribe(ts);
292
293 ts.assertTerminated();
294 assertEquals(6, c.get());
295 assertEquals(Collections.singletonList(e), ts.errors());
296 }
297
298 @Test
299 public void justAndRetry() throws Exception {
300 final AtomicBoolean throwException = new AtomicBoolean(true);
301 int value = Flowable.just(1).map(new Function<Integer, Integer>() {
302 @Override
303 public Integer apply(Integer t1) {
304 if (throwException.compareAndSet(true, false)) {
305 throw new TestException();
306 }
307 return t1;
308 }
309 }).retry(1).blockingSingle();
310
311 assertEquals(1, value);
312 }
313
314 @Test
315 public void issue3008RetryWithPredicate() {
316 final List<Long> list = new CopyOnWriteArrayList<>();
317 final AtomicBoolean isFirst = new AtomicBoolean(true);
318 Flowable.<Long> just(1L, 2L, 3L).map(new Function<Long, Long>() {
319 @Override
320 public Long apply(Long x) {
321 System.out.println("map " + x);
322 if (x == 2 && isFirst.getAndSet(false)) {
323 throw new RuntimeException("retryable error");
324 }
325 return x;
326 }})
327 .retry(new BiPredicate<Integer, Throwable>() {
328 @Override
329 public boolean test(Integer t1, Throwable t2) {
330 return true;
331 }})
332 .forEach(new Consumer<Long>() {
333
334 @Override
335 public void accept(Long t) {
336 System.out.println(t);
337 list.add(t);
338 }});
339 assertEquals(Arrays.asList(1L, 1L, 2L, 3L), list);
340 }
341
342 @Test
343 public void issue3008RetryInfinite() {
344 final List<Long> list = new CopyOnWriteArrayList<>();
345 final AtomicBoolean isFirst = new AtomicBoolean(true);
346 Flowable.<Long> just(1L, 2L, 3L).map(new Function<Long, Long>() {
347 @Override
348 public Long apply(Long x) {
349 System.out.println("map " + x);
350 if (x == 2 && isFirst.getAndSet(false)) {
351 throw new RuntimeException("retryable error");
352 }
353 return x;
354 }})
355 .retry()
356 .forEach(new Consumer<Long>() {
357
358 @Override
359 public void accept(Long t) {
360 System.out.println(t);
361 list.add(t);
362 }});
363 assertEquals(Arrays.asList(1L, 1L, 2L, 3L), list);
364 }
365
366 @Test
367 public void backpressure() {
368 final List<Long> requests = new ArrayList<>();
369
370 Flowable<Integer> source = Flowable
371 .just(1)
372 .concatWith(Flowable.<Integer>error(new TestException()))
373 .doOnRequest(new LongConsumer() {
374 @Override
375 public void accept(long t) {
376 requests.add(t);
377 }
378 });
379
380 TestSubscriber<Integer> ts = new TestSubscriber<>(3L);
381 source
382 .retry(new BiPredicate<Integer, Throwable>() {
383 @Override
384 public boolean test(Integer t1, Throwable t2) {
385 return t1 < 4; // FIXME was 3 in 1.x for some reason
386 }
387 }).subscribe(ts);
388
389 assertEquals(Arrays.asList(3L, 2L, 1L), requests);
390 ts.assertValues(1, 1, 1);
391 ts.assertNotComplete();
392 ts.assertNoErrors();
393 }
394
395 @Test
396 public void predicateThrows() {
397
398 TestSubscriberEx<Object> ts = Flowable.error(new TestException("Outer"))
399 .retry(new Predicate<Throwable>() {
400 @Override
401 public boolean test(Throwable e) throws Exception {
402 throw new TestException("Inner");
403 }
404 })
405 .to(TestHelper.<Object>testConsumer())
406 .assertFailure(CompositeException.class);
407
408 List<Throwable> errors = TestHelper.compositeList(ts.errors().get(0));
409
410 TestHelper.assertError(errors, 0, TestException.class, "Outer");
411 TestHelper.assertError(errors, 1, TestException.class, "Inner");
412 }
413
414 @Test
415 public void dontRetry() {
416 Flowable.error(new TestException("Outer"))
417 .retry(Functions.alwaysFalse())
418 .to(TestHelper.<Object>testConsumer())
419 .assertFailureAndMessage(TestException.class, "Outer");
420 }
421
422 @Test
423 public void retryDisposeRace() {
424 final TestException ex = new TestException();
425 RxJavaPlugins.setErrorHandler(Functions.emptyConsumer());
426 try {
427 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
428 final PublishProcessor<Integer> pp = PublishProcessor.create();
429
430 final TestSubscriber<Integer> ts = pp.retry(Functions.alwaysTrue()).test();
431
432 Runnable r1 = new Runnable() {
433 @Override
434 public void run() {
435 pp.onError(ex);
436 }
437 };
438
439 Runnable r2 = new Runnable() {
440 @Override
441 public void run() {
442 ts.cancel();
443 }
444 };
445
446 TestHelper.race(r1, r2);
447
448 ts.assertEmpty();
449 }
450 } finally {
451 RxJavaPlugins.reset();
452 }
453 }
454
455 @Test
456 public void bipredicateThrows() {
457
458 TestSubscriberEx<Object> ts = Flowable.error(new TestException("Outer"))
459 .retry(new BiPredicate<Integer, Throwable>() {
460 @Override
461 public boolean test(Integer n, Throwable e) throws Exception {
462 throw new TestException("Inner");
463 }
464 })
465 .to(TestHelper.<Object>testConsumer())
466 .assertFailure(CompositeException.class);
467
468 List<Throwable> errors = TestHelper.compositeList(ts.errors().get(0));
469
470 TestHelper.assertError(errors, 0, TestException.class, "Outer");
471 TestHelper.assertError(errors, 1, TestException.class, "Inner");
472 }
473
474 @Test
475 public void retryBiPredicateDisposeRace() {
476 RxJavaPlugins.setErrorHandler(Functions.emptyConsumer());
477 try {
478 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
479 final PublishProcessor<Integer> pp = PublishProcessor.create();
480
481 final TestSubscriber<Integer> ts = pp.retry(new BiPredicate<Object, Object>() {
482 @Override
483 public boolean test(Object t1, Object t2) throws Exception {
484 return true;
485 }
486 }).test();
487
488 final TestException ex = new TestException();
489
490 Runnable r1 = new Runnable() {
491 @Override
492 public void run() {
493 pp.onError(ex);
494 }
495 };
496
497 Runnable r2 = new Runnable() {
498 @Override
499 public void run() {
500 ts.cancel();
501 }
502 };
503
504 TestHelper.race(r1, r2);
505
506 ts.assertEmpty();
507 }
508 } finally {
509 RxJavaPlugins.reset();
510 }
511 }
512 }