Search in sources :

Example 11 with TestObserver

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

the class ObservableTimeoutTests method shouldSwitchToOtherIfOnErrorNotWithinTimeout.

@Test
public void shouldSwitchToOtherIfOnErrorNotWithinTimeout() {
    Observable<String> other = Observable.just("a", "b", "c");
    Observable<String> source = underlyingSubject.timeout(TIMEOUT, TIME_UNIT, testScheduler, other);
    Observer<String> observer = TestHelper.mockObserver();
    TestObserver<String> ts = new TestObserver<String>(observer);
    source.subscribe(ts);
    testScheduler.advanceTimeBy(2, TimeUnit.SECONDS);
    underlyingSubject.onNext("One");
    testScheduler.advanceTimeBy(4, TimeUnit.SECONDS);
    underlyingSubject.onError(new UnsupportedOperationException());
    InOrder inOrder = inOrder(observer);
    inOrder.verify(observer, times(1)).onNext("One");
    inOrder.verify(observer, times(1)).onNext("a");
    inOrder.verify(observer, times(1)).onNext("b");
    inOrder.verify(observer, times(1)).onNext("c");
    inOrder.verify(observer, times(1)).onComplete();
    inOrder.verifyNoMoreInteractions();
    ts.dispose();
}
Also used : InOrder(org.mockito.InOrder) TestObserver(io.reactivex.observers.TestObserver)

Example 12 with TestObserver

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

the class ObservableTimeoutWithSelectorTest method testTimeoutSelectorWithTimeoutAndOnNextRaceCondition.

@Test
public void testTimeoutSelectorWithTimeoutAndOnNextRaceCondition() throws InterruptedException {
    // Thread 1                                    Thread 2
    //
    // observer.onNext(1)
    // start timeout
    // unsubscribe timeout in thread 2          start to do some long-time work in "unsubscribe"
    // observer.onNext(2)
    // timeout.onNext(1)
    //                                          "unsubscribe" done
    //
    //
    // In the above case, the timeout operator should ignore "timeout.onNext(1)"
    // since "observer" has already seen 2.
    final CountDownLatch observerReceivedTwo = new CountDownLatch(1);
    final CountDownLatch timeoutEmittedOne = new CountDownLatch(1);
    final CountDownLatch observerCompleted = new CountDownLatch(1);
    final CountDownLatch enteredTimeoutOne = new CountDownLatch(1);
    final AtomicBoolean latchTimeout = new AtomicBoolean(false);
    final Function<Integer, Observable<Integer>> timeoutFunc = new Function<Integer, Observable<Integer>>() {

        @Override
        public Observable<Integer> apply(Integer t1) {
            if (t1 == 1) {
                // Force "unsubscribe" run on another thread
                return Observable.unsafeCreate(new ObservableSource<Integer>() {

                    @Override
                    public void subscribe(Observer<? super Integer> observer) {
                        observer.onSubscribe(Disposables.empty());
                        enteredTimeoutOne.countDown();
                        // force the timeout message be sent after observer.onNext(2)
                        while (true) {
                            try {
                                if (!observerReceivedTwo.await(30, TimeUnit.SECONDS)) {
                                    // CountDownLatch timeout
                                    // There should be something wrong
                                    latchTimeout.set(true);
                                }
                                break;
                            } catch (InterruptedException e) {
                            // Since we just want to emulate a busy method,
                            // we ignore the interrupt signal from Scheduler.
                            }
                        }
                        observer.onNext(1);
                        timeoutEmittedOne.countDown();
                    }
                }).subscribeOn(Schedulers.newThread());
            } else {
                return PublishSubject.create();
            }
        }
    };
    final Observer<Integer> o = TestHelper.mockObserver();
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            observerReceivedTwo.countDown();
            return null;
        }
    }).when(o).onNext(2);
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            observerCompleted.countDown();
            return null;
        }
    }).when(o).onComplete();
    final TestObserver<Integer> ts = new TestObserver<Integer>(o);
    new Thread(new Runnable() {

        @Override
        public void run() {
            PublishSubject<Integer> source = PublishSubject.create();
            source.timeout(timeoutFunc, Observable.just(3)).subscribe(ts);
            // start timeout
            source.onNext(1);
            try {
                if (!enteredTimeoutOne.await(30, TimeUnit.SECONDS)) {
                    latchTimeout.set(true);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            // disable timeout
            source.onNext(2);
            try {
                if (!timeoutEmittedOne.await(30, TimeUnit.SECONDS)) {
                    latchTimeout.set(true);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            source.onComplete();
        }
    }).start();
    if (!observerCompleted.await(30, TimeUnit.SECONDS)) {
        latchTimeout.set(true);
    }
    assertFalse("CoundDownLatch timeout", latchTimeout.get());
    InOrder inOrder = inOrder(o);
    inOrder.verify(o).onSubscribe((Disposable) notNull());
    inOrder.verify(o).onNext(1);
    inOrder.verify(o).onNext(2);
    inOrder.verify(o, never()).onNext(3);
    inOrder.verify(o).onComplete();
    inOrder.verifyNoMoreInteractions();
}
Also used : InOrder(org.mockito.InOrder) Observable(io.reactivex.Observable) TestObserver(io.reactivex.observers.TestObserver) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Function(io.reactivex.functions.Function) InvocationOnMock(org.mockito.invocation.InvocationOnMock) TestObserver(io.reactivex.observers.TestObserver) Observer(io.reactivex.Observer) Test(org.junit.Test)

Example 13 with TestObserver

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

the class ObservableSubscribeOnTest method testUnsubscribeInfiniteStream.

@Test(timeout = 5000)
public void testUnsubscribeInfiniteStream() throws InterruptedException {
    TestObserver<Integer> ts = new TestObserver<Integer>();
    final AtomicInteger count = new AtomicInteger();
    Observable.unsafeCreate(new ObservableSource<Integer>() {

        @Override
        public void subscribe(Observer<? super Integer> sub) {
            Disposable d = Disposables.empty();
            sub.onSubscribe(d);
            for (int i = 1; !d.isDisposed(); i++) {
                count.incrementAndGet();
                sub.onNext(i);
            }
        }
    }).subscribeOn(Schedulers.newThread()).take(10).subscribe(ts);
    ts.awaitTerminalEvent(1000, TimeUnit.MILLISECONDS);
    ts.dispose();
    // give time for the loop to continue
    Thread.sleep(200);
    ts.assertValues(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
    assertEquals(10, count.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestObserver(io.reactivex.observers.TestObserver) TestObserver(io.reactivex.observers.TestObserver)

Example 14 with TestObserver

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

the class ObservableTimeoutTests method shouldUnsubscribeFromUnderlyingSubscriptionOnImmediatelyComplete.

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

        @Override
        public void subscribe(Observer<? super String> observer) {
            observer.onSubscribe(s);
            observer.onComplete();
        }
    });
    TestScheduler testScheduler = new TestScheduler();
    Observable<String> observableWithTimeout = immediatelyComplete.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).onComplete();
    inOrder.verifyNoMoreInteractions();
    verify(s, times(1)).dispose();
}
Also used : InOrder(org.mockito.InOrder) TestScheduler(io.reactivex.schedulers.TestScheduler) TestObserver(io.reactivex.observers.TestObserver)

Example 15 with TestObserver

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

the class ObservableTimeoutTests method shouldSwitchToOtherIfOnNextNotWithinTimeout.

@Test
public void shouldSwitchToOtherIfOnNextNotWithinTimeout() {
    Observable<String> other = Observable.just("a", "b", "c");
    Observable<String> source = underlyingSubject.timeout(TIMEOUT, TIME_UNIT, testScheduler, other);
    Observer<String> observer = TestHelper.mockObserver();
    TestObserver<String> ts = new TestObserver<String>(observer);
    source.subscribe(ts);
    testScheduler.advanceTimeBy(2, TimeUnit.SECONDS);
    underlyingSubject.onNext("One");
    testScheduler.advanceTimeBy(4, TimeUnit.SECONDS);
    underlyingSubject.onNext("Two");
    InOrder inOrder = inOrder(observer);
    inOrder.verify(observer, times(1)).onNext("One");
    inOrder.verify(observer, times(1)).onNext("a");
    inOrder.verify(observer, times(1)).onNext("b");
    inOrder.verify(observer, times(1)).onNext("c");
    inOrder.verify(observer, times(1)).onComplete();
    inOrder.verifyNoMoreInteractions();
    ts.dispose();
}
Also used : InOrder(org.mockito.InOrder) 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