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());
}
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();
}
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());
}
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();
}
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!");
}
Aggregations