Search in sources :

Example 6 with StreamingOutputCallRequest

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

the class AbstractInteropTest method maxInboundSize_tooBig.

@Test(timeout = 10000)
public void maxInboundSize_tooBig() {
    StreamingOutputCallRequest request = StreamingOutputCallRequest.newBuilder().addResponseParameters(ResponseParameters.newBuilder().setSize(1)).build();
    int size = blockingStub.streamingOutputCall(request).next().getSerializedSize();
    TestServiceGrpc.TestServiceBlockingStub stub = TestServiceGrpc.newBlockingStub(channel).withMaxInboundMessageSize(size - 1);
    try {
        stub.streamingOutputCall(request).next();
        fail();
    } catch (StatusRuntimeException ex) {
        Status s = ex.getStatus();
        assertThat(s.getCode()).named(s.toString()).isEqualTo(Status.Code.INTERNAL);
        assertThat(Throwables.getStackTraceAsString(ex)).contains("exceeds maximum");
    }
}
Also used : Status(io.grpc.Status) EchoStatus(io.grpc.testing.integration.Messages.EchoStatus) StatusRuntimeException(io.grpc.StatusRuntimeException) StreamingOutputCallRequest(io.grpc.testing.integration.Messages.StreamingOutputCallRequest) Test(org.junit.Test)

Example 7 with StreamingOutputCallRequest

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

the class AbstractInteropTest method maxOutboundSize_exact.

@Test(timeout = 10000)
public void maxOutboundSize_exact() {
    // warm up the channel and JVM
    blockingStub.emptyCall(Empty.getDefaultInstance());
    // set at least one field to ensure the size is non-zero.
    StreamingOutputCallRequest request = StreamingOutputCallRequest.newBuilder().addResponseParameters(ResponseParameters.newBuilder().setSize(1)).build();
    TestServiceGrpc.TestServiceBlockingStub stub = TestServiceGrpc.newBlockingStub(channel).withMaxOutboundMessageSize(request.getSerializedSize());
    stub.streamingOutputCall(request).next();
}
Also used : StreamingOutputCallRequest(io.grpc.testing.integration.Messages.StreamingOutputCallRequest) Test(org.junit.Test)

Example 8 with StreamingOutputCallRequest

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

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

the class NettyFlowControlTest method doTest.

/**
   * Main testing method. Streams 2 MB of data from a server and records the final window and
   * average bandwidth usage.
   */
private void doTest(int bandwidth, int latency) throws InterruptedException {
    int streamSize = 1 * 1024 * 1024;
    long expectedWindow = (latency * (bandwidth / TimeUnit.SECONDS.toMillis(1)));
    TestServiceGrpc.TestServiceStub stub = TestServiceGrpc.newStub(channel);
    StreamingOutputCallRequest.Builder builder = StreamingOutputCallRequest.newBuilder().addResponseParameters(ResponseParameters.newBuilder().setSize(streamSize / 16)).addResponseParameters(ResponseParameters.newBuilder().setSize(streamSize / 16)).addResponseParameters(ResponseParameters.newBuilder().setSize(streamSize / 8)).addResponseParameters(ResponseParameters.newBuilder().setSize(streamSize / 4)).addResponseParameters(ResponseParameters.newBuilder().setSize(streamSize / 2));
    StreamingOutputCallRequest request = builder.build();
    TestStreamObserver observer = new TestStreamObserver(expectedWindow);
    stub.streamingOutputCall(request, observer);
    int lastWindow = observer.waitFor();
    // deal with cases that either don't cause a window update or hit max window
    expectedWindow = Math.min(MAX_WINDOW, (Math.max(expectedWindow, REGULAR_WINDOW)));
    // Range looks large, but this allows for only one extra/missed window update
    // (one extra update causes a 2x difference and one missed update causes a .5x difference)
    assertTrue("Window was " + lastWindow + " expecting " + expectedWindow, lastWindow < 2 * expectedWindow);
    assertTrue("Window was " + lastWindow + " expecting " + expectedWindow, expectedWindow < 2 * lastWindow);
}
Also used : StreamingOutputCallRequest(io.grpc.testing.integration.Messages.StreamingOutputCallRequest)

Example 10 with StreamingOutputCallRequest

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

Aggregations

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