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

The following examples show how to use io.reactivex.Single#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 oneToOne() {
    RxGreeterGrpc.RxGreeterStub stub = RxGreeterGrpc.newRxStub(channel);
    Single<HelloRequest> req = Single.just(HelloRequest.newBuilder().setName("rxjava").build());
    Single<HelloResponse> resp = req.compose(stub::sayHello);

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

    TestObserver<String> testObserver = resp
            .map(HelloResponse::getMessage)
            .doOnSuccess(x -> clientThreadName.set(Thread.currentThread().getName()))
            .test();
    testObserver.awaitTerminalEvent(3, TimeUnit.SECONDS);

    assertThat(clientThreadName.get()).isEqualTo("TheGrpcClient");
    assertThat(serverThreadName.get()).isEqualTo("TheGrpcServer");
}
 
Example 2
Source File: UnaryZeroMessageResponseIntegrationTest.java    From reactive-grpc with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void zeroMessageResponseOneToOne() {
    serverRule.getServiceRegistry().addService(new MissingUnaryResponseService());

    RxGreeterGrpc.RxGreeterStub stub = RxGreeterGrpc.newRxStub(serverRule.getChannel());
    Single<HelloRequest> req = Single.just(HelloRequest.newBuilder().setName("rxjava").build());
    Single<HelloResponse> resp = req.compose(stub::sayHello);

    TestObserver<String> testObserver = resp.map(HelloResponse::getMessage).test();
    testObserver.awaitTerminalEvent(3, TimeUnit.SECONDS);
    testObserver.assertError(StatusRuntimeException.class);
    testObserver.assertError(t -> ((StatusRuntimeException) t).getStatus().getCode() == Status.CANCELLED.getCode());
}
 
Example 3
Source File: EndToEndIntegrationTest.java    From reactive-grpc with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void oneToOne() {
    RxGreeterGrpc.RxGreeterStub stub = RxGreeterGrpc.newRxStub(channel);
    Single<HelloRequest> req = Single.just(HelloRequest.newBuilder().setName("rxjava").build());
    Single<HelloResponse> resp = req.compose(stub::sayHello);

    TestObserver<String> testObserver = resp.map(HelloResponse::getMessage).test();
    testObserver.awaitTerminalEvent(3, TimeUnit.SECONDS);
    testObserver.assertValue("Hello rxjava");
}
 
Example 4
Source File: RxGrpcPublisherOneToOneVerificationTest.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);
    Single<Message> request = Single.just(toMessage((int) elements));
    Single<Message> publisher = request.compose(stub::oneToOne);

    return publisher.toFlowable();
}
 
Example 5
Source File: RxGrpcPublisherOneToOneVerificationTest.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);
    Single<Message> request = Single.just(toMessage(TckService.KABOOM));
    Single<Message> publisher = request.compose(stub::oneToOne);

    return publisher.toFlowable();
}
 
Example 6
Source File: MainObserverTransformer.java    From pandroid with Apache License 2.0 5 votes vote down vote up
@Override
public SingleSource<T> apply(Single<T> upstream) {
    Single<T> tObservable = upstream
            .observeOn(AndroidSchedulers.mainThread());
    if (provider == null) {
        return tObservable;
    }
    return tObservable.compose(RxLifecycleDelegate.<T>bindLifecycle(provider));
}
 
Example 7
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();
    }
}