Search in sources :

Example 51 with Predicate

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

the class ObservableRetryTest method noCancelPreviousRepeatWhen.

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

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

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

        @Override
        public ObservableSource<?> apply(Observable<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) Observable(io.reactivex.rxjava3.core.Observable) GroupedObservable(io.reactivex.rxjava3.observables.GroupedObservable) Test(org.junit.Test)

Example 52 with Predicate

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

the class TestObserverExTest method errorInPredicate.

@Test
public void errorInPredicate() {
    TestObserverEx<Object> to = new TestObserverEx<>();
    to.onError(new RuntimeException());
    try {
        to.assertError(new Predicate<Throwable>() {

            @Override
            public boolean test(Throwable throwable) throws Exception {
                throw new TestException();
            }
        });
    } catch (TestException ex) {
        // expected
        return;
    }
    fail("Error in predicate but not thrown!");
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) TestException(io.reactivex.rxjava3.exceptions.TestException) IOException(java.io.IOException) RxJavaTest(io.reactivex.rxjava3.core.RxJavaTest) Test(org.junit.Test)

Example 53 with Predicate

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

the class ObservableFirstTest method firstWithPredicateAndEmpty.

@Test
public void firstWithPredicateAndEmpty() {
    Maybe<Integer> o = Observable.just(1).filter(new Predicate<Integer>() {

        @Override
        public boolean test(Integer t1) {
            return t1 % 2 == 0;
        }
    }).firstElement();
    o.subscribe(wm);
    InOrder inOrder = inOrder(wm);
    inOrder.verify(wm, times(1)).onComplete();
    inOrder.verify(wm, never()).onError(any(Throwable.class));
    inOrder.verifyNoMoreInteractions();
}
Also used : InOrder(org.mockito.InOrder) Predicate(io.reactivex.rxjava3.functions.Predicate)

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