Search in sources :

Example 1 with CompositeException

use of io.reactivex.rxjava3.exceptions.CompositeException in project RxJava by ReactiveX.

the class CompositeDisposableTest method compositeException.

@Test
public void compositeException() {
    final AtomicInteger counter = new AtomicInteger();
    CompositeDisposable cd = new CompositeDisposable();
    cd.add(Disposable.fromRunnable(new Runnable() {

        @Override
        public void run() {
            throw new RuntimeException("failed on first one");
        }
    }));
    cd.add(Disposable.fromRunnable(new Runnable() {

        @Override
        public void run() {
            throw new RuntimeException("failed on second one too");
        }
    }));
    cd.add(Disposable.fromRunnable(new Runnable() {

        @Override
        public void run() {
            counter.incrementAndGet();
        }
    }));
    try {
        cd.dispose();
        fail("Expecting an exception");
    } catch (CompositeException e) {
        // we expect this
        assertEquals(e.getExceptions().size(), 2);
    }
    // we should still have disposed to the second one
    assertEquals(1, counter.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CompositeException(io.reactivex.rxjava3.exceptions.CompositeException) RxJavaTest(io.reactivex.rxjava3.core.RxJavaTest) Test(org.junit.Test)

Example 2 with CompositeException

use of io.reactivex.rxjava3.exceptions.CompositeException in project RxJava by ReactiveX.

the class ExceptionHelper method addThrowable.

public static boolean addThrowable(AtomicReference<Throwable> field, Throwable exception) {
    for (; ; ) {
        Throwable current = field.get();
        if (current == TERMINATED) {
            return false;
        }
        Throwable update;
        if (current == null) {
            update = exception;
        } else {
            update = new CompositeException(current, exception);
        }
        if (field.compareAndSet(current, update)) {
            return true;
        }
    }
}
Also used : CompositeException(io.reactivex.rxjava3.exceptions.CompositeException)

Example 3 with CompositeException

use of io.reactivex.rxjava3.exceptions.CompositeException 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 4 with CompositeException

use of io.reactivex.rxjava3.exceptions.CompositeException in project RxJava by ReactiveX.

the class CompletableSafeSubscribeTest method onErrorCrash.

@Test
public void onErrorCrash() throws Throwable {
    TestHelper.withErrorTracking(errors -> {
        CompletableObserver consumer = mock(CompletableObserver.class);
        doThrow(new TestException()).when(consumer).onError(any());
        new Completable() {

            @Override
            protected void subscribeActual(@NonNull CompletableObserver observer) {
                observer.onSubscribe(Disposable.empty());
                // none of the following should arrive at the consumer
                observer.onError(new IOException());
            }
        }.safeSubscribe(consumer);
        InOrder order = inOrder(consumer);
        order.verify(consumer).onSubscribe(any(Disposable.class));
        order.verify(consumer).onError(any(IOException.class));
        order.verifyNoMoreInteractions();
        TestHelper.assertError(errors, 0, CompositeException.class);
        CompositeException compositeException = (CompositeException) errors.get(0);
        TestHelper.assertError(compositeException.getExceptions(), 0, IOException.class);
        TestHelper.assertError(compositeException.getExceptions(), 1, TestException.class);
    });
}
Also used : Disposable(io.reactivex.rxjava3.disposables.Disposable) InOrder(org.mockito.InOrder) IOException(java.io.IOException) Test(org.junit.Test)

Example 5 with CompositeException

use of io.reactivex.rxjava3.exceptions.CompositeException in project RxJava by ReactiveX.

the class FlowableSwitchMapCompletableTest method onErrorInnerErrorRace.

@Test
public void onErrorInnerErrorRace() {
    final TestException ex0 = new TestException();
    final TestException ex = new TestException();
    for (int i = 0; i < TestHelper.RACE_LONG_LOOPS; i++) {
        List<Throwable> errors = TestHelper.trackPluginErrors();
        try {
            final PublishProcessor<Integer> pp = PublishProcessor.create();
            final CompletableSubject cs = CompletableSubject.create();
            TestObserver<Void> to = pp.switchMapCompletable(Functions.justFunction(cs)).test();
            pp.onNext(1);
            Runnable r1 = new Runnable() {

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

                @Override
                public void run() {
                    cs.onError(ex);
                }
            };
            TestHelper.race(r1, r2);
            to.assertError(new Predicate<Throwable>() {

                @Override
                public boolean test(Throwable e) throws Exception {
                    return e instanceof TestException || e instanceof CompositeException;
                }
            });
            if (!errors.isEmpty()) {
                TestHelper.assertUndeliverable(errors, 0, TestException.class);
            }
        } finally {
            RxJavaPlugins.reset();
        }
    }
}
Also used : CompletableSubject(io.reactivex.rxjava3.subjects.CompletableSubject) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)8 CompositeException (io.reactivex.rxjava3.exceptions.CompositeException)4 Disposable (io.reactivex.rxjava3.disposables.Disposable)3 CompletableSubject (io.reactivex.rxjava3.subjects.CompletableSubject)3 IOException (java.io.IOException)3 InOrder (org.mockito.InOrder)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 RxJavaTest (io.reactivex.rxjava3.core.RxJavaTest)1 AtomicThrowable (io.reactivex.rxjava3.internal.util.AtomicThrowable)1