Search in sources :

Example 1 with BiFunction

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

the class RxJavaPluginsTest method observableStart.

@SuppressWarnings("rawtypes")
@Test
public void observableStart() {
    try {
        RxJavaPlugins.setOnObservableSubscribe(new BiFunction<Observable, Observer, Observer>() {

            @Override
            public Observer apply(Observable o, final Observer t) {
                return new Observer() {

                    @Override
                    public void onSubscribe(Disposable d) {
                        t.onSubscribe(d);
                    }

                    @SuppressWarnings("unchecked")
                    @Override
                    public void onNext(Object value) {
                        t.onNext((Integer) value - 9);
                    }

                    @Override
                    public void onError(Throwable e) {
                        t.onError(e);
                    }

                    @Override
                    public void onComplete() {
                        t.onComplete();
                    }
                };
            }
        });
        Observable.range(10, 3).test().assertValues(1, 2, 3).assertNoErrors().assertComplete();
    } finally {
        RxJavaPlugins.reset();
    }
    // make sure the reset worked
    Observable.range(10, 3).test().assertValues(10, 11, 12).assertNoErrors().assertComplete();
}
Also used : Observer(io.reactivex.rxjava3.core.Observer) Observable(io.reactivex.rxjava3.core.Observable) ConnectableObservable(io.reactivex.rxjava3.observables.ConnectableObservable) Test(org.junit.Test)

Example 2 with BiFunction

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

the class ObservableFlatMapTest method flatMapSelectorMaxConcurrent.

@Test
public void flatMapSelectorMaxConcurrent() {
    final int m = 4;
    final AtomicInteger subscriptionCount = new AtomicInteger();
    Observable<Integer> source = Observable.range(1, 10).flatMap(new Function<Integer, Observable<Integer>>() {

        @Override
        public Observable<Integer> apply(Integer t1) {
            return composer(Observable.range(t1 * 10, 2), subscriptionCount, m).subscribeOn(Schedulers.computation());
        }
    }, new BiFunction<Integer, Integer, Integer>() {

        @Override
        public Integer apply(Integer t1, Integer t2) {
            return t1 * 1000 + t2;
        }
    }, m);
    TestObserver<Integer> to = new TestObserver<>();
    source.subscribe(to);
    to.awaitDone(5, TimeUnit.SECONDS);
    to.assertNoErrors();
    Set<Integer> expected = new HashSet<>(Arrays.asList(1010, 1011, 2020, 2021, 3030, 3031, 4040, 4041, 5050, 5051, 6060, 6061, 7070, 7071, 8080, 8081, 9090, 9091, 10100, 10101));
    Assert.assertEquals(expected.size(), to.values().size());
    System.out.println("--> testFlatMapSelectorMaxConcurrent: " + to.values());
    Assert.assertTrue(expected.containsAll(to.values()));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Observable(io.reactivex.rxjava3.core.Observable) TestObserver(io.reactivex.rxjava3.observers.TestObserver)

Example 3 with BiFunction

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

the class FlowableRetryTest method singleSubscriptionOnFirst.

@Test
public void singleSubscriptionOnFirst() throws Exception {
    final AtomicInteger inc = new AtomicInteger(0);
    Publisher<Integer> onSubscribe = new Publisher<Integer>() {

        @Override
        public void subscribe(Subscriber<? super Integer> subscriber) {
            subscriber.onSubscribe(new BooleanSubscription());
            final int emit = inc.incrementAndGet();
            subscriber.onNext(emit);
            subscriber.onComplete();
        }
    };
    int first = Flowable.unsafeCreate(onSubscribe).retryWhen(new Function<Flowable<? extends Throwable>, Flowable<Object>>() {

        @Override
        public Flowable<Object> apply(Flowable<? extends Throwable> attempt) {
            return attempt.zipWith(Flowable.just(1), new BiFunction<Throwable, Integer, Object>() {

                @Override
                public Object apply(Throwable o, Integer integer) {
                    return 0;
                }
            });
        }
    }).blockingFirst();
    assertEquals("Observer did not receive the expected output", 1, first);
    assertEquals("Subscribe was not called once", 1, inc.get());
}
Also used : BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) GroupedFlowable(io.reactivex.rxjava3.flowables.GroupedFlowable) Test(org.junit.Test)

Example 4 with BiFunction

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

the class ObservableJoinTest method badEndSource.

@Test
public void badEndSource() {
    List<Throwable> errors = TestHelper.trackPluginErrors();
    try {
        @SuppressWarnings("rawtypes") final Observer[] o = { null };
        TestObserverEx<Integer> to = Observable.just(1).join(Observable.just(2), Functions.justFunction(Observable.never()), Functions.justFunction(new Observable<Integer>() {

            @Override
            protected void subscribeActual(Observer<? super Integer> observer) {
                o[0] = observer;
                observer.onSubscribe(Disposable.empty());
                observer.onError(new TestException("First"));
            }
        }), new BiFunction<Integer, Integer, Integer>() {

            @Override
            public Integer apply(Integer a, Integer b) throws Exception {
                return a + b;
            }
        }).to(TestHelper.<Integer>testConsumer());
        o[0].onError(new TestException("Second"));
        to.assertFailureAndMessage(TestException.class, "First");
        TestHelper.assertUndeliverable(errors, 0, TestException.class, "Second");
    } finally {
        RxJavaPlugins.reset();
    }
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) TestObserver(io.reactivex.rxjava3.observers.TestObserver)

Example 5 with BiFunction

use of io.reactivex.rxjava3.functions.BiFunction 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)

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