Search in sources :

Example 91 with Action

use of io.reactivex.rxjava3.functions.Action 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 92 with Action

use of io.reactivex.rxjava3.functions.Action 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 93 with Action

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

the class ObservableRetryTest method noCancelPreviousRetryUntil.

@Test
public void noCancelPreviousRetryUntil() {
    final AtomicInteger counter = new AtomicInteger();
    final AtomicInteger times = new AtomicInteger();
    Observable<Integer> source = Observable.defer(new Supplier<ObservableSource<Integer>>() {

        @Override
        public ObservableSource<Integer> get() throws Exception {
            if (times.getAndIncrement() < 4) {
                return Observable.error(new TestException());
            }
            return Observable.just(1);
        }
    }).doOnDispose(new Action() {

        @Override
        public void run() throws Exception {
            counter.getAndIncrement();
        }
    });
    source.retryUntil(new BooleanSupplier() {

        @Override
        public boolean getAsBoolean() throws Exception {
            return false;
        }
    }).test().assertResult(1);
    assertEquals(0, counter.get());
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) TestException(io.reactivex.rxjava3.exceptions.TestException) Test(org.junit.Test)

Example 94 with Action

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

the class ObservableUnsubscribeOnTest method error.

@Test
public void error() {
    final int[] calls = { 0 };
    Observable.error(new TestException()).doOnDispose(new Action() {

        @Override
        public void run() throws Exception {
            calls[0]++;
        }
    }).unsubscribeOn(Schedulers.single()).test().assertFailure(TestException.class);
    assertEquals(0, calls[0]);
}
Also used : Action(io.reactivex.rxjava3.functions.Action) TestException(io.reactivex.rxjava3.exceptions.TestException) Test(org.junit.Test)

Example 95 with Action

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

the class ObservableRetryTest method noCancelPreviousRepeatWhen.

@Test
public void noCancelPreviousRepeatWhen() {
    final AtomicInteger counter = new AtomicInteger();
    final AtomicInteger times = new AtomicInteger();
    Observable<Integer> source = Observable.defer(new Supplier<ObservableSource<Integer>>() {

        @Override
        public ObservableSource<Integer> get() throws Exception {
            if (times.get() < 4) {
                return Observable.error(new TestException());
            }
            return Observable.just(1);
        }
    }).doOnDispose(new Action() {

        @Override
        public void run() throws Exception {
            counter.getAndIncrement();
        }
    });
    source.retryWhen(new Function<Observable<Throwable>, ObservableSource<?>>() {

        @Override
        public ObservableSource<?> apply(Observable<Throwable> e) throws Exception {
            return e.takeWhile(new Predicate<Object>() {

                @Override
                public boolean test(Object v) throws Exception {
                    return times.getAndIncrement() < 4;
                }
            });
        }
    }).test().assertResult(1);
    assertEquals(0, counter.get());
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) TestException(io.reactivex.rxjava3.exceptions.TestException) Observable(io.reactivex.rxjava3.core.Observable) GroupedObservable(io.reactivex.rxjava3.observables.GroupedObservable) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)109 TestException (io.reactivex.rxjava3.exceptions.TestException)64 Action (io.reactivex.rxjava3.functions.Action)49 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)23 BooleanSubscription (io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription)13 TestSubscriber (io.reactivex.rxjava3.subscribers.TestSubscriber)13 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)13 TestObserver (io.reactivex.rxjava3.observers.TestObserver)10 Disposable (io.reactivex.rxjava3.disposables.Disposable)9 IOException (java.io.IOException)9 Worker (io.reactivex.rxjava3.core.Scheduler.Worker)7 TestScheduler (io.reactivex.rxjava3.schedulers.TestScheduler)7 RxJavaTest (io.reactivex.rxjava3.core.RxJavaTest)6 Observable (io.reactivex.rxjava3.core.Observable)4 ForEachWhileSubscriber (io.reactivex.rxjava3.internal.subscribers.ForEachWhileSubscriber)4 CountingRunnable (io.reactivex.rxjava3.android.testutil.CountingRunnable)3 Flowable (io.reactivex.rxjava3.core.Flowable)3 GroupedFlowable (io.reactivex.rxjava3.flowables.GroupedFlowable)3 ConditionalSubscriber (io.reactivex.rxjava3.operators.ConditionalSubscriber)3 AtomicLong (java.util.concurrent.atomic.AtomicLong)3