Search in sources :

Example 26 with ObservableSource

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

the class ObservableGroupByTest method groupByUnsubscribe.

@Test
public void groupByUnsubscribe() {
    final Disposable upstream = mock(Disposable.class);
    Observable<Integer> o = Observable.unsafeCreate(new ObservableSource<Integer>() {

        @Override
        public void subscribe(Observer<? super Integer> observer) {
            observer.onSubscribe(upstream);
        }
    });
    TestObserver<Object> to = new TestObserver<>();
    o.groupBy(new Function<Integer, Integer>() {

        @Override
        public Integer apply(Integer integer) {
            return null;
        }
    }).subscribe(to);
    to.dispose();
    verify(upstream).dispose();
}
Also used : Disposable(io.reactivex.rxjava3.disposables.Disposable) Test(org.junit.Test)

Example 27 with ObservableSource

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

the class SerializedObserverTest method infinite.

private static Observable<String> infinite(final AtomicInteger produced) {
    return Observable.unsafeCreate(new ObservableSource<String>() {

        @Override
        public void subscribe(Observer<? super String> observer) {
            Disposable bs = Disposable.empty();
            observer.onSubscribe(bs);
            while (!bs.isDisposed()) {
                observer.onNext("onNext");
                produced.incrementAndGet();
            }
        }
    }).subscribeOn(Schedulers.newThread());
}
Also used : Disposable(io.reactivex.rxjava3.disposables.Disposable)

Example 28 with ObservableSource

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

the class ObservableSubscribeOnTest method unsubscribeInfiniteStream.

@Test
public void unsubscribeInfiniteStream() throws InterruptedException {
    TestObserver<Integer> to = new TestObserver<>();
    final AtomicInteger count = new AtomicInteger();
    Observable.unsafeCreate(new ObservableSource<Integer>() {

        @Override
        public void subscribe(Observer<? super Integer> sub) {
            Disposable d = Disposable.empty();
            sub.onSubscribe(d);
            for (int i = 1; !d.isDisposed(); i++) {
                count.incrementAndGet();
                sub.onNext(i);
            }
        }
    }).subscribeOn(Schedulers.newThread()).take(10).subscribe(to);
    to.awaitDone(1000, TimeUnit.MILLISECONDS);
    to.dispose();
    // give time for the loop to continue
    Thread.sleep(200);
    to.assertValues(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
    assertEquals(10, count.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestObserver(io.reactivex.rxjava3.observers.TestObserver) TestObserver(io.reactivex.rxjava3.observers.TestObserver) Test(org.junit.Test)

Example 29 with ObservableSource

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

the class ObservableSwitchTest method switchWithError.

@Test
public void switchWithError() {
    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(final Observer<? super String> innerObserver) {
                    innerObserver.onSubscribe(Disposable.empty());
                    publishNext(innerObserver, 50, "one");
                    publishNext(innerObserver, 100, "two");
                }
            }));
            publishNext(outerObserver, 200, Observable.unsafeCreate(new ObservableSource<String>() {

                @Override
                public void subscribe(Observer<? super String> innerObserver) {
                    innerObserver.onSubscribe(Disposable.empty());
                    publishNext(innerObserver, 0, "three");
                    publishNext(innerObserver, 100, "four");
                }
            }));
            publishError(outerObserver, 250, new TestException());
        }
    });
    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(175, TimeUnit.MILLISECONDS);
    inOrder.verify(observer, times(1)).onNext("two");
    verify(observer, never()).onComplete();
    verify(observer, never()).onError(any(Throwable.class));
    scheduler.advanceTimeTo(225, TimeUnit.MILLISECONDS);
    inOrder.verify(observer, times(1)).onNext("three");
    verify(observer, never()).onComplete();
    verify(observer, never()).onError(any(Throwable.class));
    scheduler.advanceTimeTo(350, TimeUnit.MILLISECONDS);
    inOrder.verify(observer, never()).onNext(anyString());
    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 30 with ObservableSource

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

the class ObservableSwitchTest method switchWithComplete.

@Test
public void switchWithComplete() {
    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(final Observer<? super String> innerObserver) {
                    innerObserver.onSubscribe(Disposable.empty());
                    publishNext(innerObserver, 60, "one");
                    publishNext(innerObserver, 100, "two");
                }
            }));
            publishNext(outerObserver, 200, Observable.unsafeCreate(new ObservableSource<String>() {

                @Override
                public void subscribe(final Observer<? super String> innerObserver) {
                    innerObserver.onSubscribe(Disposable.empty());
                    publishNext(innerObserver, 0, "three");
                    publishNext(innerObserver, 100, "four");
                }
            }));
            publishCompleted(outerObserver, 250);
        }
    });
    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(175, TimeUnit.MILLISECONDS);
    inOrder.verify(observer, times(1)).onNext("two");
    verify(observer, never()).onComplete();
    verify(observer, never()).onError(any(Throwable.class));
    scheduler.advanceTimeTo(225, TimeUnit.MILLISECONDS);
    inOrder.verify(observer, times(1)).onNext("three");
    verify(observer, never()).onComplete();
    verify(observer, never()).onError(any(Throwable.class));
    scheduler.advanceTimeTo(350, TimeUnit.MILLISECONDS);
    inOrder.verify(observer, times(1)).onNext("four");
    verify(observer, never()).onComplete();
    verify(observer, never()).onError(any(Throwable.class));
}
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