Search in sources :

Example 1 with RouteSummary

use of com.hry.spring.grpc.stream.RouteSummary 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 RouteSummary

use of com.hry.spring.grpc.stream.RouteSummary 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)

Example 3 with RouteSummary

use of com.hry.spring.grpc.stream.RouteSummary in project spring_boot by hryou0922.

the class RouteGuideServerTest method recordRoute.

@Test
public void recordRoute() {
    Point p1 = Point.newBuilder().setLongitude(1000).setLatitude(1000).build();
    Point p2 = Point.newBuilder().setLongitude(2000).setLatitude(2000).build();
    Point p3 = Point.newBuilder().setLongitude(3000).setLatitude(3000).build();
    Point p4 = Point.newBuilder().setLongitude(4000).setLatitude(4000).build();
    // unamed
    Feature f1 = Feature.newBuilder().setLocation(p1).build();
    Feature f2 = Feature.newBuilder().setLocation(p2).setName("f2").build();
    Feature f3 = Feature.newBuilder().setLocation(p3).setName("f3").build();
    // unamed
    Feature f4 = Feature.newBuilder().setLocation(p4).build();
    features.add(f1);
    features.add(f2);
    features.add(f3);
    features.add(f4);
    @SuppressWarnings("unchecked") StreamObserver<RouteSummary> responseObserver = (StreamObserver<RouteSummary>) mock(StreamObserver.class);
    RouteGuideGrpc.RouteGuideStub stub = RouteGuideGrpc.newStub(inProcessChannel);
    ArgumentCaptor<RouteSummary> routeSummaryCaptor = ArgumentCaptor.forClass(RouteSummary.class);
    StreamObserver<Point> requestObserver = stub.recordRoute(responseObserver);
    requestObserver.onNext(p1);
    requestObserver.onNext(p2);
    requestObserver.onNext(p3);
    requestObserver.onNext(p4);
    verify(responseObserver, never()).onNext(any(RouteSummary.class));
    requestObserver.onCompleted();
    // allow some ms to let client receive the response. Similar usage later on.
    verify(responseObserver, timeout(100)).onNext(routeSummaryCaptor.capture());
    RouteSummary summary = routeSummaryCaptor.getValue();
    // 45 is the hard coded distance from p1 to p4.
    assertEquals(45, summary.getDistance());
    assertEquals(2, summary.getFeatureCount());
    verify(responseObserver, timeout(100)).onCompleted();
    verify(responseObserver, never()).onError(any(Throwable.class));
}
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) RouteGuideGrpc(com.hry.spring.grpc.stream.RouteGuideGrpc) Test(org.junit.Test)

Example 4 with RouteSummary

use of com.hry.spring.grpc.stream.RouteSummary in project spring_boot by hryou0922.

the class RouteGuideClient method recordRoute.

/**
 * Async client-streaming example. Sends {@code numPoints} randomly chosen points from {@code
 * features} with a variable delay in between. Prints the statistics when they are sent from the
 * server.
 */
public void recordRoute(List<Feature> features, int numPoints) throws InterruptedException {
    info("*** RecordRoute");
    final CountDownLatch finishLatch = new CountDownLatch(1);
    StreamObserver<RouteSummary> responseObserver = new StreamObserver<RouteSummary>() {

        @Override
        public void onNext(RouteSummary summary) {
            info("Finished trip with {0} points. Passed {1} features. " + "Travelled {2} meters. It took {3} seconds.", summary.getPointCount(), summary.getFeatureCount(), summary.getDistance(), summary.getElapsedTime());
            if (testHelper != null) {
                testHelper.onMessage(summary);
            }
        }

        @Override
        public void onError(Throwable t) {
            warning("RecordRoute Failed: {0}", Status.fromThrowable(t));
            if (testHelper != null) {
                testHelper.onRpcError(t);
            }
            finishLatch.countDown();
        }

        @Override
        public void onCompleted() {
            info("Finished RecordRoute");
            finishLatch.countDown();
        }
    };
    StreamObserver<Point> requestObserver = asyncStub.recordRoute(responseObserver);
    try {
        // Send numPoints points randomly selected from the features list.
        for (int i = 0; i < numPoints; ++i) {
            int index = random.nextInt(features.size());
            Point point = features.get(index).getLocation();
            info("Visiting point {0}, {1}", RouteGuideUtil.getLatitude(point), RouteGuideUtil.getLongitude(point));
            requestObserver.onNext(point);
            // Sleep for a bit before sending the next one.
            Thread.sleep(random.nextInt(1000) + 500);
            if (finishLatch.getCount() == 0) {
                // Sending further requests won't error, but they will just be thrown away.
                return;
            }
        }
    } catch (RuntimeException e) {
        // Cancel RPC
        requestObserver.onError(e);
        throw e;
    }
    // Mark the end of requests
    requestObserver.onCompleted();
    // Receiving happens asynchronously
    if (!finishLatch.await(1, TimeUnit.MINUTES)) {
        warning("recordRoute can not finish within 1 minutes");
    }
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) StatusRuntimeException(io.grpc.StatusRuntimeException) RouteSummary(com.hry.spring.grpc.stream.RouteSummary) Point(com.hry.spring.grpc.stream.Point) CountDownLatch(java.util.concurrent.CountDownLatch) Point(com.hry.spring.grpc.stream.Point)

Aggregations

Point (com.hry.spring.grpc.stream.Point)4 RouteSummary (com.hry.spring.grpc.stream.RouteSummary)4 StreamObserver (io.grpc.stub.StreamObserver)4 Feature (com.hry.spring.grpc.stream.Feature)3 Test (org.junit.Test)3 RouteGuideImplBase (com.hry.spring.grpc.stream.RouteGuideGrpc.RouteGuideImplBase)2 RouteGuideGrpc (com.hry.spring.grpc.stream.RouteGuideGrpc)1 StatusRuntimeException (io.grpc.StatusRuntimeException)1 ArrayList (java.util.ArrayList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1