use of io.reactivex.rxjava3.core.Flowable in project RxJava by ReactiveX.
the class FlowableRetryTest method singleSubscriptionOnFirst.
@Test
public void singleSubscriptionOnFirst() throws Exception {
final AtomicInteger inc = new AtomicInteger(0);
Publisher<Integer> onSubscribe = new Publisher<Integer>() {
@Override
public void subscribe(Subscriber<? super Integer> subscriber) {
subscriber.onSubscribe(new BooleanSubscription());
final int emit = inc.incrementAndGet();
subscriber.onNext(emit);
subscriber.onComplete();
}
};
int first = Flowable.unsafeCreate(onSubscribe).retryWhen(new Function<Flowable<? extends Throwable>, Flowable<Object>>() {
@Override
public Flowable<Object> apply(Flowable<? extends Throwable> attempt) {
return attempt.zipWith(Flowable.just(1), new BiFunction<Throwable, Integer, Object>() {
@Override
public Object apply(Throwable o, Integer integer) {
return 0;
}
});
}
}).blockingFirst();
assertEquals("Observer did not receive the expected output", 1, first);
assertEquals("Subscribe was not called once", 1, inc.get());
}
use of io.reactivex.rxjava3.core.Flowable in project RxJava by ReactiveX.
the class FlowableRetryTest method repeatFloodNoSubscriptionError.
@Test
public void repeatFloodNoSubscriptionError() {
List<Throwable> errors = TestHelper.trackPluginErrors();
final TestException error = new TestException();
try {
final PublishProcessor<Integer> source = PublishProcessor.create();
final PublishProcessor<Integer> signaller = PublishProcessor.create();
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
TestSubscriber<Integer> ts = source.take(1).map(new Function<Integer, Integer>() {
@Override
public Integer apply(Integer v) throws Exception {
throw error;
}
}).retryWhen(new Function<Flowable<Throwable>, Flowable<Integer>>() {
@Override
public Flowable<Integer> apply(Flowable<Throwable> v) throws Exception {
return signaller;
}
}).test();
Runnable r1 = new Runnable() {
@Override
public void run() {
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
source.onNext(1);
}
}
};
Runnable r2 = new Runnable() {
@Override
public void run() {
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
signaller.offer(1);
}
}
};
TestHelper.race(r1, r2);
ts.cancel();
}
if (!errors.isEmpty()) {
for (Throwable e : errors) {
e.printStackTrace();
}
fail(errors + "");
}
} finally {
RxJavaPlugins.reset();
}
}
use of io.reactivex.rxjava3.core.Flowable in project RxJava by ReactiveX.
the class FlowableRetryTest method shouldDisposeInnerFlowable.
@Test
public void shouldDisposeInnerFlowable() {
final PublishProcessor<Object> processor = PublishProcessor.create();
final Disposable disposable = Flowable.error(new RuntimeException("Leak")).retryWhen(new Function<Flowable<Throwable>, Flowable<Object>>() {
@Override
public Flowable<Object> apply(Flowable<Throwable> errors) throws Exception {
return errors.switchMap(new Function<Throwable, Flowable<Object>>() {
@Override
public Flowable<Object> apply(Throwable ignore) throws Exception {
return processor;
}
});
}
}).subscribe();
assertTrue(processor.hasSubscribers());
disposable.dispose();
assertFalse(processor.hasSubscribers());
}
use of io.reactivex.rxjava3.core.Flowable in project RxJava by ReactiveX.
the class FlowableRetryTest method noCancelPreviousRetryWhile.
@Test
public void noCancelPreviousRetryWhile() {
final AtomicInteger counter = new AtomicInteger();
final AtomicInteger times = new AtomicInteger();
Flowable<Integer> source = Flowable.defer(new Supplier<Flowable<Integer>>() {
@Override
public Flowable<Integer> get() throws Exception {
if (times.getAndIncrement() < 4) {
return Flowable.error(new TestException());
}
return Flowable.just(1);
}
}).doOnCancel(new Action() {
@Override
public void run() throws Exception {
counter.getAndIncrement();
}
});
source.retry(5, Functions.alwaysTrue()).test().assertResult(1);
assertEquals(0, counter.get());
}
use of io.reactivex.rxjava3.core.Flowable in project RxJava by ReactiveX.
the class FlowableRetryTest method retryWhenTrampolineScheduler.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void retryWhenTrampolineScheduler() {
TestSubscriber<Integer> ts = TestSubscriber.create();
Flowable.just(1).concatWith(Flowable.<Integer>error(new TestException())).subscribeOn(Schedulers.trampoline()).retryWhen((Function) new Function<Flowable, Flowable>() {
@Override
public Flowable apply(Flowable f) {
return f.take(2);
}
}).subscribe(ts);
ts.assertValues(1, 1);
ts.assertNoErrors();
ts.assertComplete();
}
Aggregations