Search in sources :

Example 1 with StreamingOutputCallResponse

use of io.grpc.testing.integration.Messages.StreamingOutputCallResponse in project grpc-java by grpc.

the class AbstractInteropTest method timeoutOnSleepingServer.

/** Start a fullDuplexCall which the server will not respond, and verify the deadline expires. */
@Test(timeout = 10000)
public void timeoutOnSleepingServer() throws Exception {
    TestServiceGrpc.TestServiceStub stub = TestServiceGrpc.newStub(channel).withDeadlineAfter(1, TimeUnit.MILLISECONDS);
    StreamRecorder<StreamingOutputCallResponse> responseObserver = StreamRecorder.create();
    StreamObserver<StreamingOutputCallRequest> requestObserver = stub.fullDuplexCall(responseObserver);
    StreamingOutputCallRequest request = StreamingOutputCallRequest.newBuilder().setPayload(Payload.newBuilder().setBody(ByteString.copyFrom(new byte[27182]))).build();
    try {
        requestObserver.onNext(request);
    } catch (IllegalStateException expected) {
    // This can happen if the stream has already been terminated due to deadline exceeded.
    }
    responseObserver.awaitCompletion(operationTimeoutMillis(), TimeUnit.MILLISECONDS);
    assertEquals(0, responseObserver.getValues().size());
    assertEquals(Status.DEADLINE_EXCEEDED.getCode(), Status.fromThrowable(responseObserver.getError()).getCode());
    if (metricsExpected()) {
        assertClientMetrics("grpc.testing.TestService/FullDuplexCall", Status.Code.DEADLINE_EXCEEDED);
    // Do not check server-side metrics, because the status on the server side is undetermined.
    }
}
Also used : StreamingOutputCallRequest(io.grpc.testing.integration.Messages.StreamingOutputCallRequest) StreamingOutputCallResponse(io.grpc.testing.integration.Messages.StreamingOutputCallResponse) Test(org.junit.Test)

Example 2 with StreamingOutputCallResponse

use of io.grpc.testing.integration.Messages.StreamingOutputCallResponse in project grpc-java by grpc.

the class AbstractInteropTest method serverStreaming.

@Test(timeout = 10000)
public void serverStreaming() throws Exception {
    final StreamingOutputCallRequest request = StreamingOutputCallRequest.newBuilder().setResponseType(PayloadType.COMPRESSABLE).addResponseParameters(ResponseParameters.newBuilder().setSize(31415)).addResponseParameters(ResponseParameters.newBuilder().setSize(9)).addResponseParameters(ResponseParameters.newBuilder().setSize(2653)).addResponseParameters(ResponseParameters.newBuilder().setSize(58979)).build();
    final List<StreamingOutputCallResponse> goldenResponses = Arrays.asList(StreamingOutputCallResponse.newBuilder().setPayload(Payload.newBuilder().setType(PayloadType.COMPRESSABLE).setBody(ByteString.copyFrom(new byte[31415]))).build(), StreamingOutputCallResponse.newBuilder().setPayload(Payload.newBuilder().setType(PayloadType.COMPRESSABLE).setBody(ByteString.copyFrom(new byte[9]))).build(), StreamingOutputCallResponse.newBuilder().setPayload(Payload.newBuilder().setType(PayloadType.COMPRESSABLE).setBody(ByteString.copyFrom(new byte[2653]))).build(), StreamingOutputCallResponse.newBuilder().setPayload(Payload.newBuilder().setType(PayloadType.COMPRESSABLE).setBody(ByteString.copyFrom(new byte[58979]))).build());
    StreamRecorder<StreamingOutputCallResponse> recorder = StreamRecorder.create();
    asyncStub.streamingOutputCall(request, recorder);
    recorder.awaitCompletion();
    assertSuccess(recorder);
    assertEquals(goldenResponses, recorder.getValues());
}
Also used : StreamingOutputCallRequest(io.grpc.testing.integration.Messages.StreamingOutputCallRequest) StreamingOutputCallResponse(io.grpc.testing.integration.Messages.StreamingOutputCallResponse) Test(org.junit.Test)

Example 3 with StreamingOutputCallResponse

use of io.grpc.testing.integration.Messages.StreamingOutputCallResponse in project grpc-java by grpc.

the class AbstractInteropTest method customMetadata.

@Test(timeout = 10000)
public void customMetadata() throws Exception {
    final int responseSize = 314159;
    final int requestSize = 271828;
    final SimpleRequest request = SimpleRequest.newBuilder().setResponseSize(responseSize).setResponseType(PayloadType.COMPRESSABLE).setPayload(Payload.newBuilder().setBody(ByteString.copyFrom(new byte[requestSize]))).build();
    final StreamingOutputCallRequest streamingRequest = StreamingOutputCallRequest.newBuilder().addResponseParameters(ResponseParameters.newBuilder().setSize(responseSize)).setResponseType(PayloadType.COMPRESSABLE).setPayload(Payload.newBuilder().setBody(ByteString.copyFrom(new byte[requestSize]))).build();
    final SimpleResponse goldenResponse = SimpleResponse.newBuilder().setPayload(Payload.newBuilder().setType(PayloadType.COMPRESSABLE).setBody(ByteString.copyFrom(new byte[responseSize]))).build();
    final StreamingOutputCallResponse goldenStreamingResponse = StreamingOutputCallResponse.newBuilder().setPayload(Payload.newBuilder().setType(PayloadType.COMPRESSABLE).setBody(ByteString.copyFrom(new byte[responseSize]))).build();
    final byte[] trailingBytes = { (byte) 0xa, (byte) 0xb, (byte) 0xa, (byte) 0xb, (byte) 0xa, (byte) 0xb };
    // Test UnaryCall
    Metadata metadata = new Metadata();
    metadata.put(Util.ECHO_INITIAL_METADATA_KEY, "test_initial_metadata_value");
    metadata.put(Util.ECHO_TRAILING_METADATA_KEY, trailingBytes);
    TestServiceGrpc.TestServiceBlockingStub blockingStub = TestServiceGrpc.newBlockingStub(channel);
    blockingStub = MetadataUtils.attachHeaders(blockingStub, metadata);
    AtomicReference<Metadata> headersCapture = new AtomicReference<Metadata>();
    AtomicReference<Metadata> trailersCapture = new AtomicReference<Metadata>();
    blockingStub = MetadataUtils.captureMetadata(blockingStub, headersCapture, trailersCapture);
    SimpleResponse response = blockingStub.unaryCall(request);
    assertEquals(goldenResponse, response);
    assertEquals("test_initial_metadata_value", headersCapture.get().get(Util.ECHO_INITIAL_METADATA_KEY));
    assertTrue(Arrays.equals(trailingBytes, trailersCapture.get().get(Util.ECHO_TRAILING_METADATA_KEY)));
    if (metricsExpected()) {
        assertMetrics("grpc.testing.TestService/UnaryCall", Status.Code.OK, Collections.singleton(request), Collections.singleton(goldenResponse));
    }
    // Test FullDuplexCall
    metadata = new Metadata();
    metadata.put(Util.ECHO_INITIAL_METADATA_KEY, "test_initial_metadata_value");
    metadata.put(Util.ECHO_TRAILING_METADATA_KEY, trailingBytes);
    TestServiceGrpc.TestServiceStub stub = TestServiceGrpc.newStub(channel);
    stub = MetadataUtils.attachHeaders(stub, metadata);
    headersCapture = new AtomicReference<Metadata>();
    trailersCapture = new AtomicReference<Metadata>();
    stub = MetadataUtils.captureMetadata(stub, headersCapture, trailersCapture);
    StreamRecorder<Messages.StreamingOutputCallResponse> recorder = StreamRecorder.create();
    StreamObserver<Messages.StreamingOutputCallRequest> requestStream = stub.fullDuplexCall(recorder);
    requestStream.onNext(streamingRequest);
    requestStream.onCompleted();
    recorder.awaitCompletion();
    assertSuccess(recorder);
    assertEquals(goldenStreamingResponse, recorder.firstValue().get());
    assertEquals("test_initial_metadata_value", headersCapture.get().get(Util.ECHO_INITIAL_METADATA_KEY));
    assertTrue(Arrays.equals(trailingBytes, trailersCapture.get().get(Util.ECHO_TRAILING_METADATA_KEY)));
    if (metricsExpected()) {
        assertMetrics("grpc.testing.TestService/FullDuplexCall", Status.Code.OK, Collections.singleton(streamingRequest), Collections.singleton(goldenStreamingResponse));
    }
}
Also used : Metadata(io.grpc.Metadata) AtomicReference(java.util.concurrent.atomic.AtomicReference) SimpleRequest(io.grpc.testing.integration.Messages.SimpleRequest) SimpleResponse(io.grpc.testing.integration.Messages.SimpleResponse) StreamingOutputCallRequest(io.grpc.testing.integration.Messages.StreamingOutputCallRequest) StreamingOutputCallResponse(io.grpc.testing.integration.Messages.StreamingOutputCallResponse) Test(org.junit.Test)

Example 4 with StreamingOutputCallResponse

use of io.grpc.testing.integration.Messages.StreamingOutputCallResponse 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 StreamingOutputCallResponse

use of io.grpc.testing.integration.Messages.StreamingOutputCallResponse in project grpc-java by grpc.

the class AbstractInteropTest method cancelAfterFirstResponse.

@Test(timeout = 10000)
public void cancelAfterFirstResponse() throws Exception {
    final StreamingOutputCallRequest request = StreamingOutputCallRequest.newBuilder().addResponseParameters(ResponseParameters.newBuilder().setSize(31415)).setPayload(Payload.newBuilder().setBody(ByteString.copyFrom(new byte[27182]))).build();
    final StreamingOutputCallResponse goldenResponse = StreamingOutputCallResponse.newBuilder().setPayload(Payload.newBuilder().setType(PayloadType.COMPRESSABLE).setBody(ByteString.copyFrom(new byte[31415]))).build();
    StreamRecorder<StreamingOutputCallResponse> responseObserver = StreamRecorder.create();
    StreamObserver<StreamingOutputCallRequest> requestObserver = asyncStub.fullDuplexCall(responseObserver);
    requestObserver.onNext(request);
    assertEquals(goldenResponse, responseObserver.firstValue().get());
    requestObserver.onError(new RuntimeException());
    responseObserver.awaitCompletion(operationTimeoutMillis(), TimeUnit.MILLISECONDS);
    assertEquals(1, responseObserver.getValues().size());
    assertEquals(Status.Code.CANCELLED, Status.fromThrowable(responseObserver.getError()).getCode());
    if (metricsExpected()) {
        assertMetrics("grpc.testing.TestService/FullDuplexCall", Status.Code.CANCELLED);
    }
}
Also used : StatusRuntimeException(io.grpc.StatusRuntimeException) StreamingOutputCallRequest(io.grpc.testing.integration.Messages.StreamingOutputCallRequest) StreamingOutputCallResponse(io.grpc.testing.integration.Messages.StreamingOutputCallResponse) Test(org.junit.Test)

Aggregations

StreamingOutputCallRequest (io.grpc.testing.integration.Messages.StreamingOutputCallRequest)9 StreamingOutputCallResponse (io.grpc.testing.integration.Messages.StreamingOutputCallResponse)9 Test (org.junit.Test)9 Metadata (io.grpc.Metadata)2 StatusRuntimeException (io.grpc.StatusRuntimeException)2 EchoStatus (io.grpc.testing.integration.Messages.EchoStatus)2 SimpleRequest (io.grpc.testing.integration.Messages.SimpleRequest)2 ArrayList (java.util.ArrayList)2 ByteString (com.google.protobuf.ByteString)1 ClientCall (io.grpc.ClientCall)1 Status (io.grpc.Status)1 ResponseParameters (io.grpc.testing.integration.Messages.ResponseParameters)1 SimpleResponse (io.grpc.testing.integration.Messages.SimpleResponse)1 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1