Search in sources :

Example 1 with TestObserver

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

the class ObservableDelaySubscriptionOtherTest method testNoPrematureSubscription.

@Test
public void testNoPrematureSubscription() {
    PublishSubject<Object> other = PublishSubject.create();
    TestObserver<Integer> ts = new TestObserver<Integer>();
    final AtomicInteger subscribed = new AtomicInteger();
    Observable.just(1).doOnSubscribe(new Consumer<Disposable>() {

        @Override
        public void accept(Disposable d) {
            subscribed.getAndIncrement();
        }
    }).delaySubscription(other).subscribe(ts);
    ts.assertNotComplete();
    ts.assertNoErrors();
    ts.assertNoValues();
    Assert.assertEquals("Premature subscription", 0, subscribed.get());
    other.onNext(1);
    Assert.assertEquals("No subscription", 1, subscribed.get());
    ts.assertValue(1);
    ts.assertNoErrors();
    ts.assertComplete();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Disposable(io.reactivex.disposables.Disposable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestObserver(io.reactivex.observers.TestObserver)

Example 2 with TestObserver

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

the class ObservableCacheTest method testValuesAndThenError.

@Test
public void testValuesAndThenError() {
    Observable<Integer> source = Observable.range(1, 10).concatWith(Observable.<Integer>error(new TestException())).cache();
    TestObserver<Integer> ts = new TestObserver<Integer>();
    source.subscribe(ts);
    ts.assertValues(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
    ts.assertNotComplete();
    ts.assertError(TestException.class);
    TestObserver<Integer> ts2 = new TestObserver<Integer>();
    source.subscribe(ts2);
    ts2.assertValues(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
    ts2.assertNotComplete();
    ts2.assertError(TestException.class);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestException(io.reactivex.exceptions.TestException) TestObserver(io.reactivex.observers.TestObserver)

Example 3 with TestObserver

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

the class ObservableCacheTest method unsafeChildThrows.

@Test
@Ignore("2.x consumers are not allowed to throw")
public void unsafeChildThrows() {
    final AtomicInteger count = new AtomicInteger();
    Observable<Integer> source = Observable.range(1, 100).doOnNext(new Consumer<Integer>() {

        @Override
        public void accept(Integer t) {
            count.getAndIncrement();
        }
    }).cache();
    TestObserver<Integer> ts = new TestObserver<Integer>() {

        @Override
        public void onNext(Integer t) {
            throw new TestException();
        }
    };
    source.subscribe(ts);
    Assert.assertEquals(100, count.get());
    ts.assertNoValues();
    ts.assertNotComplete();
    ts.assertError(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)

Example 4 with TestObserver

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

the class ObservableReplayTest method unsafeChildThrows.

@Test
@Ignore("onNext should not throw")
public void unsafeChildThrows() {
    final AtomicInteger count = new AtomicInteger();
    Observable<Integer> source = Observable.range(1, 100).doOnNext(new Consumer<Integer>() {

        @Override
        public void accept(Integer t) {
            count.getAndIncrement();
        }
    }).replay().autoConnect();
    TestObserver<Integer> ts = new TestObserver<Integer>() {

        @Override
        public void onNext(Integer t) {
            throw new TestException();
        }
    };
    source.subscribe(ts);
    Assert.assertEquals(100, count.get());
    ts.assertNoValues();
    ts.assertNotComplete();
    ts.assertError(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)

Example 5 with TestObserver

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

the class ObservableDelaySubscriptionOtherTest method testNoMultipleSubscriptions.

@Test
public void testNoMultipleSubscriptions() {
    PublishSubject<Object> other = PublishSubject.create();
    TestObserver<Integer> ts = new TestObserver<Integer>();
    final AtomicInteger subscribed = new AtomicInteger();
    Observable.just(1).doOnSubscribe(new Consumer<Disposable>() {

        @Override
        public void accept(Disposable d) {
            subscribed.getAndIncrement();
        }
    }).delaySubscription(other).subscribe(ts);
    ts.assertNotComplete();
    ts.assertNoErrors();
    ts.assertNoValues();
    Assert.assertEquals("Premature subscription", 0, subscribed.get());
    other.onNext(1);
    other.onNext(2);
    Assert.assertEquals("No subscription", 1, subscribed.get());
    ts.assertValue(1);
    ts.assertNoErrors();
    ts.assertComplete();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Disposable(io.reactivex.disposables.Disposable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestObserver(io.reactivex.observers.TestObserver)

Aggregations

TestObserver (io.reactivex.observers.TestObserver)164 Test (org.junit.Test)133 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)16 Completable (io.reactivex.Completable)13 Disposable (io.reactivex.disposables.Disposable)13 InOrder (org.mockito.InOrder)10 TestScheduler (io.reactivex.schedulers.TestScheduler)9 StorIOSQLite (com.pushtorefresh.storio3.sqlite.StorIOSQLite)8 BaseTest (io.rx_cache2.internal.common.BaseTest)8 List (java.util.List)8 EpisodeHeroNameQuery (com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery)7 Before (org.junit.Before)7 Schedulers (io.reactivex.schedulers.Schedulers)6 TimeUnit (java.util.concurrent.TimeUnit)6 After (org.junit.After)6