Java Code Examples for io.grpc.stub.MetadataUtils#captureMetadata()

The following examples show how to use io.grpc.stub.MetadataUtils#captureMetadata() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractInteropTest.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
@Test
public void exchangeMetadataUnaryCall() throws Exception {
  TestServiceGrpc.TestServiceBlockingStub stub = blockingStub;

  // Capture the metadata exchange
  Metadata fixedHeaders = new Metadata();
  // Send a context proto (as it's in the default extension registry)
  Messages.SimpleContext contextValue =
      Messages.SimpleContext.newBuilder().setValue("dog").build();
  fixedHeaders.put(Util.METADATA_KEY, contextValue);
  stub = MetadataUtils.attachHeaders(stub, fixedHeaders);
  // .. and expect it to be echoed back in trailers
  AtomicReference<Metadata> trailersCapture = new AtomicReference<>();
  AtomicReference<Metadata> headersCapture = new AtomicReference<>();
  stub = MetadataUtils.captureMetadata(stub, headersCapture, trailersCapture);

  assertNotNull(stub.emptyCall(EMPTY));

  // Assert that our side channel object is echoed back in both headers and trailers
  Assert.assertEquals(contextValue, headersCapture.get().get(Util.METADATA_KEY));
  Assert.assertEquals(contextValue, trailersCapture.get().get(Util.METADATA_KEY));
}
 
Example 2
Source File: AbstractInteropTest.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
@Test
public void exchangeMetadataUnaryCall() throws Exception {
  TestServiceGrpc.TestServiceBlockingStub stub = blockingStub;

  // Capture the metadata exchange
  Metadata fixedHeaders = new Metadata();
  // Send a context proto (as it's in the default extension registry)
  Messages.SimpleContext contextValue =
      Messages.SimpleContext.newBuilder().setValue("dog").build();
  fixedHeaders.put(Util.METADATA_KEY, contextValue);
  stub = MetadataUtils.attachHeaders(stub, fixedHeaders);
  // .. and expect it to be echoed back in trailers
  AtomicReference<Metadata> trailersCapture = new AtomicReference<>();
  AtomicReference<Metadata> headersCapture = new AtomicReference<>();
  stub = MetadataUtils.captureMetadata(stub, headersCapture, trailersCapture);

  assertNotNull(stub.emptyCall(EMPTY));

  // Assert that our side channel object is echoed back in both headers and trailers
  Assert.assertEquals(contextValue, headersCapture.get().get(Util.METADATA_KEY));
  Assert.assertEquals(contextValue, trailersCapture.get().get(Util.METADATA_KEY));
}
 
Example 3
Source File: GrpcClientTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Test
void exchangeHeadersUnaryCall_armeriaHeaders() throws Exception {
    TestServiceBlockingStub stub =
            Clients.newDerivedClient(
                    blockingStub,
                    ClientOption.HTTP_HEADERS.newValue(
                            HttpHeaders.of(TestServiceImpl.EXTRA_HEADER_NAME, "dog")));

    final AtomicReference<Metadata> headers = new AtomicReference<>();
    final AtomicReference<Metadata> trailers = new AtomicReference<>();
    stub = MetadataUtils.captureMetadata(stub, headers, trailers);

    assertThat(stub.emptyCall(EMPTY)).isNotNull();

    // Assert that our side channel object is echoed back in both headers and trailers
    assertThat(CLIENT_HEADERS_CAPTURE.get().get(TestServiceImpl.EXTRA_HEADER_NAME)).isEqualTo("dog");
    assertThat(SERVER_TRAILERS_CAPTURE.get().get(TestServiceImpl.EXTRA_HEADER_NAME)).isEqualTo("dog");

    assertThat(headers.get()).isNull();
    assertThat(trailers.get().get(TestServiceImpl.EXTRA_HEADER_KEY)).isEqualTo("dog");
    assertThat(trailers.get().getAll(TestServiceImpl.STRING_VALUE_KEY)).containsExactly(
            StringValue.newBuilder().setValue("hello").build(),
            StringValue.newBuilder().setValue("world").build());

    checkRequestLog((rpcReq, rpcRes, grpcStatus) -> {
        assertThat(rpcReq.params()).containsExactly(EMPTY);
        assertThat(rpcRes.get()).isEqualTo(EMPTY);
    });
}
 
Example 4
Source File: GrpcClientTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Test
void exchangeHeadersUnaryCall_grpcMetadata() throws Exception {
    final Metadata metadata = new Metadata();
    metadata.put(TestServiceImpl.EXTRA_HEADER_KEY, "dog");

    TestServiceBlockingStub stub = MetadataUtils.attachHeaders(blockingStub, metadata);

    final AtomicReference<Metadata> headers = new AtomicReference<>();
    final AtomicReference<Metadata> trailers = new AtomicReference<>();
    stub = MetadataUtils.captureMetadata(stub, headers, trailers);

    assertThat(stub.emptyCall(EMPTY)).isNotNull();

    final HttpHeaders clientHeaders = CLIENT_HEADERS_CAPTURE.get();
    assertThat(clientHeaders.get(HttpHeaderNames.TE))
            .isEqualTo(HttpHeaderValues.TRAILERS.toString());

    // Assert that our side channel object is echoed back in both headers and trailers
    assertThat(clientHeaders.get(TestServiceImpl.EXTRA_HEADER_NAME)).isEqualTo("dog");
    assertThat(SERVER_TRAILERS_CAPTURE.get().get(TestServiceImpl.EXTRA_HEADER_NAME)).isEqualTo("dog");

    assertThat(headers.get()).isNull();
    assertThat(trailers.get().get(TestServiceImpl.EXTRA_HEADER_KEY)).isEqualTo("dog");
    assertThat(trailers.get().getAll(TestServiceImpl.STRING_VALUE_KEY)).containsExactly(
            StringValue.newBuilder().setValue("hello").build(),
            StringValue.newBuilder().setValue("world").build());

    checkRequestLog((rpcReq, rpcRes, grpcStatus) -> {
        assertThat(rpcReq.params()).containsExactly(EMPTY);
        assertThat(rpcRes.get()).isEqualTo(EMPTY);
    });
}
 
Example 5
Source File: GrpcHeaderHandler.java    From buck with Apache License 2.0 5 votes vote down vote up
/** Receives RemoteExecutionMetadata from the initial GRPC headers. */
public static <Stub extends AbstractStub<Stub>>
    StubAndResponseMetadata<Stub> wrapStubToReceiveMetadata(Stub grpcStub) {
  AtomicReference<Metadata> initialMetadata = new AtomicReference<>();
  AtomicReference<Metadata> trailingMetadata = new AtomicReference<>();
  Stub grpcWrappedStub =
      MetadataUtils.captureMetadata(grpcStub, initialMetadata, trailingMetadata);
  return new StubAndResponseMetadataImpl<>(grpcWrappedStub, initialMetadata, trailingMetadata);
}
 
Example 6
Source File: AbstractInteropTest.java    From grpc-nebula-java with Apache License 2.0 4 votes vote down vote up
@Test
public void exchangeMetadataStreamingCall() throws Exception {
  TestServiceGrpc.TestServiceStub stub = asyncStub;

  // Capture the metadata exchange
  Metadata fixedHeaders = new Metadata();
  // Send a context proto (as it's in the default extension registry)
  Messages.SimpleContext contextValue =
      Messages.SimpleContext.newBuilder().setValue("dog").build();
  fixedHeaders.put(Util.METADATA_KEY, contextValue);
  stub = MetadataUtils.attachHeaders(stub, fixedHeaders);
  // .. and expect it to be echoed back in trailers
  AtomicReference<Metadata> trailersCapture = new AtomicReference<>();
  AtomicReference<Metadata> headersCapture = new AtomicReference<>();
  stub = MetadataUtils.captureMetadata(stub, headersCapture, trailersCapture);

  List<Integer> responseSizes = Arrays.asList(50, 100, 150, 200);
  Messages.StreamingOutputCallRequest.Builder streamingOutputBuilder =
      Messages.StreamingOutputCallRequest.newBuilder();
  for (Integer size : responseSizes) {
    streamingOutputBuilder.addResponseParameters(
        ResponseParameters.newBuilder().setSize(size).setIntervalUs(0));
  }
  final Messages.StreamingOutputCallRequest request = streamingOutputBuilder.build();

  StreamRecorder<Messages.StreamingOutputCallResponse> recorder = StreamRecorder.create();
  StreamObserver<Messages.StreamingOutputCallRequest> requestStream =
      stub.fullDuplexCall(recorder);

  final int numRequests = 10;
  for (int ix = numRequests; ix > 0; --ix) {
    requestStream.onNext(request);
  }
  requestStream.onCompleted();
  recorder.awaitCompletion();
  assertSuccess(recorder);
  org.junit.Assert.assertEquals(responseSizes.size() * numRequests, recorder.getValues().size());

  // Assert that our side channel object is echoed back in both headers and trailers
  Assert.assertEquals(contextValue, headersCapture.get().get(Util.METADATA_KEY));
  Assert.assertEquals(contextValue, trailersCapture.get().get(Util.METADATA_KEY));
}
 
Example 7
Source File: AbstractInteropTest.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
@Test
public void exchangeMetadataStreamingCall() throws Exception {
  TestServiceGrpc.TestServiceStub stub = asyncStub;

  // Capture the metadata exchange
  Metadata fixedHeaders = new Metadata();
  // Send a context proto (as it's in the default extension registry)
  Messages.SimpleContext contextValue =
      Messages.SimpleContext.newBuilder().setValue("dog").build();
  fixedHeaders.put(Util.METADATA_KEY, contextValue);
  stub = MetadataUtils.attachHeaders(stub, fixedHeaders);
  // .. and expect it to be echoed back in trailers
  AtomicReference<Metadata> trailersCapture = new AtomicReference<>();
  AtomicReference<Metadata> headersCapture = new AtomicReference<>();
  stub = MetadataUtils.captureMetadata(stub, headersCapture, trailersCapture);

  List<Integer> responseSizes = Arrays.asList(50, 100, 150, 200);
  Messages.StreamingOutputCallRequest.Builder streamingOutputBuilder =
      Messages.StreamingOutputCallRequest.newBuilder();
  for (Integer size : responseSizes) {
    streamingOutputBuilder.addResponseParameters(
        ResponseParameters.newBuilder().setSize(size).setIntervalUs(0));
  }
  final Messages.StreamingOutputCallRequest request = streamingOutputBuilder.build();

  StreamRecorder<Messages.StreamingOutputCallResponse> recorder = StreamRecorder.create();
  StreamObserver<Messages.StreamingOutputCallRequest> requestStream =
      stub.fullDuplexCall(recorder);

  final int numRequests = 10;
  for (int ix = numRequests; ix > 0; --ix) {
    requestStream.onNext(request);
  }
  requestStream.onCompleted();
  recorder.awaitCompletion();
  assertSuccess(recorder);
  org.junit.Assert.assertEquals(responseSizes.size() * numRequests, recorder.getValues().size());

  // Assert that our side channel object is echoed back in both headers and trailers
  Assert.assertEquals(contextValue, headersCapture.get().get(Util.METADATA_KEY));
  Assert.assertEquals(contextValue, trailersCapture.get().get(Util.METADATA_KEY));
}