Search in sources :

Example 1 with RouteGuideImplBase

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));
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) RouteSummary(com.hry.spring.grpc.stream.RouteSummary) ArrayList(java.util.ArrayList) Point(com.hry.spring.grpc.stream.Point) Feature(com.hry.spring.grpc.stream.Feature) RouteGuideImplBase(com.hry.spring.grpc.stream.RouteGuideGrpc.RouteGuideImplBase) Test(org.junit.Test)

Example 2 with RouteGuideImplBase

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));
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) Rectangle(com.hry.spring.grpc.stream.Rectangle) AtomicReference(java.util.concurrent.atomic.AtomicReference) Feature(com.hry.spring.grpc.stream.Feature) RouteGuideImplBase(com.hry.spring.grpc.stream.RouteGuideGrpc.RouteGuideImplBase) Test(org.junit.Test)

Example 3 with RouteGuideImplBase

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()));
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) Rectangle(com.hry.spring.grpc.stream.Rectangle) StatusRuntimeException(io.grpc.StatusRuntimeException) AtomicReference(java.util.concurrent.atomic.AtomicReference) Feature(com.hry.spring.grpc.stream.Feature) RouteGuideImplBase(com.hry.spring.grpc.stream.RouteGuideGrpc.RouteGuideImplBase) Test(org.junit.Test)

Example 4 with RouteGuideImplBase

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()));
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) StatusRuntimeException(io.grpc.StatusRuntimeException) AtomicReference(java.util.concurrent.atomic.AtomicReference) Point(com.hry.spring.grpc.stream.Point) RouteGuideImplBase(com.hry.spring.grpc.stream.RouteGuideGrpc.RouteGuideImplBase) Test(org.junit.Test)

Example 5 with RouteGuideImplBase

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());
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) RouteSummary(com.hry.spring.grpc.stream.RouteSummary) Point(com.hry.spring.grpc.stream.Point) Feature(com.hry.spring.grpc.stream.Feature) RouteGuideImplBase(com.hry.spring.grpc.stream.RouteGuideGrpc.RouteGuideImplBase) Test(org.junit.Test)

Aggregations

RouteGuideImplBase (com.hry.spring.grpc.stream.RouteGuideGrpc.RouteGuideImplBase)10 StreamObserver (io.grpc.stub.StreamObserver)10 Test (org.junit.Test)10 Point (com.hry.spring.grpc.stream.Point)7 Feature (com.hry.spring.grpc.stream.Feature)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 StatusRuntimeException (io.grpc.StatusRuntimeException)4 ArrayList (java.util.ArrayList)4 RouteNote (com.hry.spring.grpc.stream.RouteNote)3 Rectangle (com.hry.spring.grpc.stream.Rectangle)2 RouteSummary (com.hry.spring.grpc.stream.RouteSummary)2 Message (com.google.protobuf.Message)1 CountDownLatch (java.util.concurrent.CountDownLatch)1