use of io.grpc.examples.routeguide.RouteGuideGrpc.RouteGuideImplBase in project grpc-java by grpc.
the class RouteGuideClientTest method routeChat_simpleResponse.
/**
* Example for testing bi-directional call.
*/
@Test
public void routeChat_simpleResponse() throws Exception {
RouteNote fakeResponse1 = RouteNote.newBuilder().setMessage("dummy msg1").build();
RouteNote fakeResponse2 = RouteNote.newBuilder().setMessage("dummy msg2").build();
final List<String> messagesDelivered = new ArrayList<String>();
final List<Point> locationsDelivered = new ArrayList<Point>();
final AtomicReference<StreamObserver<RouteNote>> responseObserverRef = new AtomicReference<StreamObserver<RouteNote>>();
final CountDownLatch allRequestsDelivered = new CountDownLatch(1);
// implement the fake service
RouteGuideImplBase routeChatImpl = new RouteGuideImplBase() {
@Override
public StreamObserver<RouteNote> routeChat(StreamObserver<RouteNote> responseObserver) {
responseObserverRef.set(responseObserver);
StreamObserver<RouteNote> requestObserver = new StreamObserver<RouteNote>() {
@Override
public void onNext(RouteNote value) {
messagesDelivered.add(value.getMessage());
locationsDelivered.add(value.getLocation());
}
@Override
public void onError(Throwable t) {
}
@Override
public void onCompleted() {
allRequestsDelivered.countDown();
}
};
return requestObserver;
}
};
serviceRegistry.addService(routeChatImpl);
// start routeChat
CountDownLatch latch = client.routeChat();
// request message sent and delivered for four times
assertTrue(allRequestsDelivered.await(1, TimeUnit.SECONDS));
assertEquals(Arrays.asList("First message", "Second message", "Third message", "Fourth message"), messagesDelivered);
assertEquals(Arrays.asList(Point.newBuilder().setLatitude(0).setLongitude(0).build(), Point.newBuilder().setLatitude(0).setLongitude(1).build(), Point.newBuilder().setLatitude(1).setLongitude(0).build(), Point.newBuilder().setLatitude(1).setLongitude(1).build()), locationsDelivered);
// Let the server send out two simple response messages
// and verify that the client receives them.
// Allow some timeout for verify() if not using directExecutor
responseObserverRef.get().onNext(fakeResponse1);
verify(testHelper).onMessage(fakeResponse1);
responseObserverRef.get().onNext(fakeResponse2);
verify(testHelper).onMessage(fakeResponse2);
// let server complete.
responseObserverRef.get().onCompleted();
assertTrue(latch.await(1, TimeUnit.SECONDS));
verify(testHelper, never()).onRpcError(any(Throwable.class));
}
use of io.grpc.examples.routeguide.RouteGuideGrpc.RouteGuideImplBase 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.examples.routeguide.RouteGuideGrpc.RouteGuideImplBase in project grpc-java by grpc.
the class RouteGuideClientTest method recordRoute_wrongResponse.
/**
* Example for testing async client-streaming.
*/
@Test
public void recordRoute_wrongResponse() 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);
// implement the fake service
RouteGuideImplBase recordRouteImpl = new RouteGuideImplBase() {
@Override
public StreamObserver<Point> recordRoute(StreamObserver<RouteSummary> responseObserver) {
RouteSummary response = RouteSummary.getDefaultInstance();
// sending more than one responses is not right for client-streaming call.
responseObserver.onNext(response);
responseObserver.onNext(response);
responseObserver.onCompleted();
return new StreamObserver<Point>() {
@Override
public void onNext(Point value) {
}
@Override
public void onError(Throwable t) {
}
@Override
public void onCompleted() {
}
};
}
};
serviceRegistry.addService(recordRouteImpl);
client.recordRoute(features, 4);
ArgumentCaptor<Throwable> errorCaptor = ArgumentCaptor.forClass(Throwable.class);
verify(testHelper).onRpcError(errorCaptor.capture());
assertEquals(Status.Code.CANCELLED, Status.fromThrowable(errorCaptor.getValue()).getCode());
}
use of io.grpc.examples.routeguide.RouteGuideGrpc.RouteGuideImplBase in project grpc-java by grpc.
the class RouteGuideClientTest method recordRoute.
/**
* Example for testing async client-streaming.
*/
@Test
public void recordRoute() throws Exception {
client.setRandom(noRandomness);
Point point1 = Point.newBuilder().setLatitude(1).setLongitude(1).build();
Point point2 = Point.newBuilder().setLatitude(2).setLongitude(2).build();
Point point3 = Point.newBuilder().setLatitude(3).setLongitude(3).build();
Feature requestFeature1 = Feature.newBuilder().setLocation(point1).build();
Feature requestFeature2 = Feature.newBuilder().setLocation(point2).build();
Feature requestFeature3 = Feature.newBuilder().setLocation(point3).build();
final List<Feature> features = Arrays.asList(requestFeature1, requestFeature2, requestFeature3);
final List<Point> pointsDelivered = new ArrayList<Point>();
final RouteSummary fakeResponse = RouteSummary.newBuilder().setPointCount(7).setFeatureCount(8).setDistance(9).setElapsedTime(10).build();
// implement the fake service
RouteGuideImplBase recordRouteImpl = new RouteGuideImplBase() {
@Override
public StreamObserver<Point> recordRoute(final StreamObserver<RouteSummary> responseObserver) {
StreamObserver<Point> requestObserver = new StreamObserver<Point>() {
@Override
public void onNext(Point value) {
pointsDelivered.add(value);
}
@Override
public void onError(Throwable t) {
}
@Override
public void onCompleted() {
responseObserver.onNext(fakeResponse);
responseObserver.onCompleted();
}
};
return requestObserver;
}
};
serviceRegistry.addService(recordRouteImpl);
// send requestFeature1, requestFeature2, requestFeature3, and then requestFeature1 again
client.recordRoute(features, 4);
assertEquals(Arrays.asList(requestFeature1.getLocation(), requestFeature2.getLocation(), requestFeature3.getLocation(), requestFeature1.getLocation()), pointsDelivered);
verify(testHelper).onMessage(fakeResponse);
verify(testHelper, never()).onRpcError(any(Throwable.class));
}
use of io.grpc.examples.routeguide.RouteGuideGrpc.RouteGuideImplBase 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