Search in sources :

Example 86 with Observable

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

the class ObservableTakeTimedTest method takeTimed.

@Test
public void takeTimed() {
    TestScheduler scheduler = new TestScheduler();
    PublishSubject<Integer> source = PublishSubject.create();
    Observable<Integer> result = source.take(1, TimeUnit.SECONDS, scheduler);
    Observer<Object> o = TestHelper.mockObserver();
    result.subscribe(o);
    source.onNext(1);
    source.onNext(2);
    source.onNext(3);
    scheduler.advanceTimeBy(1, TimeUnit.SECONDS);
    source.onNext(4);
    InOrder inOrder = inOrder(o);
    inOrder.verify(o).onNext(1);
    inOrder.verify(o).onNext(2);
    inOrder.verify(o).onNext(3);
    inOrder.verify(o).onComplete();
    inOrder.verifyNoMoreInteractions();
    verify(o, never()).onNext(4);
    verify(o, never()).onError(any(Throwable.class));
}
Also used : InOrder(org.mockito.InOrder) TestScheduler(io.reactivex.rxjava3.schedulers.TestScheduler) Test(org.junit.Test)

Example 87 with Observable

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

the class ObservableTakeTimedTest method takeTimedErrorAfterTime.

@Test
public void takeTimedErrorAfterTime() {
    TestScheduler scheduler = new TestScheduler();
    PublishSubject<Integer> source = PublishSubject.create();
    Observable<Integer> result = source.take(1, TimeUnit.SECONDS, scheduler);
    Observer<Object> o = TestHelper.mockObserver();
    result.subscribe(o);
    source.onNext(1);
    source.onNext(2);
    source.onNext(3);
    scheduler.advanceTimeBy(1, TimeUnit.SECONDS);
    source.onNext(4);
    source.onError(new TestException());
    InOrder inOrder = inOrder(o);
    inOrder.verify(o).onNext(1);
    inOrder.verify(o).onNext(2);
    inOrder.verify(o).onNext(3);
    inOrder.verify(o).onComplete();
    inOrder.verifyNoMoreInteractions();
    verify(o, never()).onNext(4);
    verify(o, never()).onError(any(TestException.class));
}
Also used : InOrder(org.mockito.InOrder) TestException(io.reactivex.rxjava3.exceptions.TestException) TestScheduler(io.reactivex.rxjava3.schedulers.TestScheduler) Test(org.junit.Test)

Example 88 with Observable

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

the class ObservableTakeUntilTest method takeUntilOtherCompleted.

/**
 * If the 'other' onCompletes then we unsubscribe from the source and onComplete.
 */
@Test
public void takeUntilOtherCompleted() {
    Disposable sSource = mock(Disposable.class);
    Disposable sOther = mock(Disposable.class);
    TestObservable source = new TestObservable(sSource);
    TestObservable other = new TestObservable(sOther);
    Observer<String> result = TestHelper.mockObserver();
    Observable<String> stringObservable = Observable.unsafeCreate(source).takeUntil(Observable.unsafeCreate(other));
    stringObservable.subscribe(result);
    source.sendOnNext("one");
    source.sendOnNext("two");
    other.sendOnCompleted();
    source.sendOnNext("three");
    verify(result, times(1)).onNext("one");
    verify(result, times(1)).onNext("two");
    verify(result, times(0)).onNext("three");
    verify(result, times(1)).onComplete();
    verify(sSource, times(1)).dispose();
    // no longer disposing itself on terminal events
    verify(sOther, never()).dispose();
}
Also used : Disposable(io.reactivex.rxjava3.disposables.Disposable) Test(org.junit.Test)

Example 89 with Observable

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

the class ObservableTakeUntilTest method takeUntilSourceCompleted.

@Test
public void takeUntilSourceCompleted() {
    Disposable sSource = mock(Disposable.class);
    Disposable sOther = mock(Disposable.class);
    TestObservable source = new TestObservable(sSource);
    TestObservable other = new TestObservable(sOther);
    Observer<String> result = TestHelper.mockObserver();
    Observable<String> stringObservable = Observable.unsafeCreate(source).takeUntil(Observable.unsafeCreate(other));
    stringObservable.subscribe(result);
    source.sendOnNext("one");
    source.sendOnNext("two");
    source.sendOnCompleted();
    verify(result, times(1)).onNext("one");
    verify(result, times(1)).onNext("two");
    // no longer disposing itself on terminal events
    verify(sSource, never()).dispose();
    verify(sOther, times(1)).dispose();
}
Also used : Disposable(io.reactivex.rxjava3.disposables.Disposable) Test(org.junit.Test)

Example 90 with Observable

use of io.reactivex.rxjava3.core.Observable 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)

Aggregations

Test (org.junit.Test)132 TestException (io.reactivex.rxjava3.exceptions.TestException)109 Observable (io.reactivex.rxjava3.core.Observable)83 InOrder (org.mockito.InOrder)60 TestObserver (io.reactivex.rxjava3.observers.TestObserver)38 Disposable (io.reactivex.rxjava3.disposables.Disposable)35 IOException (java.io.IOException)25 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)25 RxJavaTest (io.reactivex.rxjava3.core.RxJavaTest)22 Observer (io.reactivex.rxjava3.core.Observer)19 TestScheduler (io.reactivex.rxjava3.schedulers.TestScheduler)17 Function (io.reactivex.rxjava3.functions.Function)10 List (java.util.List)8 TimeUnit (java.util.concurrent.TimeUnit)8 io.reactivex.rxjava3.core (io.reactivex.rxjava3.core)7 ConnectableObservable (io.reactivex.rxjava3.observables.ConnectableObservable)6 RxMethod (io.reactivex.rxjava3.validators.BaseTypeParser.RxMethod)6 Collections (java.util.Collections)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 Assert (org.junit.Assert)6