Search in sources :

Example 36 with Function

use of io.reactivex.rxjava3.functions.Function in project RxJava by ReactiveX.

the class FlowableMapTest method mapFilterMapperCrashFused.

@Test
public void mapFilterMapperCrashFused() {
    TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.ANY);
    Flowable.range(1, 2).hide().map(new Function<Integer, Integer>() {

        @Override
        public Integer apply(Integer v) throws Exception {
            throw new TestException();
        }
    }).filter(new Predicate<Integer>() {

        @Override
        public boolean test(Integer v) throws Exception {
            return true;
        }
    }).subscribe(ts);
    ts.assertFuseable().assertFusionMode(QueueFuseable.NONE).assertFailure(TestException.class);
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) TestException(io.reactivex.rxjava3.exceptions.TestException) IOException(java.io.IOException)

Example 37 with Function

use of io.reactivex.rxjava3.functions.Function in project RxJava by ReactiveX.

the class FlowableMapTest method sourceIgnoresCancel.

@Test
public void sourceIgnoresCancel() {
    List<Throwable> errors = TestHelper.trackPluginErrors();
    try {
        Flowable.fromPublisher(new Publisher<Integer>() {

            @Override
            public void subscribe(Subscriber<? super Integer> s) {
                s.onSubscribe(new BooleanSubscription());
                s.onNext(1);
                s.onNext(2);
                s.onError(new IOException());
                s.onComplete();
            }
        }).map(new Function<Integer, Object>() {

            @Override
            public Object apply(Integer v) throws Exception {
                throw new TestException();
            }
        }).test().assertFailure(TestException.class);
        TestHelper.assertUndeliverable(errors, 0, IOException.class);
    } finally {
        RxJavaPlugins.reset();
    }
}
Also used : BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) TestException(io.reactivex.rxjava3.exceptions.TestException) ConditionalSubscriber(io.reactivex.rxjava3.operators.ConditionalSubscriber) TestSubscriber(io.reactivex.rxjava3.subscribers.TestSubscriber) IOException(java.io.IOException) TestException(io.reactivex.rxjava3.exceptions.TestException) IOException(java.io.IOException)

Example 38 with Function

use of io.reactivex.rxjava3.functions.Function in project RxJava by ReactiveX.

the class FlowableOnErrorResumeNextViaFunctionTest method resumeNextWithAsyncExecution.

@Test
public void resumeNextWithAsyncExecution() {
    final AtomicReference<Throwable> receivedException = new AtomicReference<>();
    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> flowable = Flowable.unsafeCreate(w).onErrorResumeNext(resume);
    Subscriber<String> subscriber = TestHelper.mockSubscriber();
    flowable.subscribe(subscriber);
    try {
        w.t.join();
    } catch (InterruptedException e) {
        fail(e.getMessage());
    }
    verify(subscriber, Mockito.never()).onError(any(Throwable.class));
    verify(subscriber, times(1)).onComplete();
    verify(subscriber, times(1)).onNext("one");
    verify(subscriber, Mockito.never()).onNext("two");
    verify(subscriber, Mockito.never()).onNext("three");
    verify(subscriber, times(1)).onNext("twoResume");
    verify(subscriber, times(1)).onNext("threeResume");
    assertNotNull(receivedException.get());
}
Also used : Function(io.reactivex.rxjava3.functions.Function) AtomicReference(java.util.concurrent.atomic.AtomicReference) BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) Test(org.junit.Test)

Example 39 with Function

use of io.reactivex.rxjava3.functions.Function in project RxJava by ReactiveX.

the class ObservableOnErrorResumeNextTest method functionThrowsError.

/**
 * Test that when a function throws an exception this is propagated through onError.
 */
@Test
public void functionThrowsError() {
    Subscription s = mock(Subscription.class);
    TestObservable w = new TestObservable(s, "one");
    Function<Throwable, Observable<String>> resume = new Function<Throwable, Observable<String>>() {

        @Override
        public Observable<String> apply(Throwable t1) {
            throw new RuntimeException("exception from function");
        }
    };
    Observable<String> o = Observable.unsafeCreate(w).onErrorResumeNext(resume);
    Observer<String> observer = TestHelper.mockObserver();
    o.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.rxjava3.functions.Function) Subscription(org.reactivestreams.Subscription) Test(org.junit.Test)

Example 40 with Function

use of io.reactivex.rxjava3.functions.Function in project RxJava by ReactiveX.

the class ObservableOnErrorResumeNextTest method resumeNextWithAsyncExecution.

@Test
public void resumeNextWithAsyncExecution() {
    final AtomicReference<Throwable> receivedException = new AtomicReference<>();
    Subscription s = mock(Subscription.class);
    TestObservable w = new TestObservable(s, "one");
    Function<Throwable, Observable<String>> resume = new Function<Throwable, Observable<String>>() {

        @Override
        public Observable<String> apply(Throwable t1) {
            receivedException.set(t1);
            return Observable.just("twoResume", "threeResume");
        }
    };
    Observable<String> o = Observable.unsafeCreate(w).onErrorResumeNext(resume);
    Observer<String> observer = TestHelper.mockObserver();
    o.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.rxjava3.functions.Function) AtomicReference(java.util.concurrent.atomic.AtomicReference) Subscription(org.reactivestreams.Subscription) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)97 TestException (io.reactivex.rxjava3.exceptions.TestException)78 Observable (io.reactivex.rxjava3.core.Observable)41 InOrder (org.mockito.InOrder)36 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)21 Function (io.reactivex.rxjava3.functions.Function)20 BooleanSubscription (io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription)18 AtomicReference (java.util.concurrent.atomic.AtomicReference)14 IOException (java.io.IOException)13 Disposable (io.reactivex.rxjava3.disposables.Disposable)10 Worker (io.reactivex.rxjava3.core.Scheduler.Worker)9 TestObserver (io.reactivex.rxjava3.observers.TestObserver)9 TestSubscriber (io.reactivex.rxjava3.subscribers.TestSubscriber)9 GroupedFlowable (io.reactivex.rxjava3.flowables.GroupedFlowable)8 CountingRunnable (io.reactivex.rxjava3.android.testutil.CountingRunnable)7 Observer (io.reactivex.rxjava3.core.Observer)7 Function (org.apache.cassandra.cql3.functions.Function)7 ImmediateThinScheduler (io.reactivex.rxjava3.internal.schedulers.ImmediateThinScheduler)6 TestHelper (io.reactivex.rxjava3.testsupport.TestHelper)6 EmptyScheduler (io.reactivex.rxjava3.android.testutil.EmptyScheduler)5