Search in sources :

Example 31 with Function

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

the class FlowableOnErrorReturnTest method normalBackpressure.

@Test
public void normalBackpressure() {
    TestSubscriber<Integer> ts = TestSubscriber.create(0);
    PublishProcessor<Integer> pp = PublishProcessor.create();
    pp.onErrorReturn(new Function<Throwable, Integer>() {

        @Override
        public Integer apply(Throwable e) {
            return 3;
        }
    }).subscribe(ts);
    ts.request(2);
    pp.onNext(1);
    pp.onNext(2);
    pp.onError(new TestException("Forced failure"));
    ts.assertValues(1, 2);
    ts.assertNoErrors();
    ts.assertNotComplete();
    ts.request(2);
    ts.assertValues(1, 2, 3);
    ts.assertNoErrors();
    ts.assertComplete();
}
Also used : Function(io.reactivex.rxjava3.functions.Function) TestException(io.reactivex.rxjava3.exceptions.TestException) Test(org.junit.Test)

Example 32 with Function

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

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

use of io.reactivex.rxjava3.functions.Function 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)

Example 35 with Function

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

the class FlowableGroupByTest method firstGroupsCompleteAndParentSlowToThenEmitFinalGroupsAndThenComplete.

@Test
public void firstGroupsCompleteAndParentSlowToThenEmitFinalGroupsAndThenComplete() throws InterruptedException {
    // there are two groups to first complete
    final CountDownLatch first = new CountDownLatch(2);
    final ArrayList<String> results = new ArrayList<>();
    Flowable.unsafeCreate(new Publisher<Integer>() {

        @Override
        public void subscribe(Subscriber<? super Integer> sub) {
            sub.onSubscribe(new BooleanSubscription());
            sub.onNext(1);
            sub.onNext(2);
            sub.onNext(1);
            sub.onNext(2);
            try {
                first.await();
            } catch (InterruptedException e) {
                sub.onError(e);
                return;
            }
            sub.onNext(3);
            sub.onNext(3);
            sub.onComplete();
        }
    }).groupBy(new Function<Integer, Integer>() {

        @Override
        public Integer apply(Integer t) {
            return t;
        }
    }).flatMap(new Function<GroupedFlowable<Integer, Integer>, Flowable<String>>() {

        @Override
        public Flowable<String> apply(final GroupedFlowable<Integer, Integer> group) {
            if (group.getKey() < 3) {
                return group.map(new Function<Integer, String>() {

                    @Override
                    public String apply(Integer t1) {
                        return "first groups: " + t1;
                    }
                }).take(2).doOnComplete(new Action() {

                    @Override
                    public void run() {
                        first.countDown();
                    }
                });
            } else {
                return group.map(new Function<Integer, String>() {

                    @Override
                    public String apply(Integer t1) {
                        return "last group: " + t1;
                    }
                });
            }
        }
    }).blockingForEach(new Consumer<String>() {

        @Override
        public void accept(String s) {
            results.add(s);
        }
    });
    System.out.println("Results: " + results);
    assertEquals(6, results.size());
}
Also used : BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) GroupedFlowable(io.reactivex.rxjava3.flowables.GroupedFlowable) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)97 TestException (io.reactivex.rxjava3.exceptions.TestException)78 Observable (io.reactivex.rxjava3.core.Observable)41 InOrder (org.mockito.InOrder)36 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)21 Function (io.reactivex.rxjava3.functions.Function)20 BooleanSubscription (io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription)18 AtomicReference (java.util.concurrent.atomic.AtomicReference)14 IOException (java.io.IOException)13 Disposable (io.reactivex.rxjava3.disposables.Disposable)10 Worker (io.reactivex.rxjava3.core.Scheduler.Worker)9 TestObserver (io.reactivex.rxjava3.observers.TestObserver)9 TestSubscriber (io.reactivex.rxjava3.subscribers.TestSubscriber)9 GroupedFlowable (io.reactivex.rxjava3.flowables.GroupedFlowable)8 CountingRunnable (io.reactivex.rxjava3.android.testutil.CountingRunnable)7 Observer (io.reactivex.rxjava3.core.Observer)7 Function (org.apache.cassandra.cql3.functions.Function)7 ImmediateThinScheduler (io.reactivex.rxjava3.internal.schedulers.ImmediateThinScheduler)6 TestHelper (io.reactivex.rxjava3.testsupport.TestHelper)6 EmptyScheduler (io.reactivex.rxjava3.android.testutil.EmptyScheduler)5