Search in sources :

Example 21 with Observable

use of io.reactivex.Observable 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)

Example 22 with Observable

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

the class ObservableReplayTest 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.onError(new TestException("First"));
                observer.onNext(1);
                observer.onError(new TestException("Second"));
                observer.onComplete();
            }
        }.replay().autoConnect().test().assertFailureAndMessage(TestException.class, "First");
        TestHelper.assertUndeliverable(errors, 0, TestException.class, "Second");
    } finally {
        RxJavaPlugins.reset();
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestException(io.reactivex.exceptions.TestException) TestObserver(io.reactivex.observers.TestObserver) Observer(io.reactivex.Observer) ConnectableObservable(io.reactivex.observables.ConnectableObservable) Observable(io.reactivex.Observable)

Example 23 with Observable

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

the class ObservableReplayTest method testSynchronousDisconnect.

@Test
public void testSynchronousDisconnect() {
    final AtomicInteger effectCounter = new AtomicInteger();
    Observable<Integer> source = Observable.just(1, 2, 3, 4).doOnNext(new Consumer<Integer>() {

        @Override
        public void accept(Integer v) {
            effectCounter.incrementAndGet();
            System.out.println("Sideeffect #" + v);
        }
    });
    Observable<Integer> result = source.replay(new Function<Observable<Integer>, Observable<Integer>>() {

        @Override
        public Observable<Integer> apply(Observable<Integer> o) {
            return o.take(2);
        }
    });
    for (int i = 1; i < 3; i++) {
        effectCounter.set(0);
        System.out.printf("- %d -%n", i);
        result.subscribe(new Consumer<Integer>() {

            @Override
            public void accept(Integer t1) {
                System.out.println(t1);
            }
        }, new Consumer<Throwable>() {

            @Override
            public void accept(Throwable t1) {
                t1.printStackTrace();
            }
        }, new Action() {

            @Override
            public void run() {
                System.out.println("Done");
            }
        });
        assertEquals(2, effectCounter.get());
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConnectableObservable(io.reactivex.observables.ConnectableObservable) Observable(io.reactivex.Observable)

Example 24 with Observable

use of io.reactivex.Observable 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 25 with Observable

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

the class ObservableTimeoutWithSelectorTest method testTimeoutSelectorNormal1.

@Test(timeout = 2000)
public void testTimeoutSelectorNormal1() {
    PublishSubject<Integer> source = PublishSubject.create();
    final PublishSubject<Integer> timeout = PublishSubject.create();
    Function<Integer, Observable<Integer>> timeoutFunc = new Function<Integer, Observable<Integer>>() {

        @Override
        public Observable<Integer> apply(Integer t1) {
            return timeout;
        }
    };
    Observable<Integer> other = Observable.fromIterable(Arrays.asList(100));
    Observer<Object> o = TestHelper.mockObserver();
    InOrder inOrder = inOrder(o);
    source.timeout(timeout, timeoutFunc, other).subscribe(o);
    source.onNext(1);
    source.onNext(2);
    source.onNext(3);
    timeout.onNext(1);
    inOrder.verify(o).onNext(1);
    inOrder.verify(o).onNext(2);
    inOrder.verify(o).onNext(3);
    inOrder.verify(o).onNext(100);
    inOrder.verify(o).onComplete();
    verify(o, never()).onError(any(Throwable.class));
}
Also used : Function(io.reactivex.functions.Function) InOrder(org.mockito.InOrder) Observable(io.reactivex.Observable) Test(org.junit.Test)

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