Search in sources :

Example 76 with Observable

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

the class ObservableWithLatestFromTest method otherThrows.

@Test
public void otherThrows() {
    PublishSubject<Integer> source = PublishSubject.create();
    PublishSubject<Integer> other = PublishSubject.create();
    Observable<Integer> result = source.withLatestFrom(other, COMBINER);
    TestObserverEx<Integer> to = new TestObserverEx<>();
    result.subscribe(to);
    assertTrue(source.hasObservers());
    assertTrue(other.hasObservers());
    other.onNext(1);
    source.onNext(1);
    other.onError(new TestException());
    to.assertTerminated();
    to.assertValue((1 << 8) + 1);
    to.assertNotComplete();
    to.assertError(TestException.class);
    assertFalse(source.hasObservers());
    assertFalse(other.hasObservers());
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) RxJavaTest(io.reactivex.rxjava3.core.RxJavaTest) Test(org.junit.Test)

Example 77 with Observable

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

the class ObservableRetryTest method noCancelPreviousRetryWhile.

@Test
public void noCancelPreviousRetryWhile() {
    final AtomicInteger counter = new AtomicInteger();
    final AtomicInteger times = new AtomicInteger();
    Observable<Integer> source = Observable.defer(new Supplier<ObservableSource<Integer>>() {

        @Override
        public ObservableSource<Integer> get() throws Exception {
            if (times.getAndIncrement() < 4) {
                return Observable.error(new TestException());
            }
            return Observable.just(1);
        }
    }).doOnDispose(new Action() {

        @Override
        public void run() throws Exception {
            counter.getAndIncrement();
        }
    });
    source.retry(5, Functions.alwaysTrue()).test().assertResult(1);
    assertEquals(0, counter.get());
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) TestException(io.reactivex.rxjava3.exceptions.TestException) Test(org.junit.Test)

Example 78 with Observable

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

the class ObservableRetryTest method noCancelPreviousRetryWhile2.

@Test
public void noCancelPreviousRetryWhile2() {
    final AtomicInteger counter = new AtomicInteger();
    final AtomicInteger times = new AtomicInteger();
    Observable<Integer> source = Observable.defer(new Supplier<ObservableSource<Integer>>() {

        @Override
        public ObservableSource<Integer> get() throws Exception {
            if (times.getAndIncrement() < 4) {
                return Observable.error(new TestException());
            }
            return Observable.just(1);
        }
    }).doOnDispose(new Action() {

        @Override
        public void run() throws Exception {
            counter.getAndIncrement();
        }
    });
    source.retry(new BiPredicate<Integer, Throwable>() {

        @Override
        public boolean test(Integer a, Throwable b) throws Exception {
            return a < 5;
        }
    }).test().assertResult(1);
    assertEquals(0, counter.get());
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) TestException(io.reactivex.rxjava3.exceptions.TestException) Test(org.junit.Test)

Example 79 with Observable

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

the class ObservableRetryTest method repeatFloodNoSubscriptionError.

@Test
public void repeatFloodNoSubscriptionError() {
    List<Throwable> errors = TestHelper.trackPluginErrors();
    final TestException error = new TestException();
    try {
        final PublishSubject<Integer> source = PublishSubject.create();
        final PublishSubject<Integer> signaller = PublishSubject.create();
        for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
            TestObserver<Integer> to = source.take(1).map(new Function<Integer, Integer>() {

                @Override
                public Integer apply(Integer v) throws Exception {
                    throw error;
                }
            }).retryWhen(new Function<Observable<Throwable>, ObservableSource<Integer>>() {

                @Override
                public ObservableSource<Integer> apply(Observable<Throwable> v) throws Exception {
                    return signaller;
                }
            }).test();
            Runnable r1 = new Runnable() {

                @Override
                public void run() {
                    for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
                        source.onNext(1);
                    }
                }
            };
            Runnable r2 = new Runnable() {

                @Override
                public void run() {
                    for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
                        signaller.onNext(1);
                    }
                }
            };
            TestHelper.race(r1, r2);
            to.dispose();
        }
        if (!errors.isEmpty()) {
            for (Throwable e : errors) {
                e.printStackTrace();
            }
            fail(errors + "");
        }
    } finally {
        RxJavaPlugins.reset();
    }
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) TestException(io.reactivex.rxjava3.exceptions.TestException) Observable(io.reactivex.rxjava3.core.Observable) GroupedObservable(io.reactivex.rxjava3.observables.GroupedObservable) Test(org.junit.Test)

Example 80 with Observable

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

the class ObservableTimeoutWithSelectorTest method timeoutSelectorWithFirstTimeoutFirstAndNoOtherObservable.

@Test
public void timeoutSelectorWithFirstTimeoutFirstAndNoOtherObservable() {
    PublishSubject<Integer> source = PublishSubject.create();
    final PublishSubject<Integer> timeout = PublishSubject.create();
    Function<Integer, Observable<Integer>> timeoutFunc = new Function<Integer, Observable<Integer>>() {

        @Override
        public Observable<Integer> apply(Integer t1) {
            return PublishSubject.create();
        }
    };
    Observer<Object> o = TestHelper.mockObserver();
    source.timeout(timeout, timeoutFunc).subscribe(o);
    timeout.onNext(1);
    InOrder inOrder = inOrder(o);
    inOrder.verify(o).onError(isA(TimeoutException.class));
    inOrder.verifyNoMoreInteractions();
}
Also used : InOrder(org.mockito.InOrder) Observable(io.reactivex.rxjava3.core.Observable) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)132 TestException (io.reactivex.rxjava3.exceptions.TestException)109 Observable (io.reactivex.rxjava3.core.Observable)83 InOrder (org.mockito.InOrder)60 TestObserver (io.reactivex.rxjava3.observers.TestObserver)38 Disposable (io.reactivex.rxjava3.disposables.Disposable)35 IOException (java.io.IOException)25 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)25 RxJavaTest (io.reactivex.rxjava3.core.RxJavaTest)22 Observer (io.reactivex.rxjava3.core.Observer)19 TestScheduler (io.reactivex.rxjava3.schedulers.TestScheduler)17 Function (io.reactivex.rxjava3.functions.Function)10 List (java.util.List)8 TimeUnit (java.util.concurrent.TimeUnit)8 io.reactivex.rxjava3.core (io.reactivex.rxjava3.core)7 ConnectableObservable (io.reactivex.rxjava3.observables.ConnectableObservable)6 RxMethod (io.reactivex.rxjava3.validators.BaseTypeParser.RxMethod)6 Collections (java.util.Collections)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 Assert (org.junit.Assert)6