Search in sources :

Example 96 with Consumer

use of io.reactivex.rxjava3.functions.Consumer in project RxJava by ReactiveX.

the class FlowableDoOnUnsubscribeTest 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();
    Flowable<Long> longs = Flowable.interval(50, TimeUnit.MILLISECONDS).doOnCancel(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();
        }
    }).doOnCancel(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<TestSubscriber<Long>> subscribers = new ArrayList<>();
    for (int i = 0; i < subCount; ++i) {
        TestSubscriber<Long> subscriber = new TestSubscriber<>();
        longs.subscribe(subscriber);
        subscriptions.add(Disposable.fromSubscription(subscriber));
        subscribers.add(subscriber);
    }
    onNextLatch.await();
    for (int i = 0; i < subCount; ++i) {
        subscriptions.get(i).dispose();
    // Test that unsubscribe() method is not affected in any way
    }
    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 : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestSubscriber(io.reactivex.rxjava3.subscribers.TestSubscriber) Test(org.junit.Test)

Example 97 with Consumer

use of io.reactivex.rxjava3.functions.Consumer in project RxJava by ReactiveX.

the class FlowableDoOnUnsubscribeTest 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();
    Flowable<Long> longs = Flowable.interval(50, TimeUnit.MILLISECONDS).doOnCancel(new Action() {

        @Override
        public void run() {
            // Test that upper stream will be notified for un-subscription
            // from a child subscriber
            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();
        }
    }).doOnCancel(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<TestSubscriber<Long>> subscribers = new ArrayList<>();
    for (int i = 0; i < subCount; ++i) {
        TestSubscriber<Long> subscriber = new TestSubscriber<>();
        subscriptions.add(Disposable.fromSubscription(subscriber));
        longs.subscribe(subscriber);
        subscribers.add(subscriber);
    }
    onNextLatch.await();
    for (int i = 0; i < subCount; ++i) {
        subscriptions.get(i).dispose();
    // Test that unsubscribe() method is not affected in any way
    }
    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 : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestSubscriber(io.reactivex.rxjava3.subscribers.TestSubscriber) Test(org.junit.Test)

Example 98 with Consumer

use of io.reactivex.rxjava3.functions.Consumer in project RxJava by ReactiveX.

the class MaybeSafeSubscribeTest method normalEmpty.

@Test
public void normalEmpty() throws Throwable {
    TestHelper.withErrorTracking(errors -> {
        @SuppressWarnings("unchecked") MaybeObserver<Integer> consumer = mock(MaybeObserver.class);
        Maybe.<Integer>empty().safeSubscribe(consumer);
        InOrder order = inOrder(consumer);
        order.verify(consumer).onSubscribe(any(Disposable.class));
        order.verify(consumer).onComplete();
        order.verifyNoMoreInteractions();
    });
}
Also used : Disposable(io.reactivex.rxjava3.disposables.Disposable) InOrder(org.mockito.InOrder) Test(org.junit.Test)

Example 99 with Consumer

use of io.reactivex.rxjava3.functions.Consumer in project RxJava by ReactiveX.

the class MaybeSafeSubscribeTest method onCompleteCrash.

@Test
public void onCompleteCrash() throws Throwable {
    TestHelper.withErrorTracking(errors -> {
        @SuppressWarnings("unchecked") MaybeObserver<Integer> consumer = mock(MaybeObserver.class);
        doThrow(new TestException()).when(consumer).onComplete();
        new Maybe<Integer>() {

            @Override
            protected void subscribeActual(@NonNull MaybeObserver<? super Integer> observer) {
                observer.onSubscribe(Disposable.empty());
                // none of the following should arrive at the consumer
                observer.onComplete();
            }
        }.safeSubscribe(consumer);
        InOrder order = inOrder(consumer);
        order.verify(consumer).onSubscribe(any(Disposable.class));
        order.verify(consumer).onComplete();
        order.verifyNoMoreInteractions();
        TestHelper.assertUndeliverable(errors, 0, TestException.class);
    });
}
Also used : Disposable(io.reactivex.rxjava3.disposables.Disposable) InOrder(org.mockito.InOrder) Test(org.junit.Test)

Example 100 with Consumer

use of io.reactivex.rxjava3.functions.Consumer in project RxJava by ReactiveX.

the class ObservableWindowWithStartEndObservableTest method windowCloseIngoresCancel.

@Test
public void windowCloseIngoresCancel() {
    List<Throwable> errors = TestHelper.trackPluginErrors();
    try {
        BehaviorSubject.createDefault(1).window(BehaviorSubject.createDefault(1), new Function<Integer, Observable<Integer>>() {

            @Override
            public Observable<Integer> apply(Integer f) throws Exception {
                return new Observable<Integer>() {

                    @Override
                    protected void subscribeActual(Observer<? super Integer> observer) {
                        observer.onSubscribe(Disposable.empty());
                        observer.onNext(1);
                        observer.onNext(2);
                        observer.onError(new TestException());
                    }
                };
            }
        }).doOnNext(new Consumer<Observable<Integer>>() {

            @Override
            public void accept(Observable<Integer> w) throws Throwable {
                // avoid abandonment
                w.subscribe(Functions.emptyConsumer(), Functions.emptyConsumer());
            }
        }).test().assertValueCount(1).assertNoErrors().assertNotComplete();
        TestHelper.assertUndeliverable(errors, 0, TestException.class);
    } finally {
        RxJavaPlugins.reset();
    }
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) Observer(io.reactivex.rxjava3.core.Observer) Observable(io.reactivex.rxjava3.core.Observable)

Aggregations

Test (org.junit.Test)98 TestException (io.reactivex.rxjava3.exceptions.TestException)57 Disposable (io.reactivex.rxjava3.disposables.Disposable)39 BooleanSubscription (io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription)22 IOException (java.io.IOException)20 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)19 InOrder (org.mockito.InOrder)17 TestObserver (io.reactivex.rxjava3.observers.TestObserver)9 TestSubscriber (io.reactivex.rxjava3.subscribers.TestSubscriber)8 RxJavaTest (io.reactivex.rxjava3.core.RxJavaTest)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 Observable (io.reactivex.rxjava3.core.Observable)5 Worker (io.reactivex.rxjava3.core.Scheduler.Worker)5 Consumer (io.reactivex.rxjava3.functions.Consumer)5 CompositeException (io.reactivex.rxjava3.exceptions.CompositeException)4 ForEachWhileSubscriber (io.reactivex.rxjava3.internal.subscribers.ForEachWhileSubscriber)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 Observer (io.reactivex.rxjava3.core.Observer)3 GroupedFlowable (io.reactivex.rxjava3.flowables.GroupedFlowable)3 ArgsToString (io.reactivex.rxjava3.internal.operators.flowable.FlowableZipTest.ArgsToString)3