use of com.hry.spring.grpc.stream.RouteGuideGrpc.RouteGuideImplBase in project spring_boot by hryou0922.
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 com.hry.spring.grpc.stream.RouteGuideGrpc.RouteGuideImplBase in project spring_boot by hryou0922.
the class RouteGuideClientTest method listFeatures.
/**
* Example for testing blocking server-streaming.
*/
@Test
public void listFeatures() {
final Feature responseFeature1 = Feature.newBuilder().setName("feature 1").build();
final Feature responseFeature2 = Feature.newBuilder().setName("feature 2").build();
final AtomicReference<Rectangle> rectangleDelivered = new AtomicReference<Rectangle>();
// implement the fake service
RouteGuideImplBase listFeaturesImpl = new RouteGuideImplBase() {
@Override
public void listFeatures(Rectangle rectangle, StreamObserver<Feature> responseObserver) {
rectangleDelivered.set(rectangle);
// send two response messages
responseObserver.onNext(responseFeature1);
responseObserver.onNext(responseFeature2);
// complete the response
responseObserver.onCompleted();
}
};
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());
verify(testHelper).onMessage(responseFeature1);
verify(testHelper).onMessage(responseFeature2);
verify(testHelper, never()).onRpcError(any(Throwable.class));
}
use of com.hry.spring.grpc.stream.RouteGuideGrpc.RouteGuideImplBase in project spring_boot by hryou0922.
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 com.hry.spring.grpc.stream.RouteGuideGrpc.RouteGuideImplBase in project spring_boot by hryou0922.
the class RouteGuideClientTest method getFeature_error.
/**
* Example for testing blocking unary call.
*/
@Test
public void getFeature_error() {
Point requestPoint = Point.newBuilder().setLatitude(-1).setLongitude(-1).build();
final AtomicReference<Point> pointDelivered = new AtomicReference<Point>();
final StatusRuntimeException fakeError = new StatusRuntimeException(Status.DATA_LOSS);
// implement the fake service
RouteGuideImplBase getFeatureImpl = new RouteGuideImplBase() {
@Override
public void getFeature(Point point, StreamObserver<Feature> responseObserver) {
pointDelivered.set(point);
responseObserver.onError(fakeError);
}
};
serviceRegistry.addService(getFeatureImpl);
client.getFeature(-1, -1);
assertEquals(requestPoint, pointDelivered.get());
ArgumentCaptor<Throwable> errorCaptor = ArgumentCaptor.forClass(Throwable.class);
verify(testHelper).onRpcError(errorCaptor.capture());
assertEquals(fakeError.getStatus(), Status.fromThrowable(errorCaptor.getValue()));
}
use of com.hry.spring.grpc.stream.RouteGuideGrpc.RouteGuideImplBase in project spring_boot by hryou0922.
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());
}
Aggregations