Search in sources :

Example 36 with Predicate

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

the class FlowableSubscriberTest method onErrorThrows.

@Test
public void onErrorThrows() {
    ForEachWhileSubscriber<Integer> s = new ForEachWhileSubscriber<>(new Predicate<Integer>() {

        @Override
        public boolean test(Integer v) throws Exception {
            return true;
        }
    }, new Consumer<Throwable>() {

        @Override
        public void accept(Throwable e) throws Exception {
            throw new TestException("Inner");
        }
    }, new Action() {

        @Override
        public void run() throws Exception {
        }
    });
    List<Throwable> list = TestHelper.trackPluginErrors();
    try {
        s.onSubscribe(new BooleanSubscription());
        s.onError(new TestException("Outer"));
        TestHelper.assertError(list, 0, CompositeException.class);
        List<Throwable> cel = TestHelper.compositeList(list.get(0));
        TestHelper.assertError(cel, 0, TestException.class, "Outer");
        TestHelper.assertError(cel, 1, TestException.class, "Inner");
    } finally {
        RxJavaPlugins.reset();
    }
}
Also used : ForEachWhileSubscriber(io.reactivex.rxjava3.internal.subscribers.ForEachWhileSubscriber) BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) Test(org.junit.Test)

Example 37 with Predicate

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

the class FlowableSubscriberTest method onCompleteThrows.

@Test
public void onCompleteThrows() {
    ForEachWhileSubscriber<Integer> s = new ForEachWhileSubscriber<>(new Predicate<Integer>() {

        @Override
        public boolean test(Integer v) throws Exception {
            return true;
        }
    }, new Consumer<Throwable>() {

        @Override
        public void accept(Throwable e) throws Exception {
        }
    }, new Action() {

        @Override
        public void run() throws Exception {
            throw new TestException("Inner");
        }
    });
    List<Throwable> list = TestHelper.trackPluginErrors();
    try {
        s.onSubscribe(new BooleanSubscription());
        s.onComplete();
        TestHelper.assertUndeliverable(list, 0, TestException.class, "Inner");
    } finally {
        RxJavaPlugins.reset();
    }
}
Also used : ForEachWhileSubscriber(io.reactivex.rxjava3.internal.subscribers.ForEachWhileSubscriber) BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) Test(org.junit.Test)

Example 38 with Predicate

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

the class FlowableRetryTest method noCancelPreviousRepeatWhen.

@Test
public void noCancelPreviousRepeatWhen() {
    final AtomicInteger counter = new AtomicInteger();
    final AtomicInteger times = new AtomicInteger();
    Flowable<Integer> source = Flowable.defer(new Supplier<Flowable<Integer>>() {

        @Override
        public Flowable<Integer> get() throws Exception {
            if (times.get() < 4) {
                return Flowable.error(new TestException());
            }
            return Flowable.just(1);
        }
    }).doOnCancel(new Action() {

        @Override
        public void run() throws Exception {
            counter.getAndIncrement();
        }
    });
    source.retryWhen(new Function<Flowable<Throwable>, Flowable<?>>() {

        @Override
        public Flowable<?> apply(Flowable<Throwable> e) throws Exception {
            return e.takeWhile(new Predicate<Object>() {

                @Override
                public boolean test(Object v) throws Exception {
                    return times.getAndIncrement() < 4;
                }
            });
        }
    }).test().assertResult(1);
    assertEquals(0, counter.get());
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) TestException(io.reactivex.rxjava3.exceptions.TestException) GroupedFlowable(io.reactivex.rxjava3.flowables.GroupedFlowable) Test(org.junit.Test)

Example 39 with Predicate

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

the class FlowableFilterTest method sourceIgnoresCancelConditional.

@Test
public void sourceIgnoresCancelConditional() {
    List<Throwable> errors = TestHelper.trackPluginErrors();
    try {
        Flowable.fromPublisher(new Publisher<Integer>() {

            @Override
            public void subscribe(Subscriber<? super Integer> s) {
                ConditionalSubscriber<? super Integer> cs = (ConditionalSubscriber<? super Integer>) s;
                cs.onSubscribe(new BooleanSubscription());
                cs.tryOnNext(1);
                cs.tryOnNext(2);
                cs.onError(new IOException());
                cs.onComplete();
            }
        }).filter(new Predicate<Integer>() {

            @Override
            public boolean test(Integer v) throws Exception {
                return true;
            }
        }).filter(new Predicate<Integer>() {

            @Override
            public boolean test(Integer v) throws Exception {
                throw new TestException();
            }
        }).test().assertFailure(TestException.class);
        TestHelper.assertUndeliverable(errors, 0, IOException.class);
    } finally {
        RxJavaPlugins.reset();
    }
}
Also used : BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) TestException(io.reactivex.rxjava3.exceptions.TestException) ConditionalSubscriber(io.reactivex.rxjava3.operators.ConditionalSubscriber) IOException(java.io.IOException) TestException(io.reactivex.rxjava3.exceptions.TestException) IOException(java.io.IOException)

Example 40 with Predicate

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

the class ObservableRepeatTest method noCancelPreviousRepeatWhen.

@Test
public void noCancelPreviousRepeatWhen() {
    final AtomicInteger counter = new AtomicInteger();
    Observable<Integer> source = Observable.just(1).doOnDispose(new Action() {

        @Override
        public void run() throws Exception {
            counter.getAndIncrement();
        }
    });
    final AtomicInteger times = new AtomicInteger();
    source.repeatWhen(new Function<Observable<Object>, ObservableSource<?>>() {

        @Override
        public ObservableSource<?> apply(Observable<Object> e) throws Exception {
            return e.takeWhile(new Predicate<Object>() {

                @Override
                public boolean test(Object v) throws Exception {
                    return times.getAndIncrement() < 4;
                }
            });
        }
    }).test().assertResult(1, 1, 1, 1, 1);
    assertEquals(0, counter.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestException(io.reactivex.rxjava3.exceptions.TestException) Observable(io.reactivex.rxjava3.core.Observable) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)31 TestException (io.reactivex.rxjava3.exceptions.TestException)25 Predicate (io.reactivex.rxjava3.functions.Predicate)12 InOrder (org.mockito.InOrder)12 BooleanSubscription (io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription)11 IOException (java.io.IOException)9 TestSubscriber (io.reactivex.rxjava3.subscribers.TestSubscriber)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 ForEachWhileSubscriber (io.reactivex.rxjava3.internal.subscribers.ForEachWhileSubscriber)5 CompletableSubject (io.reactivex.rxjava3.subjects.CompletableSubject)3 Observable (io.reactivex.rxjava3.core.Observable)2 RxJavaTest (io.reactivex.rxjava3.core.RxJavaTest)2 SequentialDisposable (io.reactivex.rxjava3.internal.disposables.SequentialDisposable)2 SubscriptionArbiter (io.reactivex.rxjava3.internal.subscriptions.SubscriptionArbiter)2 ConditionalSubscriber (io.reactivex.rxjava3.operators.ConditionalSubscriber)2 Callable (java.util.concurrent.Callable)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Flowable (io.reactivex.rxjava3.core.Flowable)1 Disposable (io.reactivex.rxjava3.disposables.Disposable)1 GroupedFlowable (io.reactivex.rxjava3.flowables.GroupedFlowable)1