Search in sources :

Example 16 with Observer

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

the class AbstractSchedulerTests method subscribeOnNestedConcurrency.

@Test
public final void subscribeOnNestedConcurrency() throws InterruptedException {
    final Scheduler scheduler = getScheduler();
    Flowable<String> f = Flowable.fromArray("one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten").flatMap(new Function<String, Flowable<String>>() {

        @Override
        public Flowable<String> apply(final String v) {
            return Flowable.unsafeCreate(new Publisher<String>() {

                @Override
                public void subscribe(Subscriber<? super String> subscriber) {
                    subscriber.onSubscribe(new BooleanSubscription());
                    subscriber.onNext("value_after_map-" + v);
                    subscriber.onComplete();
                }
            }).subscribeOn(scheduler);
        }
    });
    ConcurrentObserverValidator<String> observer = new ConcurrentObserverValidator<>();
    f.subscribe(observer);
    if (!observer.completed.await(3000, TimeUnit.MILLISECONDS)) {
        fail("timed out");
    }
    if (observer.error.get() != null) {
        observer.error.get().printStackTrace();
        fail("Error: " + observer.error.get().getMessage());
    }
}
Also used : TrampolineScheduler(io.reactivex.rxjava3.internal.schedulers.TrampolineScheduler) Test(org.junit.Test)

Example 17 with Observer

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

the class RxJavaPluginsTest method observableStart.

@SuppressWarnings("rawtypes")
@Test
public void observableStart() {
    try {
        RxJavaPlugins.setOnObservableSubscribe(new BiFunction<Observable, Observer, Observer>() {

            @Override
            public Observer apply(Observable o, final Observer t) {
                return new Observer() {

                    @Override
                    public void onSubscribe(Disposable d) {
                        t.onSubscribe(d);
                    }

                    @SuppressWarnings("unchecked")
                    @Override
                    public void onNext(Object value) {
                        t.onNext((Integer) value - 9);
                    }

                    @Override
                    public void onError(Throwable e) {
                        t.onError(e);
                    }

                    @Override
                    public void onComplete() {
                        t.onComplete();
                    }
                };
            }
        });
        Observable.range(10, 3).test().assertValues(1, 2, 3).assertNoErrors().assertComplete();
    } finally {
        RxJavaPlugins.reset();
    }
    // make sure the reset worked
    Observable.range(10, 3).test().assertValues(10, 11, 12).assertNoErrors().assertComplete();
}
Also used : Observer(io.reactivex.rxjava3.core.Observer) Observable(io.reactivex.rxjava3.core.Observable) ConnectableObservable(io.reactivex.rxjava3.observables.ConnectableObservable) Test(org.junit.Test)

Example 18 with Observer

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

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

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

Aggregations

Test (org.junit.Test)139 TestException (io.reactivex.rxjava3.exceptions.TestException)88 InOrder (org.mockito.InOrder)75 Observable (io.reactivex.rxjava3.core.Observable)49 Disposable (io.reactivex.rxjava3.disposables.Disposable)49 TestObserver (io.reactivex.rxjava3.observers.TestObserver)37 IOException (java.io.IOException)29 Observer (io.reactivex.rxjava3.core.Observer)23 TestScheduler (io.reactivex.rxjava3.schedulers.TestScheduler)22 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)17 RxJavaTest (io.reactivex.rxjava3.core.RxJavaTest)11 Function (io.reactivex.rxjava3.functions.Function)10 SequentialDisposable (io.reactivex.rxjava3.internal.disposables.SequentialDisposable)6 ConnectableObservable (io.reactivex.rxjava3.observables.ConnectableObservable)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 io.reactivex.rxjava3.core (io.reactivex.rxjava3.core)5 BooleanSubscription (io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription)5 io.reactivex.rxjava3.subjects (io.reactivex.rxjava3.subjects)5 RxMethod (io.reactivex.rxjava3.validators.BaseTypeParser.RxMethod)5 InvocationOnMock (org.mockito.invocation.InvocationOnMock)5