Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableRetryTest.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.*;
26 import org.reactivestreams.*;
27
28 import io.reactivex.rxjava3.core.*;
29 import io.reactivex.rxjava3.disposables.Disposable;
30 import io.reactivex.rxjava3.exceptions.TestException;
31 import io.reactivex.rxjava3.flowables.GroupedFlowable;
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.schedulers.Schedulers;
38 import io.reactivex.rxjava3.subscribers.*;
39 import io.reactivex.rxjava3.testsupport.*;
40
41 public class FlowableRetryTest extends RxJavaTest {
42
43 @Test
44 public void iterativeBackoff() {
45 Subscriber<String> consumer = TestHelper.mockSubscriber();
46
47 Flowable<String> producer = Flowable.unsafeCreate(new Publisher<String>() {
48
49 private AtomicInteger count = new AtomicInteger(4);
50 long last = System.currentTimeMillis();
51
52 @Override
53 public void subscribe(Subscriber<? super String> t1) {
54 t1.onSubscribe(new BooleanSubscription());
55 System.out.println(count.get() + " @ " + String.valueOf(last - System.currentTimeMillis()));
56 last = System.currentTimeMillis();
57 if (count.getAndDecrement() == 0) {
58 t1.onNext("hello");
59 t1.onComplete();
60 } else {
61 t1.onError(new RuntimeException());
62 }
63 }
64
65 });
66 TestSubscriber<String> ts = new TestSubscriber<>(consumer);
67 producer.retryWhen(new Function<Flowable<? extends Throwable>, Flowable<Object>>() {
68
69 @Override
70 public Flowable<Object> apply(Flowable<? extends Throwable> attempts) {
71 // Worker w = Schedulers.computation().createWorker();
72 return attempts
73 .map(new Function<Throwable, Tuple>() {
74 @Override
75 public Tuple apply(Throwable n) {
76 return new Tuple(1L, n);
77 }})
78 .scan(new BiFunction<Tuple, Tuple, Tuple>() {
79 @Override
80 public Tuple apply(Tuple t, Tuple n) {
81 return new Tuple(t.count + n.count, n.n);
82 }})
83 .flatMap(new Function<Tuple, Flowable<Object>>() {
84 @Override
85 public Flowable<Object> apply(Tuple t) {
86 System.out.println("Retry # " + t.count);
87 return t.count > 20 ?
88 Flowable.<Object>error(t.n) :
89 Flowable.timer(t.count * 1L, TimeUnit.MILLISECONDS)
90 .cast(Object.class);
91 }});
92 }
93 }).subscribe(ts);
94 ts.awaitDone(5, TimeUnit.SECONDS);
95 ts.assertNoErrors();
96
97 InOrder inOrder = inOrder(consumer);
98 inOrder.verify(consumer, never()).onError(any(Throwable.class));
99 inOrder.verify(consumer, times(1)).onNext("hello");
100 inOrder.verify(consumer, times(1)).onComplete();
101 inOrder.verifyNoMoreInteractions();
102
103 }
104
105 public static class Tuple {
106 Long count;
107 Throwable n;
108
109 Tuple(Long c, Throwable n) {
110 count = c;
111 this.n = n;
112 }
113 }
114
115 @Test
116 public void retryIndefinitely() {
117 Subscriber<String> subscriber = TestHelper.mockSubscriber();
118 int numRetries = 20;
119 Flowable<String> origin = Flowable.unsafeCreate(new FuncWithErrors(numRetries));
120 origin.retry().subscribe(new TestSubscriber<>(subscriber));
121
122 InOrder inOrder = inOrder(subscriber);
123 // should show 3 attempts
124 inOrder.verify(subscriber, times(numRetries + 1)).onNext("beginningEveryTime");
125 // should have no errors
126 inOrder.verify(subscriber, never()).onError(any(Throwable.class));
127 // should have a single success
128 inOrder.verify(subscriber, times(1)).onNext("onSuccessOnly");
129 // should have a single successful onComplete
130 inOrder.verify(subscriber, times(1)).onComplete();
131 inOrder.verifyNoMoreInteractions();
132 }
133
134 @Test
135 public void schedulingNotificationHandler() {
136 Subscriber<String> subscriber = TestHelper.mockSubscriber();
137 int numRetries = 2;
138 Flowable<String> origin = Flowable.unsafeCreate(new FuncWithErrors(numRetries));
139 TestSubscriber<String> ts = new TestSubscriber<>(subscriber);
140 origin.retryWhen(new Function<Flowable<? extends Throwable>, Flowable<Object>>() {
141 @Override
142 public Flowable<Object> apply(Flowable<? extends Throwable> t1) {
143 return t1.observeOn(Schedulers.computation()).map(new Function<Throwable, Integer>() {
144 @Override
145 public Integer apply(Throwable t1) {
146 return 1;
147 }
148 }).startWithItem(1).cast(Object.class);
149 }
150 })
151 .doOnError(new Consumer<Throwable>() {
152 @Override
153 public void accept(Throwable e) {
154 e.printStackTrace();
155 }
156 })
157 .subscribe(ts);
158
159 ts.awaitDone(5, TimeUnit.SECONDS);
160 InOrder inOrder = inOrder(subscriber);
161 // should show 3 attempts
162 inOrder.verify(subscriber, times(1 + numRetries)).onNext("beginningEveryTime");
163 // should have no errors
164 inOrder.verify(subscriber, never()).onError(any(Throwable.class));
165 // should have a single success
166 inOrder.verify(subscriber, times(1)).onNext("onSuccessOnly");
167 // should have a single successful onComplete
168 inOrder.verify(subscriber, times(1)).onComplete();
169 inOrder.verifyNoMoreInteractions();
170 }
171
172 @Test
173 public void onNextFromNotificationHandler() {
174 Subscriber<String> subscriber = TestHelper.mockSubscriber();
175 int numRetries = 2;
176 Flowable<String> origin = Flowable.unsafeCreate(new FuncWithErrors(numRetries));
177 origin.retryWhen(new Function<Flowable<? extends Throwable>, Flowable<Object>>() {
178 @Override
179 public Flowable<Object> apply(Flowable<? extends Throwable> t1) {
180 return t1.map(new Function<Throwable, Integer>() {
181
182 @Override
183 public Integer apply(Throwable t1) {
184 return 0;
185 }
186 }).startWithItem(0).cast(Object.class);
187 }
188 }).subscribe(subscriber);
189
190 InOrder inOrder = inOrder(subscriber);
191 // should show 3 attempts
192 inOrder.verify(subscriber, times(numRetries + 1)).onNext("beginningEveryTime");
193 // should have no errors
194 inOrder.verify(subscriber, never()).onError(any(Throwable.class));
195 // should have a single success
196 inOrder.verify(subscriber, times(1)).onNext("onSuccessOnly");
197 // should have a single successful onComplete
198 inOrder.verify(subscriber, times(1)).onComplete();
199 inOrder.verifyNoMoreInteractions();
200 }
201
202 @Test
203 public void onCompletedFromNotificationHandler() {
204 Subscriber<String> subscriber = TestHelper.mockSubscriber();
205 Flowable<String> origin = Flowable.unsafeCreate(new FuncWithErrors(1));
206 TestSubscriber<String> ts = new TestSubscriber<>(subscriber);
207 origin.retryWhen(new Function<Flowable<? extends Throwable>, Flowable<Object>>() {
208 @Override
209 public Flowable<Object> apply(Flowable<? extends Throwable> t1) {
210 return Flowable.empty();
211 }
212 }).subscribe(ts);
213
214 InOrder inOrder = inOrder(subscriber);
215 inOrder.verify(subscriber).onSubscribe((Subscription)notNull());
216 inOrder.verify(subscriber, never()).onNext("beginningEveryTime");
217 inOrder.verify(subscriber, never()).onNext("onSuccessOnly");
218 inOrder.verify(subscriber, times(1)).onComplete();
219 inOrder.verify(subscriber, never()).onError(any(Exception.class));
220 inOrder.verifyNoMoreInteractions();
221 }
222
223 @Test
224 public void onErrorFromNotificationHandler() {
225 Subscriber<String> subscriber = TestHelper.mockSubscriber();
226 Flowable<String> origin = Flowable.unsafeCreate(new FuncWithErrors(2));
227 origin.retryWhen(new Function<Flowable<? extends Throwable>, Flowable<Object>>() {
228 @Override
229 public Flowable<Object> apply(Flowable<? extends Throwable> t1) {
230 return Flowable.error(new RuntimeException());
231 }
232 }).subscribe(subscriber);
233
234 InOrder inOrder = inOrder(subscriber);
235 inOrder.verify(subscriber).onSubscribe((Subscription)notNull());
236 inOrder.verify(subscriber, never()).onNext("beginningEveryTime");
237 inOrder.verify(subscriber, never()).onNext("onSuccessOnly");
238 inOrder.verify(subscriber, never()).onComplete();
239 inOrder.verify(subscriber, times(1)).onError(any(RuntimeException.class));
240 inOrder.verifyNoMoreInteractions();
241 }
242
243 @Test
244 public void singleSubscriptionOnFirst() throws Exception {
245 final AtomicInteger inc = new AtomicInteger(0);
246 Publisher<Integer> onSubscribe = new Publisher<Integer>() {
247 @Override
248 public void subscribe(Subscriber<? super Integer> subscriber) {
249 subscriber.onSubscribe(new BooleanSubscription());
250 final int emit = inc.incrementAndGet();
251 subscriber.onNext(emit);
252 subscriber.onComplete();
253 }
254 };
255
256 int first = Flowable.unsafeCreate(onSubscribe)
257 .retryWhen(new Function<Flowable<? extends Throwable>, Flowable<Object>>() {
258 @Override
259 public Flowable<Object> apply(Flowable<? extends Throwable> attempt) {
260 return attempt.zipWith(Flowable.just(1), new BiFunction<Throwable, Integer, Object>() {
261 @Override
262 public Object apply(Throwable o, Integer integer) {
263 return 0;
264 }
265 });
266 }
267 })
268 .blockingFirst();
269
270 assertEquals("Observer did not receive the expected output", 1, first);
271 assertEquals("Subscribe was not called once", 1, inc.get());
272 }
273
274 @Test
275 public void originFails() {
276 Subscriber<String> subscriber = TestHelper.mockSubscriber();
277 Flowable<String> origin = Flowable.unsafeCreate(new FuncWithErrors(1));
278 origin.subscribe(subscriber);
279
280 InOrder inOrder = inOrder(subscriber);
281 inOrder.verify(subscriber, times(1)).onNext("beginningEveryTime");
282 inOrder.verify(subscriber, times(1)).onError(any(RuntimeException.class));
283 inOrder.verify(subscriber, never()).onNext("onSuccessOnly");
284 inOrder.verify(subscriber, never()).onComplete();
285 }
286
287 @Test
288 public void retryFail() {
289 int numRetries = 1;
290 int numFailures = 2;
291 Subscriber<String> subscriber = TestHelper.mockSubscriber();
292 Flowable<String> origin = Flowable.unsafeCreate(new FuncWithErrors(numFailures));
293 origin.retry(numRetries).subscribe(subscriber);
294
295 InOrder inOrder = inOrder(subscriber);
296 // should show 2 attempts (first time fail, second time (1st retry) fail)
297 inOrder.verify(subscriber, times(1 + numRetries)).onNext("beginningEveryTime");
298 // should only retry once, fail again and emit onError
299 inOrder.verify(subscriber, times(1)).onError(any(RuntimeException.class));
300 // no success
301 inOrder.verify(subscriber, never()).onNext("onSuccessOnly");
302 inOrder.verify(subscriber, never()).onComplete();
303 inOrder.verifyNoMoreInteractions();
304 }
305
306 @Test
307 public void retrySuccess() {
308 int numFailures = 1;
309 Subscriber<String> subscriber = TestHelper.mockSubscriber();
310 Flowable<String> origin = Flowable.unsafeCreate(new FuncWithErrors(numFailures));
311 origin.retry(3).subscribe(subscriber);
312
313 InOrder inOrder = inOrder(subscriber);
314 // should show 3 attempts
315 inOrder.verify(subscriber, times(1 + numFailures)).onNext("beginningEveryTime");
316 // should have no errors
317 inOrder.verify(subscriber, never()).onError(any(Throwable.class));
318 // should have a single success
319 inOrder.verify(subscriber, times(1)).onNext("onSuccessOnly");
320 // should have a single successful onComplete
321 inOrder.verify(subscriber, times(1)).onComplete();
322 inOrder.verifyNoMoreInteractions();
323 }
324
325 @Test
326 public void infiniteRetry() {
327 int numFailures = 20;
328 Subscriber<String> subscriber = TestHelper.mockSubscriber();
329 Flowable<String> origin = Flowable.unsafeCreate(new FuncWithErrors(numFailures));
330 origin.retry().subscribe(subscriber);
331
332 InOrder inOrder = inOrder(subscriber);
333 // should show 3 attempts
334 inOrder.verify(subscriber, times(1 + numFailures)).onNext("beginningEveryTime");
335 // should have no errors
336 inOrder.verify(subscriber, never()).onError(any(Throwable.class));
337 // should have a single success
338 inOrder.verify(subscriber, times(1)).onNext("onSuccessOnly");
339 // should have a single successful onComplete
340 inOrder.verify(subscriber, times(1)).onComplete();
341 inOrder.verifyNoMoreInteractions();
342 }
343
344 /*
345 * Checks in a simple and synchronous way that retry resubscribes
346 * after error. This test fails against 0.16.1-0.17.4, hangs on 0.17.5 and
347 * passes in 0.17.6 thanks to fix for issue #1027.
348 */
349 @SuppressWarnings("unchecked")
350 @Test
351 public void retrySubscribesAgainAfterError() throws Throwable {
352
353 // record emitted values with this action
354 Consumer<Integer> record = mock(Consumer.class);
355 InOrder inOrder = inOrder(record);
356
357 // always throw an exception with this action
358 Consumer<Integer> throwException = mock(Consumer.class);
359 doThrow(new RuntimeException()).when(throwException).accept(Mockito.anyInt());
360
361 // create a retrying Flowable based on a PublishProcessor
362 PublishProcessor<Integer> processor = PublishProcessor.create();
363 processor
364 // record item
365 .doOnNext(record)
366 // throw a RuntimeException
367 .doOnNext(throwException)
368 // retry on error
369 .retry()
370 // subscribe and ignore
371 .subscribe();
372
373 inOrder.verifyNoMoreInteractions();
374
375 processor.onNext(1);
376 inOrder.verify(record).accept(1);
377
378 processor.onNext(2);
379 inOrder.verify(record).accept(2);
380
381 processor.onNext(3);
382 inOrder.verify(record).accept(3);
383
384 inOrder.verifyNoMoreInteractions();
385 }
386
387 public static class FuncWithErrors implements Publisher<String> {
388
389 private final int numFailures;
390 private final AtomicInteger count = new AtomicInteger(0);
391
392 FuncWithErrors(int count) {
393 this.numFailures = count;
394 }
395
396 @Override
397 public void subscribe(final Subscriber<? super String> subscriber) {
398 subscriber.onSubscribe(new Subscription() {
399 final AtomicLong req = new AtomicLong();
400 // 0 = not set, 1 = fast path, 2 = backpressure
401 final AtomicInteger path = new AtomicInteger(0);
402 volatile boolean done;
403
404 @Override
405 public void request(long n) {
406 if (n == Long.MAX_VALUE && path.compareAndSet(0, 1)) {
407 subscriber.onNext("beginningEveryTime");
408 int i = count.getAndIncrement();
409 if (i < numFailures) {
410 subscriber.onError(new RuntimeException("forced failure: " + (i + 1)));
411 } else {
412 subscriber.onNext("onSuccessOnly");
413 subscriber.onComplete();
414 }
415 return;
416 }
417 if (n > 0 && req.getAndAdd(n) == 0 && (path.get() == 2 || path.compareAndSet(0, 2)) && !done) {
418 int i = count.getAndIncrement();
419 if (i < numFailures) {
420 subscriber.onNext("beginningEveryTime");
421 subscriber.onError(new RuntimeException("forced failure: " + (i + 1)));
422 done = true;
423 } else {
424 do {
425 if (i == numFailures) {
426 subscriber.onNext("beginningEveryTime");
427 } else
428 if (i > numFailures) {
429 subscriber.onNext("onSuccessOnly");
430 subscriber.onComplete();
431 done = true;
432 break;
433 }
434 i = count.getAndIncrement();
435 } while (req.decrementAndGet() > 0);
436 }
437 }
438 }
439
440 @Override
441 public void cancel() {
442 // TODO Auto-generated method stub
443
444 }
445 });
446 }
447 }
448
449 @Test
450 public void unsubscribeFromRetry() {
451 PublishProcessor<Integer> processor = PublishProcessor.create();
452 final AtomicInteger count = new AtomicInteger(0);
453 Disposable sub = processor.retry().subscribe(new Consumer<Integer>() {
454 @Override
455 public void accept(Integer n) {
456 count.incrementAndGet();
457 }
458 });
459 processor.onNext(1);
460 sub.dispose();
461 processor.onNext(2);
462 assertEquals(1, count.get());
463 }
464
465 @Test
466 public void retryAllowsSubscriptionAfterAllSubscriptionsUnsubscribed() throws InterruptedException {
467 final AtomicInteger subsCount = new AtomicInteger(0);
468 Publisher<String> onSubscribe = new Publisher<String>() {
469 @Override
470 public void subscribe(Subscriber<? super String> s) {
471 subsCount.incrementAndGet();
472 s.onSubscribe(new Subscription() {
473
474 @Override
475 public void request(long n) {
476
477 }
478
479 @Override
480 public void cancel() {
481 subsCount.decrementAndGet();
482 }
483 });
484
485 }
486 };
487 Flowable<String> stream = Flowable.unsafeCreate(onSubscribe);
488 Flowable<String> streamWithRetry = stream.retry();
489 Disposable sub = streamWithRetry.subscribe();
490 assertEquals(1, subsCount.get());
491 sub.dispose();
492 assertEquals(0, subsCount.get());
493 streamWithRetry.subscribe();
494 assertEquals(1, subsCount.get());
495 }
496
497 @Test
498 public void sourceFlowableCallsUnsubscribe() throws InterruptedException {
499 final AtomicInteger subsCount = new AtomicInteger(0);
500
501 final TestSubscriber<String> ts = new TestSubscriber<>();
502
503 Publisher<String> onSubscribe = new Publisher<String>() {
504 @Override
505 public void subscribe(Subscriber<? super String> s) {
506 BooleanSubscription bs = new BooleanSubscription();
507 // if isUnsubscribed is true that means we have a bug such as
508 // https://github.com/ReactiveX/RxJava/issues/1024
509 if (!bs.isCancelled()) {
510 subsCount.incrementAndGet();
511 s.onError(new RuntimeException("failed"));
512 // it unsubscribes the child directly
513 // this simulates various error/completion scenarios that could occur
514 // or just a source that proactively triggers cleanup
515 // FIXME can't unsubscribe child
516 // s.unsubscribe();
517 bs.cancel();
518 } else {
519 s.onError(new RuntimeException());
520 }
521 }
522 };
523
524 Flowable.unsafeCreate(onSubscribe).retry(3).subscribe(ts);
525 assertEquals(4, subsCount.get()); // 1 + 3 retries
526 }
527
528 @Test
529 public void sourceFlowableRetry1() throws InterruptedException {
530 final AtomicInteger subsCount = new AtomicInteger(0);
531
532 final TestSubscriber<String> ts = new TestSubscriber<>();
533
534 Publisher<String> onSubscribe = new Publisher<String>() {
535 @Override
536 public void subscribe(Subscriber<? super String> s) {
537 s.onSubscribe(new BooleanSubscription());
538 subsCount.incrementAndGet();
539 s.onError(new RuntimeException("failed"));
540 }
541 };
542
543 Flowable.unsafeCreate(onSubscribe).retry(1).subscribe(ts);
544 assertEquals(2, subsCount.get());
545 }
546
547 @Test
548 public void sourceFlowableRetry0() throws InterruptedException {
549 final AtomicInteger subsCount = new AtomicInteger(0);
550
551 final TestSubscriber<String> ts = new TestSubscriber<>();
552
553 Publisher<String> onSubscribe = new Publisher<String>() {
554 @Override
555 public void subscribe(Subscriber<? super String> s) {
556 s.onSubscribe(new BooleanSubscription());
557 subsCount.incrementAndGet();
558 s.onError(new RuntimeException("failed"));
559 }
560 };
561
562 Flowable.unsafeCreate(onSubscribe).retry(0).subscribe(ts);
563 assertEquals(1, subsCount.get());
564 }
565
566 static final class SlowFlowable implements Publisher<Long> {
567
568 final AtomicInteger efforts = new AtomicInteger(0);
569 final AtomicInteger active = new AtomicInteger(0);
570 final AtomicInteger maxActive = new AtomicInteger(0);
571 final AtomicInteger nextBeforeFailure;
572 final String context;
573
574 private final int emitDelay;
575
576 SlowFlowable(int emitDelay, int countNext, String context) {
577 this.emitDelay = emitDelay;
578 this.nextBeforeFailure = new AtomicInteger(countNext);
579 this.context = context;
580 }
581
582 @Override
583 public void subscribe(final Subscriber<? super Long> subscriber) {
584 final AtomicBoolean terminate = new AtomicBoolean(false);
585 subscriber.onSubscribe(new Subscription() {
586 @Override
587 public void request(long n) {
588 // TODO Auto-generated method stub
589
590 }
591
592 @Override
593 public void cancel() {
594 terminate.set(true);
595 active.decrementAndGet();
596 }
597 });
598 efforts.getAndIncrement();
599 active.getAndIncrement();
600 maxActive.set(Math.max(active.get(), maxActive.get()));
601 final Thread thread = new Thread(context) {
602 @Override
603 public void run() {
604 long nr = 0;
605 try {
606 while (!terminate.get()) {
607 Thread.sleep(emitDelay);
608 if (nextBeforeFailure.getAndDecrement() > 0) {
609 subscriber.onNext(nr++);
610 } else {
611 active.decrementAndGet();
612 subscriber.onError(new RuntimeException("expected-failed"));
613 break;
614 }
615 }
616 } catch (InterruptedException t) {
617 }
618 }
619 };
620 thread.start();
621 }
622 }
623
624 /** Observer for listener on separate thread. */
625 static final class AsyncSubscriber<T> extends DefaultSubscriber<T> {
626
627 protected CountDownLatch latch = new CountDownLatch(1);
628
629 protected Subscriber<T> target;
630
631 /**
632 * Wrap existing Observer.
633 * @param target the target subscriber
634 */
635 AsyncSubscriber(Subscriber<T> target) {
636 this.target = target;
637 }
638
639 /** Wait. */
640 public void await() {
641 try {
642 latch.await();
643 } catch (InterruptedException e) {
644 fail("Test interrupted");
645 }
646 }
647
648 // Observer implementation
649
650 @Override
651 public void onComplete() {
652 target.onComplete();
653 latch.countDown();
654 }
655
656 @Override
657 public void onError(Throwable t) {
658 target.onError(t);
659 latch.countDown();
660 }
661
662 @Override
663 public void onNext(T v) {
664 target.onNext(v);
665 }
666 }
667
668 @Test
669 public void unsubscribeAfterError() {
670
671 Subscriber<Long> subscriber = TestHelper.mockSubscriber();
672
673 // Flowable that always fails after 100ms
674 SlowFlowable so = new SlowFlowable(100, 0, "testUnsubscribeAfterError");
675 Flowable<Long> f = Flowable.unsafeCreate(so).retry(5);
676
677 AsyncSubscriber<Long> async = new AsyncSubscriber<>(subscriber);
678
679 f.subscribe(async);
680
681 async.await();
682
683 InOrder inOrder = inOrder(subscriber);
684 // Should fail once
685 inOrder.verify(subscriber, times(1)).onError(any(Throwable.class));
686 inOrder.verify(subscriber, never()).onComplete();
687
688 assertEquals("Start 6 threads, retry 5 then fail on 6", 6, so.efforts.get());
689 assertEquals("Only 1 active subscription", 1, so.maxActive.get());
690 }
691
692 @Test
693 public void timeoutWithRetry() {
694
695 Subscriber<Long> subscriber = TestHelper.mockSubscriber();
696
697 // Flowable that sends every 100ms (timeout fails instead)
698 SlowFlowable sf = new SlowFlowable(100, 10, "testTimeoutWithRetry");
699 Flowable<Long> f = Flowable.unsafeCreate(sf).timeout(80, TimeUnit.MILLISECONDS).retry(5);
700
701 AsyncSubscriber<Long> async = new AsyncSubscriber<>(subscriber);
702
703 f.subscribe(async);
704
705 async.await();
706
707 InOrder inOrder = inOrder(subscriber);
708 // Should fail once
709 inOrder.verify(subscriber, times(1)).onError(any(Throwable.class));
710 inOrder.verify(subscriber, never()).onComplete();
711
712 assertEquals("Start 6 threads, retry 5 then fail on 6", 6, sf.efforts.get());
713 }
714
715 @Test
716 public void retryWithBackpressure() throws InterruptedException {
717 final int NUM_LOOPS = 1;
718 for (int j = 0; j < NUM_LOOPS; j++) {
719 final int numRetries = Flowable.bufferSize() * 2;
720 for (int i = 0; i < 400; i++) {
721 Subscriber<String> subscriber = TestHelper.mockSubscriber();
722 Flowable<String> origin = Flowable.unsafeCreate(new FuncWithErrors(numRetries));
723 TestSubscriberEx<String> ts = new TestSubscriberEx<>(subscriber);
724 origin.retry().observeOn(Schedulers.computation()).subscribe(ts);
725 ts.awaitDone(5, TimeUnit.SECONDS);
726
727 InOrder inOrder = inOrder(subscriber);
728 // should have no errors
729 verify(subscriber, never()).onError(any(Throwable.class));
730 // should show numRetries attempts
731 inOrder.verify(subscriber, times(numRetries + 1)).onNext("beginningEveryTime");
732 // should have a single success
733 inOrder.verify(subscriber, times(1)).onNext("onSuccessOnly");
734 // should have a single successful onComplete
735 inOrder.verify(subscriber, times(1)).onComplete();
736 inOrder.verifyNoMoreInteractions();
737 }
738 }
739 }
740
741 @Test
742 public void retryWithBackpressureParallel() throws InterruptedException {
743 final int NUM_LOOPS = 1;
744 final int numRetries = Flowable.bufferSize() * 2;
745 int ncpu = Runtime.getRuntime().availableProcessors();
746 ExecutorService exec = Executors.newFixedThreadPool(Math.max(ncpu / 2, 2));
747 try {
748 for (int r = 0; r < NUM_LOOPS; r++) {
749 if (r % 10 == 0) {
750 System.out.println("testRetryWithBackpressureParallelLoop -> " + r);
751 }
752
753 final AtomicInteger timeouts = new AtomicInteger();
754 final Map<Integer, List<String>> data = new ConcurrentHashMap<>();
755
756 int m = 5000;
757 final CountDownLatch cdl = new CountDownLatch(m);
758 for (int i = 0; i < m; i++) {
759 final int j = i;
760 exec.execute(new Runnable() {
761 @Override
762 public void run() {
763 final AtomicInteger nexts = new AtomicInteger();
764 try {
765 Flowable<String> origin = Flowable.unsafeCreate(new FuncWithErrors(numRetries));
766 TestSubscriberEx<String> ts = new TestSubscriberEx<>();
767 origin.retry()
768 .observeOn(Schedulers.computation()).subscribe(ts);
769 ts.awaitDone(2500, TimeUnit.MILLISECONDS);
770 List<String> onNextEvents = new ArrayList<>(ts.values());
771 if (onNextEvents.size() != numRetries + 2) {
772 for (Throwable t : ts.errors()) {
773 onNextEvents.add(t.toString());
774 }
775 for (long err = ts.completions(); err != 0; err--) {
776 onNextEvents.add("onComplete");
777 }
778 data.put(j, onNextEvents);
779 }
780 } catch (Throwable t) {
781 timeouts.incrementAndGet();
782 System.out.println(j + " | " + cdl.getCount() + " !!! " + nexts.get());
783 }
784 cdl.countDown();
785 }
786 });
787 }
788 cdl.await();
789 assertEquals(0, timeouts.get());
790 if (data.size() > 0) {
791 fail("Data content mismatch: " + allSequenceFrequency(data));
792 }
793 }
794 } finally {
795 exec.shutdown();
796 }
797 }
798 static <T> StringBuilder allSequenceFrequency(Map<Integer, List<T>> its) {
799 StringBuilder b = new StringBuilder();
800 for (Map.Entry<Integer, List<T>> e : its.entrySet()) {
801 if (b.length() > 0) {
802 b.append(", ");
803 }
804 b.append(e.getKey()).append("={");
805 b.append(sequenceFrequency(e.getValue()));
806 b.append("}");
807 }
808 return b;
809 }
810 static <T> StringBuilder sequenceFrequency(Iterable<T> it) {
811 StringBuilder sb = new StringBuilder();
812
813 Object prev = null;
814 int cnt = 0;
815
816 for (Object curr : it) {
817 if (sb.length() > 0) {
818 if (!curr.equals(prev)) {
819 if (cnt > 1) {
820 sb.append(" x ").append(cnt);
821 cnt = 1;
822 }
823 sb.append(", ");
824 sb.append(curr);
825 } else {
826 cnt++;
827 }
828 } else {
829 sb.append(curr);
830 cnt++;
831 }
832 prev = curr;
833 }
834 if (cnt > 1) {
835 sb.append(" x ").append(cnt);
836 }
837
838 return sb;
839 }
840
841 @Test
842 public void issue1900() throws InterruptedException {
843 Subscriber<String> subscriber = TestHelper.mockSubscriber();
844 final int NUM_MSG = 1034;
845 final AtomicInteger count = new AtomicInteger();
846
847 Flowable<String> origin = Flowable.range(0, NUM_MSG)
848 .map(new Function<Integer, String>() {
849 @Override
850 public String apply(Integer t1) {
851 return "msg: " + count.incrementAndGet();
852 }
853 });
854
855 origin.retry()
856 .groupBy(new Function<String, String>() {
857 @Override
858 public String apply(String t1) {
859 return t1;
860 }
861 })
862 .flatMap(new Function<GroupedFlowable<String, String>, Flowable<String>>() {
863 @Override
864 public Flowable<String> apply(GroupedFlowable<String, String> t1) {
865 return t1.take(1);
866 }
867 }, NUM_MSG) // Must request as many groups as groupBy produces to avoid MBE
868 .subscribe(new TestSubscriber<>(subscriber));
869
870 InOrder inOrder = inOrder(subscriber);
871 // should show 3 attempts
872 inOrder.verify(subscriber, times(NUM_MSG)).onNext(any(java.lang.String.class));
873 // // should have no errors
874 inOrder.verify(subscriber, never()).onError(any(Throwable.class));
875 // should have a single success
876 //inOrder.verify(observer, times(1)).onNext("onSuccessOnly");
877 // should have a single successful onComplete
878 inOrder.verify(subscriber, times(1)).onComplete();
879 inOrder.verifyNoMoreInteractions();
880 }
881
882 @Test
883 public void issue1900SourceNotSupportingBackpressure() {
884 Subscriber<String> subscriber = TestHelper.mockSubscriber();
885 final int NUM_MSG = 1034;
886 final AtomicInteger count = new AtomicInteger();
887
888 Flowable<String> origin = Flowable.unsafeCreate(new Publisher<String>() {
889
890 @Override
891 public void subscribe(Subscriber<? super String> subscriber) {
892 subscriber.onSubscribe(new BooleanSubscription());
893 for (int i = 0; i < NUM_MSG; i++) {
894 subscriber.onNext("msg:" + count.incrementAndGet());
895 }
896 subscriber.onComplete();
897 }
898 });
899
900 origin.retry()
901 .groupBy(new Function<String, String>() {
902 @Override
903 public String apply(String t1) {
904 return t1;
905 }
906 })
907 .flatMap(new Function<GroupedFlowable<String, String>, Flowable<String>>() {
908 @Override
909 public Flowable<String> apply(GroupedFlowable<String, String> t1) {
910 return t1.take(1);
911 }
912 })
913 .subscribe(new TestSubscriber<>(subscriber));
914
915 InOrder inOrder = inOrder(subscriber);
916 // should show 3 attempts
917 inOrder.verify(subscriber, times(NUM_MSG)).onNext(any(java.lang.String.class));
918 // // should have no errors
919 inOrder.verify(subscriber, never()).onError(any(Throwable.class));
920 // should have a single success
921 //inOrder.verify(observer, times(1)).onNext("onSuccessOnly");
922 // should have a single successful onComplete
923 inOrder.verify(subscriber, times(1)).onComplete();
924 inOrder.verifyNoMoreInteractions();
925 }
926
927 @SuppressWarnings({ "rawtypes", "unchecked" })
928 @Test
929 public void retryWhenDefaultScheduler() {
930 TestSubscriber<Integer> ts = TestSubscriber.create();
931
932 Flowable.just(1)
933 .concatWith(Flowable.<Integer>error(new TestException()))
934 .retryWhen((Function)new Function<Flowable, Flowable>() {
935 @Override
936 public Flowable apply(Flowable f) {
937 return f.take(2);
938 }
939 }).subscribe(ts);
940
941 ts.assertValues(1, 1);
942 ts.assertNoErrors();
943 ts.assertComplete();
944
945 }
946
947 @SuppressWarnings({ "rawtypes", "unchecked" })
948 @Test
949 public void retryWhenTrampolineScheduler() {
950 TestSubscriber<Integer> ts = TestSubscriber.create();
951
952 Flowable.just(1)
953 .concatWith(Flowable.<Integer>error(new TestException()))
954 .subscribeOn(Schedulers.trampoline())
955 .retryWhen((Function)new Function<Flowable, Flowable>() {
956 @Override
957 public Flowable apply(Flowable f) {
958 return f.take(2);
959 }
960 }).subscribe(ts);
961
962 ts.assertValues(1, 1);
963 ts.assertNoErrors();
964 ts.assertComplete();
965 }
966
967 @Test
968 public void retryPredicate() {
969 Flowable.just(1).concatWith(Flowable.<Integer>error(new TestException()))
970 .retry(new Predicate<Throwable>() {
971 @Override
972 public boolean test(Throwable v) throws Exception {
973 return true;
974 }
975 })
976 .take(5)
977 .test()
978 .assertResult(1, 1, 1, 1, 1);
979 }
980
981 @Test
982 public void retryLongPredicateInvalid() {
983 try {
984 Flowable.just(1).retry(-99, new Predicate<Throwable>() {
985 @Override
986 public boolean test(Throwable e) throws Exception {
987 return true;
988 }
989 });
990 fail("Should have thrown");
991 } catch (IllegalArgumentException ex) {
992 assertEquals("times >= 0 required but it was -99", ex.getMessage());
993 }
994 }
995
996 @Test
997 public void retryUntil() {
998 Flowable.just(1).concatWith(Flowable.<Integer>error(new TestException()))
999 .retryUntil(new BooleanSupplier() {
1000 @Override
1001 public boolean getAsBoolean() throws Exception {
1002 return false;
1003 }
1004 })
1005 .take(5)
1006 .test()
1007 .assertResult(1, 1, 1, 1, 1);
1008 }
1009
1010 @Test
1011 public void shouldDisposeInnerFlowable() {
1012 final PublishProcessor<Object> processor = PublishProcessor.create();
1013 final Disposable disposable = Flowable.error(new RuntimeException("Leak"))
1014 .retryWhen(new Function<Flowable<Throwable>, Flowable<Object>>() {
1015 @Override
1016 public Flowable<Object> apply(Flowable<Throwable> errors) throws Exception {
1017 return errors.switchMap(new Function<Throwable, Flowable<Object>>() {
1018 @Override
1019 public Flowable<Object> apply(Throwable ignore) throws Exception {
1020 return processor;
1021 }
1022 });
1023 }
1024 })
1025 .subscribe();
1026
1027 assertTrue(processor.hasSubscribers());
1028 disposable.dispose();
1029 assertFalse(processor.hasSubscribers());
1030 }
1031
1032 @Test
1033 public void noCancelPreviousRetry() {
1034 final AtomicInteger counter = new AtomicInteger();
1035
1036 final AtomicInteger times = new AtomicInteger();
1037
1038 Flowable<Integer> source = Flowable.defer(new Supplier<Flowable<Integer>>() {
1039 @Override
1040 public Flowable<Integer> get() throws Exception {
1041 if (times.getAndIncrement() < 4) {
1042 return Flowable.error(new TestException());
1043 }
1044 return Flowable.just(1);
1045 }
1046 })
1047 .doOnCancel(new Action() {
1048 @Override
1049 public void run() throws Exception {
1050 counter.getAndIncrement();
1051 }
1052 });
1053
1054 source.retry(5)
1055 .test()
1056 .assertResult(1);
1057
1058 assertEquals(0, counter.get());
1059 }
1060
1061 @Test
1062 public void noCancelPreviousRetryWhile() {
1063 final AtomicInteger counter = new AtomicInteger();
1064
1065 final AtomicInteger times = new AtomicInteger();
1066
1067 Flowable<Integer> source = Flowable.defer(new Supplier<Flowable<Integer>>() {
1068 @Override
1069 public Flowable<Integer> get() throws Exception {
1070 if (times.getAndIncrement() < 4) {
1071 return Flowable.error(new TestException());
1072 }
1073 return Flowable.just(1);
1074 }
1075 })
1076 .doOnCancel(new Action() {
1077 @Override
1078 public void run() throws Exception {
1079 counter.getAndIncrement();
1080 }
1081 });
1082
1083 source.retry(5, Functions.alwaysTrue())
1084 .test()
1085 .assertResult(1);
1086
1087 assertEquals(0, counter.get());
1088 }
1089
1090 @Test
1091 public void noCancelPreviousRetryWhile2() {
1092 final AtomicInteger counter = new AtomicInteger();
1093
1094 final AtomicInteger times = new AtomicInteger();
1095
1096 Flowable<Integer> source = Flowable.defer(new Supplier<Flowable<Integer>>() {
1097 @Override
1098 public Flowable<Integer> get() throws Exception {
1099 if (times.getAndIncrement() < 4) {
1100 return Flowable.error(new TestException());
1101 }
1102 return Flowable.just(1);
1103 }
1104 })
1105 .doOnCancel(new Action() {
1106 @Override
1107 public void run() throws Exception {
1108 counter.getAndIncrement();
1109 }
1110 });
1111
1112 source.retry(new BiPredicate<Integer, Throwable>() {
1113 @Override
1114 public boolean test(Integer a, Throwable b) throws Exception {
1115 return a < 5;
1116 }
1117 })
1118 .test()
1119 .assertResult(1);
1120
1121 assertEquals(0, counter.get());
1122 }
1123
1124 @Test
1125 public void noCancelPreviousRetryUntil() {
1126 final AtomicInteger counter = new AtomicInteger();
1127
1128 final AtomicInteger times = new AtomicInteger();
1129
1130 Flowable<Integer> source = Flowable.defer(new Supplier<Flowable<Integer>>() {
1131 @Override
1132 public Flowable<Integer> get() throws Exception {
1133 if (times.getAndIncrement() < 4) {
1134 return Flowable.error(new TestException());
1135 }
1136 return Flowable.just(1);
1137 }
1138 })
1139 .doOnCancel(new Action() {
1140 @Override
1141 public void run() throws Exception {
1142 counter.getAndIncrement();
1143 }
1144 });
1145
1146 source.retryUntil(new BooleanSupplier() {
1147 @Override
1148 public boolean getAsBoolean() throws Exception {
1149 return false;
1150 }
1151 })
1152 .test()
1153 .assertResult(1);
1154
1155 assertEquals(0, counter.get());
1156 }
1157
1158 @Test
1159 public void noCancelPreviousRepeatWhen() {
1160 final AtomicInteger counter = new AtomicInteger();
1161
1162 final AtomicInteger times = new AtomicInteger();
1163
1164 Flowable<Integer> source = Flowable.defer(new Supplier<Flowable<Integer>>() {
1165 @Override
1166 public Flowable<Integer> get() throws Exception {
1167 if (times.get() < 4) {
1168 return Flowable.error(new TestException());
1169 }
1170 return Flowable.just(1);
1171 }
1172 }).doOnCancel(new Action() {
1173 @Override
1174 public void run() throws Exception {
1175 counter.getAndIncrement();
1176 }
1177 });
1178
1179 source.retryWhen(new Function<Flowable<Throwable>, Flowable<?>>() {
1180 @Override
1181 public Flowable<?> apply(Flowable<Throwable> e) throws Exception {
1182 return e.takeWhile(new Predicate<Object>() {
1183 @Override
1184 public boolean test(Object v) throws Exception {
1185 return times.getAndIncrement() < 4;
1186 }
1187 });
1188 }
1189 })
1190 .test()
1191 .assertResult(1);
1192
1193 assertEquals(0, counter.get());
1194 }
1195
1196 @Test
1197 public void noCancelPreviousRepeatWhen2() {
1198 final AtomicInteger counter = new AtomicInteger();
1199
1200 final AtomicInteger times = new AtomicInteger();
1201
1202 Flowable<Integer> source = Flowable.<Integer>error(new TestException())
1203 .doOnCancel(new Action() {
1204 @Override
1205 public void run() throws Exception {
1206 counter.getAndIncrement();
1207 }
1208 });
1209
1210 source.retryWhen(new Function<Flowable<Throwable>, Flowable<?>>() {
1211 @Override
1212 public Flowable<?> apply(Flowable<Throwable> e) throws Exception {
1213 return e.takeWhile(new Predicate<Object>() {
1214 @Override
1215 public boolean test(Object v) throws Exception {
1216 return times.getAndIncrement() < 4;
1217 }
1218 });
1219 }
1220 })
1221 .test()
1222 .assertResult();
1223
1224 assertEquals(0, counter.get());
1225 }
1226
1227 @Test
1228 public void repeatFloodNoSubscriptionError() {
1229 List<Throwable> errors = TestHelper.trackPluginErrors();
1230
1231 final TestException error = new TestException();
1232
1233 try {
1234 final PublishProcessor<Integer> source = PublishProcessor.create();
1235 final PublishProcessor<Integer> signaller = PublishProcessor.create();
1236
1237 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
1238
1239 TestSubscriber<Integer> ts = source.take(1)
1240 .map(new Function<Integer, Integer>() {
1241 @Override
1242 public Integer apply(Integer v) throws Exception {
1243 throw error;
1244 }
1245 })
1246 .retryWhen(new Function<Flowable<Throwable>, Flowable<Integer>>() {
1247 @Override
1248 public Flowable<Integer> apply(Flowable<Throwable> v)
1249 throws Exception {
1250 return signaller;
1251 }
1252 }).test();
1253
1254 Runnable r1 = new Runnable() {
1255 @Override
1256 public void run() {
1257 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
1258 source.onNext(1);
1259 }
1260 }
1261 };
1262 Runnable r2 = new Runnable() {
1263 @Override
1264 public void run() {
1265 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
1266 signaller.offer(1);
1267 }
1268 }
1269 };
1270
1271 TestHelper.race(r1, r2);
1272
1273 ts.cancel();
1274 }
1275
1276 if (!errors.isEmpty()) {
1277 for (Throwable e : errors) {
1278 e.printStackTrace();
1279 }
1280 fail(errors + "");
1281 }
1282 } finally {
1283 RxJavaPlugins.reset();
1284 }
1285 }
1286 }