Java Code Examples for io.reactivex.Flowable#compose()

The following examples show how to use io.reactivex.Flowable#compose() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ClientThreadIntegrationTest.java    From reactive-grpc with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void manyToMany() {
    RxGreeterGrpc.RxGreeterStub stub = RxGreeterGrpc.newRxStub(channel);
    Flowable<HelloRequest> req = Flowable.just(
            HelloRequest.newBuilder().setName("a").build(),
            HelloRequest.newBuilder().setName("b").build(),
            HelloRequest.newBuilder().setName("c").build(),
            HelloRequest.newBuilder().setName("d").build(),
            HelloRequest.newBuilder().setName("e").build());

    AtomicReference<String> clientThreadName = new AtomicReference<>();

    Flowable<HelloResponse> resp = req.compose(stub::sayHelloBothStream);

    TestSubscriber<String> testSubscriber = resp
            .map(HelloResponse::getMessage)
            .doOnNext(x -> clientThreadName.set(Thread.currentThread().getName()))
            .test();
    testSubscriber.awaitTerminalEvent(3, TimeUnit.SECONDS);
    testSubscriber.assertComplete();

    assertThat(clientThreadName.get()).isEqualTo("TheGrpcClient");
    assertThat(serverThreadName.get()).isEqualTo("TheGrpcServer");
}
 
Example 2
Source File: EndToEndIntegrationTest.java    From reactive-grpc with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void manyToMany() {
    RxGreeterGrpc.RxGreeterStub stub = RxGreeterGrpc.newRxStub(channel);
    Flowable<HelloRequest> req = Flowable.just(
            HelloRequest.newBuilder().setName("a").build(),
            HelloRequest.newBuilder().setName("b").build(),
            HelloRequest.newBuilder().setName("c").build(),
            HelloRequest.newBuilder().setName("d").build(),
            HelloRequest.newBuilder().setName("e").build());

    Flowable<HelloResponse> resp = req.compose(stub::sayHelloBothStream);

    TestSubscriber<String> testSubscriber = resp.map(HelloResponse::getMessage).test();
    testSubscriber.awaitTerminalEvent(3, TimeUnit.SECONDS);
    testSubscriber.assertValues("Hello a and b", "Hello c and d", "Hello e");
    testSubscriber.assertComplete();
}
 
Example 3
Source File: RxBusBuilder.java    From RxBus2 with Apache License 2.0 6 votes vote down vote up
public Flowable<T> build(boolean applySchedular)
{
    Flowable<T> flowable = null;
    if (mKeys != null)
    {
        for (int i = 0; i < mKeys.size(); i++)
        {
            if (i == 0)
                flowable = RxBus.getInstance().observeEvent(mKeys.get(i));
            else
                flowable = flowable.mergeWith(RxBus.getInstance().observeEvent(mKeys.get(i)));
        }
    }
    else
        flowable = RxBus.getInstance().observeEvent(mEventClass);

    if (mBackpressureBeforeValve)
        flowable = flowable.onBackpressureBuffer();

    if (mQueuer != null)
        flowable = flowable.compose(FlowableTransformers.valve(mQueuer.getResumeObservable(), mQueuer.isBusResumed(), mValvePrefetch));
    if (applySchedular)
        flowable = applySchedular(flowable);
    return flowable;
}
 
Example 4
Source File: RxBusBuilder.java    From RxBus2 with Apache License 2.0 6 votes vote down vote up
public <R> Disposable subscribe(Consumer<R> onNext, Consumer<Throwable> onError, Action onCompleted, FlowableTransformer<T, R> transformer)
{
    Flowable flowable = build(false);
    if (transformer != null)
        flowable = flowable.compose(transformer);

    if (onNext == null)
        onNext = data -> {};
    if (onError == null)
        onError = error -> { throw new OnErrorNotImplementedException(error); };
    if (onCompleted == null)
        onCompleted = () -> {};

    Consumer<R> actualOnNext = onNext;
    if (mQueuer != null && mQueueSubscriptionSafetyCheckEnabled)
        actualOnNext = RxBusUtil.wrapQueueConsumer(onNext, mQueuer);

    flowable = applySchedular(flowable);
    Disposable disposable = flowable.subscribe(actualOnNext, onError, onCompleted);
    if (mBoundObject != null)
        RxDisposableManager.addDisposable(mBoundObject, disposable);
    return disposable;
}
 
Example 5
Source File: RxBusBuilder.java    From RxBus2 with Apache License 2.0 6 votes vote down vote up
public <R> Disposable subscribe(DisposableSubscriber<R> subscriber, FlowableTransformer<T, R> transformer)
{
    Flowable flowable = build(false);
    if (transformer != null)
        flowable = flowable.compose(transformer);

    Subscriber<R> actualSubscriber = subscriber;
    if (mQueuer != null && mQueueSubscriptionSafetyCheckEnabled)
        actualSubscriber = RxBusUtil.wrapSubscriber(subscriber, mQueuer);

    flowable = applySchedular(flowable);
    Disposable disposable = (DisposableSubscriber)flowable.subscribeWith(actualSubscriber);
    if (mBoundObject != null)
        RxDisposableManager.addDisposable(mBoundObject, disposable);
    return disposable;
}
 
Example 6
Source File: CollectStageFactory.java    From smallrye-reactive-streams-operators with Apache License 2.0 5 votes vote down vote up
@Override
public CompletionStage<O> apply(Flowable<I> source) {
    CompletableFuture<O> future = new CompletableFuture<>();
    Flowable<O> flow = source.compose(f -> RxJavaPlugins.onAssembly(new FlowableCollector<>(f, collector)));
    //noinspection ResultOfMethodCallIgnored
    flow
            .firstElement()
            .subscribe(
                    future::complete,
                    future::completeExceptionally);
    return future;
}
 
Example 7
Source File: VertxExecutionModel.java    From smallrye-reactive-streams-operators with Apache License 2.0 5 votes vote down vote up
@Override
public Flowable apply(Flowable input) {
    Context context = Vertx.currentContext();
    if (context != null && context.getDelegate() != null) {
        return input.compose(f -> f.observeOn(RxHelper.scheduler(context)));
    }
    return input;
}
 
Example 8
Source File: UnexpectedServerErrorIntegrationTest.java    From reactive-grpc with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void manyToMany() {
    RxGreeterGrpc.RxGreeterStub stub = RxGreeterGrpc.newRxStub(channel);
    Flowable<HelloRequest> req = Flowable.just(HelloRequest.getDefaultInstance());
    Flowable<HelloResponse> resp = req.compose(stub::sayHelloBothStream);
    TestSubscriber<HelloResponse> test = resp.test();

    test.awaitTerminalEvent(3, TimeUnit.SECONDS);
    test.assertError(t -> t instanceof StatusRuntimeException);
    test.assertError(t -> ((StatusRuntimeException)t).getStatus().getCode() == Status.Code.INTERNAL);
}
 
Example 9
Source File: RxGrpcPublisherManyToManyFusedVerificationTest.java    From reactive-grpc with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public Publisher<Message> createPublisher(long elements) {
    RxTckGrpc.RxTckStub stub = RxTckGrpc.newRxStub(channel);
    Flowable<Message> request = Flowable.range(0, (int)elements).map(this::toMessage);
    Publisher<Message> publisher = request.compose(stub::manyToMany);

    return publisher;
}
 
Example 10
Source File: RxGrpcPublisherManyToManyFusedVerificationTest.java    From reactive-grpc with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public Publisher<Message> createFailedPublisher() {
    RxTckGrpc.RxTckStub stub = RxTckGrpc.newRxStub(channel);
    Flowable<Message> request = Flowable.just(toMessage(TckService.KABOOM));
    Publisher<Message> publisher = request.compose(stub::manyToMany);

    return publisher;
}
 
Example 11
Source File: RxBusBuilder.java    From RxBus2 with Apache License 2.0 5 votes vote down vote up
private Flowable<T> applySchedular(Flowable<T> flowable)
{
    if (mBusMode == RxBusMode.Background)
        return flowable.compose(RxUtil.<T>applyBackgroundSchedulers());
    else if (mBusMode == RxBusMode.Main)
        return flowable.compose(RxUtil.<T>applySchedulars());
    return flowable;
}
 
Example 12
Source File: MainObserverTransformer.java    From pandroid with Apache License 2.0 5 votes vote down vote up
@Override
public Publisher<T> apply(Flowable<T> upstream) {
    Flowable<T> tObservable = upstream
            .observeOn(AndroidSchedulers.mainThread());
    if (provider == null) {
        return tObservable;
    }
    return tObservable.compose(RxLifecycleDelegate.<T>bindLifecycle(provider));
}
 
Example 13
Source File: ConcurrentRequestIntegrationTest.java    From reactive-grpc with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test
public void fourKindsOfRequestAtOnce() throws Exception {
    RxGreeterGrpc.RxGreeterStub stub = RxGreeterGrpc.newRxStub(channel);

    // == MAKE REQUESTS ==
    // One to One
    Single<HelloRequest> req1 = Single.just(HelloRequest.newBuilder().setName("rxjava").build());
    Single<HelloResponse> resp1 = req1.compose(stub::sayHello);

    // One to Many
    Single<HelloRequest> req2 = Single.just(HelloRequest.newBuilder().setName("rxjava").build());
    Flowable<HelloResponse> resp2 = req2.as(stub::sayHelloRespStream);

    // Many to One
    Flowable<HelloRequest> req3 = Flowable.just(
            HelloRequest.newBuilder().setName("a").build(),
            HelloRequest.newBuilder().setName("b").build(),
            HelloRequest.newBuilder().setName("c").build());

    Single<HelloResponse> resp3 = req3.as(stub::sayHelloReqStream);

    // Many to Many
    Flowable<HelloRequest> req4 = Flowable.just(
            HelloRequest.newBuilder().setName("a").build(),
            HelloRequest.newBuilder().setName("b").build(),
            HelloRequest.newBuilder().setName("c").build(),
            HelloRequest.newBuilder().setName("d").build(),
            HelloRequest.newBuilder().setName("e").build());

    Flowable<HelloResponse> resp4 = req4.compose(stub::sayHelloBothStream);

    // == VERIFY RESPONSES ==
    ListeningExecutorService executorService = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool());

    // Run all four verifications in parallel
    try {
        // One to One
        ListenableFuture<Boolean> oneToOne = executorService.submit(() -> {
            TestObserver<String> testObserver1 = resp1.map(HelloResponse::getMessage).test();
            testObserver1.awaitTerminalEvent(1, TimeUnit.SECONDS);
            testObserver1.assertValue("Hello rxjava");
            return true;
        });

        // One to Many
        ListenableFuture<Boolean> oneToMany = executorService.submit(() -> {
            TestSubscriber<String> testSubscriber1 = resp2.map(HelloResponse::getMessage).test();
            testSubscriber1.awaitTerminalEvent(1, TimeUnit.SECONDS);
            testSubscriber1.assertValues("Hello rxjava", "Hi rxjava", "Greetings rxjava");
            return true;
        });

        // Many to One
        ListenableFuture<Boolean> manyToOne = executorService.submit(() -> {
            TestObserver<String> testObserver2 = resp3.map(HelloResponse::getMessage).test();
            testObserver2.awaitTerminalEvent(1, TimeUnit.SECONDS);
            testObserver2.assertValue("Hello a and b and c");
            return true;
        });

        // Many to Many
        ListenableFuture<Boolean> manyToMany = executorService.submit(() -> {
            TestSubscriber<String> testSubscriber2 = resp4.map(HelloResponse::getMessage).test();
            testSubscriber2.awaitTerminalEvent(1, TimeUnit.SECONDS);
            testSubscriber2.assertValues("Hello a and b", "Hello c and d", "Hello e");
            testSubscriber2.assertComplete();
            return true;
        });

        @SuppressWarnings("unchecked")
        ListenableFuture<List<Boolean>> allFutures = Futures.allAsList(Lists.newArrayList(oneToOne, oneToMany, manyToOne, manyToMany));
        // Block for response
        List<Boolean> results = allFutures.get(3, TimeUnit.SECONDS);
        assertThat(results).containsExactly(true, true, true, true);

    } finally {
        executorService.shutdown();
    }
}
 
Example 14
Source File: Strings.java    From rxjava2-extras with Apache License 2.0 4 votes vote down vote up
public static Flowable<String> split(Flowable<String> source, String pattern) {
    return source.compose(Strings.split(pattern, BackpressureStrategy.BUFFER, 1));
}
 
Example 15
Source File: Strings.java    From rxjava2-extras with Apache License 2.0 4 votes vote down vote up
public static Flowable<String> decode(Flowable<byte[]> source, CharsetDecoder decoder) {
    return source.compose(Strings.decode(decoder));
}
 
Example 16
Source File: FlowableMatchTest.java    From rxjava2-extras with Apache License 2.0 4 votes vote down vote up
private static Flowable<Integer> matchThem(Flowable<Integer> a, Flowable<Integer> b) {
    return a.compose(
            Transformers.matchWith(b, Functions.identity(), Functions.identity(), COMBINER));
}