Search in sources :

Example 31 with ObservableSource

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

the class ObservableSwitchTest method switchIssue737.

@Test
public void switchIssue737() {
    // https://github.com/ReactiveX/RxJava/issues/737
    Observable<Observable<String>> source = Observable.unsafeCreate(new ObservableSource<Observable<String>>() {

        @Override
        public void subscribe(Observer<? super Observable<String>> outerObserver) {
            outerObserver.onSubscribe(Disposable.empty());
            publishNext(outerObserver, 0, Observable.unsafeCreate(new ObservableSource<String>() {

                @Override
                public void subscribe(Observer<? super String> innerObserver) {
                    innerObserver.onSubscribe(Disposable.empty());
                    publishNext(innerObserver, 10, "1-one");
                    publishNext(innerObserver, 20, "1-two");
                    // The following events will be ignored
                    publishNext(innerObserver, 30, "1-three");
                    publishCompleted(innerObserver, 40);
                }
            }));
            publishNext(outerObserver, 25, Observable.unsafeCreate(new ObservableSource<String>() {

                @Override
                public void subscribe(Observer<? super String> innerObserver) {
                    innerObserver.onSubscribe(Disposable.empty());
                    publishNext(innerObserver, 10, "2-one");
                    publishNext(innerObserver, 20, "2-two");
                    publishNext(innerObserver, 30, "2-three");
                    publishCompleted(innerObserver, 40);
                }
            }));
            publishCompleted(outerObserver, 30);
        }
    });
    Observable<String> sampled = Observable.switchOnNext(source);
    sampled.subscribe(observer);
    scheduler.advanceTimeTo(1000, TimeUnit.MILLISECONDS);
    InOrder inOrder = inOrder(observer);
    inOrder.verify(observer, times(1)).onNext("1-one");
    inOrder.verify(observer, times(1)).onNext("1-two");
    inOrder.verify(observer, times(1)).onNext("2-one");
    inOrder.verify(observer, times(1)).onNext("2-two");
    inOrder.verify(observer, times(1)).onNext("2-three");
    inOrder.verify(observer, times(1)).onComplete();
    inOrder.verifyNoMoreInteractions();
}
Also used : InOrder(org.mockito.InOrder) Observable(io.reactivex.rxjava3.core.Observable)

Example 32 with ObservableSource

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

the class ObservableSwitchTest method unsubscribe.

@Test
public void unsubscribe() {
    final AtomicBoolean isUnsubscribed = new AtomicBoolean();
    Observable.switchOnNext(Observable.unsafeCreate(new ObservableSource<Observable<Integer>>() {

        @Override
        public void subscribe(final Observer<? super Observable<Integer>> observer) {
            Disposable bs = Disposable.empty();
            observer.onSubscribe(bs);
            observer.onNext(Observable.just(1));
            isUnsubscribed.set(bs.isDisposed());
        }
    })).take(1).subscribe();
    assertTrue("Switch doesn't propagate 'unsubscribe'", isUnsubscribed.get());
}
Also used : Disposable(io.reactivex.rxjava3.disposables.Disposable) Observer(io.reactivex.rxjava3.core.Observer) Observable(io.reactivex.rxjava3.core.Observable)

Example 33 with ObservableSource

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

the class ObservableRetryTest method noCancelPreviousRetryUntil.

@Test
public void noCancelPreviousRetryUntil() {
    final AtomicInteger counter = new AtomicInteger();
    final AtomicInteger times = new AtomicInteger();
    Observable<Integer> source = Observable.defer(new Supplier<ObservableSource<Integer>>() {

        @Override
        public ObservableSource<Integer> get() throws Exception {
            if (times.getAndIncrement() < 4) {
                return Observable.error(new TestException());
            }
            return Observable.just(1);
        }
    }).doOnDispose(new Action() {

        @Override
        public void run() throws Exception {
            counter.getAndIncrement();
        }
    });
    source.retryUntil(new BooleanSupplier() {

        @Override
        public boolean getAsBoolean() throws Exception {
            return false;
        }
    }).test().assertResult(1);
    assertEquals(0, counter.get());
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) TestException(io.reactivex.rxjava3.exceptions.TestException) Test(org.junit.Test)

Example 34 with ObservableSource

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

the class ObservableRetryTest method sourceObservableCallsUnsubscribe.

@Test
public void sourceObservableCallsUnsubscribe() throws InterruptedException {
    final AtomicInteger subsCount = new AtomicInteger(0);
    final TestObserver<String> to = new TestObserver<>();
    ObservableSource<String> onSubscribe = new ObservableSource<String>() {

        @Override
        public void subscribe(Observer<? super String> observer) {
            BooleanSubscription bs = new BooleanSubscription();
            // https://github.com/ReactiveX/RxJava/issues/1024
            if (!bs.isCancelled()) {
                subsCount.incrementAndGet();
                observer.onError(new RuntimeException("failed"));
                // it unsubscribes the child directly
                // this simulates various error/completion scenarios that could occur
                // or just a source that proactively triggers cleanup
                // FIXME can't unsubscribe child
                // s.unsubscribe();
                bs.cancel();
            } else {
                observer.onError(new RuntimeException());
            }
        }
    };
    Observable.unsafeCreate(onSubscribe).retry(3).subscribe(to);
    // 1 + 3 retries
    assertEquals(4, subsCount.get());
}
Also used : BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) Observer(io.reactivex.rxjava3.core.Observer) Test(org.junit.Test)

Example 35 with ObservableSource

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

the class ObservableWindowWithStartEndObservableTest method startError.

@Test
public void startError() {
    PublishSubject<Integer> source = PublishSubject.create();
    PublishSubject<Integer> start = PublishSubject.create();
    final PublishSubject<Integer> end = PublishSubject.create();
    TestObserver<Integer> to = source.window(start, new Function<Integer, ObservableSource<Integer>>() {

        @Override
        public ObservableSource<Integer> apply(Integer v) throws Exception {
            return end;
        }
    }).flatMap(Functions.<Observable<Integer>>identity()).test();
    start.onError(new TestException());
    to.assertFailure(TestException.class);
    assertFalse("Source has observers!", source.hasObservers());
    assertFalse("Start has observers!", start.hasObservers());
    assertFalse("End has observers!", end.hasObservers());
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) TestException(io.reactivex.rxjava3.exceptions.TestException) IOException(java.io.IOException) Observable(io.reactivex.rxjava3.core.Observable)

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