Search in sources :

Example 1 with ObservableSource

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

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

the class ObservableDoOnEachTest method onCompleteAfterCrash.

@Test
public void onCompleteAfterCrash() {
    List<Throwable> errors = TestHelper.trackPluginErrors();
    try {
        Observable.wrap(new ObservableSource<Object>() {

            @Override
            public void subscribe(Observer<? super Object> observer) {
                observer.onSubscribe(Disposable.empty());
                observer.onComplete();
            }
        }).doAfterTerminate(new Action() {

            @Override
            public void run() throws Exception {
                throw new IOException();
            }
        }).test().assertResult();
        TestHelper.assertUndeliverable(errors, 0, IOException.class);
    } finally {
        RxJavaPlugins.reset();
    }
}
Also used : TestObserver(io.reactivex.rxjava3.observers.TestObserver) IOException(java.io.IOException) IOException(java.io.IOException)

Example 3 with ObservableSource

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

the class ObservableDoOnEachTest method onCompleteCrash.

@Test
public void onCompleteCrash() {
    Observable.wrap(new ObservableSource<Object>() {

        @Override
        public void subscribe(Observer<? super Object> observer) {
            observer.onSubscribe(Disposable.empty());
            observer.onComplete();
        }
    }).doOnComplete(new Action() {

        @Override
        public void run() throws Exception {
            throw new IOException();
        }
    }).test().assertFailure(IOException.class);
}
Also used : TestObserver(io.reactivex.rxjava3.observers.TestObserver) IOException(java.io.IOException) IOException(java.io.IOException)

Example 4 with ObservableSource

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

the class ObservableConcatMapSchedulerTest method concatArray.

@SuppressWarnings("unchecked")
@Test
public void concatArray() throws Exception {
    for (int i = 2; i < 10; i++) {
        Observable<Integer>[] obs = new Observable[i];
        Arrays.fill(obs, Observable.just(1));
        Integer[] expected = new Integer[i];
        Arrays.fill(expected, 1);
        Method m = Observable.class.getMethod("concatArray", ObservableSource[].class);
        TestObserver<Integer> to = TestObserver.create();
        ((Observable<Integer>) m.invoke(null, new Object[] { obs })).subscribe(to);
        to.assertValues(expected);
        to.assertNoErrors();
        to.assertComplete();
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Method(java.lang.reflect.Method) Observable(io.reactivex.rxjava3.core.Observable) Test(org.junit.Test)

Example 5 with ObservableSource

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

the class ObservableDebounceTest method debounceWithError.

@Test
public void debounceWithError() {
    Observable<String> source = Observable.unsafeCreate(new ObservableSource<String>() {

        @Override
        public void subscribe(Observer<? super String> observer) {
            observer.onSubscribe(Disposable.empty());
            Exception error = new TestException();
            // Should be published since "two" will arrive after the timeout expires.
            publishNext(observer, 100, "one");
            // Should be skipped since onError will arrive before the timeout expires.
            publishNext(observer, 600, "two");
            // Should be published as soon as the timeout expires.
            publishError(observer, 700, error);
        }
    });
    Observable<String> sampled = source.debounce(400, TimeUnit.MILLISECONDS, scheduler);
    sampled.subscribe(observer);
    scheduler.advanceTimeTo(0, TimeUnit.MILLISECONDS);
    InOrder inOrder = inOrder(observer);
    // 100 + 400 means it triggers at 500
    scheduler.advanceTimeTo(500, TimeUnit.MILLISECONDS);
    inOrder.verify(observer).onNext("one");
    scheduler.advanceTimeTo(701, TimeUnit.MILLISECONDS);
    inOrder.verify(observer).onError(any(TestException.class));
    inOrder.verifyNoMoreInteractions();
}
Also used : InOrder(org.mockito.InOrder) TestException(io.reactivex.rxjava3.exceptions.TestException) TestException(io.reactivex.rxjava3.exceptions.TestException)

Aggregations

Test (org.junit.Test)21 Observable (io.reactivex.rxjava3.core.Observable)20 TestException (io.reactivex.rxjava3.exceptions.TestException)12 InOrder (org.mockito.InOrder)12 TestObserver (io.reactivex.rxjava3.observers.TestObserver)9 IOException (java.io.IOException)8 Observer (io.reactivex.rxjava3.core.Observer)5 RxMethod (io.reactivex.rxjava3.validators.BaseTypeParser.RxMethod)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 ObservableSource (io.reactivex.rxjava3.core.ObservableSource)4 Function (io.reactivex.rxjava3.functions.Function)4 Intent (android.content.Intent)3 Disposable (io.reactivex.rxjava3.disposables.Disposable)3 File (java.io.File)3 View (android.view.View)2 RxPermissionTransformer (com.xxf.permission.transformer.RxPermissionTransformer)2 Supplier (io.reactivex.rxjava3.functions.Supplier)2 GroupedObservable (io.reactivex.rxjava3.observables.GroupedObservable)2 ArrayList (java.util.ArrayList)2 Callable (java.util.concurrent.Callable)2