Search in sources :

Example 11 with TestHelper

use of io.reactivex.rxjava3.testsupport.TestHelper in project RxJava by ReactiveX.

the class CompletableMergeTest method mainErrorInnerErrorRace.

@Test
public void mainErrorInnerErrorRace() {
    for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
        List<Throwable> errors = TestHelper.trackPluginErrors();
        try {
            final PublishProcessor<Integer> pp1 = PublishProcessor.create();
            final PublishProcessor<Integer> pp2 = PublishProcessor.create();
            TestObserverEx<Void> to = Completable.merge(pp1.map(new Function<Integer, Completable>() {

                @Override
                public Completable apply(Integer v) throws Exception {
                    return pp2.ignoreElements();
                }
            })).to(TestHelper.<Void>testConsumer());
            pp1.onNext(1);
            final Throwable ex1 = new TestException();
            final Throwable ex2 = new TestException();
            Runnable r1 = new Runnable() {

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

                @Override
                public void run() {
                    pp2.onError(ex2);
                }
            };
            TestHelper.race(r1, r2);
            Throwable ex = to.errors().get(0);
            if (ex instanceof CompositeException) {
                to.assertSubscribed().assertNoValues().assertNotComplete();
                errors = TestHelper.compositeList(ex);
                TestHelper.assertError(errors, 0, TestException.class);
                TestHelper.assertError(errors, 1, TestException.class);
            } else {
                to.assertFailure(TestException.class);
                if (!errors.isEmpty()) {
                    TestHelper.assertUndeliverable(errors, 0, TestException.class);
                }
            }
        } finally {
            RxJavaPlugins.reset();
        }
    }
}
Also used : AtomicThrowable(io.reactivex.rxjava3.internal.util.AtomicThrowable) Test(org.junit.Test)

Example 12 with TestHelper

use of io.reactivex.rxjava3.testsupport.TestHelper in project RxJava by ReactiveX.

the class CompletableDoOnLifecycleTest method onSubscribeCrash.

@Test
public void onSubscribeCrash() throws Throwable {
    TestHelper.withErrorTracking(errors -> {
        @SuppressWarnings("unchecked") Consumer<? super Disposable> onSubscribe = mock(Consumer.class);
        Action onDispose = mock(Action.class);
        doThrow(new TestException("First")).when(onSubscribe).accept(any());
        Disposable bs = Disposable.empty();
        new Completable() {

            @Override
            protected void subscribeActual(CompletableObserver observer) {
                observer.onSubscribe(bs);
                observer.onError(new TestException("Second"));
                observer.onComplete();
            }
        }.doOnLifecycle(onSubscribe, onDispose).to(TestHelper.<Integer>testConsumer()).assertFailureAndMessage(TestException.class, "First");
        assertTrue(bs.isDisposed());
        TestHelper.assertUndeliverable(errors, 0, TestException.class, "Second");
        verify(onSubscribe).accept(any());
        verify(onDispose, never()).run();
    });
}
Also used : Disposable(io.reactivex.rxjava3.disposables.Disposable) TestException(io.reactivex.rxjava3.exceptions.TestException) Test(org.junit.Test)

Example 13 with TestHelper

use of io.reactivex.rxjava3.testsupport.TestHelper in project RxJava by ReactiveX.

the class FlowableDoOnLifecycleTest method onSubscribeCrash.

@Test
public void onSubscribeCrash() {
    List<Throwable> errors = TestHelper.trackPluginErrors();
    try {
        final BooleanSubscription bs = new BooleanSubscription();
        new Flowable<Integer>() {

            @Override
            protected void subscribeActual(Subscriber<? super Integer> s) {
                s.onSubscribe(bs);
                s.onError(new TestException("Second"));
                s.onComplete();
            }
        }.doOnSubscribe(new Consumer<Subscription>() {

            @Override
            public void accept(Subscription s) throws Exception {
                throw new TestException("First");
            }
        }).to(TestHelper.<Integer>testConsumer()).assertFailureAndMessage(TestException.class, "First");
        assertTrue(bs.isCancelled());
        TestHelper.assertUndeliverable(errors, 0, TestException.class, "Second");
    } finally {
        RxJavaPlugins.reset();
    }
}
Also used : BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) TestException(io.reactivex.rxjava3.exceptions.TestException) BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) TestException(io.reactivex.rxjava3.exceptions.TestException) Test(org.junit.Test)

Example 14 with TestHelper

use of io.reactivex.rxjava3.testsupport.TestHelper in project RxJava by ReactiveX.

the class ObservableTimeoutTests method onNextOnTimeoutRaceFallback.

@Test
public void onNextOnTimeoutRaceFallback() {
    for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
        final TestScheduler sch = new TestScheduler();
        final PublishSubject<Integer> ps = PublishSubject.create();
        TestObserverEx<Integer> to = ps.timeout(1, TimeUnit.SECONDS, sch, Observable.just(2)).to(TestHelper.<Integer>testConsumer());
        Runnable r1 = new Runnable() {

            @Override
            public void run() {
                ps.onNext(1);
            }
        };
        Runnable r2 = new Runnable() {

            @Override
            public void run() {
                sch.advanceTimeBy(1, TimeUnit.SECONDS);
            }
        };
        TestHelper.race(r1, r2);
        if (to.isTerminated()) {
            int c = to.values().size();
            if (c == 1) {
                int v = to.values().get(0);
                assertTrue("" + v, v == 1 || v == 2);
            } else {
                to.assertResult(1, 2);
            }
        } else {
            to.assertValuesOnly(1);
        }
    }
}
Also used : TestScheduler(io.reactivex.rxjava3.schedulers.TestScheduler)

Example 15 with TestHelper

use of io.reactivex.rxjava3.testsupport.TestHelper in project RxJava by ReactiveX.

the class ObservableTimeoutTests method onNextOnTimeoutRace.

@Test
public void onNextOnTimeoutRace() {
    for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
        final TestScheduler sch = new TestScheduler();
        final PublishSubject<Integer> ps = PublishSubject.create();
        TestObserverEx<Integer> to = ps.timeout(1, TimeUnit.SECONDS, sch).to(TestHelper.<Integer>testConsumer());
        Runnable r1 = new Runnable() {

            @Override
            public void run() {
                ps.onNext(1);
            }
        };
        Runnable r2 = new Runnable() {

            @Override
            public void run() {
                sch.advanceTimeBy(1, TimeUnit.SECONDS);
            }
        };
        TestHelper.race(r1, r2);
        if (to.values().size() != 0) {
            if (to.errors().size() != 0) {
                to.assertFailure(TimeoutException.class, 1);
                to.assertErrorMessage(timeoutMessage(1, TimeUnit.SECONDS));
            } else {
                to.assertValuesOnly(1);
            }
        } else {
            to.assertFailure(TimeoutException.class);
            to.assertErrorMessage(timeoutMessage(1, TimeUnit.SECONDS));
        }
    }
}
Also used : TestScheduler(io.reactivex.rxjava3.schedulers.TestScheduler)

Aggregations

Test (org.junit.Test)21 TestException (io.reactivex.rxjava3.exceptions.TestException)19 BooleanSubscription (io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription)9 TestSubscriber (io.reactivex.rxjava3.subscribers.TestSubscriber)6 Disposable (io.reactivex.rxjava3.disposables.Disposable)3 ConnectableFlowable (io.reactivex.rxjava3.flowables.ConnectableFlowable)3 TestObserver (io.reactivex.rxjava3.observers.TestObserver)3 Observable (io.reactivex.rxjava3.core.Observable)2 Observer (io.reactivex.rxjava3.core.Observer)2 AtomicThrowable (io.reactivex.rxjava3.internal.util.AtomicThrowable)2 ConnectableObservable (io.reactivex.rxjava3.observables.ConnectableObservable)2 TestScheduler (io.reactivex.rxjava3.schedulers.TestScheduler)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Action (io.reactivex.rxjava3.functions.Action)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Subscriber (org.reactivestreams.Subscriber)1