Search in sources :

Example 26 with StreamObserver

use of io.grpc.stub.StreamObserver in project grpc-java by grpc.

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

Example 27 with StreamObserver

use of io.grpc.stub.StreamObserver in project grpc-java by grpc.

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) CountDownLatch(java.util.concurrent.CountDownLatch) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 28 with StreamObserver

use of io.grpc.stub.StreamObserver in project grpc-java by grpc.

the class MoreInProcessTest method asyncClientStreaming_erroneousServiceImpl.

@Test
public void asyncClientStreaming_erroneousServiceImpl() throws Exception {
    // implement a service
    TestServiceImplBase clientStreamingImpl = new TestServiceImplBase() {

        @Override
        public StreamObserver<StreamingInputCallRequest> streamingInputCall(StreamObserver<StreamingInputCallResponse> responseObserver) {
            StreamObserver<StreamingInputCallRequest> requestObserver = new StreamObserver<StreamingInputCallRequest>() {

                @Override
                public void onNext(StreamingInputCallRequest value) {
                    throw new RuntimeException("unexpected error due to careless implementation of serviceImpl");
                }

                @Override
                public void onError(Throwable t) {
                }

                @Override
                public void onCompleted() {
                }
            };
            return requestObserver;
        }
    };
    serviceRegistry.addService(clientStreamingImpl);
    // implement a client
    final CountDownLatch finishLatch = new CountDownLatch(1);
    final AtomicReference<StreamingInputCallResponse> responseRef = new AtomicReference<StreamingInputCallResponse>();
    final AtomicReference<Throwable> throwableRef = new AtomicReference<Throwable>();
    StreamObserver<StreamingInputCallResponse> responseObserver = new StreamObserver<StreamingInputCallResponse>() {

        @Override
        public void onNext(StreamingInputCallResponse response) {
            responseRef.set(response);
        }

        @Override
        public void onError(Throwable t) {
            throwableRef.set(t);
            finishLatch.countDown();
        }

        @Override
        public void onCompleted() {
            finishLatch.countDown();
        }
    };
    // make a gRPC call
    TestServiceGrpc.newStub(inProcessChannel).streamingInputCall(responseObserver).onNext(StreamingInputCallRequest.getDefaultInstance());
    assertTrue(finishLatch.await(900, TimeUnit.MILLISECONDS));
    assertEquals(Status.UNKNOWN, Status.fromThrowable(throwableRef.get()));
    assertNull(responseRef.get());
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) StreamingInputCallRequest(io.grpc.testing.integration.Messages.StreamingInputCallRequest) StatusRuntimeException(io.grpc.StatusRuntimeException) TestServiceImplBase(io.grpc.testing.integration.TestServiceGrpc.TestServiceImplBase) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) StreamingInputCallResponse(io.grpc.testing.integration.Messages.StreamingInputCallResponse) Test(org.junit.Test)

Example 29 with StreamObserver

use of io.grpc.stub.StreamObserver in project grpc-java by grpc.

the class ErrorHandlingClient method asyncCall.

void asyncCall() {
    GreeterStub stub = GreeterGrpc.newStub(channel);
    HelloRequest request = HelloRequest.newBuilder().setName("Homer").build();
    final CountDownLatch latch = new CountDownLatch(1);
    StreamObserver<HelloReply> responseObserver = new StreamObserver<HelloReply>() {

        @Override
        public void onNext(HelloReply value) {
        // Won't be called.
        }

        @Override
        public void onError(Throwable t) {
            Status status = Status.fromThrowable(t);
            Verify.verify(status.getCode() == Status.Code.INTERNAL);
            Verify.verify(status.getDescription().contains("Overbite"));
            // Cause is not transmitted over the wire..
            latch.countDown();
        }

        @Override
        public void onCompleted() {
        // Won't be called, since the server in this example always fails.
        }
    };
    stub.sayHello(request, responseObserver);
    if (!Uninterruptibles.awaitUninterruptibly(latch, 1, TimeUnit.SECONDS)) {
        throw new RuntimeException("timeout!");
    }
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) Status(io.grpc.Status) GreeterStub(io.grpc.examples.helloworld.GreeterGrpc.GreeterStub) HelloRequest(io.grpc.examples.helloworld.HelloRequest) CountDownLatch(java.util.concurrent.CountDownLatch) HelloReply(io.grpc.examples.helloworld.HelloReply)

Example 30 with StreamObserver

use of io.grpc.stub.StreamObserver in project grpc-java by grpc.

the class AbstractBenchmark method startStreamingCalls.

/**
   * Start a continuously executing set of duplex streaming ping-pong calls that will terminate when
   * {@code done.get()} is true. Each completed call will increment the counter by the specified
   * delta which benchmarks can use to measure messages per second or bandwidth.
   */
protected CountDownLatch startStreamingCalls(int callsPerChannel, final AtomicLong counter, final AtomicBoolean record, final AtomicBoolean done, final long counterDelta) {
    final CountDownLatch latch = new CountDownLatch(callsPerChannel * channels.length);
    for (final ManagedChannel channel : channels) {
        for (int i = 0; i < callsPerChannel; i++) {
            final ClientCall<ByteBuf, ByteBuf> streamingCall = channel.newCall(pingPongMethod, CALL_OPTIONS);
            final AtomicReference<StreamObserver<ByteBuf>> requestObserverRef = new AtomicReference<StreamObserver<ByteBuf>>();
            final AtomicBoolean ignoreMessages = new AtomicBoolean();
            StreamObserver<ByteBuf> requestObserver = ClientCalls.asyncBidiStreamingCall(streamingCall, new StreamObserver<ByteBuf>() {

                @Override
                public void onNext(ByteBuf value) {
                    if (done.get()) {
                        if (!ignoreMessages.getAndSet(true)) {
                            requestObserverRef.get().onCompleted();
                        }
                        return;
                    }
                    requestObserverRef.get().onNext(request.slice());
                    if (record.get()) {
                        counter.addAndGet(counterDelta);
                    }
                // request is called automatically because the observer implicitly has auto
                // inbound flow control
                }

                @Override
                public void onError(Throwable t) {
                    logger.log(Level.WARNING, "call error", t);
                    latch.countDown();
                }

                @Override
                public void onCompleted() {
                    latch.countDown();
                }
            });
            requestObserverRef.set(requestObserver);
            requestObserver.onNext(request.slice());
            requestObserver.onNext(request.slice());
        }
    }
    return latch;
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ManagedChannel(io.grpc.ManagedChannel) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuf(io.netty.buffer.ByteBuf)

Aggregations

StreamObserver (io.grpc.stub.StreamObserver)34 Test (org.junit.Test)24 CountDownLatch (java.util.concurrent.CountDownLatch)14 AtomicReference (java.util.concurrent.atomic.AtomicReference)13 RouteGuideImplBase (io.grpc.examples.routeguide.RouteGuideGrpc.RouteGuideImplBase)10 StatusRuntimeException (io.grpc.StatusRuntimeException)9 ManagedChannel (io.grpc.ManagedChannel)8 Server (io.grpc.Server)8 ArrayList (java.util.ArrayList)8 BeamFnApi (org.apache.beam.fn.v1.BeamFnApi)7 CallStreamObserver (io.grpc.stub.CallStreamObserver)5 Status (io.grpc.Status)4 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 ByteString (com.google.protobuf.ByteString)3 HelloRequest (io.grpc.examples.helloworld.HelloRequest)3 ServerCallStreamObserver (io.grpc.stub.ServerCallStreamObserver)3 SimpleRequest (io.grpc.testing.integration.Messages.SimpleRequest)3 StreamingInputCallRequest (io.grpc.testing.integration.Messages.StreamingInputCallRequest)3