コード例 #1
0
  public void halfDuplexCallShouldSucceed() throws Exception {
    // Build the request.
    Integer[] responseSizes = {50, 100, 150, 200};
    final StreamingOutputCallRequest request = new StreamingOutputCallRequest();
    request.responseParameters = new ResponseParameters[responseSizes.length];
    request.responseType = Messages.COMPRESSABLE;
    for (int i = 0; i < responseSizes.length; ++i) {
      request.responseParameters[i] = new ResponseParameters();
      request.responseParameters[i].size = responseSizes[i];
      request.responseParameters[i].intervalUs = 0;
    }

    StreamRecorder<StreamingOutputCallResponse> recorder = StreamRecorder.create();
    StreamObserver<StreamingOutputCallRequest> requestStream = asyncStub.halfDuplexCall(recorder);

    final int numRequests = 10;
    for (int ix = numRequests; ix > 0; --ix) {
      requestStream.onNext(request);
    }
    requestStream.onCompleted();
    assertTrue(recorder.awaitCompletion(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS));
    assertSuccess(recorder);
    assertEquals(responseSizes.length * numRequests, recorder.getValues().size());
    for (int ix = 0; ix < recorder.getValues().size(); ++ix) {
      StreamingOutputCallResponse response = recorder.getValues().get(ix);
      assertEquals(Messages.COMPRESSABLE, response.payload.type);
      int length = response.payload.body.length;
      int expectedSize = responseSizes[ix % responseSizes.length];
      assertEquals("comparison failed at index " + ix, expectedSize, length);
    }
  }
コード例 #2
0
  public void serverStreaming() throws Exception {
    final Messages.StreamingOutputCallRequest request = new Messages.StreamingOutputCallRequest();
    request.responseType = Messages.COMPRESSABLE;
    request.responseParameters = new Messages.ResponseParameters[4];
    for (int i = 0; i < 4; i++) {
      request.responseParameters[i] = new Messages.ResponseParameters();
    }
    request.responseParameters[0].size = 31415;
    request.responseParameters[1].size = 9;
    request.responseParameters[2].size = 2653;
    request.responseParameters[3].size = 58979;

    final Messages.StreamingOutputCallResponse[] goldenResponses =
        new Messages.StreamingOutputCallResponse[4];
    for (int i = 0; i < 4; i++) {
      goldenResponses[i] = new Messages.StreamingOutputCallResponse();
      goldenResponses[i].payload = new Payload();
      goldenResponses[i].payload.type = Messages.COMPRESSABLE;
    }
    goldenResponses[0].payload.body = new byte[31415];
    goldenResponses[1].payload.body = new byte[9];
    goldenResponses[2].payload.body = new byte[2653];
    goldenResponses[3].payload.body = new byte[58979];

    StreamRecorder<Messages.StreamingOutputCallResponse> recorder = StreamRecorder.create();
    asyncStub.streamingOutputCall(request, recorder);
    assertTrue(recorder.awaitCompletion(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS));
    assertSuccess(recorder);
    assertMessageEquals(Arrays.asList(goldenResponses), recorder.getValues());
  }
コード例 #3
0
 public void cancelAfterBegin() throws Exception {
   StreamRecorder<StreamingInputCallResponse> responseObserver = StreamRecorder.create();
   StreamObserver<StreamingInputCallRequest> requestObserver =
       asyncStub.streamingInputCall(responseObserver);
   requestObserver.onError(new RuntimeException());
   assertTrue(responseObserver.awaitCompletion(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS));
   assertEquals(Arrays.<StreamingInputCallResponse>asList(), responseObserver.getValues());
   assertCodeEquals(
       io.grpc.Status.CANCELLED, io.grpc.Status.fromThrowable(responseObserver.getError()));
 }
コード例 #4
0
  /** Start a fullDuplexCall which the server will not respond, and verify the deadline expires. */
  public void timeoutOnSleepingServer() throws Exception {
    TestServiceGrpc.TestService stub =
        TestServiceGrpc.newStub(channel).withDeadlineAfter(1, TimeUnit.MILLISECONDS);
    StreamRecorder<StreamingOutputCallResponse> recorder = StreamRecorder.create();
    StreamObserver<StreamingOutputCallRequest> requestObserver = stub.fullDuplexCall(recorder);

    try {
      StreamingOutputCallRequest request = new StreamingOutputCallRequest();
      request.payload = new Messages.Payload();
      request.payload.body = new byte[27182];
      requestObserver.onNext(request);
    } catch (IllegalStateException expected) {
      // This can happen if the stream has already been terminated due to deadline exceeded.
    }

    assertTrue(recorder.awaitCompletion(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS));
    assertCodeEquals(
        io.grpc.Status.DEADLINE_EXCEEDED, io.grpc.Status.fromThrowable(recorder.getError()));
  }
コード例 #5
0
 public void deadlineExceededServerStreaming() throws Exception {
   // warm up the channel and JVM
   blockingStub.emptyCall(new EmptyProtos.Empty());
   ResponseParameters responseParameters = new ResponseParameters();
   responseParameters.size = 1;
   responseParameters.intervalUs = 10000;
   StreamingOutputCallRequest request = new StreamingOutputCallRequest();
   request.responseType = Messages.COMPRESSABLE;
   request.responseParameters = new ResponseParameters[4];
   request.responseParameters[0] = responseParameters;
   request.responseParameters[1] = responseParameters;
   request.responseParameters[2] = responseParameters;
   request.responseParameters[3] = responseParameters;
   StreamRecorder<StreamingOutputCallResponse> recorder = StreamRecorder.create();
   TestServiceGrpc.newStub(channel)
       .withDeadlineAfter(30, TimeUnit.MILLISECONDS)
       .streamingOutputCall(request, recorder);
   assertTrue(recorder.awaitCompletion(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS));
   assertCodeEquals(
       io.grpc.Status.DEADLINE_EXCEEDED, io.grpc.Status.fromThrowable(recorder.getError()));
 }
コード例 #6
0
  public void clientStreaming() throws Exception {
    final Messages.StreamingInputCallRequest[] requests = new Messages.StreamingInputCallRequest[4];
    for (int i = 0; i < 4; i++) {
      requests[i] = new Messages.StreamingInputCallRequest();
      requests[i].payload = new Payload();
    }
    requests[0].payload.body = new byte[27182];
    requests[1].payload.body = new byte[8];
    requests[2].payload.body = new byte[1828];
    requests[3].payload.body = new byte[45904];

    final Messages.StreamingInputCallResponse goldenResponse =
        new Messages.StreamingInputCallResponse();
    goldenResponse.aggregatedPayloadSize = 74922;

    StreamRecorder<Messages.StreamingInputCallResponse> responseObserver = StreamRecorder.create();
    StreamObserver<Messages.StreamingInputCallRequest> requestObserver =
        asyncStub.streamingInputCall(responseObserver);
    for (Messages.StreamingInputCallRequest request : requests) {
      requestObserver.onNext(request);
    }
    requestObserver.onCompleted();
    assertMessageEquals(goldenResponse, responseObserver.firstValue().get());
  }