Search in sources :

Example 6 with Observable

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

the class RxJavaPluginsTest method clearIsPassthrough.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void clearIsPassthrough() {
    try {
        RxJavaPlugins.reset();
        assertNull(RxJavaPlugins.onAssembly((Observable) null));
        assertNull(RxJavaPlugins.onAssembly((ConnectableObservable) null));
        assertNull(RxJavaPlugins.onAssembly((Flowable) null));
        assertNull(RxJavaPlugins.onAssembly((ConnectableFlowable) null));
        Observable oos = new Observable() {

            @Override
            public void subscribeActual(Observer t) {
            }
        };
        Flowable fos = new Flowable() {

            @Override
            public void subscribeActual(Subscriber t) {
            }
        };
        assertSame(oos, RxJavaPlugins.onAssembly(oos));
        assertSame(fos, RxJavaPlugins.onAssembly(fos));
        assertNull(RxJavaPlugins.onAssembly((Single) null));
        Single sos = new Single() {

            @Override
            public void subscribeActual(SingleObserver t) {
            }
        };
        assertSame(sos, RxJavaPlugins.onAssembly(sos));
        assertNull(RxJavaPlugins.onAssembly((Completable) null));
        Completable cos = new Completable() {

            @Override
            public void subscribeActual(CompletableObserver t) {
            }
        };
        assertSame(cos, RxJavaPlugins.onAssembly(cos));
        assertNull(RxJavaPlugins.onAssembly((Maybe) null));
        Maybe myb = new Maybe() {

            @Override
            public void subscribeActual(MaybeObserver t) {
            }
        };
        assertSame(myb, RxJavaPlugins.onAssembly(myb));
        Runnable action = Functions.EMPTY_RUNNABLE;
        assertSame(action, RxJavaPlugins.onSchedule(action));
        class AllSubscriber implements Subscriber, Observer, SingleObserver, CompletableObserver, MaybeObserver {

            @Override
            public void onSuccess(Object value) {
            }

            @Override
            public void onSubscribe(Disposable d) {
            }

            @Override
            public void onSubscribe(Subscription s) {
            }

            @Override
            public void onNext(Object t) {
            }

            @Override
            public void onError(Throwable t) {
            }

            @Override
            public void onComplete() {
            }
        }
        AllSubscriber all = new AllSubscriber();
        Subscriber[] allArray = { all };
        assertNull(RxJavaPlugins.onSubscribe(Observable.never(), null));
        assertSame(all, RxJavaPlugins.onSubscribe(Observable.never(), all));
        assertNull(RxJavaPlugins.onSubscribe(Flowable.never(), null));
        assertSame(all, RxJavaPlugins.onSubscribe(Flowable.never(), all));
        assertNull(RxJavaPlugins.onSubscribe(Single.just(1), null));
        assertSame(all, RxJavaPlugins.onSubscribe(Single.just(1), all));
        assertNull(RxJavaPlugins.onSubscribe(Completable.never(), null));
        assertSame(all, RxJavaPlugins.onSubscribe(Completable.never(), all));
        assertNull(RxJavaPlugins.onSubscribe(Maybe.never(), null));
        assertSame(all, RxJavaPlugins.onSubscribe(Maybe.never(), all));
        assertNull(RxJavaPlugins.onSubscribe(Flowable.never().parallel(), null));
        assertSame(allArray, RxJavaPlugins.onSubscribe(Flowable.never().parallel(), allArray));
        final Scheduler s = ImmediateThinScheduler.INSTANCE;
        Supplier<Scheduler> c = new Supplier<Scheduler>() {

            @Override
            public Scheduler get() throws Exception {
                return s;
            }
        };
        assertSame(s, RxJavaPlugins.onComputationScheduler(s));
        assertSame(s, RxJavaPlugins.onIoScheduler(s));
        assertSame(s, RxJavaPlugins.onNewThreadScheduler(s));
        assertSame(s, RxJavaPlugins.onSingleScheduler(s));
        assertSame(s, RxJavaPlugins.initComputationScheduler(c));
        assertSame(s, RxJavaPlugins.initIoScheduler(c));
        assertSame(s, RxJavaPlugins.initNewThreadScheduler(c));
        assertSame(s, RxJavaPlugins.initSingleScheduler(c));
    } finally {
        RxJavaPlugins.reset();
    }
}
Also used : ImmediateThinScheduler(io.reactivex.rxjava3.internal.schedulers.ImmediateThinScheduler) ConnectableFlowable(io.reactivex.rxjava3.flowables.ConnectableFlowable) Observable(io.reactivex.rxjava3.core.Observable) ConnectableObservable(io.reactivex.rxjava3.observables.ConnectableObservable) Observer(io.reactivex.rxjava3.core.Observer) ConnectableObservable(io.reactivex.rxjava3.observables.ConnectableObservable) ScalarSubscription(io.reactivex.rxjava3.internal.subscriptions.ScalarSubscription) ParallelFlowable(io.reactivex.rxjava3.parallel.ParallelFlowable) ConnectableFlowable(io.reactivex.rxjava3.flowables.ConnectableFlowable) Test(org.junit.Test)

Example 7 with Observable

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

the class ObservableConcatTest method nestedAsyncConcat.

/**
 * Test an async Observable that emits more async Observables.
 * @throws InterruptedException if the test is interrupted
 */
@Test
public void nestedAsyncConcat() throws InterruptedException {
    Observer<String> observer = TestHelper.mockObserver();
    final TestObservable<String> o1 = new TestObservable<>("one", "two", "three");
    final TestObservable<String> o2 = new TestObservable<>("four", "five", "six");
    final TestObservable<String> o3 = new TestObservable<>("seven", "eight", "nine");
    final CountDownLatch allowThird = new CountDownLatch(1);
    final AtomicReference<Thread> parent = new AtomicReference<>();
    final CountDownLatch parentHasStarted = new CountDownLatch(1);
    final CountDownLatch parentHasFinished = new CountDownLatch(1);
    Observable<Observable<String>> observableOfObservables = Observable.unsafeCreate(new ObservableSource<Observable<String>>() {

        @Override
        public void subscribe(final Observer<? super Observable<String>> observer) {
            final Disposable d = Disposable.empty();
            observer.onSubscribe(d);
            parent.set(new Thread(new Runnable() {

                @Override
                public void run() {
                    try {
                        // emit first
                        if (!d.isDisposed()) {
                            System.out.println("Emit o1");
                            observer.onNext(Observable.unsafeCreate(o1));
                        }
                        // emit second
                        if (!d.isDisposed()) {
                            System.out.println("Emit o2");
                            observer.onNext(Observable.unsafeCreate(o2));
                        }
                        // wait until sometime later and emit third
                        try {
                            allowThird.await();
                        } catch (InterruptedException e) {
                            observer.onError(e);
                        }
                        if (!d.isDisposed()) {
                            System.out.println("Emit o3");
                            observer.onNext(Observable.unsafeCreate(o3));
                        }
                    } catch (Throwable e) {
                        observer.onError(e);
                    } finally {
                        System.out.println("Done parent Observable");
                        observer.onComplete();
                        parentHasFinished.countDown();
                    }
                }
            }));
            parent.get().start();
            parentHasStarted.countDown();
        }
    });
    Observable.concat(observableOfObservables).subscribe(observer);
    // wait for parent to start
    parentHasStarted.await();
    try {
        // wait for first 2 async observables to complete
        System.out.println("Thread1 is starting ... waiting for it to complete ...");
        o1.waitForThreadDone();
        System.out.println("Thread2 is starting ... waiting for it to complete ...");
        o2.waitForThreadDone();
    } catch (Throwable e) {
        throw new RuntimeException("failed waiting on threads", e);
    }
    InOrder inOrder = inOrder(observer);
    inOrder.verify(observer, times(1)).onNext("one");
    inOrder.verify(observer, times(1)).onNext("two");
    inOrder.verify(observer, times(1)).onNext("three");
    inOrder.verify(observer, times(1)).onNext("four");
    inOrder.verify(observer, times(1)).onNext("five");
    inOrder.verify(observer, times(1)).onNext("six");
    // we shouldn't have the following 3 yet
    inOrder.verify(observer, never()).onNext("seven");
    inOrder.verify(observer, never()).onNext("eight");
    inOrder.verify(observer, never()).onNext("nine");
    // we should not be completed yet
    verify(observer, never()).onComplete();
    verify(observer, never()).onError(any(Throwable.class));
    // now allow the third
    allowThird.countDown();
    try {
        // wait for 3rd to complete
        o3.waitForThreadDone();
    } catch (Throwable e) {
        throw new RuntimeException("failed waiting on threads", e);
    }
    try {
        // wait for the parent to complete
        if (!parentHasFinished.await(5, TimeUnit.SECONDS)) {
            fail("Parent didn't finish within the time limit");
        }
    } catch (Throwable e) {
        throw new RuntimeException("failed waiting on threads", e);
    }
    inOrder.verify(observer, times(1)).onNext("seven");
    inOrder.verify(observer, times(1)).onNext("eight");
    inOrder.verify(observer, times(1)).onNext("nine");
    verify(observer, never()).onError(any(Throwable.class));
    inOrder.verify(observer, times(1)).onComplete();
}
Also used : InOrder(org.mockito.InOrder) Observable(io.reactivex.rxjava3.core.Observable) Test(org.junit.Test)

Example 8 with Observable

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

the class ObservableConcatTest method concatReportsDisposedOnErrorDelayError.

@Test
public void concatReportsDisposedOnErrorDelayError() {
    final Disposable[] disposable = { null };
    Observable.concatArrayDelayError(Observable.just(1), Observable.<Integer>error(new TestException())).subscribe(new Observer<Integer>() {

        @Override
        public void onSubscribe(Disposable d) {
            disposable[0] = d;
        }

        @Override
        public void onNext(Integer t) {
        }

        @Override
        public void onError(Throwable e) {
        }

        @Override
        public void onComplete() {
        }
    });
    assertTrue(disposable[0].isDisposed());
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) Test(org.junit.Test)

Example 9 with Observable

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

the class ObservableConcatTest method concatConcurrentWithInfinity.

@Test
public void concatConcurrentWithInfinity() {
    final TestObservable<String> w1 = new TestObservable<>("one", "two", "three");
    // This Observable will send "hello" MAX_VALUE time.
    final TestObservable<String> w2 = new TestObservable<>("hello", Integer.MAX_VALUE);
    Observer<String> observer = TestHelper.mockObserver();
    TestObservable<Observable<String>> observableOfObservables = new TestObservable<>(Observable.unsafeCreate(w1), Observable.unsafeCreate(w2));
    Observable<String> concatF = Observable.concat(Observable.unsafeCreate(observableOfObservables));
    concatF.take(50).subscribe(observer);
    // Wait for the thread to start up.
    try {
        w1.waitForThreadDone();
        w2.waitForThreadDone();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    InOrder inOrder = inOrder(observer);
    inOrder.verify(observer, times(1)).onNext("one");
    inOrder.verify(observer, times(1)).onNext("two");
    inOrder.verify(observer, times(1)).onNext("three");
    inOrder.verify(observer, times(47)).onNext("hello");
    verify(observer, times(1)).onComplete();
    verify(observer, never()).onError(any(Throwable.class));
}
Also used : InOrder(org.mockito.InOrder) Observable(io.reactivex.rxjava3.core.Observable) Test(org.junit.Test)

Example 10 with Observable

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

the class ObservableConcatWithMaybeTest method mainError.

@Test
public void mainError() {
    final TestObserver<Integer> to = new TestObserver<>();
    Observable.<Integer>error(new TestException()).concatWith(Maybe.<Integer>fromAction(new Action() {

        @Override
        public void run() throws Exception {
            to.onNext(100);
        }
    })).subscribe(to);
    to.assertFailure(TestException.class);
}
Also used : Action(io.reactivex.rxjava3.functions.Action) TestException(io.reactivex.rxjava3.exceptions.TestException) TestObserver(io.reactivex.rxjava3.observers.TestObserver) 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