Search in sources :

Example 6 with BiFunction

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

the class FlowableReduceTest method shouldReduceTo10Events.

/**
 * Make sure an asynchronous reduce with flatMap works.
 * Original Reactor-Core test case: https://gist.github.com/jurna/353a2bd8ff83f0b24f0b5bc772077d61
 */
@Test
public void shouldReduceTo10Events() {
    final AtomicInteger count = new AtomicInteger();
    Flowable.range(0, 10).flatMap(new Function<Integer, Publisher<String>>() {

        @Override
        public Publisher<String> apply(final Integer x) throws Exception {
            return Flowable.range(0, 2).map(new Function<Integer, String>() {

                @Override
                public String apply(Integer y) throws Exception {
                    return blockingOp(x, y);
                }
            }).subscribeOn(Schedulers.io()).reduce(new BiFunction<String, String, String>() {

                @Override
                public String apply(String l, String r) throws Exception {
                    return l + "_" + r;
                }
            }).doOnSuccess(new Consumer<String>() {

                @Override
                public void accept(String s) throws Exception {
                    count.incrementAndGet();
                    System.out.println("Completed with " + s);
                }
            }).toFlowable();
        }
    }).blockingLast();
    assertEquals(10, count.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestException(io.reactivex.rxjava3.exceptions.TestException)

Example 7 with BiFunction

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

the class ParallelReduceFullTest method error.

@Test
public void error() {
    List<Throwable> errors = TestHelper.trackPluginErrors();
    try {
        Flowable.<Integer>error(new TestException()).parallel().reduce(new BiFunction<Integer, Integer, Integer>() {

            @Override
            public Integer apply(Integer a, Integer b) throws Exception {
                return a + b;
            }
        }).test().assertFailure(TestException.class);
        assertTrue(errors.isEmpty());
    } finally {
        RxJavaPlugins.reset();
    }
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) TestException(io.reactivex.rxjava3.exceptions.TestException) IOException(java.io.IOException) Test(org.junit.Test)

Example 8 with BiFunction

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

the class ParallelReduceFullTest method error2.

@Test
public void error2() {
    List<Throwable> errors = TestHelper.trackPluginErrors();
    try {
        ParallelFlowable.fromArray(Flowable.<Integer>error(new IOException()), Flowable.<Integer>error(new TestException())).reduce(new BiFunction<Integer, Integer, Integer>() {

            @Override
            public Integer apply(Integer a, Integer b) throws Exception {
                return a + b;
            }
        }).test().assertFailure(IOException.class);
        TestHelper.assertUndeliverable(errors, 0, TestException.class);
    } finally {
        RxJavaPlugins.reset();
    }
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) IOException(java.io.IOException) TestException(io.reactivex.rxjava3.exceptions.TestException) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

TestException (io.reactivex.rxjava3.exceptions.TestException)5 Test (org.junit.Test)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 Observable (io.reactivex.rxjava3.core.Observable)2 TestObserver (io.reactivex.rxjava3.observers.TestObserver)2 IOException (java.io.IOException)2 Observer (io.reactivex.rxjava3.core.Observer)1 GroupedFlowable (io.reactivex.rxjava3.flowables.GroupedFlowable)1 BooleanSubscription (io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription)1 ConnectableObservable (io.reactivex.rxjava3.observables.ConnectableObservable)1