Search in sources :

Example 21 with io.reactivex.rxjava3.subscribers

use of io.reactivex.rxjava3.subscribers in project RxJava by ReactiveX.

the class ObservableDoOnUnsubscribeTest method doOnUnsubscribe.

@Test
public void doOnUnsubscribe() 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<>();
    List<TestObserver<Long>> subscribers = new ArrayList<>();
    for (int i = 0; i < subCount; ++i) {
        TestObserver<Long> observer = new TestObserver<>();
        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.rxjava3.disposables.Disposable) TestObserver(io.reactivex.rxjava3.observers.TestObserver) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RxJavaTest(io.reactivex.rxjava3.core.RxJavaTest) Test(org.junit.Test)

Example 22 with io.reactivex.rxjava3.subscribers

use of io.reactivex.rxjava3.subscribers in project RxJava by ReactiveX.

the class ObservableDoOnUnsubscribeTest method doOnUnSubscribeWorksWithRefCount.

@Test
public void doOnUnSubscribeWorksWithRefCount() 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<>();
    List<TestObserver<Long>> subscribers = new ArrayList<>();
    for (int i = 0; i < subCount; ++i) {
        TestObserver<Long> observer = new TestObserver<>();
        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.rxjava3.disposables.Disposable) TestObserver(io.reactivex.rxjava3.observers.TestObserver) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RxJavaTest(io.reactivex.rxjava3.core.RxJavaTest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)19 RxMethod (io.reactivex.rxjava3.validators.BaseTypeParser.RxMethod)6 TestException (io.reactivex.rxjava3.exceptions.TestException)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 BooleanSubscription (io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription)4 TestSubscriber (io.reactivex.rxjava3.subscribers.TestSubscriber)4 RxJavaTest (io.reactivex.rxjava3.core.RxJavaTest)3 TestObserver (io.reactivex.rxjava3.observers.TestObserver)3 Disposable (io.reactivex.rxjava3.disposables.Disposable)2 Pattern (java.util.regex.Pattern)2 Subscriber (org.reactivestreams.Subscriber)2 SchedulerMultiWorkerSupport (io.reactivex.rxjava3.internal.schedulers.SchedulerMultiWorkerSupport)1 ConnectConsumer (io.reactivex.rxjava3.internal.util.ConnectConsumer)1 ConditionalSubscriber (io.reactivex.rxjava3.operators.ConditionalSubscriber)1 InOrder (org.mockito.InOrder)1