Search in sources :

Example 6 with TestObserver

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

the class ObservableDoOnUnsubscribeTest method testDoOnUnSubscribeWorksWithRefCount.

@Test
public void testDoOnUnSubscribeWorksWithRefCount() throws Exception {
    int subCount = 3;
    final CountDownLatch upperLatch = new CountDownLatch(1);
    final CountDownLatch lowerLatch = new CountDownLatch(1);
    final CountDownLatch onNextLatch = new CountDownLatch(subCount);
    final AtomicInteger upperCount = new AtomicInteger();
    final AtomicInteger lowerCount = new AtomicInteger();
    Observable<Long> longs = Observable.interval(50, TimeUnit.MILLISECONDS).doOnDispose(new Action() {

        @Override
        public void run() {
            // Test that upper stream will be notified for un-subscription
            upperLatch.countDown();
            upperCount.incrementAndGet();
        }
    }).doOnNext(new Consumer<Long>() {

        @Override
        public void accept(Long aLong) {
            // Ensure there is at least some onNext events before un-subscription happens
            onNextLatch.countDown();
        }
    }).doOnDispose(new Action() {

        @Override
        public void run() {
            // Test that lower stream will be notified for un-subscription
            lowerLatch.countDown();
            lowerCount.incrementAndGet();
        }
    }).publish().refCount();
    List<Disposable> subscriptions = new ArrayList<Disposable>();
    List<TestObserver<Long>> subscribers = new ArrayList<TestObserver<Long>>();
    for (int i = 0; i < subCount; ++i) {
        TestObserver<Long> observer = new TestObserver<Long>();
        longs.subscribe(observer);
        subscriptions.add(observer);
        subscribers.add(observer);
    }
    onNextLatch.await();
    for (int i = 0; i < subCount; ++i) {
        subscriptions.get(i).dispose();
    // Test that unsubscribe() method is not affected in any way
    // FIXME no longer valid
    //            subscribers.get(i).assertUnsubscribed();
    }
    upperLatch.await();
    lowerLatch.await();
    assertEquals("There should exactly 1 un-subscription events for upper stream", 1, upperCount.get());
    assertEquals("There should exactly 1 un-subscription events for lower stream", 1, lowerCount.get());
}
Also used : Disposable(io.reactivex.disposables.Disposable) TestObserver(io.reactivex.observers.TestObserver) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 7 with TestObserver

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

the class ObservableDoOnUnsubscribeTest method testDoOnUnsubscribe.

@Test
public void testDoOnUnsubscribe() throws Exception {
    int subCount = 3;
    final CountDownLatch upperLatch = new CountDownLatch(subCount);
    final CountDownLatch lowerLatch = new CountDownLatch(subCount);
    final CountDownLatch onNextLatch = new CountDownLatch(subCount);
    final AtomicInteger upperCount = new AtomicInteger();
    final AtomicInteger lowerCount = new AtomicInteger();
    Observable<Long> longs = Observable.interval(50, TimeUnit.MILLISECONDS).doOnDispose(new Action() {

        @Override
        public void run() {
            // Test that upper stream will be notified for un-subscription
            // from a child Observer
            upperLatch.countDown();
            upperCount.incrementAndGet();
        }
    }).doOnNext(new Consumer<Long>() {

        @Override
        public void accept(Long aLong) {
            // Ensure there is at least some onNext events before un-subscription happens
            onNextLatch.countDown();
        }
    }).doOnDispose(new Action() {

        @Override
        public void run() {
            // Test that lower stream will be notified for a direct un-subscription
            lowerLatch.countDown();
            lowerCount.incrementAndGet();
        }
    });
    List<Disposable> subscriptions = new ArrayList<Disposable>();
    List<TestObserver<Long>> subscribers = new ArrayList<TestObserver<Long>>();
    for (int i = 0; i < subCount; ++i) {
        TestObserver<Long> observer = new TestObserver<Long>();
        subscriptions.add(observer);
        longs.subscribe(observer);
        subscribers.add(observer);
    }
    onNextLatch.await();
    for (int i = 0; i < subCount; ++i) {
        subscriptions.get(i).dispose();
    // Test that unsubscribe() method is not affected in any way
    // FIXME no longer valid
    //            subscribers.get(i).assertUnsubscribed();
    }
    upperLatch.await();
    lowerLatch.await();
    assertEquals(String.format("There should exactly %d un-subscription events for upper stream", subCount), subCount, upperCount.get());
    assertEquals(String.format("There should exactly %d un-subscription events for lower stream", subCount), subCount, lowerCount.get());
}
Also used : Disposable(io.reactivex.disposables.Disposable) TestObserver(io.reactivex.observers.TestObserver) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 8 with TestObserver

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

the class ObservableIgnoreElementsTest method testErrorReceived.

@Test
public void testErrorReceived() {
    TestObserver<Object> ts = new TestObserver<Object>();
    TestException ex = new TestException("boo");
    Observable.error(ex).ignoreElements().subscribe(ts);
    ts.assertNoValues();
    ts.assertTerminated();
    // FIXME no longer testable
    //        ts.assertUnsubscribed();
    ts.assertError(TestException.class);
    ts.assertErrorMessage("boo");
}
Also used : TestException(io.reactivex.exceptions.TestException) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test)

Example 9 with TestObserver

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

the class ObservableJoinTest method badEndSource.

@Test
public void badEndSource() {
    List<Throwable> errors = TestHelper.trackPluginErrors();
    try {
        @SuppressWarnings("rawtypes") final Observer[] o = { null };
        TestObserver<Integer> to = Observable.just(1).join(Observable.just(2), Functions.justFunction(Observable.never()), Functions.justFunction(new Observable<Integer>() {

            @Override
            protected void subscribeActual(Observer<? super Integer> observer) {
                o[0] = observer;
                observer.onSubscribe(Disposables.empty());
                observer.onError(new TestException("First"));
            }
        }), new BiFunction<Integer, Integer, Integer>() {

            @Override
            public Integer apply(Integer a, Integer b) throws Exception {
                return a + b;
            }
        }).test();
        o[0].onError(new TestException("Second"));
        to.assertFailureAndMessage(TestException.class, "First");
        TestHelper.assertUndeliverable(errors, 0, TestException.class, "Second");
    } finally {
        RxJavaPlugins.reset();
    }
}
Also used : TestException(io.reactivex.exceptions.TestException) TestObserver(io.reactivex.observers.TestObserver)

Example 10 with TestObserver

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

the class ObservableFlatMapTest method testFlatMapMaxConcurrent.

@Test
public void testFlatMapMaxConcurrent() {
    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());
        }
    }, m);
    TestObserver<Integer> ts = new TestObserver<Integer>();
    source.subscribe(ts);
    ts.awaitTerminalEvent();
    ts.assertNoErrors();
    Set<Integer> expected = new HashSet<Integer>(Arrays.asList(10, 11, 20, 21, 30, 31, 40, 41, 50, 51, 60, 61, 70, 71, 80, 81, 90, 91, 100, 101));
    Assert.assertEquals(expected.size(), ts.valueCount());
    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)

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