use of io.reactivex.functions.Function in project RxJava by ReactiveX.
the class XFlatMapTest method flowableCompletable.
@Test
public void flowableCompletable() throws Exception {
List<Throwable> errors = TestHelper.trackPluginErrors();
try {
TestObserver<Void> ts = Flowable.just(1).subscribeOn(Schedulers.io()).flatMapCompletable(new Function<Integer, Completable>() {
@Override
public Completable apply(Integer v) throws Exception {
sleep();
return Completable.error(new TestException());
}
}).test();
cb.await();
Thread.sleep(50);
ts.cancel();
Thread.sleep(SLEEP_AFTER_CANCEL);
ts.assertEmpty();
assertTrue(errors.toString(), errors.isEmpty());
} finally {
RxJavaPlugins.reset();
}
}
use of io.reactivex.functions.Function in project RxJava by ReactiveX.
the class FlowableDebounceTest method debounceSelectorNormal1.
@Test
public void debounceSelectorNormal1() {
PublishProcessor<Integer> source = PublishProcessor.create();
final PublishProcessor<Integer> debouncer = PublishProcessor.create();
Function<Integer, Flowable<Integer>> debounceSel = new Function<Integer, Flowable<Integer>>() {
@Override
public Flowable<Integer> apply(Integer t1) {
return debouncer;
}
};
Subscriber<Object> o = TestHelper.mockSubscriber();
InOrder inOrder = inOrder(o);
source.debounce(debounceSel).subscribe(o);
source.onNext(1);
debouncer.onNext(1);
source.onNext(2);
source.onNext(3);
source.onNext(4);
debouncer.onNext(2);
source.onNext(5);
source.onComplete();
inOrder.verify(o).onNext(1);
inOrder.verify(o).onNext(4);
inOrder.verify(o).onNext(5);
inOrder.verify(o).onComplete();
verify(o, never()).onError(any(Throwable.class));
}
use of io.reactivex.functions.Function in project RxJava by ReactiveX.
the class FlowableOnErrorResumeNextViaFunctionTest method normalBackpressure.
@Test
public void normalBackpressure() {
TestSubscriber<Integer> ts = TestSubscriber.create(0);
PublishProcessor<Integer> ps = PublishProcessor.create();
ps.onErrorResumeNext(new Function<Throwable, Flowable<Integer>>() {
@Override
public Flowable<Integer> apply(Throwable v) {
return Flowable.range(3, 2);
}
}).subscribe(ts);
ts.request(2);
ps.onNext(1);
ps.onNext(2);
ps.onError(new TestException("Forced failure"));
ts.assertValues(1, 2);
ts.assertNoErrors();
ts.assertNotComplete();
ts.request(2);
ts.assertValues(1, 2, 3, 4);
ts.assertNoErrors();
ts.assertComplete();
}
use of io.reactivex.functions.Function in project RxJava by ReactiveX.
the class FlowableOnErrorResumeNextViaFunctionTest method testResumeNextWithAsyncExecution.
@Test
public void testResumeNextWithAsyncExecution() {
final AtomicReference<Throwable> receivedException = new AtomicReference<Throwable>();
Subscription s = mock(Subscription.class);
TestFlowable w = new TestFlowable(s, "one");
Function<Throwable, Flowable<String>> resume = new Function<Throwable, Flowable<String>>() {
@Override
public Flowable<String> apply(Throwable t1) {
receivedException.set(t1);
return Flowable.just("twoResume", "threeResume");
}
};
Flowable<String> observable = Flowable.unsafeCreate(w).onErrorResumeNext(resume);
Subscriber<String> observer = TestHelper.mockSubscriber();
observable.subscribe(observer);
try {
w.t.join();
} catch (InterruptedException e) {
fail(e.getMessage());
}
verify(observer, Mockito.never()).onError(any(Throwable.class));
verify(observer, times(1)).onComplete();
verify(observer, times(1)).onNext("one");
verify(observer, Mockito.never()).onNext("two");
verify(observer, Mockito.never()).onNext("three");
verify(observer, times(1)).onNext("twoResume");
verify(observer, times(1)).onNext("threeResume");
assertNotNull(receivedException.get());
}
use of io.reactivex.functions.Function in project RxJava by ReactiveX.
the class FlowableOnErrorResumeNextViaFunctionTest method testFunctionThrowsError.
/**
* Test that when a function throws an exception this is propagated through onError.
*/
@Test
public void testFunctionThrowsError() {
Subscription s = mock(Subscription.class);
TestFlowable w = new TestFlowable(s, "one");
Function<Throwable, Flowable<String>> resume = new Function<Throwable, Flowable<String>>() {
@Override
public Flowable<String> apply(Throwable t1) {
throw new RuntimeException("exception from function");
}
};
Flowable<String> observable = Flowable.unsafeCreate(w).onErrorResumeNext(resume);
@SuppressWarnings("unchecked") DefaultSubscriber<String> observer = mock(DefaultSubscriber.class);
observable.subscribe(observer);
try {
w.t.join();
} catch (InterruptedException e) {
fail(e.getMessage());
}
// we should get the "one" value before the error
verify(observer, times(1)).onNext("one");
// we should have received an onError call on the Observer since the resume function threw an exception
verify(observer, times(1)).onError(any(Throwable.class));
verify(observer, times(0)).onComplete();
}
Aggregations