Search in sources :

Example 51 with Flowable

use of io.reactivex.rxjava3.core.Flowable in project RxJava by ReactiveX.

the class FlowableRetryWithPredicateTest method retryTwice.

@Test
public void retryTwice() {
    Flowable<Integer> source = Flowable.unsafeCreate(new Publisher<Integer>() {

        int count;

        @Override
        public void subscribe(Subscriber<? super Integer> t1) {
            t1.onSubscribe(new BooleanSubscription());
            count++;
            t1.onNext(0);
            t1.onNext(1);
            if (count == 1) {
                t1.onError(new TestException());
                return;
            }
            t1.onNext(2);
            t1.onNext(3);
            t1.onComplete();
        }
    });
    Subscriber<Integer> subscriber = TestHelper.mockSubscriber();
    InOrder inOrder = inOrder(subscriber);
    source.retry(retryTwice).subscribe(subscriber);
    inOrder.verify(subscriber).onNext(0);
    inOrder.verify(subscriber).onNext(1);
    inOrder.verify(subscriber).onNext(0);
    inOrder.verify(subscriber).onNext(1);
    inOrder.verify(subscriber).onNext(2);
    inOrder.verify(subscriber).onNext(3);
    inOrder.verify(subscriber).onComplete();
    verify(subscriber, never()).onError(any(Throwable.class));
}
Also used : BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) InOrder(org.mockito.InOrder) Test(org.junit.Test)

Example 52 with Flowable

use of io.reactivex.rxjava3.core.Flowable in project RxJava by ReactiveX.

the class FlowableRetryWithPredicateTest method retryTwiceAndGiveUp.

@Test
public void retryTwiceAndGiveUp() {
    Flowable<Integer> source = Flowable.unsafeCreate(new Publisher<Integer>() {

        @Override
        public void subscribe(Subscriber<? super Integer> t1) {
            t1.onSubscribe(new BooleanSubscription());
            t1.onNext(0);
            t1.onNext(1);
            t1.onError(new TestException());
        }
    });
    Subscriber<Integer> subscriber = TestHelper.mockSubscriber();
    InOrder inOrder = inOrder(subscriber);
    source.retry(retryTwice).subscribe(subscriber);
    inOrder.verify(subscriber).onNext(0);
    inOrder.verify(subscriber).onNext(1);
    inOrder.verify(subscriber).onNext(0);
    inOrder.verify(subscriber).onNext(1);
    inOrder.verify(subscriber).onNext(0);
    inOrder.verify(subscriber).onNext(1);
    inOrder.verify(subscriber).onError(any(TestException.class));
    verify(subscriber, never()).onComplete();
}
Also used : BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) InOrder(org.mockito.InOrder) Test(org.junit.Test)

Example 53 with Flowable

use of io.reactivex.rxjava3.core.Flowable in project RxJava by ReactiveX.

the class FlowableSubscribeOnTest method deferredRequestRace.

@Test
public void deferredRequestRace() {
    for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
        final TestSubscriber<Integer> ts = new TestSubscriber<>(0L);
        Worker w = Schedulers.computation().createWorker();
        final SubscribeOnSubscriber<Integer> so = new SubscribeOnSubscriber<>(ts, w, Flowable.<Integer>never(), true);
        ts.onSubscribe(so);
        final BooleanSubscription bs = new BooleanSubscription();
        try {
            Runnable r1 = new Runnable() {

                @Override
                public void run() {
                    so.onSubscribe(bs);
                }
            };
            Runnable r2 = new Runnable() {

                @Override
                public void run() {
                    so.request(1);
                }
            };
            TestHelper.race(r1, r2);
        } finally {
            w.dispose();
        }
    }
}
Also used : BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) SubscribeOnSubscriber(io.reactivex.rxjava3.internal.operators.flowable.FlowableSubscribeOn.SubscribeOnSubscriber) Worker(io.reactivex.rxjava3.core.Scheduler.Worker) Test(org.junit.Test)

Example 54 with Flowable

use of io.reactivex.rxjava3.core.Flowable in project RxJava by ReactiveX.

the class FlowableSwitchTest method switchWithError.

@Test
public void switchWithError() {
    Flowable<Flowable<String>> source = Flowable.unsafeCreate(new Publisher<Flowable<String>>() {

        @Override
        public void subscribe(Subscriber<? super Flowable<String>> subscriber) {
            subscriber.onSubscribe(new BooleanSubscription());
            publishNext(subscriber, 50, Flowable.unsafeCreate(new Publisher<String>() {

                @Override
                public void subscribe(final Subscriber<? super String> subscriber) {
                    subscriber.onSubscribe(new BooleanSubscription());
                    publishNext(subscriber, 50, "one");
                    publishNext(subscriber, 100, "two");
                }
            }));
            publishNext(subscriber, 200, Flowable.unsafeCreate(new Publisher<String>() {

                @Override
                public void subscribe(Subscriber<? super String> subscriber) {
                    subscriber.onSubscribe(new BooleanSubscription());
                    publishNext(subscriber, 0, "three");
                    publishNext(subscriber, 100, "four");
                }
            }));
            publishError(subscriber, 250, new TestException());
        }
    });
    Flowable<String> sampled = Flowable.switchOnNext(source);
    sampled.subscribe(subscriber);
    InOrder inOrder = inOrder(subscriber);
    scheduler.advanceTimeTo(90, TimeUnit.MILLISECONDS);
    inOrder.verify(subscriber, never()).onNext(anyString());
    verify(subscriber, never()).onComplete();
    verify(subscriber, never()).onError(any(Throwable.class));
    scheduler.advanceTimeTo(125, TimeUnit.MILLISECONDS);
    inOrder.verify(subscriber, times(1)).onNext("one");
    verify(subscriber, never()).onComplete();
    verify(subscriber, never()).onError(any(Throwable.class));
    scheduler.advanceTimeTo(175, TimeUnit.MILLISECONDS);
    inOrder.verify(subscriber, times(1)).onNext("two");
    verify(subscriber, never()).onComplete();
    verify(subscriber, never()).onError(any(Throwable.class));
    scheduler.advanceTimeTo(225, TimeUnit.MILLISECONDS);
    inOrder.verify(subscriber, times(1)).onNext("three");
    verify(subscriber, never()).onComplete();
    verify(subscriber, never()).onError(any(Throwable.class));
    scheduler.advanceTimeTo(350, TimeUnit.MILLISECONDS);
    inOrder.verify(subscriber, never()).onNext(anyString());
    verify(subscriber, never()).onComplete();
    verify(subscriber, times(1)).onError(any(TestException.class));
}
Also used : BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) InOrder(org.mockito.InOrder)

Example 55 with Flowable

use of io.reactivex.rxjava3.core.Flowable in project RxJava by ReactiveX.

the class FlowableSwitchTest method switchIssue737.

@Test
public void switchIssue737() {
    // https://github.com/ReactiveX/RxJava/issues/737
    Flowable<Flowable<String>> source = Flowable.unsafeCreate(new Publisher<Flowable<String>>() {

        @Override
        public void subscribe(Subscriber<? super Flowable<String>> subscriber) {
            subscriber.onSubscribe(new BooleanSubscription());
            publishNext(subscriber, 0, Flowable.unsafeCreate(new Publisher<String>() {

                @Override
                public void subscribe(Subscriber<? super String> subscriber) {
                    subscriber.onSubscribe(new BooleanSubscription());
                    publishNext(subscriber, 10, "1-one");
                    publishNext(subscriber, 20, "1-two");
                    // The following events will be ignored
                    publishNext(subscriber, 30, "1-three");
                    publishCompleted(subscriber, 40);
                }
            }));
            publishNext(subscriber, 25, Flowable.unsafeCreate(new Publisher<String>() {

                @Override
                public void subscribe(Subscriber<? super String> subscriber) {
                    subscriber.onSubscribe(new BooleanSubscription());
                    publishNext(subscriber, 10, "2-one");
                    publishNext(subscriber, 20, "2-two");
                    publishNext(subscriber, 30, "2-three");
                    publishCompleted(subscriber, 40);
                }
            }));
            publishCompleted(subscriber, 30);
        }
    });
    Flowable<String> sampled = Flowable.switchOnNext(source);
    sampled.subscribe(subscriber);
    scheduler.advanceTimeTo(1000, TimeUnit.MILLISECONDS);
    InOrder inOrder = inOrder(subscriber);
    inOrder.verify(subscriber, times(1)).onNext("1-one");
    inOrder.verify(subscriber, times(1)).onNext("1-two");
    inOrder.verify(subscriber, times(1)).onNext("2-one");
    inOrder.verify(subscriber, times(1)).onNext("2-two");
    inOrder.verify(subscriber, times(1)).onNext("2-three");
    inOrder.verify(subscriber, times(1)).onComplete();
    inOrder.verifyNoMoreInteractions();
}
Also used : BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) InOrder(org.mockito.InOrder)

Aggregations

Test (org.junit.Test)159 BooleanSubscription (io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription)121 TestException (io.reactivex.rxjava3.exceptions.TestException)95 TestSubscriber (io.reactivex.rxjava3.subscribers.TestSubscriber)49 InOrder (org.mockito.InOrder)41 IOException (java.io.IOException)27 TestScheduler (io.reactivex.rxjava3.schedulers.TestScheduler)22 Disposable (io.reactivex.rxjava3.disposables.Disposable)21 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)12 io.reactivex.rxjava3.core (io.reactivex.rxjava3.core)11 io.reactivex.rxjava3.testsupport (io.reactivex.rxjava3.testsupport)9 java.util (java.util)9 Assert (org.junit.Assert)9 Subscriber (org.reactivestreams.Subscriber)9 GroupedFlowable (io.reactivex.rxjava3.flowables.GroupedFlowable)8 java.util.concurrent.atomic (java.util.concurrent.atomic)8 Worker (io.reactivex.rxjava3.core.Scheduler.Worker)7 io.reactivex.rxjava3.functions (io.reactivex.rxjava3.functions)7 Functions (io.reactivex.rxjava3.internal.functions.Functions)7 RxJavaPlugins (io.reactivex.rxjava3.plugins.RxJavaPlugins)7