Search in sources :

Example 16 with TestObserver

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

the class ObservableTimeoutTests method shouldTimeoutIfSynchronizedObservableEmitFirstOnNextNotWithinTimeout.

@Test
public void shouldTimeoutIfSynchronizedObservableEmitFirstOnNextNotWithinTimeout() throws InterruptedException {
    final CountDownLatch exit = new CountDownLatch(1);
    final CountDownLatch timeoutSetuped = new CountDownLatch(1);
    final Observer<String> observer = TestHelper.mockObserver();
    final TestObserver<String> ts = new TestObserver<String>(observer);
    new Thread(new Runnable() {

        @Override
        public void run() {
            Observable.unsafeCreate(new ObservableSource<String>() {

                @Override
                public void subscribe(Observer<? super String> observer) {
                    observer.onSubscribe(Disposables.empty());
                    try {
                        timeoutSetuped.countDown();
                        exit.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    observer.onNext("a");
                    observer.onComplete();
                }
            }).timeout(1, TimeUnit.SECONDS, testScheduler).subscribe(ts);
        }
    }).start();
    timeoutSetuped.await();
    testScheduler.advanceTimeBy(2, TimeUnit.SECONDS);
    InOrder inOrder = inOrder(observer);
    inOrder.verify(observer, times(1)).onError(isA(TimeoutException.class));
    inOrder.verifyNoMoreInteractions();
    // exit the thread
    exit.countDown();
}
Also used : InOrder(org.mockito.InOrder) TestObserver(io.reactivex.observers.TestObserver)

Example 17 with TestObserver

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

the class ObservableWithLatestFromTest method testSourceThrows.

@Test
public void testSourceThrows() {
    PublishSubject<Integer> source = PublishSubject.create();
    PublishSubject<Integer> other = PublishSubject.create();
    Observable<Integer> result = source.withLatestFrom(other, COMBINER);
    TestObserver<Integer> ts = new TestObserver<Integer>();
    result.subscribe(ts);
    assertTrue(source.hasObservers());
    assertTrue(other.hasObservers());
    other.onNext(1);
    source.onNext(1);
    source.onError(new TestException());
    ts.assertTerminated();
    ts.assertValue((1 << 8) + 1);
    ts.assertError(TestException.class);
    ts.assertNotComplete();
    assertFalse(source.hasObservers());
    assertFalse(other.hasObservers());
}
Also used : TestException(io.reactivex.exceptions.TestException) TestObserver(io.reactivex.observers.TestObserver)

Example 18 with TestObserver

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

the class ObservableWithLatestFromTest method testOtherThrows.

@Test
public void testOtherThrows() {
    PublishSubject<Integer> source = PublishSubject.create();
    PublishSubject<Integer> other = PublishSubject.create();
    Observable<Integer> result = source.withLatestFrom(other, COMBINER);
    TestObserver<Integer> ts = new TestObserver<Integer>();
    result.subscribe(ts);
    assertTrue(source.hasObservers());
    assertTrue(other.hasObservers());
    other.onNext(1);
    source.onNext(1);
    other.onError(new TestException());
    ts.assertTerminated();
    ts.assertValue((1 << 8) + 1);
    ts.assertNotComplete();
    ts.assertError(TestException.class);
    assertFalse(source.hasObservers());
    assertFalse(other.hasObservers());
}
Also used : TestException(io.reactivex.exceptions.TestException) TestObserver(io.reactivex.observers.TestObserver)

Example 19 with TestObserver

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

the class ObservableToFutureTest method testCancellationDuringFutureGet.

@Test
public void testCancellationDuringFutureGet() throws Exception {
    Future<Object> future = new Future<Object>() {

        private AtomicBoolean isCancelled = new AtomicBoolean(false);

        private AtomicBoolean isDone = new AtomicBoolean(false);

        @Override
        public boolean cancel(boolean mayInterruptIfRunning) {
            isCancelled.compareAndSet(false, true);
            return true;
        }

        @Override
        public boolean isCancelled() {
            return isCancelled.get();
        }

        @Override
        public boolean isDone() {
            return isCancelled() || isDone.get();
        }

        @Override
        public Object get() throws InterruptedException, ExecutionException {
            Thread.sleep(500);
            isDone.compareAndSet(false, true);
            return "foo";
        }

        @Override
        public Object get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
            return get();
        }
    };
    Observer<Object> o = TestHelper.mockObserver();
    TestObserver<Object> ts = new TestObserver<Object>(o);
    Observable<Object> futureObservable = Observable.fromFuture(future);
    futureObservable.subscribeOn(Schedulers.computation()).subscribe(ts);
    Thread.sleep(100);
    ts.dispose();
    ts.assertNoErrors();
    ts.assertNoValues();
    ts.assertNotComplete();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test)

Example 20 with TestObserver

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

the class ObservableWindowWithSizeTest method testWindowUnsubscribeOverlapping.

@Test
public void testWindowUnsubscribeOverlapping() {
    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, 4).take(2)).subscribe(ts);
    ts.awaitTerminalEvent(500, TimeUnit.MILLISECONDS);
    ts.assertTerminated();
    //        System.out.println(ts.getOnNextEvents());
    ts.assertValues(1, 2, 3, 4, 5, 5, 6, 7, 8, 9);
    assertEquals(9, 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)

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