Search in sources :

Example 16 with Predicate

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

the class CompletableRetryTest method retryTimesPredicateWithMatchingPredicate.

@Test
public void retryTimesPredicateWithMatchingPredicate() {
    final AtomicInteger atomicInteger = new AtomicInteger(3);
    final AtomicInteger numberOfSubscribeCalls = new AtomicInteger(0);
    Completable.fromAction(new Action() {

        @Override
        public void run() throws Exception {
            numberOfSubscribeCalls.incrementAndGet();
            if (atomicInteger.decrementAndGet() != 0) {
                throw new RuntimeException();
            }
            throw new IllegalArgumentException();
        }
    }).retry(Integer.MAX_VALUE, new Predicate<Throwable>() {

        @Override
        public boolean test(final Throwable throwable) throws Exception {
            return !(throwable instanceof IllegalArgumentException);
        }
    }).test().assertFailure(IllegalArgumentException.class);
    assertEquals(3, numberOfSubscribeCalls.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestException(io.reactivex.rxjava3.exceptions.TestException) Test(org.junit.Test)

Example 17 with Predicate

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

the class FlowableTakeUntilPredicateTest method sourceThrows.

@Test
public void sourceThrows() {
    Subscriber<Object> subscriber = TestHelper.mockSubscriber();
    Flowable.just(1).concatWith(Flowable.<Integer>error(new TestException())).concatWith(Flowable.just(2)).takeUntil(new Predicate<Integer>() {

        @Override
        public boolean test(Integer v) {
            return false;
        }
    }).subscribe(subscriber);
    verify(subscriber).onNext(1);
    verify(subscriber, never()).onNext(2);
    verify(subscriber).onError(any(TestException.class));
    verify(subscriber, never()).onComplete();
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) Test(org.junit.Test)

Example 18 with Predicate

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

the class FlowableRepeatTest method noCancelPreviousRepeatWhen.

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

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

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

Example 19 with Predicate

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

the class TestObserverTest method assertError.

@Test
public void assertError() {
    TestObserver<Integer> to = TestObserver.create();
    try {
        to.assertError(TestException.class);
        throw new RuntimeException("Should have thrown");
    } catch (AssertionError ex) {
    // expected
    }
    try {
        to.assertError(new TestException());
        throw new RuntimeException("Should have thrown");
    } catch (AssertionError ex) {
    // expected
    }
    try {
        to.assertError(Functions.<Throwable>alwaysTrue());
        throw new RuntimeException("Should have thrown");
    } catch (AssertionError ex) {
    // expected
    }
    try {
        to.assertSubscribed();
        throw new RuntimeException("Should have thrown");
    } catch (AssertionError exc) {
    // expected
    }
    to.onSubscribe(Disposable.empty());
    to.assertSubscribed();
    to.assertNoErrors();
    TestException ex = new TestException("Forced failure");
    to.onError(ex);
    to.assertError(ex);
    to.assertError(TestException.class);
    to.assertError(Functions.<Throwable>alwaysTrue());
    to.assertError(new Predicate<Throwable>() {

        @Override
        public boolean test(Throwable t) throws Exception {
            return t.getMessage() != null && t.getMessage().contains("Forced");
        }
    });
    try {
        to.assertError(new RuntimeException());
        throw new RuntimeException("Should have thrown");
    } catch (AssertionError exc) {
    // expected
    }
    try {
        to.assertError(IOException.class);
        throw new RuntimeException("Should have thrown");
    } catch (AssertionError exc) {
    // expected
    }
    try {
        to.assertError(Functions.<Throwable>alwaysFalse());
        throw new RuntimeException("Should have thrown");
    } catch (AssertionError exc) {
    // expected
    }
    try {
        to.assertNoErrors();
        throw new RuntimeException("Should have thrown");
    } catch (AssertionError exc) {
    // expected
    }
    to.assertValueCount(0);
    to.assertNoValues();
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) TestException(io.reactivex.rxjava3.exceptions.TestException) IOException(java.io.IOException) Test(org.junit.Test)

Example 20 with Predicate

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

the class TestObserverTest method errorInPredicate.

@Test
public void errorInPredicate() {
    TestObserver<Object> to = new TestObserver<>();
    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) 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