Search in sources :

Example 66 with Function

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

the class MaybeTest method usingNonEager.

@Test
public void usingNonEager() {
    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);
        }
    }, false).map(new Function<Integer, Object>() {

        @Override
        public String apply(Integer v) throws Exception {
            return "" + disposeCount.get() + v * 10;
        }
    }).test().assertResult("010");
    assertEquals(1, disposeCount.get());
}
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)

Example 67 with Function

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

the class CompletableMergeTest method mainErrorInnerErrorRace.

@Test
public void mainErrorInnerErrorRace() {
    for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
        List<Throwable> errors = TestHelper.trackPluginErrors();
        try {
            final PublishProcessor<Integer> pp1 = PublishProcessor.create();
            final PublishProcessor<Integer> pp2 = PublishProcessor.create();
            TestObserverEx<Void> to = Completable.merge(pp1.map(new Function<Integer, Completable>() {

                @Override
                public Completable apply(Integer v) throws Exception {
                    return pp2.ignoreElements();
                }
            })).to(TestHelper.<Void>testConsumer());
            pp1.onNext(1);
            final Throwable ex1 = new TestException();
            final Throwable ex2 = new TestException();
            Runnable r1 = new Runnable() {

                @Override
                public void run() {
                    pp1.onError(ex1);
                }
            };
            Runnable r2 = new Runnable() {

                @Override
                public void run() {
                    pp2.onError(ex2);
                }
            };
            TestHelper.race(r1, r2);
            Throwable ex = to.errors().get(0);
            if (ex instanceof CompositeException) {
                to.assertSubscribed().assertNoValues().assertNotComplete();
                errors = TestHelper.compositeList(ex);
                TestHelper.assertError(errors, 0, TestException.class);
                TestHelper.assertError(errors, 1, TestException.class);
            } else {
                to.assertFailure(TestException.class);
                if (!errors.isEmpty()) {
                    TestHelper.assertUndeliverable(errors, 0, TestException.class);
                }
            }
        } finally {
            RxJavaPlugins.reset();
        }
    }
}
Also used : AtomicThrowable(io.reactivex.rxjava3.internal.util.AtomicThrowable) Test(org.junit.Test)

Example 68 with Function

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

the class FlowableDelayTest method delayWithFlowableSourceThrows.

@Test
public void delayWithFlowableSourceThrows() {
    PublishProcessor<Integer> source = PublishProcessor.create();
    final PublishProcessor<Integer> delay = PublishProcessor.create();
    Function<Integer, Flowable<Integer>> delayFunc = new Function<Integer, Flowable<Integer>>() {

        @Override
        public Flowable<Integer> apply(Integer t1) {
            return delay;
        }
    };
    Subscriber<Object> subscriber = TestHelper.mockSubscriber();
    InOrder inOrder = inOrder(subscriber);
    source.delay(delayFunc).subscribe(subscriber);
    source.onNext(1);
    source.onError(new TestException());
    delay.onNext(1);
    inOrder.verify(subscriber).onError(any(TestException.class));
    inOrder.verifyNoMoreInteractions();
    verify(subscriber, never()).onNext(any());
    verify(subscriber, never()).onComplete();
}
Also used : InOrder(org.mockito.InOrder) TestException(io.reactivex.rxjava3.exceptions.TestException)

Example 69 with Function

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

the class FlowableDelayTest method delayWithFlowableReorder.

@Test
public void delayWithFlowableReorder() {
    int n = 3;
    PublishProcessor<Integer> source = PublishProcessor.create();
    final List<PublishProcessor<Integer>> subjects = new ArrayList<>();
    for (int i = 0; i < n; i++) {
        subjects.add(PublishProcessor.<Integer>create());
    }
    Flowable<Integer> result = source.delay(new Function<Integer, Flowable<Integer>>() {

        @Override
        public Flowable<Integer> apply(Integer t1) {
            return subjects.get(t1);
        }
    });
    Subscriber<Object> subscriber = TestHelper.mockSubscriber();
    InOrder inOrder = inOrder(subscriber);
    result.subscribe(subscriber);
    for (int i = 0; i < n; i++) {
        source.onNext(i);
    }
    source.onComplete();
    inOrder.verify(subscriber, never()).onNext(anyInt());
    inOrder.verify(subscriber, never()).onComplete();
    for (int i = n - 1; i >= 0; i--) {
        subjects.get(i).onComplete();
        inOrder.verify(subscriber).onNext(i);
    }
    inOrder.verify(subscriber).onComplete();
    verify(subscriber, never()).onError(any(Throwable.class));
}
Also used : InOrder(org.mockito.InOrder) PublishProcessor(io.reactivex.rxjava3.processors.PublishProcessor)

Example 70 with Function

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

the class FlowableDelayTest method delayWithFlowableSubscriptionThrows.

@Test
public void delayWithFlowableSubscriptionThrows() {
    PublishProcessor<Integer> source = PublishProcessor.create();
    final PublishProcessor<Integer> delay = PublishProcessor.create();
    Supplier<Flowable<Integer>> subFunc = new Supplier<Flowable<Integer>>() {

        @Override
        public Flowable<Integer> get() {
            return delay;
        }
    };
    Function<Integer, Flowable<Integer>> delayFunc = new Function<Integer, Flowable<Integer>>() {

        @Override
        public Flowable<Integer> apply(Integer t1) {
            return delay;
        }
    };
    Subscriber<Object> subscriber = TestHelper.mockSubscriber();
    InOrder inOrder = inOrder(subscriber);
    source.delay(Flowable.defer(subFunc), delayFunc).subscribe(subscriber);
    source.onNext(1);
    delay.onError(new TestException());
    source.onNext(2);
    inOrder.verify(subscriber).onError(any(TestException.class));
    inOrder.verifyNoMoreInteractions();
    verify(subscriber, never()).onNext(any());
    verify(subscriber, never()).onComplete();
}
Also used : InOrder(org.mockito.InOrder) 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