Search in sources :

Example 61 with Metadata

use of io.grpc.Metadata in project grpc-java by grpc.

the class DetailErrorSample method run.

void run() throws Exception {
    Server server = ServerBuilder.forPort(0).addService(new GreeterGrpc.GreeterImplBase() {

        @Override
        public void sayHello(HelloRequest request, StreamObserver<HelloReply> responseObserver) {
            Metadata trailers = new Metadata();
            trailers.put(DEBUG_INFO_TRAILER_KEY, DEBUG_INFO);
            responseObserver.onError(Status.INTERNAL.withDescription(DEBUG_DESC).asRuntimeException(trailers));
        }
    }).build().start();
    channel = ManagedChannelBuilder.forAddress("localhost", server.getPort()).usePlaintext(true).build();
    blockingCall();
    futureCallDirect();
    futureCallCallback();
    asyncCall();
    advancedAsyncCall();
    channel.shutdown();
    server.shutdown();
    channel.awaitTermination(1, TimeUnit.SECONDS);
    server.awaitTermination();
}
Also used : Server(io.grpc.Server) HelloRequest(io.grpc.examples.helloworld.HelloRequest) Metadata(io.grpc.Metadata) GreeterGrpc(io.grpc.examples.helloworld.GreeterGrpc) HelloReply(io.grpc.examples.helloworld.HelloReply)

Example 62 with Metadata

use of io.grpc.Metadata in project grpc-java by grpc.

the class AbstractInteropTest method customMetadata.

@Test(timeout = 10000)
public void customMetadata() throws Exception {
    final int responseSize = 314159;
    final int requestSize = 271828;
    final SimpleRequest request = SimpleRequest.newBuilder().setResponseSize(responseSize).setResponseType(PayloadType.COMPRESSABLE).setPayload(Payload.newBuilder().setBody(ByteString.copyFrom(new byte[requestSize]))).build();
    final StreamingOutputCallRequest streamingRequest = StreamingOutputCallRequest.newBuilder().addResponseParameters(ResponseParameters.newBuilder().setSize(responseSize)).setResponseType(PayloadType.COMPRESSABLE).setPayload(Payload.newBuilder().setBody(ByteString.copyFrom(new byte[requestSize]))).build();
    final SimpleResponse goldenResponse = SimpleResponse.newBuilder().setPayload(Payload.newBuilder().setType(PayloadType.COMPRESSABLE).setBody(ByteString.copyFrom(new byte[responseSize]))).build();
    final StreamingOutputCallResponse goldenStreamingResponse = StreamingOutputCallResponse.newBuilder().setPayload(Payload.newBuilder().setType(PayloadType.COMPRESSABLE).setBody(ByteString.copyFrom(new byte[responseSize]))).build();
    final byte[] trailingBytes = { (byte) 0xa, (byte) 0xb, (byte) 0xa, (byte) 0xb, (byte) 0xa, (byte) 0xb };
    // Test UnaryCall
    Metadata metadata = new Metadata();
    metadata.put(Util.ECHO_INITIAL_METADATA_KEY, "test_initial_metadata_value");
    metadata.put(Util.ECHO_TRAILING_METADATA_KEY, trailingBytes);
    TestServiceGrpc.TestServiceBlockingStub blockingStub = TestServiceGrpc.newBlockingStub(channel);
    blockingStub = MetadataUtils.attachHeaders(blockingStub, metadata);
    AtomicReference<Metadata> headersCapture = new AtomicReference<Metadata>();
    AtomicReference<Metadata> trailersCapture = new AtomicReference<Metadata>();
    blockingStub = MetadataUtils.captureMetadata(blockingStub, headersCapture, trailersCapture);
    SimpleResponse response = blockingStub.unaryCall(request);
    assertEquals(goldenResponse, response);
    assertEquals("test_initial_metadata_value", headersCapture.get().get(Util.ECHO_INITIAL_METADATA_KEY));
    assertTrue(Arrays.equals(trailingBytes, trailersCapture.get().get(Util.ECHO_TRAILING_METADATA_KEY)));
    if (metricsExpected()) {
        assertMetrics("grpc.testing.TestService/UnaryCall", Status.Code.OK, Collections.singleton(request), Collections.singleton(goldenResponse));
    }
    // Test FullDuplexCall
    metadata = new Metadata();
    metadata.put(Util.ECHO_INITIAL_METADATA_KEY, "test_initial_metadata_value");
    metadata.put(Util.ECHO_TRAILING_METADATA_KEY, trailingBytes);
    TestServiceGrpc.TestServiceStub stub = TestServiceGrpc.newStub(channel);
    stub = MetadataUtils.attachHeaders(stub, metadata);
    headersCapture = new AtomicReference<Metadata>();
    trailersCapture = new AtomicReference<Metadata>();
    stub = MetadataUtils.captureMetadata(stub, headersCapture, trailersCapture);
    StreamRecorder<Messages.StreamingOutputCallResponse> recorder = StreamRecorder.create();
    StreamObserver<Messages.StreamingOutputCallRequest> requestStream = stub.fullDuplexCall(recorder);
    requestStream.onNext(streamingRequest);
    requestStream.onCompleted();
    recorder.awaitCompletion();
    assertSuccess(recorder);
    assertEquals(goldenStreamingResponse, recorder.firstValue().get());
    assertEquals("test_initial_metadata_value", headersCapture.get().get(Util.ECHO_INITIAL_METADATA_KEY));
    assertTrue(Arrays.equals(trailingBytes, trailersCapture.get().get(Util.ECHO_TRAILING_METADATA_KEY)));
    if (metricsExpected()) {
        assertMetrics("grpc.testing.TestService/FullDuplexCall", Status.Code.OK, Collections.singleton(streamingRequest), Collections.singleton(goldenStreamingResponse));
    }
}
Also used : Metadata(io.grpc.Metadata) AtomicReference(java.util.concurrent.atomic.AtomicReference) SimpleRequest(io.grpc.testing.integration.Messages.SimpleRequest) SimpleResponse(io.grpc.testing.integration.Messages.SimpleResponse) StreamingOutputCallRequest(io.grpc.testing.integration.Messages.StreamingOutputCallRequest) StreamingOutputCallResponse(io.grpc.testing.integration.Messages.StreamingOutputCallResponse) Test(org.junit.Test)

Example 63 with Metadata

use of io.grpc.Metadata in project grpc-java by grpc.

the class ServerCallImplTest method sendMessage.

@Test
public void sendMessage() {
    call.sendHeaders(new Metadata());
    call.sendMessage(1234L);
    verify(stream).writeMessage(isA(InputStream.class));
    verify(stream).flush();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Metadata(io.grpc.Metadata) Test(org.junit.Test)

Example 64 with Metadata

use of io.grpc.Metadata in project grpc-java by grpc.

the class ServerCallImplTest method sendMessage_failsOnClosed.

@Test
public void sendMessage_failsOnClosed() {
    call.sendHeaders(new Metadata());
    call.close(Status.CANCELLED, new Metadata());
    thrown.expect(IllegalStateException.class);
    thrown.expectMessage("call is closed");
    call.sendMessage(1234L);
}
Also used : Metadata(io.grpc.Metadata) Test(org.junit.Test)

Example 65 with Metadata

use of io.grpc.Metadata in project grpc-java by grpc.

the class ServerImplTest method exceptionInStartCallPropagatesToStream.

@Test
public void exceptionInStartCallPropagatesToStream() throws Exception {
    createAndStartServer(NO_FILTERS);
    final Status status = Status.ABORTED.withDescription("Oh, no!");
    MethodDescriptor<String, Integer> method = MethodDescriptor.<String, Integer>newBuilder().setType(MethodDescriptor.MethodType.UNKNOWN).setFullMethodName("Waiter/serve").setRequestMarshaller(STRING_MARSHALLER).setResponseMarshaller(INTEGER_MARSHALLER).build();
    mutableFallbackRegistry.addService(ServerServiceDefinition.builder(new ServiceDescriptor("Waiter", method)).addMethod(method, new ServerCallHandler<String, Integer>() {

        @Override
        public ServerCall.Listener<String> startCall(ServerCall<String, Integer> call, Metadata headers) {
            throw status.asRuntimeException();
        }
    }).build());
    ServerTransportListener transportListener = transportServer.registerNewServerTransport(new SimpleServerTransport());
    Metadata requestHeaders = new Metadata();
    StatsTraceContext statsTraceCtx = transportListener.methodDetermined("Waiter/serve", requestHeaders);
    assertNotNull(statsTraceCtx);
    when(stream.statsTraceContext()).thenReturn(statsTraceCtx);
    transportListener.streamCreated(stream, "Waiter/serve", requestHeaders);
    verify(stream).setListener(streamListenerCaptor.capture());
    ServerStreamListener streamListener = streamListenerCaptor.getValue();
    assertNotNull(streamListener);
    verify(stream, atLeast(1)).statsTraceContext();
    verifyNoMoreInteractions(stream);
    assertEquals(1, executor.runDueTasks());
    verify(stream).getAuthority();
    verify(stream).close(same(status), notNull(Metadata.class));
    verify(stream, atLeast(1)).statsTraceContext();
    verifyNoMoreInteractions(stream);
}
Also used : Status(io.grpc.Status) JumpToApplicationThreadServerStreamListener(io.grpc.internal.ServerImpl.JumpToApplicationThreadServerStreamListener) Metadata(io.grpc.Metadata) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ServiceDescriptor(io.grpc.ServiceDescriptor) JumpToApplicationThreadServerStreamListener(io.grpc.internal.ServerImpl.JumpToApplicationThreadServerStreamListener) Test(org.junit.Test)

Aggregations

Metadata (io.grpc.Metadata)283 Test (org.junit.Test)229 Status (io.grpc.Status)78 ByteArrayInputStream (java.io.ByteArrayInputStream)25 ClientStream (io.grpc.internal.ClientStream)20 InputStream (java.io.InputStream)19 Buffer (okio.Buffer)16 ClientCall (io.grpc.ClientCall)14 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)14 ServerStreamListener (io.grpc.internal.ServerStreamListener)13 AtomicReference (java.util.concurrent.atomic.AtomicReference)13 ServerCall (io.grpc.ServerCall)12 StatsContext (com.google.instrumentation.stats.StatsContext)11 CallOptions (io.grpc.CallOptions)11 IOException (java.io.IOException)11 Context (io.grpc.Context)10 StatusRuntimeException (io.grpc.StatusRuntimeException)10 Matchers.anyString (org.mockito.Matchers.anyString)10 ServerStream (io.grpc.internal.ServerStream)9 ServiceDescriptor (io.grpc.ServiceDescriptor)8