Search in sources :

Example 41 with Disposable

use of io.reactivex.disposables.Disposable in project RxJava by ReactiveX.

the class ObservableDelaySubscriptionOtherTest method testNoPrematureSubscription.

@Test
public void testNoPrematureSubscription() {
    PublishSubject<Object> other = PublishSubject.create();
    TestObserver<Integer> ts = new TestObserver<Integer>();
    final AtomicInteger subscribed = new AtomicInteger();
    Observable.just(1).doOnSubscribe(new Consumer<Disposable>() {

        @Override
        public void accept(Disposable d) {
            subscribed.getAndIncrement();
        }
    }).delaySubscription(other).subscribe(ts);
    ts.assertNotComplete();
    ts.assertNoErrors();
    ts.assertNoValues();
    Assert.assertEquals("Premature subscription", 0, subscribed.get());
    other.onNext(1);
    Assert.assertEquals("No subscription", 1, subscribed.get());
    ts.assertValue(1);
    ts.assertNoErrors();
    ts.assertComplete();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Disposable(io.reactivex.disposables.Disposable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestObserver(io.reactivex.observers.TestObserver)

Example 42 with Disposable

use of io.reactivex.disposables.Disposable in project RxJava by ReactiveX.

the class ObservableOnErrorResumeNextViaObservableTest method testMapResumeAsyncNext.

@Test
public void testMapResumeAsyncNext() {
    Disposable sr = mock(Disposable.class);
    // Trigger multiple failures
    Observable<String> w = Observable.just("one", "fail", "two", "three", "fail");
    // Resume Observable is async
    TestObservable f = new TestObservable(sr, "twoResume", "threeResume");
    Observable<String> resume = Observable.unsafeCreate(f);
    // Introduce map function that fails intermittently (Map does not prevent this when the Observer is a
    //  rx.operator incl onErrorResumeNextViaObservable)
    w = w.map(new Function<String, String>() {

        @Override
        public String apply(String s) {
            if ("fail".equals(s)) {
                throw new RuntimeException("Forced Failure");
            }
            System.out.println("BadMapper:" + s);
            return s;
        }
    });
    Observable<String> observable = w.onErrorResumeNext(resume);
    Observer<String> observer = TestHelper.mockObserver();
    observable.subscribe(observer);
    try {
        f.t.join();
    } catch (InterruptedException e) {
        fail(e.getMessage());
    }
    verify(observer, Mockito.never()).onError(any(Throwable.class));
    verify(observer, times(1)).onComplete();
    verify(observer, times(1)).onNext("one");
    verify(observer, Mockito.never()).onNext("two");
    verify(observer, Mockito.never()).onNext("three");
    verify(observer, times(1)).onNext("twoResume");
    verify(observer, times(1)).onNext("threeResume");
}
Also used : Disposable(io.reactivex.disposables.Disposable) Function(io.reactivex.functions.Function)

Example 43 with Disposable

use of io.reactivex.disposables.Disposable in project RxJava by ReactiveX.

the class ObservableDelaySubscriptionOtherTest method testNoMultipleSubscriptions.

@Test
public void testNoMultipleSubscriptions() {
    PublishSubject<Object> other = PublishSubject.create();
    TestObserver<Integer> ts = new TestObserver<Integer>();
    final AtomicInteger subscribed = new AtomicInteger();
    Observable.just(1).doOnSubscribe(new Consumer<Disposable>() {

        @Override
        public void accept(Disposable d) {
            subscribed.getAndIncrement();
        }
    }).delaySubscription(other).subscribe(ts);
    ts.assertNotComplete();
    ts.assertNoErrors();
    ts.assertNoValues();
    Assert.assertEquals("Premature subscription", 0, subscribed.get());
    other.onNext(1);
    other.onNext(2);
    Assert.assertEquals("No subscription", 1, subscribed.get());
    ts.assertValue(1);
    ts.assertNoErrors();
    ts.assertComplete();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Disposable(io.reactivex.disposables.Disposable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestObserver(io.reactivex.observers.TestObserver)

Example 44 with Disposable

use of io.reactivex.disposables.Disposable in project RxJava by ReactiveX.

the class ObservableDoOnUnsubscribeTest method testDoOnUnSubscribeWorksWithRefCount.

@Test
public void testDoOnUnSubscribeWorksWithRefCount() throws Exception {
    int subCount = 3;
    final CountDownLatch upperLatch = new CountDownLatch(1);
    final CountDownLatch lowerLatch = new CountDownLatch(1);
    final CountDownLatch onNextLatch = new CountDownLatch(subCount);
    final AtomicInteger upperCount = new AtomicInteger();
    final AtomicInteger lowerCount = new AtomicInteger();
    Observable<Long> longs = Observable.interval(50, TimeUnit.MILLISECONDS).doOnDispose(new Action() {

        @Override
        public void run() {
            // Test that upper stream will be notified for un-subscription
            upperLatch.countDown();
            upperCount.incrementAndGet();
        }
    }).doOnNext(new Consumer<Long>() {

        @Override
        public void accept(Long aLong) {
            // Ensure there is at least some onNext events before un-subscription happens
            onNextLatch.countDown();
        }
    }).doOnDispose(new Action() {

        @Override
        public void run() {
            // Test that lower stream will be notified for un-subscription
            lowerLatch.countDown();
            lowerCount.incrementAndGet();
        }
    }).publish().refCount();
    List<Disposable> subscriptions = new ArrayList<Disposable>();
    List<TestObserver<Long>> subscribers = new ArrayList<TestObserver<Long>>();
    for (int i = 0; i < subCount; ++i) {
        TestObserver<Long> observer = new TestObserver<Long>();
        longs.subscribe(observer);
        subscriptions.add(observer);
        subscribers.add(observer);
    }
    onNextLatch.await();
    for (int i = 0; i < subCount; ++i) {
        subscriptions.get(i).dispose();
    // Test that unsubscribe() method is not affected in any way
    // FIXME no longer valid
    //            subscribers.get(i).assertUnsubscribed();
    }
    upperLatch.await();
    lowerLatch.await();
    assertEquals("There should exactly 1 un-subscription events for upper stream", 1, upperCount.get());
    assertEquals("There should exactly 1 un-subscription events for lower stream", 1, lowerCount.get());
}
Also used : Disposable(io.reactivex.disposables.Disposable) TestObserver(io.reactivex.observers.TestObserver) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 45 with Disposable

use of io.reactivex.disposables.Disposable in project RxJava by ReactiveX.

the class ObservableDoOnUnsubscribeTest method testDoOnUnsubscribe.

@Test
public void testDoOnUnsubscribe() throws Exception {
    int subCount = 3;
    final CountDownLatch upperLatch = new CountDownLatch(subCount);
    final CountDownLatch lowerLatch = new CountDownLatch(subCount);
    final CountDownLatch onNextLatch = new CountDownLatch(subCount);
    final AtomicInteger upperCount = new AtomicInteger();
    final AtomicInteger lowerCount = new AtomicInteger();
    Observable<Long> longs = Observable.interval(50, TimeUnit.MILLISECONDS).doOnDispose(new Action() {

        @Override
        public void run() {
            // Test that upper stream will be notified for un-subscription
            // from a child Observer
            upperLatch.countDown();
            upperCount.incrementAndGet();
        }
    }).doOnNext(new Consumer<Long>() {

        @Override
        public void accept(Long aLong) {
            // Ensure there is at least some onNext events before un-subscription happens
            onNextLatch.countDown();
        }
    }).doOnDispose(new Action() {

        @Override
        public void run() {
            // Test that lower stream will be notified for a direct un-subscription
            lowerLatch.countDown();
            lowerCount.incrementAndGet();
        }
    });
    List<Disposable> subscriptions = new ArrayList<Disposable>();
    List<TestObserver<Long>> subscribers = new ArrayList<TestObserver<Long>>();
    for (int i = 0; i < subCount; ++i) {
        TestObserver<Long> observer = new TestObserver<Long>();
        subscriptions.add(observer);
        longs.subscribe(observer);
        subscribers.add(observer);
    }
    onNextLatch.await();
    for (int i = 0; i < subCount; ++i) {
        subscriptions.get(i).dispose();
    // Test that unsubscribe() method is not affected in any way
    // FIXME no longer valid
    //            subscribers.get(i).assertUnsubscribed();
    }
    upperLatch.await();
    lowerLatch.await();
    assertEquals(String.format("There should exactly %d un-subscription events for upper stream", subCount), subCount, upperCount.get());
    assertEquals(String.format("There should exactly %d un-subscription events for lower stream", subCount), subCount, lowerCount.get());
}
Also used : Disposable(io.reactivex.disposables.Disposable) TestObserver(io.reactivex.observers.TestObserver) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Aggregations

Disposable (io.reactivex.disposables.Disposable)224 Test (org.junit.Test)95 CompositeDisposable (io.reactivex.disposables.CompositeDisposable)31 Logger (chat.rocket.android.helper.Logger)19 AndroidSchedulers (io.reactivex.android.schedulers.AndroidSchedulers)19 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)18 Worker (io.reactivex.Scheduler.Worker)15 List (java.util.List)14 Single (io.reactivex.Single)13 Function (io.reactivex.functions.Function)13 NonNull (io.reactivex.annotations.NonNull)12 Nullable (android.support.annotation.Nullable)11 Bundle (android.os.Bundle)10 CountingRunnable (io.reactivex.android.testutil.CountingRunnable)10 EmptyDisposable (io.reactivex.internal.disposables.EmptyDisposable)10 TestSubscriber (io.reactivex.subscribers.TestSubscriber)10 Optional (com.fernandocejas.arrow.optional.Optional)9 Schedulers (io.reactivex.schedulers.Schedulers)9 TimeUnit (java.util.concurrent.TimeUnit)8 NonNull (android.support.annotation.NonNull)7