Search in sources :

Example 31 with Supplier

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

the class ObservableCollectWithCollectorTest method collectorAccumulatorDropSignals.

@Test
public void collectorAccumulatorDropSignals() throws Throwable {
    TestHelper.withErrorTracking(errors -> {
        Observable<Integer> source = new Observable<Integer>() {

            @Override
            protected void subscribeActual(Observer<? super Integer> observer) {
                observer.onSubscribe(Disposable.empty());
                observer.onNext(1);
                observer.onNext(2);
                observer.onError(new IOException());
                observer.onComplete();
            }
        };
        source.collect(new Collector<Integer, Integer, Integer>() {

            @Override
            public Supplier<Integer> supplier() {
                return () -> 1;
            }

            @Override
            public BiConsumer<Integer, Integer> accumulator() {
                return (a, b) -> {
                    throw new TestException();
                };
            }

            @Override
            public BinaryOperator<Integer> combiner() {
                return (a, b) -> a + b;
            }

            @Override
            public Function<Integer, Integer> finisher() {
                return a -> a;
            }

            @Override
            public Set<Characteristics> characteristics() {
                return Collections.emptySet();
            }
        }).test().assertFailure(TestException.class);
        TestHelper.assertUndeliverable(errors, 0, IOException.class);
    });
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) IOException(java.io.IOException) Observable(io.reactivex.rxjava3.core.Observable) Observer(io.reactivex.rxjava3.core.Observer) TestObserver(io.reactivex.rxjava3.observers.TestObserver) RxJavaTest(io.reactivex.rxjava3.core.RxJavaTest) Test(org.junit.Test)

Example 32 with Supplier

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

the class FlowableCollectWithCollectorTest method collectorAccumulatorDropSignalsToFlowable.

@Test
public void collectorAccumulatorDropSignalsToFlowable() throws Throwable {
    TestHelper.withErrorTracking(errors -> {
        Flowable<Integer> source = new Flowable<Integer>() {

            @Override
            protected void subscribeActual(Subscriber<? super Integer> s) {
                s.onSubscribe(new BooleanSubscription());
                s.onNext(1);
                s.onNext(2);
                s.onError(new IOException());
                s.onComplete();
            }
        };
        source.collect(new Collector<Integer, Integer, Integer>() {

            @Override
            public Supplier<Integer> supplier() {
                return () -> 1;
            }

            @Override
            public BiConsumer<Integer, Integer> accumulator() {
                return (a, b) -> {
                    throw new TestException();
                };
            }

            @Override
            public BinaryOperator<Integer> combiner() {
                return (a, b) -> a + b;
            }

            @Override
            public Function<Integer, Integer> finisher() {
                return a -> a;
            }

            @Override
            public Set<Characteristics> characteristics() {
                return Collections.emptySet();
            }
        }).toFlowable().test().assertFailure(TestException.class);
        TestHelper.assertUndeliverable(errors, 0, IOException.class);
    });
}
Also used : java.util(java.util) TestHelper(io.reactivex.rxjava3.testsupport.TestHelper) TestException(io.reactivex.rxjava3.exceptions.TestException) Assert.assertFalse(org.junit.Assert.assertFalse) java.util.stream(java.util.stream) IOException(java.io.IOException) Test(org.junit.Test) java.util.function(java.util.function) io.reactivex.rxjava3.core(io.reactivex.rxjava3.core) Subscriber(org.reactivestreams.Subscriber) BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) io.reactivex.rxjava3.processors(io.reactivex.rxjava3.processors) BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) TestException(io.reactivex.rxjava3.exceptions.TestException) Subscriber(org.reactivestreams.Subscriber) IOException(java.io.IOException) Test(org.junit.Test)

Example 33 with Supplier

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

the class FlowableCollectWithCollectorTest method collectorAccumulatorCrashToFlowable.

@Test
public void collectorAccumulatorCrashToFlowable() {
    BehaviorProcessor<Integer> source = BehaviorProcessor.createDefault(1);
    source.collect(new Collector<Integer, Integer, Integer>() {

        @Override
        public Supplier<Integer> supplier() {
            return () -> 1;
        }

        @Override
        public BiConsumer<Integer, Integer> accumulator() {
            return (a, b) -> {
                throw new TestException();
            };
        }

        @Override
        public BinaryOperator<Integer> combiner() {
            return (a, b) -> a + b;
        }

        @Override
        public Function<Integer, Integer> finisher() {
            return a -> a;
        }

        @Override
        public Set<Characteristics> characteristics() {
            return Collections.emptySet();
        }
    }).toFlowable().test().assertFailure(TestException.class);
    assertFalse(source.hasSubscribers());
}
Also used : java.util(java.util) TestHelper(io.reactivex.rxjava3.testsupport.TestHelper) TestException(io.reactivex.rxjava3.exceptions.TestException) Assert.assertFalse(org.junit.Assert.assertFalse) java.util.stream(java.util.stream) IOException(java.io.IOException) Test(org.junit.Test) java.util.function(java.util.function) io.reactivex.rxjava3.core(io.reactivex.rxjava3.core) Subscriber(org.reactivestreams.Subscriber) BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) io.reactivex.rxjava3.processors(io.reactivex.rxjava3.processors) TestException(io.reactivex.rxjava3.exceptions.TestException) Test(org.junit.Test)

Example 34 with Supplier

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

the class FlowableRetryTest method noCancelPreviousRetryWhile2.

@Test
public void noCancelPreviousRetryWhile2() {
    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(new BiPredicate<Integer, Throwable>() {

        @Override
        public boolean test(Integer a, Throwable b) throws Exception {
            return a < 5;
        }
    }).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 35 with Supplier

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

the class FlowableRetryTest method noCancelPreviousRepeatWhen.

@Test
public void noCancelPreviousRepeatWhen() {
    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.get() < 4) {
                return Flowable.error(new TestException());
            }
            return Flowable.just(1);
        }
    }).doOnCancel(new Action() {

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

        @Override
        public Flowable<?> apply(Flowable<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) GroupedFlowable(io.reactivex.rxjava3.flowables.GroupedFlowable) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)37 TestException (io.reactivex.rxjava3.exceptions.TestException)33 Observable (io.reactivex.rxjava3.core.Observable)13 IOException (java.io.IOException)9 InOrder (org.mockito.InOrder)8 java.util (java.util)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 Disposable (io.reactivex.rxjava3.disposables.Disposable)5 Supplier (io.reactivex.rxjava3.functions.Supplier)5 ImmediateThinScheduler (io.reactivex.rxjava3.internal.schedulers.ImmediateThinScheduler)5 TestObserver (io.reactivex.rxjava3.observers.TestObserver)5 Observer (io.reactivex.rxjava3.core.Observer)4 BooleanSubscription (io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription)4 ImmutableList (com.google.common.collect.ImmutableList)3 io.reactivex.rxjava3.processors (io.reactivex.rxjava3.processors)3 TestHelper (io.reactivex.rxjava3.testsupport.TestHelper)3 java.util.function (java.util.function)3 java.util.stream (java.util.stream)3 Assert.assertFalse (org.junit.Assert.assertFalse)3 SQLUtils (com.alibaba.druid.sql.SQLUtils)2