Search in sources :

Example 1 with Rectangle

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

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

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

the class RouteGuideClient method listFeatures.

/**
 * Blocking server-streaming example. Calls listFeatures with a rectangle of interest. Prints each
 * response feature as it arrives.
 */
public void listFeatures(int lowLat, int lowLon, int hiLat, int hiLon) {
    info("*** ListFeatures: lowLat={0} lowLon={1} hiLat={2} hiLon={3}", lowLat, lowLon, hiLat, hiLon);
    Rectangle request = Rectangle.newBuilder().setLo(Point.newBuilder().setLatitude(lowLat).setLongitude(lowLon).build()).setHi(Point.newBuilder().setLatitude(hiLat).setLongitude(hiLon).build()).build();
    Iterator<Feature> features;
    try {
        features = blockingStub.listFeatures(request);
        for (int i = 1; features.hasNext(); i++) {
            Feature feature = features.next();
            info("Result #" + i + ": {0}", feature);
            if (testHelper != null) {
                testHelper.onMessage(feature);
            }
        }
    } catch (StatusRuntimeException e) {
        warning("RPC failed: {0}", e.getStatus());
        if (testHelper != null) {
            testHelper.onRpcError(e);
        }
    }
}
Also used : Rectangle(com.hry.spring.grpc.stream.Rectangle) StatusRuntimeException(io.grpc.StatusRuntimeException) Feature(com.hry.spring.grpc.stream.Feature) Point(com.hry.spring.grpc.stream.Point)

Example 4 with Rectangle

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

the class RouteGuideServerTest method listFeatures.

@Test
public void listFeatures() throws Exception {
    // setup
    Rectangle rect = Rectangle.newBuilder().setLo(Point.newBuilder().setLongitude(0).setLatitude(0).build()).setHi(Point.newBuilder().setLongitude(10).setLatitude(10).build()).build();
    Feature f1 = Feature.newBuilder().setLocation(Point.newBuilder().setLongitude(-1).setLatitude(-1).build()).setName("f1").build();
    Feature f2 = Feature.newBuilder().setLocation(Point.newBuilder().setLongitude(2).setLatitude(2).build()).setName("f2").build();
    Feature f3 = Feature.newBuilder().setLocation(Point.newBuilder().setLongitude(3).setLatitude(3).build()).setName("f3").build();
    Feature f4 = Feature.newBuilder().setLocation(Point.newBuilder().setLongitude(4).setLatitude(4).build()).build();
    features.add(f1);
    features.add(f2);
    features.add(f3);
    features.add(f4);
    final Collection<Feature> result = new HashSet<Feature>();
    final CountDownLatch latch = new CountDownLatch(1);
    StreamObserver<Feature> responseObserver = new StreamObserver<Feature>() {

        @Override
        public void onNext(Feature value) {
            result.add(value);
        }

        @Override
        public void onError(Throwable t) {
            fail();
        }

        @Override
        public void onCompleted() {
            latch.countDown();
        }
    };
    RouteGuideGrpc.RouteGuideStub stub = RouteGuideGrpc.newStub(inProcessChannel);
    // run
    stub.listFeatures(rect, responseObserver);
    assertTrue(latch.await(1, TimeUnit.SECONDS));
    // verify
    assertEquals(new HashSet<Feature>(Arrays.asList(f2, f3)), result);
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) Rectangle(com.hry.spring.grpc.stream.Rectangle) CountDownLatch(java.util.concurrent.CountDownLatch) Feature(com.hry.spring.grpc.stream.Feature) HashSet(java.util.HashSet) RouteGuideGrpc(com.hry.spring.grpc.stream.RouteGuideGrpc) Test(org.junit.Test)

Aggregations

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