Search in sources :

Example 41 with Function

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

the class ObservableFlattenIterableTest method failingInnerCancelsSource.

@Test
public void failingInnerCancelsSource() {
    final AtomicInteger counter = new AtomicInteger();
    Observable.range(1, 5).doOnNext(new Consumer<Integer>() {

        @Override
        public void accept(Integer v) throws Exception {
            counter.getAndIncrement();
        }
    }).flatMapIterable(new Function<Integer, Iterable<Integer>>() {

        @Override
        public Iterable<Integer> apply(Integer v) throws Exception {
            return new Iterable<Integer>() {

                @Override
                public Iterator<Integer> iterator() {
                    return new Iterator<Integer>() {

                        @Override
                        public boolean hasNext() {
                            return true;
                        }

                        @Override
                        public Integer next() {
                            throw new TestException();
                        }

                        @Override
                        public void remove() {
                            throw new UnsupportedOperationException();
                        }
                    };
                }
            };
        }
    }).test().assertFailure(TestException.class);
    assertEquals(1, counter.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestException(io.reactivex.rxjava3.exceptions.TestException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestException(io.reactivex.rxjava3.exceptions.TestException) RxJavaTest(io.reactivex.rxjava3.core.RxJavaTest) Test(org.junit.Test)

Example 42 with Function

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

the class ObservableGroupByTest method newGroupValueSelectorFails.

@Test
public void newGroupValueSelectorFails() {
    TestObserver<Object> to1 = new TestObserver<>();
    final TestObserver<Object> to2 = new TestObserver<>();
    Observable.just(1).groupBy(Functions.<Integer>identity(), new Function<Integer, Object>() {

        @Override
        public Object apply(Integer v) throws Throwable {
            throw new TestException();
        }
    }).doOnNext(new Consumer<GroupedObservable<Integer, Object>>() {

        @Override
        public void accept(GroupedObservable<Integer, Object> g) throws Throwable {
            g.subscribe(to2);
        }
    }).subscribe(to1);
    to1.assertValueCount(1).assertError(TestException.class).assertNotComplete();
    to2.assertFailure(TestException.class);
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) GroupedObservable(io.reactivex.rxjava3.observables.GroupedObservable) Test(org.junit.Test)

Example 43 with Function

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

the class XFlatMapTest method flowableCompletable.

@Test
public void flowableCompletable() throws Exception {
    List<Throwable> errors = TestHelper.trackPluginErrors();
    try {
        TestObserver<Void> to = Flowable.just(1).subscribeOn(Schedulers.io()).flatMapCompletable(new Function<Integer, Completable>() {

            @Override
            public Completable apply(Integer v) throws Exception {
                sleep();
                return Completable.error(new TestException());
            }
        }).test();
        cb.await();
        beforeCancelSleep(to);
        to.dispose();
        Thread.sleep(SLEEP_AFTER_CANCEL);
        to.assertEmpty();
        assertTrue(errors.toString(), errors.isEmpty());
    } finally {
        RxJavaPlugins.reset();
    }
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException)

Example 44 with Function

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

the class XFlatMapTest method maybeObservable.

@Test
public void maybeObservable() throws Exception {
    List<Throwable> errors = TestHelper.trackPluginErrors();
    try {
        TestObserver<Integer> to = Maybe.just(1).subscribeOn(Schedulers.io()).flatMapObservable(new Function<Integer, Observable<Integer>>() {

            @Override
            public Observable<Integer> apply(Integer v) throws Exception {
                sleep();
                return Observable.<Integer>error(new TestException());
            }
        }).test();
        cb.await();
        beforeCancelSleep(to);
        to.dispose();
        Thread.sleep(SLEEP_AFTER_CANCEL);
        to.assertEmpty();
        assertTrue(errors.toString(), errors.isEmpty());
    } finally {
        RxJavaPlugins.reset();
    }
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException)

Example 45 with Function

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

the class XFlatMapTest method flowableFlowable.

@Test
public void flowableFlowable() throws Exception {
    List<Throwable> errors = TestHelper.trackPluginErrors();
    try {
        TestSubscriber<Integer> ts = Flowable.just(1).subscribeOn(Schedulers.io()).flatMap(new Function<Integer, Publisher<Integer>>() {

            @Override
            public Publisher<Integer> apply(Integer v) throws Exception {
                sleep();
                return Flowable.<Integer>error(new TestException());
            }
        }).test();
        cb.await();
        beforeCancelSleep(ts);
        ts.cancel();
        Thread.sleep(SLEEP_AFTER_CANCEL);
        ts.assertEmpty();
        assertTrue(errors.toString(), errors.isEmpty());
    } finally {
        RxJavaPlugins.reset();
    }
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException)

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