Search in sources :

Example 1 with TestServiceImplBase

use of io.grpc.testing.integration.TestServiceGrpc.TestServiceImplBase 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 TestServiceImplBase

use of io.grpc.testing.integration.TestServiceGrpc.TestServiceImplBase 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 TestServiceImplBase

use of io.grpc.testing.integration.TestServiceGrpc.TestServiceImplBase 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

StreamObserver (io.grpc.stub.StreamObserver)3 StreamingInputCallRequest (io.grpc.testing.integration.Messages.StreamingInputCallRequest)3 StreamingInputCallResponse (io.grpc.testing.integration.Messages.StreamingInputCallResponse)3 TestServiceImplBase (io.grpc.testing.integration.TestServiceGrpc.TestServiceImplBase)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 Test (org.junit.Test)3 StatusRuntimeException (io.grpc.StatusRuntimeException)2 Status (io.grpc.Status)1