Search in sources :

Example 6 with ClientCall

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

the class InteropTester method serverStreamingShouldBeFlowControlled.

public void serverStreamingShouldBeFlowControlled() throws Exception {
    final StreamingOutputCallRequest request = new StreamingOutputCallRequest();
    request.responseType = Messages.COMPRESSABLE;
    request.responseParameters = new ResponseParameters[2];
    request.responseParameters[0] = new ResponseParameters();
    request.responseParameters[0].size = 100000;
    request.responseParameters[1] = new ResponseParameters();
    request.responseParameters[1].size = 100001;
    final StreamingOutputCallResponse[] goldenResponses = new StreamingOutputCallResponse[2];
    goldenResponses[0] = new StreamingOutputCallResponse();
    goldenResponses[0].payload = new Payload();
    goldenResponses[0].payload.type = Messages.COMPRESSABLE;
    goldenResponses[0].payload.body = new byte[100000];
    goldenResponses[1] = new StreamingOutputCallResponse();
    goldenResponses[1].payload = new Payload();
    goldenResponses[1].payload.type = Messages.COMPRESSABLE;
    goldenResponses[1].payload.body = new byte[100001];
    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(io.grpc.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);
    assertMessageEquals(goldenResponses[0], (StreamingOutputCallResponse) queue.poll(TIMEOUT_MILLIS, 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);
    assertMessageEquals(goldenResponses[1], (StreamingOutputCallResponse) queue.poll(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS));
    assertCodeEquals(io.grpc.Status.OK, (io.grpc.Status) queue.poll(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS));
}
Also used : ResponseParameters(io.grpc.android.integrationtest.nano.Messages.ResponseParameters) Metadata(io.grpc.Metadata) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) ClientCall(io.grpc.ClientCall) Payload(io.grpc.android.integrationtest.nano.Messages.Payload) StreamingOutputCallRequest(io.grpc.android.integrationtest.nano.Messages.StreamingOutputCallRequest) StreamingOutputCallResponse(io.grpc.android.integrationtest.nano.Messages.StreamingOutputCallResponse)

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