Search in sources :

Example 6 with Point

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

the class RouteGuideServerTest method getFeature.

@Test
public void getFeature() {
    Point point = Point.newBuilder().setLongitude(1).setLatitude(1).build();
    Feature unnamedFeature = Feature.newBuilder().setName("").setLocation(point).build();
    RouteGuideGrpc.RouteGuideBlockingStub stub = RouteGuideGrpc.newBlockingStub(inProcessChannel);
    // feature not found in the server
    Feature feature = stub.getFeature(point);
    assertEquals(unnamedFeature, feature);
    // feature found in the server
    Feature namedFeature = Feature.newBuilder().setName("name").setLocation(point).build();
    features.add(namedFeature);
    feature = stub.getFeature(point);
    assertEquals(namedFeature, feature);
}
Also used : 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 7 with Point

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

the class RouteGuideClientTest method getFeature.

/**
 * Example for testing blocking unary call.
 */
@Test
public void getFeature() {
    Point requestPoint = Point.newBuilder().setLatitude(-1).setLongitude(-1).build();
    Point responsePoint = Point.newBuilder().setLatitude(-123).setLongitude(-123).build();
    final AtomicReference<Point> pointDelivered = new AtomicReference<Point>();
    final Feature responseFeature = Feature.newBuilder().setName("dummyFeature").setLocation(responsePoint).build();
    // implement the fake service
    RouteGuideImplBase getFeatureImpl = new RouteGuideImplBase() {

        @Override
        public void getFeature(Point point, StreamObserver<Feature> responseObserver) {
            pointDelivered.set(point);
            responseObserver.onNext(responseFeature);
            responseObserver.onCompleted();
        }
    };
    serviceRegistry.addService(getFeatureImpl);
    client.getFeature(-1, -1);
    assertEquals(requestPoint, pointDelivered.get());
    verify(testHelper).onMessage(responseFeature);
    verify(testHelper, never()).onRpcError(any(Throwable.class));
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) AtomicReference(java.util.concurrent.atomic.AtomicReference) 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 8 with Point

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

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()));
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) StatusRuntimeException(io.grpc.StatusRuntimeException) 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 9 with Point

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

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

Example 10 with Point

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

the class RouteGuideClient method getFeature.

/**
 * Blocking unary call example.  Calls getFeature and prints the response.
 */
public void getFeature(int lat, int lon) {
    info("*** GetFeature: lat={0} lon={1}", lat, lon);
    Point request = Point.newBuilder().setLatitude(lat).setLongitude(lon).build();
    Feature feature;
    try {
        feature = blockingStub.getFeature(request);
        if (testHelper != null) {
            testHelper.onMessage(feature);
        }
    } catch (StatusRuntimeException e) {
        warning("RPC failed: {0}", e.getStatus());
        if (testHelper != null) {
            testHelper.onRpcError(e);
        }
        return;
    }
    if (RouteGuideUtil.exists(feature)) {
        info("Found feature called \"{0}\" at {1}, {2}", feature.getName(), RouteGuideUtil.getLatitude(feature.getLocation()), RouteGuideUtil.getLongitude(feature.getLocation()));
    } else {
        info("Found no feature at {0}, {1}", RouteGuideUtil.getLatitude(feature.getLocation()), RouteGuideUtil.getLongitude(feature.getLocation()));
    }
}
Also used : StatusRuntimeException(io.grpc.StatusRuntimeException) Point(com.hry.spring.grpc.stream.Point) Feature(com.hry.spring.grpc.stream.Feature)

Aggregations

Point (com.hry.spring.grpc.stream.Point)11 StreamObserver (io.grpc.stub.StreamObserver)9 Test (org.junit.Test)9 Feature (com.hry.spring.grpc.stream.Feature)7 RouteGuideImplBase (com.hry.spring.grpc.stream.RouteGuideGrpc.RouteGuideImplBase)6 RouteSummary (com.hry.spring.grpc.stream.RouteSummary)4 StatusRuntimeException (io.grpc.StatusRuntimeException)4 RouteGuideGrpc (com.hry.spring.grpc.stream.RouteGuideGrpc)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 RouteNote (com.hry.spring.grpc.stream.RouteNote)2 ArrayList (java.util.ArrayList)2 CountDownLatch (java.util.concurrent.CountDownLatch)2