Search in sources :

Example 36 with Observer

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

the class ObservableDelayTest method delayWithObservableDelayThrows.

@Test
public void delayWithObservableDelayThrows() {
    PublishSubject<Integer> source = PublishSubject.create();
    final PublishSubject<Integer> delay = PublishSubject.create();
    Function<Integer, Observable<Integer>> delayFunc = new Function<Integer, Observable<Integer>>() {

        @Override
        public Observable<Integer> apply(Integer t1) {
            return delay;
        }
    };
    Observer<Object> o = TestHelper.mockObserver();
    InOrder inOrder = inOrder(o);
    source.delay(delayFunc).subscribe(o);
    source.onNext(1);
    delay.onError(new TestException());
    inOrder.verify(o).onError(any(TestException.class));
    inOrder.verifyNoMoreInteractions();
    verify(o, never()).onNext(any());
    verify(o, never()).onComplete();
}
Also used : InOrder(org.mockito.InOrder) TestException(io.reactivex.rxjava3.exceptions.TestException) Observable(io.reactivex.rxjava3.core.Observable)

Example 37 with Observer

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

the class ObservableDelayTest method delayWithObservableAsTimed.

@Test
public void delayWithObservableAsTimed() {
    Observable<Long> source = Observable.interval(1L, TimeUnit.SECONDS, scheduler).take(3);
    final Observable<Long> delayer = Observable.timer(500L, TimeUnit.MILLISECONDS, scheduler);
    Function<Long, Observable<Long>> delayFunc = new Function<Long, Observable<Long>>() {

        @Override
        public Observable<Long> apply(Long t1) {
            return delayer;
        }
    };
    Observable<Long> delayed = source.delay(delayFunc);
    delayed.subscribe(observer);
    InOrder inOrder = inOrder(observer);
    scheduler.advanceTimeTo(1499L, TimeUnit.MILLISECONDS);
    verify(observer, never()).onNext(anyLong());
    verify(observer, never()).onComplete();
    verify(observer, never()).onError(any(Throwable.class));
    scheduler.advanceTimeTo(1500L, TimeUnit.MILLISECONDS);
    inOrder.verify(observer, times(1)).onNext(0L);
    inOrder.verify(observer, never()).onNext(anyLong());
    verify(observer, never()).onComplete();
    verify(observer, never()).onError(any(Throwable.class));
    scheduler.advanceTimeTo(2400L, TimeUnit.MILLISECONDS);
    inOrder.verify(observer, never()).onNext(anyLong());
    verify(observer, never()).onComplete();
    verify(observer, never()).onError(any(Throwable.class));
    scheduler.advanceTimeTo(2500L, TimeUnit.MILLISECONDS);
    inOrder.verify(observer, times(1)).onNext(1L);
    inOrder.verify(observer, never()).onNext(anyLong());
    verify(observer, never()).onComplete();
    verify(observer, never()).onError(any(Throwable.class));
    scheduler.advanceTimeTo(3400L, TimeUnit.MILLISECONDS);
    inOrder.verify(observer, never()).onNext(anyLong());
    verify(observer, never()).onComplete();
    verify(observer, never()).onError(any(Throwable.class));
    scheduler.advanceTimeTo(3500L, TimeUnit.MILLISECONDS);
    inOrder.verify(observer, times(1)).onNext(2L);
    verify(observer, times(1)).onComplete();
    verify(observer, never()).onError(any(Throwable.class));
}
Also used : InOrder(org.mockito.InOrder) Observable(io.reactivex.rxjava3.core.Observable)

Example 38 with Observer

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

the class ObservableDelayTest method delayWithObservableDelayFunctionThrows.

@Test
public void delayWithObservableDelayFunctionThrows() {
    PublishSubject<Integer> source = PublishSubject.create();
    Function<Integer, Observable<Integer>> delayFunc = new Function<Integer, Observable<Integer>>() {

        @Override
        public Observable<Integer> apply(Integer t1) {
            throw new TestException();
        }
    };
    Observer<Object> o = TestHelper.mockObserver();
    InOrder inOrder = inOrder(o);
    source.delay(delayFunc).subscribe(o);
    source.onNext(1);
    inOrder.verify(o).onError(any(TestException.class));
    inOrder.verifyNoMoreInteractions();
    verify(o, never()).onNext(any());
    verify(o, never()).onComplete();
}
Also used : InOrder(org.mockito.InOrder) TestException(io.reactivex.rxjava3.exceptions.TestException) Observable(io.reactivex.rxjava3.core.Observable)

Example 39 with Observer

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

the class ObservableFromSupplierTest method shouldNotDeliverResultIfSubscriberUnsubscribedBeforeEmission.

@SuppressWarnings("unchecked")
@Test
public void shouldNotDeliverResultIfSubscriberUnsubscribedBeforeEmission() throws Throwable {
    Supplier<String> func = mock(Supplier.class);
    final CountDownLatch funcLatch = new CountDownLatch(1);
    final CountDownLatch observerLatch = new CountDownLatch(1);
    when(func.get()).thenAnswer(new Answer<String>() {

        @Override
        public String answer(InvocationOnMock invocation) throws Throwable {
            observerLatch.countDown();
            try {
                funcLatch.await();
            } catch (InterruptedException e) {
                // It's okay, unsubscription causes Thread interruption
                // Restoring interruption status of the Thread
                Thread.currentThread().interrupt();
            }
            return "should_not_be_delivered";
        }
    });
    Observable<String> fromSupplierObservable = Observable.fromSupplier(func);
    Observer<Object> observer = TestHelper.mockObserver();
    TestObserver<String> outer = new TestObserver<>(observer);
    fromSupplierObservable.subscribeOn(Schedulers.computation()).subscribe(outer);
    // Wait until func will be invoked
    observerLatch.await();
    // Unsubscribing before emission
    outer.dispose();
    // Emitting result
    funcLatch.countDown();
    // func must be invoked
    verify(func).get();
    // Observer must not be notified at all
    verify(observer).onSubscribe(any(Disposable.class));
    verifyNoMoreInteractions(observer);
}
Also used : Disposable(io.reactivex.rxjava3.disposables.Disposable) CountDownLatch(java.util.concurrent.CountDownLatch) TestObserver(io.reactivex.rxjava3.observers.TestObserver) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Test(org.junit.Test)

Example 40 with Observer

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

the class ObservableBufferTest method openCloseBadClose.

@Test
public void openCloseBadClose() {
    List<Throwable> errors = TestHelper.trackPluginErrors();
    try {
        Observable.never().buffer(Observable.just(1).concatWith(Observable.<Integer>never()), Functions.justFunction(new Observable<Object>() {

            @Override
            protected void subscribeActual(Observer<? super Object> observer) {
                assertFalse(((Disposable) observer).isDisposed());
                Disposable bs1 = Disposable.empty();
                Disposable bs2 = Disposable.empty();
                observer.onSubscribe(bs1);
                assertFalse(bs1.isDisposed());
                assertFalse(bs2.isDisposed());
                observer.onSubscribe(bs2);
                assertFalse(bs1.isDisposed());
                assertTrue(bs2.isDisposed());
                observer.onError(new IOException());
                assertTrue(((Disposable) observer).isDisposed());
                observer.onComplete();
                observer.onNext(1);
                observer.onError(new TestException());
            }
        })).test().assertFailure(IOException.class);
        TestHelper.assertError(errors, 0, ProtocolViolationException.class);
        TestHelper.assertUndeliverable(errors, 1, TestException.class);
    } finally {
        RxJavaPlugins.reset();
    }
}
Also used : Observer(io.reactivex.rxjava3.core.Observer) BufferExactObserver(io.reactivex.rxjava3.internal.operators.observable.ObservableBuffer.BufferExactObserver) IOException(java.io.IOException) Observable(io.reactivex.rxjava3.core.Observable)

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