Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions context/src/main/java/io/grpc/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package io.grpc;

import io.grpc.Context.CheckReturnValue;
import java.io.Closeable;
import java.util.ArrayList;
import java.util.concurrent.Callable;
Expand Down Expand Up @@ -93,6 +94,7 @@
* </ul>
*/
/* @DoNotMock("Use ROOT for a non-null Context") // commented out to avoid dependencies */
@CheckReturnValue
public class Context {

private static final Logger log = Logger.getLogger(Context.class.getName());
Expand Down Expand Up @@ -571,6 +573,7 @@ public void run(Runnable r) {
* @param c {@link Callable} to call.
* @return result of call.
*/
@CanIgnoreReturnValue
public <V> V call(Callable<V> c) throws Exception {
Context previous = attach();
try {
Expand Down Expand Up @@ -776,6 +779,7 @@ public boolean isCurrent() {
* @return {@code true} if this context cancelled the context and notified listeners,
* {@code false} if the context was already cancelled.
*/
@CanIgnoreReturnValue
public boolean cancel(Throwable cause) {
boolean triggeredCancel = false;
synchronized (this) {
Expand Down Expand Up @@ -1008,6 +1012,7 @@ public void cancelled(Context context) {
}
}

@CanIgnoreReturnValue
private static <T> T checkNotNull(T reference, Object errorMessage) {
if (reference == null) {
throw new NullPointerException(String.valueOf(errorMessage));
Expand Down Expand Up @@ -1059,4 +1064,10 @@ private static void validateGeneration(int generation) {
new Exception());
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This probably deserves a comment why it isn't the error prone version.

// Not using the standard com.google.errorprone.annotations.CheckReturnValue because that will
// introduce dependencies that some io.grpc.Context API consumers may not want.
@interface CheckReturnValue {}

@interface CanIgnoreReturnValue {}
}
1 change: 1 addition & 0 deletions context/src/test/java/io/grpc/ContextTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
* Tests for {@link Context}.
*/
@RunWith(JUnit4.class)
@SuppressWarnings("CheckReturnValue") // false-positive in test for current ver errorprone plugin
public class ContextTest {

private static final Context.Key<String> PET = Context.key("pet");
Expand Down
23 changes: 14 additions & 9 deletions core/src/test/java/io/grpc/internal/ClientCallImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ public void setUp() {

@After
public void tearDown() {
Context.ROOT.attach();
verifyZeroInteractions(streamTracerFactory);
}

Expand Down Expand Up @@ -500,7 +499,8 @@ public void prepareHeaders_removeReservedHeaders() {
public void callerContextPropagatedToListener() throws Exception {
// Attach the context which is recorded when the call is created
final Context.Key<String> testKey = Context.key("testing");
Context.current().withValue(testKey, "testValue").attach();
Context context = Context.current().withValue(testKey, "testValue");
Context previous = context.attach();

ClientCallImpl<Void, Void> call = new ClientCallImpl<Void, Void>(
method,
Expand All @@ -512,10 +512,11 @@ public void callerContextPropagatedToListener() throws Exception {
false /* retryEnabled */)
.setDecompressorRegistry(decompressorRegistry);

Context.ROOT.attach();
context.detach(previous);

// Override the value after creating the call, this should not be seen by callbacks
Context.current().withValue(testKey, "badValue").attach();
context = Context.current().withValue(testKey, "badValue");
previous = context.attach();

final AtomicBoolean onHeadersCalled = new AtomicBoolean();
final AtomicBoolean onMessageCalled = new AtomicBoolean();
Expand Down Expand Up @@ -555,6 +556,8 @@ private void checkContext() {
}
}, new Metadata());

context.detach(previous);

verify(stream).start(listenerArgumentCaptor.capture());
ClientStreamListener listener = listenerArgumentCaptor.getValue();
listener.onReady();
Expand Down Expand Up @@ -587,7 +590,7 @@ public void contextCancellationCancelsStream() throws Exception {
false /* retryEnabled */)
.setDecompressorRegistry(decompressorRegistry);

previous.attach();
cancellableContext.detach(previous);

call.start(callListener, new Metadata());

Expand Down Expand Up @@ -617,7 +620,7 @@ public void contextAlreadyCancelledNotifiesImmediately() throws Exception {
false /* retryEnabled */)
.setDecompressorRegistry(decompressorRegistry);

previous.attach();
cancellableContext.detach(previous);

final SettableFuture<Status> statusFuture = SettableFuture.create();
call.start(new ClientCall.Listener<Void>() {
Expand Down Expand Up @@ -803,9 +806,9 @@ public void expiredDeadlineCancelsStream_CallOptions() {
public void expiredDeadlineCancelsStream_Context() {
fakeClock.forwardTime(System.nanoTime(), TimeUnit.NANOSECONDS);

Context.current()
.withDeadlineAfter(1, TimeUnit.SECONDS, deadlineCancellationExecutor)
.attach();
Context context = Context.current()
.withDeadlineAfter(1, TimeUnit.SECONDS, deadlineCancellationExecutor);
Context origContext = context.attach();

ClientCallImpl<Void, Void> call = new ClientCallImpl<Void, Void>(
method,
Expand All @@ -816,6 +819,8 @@ public void expiredDeadlineCancelsStream_Context() {
channelCallTracer,
false /* retryEnabled */);

context.detach(origContext);

call.start(callListener, new Metadata());

fakeClock.forwardNanos(TimeUnit.SECONDS.toNanos(1) + 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import com.google.common.util.concurrent.SettableFuture;
import io.grpc.Attributes;
import io.grpc.CallOptions;
import io.grpc.Context;
import io.grpc.Grpc;
import io.grpc.Metadata;
import io.grpc.MethodDescriptor;
Expand Down Expand Up @@ -127,7 +126,6 @@ public void setup() {

@After
public void teardown() throws Exception {
Context.ROOT.attach();
for (NettyClientTransport transport : transports) {
transport.shutdown(Status.UNAVAILABLE);
}
Expand Down