Search in sources :

Example 16 with Observable

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

the class ObservableCreateTest method onErrorRace.

@Test
public void onErrorRace() {
    Observable<Object> source = Observable.create(new ObservableOnSubscribe<Object>() {

        @Override
        public void subscribe(ObservableEmitter<Object> e) throws Exception {
            final ObservableEmitter<Object> f = e.serialize();
            final TestException ex = new TestException();
            Runnable r1 = new Runnable() {

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

                @Override
                public void run() {
                    f.onError(ex);
                }
            };
            TestHelper.race(r1, r2);
        }
    });
    List<Throwable> errors = TestHelper.trackPluginErrors();
    try {
        for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
            source.test().assertFailure(Throwable.class);
        }
    } finally {
        RxJavaPlugins.reset();
    }
    assertFalse(errors.isEmpty());
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) TestException(io.reactivex.rxjava3.exceptions.TestException) IOException(java.io.IOException) Test(org.junit.Test)

Example 17 with Observable

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

the class ObservableCreateTest method serializedDrainDoneButNotEmpty.

@Test
public void serializedDrainDoneButNotEmpty() throws Throwable {
    for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
        TestObserver<Integer> to = new TestObserver<>();
        AtomicReference<ObservableEmitter<Integer>> ref = new AtomicReference<>();
        CountDownLatch cdl = new CountDownLatch(1);
        Observable.<Integer>create(emitter -> {
            emitter = emitter.serialize();
            ref.set(emitter);
            emitter.onNext(1);
        }).doOnNext(v -> {
            if (v == 1) {
                TestHelper.raceOther(() -> {
                    ref.get().onNext(2);
                    ref.get().onComplete();
                }, cdl);
                ref.get().onNext(3);
            }
        }).subscribe(to);
        cdl.await();
    }
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) Cancellable(io.reactivex.rxjava3.functions.Cancellable) IOException(java.io.IOException) Test(org.junit.Test) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) TestObserver(io.reactivex.rxjava3.observers.TestObserver) Disposable(io.reactivex.rxjava3.disposables.Disposable) io.reactivex.rxjava3.core(io.reactivex.rxjava3.core) Assert(org.junit.Assert) io.reactivex.rxjava3.testsupport(io.reactivex.rxjava3.testsupport) RxJavaPlugins(io.reactivex.rxjava3.plugins.RxJavaPlugins) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) TestObserver(io.reactivex.rxjava3.observers.TestObserver) Test(org.junit.Test)

Example 18 with Observable

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

the class ObservableDelaySubscriptionOtherTest method noPrematureSubscriptionToError.

@Test
public void noPrematureSubscriptionToError() {
    PublishSubject<Object> other = PublishSubject.create();
    TestObserver<Integer> to = new TestObserver<>();
    final AtomicInteger subscribed = new AtomicInteger();
    Observable.<Integer>error(new TestException()).doOnSubscribe(new Consumer<Disposable>() {

        @Override
        public void accept(Disposable d) {
            subscribed.getAndIncrement();
        }
    }).delaySubscription(other).subscribe(to);
    to.assertNotComplete();
    to.assertNoErrors();
    to.assertNoValues();
    Assert.assertEquals("Premature subscription", 0, subscribed.get());
    other.onComplete();
    Assert.assertEquals("No subscription", 1, subscribed.get());
    to.assertNoValues();
    to.assertNotComplete();
    to.assertError(TestException.class);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Disposable(io.reactivex.rxjava3.disposables.Disposable) TestException(io.reactivex.rxjava3.exceptions.TestException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestObserver(io.reactivex.rxjava3.observers.TestObserver)

Example 19 with Observable

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

the class ObservableDematerializeTest method dematerialize3.

@Test
public void dematerialize3() {
    Exception exception = new Exception("test");
    Observable<Integer> o = Observable.error(exception);
    Observable<Integer> dematerialize = o.materialize().dematerialize(Functions.<Notification<Integer>>identity());
    Observer<Integer> observer = TestHelper.mockObserver();
    dematerialize.subscribe(observer);
    verify(observer, times(1)).onError(exception);
    verify(observer, times(0)).onComplete();
    verify(observer, times(0)).onNext(any(Integer.class));
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) Test(org.junit.Test)

Example 20 with Observable

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

the class ObservableDematerializeTest method errorPassThru.

@Test
public void errorPassThru() {
    Exception exception = new Exception("test");
    Observable<Notification<Integer>> o = Observable.error(exception);
    Observable<Integer> dematerialize = o.dematerialize(Functions.<Notification<Integer>>identity());
    Observer<Integer> observer = TestHelper.mockObserver();
    dematerialize.subscribe(observer);
    verify(observer, times(1)).onError(exception);
    verify(observer, times(0)).onComplete();
    verify(observer, times(0)).onNext(any(Integer.class));
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) 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