Search in sources :

Example 26 with Flowable

use of io.reactivex.rxjava3.core.Flowable in project RxJava by ReactiveX.

the class FlowableFromSupplierTest method shouldAllowToThrowCheckedException.

@Test
public void shouldAllowToThrowCheckedException() {
    final Exception checkedException = new Exception("test exception");
    Flowable<Object> fromSupplierFlowable = Flowable.fromSupplier(new Supplier<Object>() {

        @Override
        public Object get() throws Exception {
            throw checkedException;
        }
    });
    Subscriber<Object> subscriber = TestHelper.mockSubscriber();
    fromSupplierFlowable.subscribe(subscriber);
    verify(subscriber).onSubscribe(any(Subscription.class));
    verify(subscriber).onError(checkedException);
    verifyNoMoreInteractions(subscriber);
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) Test(org.junit.Test)

Example 27 with Flowable

use of io.reactivex.rxjava3.core.Flowable in project RxJava by ReactiveX.

the class FlowableGroupByTest method firstGroupsCompleteAndParentSlowToThenEmitFinalGroupsAndThenComplete.

@Test
public void firstGroupsCompleteAndParentSlowToThenEmitFinalGroupsAndThenComplete() throws InterruptedException {
    // there are two groups to first complete
    final CountDownLatch first = new CountDownLatch(2);
    final ArrayList<String> results = new ArrayList<>();
    Flowable.unsafeCreate(new Publisher<Integer>() {

        @Override
        public void subscribe(Subscriber<? super Integer> sub) {
            sub.onSubscribe(new BooleanSubscription());
            sub.onNext(1);
            sub.onNext(2);
            sub.onNext(1);
            sub.onNext(2);
            try {
                first.await();
            } catch (InterruptedException e) {
                sub.onError(e);
                return;
            }
            sub.onNext(3);
            sub.onNext(3);
            sub.onComplete();
        }
    }).groupBy(new Function<Integer, Integer>() {

        @Override
        public Integer apply(Integer t) {
            return t;
        }
    }).flatMap(new Function<GroupedFlowable<Integer, Integer>, Flowable<String>>() {

        @Override
        public Flowable<String> apply(final GroupedFlowable<Integer, Integer> group) {
            if (group.getKey() < 3) {
                return group.map(new Function<Integer, String>() {

                    @Override
                    public String apply(Integer t1) {
                        return "first groups: " + t1;
                    }
                }).take(2).doOnComplete(new Action() {

                    @Override
                    public void run() {
                        first.countDown();
                    }
                });
            } else {
                return group.map(new Function<Integer, String>() {

                    @Override
                    public String apply(Integer t1) {
                        return "last group: " + t1;
                    }
                });
            }
        }
    }).blockingForEach(new Consumer<String>() {

        @Override
        public void accept(String s) {
            results.add(s);
        }
    });
    System.out.println("Results: " + results);
    assertEquals(6, results.size());
}
Also used : BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) GroupedFlowable(io.reactivex.rxjava3.flowables.GroupedFlowable) Test(org.junit.Test)

Example 28 with Flowable

use of io.reactivex.rxjava3.core.Flowable 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 29 with Flowable

use of io.reactivex.rxjava3.core.Flowable 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 30 with Flowable

use of io.reactivex.rxjava3.core.Flowable in project RxJava by ReactiveX.

the class FlowableFromCallableTest method shouldAllowToThrowCheckedException.

@Test
public void shouldAllowToThrowCheckedException() {
    final Exception checkedException = new Exception("test exception");
    Flowable<Object> fromCallableFlowable = Flowable.fromCallable(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            throw checkedException;
        }
    });
    Subscriber<Object> subscriber = TestHelper.mockSubscriber();
    fromCallableFlowable.subscribe(subscriber);
    verify(subscriber).onSubscribe(any(Subscription.class));
    verify(subscriber).onError(checkedException);
    verifyNoMoreInteractions(subscriber);
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)177 BooleanSubscription (io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription)121 TestException (io.reactivex.rxjava3.exceptions.TestException)95 TestSubscriber (io.reactivex.rxjava3.subscribers.TestSubscriber)67 InOrder (org.mockito.InOrder)41 IOException (java.io.IOException)27 TestScheduler (io.reactivex.rxjava3.schedulers.TestScheduler)22 Disposable (io.reactivex.rxjava3.disposables.Disposable)21 Flowable (io.reactivex.rxjava3.core.Flowable)19 TimeUnit (java.util.concurrent.TimeUnit)19 FlowableRxInvokerProvider (org.apache.cxf.jaxrs.rx3.client.FlowableRxInvokerProvider)18 FlowableRxInvoker (org.apache.cxf.jaxrs.rx3.client.FlowableRxInvoker)17 JacksonJsonProvider (com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider)15 InternalServerErrorException (javax.ws.rs.InternalServerErrorException)13 ClientBuilder (javax.ws.rs.client.ClientBuilder)13 MediaType (javax.ws.rs.core.MediaType)13 AbstractResourceInfo (org.apache.cxf.jaxrs.model.AbstractResourceInfo)13 AbstractBusClientServerTestBase (org.apache.cxf.testutil.common.AbstractBusClientServerTestBase)13 Assert.assertTrue (org.junit.Assert.assertTrue)13 BeforeClass (org.junit.BeforeClass)13