Search in sources :

Example 6 with Predicate

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

the class ObservableFirstTest method firstOrDefaultWithPredicateAndOneElement.

@Test
public void firstOrDefaultWithPredicateAndOneElement() {
    Single<Integer> o = Observable.just(1, 2).filter(new Predicate<Integer>() {

        @Override
        public boolean test(Integer t1) {
            return t1 % 2 == 0;
        }
    }).first(4);
    o.subscribe(wo);
    InOrder inOrder = inOrder(wo);
    inOrder.verify(wo, times(1)).onSuccess(2);
    inOrder.verifyNoMoreInteractions();
}
Also used : InOrder(org.mockito.InOrder) Predicate(io.reactivex.rxjava3.functions.Predicate)

Example 7 with Predicate

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

the class ObservableFirstTest method firstWithPredicate.

@Test
public void firstWithPredicate() {
    Maybe<Integer> o = Observable.just(1, 2, 3, 4, 5, 6).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)).onSuccess(2);
    inOrder.verifyNoMoreInteractions();
}
Also used : InOrder(org.mockito.InOrder) Predicate(io.reactivex.rxjava3.functions.Predicate)

Example 8 with Predicate

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

the class FlowableFilterTest method withBackpressure2.

/**
 * Make sure we are adjusting subscriber.request() for filtered items.
 * @throws InterruptedException if the test is interrupted
 */
@Test
public void withBackpressure2() throws InterruptedException {
    Flowable<Integer> w = Flowable.range(1, Flowable.bufferSize() * 2);
    Flowable<Integer> f = w.filter(new Predicate<Integer>() {

        @Override
        public boolean test(Integer t1) {
            return t1 > 100;
        }
    });
    final CountDownLatch latch = new CountDownLatch(1);
    final TestSubscriber<Integer> ts = new TestSubscriber<Integer>() {

        @Override
        public void onComplete() {
            System.out.println("onComplete");
            latch.countDown();
        }

        @Override
        public void onError(Throwable e) {
            e.printStackTrace();
            latch.countDown();
        }

        @Override
        public void onNext(Integer t) {
            System.out.println("Received: " + t);
            // request more each time we receive
            request(1);
        }
    };
    // this means it will only request 1 item and expect to receive more
    ts.request(1);
    f.subscribe(ts);
    // this will wait forever unless OperatorTake handles the request(n) on filtered items
    latch.await();
}
Also used : TestSubscriber(io.reactivex.rxjava3.subscribers.TestSubscriber) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 9 with Predicate

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

the class FlowableFilterTest method withBackpressure.

/**
 * Make sure we are adjusting subscriber.request() for filtered items.
 * @throws InterruptedException if the test is interrupted
 * @throws InterruptedException if the test is interrupted
 */
@Test
public void withBackpressure() throws InterruptedException {
    Flowable<String> w = Flowable.just("one", "two", "three");
    Flowable<String> f = w.filter(new Predicate<String>() {

        @Override
        public boolean test(String t1) {
            return t1.equals("three");
        }
    });
    final CountDownLatch latch = new CountDownLatch(1);
    TestSubscriber<String> ts = new TestSubscriber<String>() {

        @Override
        public void onComplete() {
            System.out.println("onComplete");
            latch.countDown();
        }

        @Override
        public void onError(Throwable e) {
            e.printStackTrace();
            latch.countDown();
        }

        @Override
        public void onNext(String t) {
            System.out.println("Received: " + t);
            // request more each time we receive
            request(1);
        }
    };
    // this means it will only request "one" and "two", expecting to receive them before requesting more
    ts.request(2);
    f.subscribe(ts);
    // this will wait forever unless OperatorTake handles the request(n) on filtered items
    latch.await();
}
Also used : TestSubscriber(io.reactivex.rxjava3.subscribers.TestSubscriber) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 10 with Predicate

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

the class FlowableMapTest method mapFilterMapperCrashFused.

@Test
public void mapFilterMapperCrashFused() {
    TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.ANY);
    Flowable.range(1, 2).hide().map(new Function<Integer, Integer>() {

        @Override
        public Integer apply(Integer v) throws Exception {
            throw new TestException();
        }
    }).filter(new Predicate<Integer>() {

        @Override
        public boolean test(Integer v) throws Exception {
            return true;
        }
    }).subscribe(ts);
    ts.assertFuseable().assertFusionMode(QueueFuseable.NONE).assertFailure(TestException.class);
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) TestException(io.reactivex.rxjava3.exceptions.TestException) IOException(java.io.IOException)

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