Search in sources :

Example 86 with TestObserver

use of io.reactivex.observers.TestObserver in project retrofit by square.

the class AsyncTest method success.

@Test
public void success() throws InterruptedException {
    TestObserver<Void> observer = new TestObserver<>();
    service.completable().subscribe(observer);
    assertFalse(observer.await(1, SECONDS));
    server.enqueue(new MockResponse());
    observer.awaitTerminalEvent(1, SECONDS);
    observer.assertComplete();
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test)

Example 87 with TestObserver

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

the class CompletableCacheTest method crossDisposeOnError.

@Test
public void crossDisposeOnError() {
    PublishSubject<Integer> ps = PublishSubject.create();
    final TestObserver<Void> ts1 = new TestObserver<Void>();
    final TestObserver<Void> ts2 = new TestObserver<Void>() {

        @Override
        public void onError(Throwable ex) {
            super.onError(ex);
            ts1.cancel();
        }
    };
    Completable c = ps.ignoreElements().cache();
    c.subscribe(ts2);
    c.subscribe(ts1);
    ps.onError(new TestException());
    ts1.assertEmpty();
    ts2.assertFailure(TestException.class);
}
Also used : TestException(io.reactivex.exceptions.TestException) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test)

Example 88 with TestObserver

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

the class ObservableDebounceTest method debounceWithTimeBackpressure.

@Test
public void debounceWithTimeBackpressure() throws InterruptedException {
    TestScheduler scheduler = new TestScheduler();
    TestObserver<Integer> observer = new TestObserver<Integer>();
    Observable.merge(Observable.just(1), Observable.just(2).delay(10, TimeUnit.MILLISECONDS, scheduler)).debounce(20, TimeUnit.MILLISECONDS, scheduler).take(1).subscribe(observer);
    scheduler.advanceTimeBy(30, TimeUnit.MILLISECONDS);
    observer.assertValue(2);
    observer.assertTerminated();
    observer.assertNoErrors();
}
Also used : TestScheduler(io.reactivex.schedulers.TestScheduler) TestObserver(io.reactivex.observers.TestObserver)

Example 89 with TestObserver

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

the class ObservableDelaySubscriptionOtherTest method testNoSubscriptionIfOtherErrors.

@Test
public void testNoSubscriptionIfOtherErrors() {
    PublishSubject<Object> other = PublishSubject.create();
    TestObserver<Integer> ts = new TestObserver<Integer>();
    final AtomicInteger subscribed = new AtomicInteger();
    Observable.<Integer>error(new TestException()).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.onError(new TestException());
    Assert.assertEquals("Premature subscription", 0, subscribed.get());
    ts.assertNoValues();
    ts.assertNotComplete();
    ts.assertError(TestException.class);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Disposable(io.reactivex.disposables.Disposable) TestException(io.reactivex.exceptions.TestException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestObserver(io.reactivex.observers.TestObserver)

Example 90 with TestObserver

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

the class ObservableAmbTest method testSubscriptionOnlyHappensOnce.

@SuppressWarnings("unchecked")
@Test
public void testSubscriptionOnlyHappensOnce() throws InterruptedException {
    final AtomicLong count = new AtomicLong();
    Consumer<Disposable> incrementer = new Consumer<Disposable>() {

        @Override
        public void accept(Disposable s) {
            count.incrementAndGet();
        }
    };
    //this aync stream should emit first
    Observable<Integer> o1 = Observable.just(1).doOnSubscribe(incrementer).delay(100, TimeUnit.MILLISECONDS).subscribeOn(Schedulers.computation());
    //this stream emits second
    Observable<Integer> o2 = Observable.just(1).doOnSubscribe(incrementer).delay(100, TimeUnit.MILLISECONDS).subscribeOn(Schedulers.computation());
    TestObserver<Integer> ts = new TestObserver<Integer>();
    Observable.ambArray(o1, o2).subscribe(ts);
    ts.awaitTerminalEvent(5, TimeUnit.SECONDS);
    ts.assertNoErrors();
    assertEquals(2, count.get());
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) Consumer(io.reactivex.functions.Consumer) TestObserver(io.reactivex.observers.TestObserver)

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