Search in sources :

Example 6 with Function

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();
    }
}
Also used : Function(io.reactivex.functions.Function) TestException(io.reactivex.exceptions.TestException)

Example 7 with Function

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));
}
Also used : Function(io.reactivex.functions.Function) InOrder(org.mockito.InOrder)

Example 8 with Function

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();
}
Also used : Function(io.reactivex.functions.Function) TestException(io.reactivex.exceptions.TestException)

Example 9 with Function

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());
}
Also used : Function(io.reactivex.functions.Function) AtomicReference(java.util.concurrent.atomic.AtomicReference) BooleanSubscription(io.reactivex.internal.subscriptions.BooleanSubscription)

Example 10 with Function

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();
}
Also used : Function(io.reactivex.functions.Function) BooleanSubscription(io.reactivex.internal.subscriptions.BooleanSubscription)

Aggregations

Function (io.reactivex.functions.Function)60 Test (org.junit.Test)27 TestException (io.reactivex.exceptions.TestException)24 InOrder (org.mockito.InOrder)21 Observable (io.reactivex.Observable)10 Disposable (io.reactivex.disposables.Disposable)7 NonNull (io.reactivex.annotations.NonNull)5 CompositeDisposable (io.reactivex.disposables.CompositeDisposable)5 BuildDetails (com.khmelenko.lab.varis.network.response.BuildDetails)3 BooleanSubscription (io.reactivex.internal.subscriptions.BooleanSubscription)3 Person (io.requery.test.model.Person)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 Phone (io.requery.test.model.Phone)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 RequestBody (okhttp3.RequestBody)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Subscription (org.reactivestreams.Subscription)2