Search in sources :

Example 21 with TestObserver

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

the class HalfSerializerObserverTest method reentrantOnNextOnComplete.

@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void reentrantOnNextOnComplete() {
    final AtomicInteger wip = new AtomicInteger();
    final AtomicThrowable error = new AtomicThrowable();
    final Observer[] a = { null };
    final TestObserver ts = new TestObserver();
    Observer s = new Observer() {

        @Override
        public void onSubscribe(Disposable s) {
            ts.onSubscribe(s);
        }

        @Override
        public void onNext(Object t) {
            if (t.equals(1)) {
                HalfSerializer.onComplete(a[0], wip, error);
            }
            ts.onNext(t);
        }

        @Override
        public void onError(Throwable t) {
            ts.onError(t);
        }

        @Override
        public void onComplete() {
            ts.onComplete();
        }
    };
    a[0] = s;
    s.onSubscribe(Disposables.empty());
    HalfSerializer.onNext(s, 1, wip, error);
    ts.assertResult(1);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestObserver(io.reactivex.observers.TestObserver) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test)

Example 22 with TestObserver

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

the class HalfSerializerObserverTest method onNextOnCompleteRace.

@Test
public void onNextOnCompleteRace() {
    for (int i = 0; i < 500; i++) {
        final AtomicInteger wip = new AtomicInteger();
        final AtomicThrowable error = new AtomicThrowable();
        final TestObserver<Integer> ts = new TestObserver<Integer>();
        ts.onSubscribe(Disposables.empty());
        Runnable r1 = new Runnable() {

            @Override
            public void run() {
                HalfSerializer.onNext(ts, 1, wip, error);
            }
        };
        Runnable r2 = new Runnable() {

            @Override
            public void run() {
                HalfSerializer.onComplete(ts, wip, error);
            }
        };
        TestHelper.race(r1, r2, Schedulers.single());
        ts.assertComplete().assertNoErrors();
        assertTrue(ts.valueCount() <= 1);
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test)

Example 23 with TestObserver

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

the class HalfSerializerObserverTest method onErrorOnCompleteRace.

@Test
public void onErrorOnCompleteRace() {
    for (int i = 0; i < 500; i++) {
        final AtomicInteger wip = new AtomicInteger();
        final AtomicThrowable error = new AtomicThrowable();
        final TestObserver<Integer> ts = new TestObserver<Integer>();
        ts.onSubscribe(Disposables.empty());
        final TestException ex = new TestException();
        Runnable r1 = new Runnable() {

            @Override
            public void run() {
                HalfSerializer.onError(ts, ex, wip, error);
            }
        };
        Runnable r2 = new Runnable() {

            @Override
            public void run() {
                HalfSerializer.onComplete(ts, wip, error);
            }
        };
        TestHelper.race(r1, r2, Schedulers.single());
        if (ts.completions() != 0) {
            ts.assertResult();
        } else {
            ts.assertFailure(TestException.class);
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestException(io.reactivex.exceptions.TestException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test)

Example 24 with TestObserver

use of io.reactivex.observers.TestObserver in project RxRelay by JakeWharton.

the class PublishRelayTest method testReSubscribe.

/**
     * Should be able to unsubscribe all Subscribers, have it stop emitting, then subscribe new ones and it start emitting again.
     */
@Test
public void testReSubscribe() {
    final PublishRelay<Integer> ps = PublishRelay.create();
    Observer<Integer> o1 = TestHelper.mockObserver();
    TestObserver<Integer> ts = new TestObserver<Integer>(o1);
    ps.subscribe(ts);
    // emit
    ps.accept(1);
    // validate we got it
    InOrder inOrder1 = inOrder(o1);
    inOrder1.verify(o1, times(1)).onNext(1);
    inOrder1.verifyNoMoreInteractions();
    // unsubscribe
    ts.dispose();
    // emit again but nothing will be there to receive it
    ps.accept(2);
    Observer<Integer> o2 = TestHelper.mockObserver();
    TestObserver<Integer> ts2 = new TestObserver<Integer>(o2);
    ps.subscribe(ts2);
    // emit
    ps.accept(3);
    // validate we got it
    InOrder inOrder2 = inOrder(o2);
    inOrder2.verify(o2, times(1)).onNext(3);
    inOrder2.verifyNoMoreInteractions();
    ts2.dispose();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InOrder(org.mockito.InOrder) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test)

Example 25 with TestObserver

use of io.reactivex.observers.TestObserver in project RxRelay by JakeWharton.

the class ReplayRelayTest method reentrantDrain.

@Test
public void reentrantDrain() {
    TestScheduler scheduler = new TestScheduler();
    final ReplayRelay<Integer> rp = ReplayRelay.createWithTimeAndSize(1, TimeUnit.SECONDS, scheduler, 2);
    TestObserver<Integer> ts = new TestObserver<Integer>() {

        @Override
        public void onNext(Integer t) {
            if (t == 1) {
                rp.accept(2);
            }
            super.onNext(t);
        }
    };
    rp.subscribe(ts);
    rp.accept(1);
    ts.assertValues(1, 2);
}
Also used : TestScheduler(io.reactivex.schedulers.TestScheduler) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test)

Aggregations

TestObserver (io.reactivex.observers.TestObserver)158 Test (org.junit.Test)128 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)31 TargetApi (android.annotation.TargetApi)21 Matchers.anyString (org.mockito.Matchers.anyString)21 StorIOException (com.pushtorefresh.storio3.StorIOException)19 TestException (io.reactivex.exceptions.TestException)19 Observable (io.reactivex.Observable)12 BaseTest (io.rx_cache2.internal.common.BaseTest)12 Completable (io.reactivex.Completable)11 Disposable (io.reactivex.disposables.Disposable)11 InOrder (org.mockito.InOrder)10 TestScheduler (io.reactivex.schedulers.TestScheduler)9 StorIOSQLite (com.pushtorefresh.storio3.sqlite.StorIOSQLite)8 EpisodeHeroNameQuery (com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery)7 Schedulers (io.reactivex.schedulers.Schedulers)6 Activity (android.app.Activity)5 Instrumentation (android.app.Instrumentation)5 ContentValues (android.content.ContentValues)5 Context (android.content.Context)5