Search in sources :

Example 21 with Flowable

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

the class FlowableRetryTest method singleSubscriptionOnFirst.

@Test
public void singleSubscriptionOnFirst() throws Exception {
    final AtomicInteger inc = new AtomicInteger(0);
    Publisher<Integer> onSubscribe = new Publisher<Integer>() {

        @Override
        public void subscribe(Subscriber<? super Integer> subscriber) {
            subscriber.onSubscribe(new BooleanSubscription());
            final int emit = inc.incrementAndGet();
            subscriber.onNext(emit);
            subscriber.onComplete();
        }
    };
    int first = Flowable.unsafeCreate(onSubscribe).retryWhen(new Function<Flowable<? extends Throwable>, Flowable<Object>>() {

        @Override
        public Flowable<Object> apply(Flowable<? extends Throwable> attempt) {
            return attempt.zipWith(Flowable.just(1), new BiFunction<Throwable, Integer, Object>() {

                @Override
                public Object apply(Throwable o, Integer integer) {
                    return 0;
                }
            });
        }
    }).blockingFirst();
    assertEquals("Observer did not receive the expected output", 1, first);
    assertEquals("Subscribe was not called once", 1, inc.get());
}
Also used : BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) GroupedFlowable(io.reactivex.rxjava3.flowables.GroupedFlowable) Test(org.junit.Test)

Example 22 with Flowable

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

the class FlowableRetryTest method repeatFloodNoSubscriptionError.

@Test
public void repeatFloodNoSubscriptionError() {
    List<Throwable> errors = TestHelper.trackPluginErrors();
    final TestException error = new TestException();
    try {
        final PublishProcessor<Integer> source = PublishProcessor.create();
        final PublishProcessor<Integer> signaller = PublishProcessor.create();
        for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
            TestSubscriber<Integer> ts = source.take(1).map(new Function<Integer, Integer>() {

                @Override
                public Integer apply(Integer v) throws Exception {
                    throw error;
                }
            }).retryWhen(new Function<Flowable<Throwable>, Flowable<Integer>>() {

                @Override
                public Flowable<Integer> apply(Flowable<Throwable> v) throws Exception {
                    return signaller;
                }
            }).test();
            Runnable r1 = new Runnable() {

                @Override
                public void run() {
                    for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
                        source.onNext(1);
                    }
                }
            };
            Runnable r2 = new Runnable() {

                @Override
                public void run() {
                    for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
                        signaller.offer(1);
                    }
                }
            };
            TestHelper.race(r1, r2);
            ts.cancel();
        }
        if (!errors.isEmpty()) {
            for (Throwable e : errors) {
                e.printStackTrace();
            }
            fail(errors + "");
        }
    } finally {
        RxJavaPlugins.reset();
    }
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) TestException(io.reactivex.rxjava3.exceptions.TestException) GroupedFlowable(io.reactivex.rxjava3.flowables.GroupedFlowable) Test(org.junit.Test)

Example 23 with Flowable

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

the class FlowableRetryTest method shouldDisposeInnerFlowable.

@Test
public void shouldDisposeInnerFlowable() {
    final PublishProcessor<Object> processor = PublishProcessor.create();
    final Disposable disposable = Flowable.error(new RuntimeException("Leak")).retryWhen(new Function<Flowable<Throwable>, Flowable<Object>>() {

        @Override
        public Flowable<Object> apply(Flowable<Throwable> errors) throws Exception {
            return errors.switchMap(new Function<Throwable, Flowable<Object>>() {

                @Override
                public Flowable<Object> apply(Throwable ignore) throws Exception {
                    return processor;
                }
            });
        }
    }).subscribe();
    assertTrue(processor.hasSubscribers());
    disposable.dispose();
    assertFalse(processor.hasSubscribers());
}
Also used : Disposable(io.reactivex.rxjava3.disposables.Disposable) TestException(io.reactivex.rxjava3.exceptions.TestException) GroupedFlowable(io.reactivex.rxjava3.flowables.GroupedFlowable) Test(org.junit.Test)

Example 24 with Flowable

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

the class FlowableRetryTest method noCancelPreviousRetryWhile.

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

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

        @Override
        public void run() throws Exception {
            counter.getAndIncrement();
        }
    });
    source.retry(5, Functions.alwaysTrue()).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 25 with Flowable

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

the class FlowableRetryTest method retryWhenTrampolineScheduler.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void retryWhenTrampolineScheduler() {
    TestSubscriber<Integer> ts = TestSubscriber.create();
    Flowable.just(1).concatWith(Flowable.<Integer>error(new TestException())).subscribeOn(Schedulers.trampoline()).retryWhen((Function) new Function<Flowable, Flowable>() {

        @Override
        public Flowable apply(Flowable f) {
            return f.take(2);
        }
    }).subscribe(ts);
    ts.assertValues(1, 1);
    ts.assertNoErrors();
    ts.assertComplete();
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) GroupedFlowable(io.reactivex.rxjava3.flowables.GroupedFlowable) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)177 BooleanSubscription (io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription)121 TestException (io.reactivex.rxjava3.exceptions.TestException)95 TestSubscriber (io.reactivex.rxjava3.subscribers.TestSubscriber)67 InOrder (org.mockito.InOrder)41 IOException (java.io.IOException)27 TestScheduler (io.reactivex.rxjava3.schedulers.TestScheduler)22 Disposable (io.reactivex.rxjava3.disposables.Disposable)21 Flowable (io.reactivex.rxjava3.core.Flowable)19 TimeUnit (java.util.concurrent.TimeUnit)19 FlowableRxInvokerProvider (org.apache.cxf.jaxrs.rx3.client.FlowableRxInvokerProvider)18 FlowableRxInvoker (org.apache.cxf.jaxrs.rx3.client.FlowableRxInvoker)17 JacksonJsonProvider (com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider)15 InternalServerErrorException (javax.ws.rs.InternalServerErrorException)13 ClientBuilder (javax.ws.rs.client.ClientBuilder)13 MediaType (javax.ws.rs.core.MediaType)13 AbstractResourceInfo (org.apache.cxf.jaxrs.model.AbstractResourceInfo)13 AbstractBusClientServerTestBase (org.apache.cxf.testutil.common.AbstractBusClientServerTestBase)13 Assert.assertTrue (org.junit.Assert.assertTrue)13 BeforeClass (org.junit.BeforeClass)13