Search in sources :

Example 66 with Disposable

use of io.reactivex.rxjava3.disposables.Disposable 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 67 with Disposable

use of io.reactivex.rxjava3.disposables.Disposable 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 68 with Disposable

use of io.reactivex.rxjava3.disposables.Disposable 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 69 with Disposable

use of io.reactivex.rxjava3.disposables.Disposable in project RxJava by ReactiveX.

the class ObservableCreateTest method basicSerialized.

@Test
@SuppressUndeliverable
public void basicSerialized() {
    final Disposable d = Disposable.empty();
    Observable.<Integer>create(new ObservableOnSubscribe<Integer>() {

        @Override
        public void subscribe(ObservableEmitter<Integer> e) throws Exception {
            e = e.serialize();
            e.setDisposable(d);
            e.onNext(1);
            e.onNext(2);
            e.onNext(3);
            e.onComplete();
            e.onError(new TestException());
            e.onNext(4);
            e.onError(new TestException());
            e.onComplete();
        }
    }).test().assertResult(1, 2, 3);
    assertTrue(d.isDisposed());
}
Also used : Disposable(io.reactivex.rxjava3.disposables.Disposable) TestException(io.reactivex.rxjava3.exceptions.TestException) TestException(io.reactivex.rxjava3.exceptions.TestException) IOException(java.io.IOException) Test(org.junit.Test)

Example 70 with Disposable

use of io.reactivex.rxjava3.disposables.Disposable in project RxJava by ReactiveX.

the class ObservableCreateTest method basicWithCancellable.

@Test
@SuppressUndeliverable
public void basicWithCancellable() {
    final Disposable d1 = Disposable.empty();
    final Disposable d2 = Disposable.empty();
    Observable.<Integer>create(new ObservableOnSubscribe<Integer>() {

        @Override
        public void subscribe(ObservableEmitter<Integer> e) throws Exception {
            e.setDisposable(d1);
            e.setCancellable(new Cancellable() {

                @Override
                public void cancel() throws Exception {
                    d2.dispose();
                }
            });
            e.onNext(1);
            e.onNext(2);
            e.onNext(3);
            e.onComplete();
            e.onError(new TestException());
            e.onNext(4);
            e.onError(new TestException());
            e.onComplete();
        }
    }).test().assertResult(1, 2, 3);
    assertTrue(d1.isDisposed());
    assertTrue(d2.isDisposed());
}
Also used : Disposable(io.reactivex.rxjava3.disposables.Disposable) TestException(io.reactivex.rxjava3.exceptions.TestException) Cancellable(io.reactivex.rxjava3.functions.Cancellable) TestException(io.reactivex.rxjava3.exceptions.TestException) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)250 Disposable (io.reactivex.rxjava3.disposables.Disposable)219 TestException (io.reactivex.rxjava3.exceptions.TestException)74 RxJavaTest (io.reactivex.rxjava3.core.RxJavaTest)56 IOException (java.io.IOException)37 Scheduler (io.reactivex.rxjava3.core.Scheduler)36 EmptyDisposable (io.reactivex.rxjava3.internal.disposables.EmptyDisposable)35 Worker (io.reactivex.rxjava3.core.Scheduler.Worker)34 BooleanSubscription (io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription)32 TestSubscriber (io.reactivex.rxjava3.subscribers.TestSubscriber)22 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)22 TestObserver (io.reactivex.rxjava3.observers.TestObserver)18 SequentialDisposable (io.reactivex.rxjava3.internal.disposables.SequentialDisposable)15 QueueDisposable (io.reactivex.rxjava3.operators.QueueDisposable)13 CountingRunnable (io.reactivex.rxjava3.android.testutil.CountingRunnable)10 TrampolineScheduler (io.reactivex.rxjava3.internal.schedulers.TrampolineScheduler)9 Observable (io.reactivex.rxjava3.core.Observable)6 Observer (io.reactivex.rxjava3.core.Observer)6 ScalarDisposable (io.reactivex.rxjava3.internal.operators.observable.ObservableScalarXMap.ScalarDisposable)6 TestScheduler (io.reactivex.rxjava3.schedulers.TestScheduler)6