Search in sources :

Example 51 with HelloReply

use of io.grpc.examples.helloworld.HelloReply in project grpc-java by grpc.

the class HostnameGreeter method sayHello.

@Override
public void sayHello(HelloRequest req, StreamObserver<HelloReply> responseObserver) {
    HelloReply reply = HelloReply.newBuilder().setMessage("Hello " + req.getName() + ", from " + serverName).build();
    responseObserver.onNext(reply);
    responseObserver.onCompleted();
}
Also used : HelloReply(io.grpc.examples.helloworld.HelloReply)

Example 52 with HelloReply

use of io.grpc.examples.helloworld.HelloReply in project grpc-java by grpc.

the class HostnameGreeterTest method sayHello_dynamicHostname.

@Test
public void sayHello_dynamicHostname() throws Exception {
    grpcCleanup.register(InProcessServerBuilder.forName("hostname").directExecutor().addService(new HostnameGreeter(null)).build().start());
    // Just verifing the service doesn't crash
    HelloReply reply = blockingStub.sayHello(HelloRequest.newBuilder().setName("anonymous").build());
    assertTrue(reply.getMessage(), reply.getMessage().startsWith("Hello anonymous, from "));
}
Also used : HostnameGreeter(io.grpc.examples.hostname.HostnameGreeter) HelloReply(io.grpc.examples.helloworld.HelloReply) Test(org.junit.Test)

Example 53 with HelloReply

use of io.grpc.examples.helloworld.HelloReply in project grpc-java by grpc.

the class DetailErrorSample method asyncCall.

void asyncCall() {
    GreeterStub stub = GreeterGrpc.newStub(channel);
    HelloRequest request = HelloRequest.newBuilder().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) {
            verifyErrorReply(t);
            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) 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 54 with HelloReply

use of io.grpc.examples.helloworld.HelloReply in project grpc-java by grpc.

the class DetailErrorSample method advancedAsyncCall.

/**
 * This is more advanced and does not make use of the stub.  You should not normally need to do
 * this, but here is how you would.
 */
void advancedAsyncCall() {
    ClientCall<HelloRequest, HelloReply> call = channel.newCall(GreeterGrpc.getSayHelloMethod(), CallOptions.DEFAULT);
    final CountDownLatch latch = new CountDownLatch(1);
    call.start(new ClientCall.Listener<HelloReply>() {

        @Override
        public void onClose(Status status, Metadata trailers) {
            Verify.verify(status.getCode() == Status.Code.INTERNAL);
            Verify.verify(trailers.containsKey(DEBUG_INFO_TRAILER_KEY));
            try {
                Verify.verify(trailers.get(DEBUG_INFO_TRAILER_KEY).equals(DEBUG_INFO));
            } catch (IllegalArgumentException e) {
                throw new VerifyException(e);
            }
            latch.countDown();
        }
    }, new Metadata());
    call.sendMessage(HelloRequest.newBuilder().build());
    call.halfClose();
    if (!Uninterruptibles.awaitUninterruptibly(latch, 1, TimeUnit.SECONDS)) {
        throw new RuntimeException("timeout!");
    }
}
Also used : Status(io.grpc.Status) VerifyException(com.google.common.base.VerifyException) ClientCall(io.grpc.ClientCall) HelloRequest(io.grpc.examples.helloworld.HelloRequest) Metadata(io.grpc.Metadata) HelloReply(io.grpc.examples.helloworld.HelloReply) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 55 with HelloReply

use of io.grpc.examples.helloworld.HelloReply in project brave by openzipkin.

the class BaseITTracingClientInterceptor method setsErrorTag_onCanceledFuture.

@Test
public void setsErrorTag_onCanceledFuture() {
    server.enqueueDelay(TimeUnit.SECONDS.toMillis(1));
    ListenableFuture<HelloReply> resp = GreeterGrpc.newFutureStub(client).sayHello(HELLO_REQUEST);
    assumeTrue("lost race on cancel", resp.cancel(true));
    MutableSpan span = testSpanHandler.takeRemoteSpanWithErrorTag(CLIENT, "CANCELLED");
    assertThat(span.tags().get("grpc.status_code")).isEqualTo("CANCELLED");
}
Also used : MutableSpan(brave.handler.MutableSpan) HelloReply(io.grpc.examples.helloworld.HelloReply) Test(org.junit.Test)

Aggregations

HelloReply (io.grpc.examples.helloworld.HelloReply)55 Test (org.junit.Test)30 HelloRequest (io.grpc.examples.helloworld.HelloRequest)20 StatusRuntimeException (io.grpc.StatusRuntimeException)8 GreeterGrpc (io.grpc.examples.helloworld.GreeterGrpc)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 Metadata (io.grpc.Metadata)5 Status (io.grpc.Status)5 GreeterFutureStub (io.grpc.examples.helloworld.GreeterGrpc.GreeterFutureStub)4 SpanCustomizer (brave.SpanCustomizer)3 ManagedChannel (io.grpc.ManagedChannel)3 Span (zipkin2.Span)3 TraceContext (brave.propagation.TraceContext)2 ClientCall (io.grpc.ClientCall)2 Server (io.grpc.Server)2 GreeterStub (io.grpc.examples.helloworld.GreeterGrpc.GreeterStub)2 HostnameGreeter (io.grpc.examples.hostname.HostnameGreeter)2 StreamObserver (io.grpc.stub.StreamObserver)2 ExecutionException (java.util.concurrent.ExecutionException)2 CurrentSpanCustomizer (brave.CurrentSpanCustomizer)1