Search in sources :

Example 1 with Supplier

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

the class ObservableCollectWithCollectorTest method collectorAccumulatorDropSignalsToObservable.

@Test
public void collectorAccumulatorDropSignalsToObservable() 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();
            }
        }).toObservable().test().assertFailure(TestException.class);
        TestHelper.assertUndeliverable(errors, 0, IOException.class);
    });
}
Also used : java.util(java.util) Observer(io.reactivex.rxjava3.core.Observer) TestException(io.reactivex.rxjava3.exceptions.TestException) java.util.stream(java.util.stream) RxJavaTest(io.reactivex.rxjava3.core.RxJavaTest) IOException(java.io.IOException) Test(org.junit.Test) io.reactivex.rxjava3.processors(io.reactivex.rxjava3.processors) TestHelper(io.reactivex.rxjava3.testsupport.TestHelper) TestObserver(io.reactivex.rxjava3.observers.TestObserver) Assert.assertFalse(org.junit.Assert.assertFalse) Observable(io.reactivex.rxjava3.core.Observable) PublishSubject(io.reactivex.rxjava3.subjects.PublishSubject) Disposable(io.reactivex.rxjava3.disposables.Disposable) java.util.function(java.util.function) TestException(io.reactivex.rxjava3.exceptions.TestException) Observer(io.reactivex.rxjava3.core.Observer) TestObserver(io.reactivex.rxjava3.observers.TestObserver) IOException(java.io.IOException) Observable(io.reactivex.rxjava3.core.Observable) RxJavaTest(io.reactivex.rxjava3.core.RxJavaTest) Test(org.junit.Test)

Example 2 with Supplier

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

the class FlowableCollectWithCollectorTest method collectorAccumulatorDropSignals.

@Test
public void collectorAccumulatorDropSignals() 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();
            }
        }).test().assertFailure(TestException.class);
        TestHelper.assertUndeliverable(errors, 0, IOException.class);
    });
}
Also used : BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) TestException(io.reactivex.rxjava3.exceptions.TestException) IOException(java.io.IOException) Subscriber(org.reactivestreams.Subscriber) Test(org.junit.Test)

Example 3 with Supplier

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

the class SingleFromSupplierTest method fromSupplierTwice.

@Test
public void fromSupplierTwice() {
    final AtomicInteger atomicInteger = new AtomicInteger();
    Supplier<Integer> supplier = new Supplier<Integer>() {

        @Override
        public Integer get() throws Exception {
            return atomicInteger.incrementAndGet();
        }
    };
    Single.fromSupplier(supplier).test().assertResult(1);
    assertEquals(1, atomicInteger.get());
    Single.fromSupplier(supplier).test().assertResult(2);
    assertEquals(2, atomicInteger.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Supplier(io.reactivex.rxjava3.functions.Supplier) Test(org.junit.Test)

Example 4 with Supplier

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

the class RxJavaPluginsTest method clearIsPassthrough.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void clearIsPassthrough() {
    try {
        RxJavaPlugins.reset();
        assertNull(RxJavaPlugins.onAssembly((Observable) null));
        assertNull(RxJavaPlugins.onAssembly((ConnectableObservable) null));
        assertNull(RxJavaPlugins.onAssembly((Flowable) null));
        assertNull(RxJavaPlugins.onAssembly((ConnectableFlowable) null));
        Observable oos = new Observable() {

            @Override
            public void subscribeActual(Observer t) {
            }
        };
        Flowable fos = new Flowable() {

            @Override
            public void subscribeActual(Subscriber t) {
            }
        };
        assertSame(oos, RxJavaPlugins.onAssembly(oos));
        assertSame(fos, RxJavaPlugins.onAssembly(fos));
        assertNull(RxJavaPlugins.onAssembly((Single) null));
        Single sos = new Single() {

            @Override
            public void subscribeActual(SingleObserver t) {
            }
        };
        assertSame(sos, RxJavaPlugins.onAssembly(sos));
        assertNull(RxJavaPlugins.onAssembly((Completable) null));
        Completable cos = new Completable() {

            @Override
            public void subscribeActual(CompletableObserver t) {
            }
        };
        assertSame(cos, RxJavaPlugins.onAssembly(cos));
        assertNull(RxJavaPlugins.onAssembly((Maybe) null));
        Maybe myb = new Maybe() {

            @Override
            public void subscribeActual(MaybeObserver t) {
            }
        };
        assertSame(myb, RxJavaPlugins.onAssembly(myb));
        Runnable action = Functions.EMPTY_RUNNABLE;
        assertSame(action, RxJavaPlugins.onSchedule(action));
        class AllSubscriber implements Subscriber, Observer, SingleObserver, CompletableObserver, MaybeObserver {

            @Override
            public void onSuccess(Object value) {
            }

            @Override
            public void onSubscribe(Disposable d) {
            }

            @Override
            public void onSubscribe(Subscription s) {
            }

            @Override
            public void onNext(Object t) {
            }

            @Override
            public void onError(Throwable t) {
            }

            @Override
            public void onComplete() {
            }
        }
        AllSubscriber all = new AllSubscriber();
        Subscriber[] allArray = { all };
        assertNull(RxJavaPlugins.onSubscribe(Observable.never(), null));
        assertSame(all, RxJavaPlugins.onSubscribe(Observable.never(), all));
        assertNull(RxJavaPlugins.onSubscribe(Flowable.never(), null));
        assertSame(all, RxJavaPlugins.onSubscribe(Flowable.never(), all));
        assertNull(RxJavaPlugins.onSubscribe(Single.just(1), null));
        assertSame(all, RxJavaPlugins.onSubscribe(Single.just(1), all));
        assertNull(RxJavaPlugins.onSubscribe(Completable.never(), null));
        assertSame(all, RxJavaPlugins.onSubscribe(Completable.never(), all));
        assertNull(RxJavaPlugins.onSubscribe(Maybe.never(), null));
        assertSame(all, RxJavaPlugins.onSubscribe(Maybe.never(), all));
        assertNull(RxJavaPlugins.onSubscribe(Flowable.never().parallel(), null));
        assertSame(allArray, RxJavaPlugins.onSubscribe(Flowable.never().parallel(), allArray));
        final Scheduler s = ImmediateThinScheduler.INSTANCE;
        Supplier<Scheduler> c = new Supplier<Scheduler>() {

            @Override
            public Scheduler get() throws Exception {
                return s;
            }
        };
        assertSame(s, RxJavaPlugins.onComputationScheduler(s));
        assertSame(s, RxJavaPlugins.onIoScheduler(s));
        assertSame(s, RxJavaPlugins.onNewThreadScheduler(s));
        assertSame(s, RxJavaPlugins.onSingleScheduler(s));
        assertSame(s, RxJavaPlugins.initComputationScheduler(c));
        assertSame(s, RxJavaPlugins.initIoScheduler(c));
        assertSame(s, RxJavaPlugins.initNewThreadScheduler(c));
        assertSame(s, RxJavaPlugins.initSingleScheduler(c));
    } finally {
        RxJavaPlugins.reset();
    }
}
Also used : ImmediateThinScheduler(io.reactivex.rxjava3.internal.schedulers.ImmediateThinScheduler) ConnectableFlowable(io.reactivex.rxjava3.flowables.ConnectableFlowable) Observable(io.reactivex.rxjava3.core.Observable) ConnectableObservable(io.reactivex.rxjava3.observables.ConnectableObservable) Observer(io.reactivex.rxjava3.core.Observer) ConnectableObservable(io.reactivex.rxjava3.observables.ConnectableObservable) ScalarSubscription(io.reactivex.rxjava3.internal.subscriptions.ScalarSubscription) ParallelFlowable(io.reactivex.rxjava3.parallel.ParallelFlowable) ConnectableFlowable(io.reactivex.rxjava3.flowables.ConnectableFlowable) Test(org.junit.Test)

Example 5 with Supplier

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

the class ObservableDeferTest method deferFunctionThrows.

@Test
public void deferFunctionThrows() throws Throwable {
    Supplier<Observable<String>> factory = mock(Supplier.class);
    when(factory.get()).thenThrow(new TestException());
    Observable<String> result = Observable.defer(factory);
    Observer<String> o = TestHelper.mockObserver();
    result.subscribe(o);
    verify(o).onError(any(TestException.class));
    verify(o, never()).onNext(any(String.class));
    verify(o, never()).onComplete();
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) 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