Search in sources :

Example 71 with StatusRuntimeException

use of io.grpc.StatusRuntimeException in project grpc-java by grpc.

the class HelloJsonClient method greet.

/** Say hello to server. */
public void greet(String name) {
    logger.info("Will try to greet " + name + " ...");
    HelloRequest request = HelloRequest.newBuilder().setName(name).build();
    HelloReply response;
    try {
        response = blockingStub.sayHello(request);
    } catch (StatusRuntimeException e) {
        logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus());
        return;
    }
    logger.info("Greeting: " + response.getMessage());
}
Also used : HelloRequest(io.grpc.examples.helloworld.HelloRequest) StatusRuntimeException(io.grpc.StatusRuntimeException) HelloReply(io.grpc.examples.helloworld.HelloReply)

Example 72 with StatusRuntimeException

use of io.grpc.StatusRuntimeException in project grpc-java by grpc.

the class CascadingTest method testCascadingCancellationViaLeafFailure.

/**
   * Test that when RPC cancellation propagates up a call chain, the cancellation of the parent
   * RPC triggers cancellation of all of its children.
   */
@Test
public void testCascadingCancellationViaLeafFailure() throws Exception {
    // All nodes (15) except one edge of the tree (4) will be cancelled.
    observedCancellations = new CountDownLatch(11);
    receivedCancellations = new CountDownLatch(11);
    startCallTreeServer(3);
    try {
        // Use response size limit to control tree nodeCount.
        blockingStub.unaryCall(Messages.SimpleRequest.newBuilder().setResponseSize(3).build());
        fail("Expected abort");
    } catch (StatusRuntimeException sre) {
        // Wait for the workers to finish
        Status status = sre.getStatus();
        // Outermost caller observes ABORTED propagating up from the failing leaf,
        // The descendant RPCs are cancelled so they receive CANCELLED.
        assertEquals(Status.Code.ABORTED, status.getCode());
        if (!observedCancellations.await(5, TimeUnit.SECONDS)) {
            fail("Expected number of cancellations not observed by clients");
        }
        if (!receivedCancellations.await(5, TimeUnit.SECONDS)) {
            fail("Expected number of cancellations to be received by servers not observed");
        }
    }
}
Also used : Status(io.grpc.Status) StatusRuntimeException(io.grpc.StatusRuntimeException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 73 with StatusRuntimeException

use of io.grpc.StatusRuntimeException in project grpc-java by grpc.

the class RouteGuideClientTest method routeChat_errorResponse.

/**
   * Example for testing bi-directional call.
   */
@Test
public void routeChat_errorResponse() throws Exception {
    final List<RouteNote> notesDelivered = new ArrayList<RouteNote>();
    final StatusRuntimeException fakeError = new StatusRuntimeException(Status.PERMISSION_DENIED);
    // implement the fake service
    RouteGuideImplBase routeChatImpl = new RouteGuideImplBase() {

        @Override
        public StreamObserver<RouteNote> routeChat(final StreamObserver<RouteNote> responseObserver) {
            StreamObserver<RouteNote> requestObserver = new StreamObserver<RouteNote>() {

                @Override
                public void onNext(RouteNote value) {
                    notesDelivered.add(value);
                    responseObserver.onError(fakeError);
                }

                @Override
                public void onError(Throwable t) {
                }

                @Override
                public void onCompleted() {
                    responseObserver.onCompleted();
                }
            };
            return requestObserver;
        }
    };
    serviceRegistry.addService(routeChatImpl);
    client.routeChat().await(1, TimeUnit.SECONDS);
    assertEquals("First message", notesDelivered.get(0).getMessage());
    verify(testHelper, never()).onMessage(any(Message.class));
    ArgumentCaptor<Throwable> errorCaptor = ArgumentCaptor.forClass(Throwable.class);
    verify(testHelper).onRpcError(errorCaptor.capture());
    assertEquals(fakeError.getStatus(), Status.fromThrowable(errorCaptor.getValue()));
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) Message(com.google.protobuf.Message) ArrayList(java.util.ArrayList) StatusRuntimeException(io.grpc.StatusRuntimeException) RouteGuideImplBase(io.grpc.examples.routeguide.RouteGuideGrpc.RouteGuideImplBase) Test(org.junit.Test)

Example 74 with StatusRuntimeException

use of io.grpc.StatusRuntimeException in project grpc-java by grpc.

the class RouteGuideClientTest method listFeatures_error.

/**
   * Example for testing blocking server-streaming.
   */
@Test
public void listFeatures_error() {
    final Feature responseFeature1 = Feature.newBuilder().setName("feature 1").build();
    final AtomicReference<Rectangle> rectangleDelivered = new AtomicReference<Rectangle>();
    final StatusRuntimeException fakeError = new StatusRuntimeException(Status.INVALID_ARGUMENT);
    // implement the fake service
    RouteGuideImplBase listFeaturesImpl = new RouteGuideImplBase() {

        @Override
        public void listFeatures(Rectangle rectangle, StreamObserver<Feature> responseObserver) {
            rectangleDelivered.set(rectangle);
            // send one response message
            responseObserver.onNext(responseFeature1);
            // let the rpc fail
            responseObserver.onError(fakeError);
        }
    };
    serviceRegistry.addService(listFeaturesImpl);
    client.listFeatures(1, 2, 3, 4);
    assertEquals(Rectangle.newBuilder().setLo(Point.newBuilder().setLatitude(1).setLongitude(2).build()).setHi(Point.newBuilder().setLatitude(3).setLongitude(4).build()).build(), rectangleDelivered.get());
    ArgumentCaptor<Throwable> errorCaptor = ArgumentCaptor.forClass(Throwable.class);
    verify(testHelper).onMessage(responseFeature1);
    verify(testHelper).onRpcError(errorCaptor.capture());
    assertEquals(fakeError.getStatus(), Status.fromThrowable(errorCaptor.getValue()));
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) StatusRuntimeException(io.grpc.StatusRuntimeException) AtomicReference(java.util.concurrent.atomic.AtomicReference) RouteGuideImplBase(io.grpc.examples.routeguide.RouteGuideGrpc.RouteGuideImplBase) Test(org.junit.Test)

Example 75 with StatusRuntimeException

use of io.grpc.StatusRuntimeException in project grpc-java by grpc.

the class RouteGuideClientTest method recordRoute_serverError.

/**
   * Example for testing async client-streaming.
   */
@Test
public void recordRoute_serverError() throws Exception {
    client.setRandom(noRandomness);
    Point point1 = Point.newBuilder().setLatitude(1).setLongitude(1).build();
    final Feature requestFeature1 = Feature.newBuilder().setLocation(point1).build();
    final List<Feature> features = Arrays.asList(requestFeature1);
    final StatusRuntimeException fakeError = new StatusRuntimeException(Status.INVALID_ARGUMENT);
    // implement the fake service
    RouteGuideImplBase recordRouteImpl = new RouteGuideImplBase() {

        @Override
        public StreamObserver<Point> recordRoute(StreamObserver<RouteSummary> responseObserver) {
            // send an error immediately
            responseObserver.onError(fakeError);
            StreamObserver<Point> requestObserver = new StreamObserver<Point>() {

                @Override
                public void onNext(Point value) {
                }

                @Override
                public void onError(Throwable t) {
                }

                @Override
                public void onCompleted() {
                }
            };
            return requestObserver;
        }
    };
    serviceRegistry.addService(recordRouteImpl);
    client.recordRoute(features, 4);
    ArgumentCaptor<Throwable> errorCaptor = ArgumentCaptor.forClass(Throwable.class);
    verify(testHelper).onRpcError(errorCaptor.capture());
    assertEquals(fakeError.getStatus(), Status.fromThrowable(errorCaptor.getValue()));
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) StatusRuntimeException(io.grpc.StatusRuntimeException) RouteGuideImplBase(io.grpc.examples.routeguide.RouteGuideGrpc.RouteGuideImplBase) Test(org.junit.Test)

Aggregations

StatusRuntimeException (io.grpc.StatusRuntimeException)131 Test (org.junit.Test)110 ApiException (com.google.api.gax.grpc.ApiException)74 ByteString (com.google.protobuf.ByteString)12 Status (io.grpc.Status)12 ArrayList (java.util.ArrayList)11 Metadata (io.grpc.Metadata)10 SubscriptionName (com.google.pubsub.v1.SubscriptionName)9 ProjectName (com.google.monitoring.v3.ProjectName)6 TopicName (com.google.pubsub.v1.TopicName)6 StreamObserver (io.grpc.stub.StreamObserver)6 ByteArrayInputStream (java.io.ByteArrayInputStream)6 Document (com.google.cloud.language.v1beta2.Document)5 ParentNameOneof (com.google.logging.v2.ParentNameOneof)5 IOException (java.io.IOException)5 ExecutionException (java.util.concurrent.ExecutionException)5 Document (com.google.cloud.language.v1.Document)4 EncodingType (com.google.cloud.language.v1beta2.EncodingType)4 HelloReply (io.grpc.examples.helloworld.HelloReply)4 HelloRequest (io.grpc.examples.helloworld.HelloRequest)4