Search in sources :

Example 41 with Observable

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

the class ObservableMergeTest method mergeErrors.

@Test
public void mergeErrors() {
    List<Throwable> errors = TestHelper.trackPluginErrors();
    try {
        Observable<Integer> source1 = Observable.error(new TestException("First"));
        Observable<Integer> source2 = Observable.error(new TestException("Second"));
        Observable.merge(source1, source2).to(TestHelper.<Integer>testConsumer()).assertFailureAndMessage(TestException.class, "First");
        assertTrue(errors.toString(), errors.isEmpty());
    } finally {
        RxJavaPlugins.reset();
    }
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException)

Example 42 with Observable

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

the class ObservableObserveOnTest method observeOnTheSameSchedulerTwice.

@Test
public void observeOnTheSameSchedulerTwice() {
    Scheduler scheduler = ImmediateThinScheduler.INSTANCE;
    Observable<Integer> o = Observable.just(1, 2, 3);
    Observable<Integer> o2 = o.observeOn(scheduler);
    Observer<Object> observer1 = TestHelper.mockObserver();
    Observer<Object> observer2 = TestHelper.mockObserver();
    InOrder inOrder1 = inOrder(observer1);
    InOrder inOrder2 = inOrder(observer2);
    o2.subscribe(observer1);
    o2.subscribe(observer2);
    inOrder1.verify(observer1, times(1)).onNext(1);
    inOrder1.verify(observer1, times(1)).onNext(2);
    inOrder1.verify(observer1, times(1)).onNext(3);
    inOrder1.verify(observer1, times(1)).onComplete();
    verify(observer1, never()).onError(any(Throwable.class));
    inOrder1.verifyNoMoreInteractions();
    inOrder2.verify(observer2, times(1)).onNext(1);
    inOrder2.verify(observer2, times(1)).onNext(2);
    inOrder2.verify(observer2, times(1)).onNext(3);
    inOrder2.verify(observer2, times(1)).onComplete();
    verify(observer2, never()).onError(any(Throwable.class));
    inOrder2.verifyNoMoreInteractions();
}
Also used : InOrder(org.mockito.InOrder) ImmediateThinScheduler(io.reactivex.rxjava3.internal.schedulers.ImmediateThinScheduler) DisposeTrackingScheduler(io.reactivex.rxjava3.internal.operators.flowable.FlowableObserveOnTest.DisposeTrackingScheduler) Test(org.junit.Test)

Example 43 with Observable

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

the class ObservableOnErrorResumeNextTest method functionThrowsError.

/**
 * Test that when a function throws an exception this is propagated through onError.
 */
@Test
public void functionThrowsError() {
    Subscription s = mock(Subscription.class);
    TestObservable w = new TestObservable(s, "one");
    Function<Throwable, Observable<String>> resume = new Function<Throwable, Observable<String>>() {

        @Override
        public Observable<String> apply(Throwable t1) {
            throw new RuntimeException("exception from function");
        }
    };
    Observable<String> o = Observable.unsafeCreate(w).onErrorResumeNext(resume);
    Observer<String> observer = TestHelper.mockObserver();
    o.subscribe(observer);
    try {
        w.t.join();
    } catch (InterruptedException e) {
        fail(e.getMessage());
    }
    // we should get the "one" value before the error
    verify(observer, times(1)).onNext("one");
    // we should have received an onError call on the Observer since the resume function threw an exception
    verify(observer, times(1)).onError(any(Throwable.class));
    verify(observer, times(0)).onComplete();
}
Also used : Function(io.reactivex.rxjava3.functions.Function) Subscription(org.reactivestreams.Subscription) Test(org.junit.Test)

Example 44 with Observable

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

the class ObservableOnErrorResumeNextTest method resumeNextWithAsyncExecution.

@Test
public void resumeNextWithAsyncExecution() {
    final AtomicReference<Throwable> receivedException = new AtomicReference<>();
    Subscription s = mock(Subscription.class);
    TestObservable w = new TestObservable(s, "one");
    Function<Throwable, Observable<String>> resume = new Function<Throwable, Observable<String>>() {

        @Override
        public Observable<String> apply(Throwable t1) {
            receivedException.set(t1);
            return Observable.just("twoResume", "threeResume");
        }
    };
    Observable<String> o = Observable.unsafeCreate(w).onErrorResumeNext(resume);
    Observer<String> observer = TestHelper.mockObserver();
    o.subscribe(observer);
    try {
        w.t.join();
    } catch (InterruptedException e) {
        fail(e.getMessage());
    }
    verify(observer, Mockito.never()).onError(any(Throwable.class));
    verify(observer, times(1)).onComplete();
    verify(observer, times(1)).onNext("one");
    verify(observer, Mockito.never()).onNext("two");
    verify(observer, Mockito.never()).onNext("three");
    verify(observer, times(1)).onNext("twoResume");
    verify(observer, times(1)).onNext("threeResume");
    assertNotNull(receivedException.get());
}
Also used : Function(io.reactivex.rxjava3.functions.Function) AtomicReference(java.util.concurrent.atomic.AtomicReference) Subscription(org.reactivestreams.Subscription) Test(org.junit.Test)

Example 45 with Observable

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

the class ObservableSkipLastTimedTest method skipLastTimedErrorBeforeTime.

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

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