use of io.reactivex.rxjava3.functions.Function in project RxJava by ReactiveX.
the class FlowableOnErrorReturnTest method normalBackpressure.
@Test
public void normalBackpressure() {
TestSubscriber<Integer> ts = TestSubscriber.create(0);
PublishProcessor<Integer> pp = PublishProcessor.create();
pp.onErrorReturn(new Function<Throwable, Integer>() {
@Override
public Integer apply(Throwable e) {
return 3;
}
}).subscribe(ts);
ts.request(2);
pp.onNext(1);
pp.onNext(2);
pp.onError(new TestException("Forced failure"));
ts.assertValues(1, 2);
ts.assertNoErrors();
ts.assertNotComplete();
ts.request(2);
ts.assertValues(1, 2, 3);
ts.assertNoErrors();
ts.assertComplete();
}
use of io.reactivex.rxjava3.functions.Function 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.functions.Function 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.functions.Function 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();
}
use of io.reactivex.rxjava3.functions.Function in project RxJava by ReactiveX.
the class FlowableGroupByTest method firstGroupsCompleteAndParentSlowToThenEmitFinalGroupsAndThenComplete.
@Test
public void firstGroupsCompleteAndParentSlowToThenEmitFinalGroupsAndThenComplete() throws InterruptedException {
// there are two groups to first complete
final CountDownLatch first = new CountDownLatch(2);
final ArrayList<String> results = new ArrayList<>();
Flowable.unsafeCreate(new Publisher<Integer>() {
@Override
public void subscribe(Subscriber<? super Integer> sub) {
sub.onSubscribe(new BooleanSubscription());
sub.onNext(1);
sub.onNext(2);
sub.onNext(1);
sub.onNext(2);
try {
first.await();
} catch (InterruptedException e) {
sub.onError(e);
return;
}
sub.onNext(3);
sub.onNext(3);
sub.onComplete();
}
}).groupBy(new Function<Integer, Integer>() {
@Override
public Integer apply(Integer t) {
return t;
}
}).flatMap(new Function<GroupedFlowable<Integer, Integer>, Flowable<String>>() {
@Override
public Flowable<String> apply(final GroupedFlowable<Integer, Integer> group) {
if (group.getKey() < 3) {
return group.map(new Function<Integer, String>() {
@Override
public String apply(Integer t1) {
return "first groups: " + t1;
}
}).take(2).doOnComplete(new Action() {
@Override
public void run() {
first.countDown();
}
});
} else {
return group.map(new Function<Integer, String>() {
@Override
public String apply(Integer t1) {
return "last group: " + t1;
}
});
}
}
}).blockingForEach(new Consumer<String>() {
@Override
public void accept(String s) {
results.add(s);
}
});
System.out.println("Results: " + results);
assertEquals(6, results.size());
}
Aggregations