Search in sources :

Example 96 with TestObserver

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

the class ObservableIgnoreElementsTest method testErrorReceivedObservable.

@Test
public void testErrorReceivedObservable() {
    TestObserver<Object> ts = new TestObserver<Object>();
    TestException ex = new TestException("boo");
    Observable.error(ex).ignoreElements().toObservable().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 97 with TestObserver

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

the class ObservableWindowWithSizeTest method testWindowUnsubscribeNonOverlapping.

@Test
public void testWindowUnsubscribeNonOverlapping() {
    TestObserver<Integer> ts = new TestObserver<Integer>();
    final AtomicInteger count = new AtomicInteger();
    Observable.merge(Observable.range(1, 10000).doOnNext(new Consumer<Integer>() {

        @Override
        public void accept(Integer t1) {
            count.incrementAndGet();
        }
    }).window(5).take(2)).subscribe(ts);
    ts.awaitTerminalEvent(500, TimeUnit.MILLISECONDS);
    ts.assertTerminated();
    ts.assertValues(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
    //        System.out.println(ts.getOnNextEvents());
    assertEquals(10, count.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test)

Example 98 with TestObserver

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

the class ObservableWindowWithSizeTest method testWindowUnsubscribeNonOverlappingAsyncSource.

@Test
public void testWindowUnsubscribeNonOverlappingAsyncSource() {
    TestObserver<Integer> ts = new TestObserver<Integer>();
    final AtomicInteger count = new AtomicInteger();
    Observable.merge(Observable.range(1, 100000).doOnNext(new Consumer<Integer>() {

        @Override
        public void accept(Integer t1) {
            if (count.incrementAndGet() == 500000) {
                // give it a small break halfway through
                try {
                    Thread.sleep(50);
                } catch (InterruptedException ex) {
                // ignored
                }
            }
        }
    }).observeOn(Schedulers.computation()).window(5).take(2)).subscribe(ts);
    ts.awaitTerminalEvent(500, TimeUnit.MILLISECONDS);
    ts.assertTerminated();
    ts.assertValues(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
    // make sure we don't emit all values ... the unsubscribe should propagate
    assertTrue(count.get() < 100000);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test)

Example 99 with TestObserver

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

the class ObservableWindowWithSizeTest method testWindowUnsubscribeOverlappingAsyncSource.

@Test
public void testWindowUnsubscribeOverlappingAsyncSource() {
    TestObserver<Integer> ts = new TestObserver<Integer>();
    final AtomicInteger count = new AtomicInteger();
    Observable.merge(Observable.range(1, 100000).doOnNext(new Consumer<Integer>() {

        @Override
        public void accept(Integer t1) {
            count.incrementAndGet();
        }
    }).observeOn(Schedulers.computation()).window(5, 4).take(2), 128).subscribe(ts);
    ts.awaitTerminalEvent(500, TimeUnit.MILLISECONDS);
    ts.assertTerminated();
    ts.assertValues(1, 2, 3, 4, 5, 5, 6, 7, 8, 9);
// make sure we don't emit all values ... the unsubscribe should propagate
// assertTrue(count.get() < 100000); // disabled: a small hiccup in the consumption may allow the source to run to completion
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test)

Example 100 with TestObserver

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

the class ObservableTimeoutTests method shouldUnsubscribeFromUnderlyingSubscriptionOnImmediatelyErrored.

@Test
@Ignore("s should be considered cancelled upon executing onError and not expect downstream to call cancel")
public void shouldUnsubscribeFromUnderlyingSubscriptionOnImmediatelyErrored() throws InterruptedException {
    // From https://github.com/ReactiveX/RxJava/pull/951
    final Disposable s = mock(Disposable.class);
    Observable<String> immediatelyError = Observable.unsafeCreate(new ObservableSource<String>() {

        @Override
        public void subscribe(Observer<? super String> observer) {
            observer.onSubscribe(s);
            observer.onError(new IOException("Error"));
        }
    });
    TestScheduler testScheduler = new TestScheduler();
    Observable<String> observableWithTimeout = immediatelyError.timeout(1000, TimeUnit.MILLISECONDS, testScheduler);
    Observer<String> observer = TestHelper.mockObserver();
    TestObserver<String> ts = new TestObserver<String>(observer);
    observableWithTimeout.subscribe(ts);
    testScheduler.advanceTimeBy(2000, TimeUnit.MILLISECONDS);
    InOrder inOrder = inOrder(observer);
    inOrder.verify(observer).onError(isA(IOException.class));
    inOrder.verifyNoMoreInteractions();
    verify(s, times(1)).dispose();
}
Also used : InOrder(org.mockito.InOrder) IOException(java.io.IOException) TestScheduler(io.reactivex.schedulers.TestScheduler) 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