Search in sources :

Example 6 with Action

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

the class MaybeDoFinallyTest method actionThrowsConditional.

@Test
public void actionThrowsConditional() {
    List<Throwable> errors = TestHelper.trackPluginErrors();
    try {
        Maybe.just(1).doFinally(new Action() {

            @Override
            public void run() throws Exception {
                throw new TestException();
            }
        }).filter(Functions.alwaysTrue()).test().assertResult(1).dispose();
        TestHelper.assertUndeliverable(errors, 0, TestException.class);
    } finally {
        RxJavaPlugins.reset();
    }
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) TestException(io.reactivex.rxjava3.exceptions.TestException) Test(org.junit.Test)

Example 7 with Action

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

the class MaybeBlockingSubscribeTest method threeArgEmptyFails.

@Test
public void threeArgEmptyFails() throws Throwable {
    TestHelper.withErrorTracking(errors -> {
        @SuppressWarnings("unchecked") Consumer<Integer> success = mock(Consumer.class);
        @SuppressWarnings("unchecked") Consumer<? super Throwable> consumer = mock(Consumer.class);
        Action action = mock(Action.class);
        doThrow(new TestException()).when(action).run();
        Maybe.<Integer>empty().delay(50, TimeUnit.MILLISECONDS, Schedulers.computation()).blockingSubscribe(success, consumer, action);
        TestHelper.assertUndeliverable(errors, 0, TestException.class);
        verify(success, never()).accept(any());
        verify(consumer, never()).accept(any());
        verify(action).run();
    });
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) Test(org.junit.Test)

Example 8 with Action

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

the class SingleDoOnLifecycleTest method onDisposeCrash.

@Test
public void onDisposeCrash() throws Throwable {
    TestHelper.withErrorTracking(errors -> {
        @SuppressWarnings("unchecked") Consumer<? super Disposable> onSubscribe = mock(Consumer.class);
        Action onDispose = mock(Action.class);
        doThrow(new TestException("First")).when(onDispose).run();
        SingleSubject<Integer> ss = SingleSubject.create();
        TestObserver<Integer> to = ss.doOnLifecycle(onSubscribe, onDispose).test();
        assertTrue(ss.hasObservers());
        to.dispose();
        assertFalse(ss.hasObservers());
        TestHelper.assertUndeliverable(errors, 0, TestException.class, "First");
        verify(onSubscribe).accept(any());
        verify(onDispose).run();
    });
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) Test(org.junit.Test)

Example 9 with Action

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

the class SingleDoOnTerminateTest method doOnTerminateErrorCrash.

@Test
public void doOnTerminateErrorCrash() {
    TestObserverEx<Object> to = Single.error(new TestException("Outer")).doOnTerminate(new Action() {

        @Override
        public void run() {
            throw new TestException("Inner");
        }
    }).to(TestHelper.testConsumer()).assertFailure(CompositeException.class);
    List<Throwable> errors = TestHelper.compositeList(to.errors().get(0));
    TestHelper.assertError(errors, 0, TestException.class, "Outer");
    TestHelper.assertError(errors, 1, TestException.class, "Inner");
}
Also used : Action(io.reactivex.rxjava3.functions.Action) Test(org.junit.Test)

Example 10 with Action

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

the class TrampolineSchedulerTest method trampolineWorkerHandlesConcurrentScheduling.

/**
 * This is a regression test for #1702. Concurrent work scheduling that is improperly synchronized can cause an
 * action to be added or removed onto the priority queue during a poll, which can result in NPEs during queue
 * sifting. While it is difficult to isolate the issue directly, we can easily trigger the behavior by spamming the
 * trampoline with enqueue requests from multiple threads concurrently.
 */
@Test
public void trampolineWorkerHandlesConcurrentScheduling() {
    final Worker trampolineWorker = Schedulers.trampoline().createWorker();
    final Subscriber<Object> subscriber = TestHelper.mockSubscriber();
    final TestSubscriber<Disposable> ts = new TestSubscriber<>(subscriber);
    // Spam the trampoline with actions.
    Flowable.range(0, 50).flatMap(new Function<Integer, Publisher<Disposable>>() {

        @Override
        public Publisher<Disposable> apply(Integer count) {
            return Flowable.interval(1, TimeUnit.MICROSECONDS).map(new Function<Long, Disposable>() {

                @Override
                public Disposable apply(Long ount1) {
                    return trampolineWorker.schedule(Functions.EMPTY_RUNNABLE);
                }
            }).take(100);
        }
    }).subscribeOn(Schedulers.computation()).subscribe(ts);
    ts.awaitDone(5, TimeUnit.SECONDS);
    ts.assertNoErrors();
}
Also used : Worker(io.reactivex.rxjava3.core.Scheduler.Worker) TestSubscriber(io.reactivex.rxjava3.subscribers.TestSubscriber) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)109 TestException (io.reactivex.rxjava3.exceptions.TestException)64 Action (io.reactivex.rxjava3.functions.Action)49 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)23 BooleanSubscription (io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription)13 TestSubscriber (io.reactivex.rxjava3.subscribers.TestSubscriber)13 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)13 TestObserver (io.reactivex.rxjava3.observers.TestObserver)10 Disposable (io.reactivex.rxjava3.disposables.Disposable)9 IOException (java.io.IOException)9 Worker (io.reactivex.rxjava3.core.Scheduler.Worker)7 TestScheduler (io.reactivex.rxjava3.schedulers.TestScheduler)7 RxJavaTest (io.reactivex.rxjava3.core.RxJavaTest)6 Observable (io.reactivex.rxjava3.core.Observable)4 ForEachWhileSubscriber (io.reactivex.rxjava3.internal.subscribers.ForEachWhileSubscriber)4 CountingRunnable (io.reactivex.rxjava3.android.testutil.CountingRunnable)3 Flowable (io.reactivex.rxjava3.core.Flowable)3 GroupedFlowable (io.reactivex.rxjava3.flowables.GroupedFlowable)3 ConditionalSubscriber (io.reactivex.rxjava3.operators.ConditionalSubscriber)3 AtomicLong (java.util.concurrent.atomic.AtomicLong)3