Search in sources :

Example 1 with RouteGuideImplBase

use of io.grpc.examples.routeguide.RouteGuideGrpc.RouteGuideImplBase in project grpc-java by grpc.

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) AtomicReference(java.util.concurrent.atomic.AtomicReference) RouteGuideImplBase(io.grpc.examples.routeguide.RouteGuideGrpc.RouteGuideImplBase) Test(org.junit.Test)

Example 2 with RouteGuideImplBase

use of io.grpc.examples.routeguide.RouteGuideGrpc.RouteGuideImplBase in project grpc-java by grpc.

the class RouteGuideClientTest method routeChat_echoResponse.

/**
   * Example for testing bi-directional call.
   */
@Test
public void routeChat_echoResponse() throws Exception {
    final List<RouteNote> notesDelivered = new ArrayList<RouteNote>();
    // implement the fake service
    RouteGuideImplBase routeChatImpl = new RouteGuideImplBase() {

        @Override
        public StreamObserver<RouteNote> routeChat(final StreamObserver<RouteNote> responseObserver) {
            StreamObserver<RouteNote> requestObserver = new StreamObserver<RouteNote>() {

                @Override
                public void onNext(RouteNote value) {
                    notesDelivered.add(value);
                    responseObserver.onNext(value);
                }

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

                @Override
                public void onCompleted() {
                    responseObserver.onCompleted();
                }
            };
            return requestObserver;
        }
    };
    serviceRegistry.addService(routeChatImpl);
    client.routeChat().await(1, TimeUnit.SECONDS);
    String[] messages = { "First message", "Second message", "Third message", "Fourth message" };
    for (int i = 0; i < 4; i++) {
        verify(testHelper).onMessage(notesDelivered.get(i));
        assertEquals(messages[i], notesDelivered.get(i).getMessage());
    }
    verify(testHelper, never()).onRpcError(any(Throwable.class));
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) ArrayList(java.util.ArrayList) RouteGuideImplBase(io.grpc.examples.routeguide.RouteGuideGrpc.RouteGuideImplBase) Test(org.junit.Test)

Example 3 with RouteGuideImplBase

use of io.grpc.examples.routeguide.RouteGuideGrpc.RouteGuideImplBase in project grpc-java by grpc.

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) RouteGuideImplBase(io.grpc.examples.routeguide.RouteGuideGrpc.RouteGuideImplBase) Test(org.junit.Test)

Example 4 with RouteGuideImplBase

use of io.grpc.examples.routeguide.RouteGuideGrpc.RouteGuideImplBase in project grpc-java by grpc.

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) RouteGuideImplBase(io.grpc.examples.routeguide.RouteGuideGrpc.RouteGuideImplBase) Test(org.junit.Test)

Example 5 with RouteGuideImplBase

use of io.grpc.examples.routeguide.RouteGuideGrpc.RouteGuideImplBase in project grpc-java by grpc.

the class RouteGuideClientTest method routeChat_errorResponse.

/**
   * Example for testing bi-directional call.
   */
@Test
public void routeChat_errorResponse() throws Exception {
    final List<RouteNote> notesDelivered = new ArrayList<RouteNote>();
    final StatusRuntimeException fakeError = new StatusRuntimeException(Status.PERMISSION_DENIED);
    // implement the fake service
    RouteGuideImplBase routeChatImpl = new RouteGuideImplBase() {

        @Override
        public StreamObserver<RouteNote> routeChat(final StreamObserver<RouteNote> responseObserver) {
            StreamObserver<RouteNote> requestObserver = new StreamObserver<RouteNote>() {

                @Override
                public void onNext(RouteNote value) {
                    notesDelivered.add(value);
                    responseObserver.onError(fakeError);
                }

                @Override
                public void onError(Throwable t) {
                }

                @Override
                public void onCompleted() {
                    responseObserver.onCompleted();
                }
            };
            return requestObserver;
        }
    };
    serviceRegistry.addService(routeChatImpl);
    client.routeChat().await(1, TimeUnit.SECONDS);
    assertEquals("First message", notesDelivered.get(0).getMessage());
    verify(testHelper, never()).onMessage(any(Message.class));
    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) Message(com.google.protobuf.Message) ArrayList(java.util.ArrayList) StatusRuntimeException(io.grpc.StatusRuntimeException) RouteGuideImplBase(io.grpc.examples.routeguide.RouteGuideGrpc.RouteGuideImplBase) Test(org.junit.Test)

Aggregations

RouteGuideImplBase (io.grpc.examples.routeguide.RouteGuideGrpc.RouteGuideImplBase)10 StreamObserver (io.grpc.stub.StreamObserver)10 Test (org.junit.Test)10 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 StatusRuntimeException (io.grpc.StatusRuntimeException)4 ArrayList (java.util.ArrayList)4 Message (com.google.protobuf.Message)1 CountDownLatch (java.util.concurrent.CountDownLatch)1