Search in sources :

Example 11 with ObservableSource

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

the class ObservableRetryTest method repeatFloodNoSubscriptionError.

@Test
public void repeatFloodNoSubscriptionError() {
    List<Throwable> errors = TestHelper.trackPluginErrors();
    final TestException error = new TestException();
    try {
        final PublishSubject<Integer> source = PublishSubject.create();
        final PublishSubject<Integer> signaller = PublishSubject.create();
        for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
            TestObserver<Integer> to = source.take(1).map(new Function<Integer, Integer>() {

                @Override
                public Integer apply(Integer v) throws Exception {
                    throw error;
                }
            }).retryWhen(new Function<Observable<Throwable>, ObservableSource<Integer>>() {

                @Override
                public ObservableSource<Integer> apply(Observable<Throwable> v) throws Exception {
                    return signaller;
                }
            }).test();
            Runnable r1 = new Runnable() {

                @Override
                public void run() {
                    for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
                        source.onNext(1);
                    }
                }
            };
            Runnable r2 = new Runnable() {

                @Override
                public void run() {
                    for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
                        signaller.onNext(1);
                    }
                }
            };
            TestHelper.race(r1, r2);
            to.dispose();
        }
        if (!errors.isEmpty()) {
            for (Throwable e : errors) {
                e.printStackTrace();
            }
            fail(errors + "");
        }
    } finally {
        RxJavaPlugins.reset();
    }
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) TestException(io.reactivex.rxjava3.exceptions.TestException) Observable(io.reactivex.rxjava3.core.Observable) GroupedObservable(io.reactivex.rxjava3.observables.GroupedObservable) Test(org.junit.Test)

Example 12 with ObservableSource

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

the class ObservableTimeoutWithSelectorTest method timeoutSelectorWithTimeoutAndOnNextRaceCondition.

@Test
public void timeoutSelectorWithTimeoutAndOnNextRaceCondition() throws InterruptedException {
    // Thread 1                                    Thread 2
    // 
    // observer.onNext(1)
    // start timeout
    // unsubscribe timeout in thread 2          start to do some long-time work in "unsubscribe"
    // observer.onNext(2)
    // timeout.onNext(1)
    // "unsubscribe" done
    // 
    // 
    // In the above case, the timeout operator should ignore "timeout.onNext(1)"
    // since "observer" has already seen 2.
    final CountDownLatch observerReceivedTwo = new CountDownLatch(1);
    final CountDownLatch timeoutEmittedOne = new CountDownLatch(1);
    final CountDownLatch observerCompleted = new CountDownLatch(1);
    final CountDownLatch enteredTimeoutOne = new CountDownLatch(1);
    final AtomicBoolean latchTimeout = new AtomicBoolean(false);
    final Function<Integer, Observable<Integer>> timeoutFunc = new Function<Integer, Observable<Integer>>() {

        @Override
        public Observable<Integer> apply(Integer t1) {
            if (t1 == 1) {
                // Force "unsubscribe" run on another thread
                return Observable.unsafeCreate(new ObservableSource<Integer>() {

                    @Override
                    public void subscribe(Observer<? super Integer> observer) {
                        observer.onSubscribe(Disposable.empty());
                        enteredTimeoutOne.countDown();
                        // force the timeout message be sent after observer.onNext(2)
                        while (true) {
                            try {
                                if (!observerReceivedTwo.await(30, TimeUnit.SECONDS)) {
                                    // CountDownLatch timeout
                                    // There should be something wrong
                                    latchTimeout.set(true);
                                }
                                break;
                            } catch (InterruptedException e) {
                            // Since we just want to emulate a busy method,
                            // we ignore the interrupt signal from Scheduler.
                            }
                        }
                        observer.onNext(1);
                        timeoutEmittedOne.countDown();
                    }
                }).subscribeOn(Schedulers.newThread());
            } else {
                return PublishSubject.create();
            }
        }
    };
    final Observer<Integer> o = TestHelper.mockObserver();
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            observerReceivedTwo.countDown();
            return null;
        }
    }).when(o).onNext(2);
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            observerCompleted.countDown();
            return null;
        }
    }).when(o).onComplete();
    final TestObserver<Integer> to = new TestObserver<>(o);
    new Thread(new Runnable() {

        @Override
        public void run() {
            PublishSubject<Integer> source = PublishSubject.create();
            source.timeout(timeoutFunc, Observable.just(3)).subscribe(to);
            // start timeout
            source.onNext(1);
            try {
                if (!enteredTimeoutOne.await(30, TimeUnit.SECONDS)) {
                    latchTimeout.set(true);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            // disable timeout
            source.onNext(2);
            try {
                if (!timeoutEmittedOne.await(30, TimeUnit.SECONDS)) {
                    latchTimeout.set(true);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            source.onComplete();
        }
    }).start();
    if (!observerCompleted.await(30, TimeUnit.SECONDS)) {
        latchTimeout.set(true);
    }
    assertFalse("CoundDownLatch timeout", latchTimeout.get());
    InOrder inOrder = inOrder(o);
    inOrder.verify(o).onSubscribe((Disposable) notNull());
    inOrder.verify(o).onNext(1);
    inOrder.verify(o).onNext(2);
    inOrder.verify(o, never()).onNext(3);
    inOrder.verify(o).onComplete();
    inOrder.verifyNoMoreInteractions();
}
Also used : InOrder(org.mockito.InOrder) Observable(io.reactivex.rxjava3.core.Observable) TestObserver(io.reactivex.rxjava3.observers.TestObserver) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Observer(io.reactivex.rxjava3.core.Observer) TestObserver(io.reactivex.rxjava3.observers.TestObserver) Test(org.junit.Test)

Example 13 with ObservableSource

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

the class ObservableSwitchTest method switchWithSubsequenceError.

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

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

                @Override
                public void subscribe(Observer<? super String> observer) {
                    observer.onSubscribe(Disposable.empty());
                    publishNext(observer, 50, "one");
                    publishNext(observer, 100, "two");
                }
            }));
            publishNext(observer, 130, Observable.unsafeCreate(new ObservableSource<String>() {

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

                @Override
                public void subscribe(Observer<? super String> observer) {
                    observer.onSubscribe(Disposable.empty());
                    publishNext(observer, 50, "three");
                }
            }));
        }
    });
    Observable<String> sampled = Observable.switchOnNext(source);
    sampled.subscribe(observer);
    InOrder inOrder = inOrder(observer);
    scheduler.advanceTimeTo(90, TimeUnit.MILLISECONDS);
    inOrder.verify(observer, never()).onNext(anyString());
    verify(observer, never()).onComplete();
    verify(observer, never()).onError(any(Throwable.class));
    scheduler.advanceTimeTo(125, TimeUnit.MILLISECONDS);
    inOrder.verify(observer, times(1)).onNext("one");
    verify(observer, never()).onComplete();
    verify(observer, never()).onError(any(Throwable.class));
    scheduler.advanceTimeTo(250, TimeUnit.MILLISECONDS);
    inOrder.verify(observer, never()).onNext("three");
    verify(observer, never()).onComplete();
    verify(observer, times(1)).onError(any(TestException.class));
}
Also used : InOrder(org.mockito.InOrder) Observable(io.reactivex.rxjava3.core.Observable)

Example 14 with ObservableSource

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

the class ObservableSwitchTest method switchWithSubsequenceComplete.

@Test
public void switchWithSubsequenceComplete() {
    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, 50, Observable.unsafeCreate(new ObservableSource<String>() {

                @Override
                public void subscribe(Observer<? super String> innerObserver) {
                    innerObserver.onSubscribe(Disposable.empty());
                    publishNext(innerObserver, 50, "one");
                    publishNext(innerObserver, 100, "two");
                }
            }));
            publishNext(outerObserver, 130, Observable.unsafeCreate(new ObservableSource<String>() {

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

                @Override
                public void subscribe(Observer<? super String> innerObserver) {
                    innerObserver.onSubscribe(Disposable.empty());
                    publishNext(innerObserver, 50, "three");
                }
            }));
        }
    });
    Observable<String> sampled = Observable.switchOnNext(source);
    sampled.subscribe(observer);
    InOrder inOrder = inOrder(observer);
    scheduler.advanceTimeTo(90, TimeUnit.MILLISECONDS);
    inOrder.verify(observer, never()).onNext(anyString());
    verify(observer, never()).onComplete();
    verify(observer, never()).onError(any(Throwable.class));
    scheduler.advanceTimeTo(125, TimeUnit.MILLISECONDS);
    inOrder.verify(observer, times(1)).onNext("one");
    verify(observer, never()).onComplete();
    verify(observer, never()).onError(any(Throwable.class));
    scheduler.advanceTimeTo(250, TimeUnit.MILLISECONDS);
    inOrder.verify(observer, times(1)).onNext("three");
    verify(observer, never()).onComplete();
    verify(observer, never()).onError(any(Throwable.class));
}
Also used : InOrder(org.mockito.InOrder) Observable(io.reactivex.rxjava3.core.Observable)

Example 15 with ObservableSource

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

the class ObservableSwitchTest method switchWhenOuterCompleteBeforeInner.

@Test
public void switchWhenOuterCompleteBeforeInner() {
    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, 50, Observable.unsafeCreate(new ObservableSource<String>() {

                @Override
                public void subscribe(Observer<? super String> innerObserver) {
                    innerObserver.onSubscribe(Disposable.empty());
                    publishNext(innerObserver, 70, "one");
                    publishNext(innerObserver, 100, "two");
                    publishCompleted(innerObserver, 200);
                }
            }));
            publishCompleted(outerObserver, 60);
        }
    });
    Observable<String> sampled = Observable.switchOnNext(source);
    sampled.subscribe(observer);
    InOrder inOrder = inOrder(observer);
    scheduler.advanceTimeTo(350, TimeUnit.MILLISECONDS);
    inOrder.verify(observer, times(2)).onNext(anyString());
    inOrder.verify(observer, times(1)).onComplete();
}
Also used : InOrder(org.mockito.InOrder) 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