Search in sources :

Example 41 with TestObserver

use of io.reactivex.rxjava3.observers.TestObserver 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 42 with TestObserver

use of io.reactivex.rxjava3.observers.TestObserver in project RxJava by ReactiveX.

the class ObservableReplayEagerTruncateTest method disposeNoNeedForResetTimeAndSIzeBound.

@Test
public void disposeNoNeedForResetTimeAndSIzeBound() {
    PublishSubject<Integer> ps = PublishSubject.create();
    ConnectableObservable<Integer> co = ps.replay(10, 10, TimeUnit.MINUTES, Schedulers.single(), true);
    TestObserver<Integer> to = co.test();
    Disposable d = co.connect();
    ps.onNext(1);
    d.dispose();
    to = co.test();
    to.assertEmpty();
    co.connect();
    to.assertEmpty();
    ps.onNext(2);
    to.assertValuesOnly(2);
}
Also used : Disposable(io.reactivex.rxjava3.disposables.Disposable)

Example 43 with TestObserver

use of io.reactivex.rxjava3.observers.TestObserver in project RxJava by ReactiveX.

the class ObservableResourceWrapperTest method onErrorDisposes.

@Test
public void onErrorDisposes() {
    TestObserver<Object> to = new TestObserver<>();
    ObserverResourceWrapper<Object> orw = new ObserverResourceWrapper<>(to);
    Disposable d = Disposable.empty();
    Disposable d1 = Disposable.empty();
    orw.setResource(d1);
    orw.onSubscribe(d);
    orw.onError(new TestException());
    assertTrue(d1.isDisposed());
    to.assertFailure(TestException.class);
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) TestObserver(io.reactivex.rxjava3.observers.TestObserver) RxJavaTest(io.reactivex.rxjava3.core.RxJavaTest) Test(org.junit.Test)

Example 44 with TestObserver

use of io.reactivex.rxjava3.observers.TestObserver in project RxJava by ReactiveX.

the class ObservableMergeWithMaybeTest method cancelMainOnOtherError.

@Test
public void cancelMainOnOtherError() {
    PublishSubject<Integer> ps = PublishSubject.create();
    MaybeSubject<Integer> ms = MaybeSubject.create();
    TestObserver<Integer> to = ps.mergeWith(ms).test();
    assertTrue(ps.hasObservers());
    assertTrue(ms.hasObservers());
    ms.onError(new TestException());
    to.assertFailure(TestException.class);
    assertFalse("main has observers!", ps.hasObservers());
    assertFalse("other has observers", ms.hasObservers());
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) Test(org.junit.Test)

Example 45 with TestObserver

use of io.reactivex.rxjava3.observers.TestObserver in project RxJava by ReactiveX.

the class ObservableMergeWithSingleTest method cancelOtherOnMainError.

@Test
public void cancelOtherOnMainError() {
    PublishSubject<Integer> ps = PublishSubject.create();
    SingleSubject<Integer> ss = SingleSubject.create();
    TestObserver<Integer> to = ps.mergeWith(ss).test();
    assertTrue(ps.hasObservers());
    assertTrue(ss.hasObservers());
    ps.onError(new TestException());
    to.assertFailure(TestException.class);
    assertFalse("main has observers!", ps.hasObservers());
    assertFalse("other has observers", ss.hasObservers());
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)225 TestException (io.reactivex.rxjava3.exceptions.TestException)169 TestObserver (io.reactivex.rxjava3.observers.TestObserver)90 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)35 Disposable (io.reactivex.rxjava3.disposables.Disposable)34 CompletableSubject (io.reactivex.rxjava3.subjects.CompletableSubject)27 TargetApi (android.annotation.TargetApi)21 Observable (io.reactivex.rxjava3.core.Observable)19 TestScheduler (io.reactivex.rxjava3.schedulers.TestScheduler)19 IOException (java.io.IOException)19 Matchers.anyString (org.mockito.Matchers.anyString)16 RxJavaTest (io.reactivex.rxjava3.core.RxJavaTest)13 Action (io.reactivex.rxjava3.functions.Action)10 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 InOrder (org.mockito.InOrder)7 io.reactivex.rxjava3.core (io.reactivex.rxjava3.core)6 RxJavaPlugins (io.reactivex.rxjava3.plugins.RxJavaPlugins)6 List (java.util.List)6 Assert (org.junit.Assert)6 Activity (android.app.Activity)5