Search in sources :

Example 11 with Completable

use of io.reactivex.rxjava3.core.Completable in project RxJava by ReactiveX.

the class CompletableMergeArrayDelayError method subscribeActual.

@Override
public void subscribeActual(final CompletableObserver observer) {
    final CompositeDisposable set = new CompositeDisposable();
    final AtomicInteger wip = new AtomicInteger(sources.length + 1);
    final AtomicThrowable errors = new AtomicThrowable();
    set.add(new TryTerminateAndReportDisposable(errors));
    observer.onSubscribe(set);
    for (CompletableSource c : sources) {
        if (set.isDisposed()) {
            return;
        }
        if (c == null) {
            Throwable ex = new NullPointerException("A completable source is null");
            errors.tryAddThrowableOrReport(ex);
            wip.decrementAndGet();
            continue;
        }
        c.subscribe(new MergeInnerCompletableObserver(observer, set, errors, wip));
    }
    if (wip.decrementAndGet() == 0) {
        errors.tryTerminateConsumer(observer);
    }
}
Also used : AtomicThrowable(io.reactivex.rxjava3.internal.util.AtomicThrowable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicThrowable(io.reactivex.rxjava3.internal.util.AtomicThrowable)

Example 12 with Completable

use of io.reactivex.rxjava3.core.Completable in project RxJava by ReactiveX.

the class CompletableSwitchOnNextTest method delaySwitch.

@Test
public void delaySwitch() {
    PublishProcessor<Completable> pp = PublishProcessor.create();
    TestObserver<Void> to = Completable.switchOnNextDelayError(pp).test();
    assertTrue(pp.hasSubscribers());
    to.assertEmpty();
    CompletableSubject cs1 = CompletableSubject.create();
    CompletableSubject cs2 = CompletableSubject.create();
    pp.onNext(cs1);
    assertTrue(cs1.hasObservers());
    pp.onNext(cs2);
    assertFalse(cs1.hasObservers());
    assertTrue(cs2.hasObservers());
    assertTrue(cs2.hasObservers());
    cs2.onError(new TestException());
    assertTrue(pp.hasSubscribers());
    to.assertEmpty();
    pp.onComplete();
    to.assertFailure(TestException.class);
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) CompletableSubject(io.reactivex.rxjava3.subjects.CompletableSubject) Test(org.junit.Test)

Example 13 with Completable

use of io.reactivex.rxjava3.core.Completable in project RxJava by ReactiveX.

the class CompletableTimeoutTest method timeoutContinueOther.

@Test
public void timeoutContinueOther() throws Exception {
    final int[] call = { 0 };
    Completable other = Completable.fromAction(new Action() {

        @Override
        public void run() throws Exception {
            call[0]++;
        }
    });
    Completable.never().timeout(100, TimeUnit.MILLISECONDS, Schedulers.io(), other).test().awaitDone(5, TimeUnit.SECONDS).assertResult();
    assertEquals(1, call[0]);
}
Also used : Action(io.reactivex.rxjava3.functions.Action) TestException(io.reactivex.rxjava3.exceptions.TestException) Test(org.junit.Test)

Example 14 with Completable

use of io.reactivex.rxjava3.core.Completable 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 15 with Completable

use of io.reactivex.rxjava3.core.Completable 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)

Aggregations

Test (org.junit.Test)36 TestException (io.reactivex.rxjava3.exceptions.TestException)21 Action (io.reactivex.rxjava3.functions.Action)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 RxMethod (io.reactivex.rxjava3.validators.BaseTypeParser.RxMethod)6 Disposable (io.reactivex.rxjava3.disposables.Disposable)5 IOException (java.io.IOException)4 Completable (io.reactivex.rxjava3.core.Completable)3 AtomicThrowable (io.reactivex.rxjava3.internal.util.AtomicThrowable)3 TestScheduler (io.reactivex.rxjava3.schedulers.TestScheduler)3 Flowable (io.reactivex.rxjava3.core.Flowable)2 TestObserver (io.reactivex.rxjava3.observers.TestObserver)2 CompletableSubject (io.reactivex.rxjava3.subjects.CompletableSubject)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Pattern (java.util.regex.Pattern)2 InOrder (org.mockito.InOrder)2 ConverterTest (io.reactivex.rxjava3.core.ConverterTest)1 Observable (io.reactivex.rxjava3.core.Observable)1 Observer (io.reactivex.rxjava3.core.Observer)1 ConnectableFlowable (io.reactivex.rxjava3.flowables.ConnectableFlowable)1