Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stub: Mark Stub-based MetadataUtils methods deprecated #8395

Merged
merged 2 commits into from Aug 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -1043,19 +1043,18 @@ public void veryLargeResponse() throws Exception {

@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);
TestServiceGrpc.TestServiceBlockingStub stub = blockingStub.withInterceptors(
MetadataUtils.newAttachHeadersInterceptor(fixedHeaders),
MetadataUtils.newCaptureMetadataInterceptor(headersCapture, trailersCapture));

assertNotNull(stub.emptyCall(EMPTY));

Expand All @@ -1066,19 +1065,18 @@ public void exchangeMetadataUnaryCall() throws Exception {

@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);
TestServiceGrpc.TestServiceStub stub = asyncStub.withInterceptors(
MetadataUtils.newAttachHeadersInterceptor(fixedHeaders),
MetadataUtils.newCaptureMetadataInterceptor(headersCapture, trailersCapture));

List<Integer> responseSizes = Arrays.asList(50, 100, 150, 200);
Messages.StreamingOutputCallRequest.Builder streamingOutputBuilder =
Expand Down Expand Up @@ -1490,11 +1488,11 @@ public void customMetadata() throws Exception {
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 = this.blockingStub;
blockingStub = MetadataUtils.attachHeaders(blockingStub, metadata);
AtomicReference<Metadata> headersCapture = new AtomicReference<>();
AtomicReference<Metadata> trailersCapture = new AtomicReference<>();
blockingStub = MetadataUtils.captureMetadata(blockingStub, headersCapture, trailersCapture);
TestServiceGrpc.TestServiceBlockingStub blockingStub = this.blockingStub.withInterceptors(
MetadataUtils.newAttachHeadersInterceptor(metadata),
MetadataUtils.newCaptureMetadataInterceptor(headersCapture, trailersCapture));
SimpleResponse response = blockingStub.unaryCall(request);

assertResponse(goldenResponse, response);
Expand All @@ -1509,11 +1507,11 @@ public void customMetadata() throws Exception {
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 = asyncStub;
stub = MetadataUtils.attachHeaders(stub, metadata);
headersCapture = new AtomicReference<>();
trailersCapture = new AtomicReference<>();
stub = MetadataUtils.captureMetadata(stub, headersCapture, trailersCapture);
TestServiceGrpc.TestServiceStub stub = asyncStub.withInterceptors(
MetadataUtils.newAttachHeadersInterceptor(metadata),
MetadataUtils.newCaptureMetadataInterceptor(headersCapture, trailersCapture));

StreamRecorder<Messages.StreamingOutputCallResponse> recorder = StreamRecorder.create();
StreamObserver<Messages.StreamingOutputCallRequest> requestStream =
Expand Down
4 changes: 4 additions & 0 deletions stub/src/main/java/io/grpc/stub/MetadataUtils.java
Expand Up @@ -43,8 +43,10 @@ private MetadataUtils() {}
* @param stub to bind the headers to.
* @param extraHeaders the headers to be passed by each call on the returned stub.
* @return an implementation of the stub with {@code extraHeaders} bound to each call.
* @deprecated Use {@code stub.withInterceptors(newAttachHeadersInterceptor(...))} instead.
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1789")
@Deprecated
public static <T extends AbstractStub<T>> T attachHeaders(T stub, Metadata extraHeaders) {
return stub.withInterceptors(newAttachHeadersInterceptor(extraHeaders));
}
Expand Down Expand Up @@ -98,8 +100,10 @@ public void start(Listener<RespT> responseListener, Metadata headers) {
* @param trailersCapture to record the last received trailers
* @return an implementation of the stub that allows to access the last received call's
* headers and trailers via {@code headersCapture} and {@code trailersCapture}.
* @deprecated Use {@code stub.withInterceptors(newCaptureMetadataInterceptor())} instead.
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1789")
@Deprecated
public static <T extends AbstractStub<T>> T captureMetadata(
T stub,
AtomicReference<Metadata> headersCapture,
Expand Down