Search in sources :

Example 1 with Consumer

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

the class HandlerSchedulerTest method throwingActionRoutedToRxJavaPlugins.

@Test
public void throwingActionRoutedToRxJavaPlugins() {
    Consumer<? super Throwable> originalErrorHandler = RxJavaPlugins.getErrorHandler();
    try {
        final AtomicReference<Throwable> throwableRef = new AtomicReference<>();
        RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() {

            @Override
            public void accept(Throwable throwable) throws Exception {
                throwableRef.set(throwable);
            }
        });
        Worker worker = scheduler.createWorker();
        final NullPointerException npe = new NullPointerException();
        Runnable action = new Runnable() {

            @Override
            public void run() {
                throw npe;
            }
        };
        worker.schedule(action);
        runUiThreadTasks();
        assertSame(npe, throwableRef.get());
    } finally {
        RxJavaPlugins.setErrorHandler(originalErrorHandler);
    }
}
Also used : CountingRunnable(io.reactivex.rxjava3.android.testutil.CountingRunnable) Worker(io.reactivex.rxjava3.core.Scheduler.Worker) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 2 with Consumer

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

the class SingleConsumersTest method onErrorCrash.

@Test
public void onErrorCrash() {
    List<Throwable> errors = TestHelper.trackPluginErrors();
    try {
        subscribeAutoDispose(processor, composite, this, new Consumer<Throwable>() {

            @Override
            public void accept(Throwable t) throws Exception {
                throw new IOException(t);
            }
        });
        processor.onError(new IllegalArgumentException());
        assertTrue(events.toString(), events.isEmpty());
        TestHelper.assertError(errors, 0, CompositeException.class);
        List<Throwable> inners = TestHelper.compositeList(errors.get(0));
        TestHelper.assertError(inners, 0, IllegalArgumentException.class);
        TestHelper.assertError(inners, 1, IOException.class);
    } finally {
        RxJavaPlugins.reset();
    }
}
Also used : IOException(java.io.IOException) IOException(java.io.IOException) CompositeException(io.reactivex.rxjava3.exceptions.CompositeException) Test(org.junit.Test)

Example 3 with Consumer

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

the class MaybeAmbTest method noWinnerSuccessDispose.

@Test
public void noWinnerSuccessDispose() throws Exception {
    for (int i = 0; i < TestHelper.RACE_LONG_LOOPS; i++) {
        final AtomicBoolean interrupted = new AtomicBoolean();
        final CountDownLatch cdl = new CountDownLatch(1);
        Maybe.ambArray(Maybe.just(1).subscribeOn(Schedulers.single()).observeOn(Schedulers.computation()), Maybe.never()).subscribe(new Consumer<Object>() {

            @Override
            public void accept(Object v) throws Exception {
                interrupted.set(Thread.currentThread().isInterrupted());
                cdl.countDown();
            }
        });
        assertTrue(cdl.await(500, TimeUnit.SECONDS));
        assertFalse("Interrupted!", interrupted.get());
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestException(io.reactivex.rxjava3.exceptions.TestException) Test(org.junit.Test)

Example 4 with Consumer

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

the class MaybeAmbTest method noWinnerErrorDispose.

@Test
public void noWinnerErrorDispose() throws Exception {
    final TestException ex = new TestException();
    for (int i = 0; i < TestHelper.RACE_LONG_LOOPS; i++) {
        final AtomicBoolean interrupted = new AtomicBoolean();
        final CountDownLatch cdl = new CountDownLatch(1);
        Maybe.ambArray(Maybe.error(ex).subscribeOn(Schedulers.single()).observeOn(Schedulers.computation()), Maybe.never()).subscribe(Functions.emptyConsumer(), new Consumer<Throwable>() {

            @Override
            public void accept(Throwable e) throws Exception {
                interrupted.set(Thread.currentThread().isInterrupted());
                cdl.countDown();
            }
        });
        assertTrue(cdl.await(500, TimeUnit.SECONDS));
        assertFalse("Interrupted!", interrupted.get());
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestException(io.reactivex.rxjava3.exceptions.TestException) TestException(io.reactivex.rxjava3.exceptions.TestException) Test(org.junit.Test)

Example 5 with Consumer

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

the class MaybeCacheTest method crossCancelOnError.

@Test
public void crossCancelOnError() {
    final TestSubscriber<Integer> ts = new TestSubscriber<>();
    PublishProcessor<Integer> pp = PublishProcessor.create();
    Maybe<Integer> source = pp.singleElement().cache();
    source.subscribe(Functions.emptyConsumer(), new Consumer<Object>() {

        @Override
        public void accept(Object v) throws Exception {
            ts.cancel();
        }
    });
    source.toFlowable().subscribe(ts);
    pp.onError(new TestException());
    ts.assertEmpty();
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) TestSubscriber(io.reactivex.rxjava3.subscribers.TestSubscriber) 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