Search in sources :

Example 11 with Observable

use of io.reactivex.Observable in project RxJava by ReactiveX.

the class ObservableConcatTest method testConcatUnsubscribeConcurrent.

/**
     * All observables will be running in different threads so subscribe() is unblocked. CountDownLatch is only used in order to call unsubscribe() in a predictable manner.
     */
@Test
public void testConcatUnsubscribeConcurrent() {
    final CountDownLatch callOnce = new CountDownLatch(1);
    final CountDownLatch okToContinue = new CountDownLatch(1);
    final TestObservable<String> w1 = new TestObservable<String>("one", "two", "three");
    final TestObservable<String> w2 = new TestObservable<String>(callOnce, okToContinue, "four", "five", "six");
    Observer<String> observer = TestHelper.mockObserver();
    TestObserver<String> ts = new TestObserver<String>(observer);
    @SuppressWarnings("unchecked") TestObservable<Observable<String>> observableOfObservables = new TestObservable<Observable<String>>(Observable.unsafeCreate(w1), Observable.unsafeCreate(w2));
    Observable<String> concatF = Observable.concat(Observable.unsafeCreate(observableOfObservables));
    concatF.subscribe(ts);
    try {
        //Block main thread to allow Observable "w1" to complete and Observable "w2" to call onNext exactly once.
        callOnce.await();
        //"four" from w2 has been processed by onNext()
        ts.dispose();
        //"five" and "six" will NOT be processed by onNext()
        //Unblock the Observable to continue.
        okToContinue.countDown();
        w1.t.join();
        w2.t.join();
    } catch (Throwable e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
    InOrder inOrder = inOrder(observer);
    inOrder.verify(observer, times(1)).onNext("one");
    inOrder.verify(observer, times(1)).onNext("two");
    inOrder.verify(observer, times(1)).onNext("three");
    inOrder.verify(observer, times(1)).onNext("four");
    inOrder.verify(observer, never()).onNext("five");
    inOrder.verify(observer, never()).onNext("six");
    verify(observer, never()).onComplete();
    verify(observer, never()).onError(any(Throwable.class));
}
Also used : InOrder(org.mockito.InOrder) Observable(io.reactivex.Observable) Test(org.junit.Test)

Example 12 with Observable

use of io.reactivex.Observable in project RxJava by ReactiveX.

the class ObservableConcatTest method concatVeryLongObservableOfObservables.

@Test
public void concatVeryLongObservableOfObservables() {
    final int n = 10000;
    Observable<Observable<Integer>> source = Observable.range(0, n).map(new Function<Integer, Observable<Integer>>() {

        @Override
        public Observable<Integer> apply(Integer v) {
            return Observable.just(v);
        }
    });
    Single<List<Integer>> result = Observable.concat(source).toList();
    SingleObserver<List<Integer>> o = TestHelper.mockSingleObserver();
    InOrder inOrder = inOrder(o);
    result.subscribe(o);
    List<Integer> list = new ArrayList<Integer>(n);
    for (int i = 0; i < n; i++) {
        list.add(i);
    }
    inOrder.verify(o).onSuccess(list);
    verify(o, never()).onError(any(Throwable.class));
}
Also used : InOrder(org.mockito.InOrder) Observable(io.reactivex.Observable) Test(org.junit.Test)

Example 13 with Observable

use of io.reactivex.Observable in project RxJava by ReactiveX.

the class ObservableBufferTest method bufferWithStartEndStartThrows.

@Test
public void bufferWithStartEndStartThrows() {
    PublishSubject<Integer> start = PublishSubject.create();
    Function<Integer, Observable<Integer>> end = new Function<Integer, Observable<Integer>>() {

        @Override
        public Observable<Integer> apply(Integer t1) {
            return Observable.never();
        }
    };
    PublishSubject<Integer> source = PublishSubject.create();
    Observable<List<Integer>> result = source.buffer(start, end);
    Observer<Object> o = TestHelper.mockObserver();
    result.subscribe(o);
    start.onNext(1);
    source.onNext(1);
    source.onNext(2);
    start.onError(new TestException());
    verify(o, never()).onNext(any());
    verify(o, never()).onComplete();
    verify(o).onError(any(TestException.class));
}
Also used : TestException(io.reactivex.exceptions.TestException) Observable(io.reactivex.Observable)

Example 14 with Observable

use of io.reactivex.Observable in project RxJava by ReactiveX.

the class ObservablePublishTest method badSource.

@Test
public void badSource() {
    List<Throwable> errors = TestHelper.trackPluginErrors();
    try {
        new Observable<Integer>() {

            @Override
            protected void subscribeActual(Observer<? super Integer> observer) {
                observer.onSubscribe(Disposables.empty());
                observer.onNext(1);
                observer.onComplete();
                observer.onNext(2);
                observer.onError(new TestException());
                observer.onComplete();
            }
        }.publish().autoConnect().test().assertResult(1);
        TestHelper.assertUndeliverable(errors, 0, TestException.class);
    } finally {
        RxJavaPlugins.reset();
    }
}
Also used : TestException(io.reactivex.exceptions.TestException) TestObserver(io.reactivex.observers.TestObserver) Observer(io.reactivex.Observer) ConnectableObservable(io.reactivex.observables.ConnectableObservable) Observable(io.reactivex.Observable) Test(org.junit.Test)

Example 15 with Observable

use of io.reactivex.Observable in project RxJava by ReactiveX.

the class ObservableDelayTest method testDelayWithObservableSubscriptionFunctionThrows.

@Test
public void testDelayWithObservableSubscriptionFunctionThrows() {
    PublishSubject<Integer> source = PublishSubject.create();
    final PublishSubject<Integer> delay = PublishSubject.create();
    Callable<Observable<Integer>> subFunc = new Callable<Observable<Integer>>() {

        @Override
        public Observable<Integer> call() {
            throw new TestException();
        }
    };
    Function<Integer, Observable<Integer>> delayFunc = new Function<Integer, Observable<Integer>>() {

        @Override
        public Observable<Integer> apply(Integer t1) {
            return delay;
        }
    };
    Observer<Object> o = TestHelper.mockObserver();
    InOrder inOrder = inOrder(o);
    source.delay(Observable.defer(subFunc), delayFunc).subscribe(o);
    source.onNext(1);
    delay.onNext(1);
    source.onNext(2);
    inOrder.verify(o).onError(any(TestException.class));
    inOrder.verifyNoMoreInteractions();
    verify(o, never()).onNext(any());
    verify(o, never()).onComplete();
}
Also used : InOrder(org.mockito.InOrder) TestException(io.reactivex.exceptions.TestException) Observable(io.reactivex.Observable)

Aggregations

Observable (io.reactivex.Observable)88 InOrder (org.mockito.InOrder)27 Test (org.junit.Test)25 TestException (io.reactivex.exceptions.TestException)24 List (java.util.List)17 Observer (io.reactivex.Observer)12 Function (io.reactivex.functions.Function)11 ArrayList (java.util.ArrayList)11 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)11 AndroidSchedulers (io.reactivex.android.schedulers.AndroidSchedulers)10 TestObserver (io.reactivex.observers.TestObserver)10 Bundle (android.os.Bundle)8 Nullable (android.support.annotation.Nullable)8 TimeUnit (java.util.concurrent.TimeUnit)8 LayoutInflater (android.view.LayoutInflater)7 View (android.view.View)7 ViewGroup (android.view.ViewGroup)7 TypeReference (com.fasterxml.jackson.core.type.TypeReference)7 JsonObject (io.vertx.core.json.JsonObject)7 HttpResponse (io.vertx.reactivex.ext.web.client.HttpResponse)7