Search in sources :

Example 86 with Consumer

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

the class FlowableReduceTest method shouldReduceTo10EventsFlowable.

/**
 * Make sure an asynchronous reduce with flatMap works.
 * Original Reactor-Core test case: https://gist.github.com/jurna/353a2bd8ff83f0b24f0b5bc772077d61
 */
@Test
public void shouldReduceTo10EventsFlowable() {
    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;
                }
            }).toFlowable().doOnNext(new Consumer<String>() {

                @Override
                public void accept(String s) throws Exception {
                    count.incrementAndGet();
                    System.out.println("Completed with " + s);
                }
            });
        }
    }).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 87 with Consumer

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

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

the class ObservablePublishTest method altConnectCrash.

@Test
public void altConnectCrash() {
    try {
        new ObservablePublish<>(Observable.<Integer>empty()).connect(new Consumer<Disposable>() {

            @Override
            public void accept(Disposable t) throws Exception {
                throw new TestException();
            }
        });
        fail("Should have thrown");
    } catch (TestException expected) {
    // expected
    }
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) TestException(io.reactivex.rxjava3.exceptions.TestException) Test(org.junit.Test)

Example 89 with Consumer

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

the class ObservableGroupByTest method existingGroupValueSelectorFails.

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

        @Override
        public Object apply(Integer v) throws Throwable {
            if (v == 2) {
                throw new TestException();
            }
            return v;
        }
    }).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, 1);
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) GroupedObservable(io.reactivex.rxjava3.observables.GroupedObservable) Test(org.junit.Test)

Example 90 with Consumer

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

the class MaybeTest method using.

@Test
public void using() {
    final AtomicInteger disposeCount = new AtomicInteger();
    Maybe.using(Functions.justSupplier(1), new Function<Integer, MaybeSource<Integer>>() {

        @Override
        public MaybeSource<Integer> apply(Integer v) throws Exception {
            return Maybe.just(v);
        }
    }, new Consumer<Integer>() {

        @Override
        public void accept(Integer d) throws Exception {
            disposeCount.set(d);
        }
    }).map(new Function<Integer, Object>() {

        @Override
        public String apply(Integer v) throws Exception {
            return "" + disposeCount.get() + v * 10;
        }
    }).test().assertResult("110");
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArgsToString(io.reactivex.rxjava3.internal.operators.flowable.FlowableZipTest.ArgsToString) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)98 TestException (io.reactivex.rxjava3.exceptions.TestException)57 Disposable (io.reactivex.rxjava3.disposables.Disposable)39 BooleanSubscription (io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription)22 IOException (java.io.IOException)20 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)19 InOrder (org.mockito.InOrder)17 TestObserver (io.reactivex.rxjava3.observers.TestObserver)9 TestSubscriber (io.reactivex.rxjava3.subscribers.TestSubscriber)8 RxJavaTest (io.reactivex.rxjava3.core.RxJavaTest)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 Observable (io.reactivex.rxjava3.core.Observable)5 Worker (io.reactivex.rxjava3.core.Scheduler.Worker)5 Consumer (io.reactivex.rxjava3.functions.Consumer)5 CompositeException (io.reactivex.rxjava3.exceptions.CompositeException)4 ForEachWhileSubscriber (io.reactivex.rxjava3.internal.subscribers.ForEachWhileSubscriber)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 Observer (io.reactivex.rxjava3.core.Observer)3 GroupedFlowable (io.reactivex.rxjava3.flowables.GroupedFlowable)3 ArgsToString (io.reactivex.rxjava3.internal.operators.flowable.FlowableZipTest.ArgsToString)3