Search in sources :

Example 11 with Function

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

the class FlowableOnErrorReturnTest method normalBackpressure.

@Test
public void normalBackpressure() {
    TestSubscriber<Integer> ts = TestSubscriber.create(0);
    PublishProcessor<Integer> ps = PublishProcessor.create();
    ps.onErrorReturn(new Function<Throwable, Integer>() {

        @Override
        public Integer apply(Throwable e) {
            return 3;
        }
    }).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);
    ts.assertNoErrors();
    ts.assertComplete();
}
Also used : Function(io.reactivex.functions.Function) TestException(io.reactivex.exceptions.TestException) Test(org.junit.Test)

Example 12 with Function

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

the class FlowableTimeoutWithSelectorTest method testTimeoutSelectorWithTimeoutAndOnNextRaceCondition.

@Test
public void testTimeoutSelectorWithTimeoutAndOnNextRaceCondition() throws InterruptedException {
    // Thread 1                                    Thread 2
    //
    // observer.onNext(1)
    // start timeout
    // unsubscribe timeout in thread 2          start to do some long-time work in "unsubscribe"
    // observer.onNext(2)
    // timeout.onNext(1)
    //                                          "unsubscribe" done
    //
    //
    // In the above case, the timeout operator should ignore "timeout.onNext(1)"
    // since "observer" has already seen 2.
    final CountDownLatch observerReceivedTwo = new CountDownLatch(1);
    final CountDownLatch timeoutEmittedOne = new CountDownLatch(1);
    final CountDownLatch observerCompleted = new CountDownLatch(1);
    final CountDownLatch enteredTimeoutOne = new CountDownLatch(1);
    final AtomicBoolean latchTimeout = new AtomicBoolean(false);
    final Function<Integer, Flowable<Integer>> timeoutFunc = new Function<Integer, Flowable<Integer>>() {

        @Override
        public Flowable<Integer> apply(Integer t1) {
            if (t1 == 1) {
                // Force "unsubscribe" run on another thread
                return Flowable.unsafeCreate(new Publisher<Integer>() {

                    @Override
                    public void subscribe(Subscriber<? super Integer> subscriber) {
                        subscriber.onSubscribe(new BooleanSubscription());
                        enteredTimeoutOne.countDown();
                        // force the timeout message be sent after observer.onNext(2)
                        while (true) {
                            try {
                                if (!observerReceivedTwo.await(30, TimeUnit.SECONDS)) {
                                    // CountDownLatch timeout
                                    // There should be something wrong
                                    latchTimeout.set(true);
                                }
                                break;
                            } catch (InterruptedException e) {
                            // Since we just want to emulate a busy method,
                            // we ignore the interrupt signal from Scheduler.
                            }
                        }
                        subscriber.onNext(1);
                        timeoutEmittedOne.countDown();
                    }
                }).subscribeOn(Schedulers.newThread());
            } else {
                return PublishProcessor.create();
            }
        }
    };
    final Subscriber<Integer> o = TestHelper.mockSubscriber();
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            observerReceivedTwo.countDown();
            return null;
        }
    }).when(o).onNext(2);
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            observerCompleted.countDown();
            return null;
        }
    }).when(o).onComplete();
    final TestSubscriber<Integer> ts = new TestSubscriber<Integer>(o);
    new Thread(new Runnable() {

        @Override
        public void run() {
            PublishProcessor<Integer> source = PublishProcessor.create();
            source.timeout(timeoutFunc, Flowable.just(3)).subscribe(ts);
            // start timeout
            source.onNext(1);
            try {
                if (!enteredTimeoutOne.await(30, TimeUnit.SECONDS)) {
                    latchTimeout.set(true);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            // disable timeout
            source.onNext(2);
            try {
                if (!timeoutEmittedOne.await(30, TimeUnit.SECONDS)) {
                    latchTimeout.set(true);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            source.onComplete();
        }
    }).start();
    if (!observerCompleted.await(30, TimeUnit.SECONDS)) {
        latchTimeout.set(true);
    }
    assertFalse("CoundDownLatch timeout", latchTimeout.get());
    InOrder inOrder = inOrder(o);
    inOrder.verify(o).onSubscribe((Subscription) notNull());
    inOrder.verify(o).onNext(1);
    inOrder.verify(o).onNext(2);
    inOrder.verify(o, never()).onNext(3);
    inOrder.verify(o).onComplete();
    inOrder.verifyNoMoreInteractions();
}
Also used : BooleanSubscription(io.reactivex.internal.subscriptions.BooleanSubscription) InOrder(org.mockito.InOrder) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Function(io.reactivex.functions.Function) TestSubscriber(io.reactivex.subscribers.TestSubscriber) InvocationOnMock(org.mockito.invocation.InvocationOnMock) TestSubscriber(io.reactivex.subscribers.TestSubscriber) Test(org.junit.Test)

Example 13 with Function

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

the class FlowableTimeoutWithSelectorTest method testTimeoutSelectorSubsequentFlowableThrows.

@Test
public void testTimeoutSelectorSubsequentFlowableThrows() {
    PublishProcessor<Integer> source = PublishProcessor.create();
    final PublishProcessor<Integer> timeout = PublishProcessor.create();
    Function<Integer, Flowable<Integer>> timeoutFunc = new Function<Integer, Flowable<Integer>>() {

        @Override
        public Flowable<Integer> apply(Integer t1) {
            return Flowable.<Integer>error(new TestException());
        }
    };
    Flowable<Integer> other = Flowable.fromIterable(Arrays.asList(100));
    Subscriber<Object> o = TestHelper.mockSubscriber();
    InOrder inOrder = inOrder(o);
    source.timeout(timeout, timeoutFunc, other).subscribe(o);
    source.onNext(1);
    inOrder.verify(o).onNext(1);
    inOrder.verify(o).onError(any(TestException.class));
    verify(o, never()).onComplete();
}
Also used : Function(io.reactivex.functions.Function) InOrder(org.mockito.InOrder) TestException(io.reactivex.exceptions.TestException) Test(org.junit.Test)

Example 14 with Function

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

the class FlowableTimeoutWithSelectorTest method testTimeoutSelectorSubsequentThrows.

@Test
public void testTimeoutSelectorSubsequentThrows() {
    PublishProcessor<Integer> source = PublishProcessor.create();
    final PublishProcessor<Integer> timeout = PublishProcessor.create();
    Function<Integer, Flowable<Integer>> timeoutFunc = new Function<Integer, Flowable<Integer>>() {

        @Override
        public Flowable<Integer> apply(Integer t1) {
            throw new TestException();
        }
    };
    Flowable<Integer> other = Flowable.fromIterable(Arrays.asList(100));
    Subscriber<Object> o = TestHelper.mockSubscriber();
    InOrder inOrder = inOrder(o);
    source.timeout(timeout, timeoutFunc, other).subscribe(o);
    source.onNext(1);
    inOrder.verify(o).onNext(1);
    inOrder.verify(o).onError(any(TestException.class));
    verify(o, never()).onComplete();
}
Also used : Function(io.reactivex.functions.Function) InOrder(org.mockito.InOrder) TestException(io.reactivex.exceptions.TestException) Test(org.junit.Test)

Example 15 with Function

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

the class ObservableConcatMapTest method onErrorRace.

@Test
public void onErrorRace() {
    for (int i = 0; i < 500; i++) {
        List<Throwable> errors = TestHelper.trackPluginErrors();
        try {
            final PublishSubject<Integer> ps1 = PublishSubject.create();
            final PublishSubject<Integer> ps2 = PublishSubject.create();
            TestObserver<Integer> to = ps1.concatMap(new Function<Integer, ObservableSource<Integer>>() {

                @Override
                public ObservableSource<Integer> apply(Integer v) throws Exception {
                    return ps2;
                }
            }).test();
            final TestException ex1 = new TestException();
            final TestException ex2 = new TestException();
            Runnable r1 = new Runnable() {

                @Override
                public void run() {
                    ps1.onError(ex1);
                }
            };
            Runnable r2 = new Runnable() {

                @Override
                public void run() {
                    ps2.onError(ex2);
                }
            };
            TestHelper.race(r1, r2, Schedulers.single());
            to.assertFailure(TestException.class);
            if (!errors.isEmpty()) {
                TestHelper.assertError(errors, 0, TestException.class);
            }
        } finally {
            RxJavaPlugins.reset();
        }
    }
}
Also used : Function(io.reactivex.functions.Function) TestException(io.reactivex.exceptions.TestException) Test(org.junit.Test)

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