Search in sources :

Example 1 with ClientCall

use of io.grpc.ClientCall in project grpc-java by grpc.

the class TransportCompressionTest method createChannel.

@Override
protected ManagedChannel createChannel() {
    NettyChannelBuilder builder = NettyChannelBuilder.forAddress("localhost", getPort()).maxInboundMessageSize(AbstractInteropTest.MAX_MESSAGE_SIZE).decompressorRegistry(decompressors).compressorRegistry(compressors).intercept(new ClientInterceptor() {

        @Override
        public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
            final ClientCall<ReqT, RespT> call = next.newCall(method, callOptions);
            return new ForwardingClientCall<ReqT, RespT>() {

                @Override
                protected ClientCall<ReqT, RespT> delegate() {
                    return call;
                }

                @Override
                public void start(final ClientCall.Listener<RespT> responseListener, Metadata headers) {
                    ClientCall.Listener<RespT> listener = new ForwardingClientCallListener<RespT>() {

                        @Override
                        protected io.grpc.ClientCall.Listener<RespT> delegate() {
                            return responseListener;
                        }

                        @Override
                        public void onHeaders(Metadata headers) {
                            super.onHeaders(headers);
                            if (expectFzip) {
                                String encoding = headers.get(GrpcUtil.MESSAGE_ENCODING_KEY);
                                assertEquals(encoding, FZIPPER.getMessageEncoding());
                            }
                        }
                    };
                    super.start(listener, headers);
                    setMessageCompression(true);
                }
            };
        }
    }).usePlaintext(true);
    io.grpc.internal.TestingAccessor.setStatsContextFactory(builder, getClientStatsFactory());
    return builder.build();
}
Also used : ForwardingClientCallListener(io.grpc.ForwardingClientCallListener) Listener(io.grpc.ServerCall.Listener) ManagedChannel(io.grpc.ManagedChannel) Channel(io.grpc.Channel) ForwardingClientCall(io.grpc.ForwardingClientCall) Metadata(io.grpc.Metadata) CallOptions(io.grpc.CallOptions) ByteString(com.google.protobuf.ByteString) MethodDescriptor(io.grpc.MethodDescriptor) ClientCall(io.grpc.ClientCall) ForwardingClientCall(io.grpc.ForwardingClientCall) ClientInterceptor(io.grpc.ClientInterceptor) NettyChannelBuilder(io.grpc.netty.NettyChannelBuilder) ForwardingClientCallListener(io.grpc.ForwardingClientCallListener)

Example 2 with ClientCall

use of io.grpc.ClientCall in project grpc-java by grpc.

the class ServerCallsTest method inprocessTransportManualFlow.

@Test
public void inprocessTransportManualFlow() throws Exception {
    final Semaphore semaphore = new Semaphore(1);
    ServerServiceDefinition service = ServerServiceDefinition.builder(new ServiceDescriptor("some", STREAMING_METHOD)).addMethod(STREAMING_METHOD, ServerCalls.asyncBidiStreamingCall(new ServerCalls.BidiStreamingMethod<Integer, Integer>() {

        int iteration;

        @Override
        public StreamObserver<Integer> invoke(StreamObserver<Integer> responseObserver) {
            final ServerCallStreamObserver<Integer> serverCallObserver = (ServerCallStreamObserver<Integer>) responseObserver;
            serverCallObserver.setOnReadyHandler(new Runnable() {

                @Override
                public void run() {
                    while (serverCallObserver.isReady()) {
                        serverCallObserver.onNext(iteration);
                    }
                    iteration++;
                    semaphore.release();
                }
            });
            return new ServerCalls.NoopStreamObserver<Integer>() {

                @Override
                public void onCompleted() {
                    serverCallObserver.onCompleted();
                }
            };
        }
    })).build();
    long tag = System.nanoTime();
    InProcessServerBuilder.forName("go-with-the-flow" + tag).addService(service).build().start();
    ManagedChannel channel = InProcessChannelBuilder.forName("go-with-the-flow" + tag).build();
    final ClientCall<Integer, Integer> clientCall = channel.newCall(STREAMING_METHOD, CallOptions.DEFAULT);
    final CountDownLatch latch = new CountDownLatch(1);
    final int[] receivedMessages = new int[6];
    clientCall.start(new ClientCall.Listener<Integer>() {

        int index;

        @Override
        public void onMessage(Integer message) {
            receivedMessages[index++] = message;
        }

        @Override
        public void onClose(Status status, Metadata trailers) {
            latch.countDown();
        }
    }, new Metadata());
    semaphore.acquire();
    clientCall.request(1);
    semaphore.acquire();
    clientCall.request(2);
    semaphore.acquire();
    clientCall.request(3);
    clientCall.halfClose();
    latch.await(5, TimeUnit.SECONDS);
    // Very that number of messages produced in each onReady handler call matches the number
    // requested by the client.
    assertArrayEquals(new int[] { 0, 1, 1, 2, 2, 2 }, receivedMessages);
}
Also used : Status(io.grpc.Status) Metadata(io.grpc.Metadata) Semaphore(java.util.concurrent.Semaphore) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ServiceDescriptor(io.grpc.ServiceDescriptor) ClientCall(io.grpc.ClientCall) ServerServiceDefinition(io.grpc.ServerServiceDefinition) ManagedChannel(io.grpc.ManagedChannel) Test(org.junit.Test)

Example 3 with ClientCall

use of io.grpc.ClientCall in project grpc-java by grpc.

the class DetailErrorSample method advancedAsyncCall.

/**
   * This is more advanced and does not make use of the stub.  You should not normally need to do
   * this, but here is how you would.
   */
void advancedAsyncCall() {
    ClientCall<HelloRequest, HelloReply> call = channel.newCall(GreeterGrpc.METHOD_SAY_HELLO, CallOptions.DEFAULT);
    final CountDownLatch latch = new CountDownLatch(1);
    call.start(new ClientCall.Listener<HelloReply>() {

        @Override
        public void onClose(Status status, Metadata trailers) {
            Verify.verify(status.getCode() == Status.Code.INTERNAL);
            Verify.verify(trailers.containsKey(DEBUG_INFO_TRAILER_KEY));
            try {
                Verify.verify(trailers.get(DEBUG_INFO_TRAILER_KEY).equals(DEBUG_INFO));
            } catch (IllegalArgumentException e) {
                throw new VerifyException(e);
            }
            latch.countDown();
        }
    }, new Metadata());
    call.sendMessage(HelloRequest.newBuilder().build());
    call.halfClose();
    if (!Uninterruptibles.awaitUninterruptibly(latch, 1, TimeUnit.SECONDS)) {
        throw new RuntimeException("timeout!");
    }
}
Also used : Status(io.grpc.Status) VerifyException(com.google.common.base.VerifyException) ClientCall(io.grpc.ClientCall) HelloRequest(io.grpc.examples.helloworld.HelloRequest) Metadata(io.grpc.Metadata) HelloReply(io.grpc.examples.helloworld.HelloReply) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 4 with ClientCall

use of io.grpc.ClientCall in project grpc-java by grpc.

the class AbstractInteropTest method serverStreamingShouldBeFlowControlled.

@Test(timeout = 10000)
public void serverStreamingShouldBeFlowControlled() throws Exception {
    final StreamingOutputCallRequest request = StreamingOutputCallRequest.newBuilder().setResponseType(COMPRESSABLE).addResponseParameters(ResponseParameters.newBuilder().setSize(100000)).addResponseParameters(ResponseParameters.newBuilder().setSize(100001)).build();
    final List<StreamingOutputCallResponse> goldenResponses = Arrays.asList(StreamingOutputCallResponse.newBuilder().setPayload(Payload.newBuilder().setType(PayloadType.COMPRESSABLE).setBody(ByteString.copyFrom(new byte[100000]))).build(), StreamingOutputCallResponse.newBuilder().setPayload(Payload.newBuilder().setType(PayloadType.COMPRESSABLE).setBody(ByteString.copyFrom(new byte[100001]))).build());
    long start = System.nanoTime();
    final ArrayBlockingQueue<Object> queue = new ArrayBlockingQueue<Object>(10);
    ClientCall<StreamingOutputCallRequest, StreamingOutputCallResponse> call = channel.newCall(TestServiceGrpc.METHOD_STREAMING_OUTPUT_CALL, CallOptions.DEFAULT);
    call.start(new ClientCall.Listener<StreamingOutputCallResponse>() {

        @Override
        public void onHeaders(Metadata headers) {
        }

        @Override
        public void onMessage(final StreamingOutputCallResponse message) {
            queue.add(message);
        }

        @Override
        public void onClose(Status status, Metadata trailers) {
            queue.add(status);
        }
    }, new Metadata());
    call.sendMessage(request);
    call.halfClose();
    // Time how long it takes to get the first response.
    call.request(1);
    assertEquals(goldenResponses.get(0), queue.poll(operationTimeoutMillis(), TimeUnit.MILLISECONDS));
    long firstCallDuration = System.nanoTime() - start;
    // Without giving additional flow control, make sure that we don't get another response. We wait
    // until we are comfortable the next message isn't coming. We may have very low nanoTime
    // resolution (like on Windows) or be using a testing, in-process transport where message
    // handling is instantaneous. In both cases, firstCallDuration may be 0, so round up sleep time
    // to at least 1ms.
    assertNull(queue.poll(Math.max(firstCallDuration * 4, 1 * 1000 * 1000), TimeUnit.NANOSECONDS));
    // Make sure that everything still completes.
    call.request(1);
    assertEquals(goldenResponses.get(1), queue.poll(operationTimeoutMillis(), TimeUnit.MILLISECONDS));
    assertEquals(Status.OK, queue.poll(operationTimeoutMillis(), TimeUnit.MILLISECONDS));
}
Also used : Status(io.grpc.Status) EchoStatus(io.grpc.testing.integration.Messages.EchoStatus) Metadata(io.grpc.Metadata) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) ClientCall(io.grpc.ClientCall) StreamingOutputCallRequest(io.grpc.testing.integration.Messages.StreamingOutputCallRequest) StreamingOutputCallResponse(io.grpc.testing.integration.Messages.StreamingOutputCallResponse) Test(org.junit.Test)

Example 5 with ClientCall

use of io.grpc.ClientCall in project grpc-java by grpc.

the class ErrorHandlingClient method advancedAsyncCall.

/**
   * This is more advanced and does not make use of the stub.  You should not normally need to do
   * this, but here is how you would.
   */
void advancedAsyncCall() {
    ClientCall<HelloRequest, HelloReply> call = channel.newCall(GreeterGrpc.METHOD_SAY_HELLO, CallOptions.DEFAULT);
    final CountDownLatch latch = new CountDownLatch(1);
    call.start(new ClientCall.Listener<HelloReply>() {

        @Override
        public void onClose(Status status, Metadata trailers) {
            Verify.verify(status.getCode() == Status.Code.INTERNAL);
            Verify.verify(status.getDescription().contains("Narwhal"));
            // Cause is not transmitted over the wire.
            latch.countDown();
        }
    }, new Metadata());
    call.sendMessage(HelloRequest.newBuilder().setName("Marge").build());
    call.halfClose();
    if (!Uninterruptibles.awaitUninterruptibly(latch, 1, TimeUnit.SECONDS)) {
        throw new RuntimeException("timeout!");
    }
}
Also used : Status(io.grpc.Status) ClientCall(io.grpc.ClientCall) HelloRequest(io.grpc.examples.helloworld.HelloRequest) Metadata(io.grpc.Metadata) HelloReply(io.grpc.examples.helloworld.HelloReply) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

ClientCall (io.grpc.ClientCall)6 Metadata (io.grpc.Metadata)6 Status (io.grpc.Status)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 ManagedChannel (io.grpc.ManagedChannel)2 HelloReply (io.grpc.examples.helloworld.HelloReply)2 HelloRequest (io.grpc.examples.helloworld.HelloRequest)2 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)2 Test (org.junit.Test)2 VerifyException (com.google.common.base.VerifyException)1 ByteString (com.google.protobuf.ByteString)1 CallOptions (io.grpc.CallOptions)1 Channel (io.grpc.Channel)1 ClientInterceptor (io.grpc.ClientInterceptor)1 ForwardingClientCall (io.grpc.ForwardingClientCall)1 ForwardingClientCallListener (io.grpc.ForwardingClientCallListener)1 MethodDescriptor (io.grpc.MethodDescriptor)1 Listener (io.grpc.ServerCall.Listener)1 ServerServiceDefinition (io.grpc.ServerServiceDefinition)1 ServiceDescriptor (io.grpc.ServiceDescriptor)1