Search in sources :

Example 11 with Consumer

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

the class MaybeBlockingSubscribeTest method twoArgErrorFails.

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

Example 12 with Consumer

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

the class SingleSafeSubscribeTest method normalSuccess.

@Test
public void normalSuccess() throws Throwable {
    TestHelper.withErrorTracking(errors -> {
        @SuppressWarnings("unchecked") SingleObserver<Integer> consumer = mock(SingleObserver.class);
        Single.just(1).safeSubscribe(consumer);
        InOrder order = inOrder(consumer);
        order.verify(consumer).onSubscribe(any(Disposable.class));
        order.verify(consumer).onSuccess(1);
        order.verifyNoMoreInteractions();
        assertTrue("" + errors, errors.isEmpty());
    });
}
Also used : Disposable(io.reactivex.rxjava3.disposables.Disposable) InOrder(org.mockito.InOrder) Test(org.junit.Test)

Example 13 with Consumer

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

the class SingleSafeSubscribeTest method onSuccessCrash.

@Test
public void onSuccessCrash() throws Throwable {
    TestHelper.withErrorTracking(errors -> {
        @SuppressWarnings("unchecked") SingleObserver<Integer> consumer = mock(SingleObserver.class);
        doThrow(new TestException()).when(consumer).onSuccess(any());
        new Single<Integer>() {

            @Override
            protected void subscribeActual(@NonNull SingleObserver<? super Integer> observer) {
                observer.onSubscribe(Disposable.empty());
                observer.onSuccess(1);
            }
        }.safeSubscribe(consumer);
        InOrder order = inOrder(consumer);
        order.verify(consumer).onSubscribe(any(Disposable.class));
        order.verify(consumer).onSuccess(1);
        order.verifyNoMoreInteractions();
        TestHelper.assertUndeliverable(errors, 0, TestException.class);
    });
}
Also used : Disposable(io.reactivex.rxjava3.disposables.Disposable) InOrder(org.mockito.InOrder) Test(org.junit.Test)

Example 14 with Consumer

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

the class SingleSafeSubscribeTest method onSubscribeCrash.

@Test
public void onSubscribeCrash() throws Throwable {
    TestHelper.withErrorTracking(errors -> {
        @SuppressWarnings("unchecked") SingleObserver<Integer> consumer = mock(SingleObserver.class);
        doThrow(new TestException()).when(consumer).onSubscribe(any());
        Disposable d = Disposable.empty();
        new Single<Integer>() {

            @Override
            protected void subscribeActual(@NonNull SingleObserver<? super Integer> observer) {
                observer.onSubscribe(d);
                // none of the following should arrive at the consumer
                observer.onSuccess(1);
                observer.onError(new IOException());
            }
        }.safeSubscribe(consumer);
        InOrder order = inOrder(consumer);
        order.verify(consumer).onSubscribe(any(Disposable.class));
        order.verifyNoMoreInteractions();
        assertTrue(d.isDisposed());
        TestHelper.assertUndeliverable(errors, 0, TestException.class);
        TestHelper.assertUndeliverable(errors, 1, IOException.class);
    });
}
Also used : Disposable(io.reactivex.rxjava3.disposables.Disposable) InOrder(org.mockito.InOrder) IOException(java.io.IOException) Test(org.junit.Test)

Example 15 with Consumer

use of io.reactivex.rxjava3.functions.Consumer 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)

Aggregations

Test (org.junit.Test)98 TestException (io.reactivex.rxjava3.exceptions.TestException)57 Disposable (io.reactivex.rxjava3.disposables.Disposable)39 BooleanSubscription (io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription)22 IOException (java.io.IOException)20 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)19 InOrder (org.mockito.InOrder)17 TestObserver (io.reactivex.rxjava3.observers.TestObserver)9 TestSubscriber (io.reactivex.rxjava3.subscribers.TestSubscriber)8 RxJavaTest (io.reactivex.rxjava3.core.RxJavaTest)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 Observable (io.reactivex.rxjava3.core.Observable)5 Worker (io.reactivex.rxjava3.core.Scheduler.Worker)5 Consumer (io.reactivex.rxjava3.functions.Consumer)5 CompositeException (io.reactivex.rxjava3.exceptions.CompositeException)4 ForEachWhileSubscriber (io.reactivex.rxjava3.internal.subscribers.ForEachWhileSubscriber)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 Observer (io.reactivex.rxjava3.core.Observer)3 GroupedFlowable (io.reactivex.rxjava3.flowables.GroupedFlowable)3 ArgsToString (io.reactivex.rxjava3.internal.operators.flowable.FlowableZipTest.ArgsToString)3