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());
}
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");
}
}
}
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()));
}
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()));
}
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()));
}
Aggregations