Search in sources :

Example 11 with Function

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

the class HandlerSchedulerTest method workerScheduleOnceUsesHook.

@Test
public void workerScheduleOnceUsesHook() {
    final CountingRunnable newCounter = new CountingRunnable();
    final AtomicReference<Runnable> runnableRef = new AtomicReference<>();
    RxJavaPlugins.setScheduleHandler(new Function<Runnable, Runnable>() {

        @Override
        public Runnable apply(Runnable runnable) {
            runnableRef.set(runnable);
            return newCounter;
        }
    });
    Worker worker = scheduler.createWorker();
    CountingRunnable counter = new CountingRunnable();
    worker.schedule(counter);
    // Verify our runnable was passed to the schedulers hook.
    assertSame(counter, runnableRef.get());
    runUiThreadTasks();
    // Verify the scheduled runnable was the one returned from the hook.
    assertEquals(1, newCounter.get());
    assertEquals(0, counter.get());
}
Also used : CountingRunnable(io.reactivex.rxjava3.android.testutil.CountingRunnable) CountingRunnable(io.reactivex.rxjava3.android.testutil.CountingRunnable) Worker(io.reactivex.rxjava3.core.Scheduler.Worker) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 12 with Function

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

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

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

the class SingleConcatMapCompletableTest method mapperThrows.

@Test
public void mapperThrows() {
    final boolean[] b = { false };
    Single.just(1).concatMapCompletable(new Function<Integer, Completable>() {

        @Override
        public Completable apply(Integer t) throws Exception {
            throw new TestException();
        }
    }).test().assertFailure(TestException.class);
    assertFalse(b[0]);
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) TestException(io.reactivex.rxjava3.exceptions.TestException) Test(org.junit.Test)

Example 15 with Function

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

the class SingleFlatMapTest method mapperThrows.

@Test
public void mapperThrows() {
    final boolean[] b = { false };
    Single.just(1).flatMapCompletable(new Function<Integer, Completable>() {

        @Override
        public Completable apply(Integer t) throws Exception {
            throw new TestException();
        }
    }).test().assertFailure(TestException.class);
    assertFalse(b[0]);
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) TestException(io.reactivex.rxjava3.exceptions.TestException) 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