Search in sources :

Example 76 with Consumer

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

the class SingleDelayTest method onErrorCalledOnScheduler.

@Test
public void onErrorCalledOnScheduler() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<Thread> thread = new AtomicReference<>();
    Single.<String>error(new Exception()).delay(0, TimeUnit.MILLISECONDS, Schedulers.newThread()).doOnError(new Consumer<Throwable>() {

        @Override
        public void accept(Throwable throwable) throws Exception {
            thread.set(Thread.currentThread());
            latch.countDown();
        }
    }).onErrorResumeWith(Single.just("")).subscribe();
    latch.await();
    assertNotEquals(Thread.currentThread(), thread.get());
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) TestException(io.reactivex.rxjava3.exceptions.TestException) Test(org.junit.Test)

Example 77 with Consumer

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

the class SingleDoOnLifecycleTest 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 Single<Integer>() {

            @Override
            protected void subscribeActual(SingleObserver<? super Integer> observer) {
                observer.onSubscribe(bs);
                observer.onError(new TestException("Second"));
                observer.onSuccess(1);
            }
        }.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 78 with Consumer

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

the class BoundedSubscriberTest method badSourceOnSubscribe.

@Test
public void badSourceOnSubscribe() {
    Flowable<Integer> source = Flowable.fromPublisher(new Publisher<Integer>() {

        @Override
        public void subscribe(Subscriber<? super Integer> s) {
            BooleanSubscription s1 = new BooleanSubscription();
            s.onSubscribe(s1);
            BooleanSubscription s2 = new BooleanSubscription();
            s.onSubscribe(s2);
            assertFalse(s1.isCancelled());
            assertTrue(s2.isCancelled());
            s.onNext(1);
            s.onComplete();
        }
    });
    final List<Object> received = new ArrayList<>();
    BoundedSubscriber<Object> subscriber = new BoundedSubscriber<>(new Consumer<Object>() {

        @Override
        public void accept(Object v) throws Exception {
            received.add(v);
        }
    }, new Consumer<Throwable>() {

        @Override
        public void accept(Throwable e) throws Exception {
            received.add(e);
        }
    }, new Action() {

        @Override
        public void run() throws Exception {
            received.add(100);
        }
    }, new Consumer<Subscription>() {

        @Override
        public void accept(Subscription s) throws Exception {
            s.request(128);
        }
    }, 128);
    source.subscribe(subscriber);
    assertEquals(Arrays.asList(1, 100), received);
}
Also used : BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) Test(org.junit.Test)

Example 79 with Consumer

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

the class LambdaSubscriberTest method badSourceOnSubscribe.

@Test
public void badSourceOnSubscribe() {
    Flowable<Integer> source = Flowable.fromPublisher(new Publisher<Integer>() {

        @Override
        public void subscribe(Subscriber<? super Integer> s) {
            BooleanSubscription s1 = new BooleanSubscription();
            s.onSubscribe(s1);
            BooleanSubscription s2 = new BooleanSubscription();
            s.onSubscribe(s2);
            assertFalse(s1.isCancelled());
            assertTrue(s2.isCancelled());
            s.onNext(1);
            s.onComplete();
        }
    });
    final List<Object> received = new ArrayList<>();
    LambdaSubscriber<Object> subscriber = new LambdaSubscriber<>(new Consumer<Object>() {

        @Override
        public void accept(Object v) throws Exception {
            received.add(v);
        }
    }, new Consumer<Throwable>() {

        @Override
        public void accept(Throwable e) throws Exception {
            received.add(e);
        }
    }, new Action() {

        @Override
        public void run() throws Exception {
            received.add(100);
        }
    }, new Consumer<Subscription>() {

        @Override
        public void accept(Subscription s) throws Exception {
            s.request(Long.MAX_VALUE);
        }
    });
    source.subscribe(subscriber);
    assertEquals(Arrays.asList(1, 100), received);
}
Also used : BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) Test(org.junit.Test)

Example 80 with Consumer

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

the class FlowableSubscriberTest method onErrorThrows.

@Test
public void onErrorThrows() {
    ForEachWhileSubscriber<Integer> s = new ForEachWhileSubscriber<>(new Predicate<Integer>() {

        @Override
        public boolean test(Integer v) throws Exception {
            return true;
        }
    }, new Consumer<Throwable>() {

        @Override
        public void accept(Throwable e) throws Exception {
            throw new TestException("Inner");
        }
    }, new Action() {

        @Override
        public void run() throws Exception {
        }
    });
    List<Throwable> list = TestHelper.trackPluginErrors();
    try {
        s.onSubscribe(new BooleanSubscription());
        s.onError(new TestException("Outer"));
        TestHelper.assertError(list, 0, CompositeException.class);
        List<Throwable> cel = TestHelper.compositeList(list.get(0));
        TestHelper.assertError(cel, 0, TestException.class, "Outer");
        TestHelper.assertError(cel, 1, TestException.class, "Inner");
    } finally {
        RxJavaPlugins.reset();
    }
}
Also used : ForEachWhileSubscriber(io.reactivex.rxjava3.internal.subscribers.ForEachWhileSubscriber) BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) 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