Search in sources :

Example 91 with TestObserver

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

the class ObservableFlatMapTest method testFlatMapSelectorMaxConcurrent.

@Test
public void testFlatMapSelectorMaxConcurrent() {
    final int m = 4;
    final AtomicInteger subscriptionCount = new AtomicInteger();
    Observable<Integer> source = Observable.range(1, 10).flatMap(new Function<Integer, Observable<Integer>>() {

        @Override
        public Observable<Integer> apply(Integer t1) {
            return composer(Observable.range(t1 * 10, 2), subscriptionCount, m).subscribeOn(Schedulers.computation());
        }
    }, new BiFunction<Integer, Integer, Integer>() {

        @Override
        public Integer apply(Integer t1, Integer t2) {
            return t1 * 1000 + t2;
        }
    }, m);
    TestObserver<Integer> ts = new TestObserver<Integer>();
    source.subscribe(ts);
    ts.awaitTerminalEvent();
    ts.assertNoErrors();
    Set<Integer> expected = new HashSet<Integer>(Arrays.asList(1010, 1011, 2020, 2021, 3030, 3031, 4040, 4041, 5050, 5051, 6060, 6061, 7070, 7071, 8080, 8081, 9090, 9091, 10100, 10101));
    Assert.assertEquals(expected.size(), ts.valueCount());
    System.out.println("--> testFlatMapSelectorMaxConcurrent: " + ts.values());
    Assert.assertTrue(expected.containsAll(ts.values()));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Observable(io.reactivex.Observable) TestObserver(io.reactivex.observers.TestObserver)

Example 92 with TestObserver

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

the class ObservableFlatMapTest method testFlatMapTransformsMaxConcurrentNormal.

@Test
public void testFlatMapTransformsMaxConcurrentNormal() {
    final int m = 2;
    final AtomicInteger subscriptionCount = new AtomicInteger();
    Observable<Integer> onNext = composer(Observable.fromIterable(Arrays.asList(1, 2, 3)).observeOn(Schedulers.computation()), subscriptionCount, m).subscribeOn(Schedulers.computation());
    Observable<Integer> onComplete = composer(Observable.fromIterable(Arrays.asList(4)), subscriptionCount, m).subscribeOn(Schedulers.computation());
    Observable<Integer> onError = Observable.fromIterable(Arrays.asList(5));
    Observable<Integer> source = Observable.fromIterable(Arrays.asList(10, 20, 30));
    Observer<Object> o = TestHelper.mockObserver();
    TestObserver<Object> ts = new TestObserver<Object>(o);
    Function<Throwable, Observable<Integer>> just = just(onError);
    source.flatMap(just(onNext), just, just0(onComplete), m).subscribe(ts);
    ts.awaitTerminalEvent(1, TimeUnit.SECONDS);
    ts.assertNoErrors();
    ts.assertTerminated();
    verify(o, times(3)).onNext(1);
    verify(o, times(3)).onNext(2);
    verify(o, times(3)).onNext(3);
    verify(o).onNext(4);
    verify(o).onComplete();
    verify(o, never()).onNext(5);
    verify(o, never()).onError(any(Throwable.class));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestObserver(io.reactivex.observers.TestObserver) Observable(io.reactivex.Observable)

Example 93 with TestObserver

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

the class ObservableDelaySubscriptionOtherTest method testCompleteTriggersSubscription.

@Test
public void testCompleteTriggersSubscription() {
    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.onComplete();
    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 94 with TestObserver

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

the class ObservableDelaySubscriptionOtherTest method testNoPrematureSubscriptionToError.

@Test
public void testNoPrematureSubscriptionToError() {
    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.onComplete();
    Assert.assertEquals("No subscription", 1, 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 95 with TestObserver

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

the class ObservableDetachTest method error.

@Test
public void error() {
    TestObserver<Object> ts = new TestObserver<Object>();
    Observable.error(new TestException()).onTerminateDetach().subscribe(ts);
    ts.assertNoValues();
    ts.assertError(TestException.class);
    ts.assertNotComplete();
}
Also used : TestException(io.reactivex.exceptions.TestException) 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