Search in sources :

Example 1 with StreamingInputCallResponse

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

the class MoreInProcessTest method asyncClientStreaming_serverErrorPriorToRequest.

@Test
public void asyncClientStreaming_serverErrorPriorToRequest() throws Exception {
    // implement a service
    final Status fakeError = Status.INVALID_ARGUMENT;
    TestServiceImplBase clientStreamingImpl = new TestServiceImplBase() {

        @Override
        public StreamObserver<StreamingInputCallRequest> streamingInputCall(StreamObserver<StreamingInputCallResponse> responseObserver) {
            // send error directly
            responseObserver.onError(new StatusRuntimeException(fakeError));
            responseObserver.onCompleted();
            return new StreamObserver<StreamingInputCallRequest>() {

                @Override
                public void onNext(StreamingInputCallRequest value) {
                }

                @Override
                public void onError(Throwable t) {
                }

                @Override
                public void onCompleted() {
                }
            };
        }
    };
    serviceRegistry.addService(clientStreamingImpl);
    // implement a client
    final CountDownLatch finishLatch = new CountDownLatch(1);
    final AtomicReference<StreamingInputCallResponse> responseRef = new AtomicReference<>();
    final AtomicReference<Throwable> throwableRef = new AtomicReference<>();
    StreamObserver<StreamingInputCallResponse> responseObserver = new StreamObserver<StreamingInputCallResponse>() {

        @Override
        public void onNext(StreamingInputCallResponse response) {
            responseRef.set(response);
        }

        @Override
        public void onError(Throwable t) {
            throwableRef.set(t);
            finishLatch.countDown();
        }

        @Override
        public void onCompleted() {
            finishLatch.countDown();
        }
    };
    // make a gRPC call
    TestServiceGrpc.newStub(inProcessChannel).streamingInputCall(responseObserver);
    assertTrue(finishLatch.await(900, TimeUnit.MILLISECONDS));
    assertEquals(fakeError.getCode(), Status.fromThrowable(throwableRef.get()).getCode());
    assertNull(responseRef.get());
}
Also used : Status(io.grpc.Status) StreamObserver(io.grpc.stub.StreamObserver) StreamingInputCallRequest(io.grpc.testing.integration.Messages.StreamingInputCallRequest) StatusRuntimeException(io.grpc.StatusRuntimeException) TestServiceImplBase(io.grpc.testing.integration.TestServiceGrpc.TestServiceImplBase) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) StreamingInputCallResponse(io.grpc.testing.integration.Messages.StreamingInputCallResponse) Test(org.junit.Test)

Example 2 with StreamingInputCallResponse

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

the class MoreInProcessTest method asyncClientStreaming_serverResponsePriorToRequest.

@Test
public void asyncClientStreaming_serverResponsePriorToRequest() throws Exception {
    // implement a service
    final StreamingInputCallResponse fakeResponse = StreamingInputCallResponse.newBuilder().setAggregatedPayloadSize(100).build();
    TestServiceImplBase clientStreamingImpl = new TestServiceImplBase() {

        @Override
        public StreamObserver<StreamingInputCallRequest> streamingInputCall(StreamObserver<StreamingInputCallResponse> responseObserver) {
            // send response directly
            responseObserver.onNext(fakeResponse);
            responseObserver.onCompleted();
            return new StreamObserver<StreamingInputCallRequest>() {

                @Override
                public void onNext(StreamingInputCallRequest value) {
                }

                @Override
                public void onError(Throwable t) {
                }

                @Override
                public void onCompleted() {
                }
            };
        }
    };
    serviceRegistry.addService(clientStreamingImpl);
    // implement a client
    final CountDownLatch finishLatch = new CountDownLatch(1);
    final AtomicReference<StreamingInputCallResponse> responseRef = new AtomicReference<>();
    final AtomicReference<Throwable> throwableRef = new AtomicReference<>();
    StreamObserver<StreamingInputCallResponse> responseObserver = new StreamObserver<StreamingInputCallResponse>() {

        @Override
        public void onNext(StreamingInputCallResponse response) {
            responseRef.set(response);
        }

        @Override
        public void onError(Throwable t) {
            throwableRef.set(t);
            finishLatch.countDown();
        }

        @Override
        public void onCompleted() {
            finishLatch.countDown();
        }
    };
    // make a gRPC call
    TestServiceGrpc.newStub(inProcessChannel).streamingInputCall(responseObserver);
    assertTrue(finishLatch.await(900, TimeUnit.MILLISECONDS));
    assertEquals(fakeResponse, responseRef.get());
    assertNull(throwableRef.get());
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) StreamingInputCallRequest(io.grpc.testing.integration.Messages.StreamingInputCallRequest) TestServiceImplBase(io.grpc.testing.integration.TestServiceGrpc.TestServiceImplBase) AtomicReference(java.util.concurrent.atomic.AtomicReference) StreamingInputCallResponse(io.grpc.testing.integration.Messages.StreamingInputCallResponse) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 3 with StreamingInputCallResponse

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

the class AbstractInteropTest method clientCompressedStreaming.

/**
 * Tests client per-message compression for streaming calls. The Java API does not support
 * inspecting a message's compression level, so this is primarily intended to run against a gRPC
 * C++ server.
 */
public void clientCompressedStreaming(boolean probe) throws Exception {
    final StreamingInputCallRequest expectCompressedRequest = StreamingInputCallRequest.newBuilder().setExpectCompressed(BoolValue.newBuilder().setValue(true)).setPayload(Payload.newBuilder().setBody(ByteString.copyFrom(new byte[27182]))).build();
    final StreamingInputCallRequest expectUncompressedRequest = StreamingInputCallRequest.newBuilder().setExpectCompressed(BoolValue.newBuilder().setValue(false)).setPayload(Payload.newBuilder().setBody(ByteString.copyFrom(new byte[45904]))).build();
    final StreamingInputCallResponse goldenResponse = StreamingInputCallResponse.newBuilder().setAggregatedPayloadSize(73086).build();
    StreamRecorder<StreamingInputCallResponse> responseObserver = StreamRecorder.create();
    StreamObserver<StreamingInputCallRequest> requestObserver = asyncStub.streamingInputCall(responseObserver);
    if (probe) {
        // Send a non-compressed message with expectCompress=true. Servers supporting this test case
        // should return INVALID_ARGUMENT.
        requestObserver.onNext(expectCompressedRequest);
        responseObserver.awaitCompletion(operationTimeoutMillis(), TimeUnit.MILLISECONDS);
        Throwable e = responseObserver.getError();
        assertNotNull("expected INVALID_ARGUMENT", e);
        assertEquals(Status.INVALID_ARGUMENT.getCode(), Status.fromThrowable(e).getCode());
    }
    // Start a new stream
    responseObserver = StreamRecorder.create();
    @SuppressWarnings("unchecked") ClientCallStreamObserver<StreamingInputCallRequest> clientCallStreamObserver = (ClientCallStreamObserver) asyncStub.withCompression("gzip").streamingInputCall(responseObserver);
    clientCallStreamObserver.setMessageCompression(true);
    clientCallStreamObserver.onNext(expectCompressedRequest);
    clientCallStreamObserver.setMessageCompression(false);
    clientCallStreamObserver.onNext(expectUncompressedRequest);
    clientCallStreamObserver.onCompleted();
    responseObserver.awaitCompletion();
    assertSuccess(responseObserver);
    assertEquals(goldenResponse, responseObserver.firstValue().get());
}
Also used : StreamingInputCallRequest(io.grpc.testing.integration.Messages.StreamingInputCallRequest) StreamingInputCallResponse(io.grpc.testing.integration.Messages.StreamingInputCallResponse) ClientCallStreamObserver(io.grpc.stub.ClientCallStreamObserver)

Example 4 with StreamingInputCallResponse

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

the class AbstractInteropTest method cancelAfterBegin.

@Test
public void cancelAfterBegin() throws Exception {
    StreamRecorder<StreamingInputCallResponse> responseObserver = StreamRecorder.create();
    StreamObserver<StreamingInputCallRequest> requestObserver = asyncStub.streamingInputCall(responseObserver);
    requestObserver.onError(new RuntimeException());
    responseObserver.awaitCompletion();
    assertEquals(Arrays.<StreamingInputCallResponse>asList(), responseObserver.getValues());
    assertEquals(Status.Code.CANCELLED, Status.fromThrowable(responseObserver.getError()).getCode());
    if (metricsExpected()) {
        MetricsRecord clientStartRecord = clientStatsRecorder.pollRecord(5, TimeUnit.SECONDS);
        checkStartTags(clientStartRecord, "grpc.testing.TestService/StreamingInputCall", true);
        // CensusStreamTracerModule record final status in the interceptor, thus is guaranteed to be
        // recorded.  The tracer stats rely on the stream being created, which is not always the case
        // in this test.  Therefore we don't check the tracer stats.
        MetricsRecord clientEndRecord = clientStatsRecorder.pollRecord(5, TimeUnit.SECONDS);
        checkEndTags(clientEndRecord, "grpc.testing.TestService/StreamingInputCall", Status.CANCELLED.getCode(), true);
    // Do not check server-side metrics, because the status on the server side is undetermined.
    }
}
Also used : StreamingInputCallRequest(io.grpc.testing.integration.Messages.StreamingInputCallRequest) StatusRuntimeException(io.grpc.StatusRuntimeException) MetricsRecord(io.grpc.internal.testing.StatsTestUtils.MetricsRecord) StreamingInputCallResponse(io.grpc.testing.integration.Messages.StreamingInputCallResponse) Test(org.junit.Test)

Example 5 with StreamingInputCallResponse

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

the class MoreInProcessTest method asyncClientStreaming_erroneousServiceImpl.

@Test
public void asyncClientStreaming_erroneousServiceImpl() throws Exception {
    // implement a service
    TestServiceImplBase clientStreamingImpl = new TestServiceImplBase() {

        @Override
        public StreamObserver<StreamingInputCallRequest> streamingInputCall(StreamObserver<StreamingInputCallResponse> responseObserver) {
            StreamObserver<StreamingInputCallRequest> requestObserver = new StreamObserver<StreamingInputCallRequest>() {

                @Override
                public void onNext(StreamingInputCallRequest value) {
                    throw new RuntimeException("unexpected error due to careless implementation of serviceImpl");
                }

                @Override
                public void onError(Throwable t) {
                }

                @Override
                public void onCompleted() {
                }
            };
            return requestObserver;
        }
    };
    serviceRegistry.addService(clientStreamingImpl);
    // implement a client
    final CountDownLatch finishLatch = new CountDownLatch(1);
    final AtomicReference<StreamingInputCallResponse> responseRef = new AtomicReference<>();
    final AtomicReference<Throwable> throwableRef = new AtomicReference<>();
    StreamObserver<StreamingInputCallResponse> responseObserver = new StreamObserver<StreamingInputCallResponse>() {

        @Override
        public void onNext(StreamingInputCallResponse response) {
            responseRef.set(response);
        }

        @Override
        public void onError(Throwable t) {
            throwableRef.set(t);
            finishLatch.countDown();
        }

        @Override
        public void onCompleted() {
            finishLatch.countDown();
        }
    };
    // make a gRPC call
    TestServiceGrpc.newStub(inProcessChannel).streamingInputCall(responseObserver).onNext(StreamingInputCallRequest.getDefaultInstance());
    assertTrue(finishLatch.await(900, TimeUnit.MILLISECONDS));
    assertEquals(Status.UNKNOWN, Status.fromThrowable(throwableRef.get()));
    assertNull(responseRef.get());
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) StreamingInputCallRequest(io.grpc.testing.integration.Messages.StreamingInputCallRequest) StatusRuntimeException(io.grpc.StatusRuntimeException) TestServiceImplBase(io.grpc.testing.integration.TestServiceGrpc.TestServiceImplBase) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) StreamingInputCallResponse(io.grpc.testing.integration.Messages.StreamingInputCallResponse) Test(org.junit.Test)

Aggregations

StreamingInputCallRequest (io.grpc.testing.integration.Messages.StreamingInputCallRequest)6 StreamingInputCallResponse (io.grpc.testing.integration.Messages.StreamingInputCallResponse)6 Test (org.junit.Test)5 StatusRuntimeException (io.grpc.StatusRuntimeException)3 StreamObserver (io.grpc.stub.StreamObserver)3 TestServiceImplBase (io.grpc.testing.integration.TestServiceGrpc.TestServiceImplBase)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 Status (io.grpc.Status)1 MetricsRecord (io.grpc.internal.testing.StatsTestUtils.MetricsRecord)1 ClientCallStreamObserver (io.grpc.stub.ClientCallStreamObserver)1