Search in sources :

Example 91 with Observer

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

the class ObservableTakeUntilPredicateTest method functionThrows.

@Test
public void functionThrows() {
    Observer<Object> o = TestHelper.mockObserver();
    Predicate<Integer> predicate = (new Predicate<Integer>() {

        @Override
        public boolean test(Integer t1) {
            throw new TestException("Forced failure");
        }
    });
    Observable.just(1, 2, 3).takeUntil(predicate).subscribe(o);
    verify(o).onNext(1);
    verify(o, never()).onNext(2);
    verify(o, never()).onNext(3);
    verify(o).onError(any(TestException.class));
    verify(o, never()).onComplete();
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) Test(org.junit.Test)

Example 92 with Observer

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

the class ObservableTakeUntilPredicateTest method sourceThrows.

@Test
public void sourceThrows() {
    Observer<Object> o = TestHelper.mockObserver();
    Observable.just(1).concatWith(Observable.<Integer>error(new TestException())).concatWith(Observable.just(2)).takeUntil(new Predicate<Integer>() {

        @Override
        public boolean test(Integer v) {
            return false;
        }
    }).subscribe(o);
    verify(o).onNext(1);
    verify(o, never()).onNext(2);
    verify(o).onError(any(TestException.class));
    verify(o, never()).onComplete();
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) Test(org.junit.Test)

Example 93 with Observer

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

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

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

Aggregations

Test (org.junit.Test)139 TestException (io.reactivex.rxjava3.exceptions.TestException)88 InOrder (org.mockito.InOrder)75 Observable (io.reactivex.rxjava3.core.Observable)49 Disposable (io.reactivex.rxjava3.disposables.Disposable)49 TestObserver (io.reactivex.rxjava3.observers.TestObserver)37 IOException (java.io.IOException)29 Observer (io.reactivex.rxjava3.core.Observer)23 TestScheduler (io.reactivex.rxjava3.schedulers.TestScheduler)22 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)17 RxJavaTest (io.reactivex.rxjava3.core.RxJavaTest)11 Function (io.reactivex.rxjava3.functions.Function)10 SequentialDisposable (io.reactivex.rxjava3.internal.disposables.SequentialDisposable)6 ConnectableObservable (io.reactivex.rxjava3.observables.ConnectableObservable)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 io.reactivex.rxjava3.core (io.reactivex.rxjava3.core)5 BooleanSubscription (io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription)5 io.reactivex.rxjava3.subjects (io.reactivex.rxjava3.subjects)5 RxMethod (io.reactivex.rxjava3.validators.BaseTypeParser.RxMethod)5 InvocationOnMock (org.mockito.invocation.InvocationOnMock)5