mirror of https://github.com/grpc/grpc-java.git
context/core/netty: Add @CheckReturnValue to Context
By adding inner class annotations without introducing external dependencies.
This commit is contained in:
parent
561583be14
commit
bf4a00c6de
|
|
@ -16,6 +16,7 @@
|
||||||
|
|
||||||
package io.grpc;
|
package io.grpc;
|
||||||
|
|
||||||
|
import io.grpc.Context.CheckReturnValue;
|
||||||
import java.io.Closeable;
|
import java.io.Closeable;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.concurrent.Callable;
|
import java.util.concurrent.Callable;
|
||||||
|
|
@ -93,6 +94,7 @@ import java.util.logging.Logger;
|
||||||
* </ul>
|
* </ul>
|
||||||
*/
|
*/
|
||||||
/* @DoNotMock("Use ROOT for a non-null Context") // commented out to avoid dependencies */
|
/* @DoNotMock("Use ROOT for a non-null Context") // commented out to avoid dependencies */
|
||||||
|
@CheckReturnValue
|
||||||
public class Context {
|
public class Context {
|
||||||
|
|
||||||
private static final Logger log = Logger.getLogger(Context.class.getName());
|
private static final Logger log = Logger.getLogger(Context.class.getName());
|
||||||
|
|
@ -571,6 +573,7 @@ public class Context {
|
||||||
* @param c {@link Callable} to call.
|
* @param c {@link Callable} to call.
|
||||||
* @return result of call.
|
* @return result of call.
|
||||||
*/
|
*/
|
||||||
|
@CanIgnoreReturnValue
|
||||||
public <V> V call(Callable<V> c) throws Exception {
|
public <V> V call(Callable<V> c) throws Exception {
|
||||||
Context previous = attach();
|
Context previous = attach();
|
||||||
try {
|
try {
|
||||||
|
|
@ -776,6 +779,7 @@ public class Context {
|
||||||
* @return {@code true} if this context cancelled the context and notified listeners,
|
* @return {@code true} if this context cancelled the context and notified listeners,
|
||||||
* {@code false} if the context was already cancelled.
|
* {@code false} if the context was already cancelled.
|
||||||
*/
|
*/
|
||||||
|
@CanIgnoreReturnValue
|
||||||
public boolean cancel(Throwable cause) {
|
public boolean cancel(Throwable cause) {
|
||||||
boolean triggeredCancel = false;
|
boolean triggeredCancel = false;
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
|
|
@ -1008,6 +1012,7 @@ public class Context {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@CanIgnoreReturnValue
|
||||||
private static <T> T checkNotNull(T reference, Object errorMessage) {
|
private static <T> T checkNotNull(T reference, Object errorMessage) {
|
||||||
if (reference == null) {
|
if (reference == null) {
|
||||||
throw new NullPointerException(String.valueOf(errorMessage));
|
throw new NullPointerException(String.valueOf(errorMessage));
|
||||||
|
|
@ -1059,4 +1064,10 @@ public class Context {
|
||||||
new Exception());
|
new Exception());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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 {}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -60,6 +60,7 @@ import org.junit.runners.JUnit4;
|
||||||
* Tests for {@link Context}.
|
* Tests for {@link Context}.
|
||||||
*/
|
*/
|
||||||
@RunWith(JUnit4.class)
|
@RunWith(JUnit4.class)
|
||||||
|
@SuppressWarnings("CheckReturnValue") // false-positive in test for current ver errorprone plugin
|
||||||
public class ContextTest {
|
public class ContextTest {
|
||||||
|
|
||||||
private static final Context.Key<String> PET = Context.key("pet");
|
private static final Context.Key<String> PET = Context.key("pet");
|
||||||
|
|
|
||||||
|
|
@ -137,7 +137,6 @@ public class ClientCallImplTest {
|
||||||
|
|
||||||
@After
|
@After
|
||||||
public void tearDown() {
|
public void tearDown() {
|
||||||
Context.ROOT.attach();
|
|
||||||
verifyZeroInteractions(streamTracerFactory);
|
verifyZeroInteractions(streamTracerFactory);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -500,7 +499,8 @@ public class ClientCallImplTest {
|
||||||
public void callerContextPropagatedToListener() throws Exception {
|
public void callerContextPropagatedToListener() throws Exception {
|
||||||
// Attach the context which is recorded when the call is created
|
// Attach the context which is recorded when the call is created
|
||||||
final Context.Key<String> testKey = Context.key("testing");
|
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>(
|
ClientCallImpl<Void, Void> call = new ClientCallImpl<Void, Void>(
|
||||||
method,
|
method,
|
||||||
|
|
@ -512,10 +512,11 @@ public class ClientCallImplTest {
|
||||||
false /* retryEnabled */)
|
false /* retryEnabled */)
|
||||||
.setDecompressorRegistry(decompressorRegistry);
|
.setDecompressorRegistry(decompressorRegistry);
|
||||||
|
|
||||||
Context.ROOT.attach();
|
context.detach(previous);
|
||||||
|
|
||||||
// Override the value after creating the call, this should not be seen by callbacks
|
// 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 onHeadersCalled = new AtomicBoolean();
|
||||||
final AtomicBoolean onMessageCalled = new AtomicBoolean();
|
final AtomicBoolean onMessageCalled = new AtomicBoolean();
|
||||||
|
|
@ -555,6 +556,8 @@ public class ClientCallImplTest {
|
||||||
}
|
}
|
||||||
}, new Metadata());
|
}, new Metadata());
|
||||||
|
|
||||||
|
context.detach(previous);
|
||||||
|
|
||||||
verify(stream).start(listenerArgumentCaptor.capture());
|
verify(stream).start(listenerArgumentCaptor.capture());
|
||||||
ClientStreamListener listener = listenerArgumentCaptor.getValue();
|
ClientStreamListener listener = listenerArgumentCaptor.getValue();
|
||||||
listener.onReady();
|
listener.onReady();
|
||||||
|
|
@ -587,7 +590,7 @@ public class ClientCallImplTest {
|
||||||
false /* retryEnabled */)
|
false /* retryEnabled */)
|
||||||
.setDecompressorRegistry(decompressorRegistry);
|
.setDecompressorRegistry(decompressorRegistry);
|
||||||
|
|
||||||
previous.attach();
|
cancellableContext.detach(previous);
|
||||||
|
|
||||||
call.start(callListener, new Metadata());
|
call.start(callListener, new Metadata());
|
||||||
|
|
||||||
|
|
@ -617,7 +620,7 @@ public class ClientCallImplTest {
|
||||||
false /* retryEnabled */)
|
false /* retryEnabled */)
|
||||||
.setDecompressorRegistry(decompressorRegistry);
|
.setDecompressorRegistry(decompressorRegistry);
|
||||||
|
|
||||||
previous.attach();
|
cancellableContext.detach(previous);
|
||||||
|
|
||||||
final SettableFuture<Status> statusFuture = SettableFuture.create();
|
final SettableFuture<Status> statusFuture = SettableFuture.create();
|
||||||
call.start(new ClientCall.Listener<Void>() {
|
call.start(new ClientCall.Listener<Void>() {
|
||||||
|
|
@ -803,9 +806,9 @@ public class ClientCallImplTest {
|
||||||
public void expiredDeadlineCancelsStream_Context() {
|
public void expiredDeadlineCancelsStream_Context() {
|
||||||
fakeClock.forwardTime(System.nanoTime(), TimeUnit.NANOSECONDS);
|
fakeClock.forwardTime(System.nanoTime(), TimeUnit.NANOSECONDS);
|
||||||
|
|
||||||
Context.current()
|
Context context = Context.current()
|
||||||
.withDeadlineAfter(1, TimeUnit.SECONDS, deadlineCancellationExecutor)
|
.withDeadlineAfter(1, TimeUnit.SECONDS, deadlineCancellationExecutor);
|
||||||
.attach();
|
Context origContext = context.attach();
|
||||||
|
|
||||||
ClientCallImpl<Void, Void> call = new ClientCallImpl<Void, Void>(
|
ClientCallImpl<Void, Void> call = new ClientCallImpl<Void, Void>(
|
||||||
method,
|
method,
|
||||||
|
|
@ -816,6 +819,8 @@ public class ClientCallImplTest {
|
||||||
channelCallTracer,
|
channelCallTracer,
|
||||||
false /* retryEnabled */);
|
false /* retryEnabled */);
|
||||||
|
|
||||||
|
context.detach(origContext);
|
||||||
|
|
||||||
call.start(callListener, new Metadata());
|
call.start(callListener, new Metadata());
|
||||||
|
|
||||||
fakeClock.forwardNanos(TimeUnit.SECONDS.toNanos(1) + 1);
|
fakeClock.forwardNanos(TimeUnit.SECONDS.toNanos(1) + 1);
|
||||||
|
|
|
||||||
|
|
@ -39,7 +39,6 @@ import com.google.common.io.ByteStreams;
|
||||||
import com.google.common.util.concurrent.SettableFuture;
|
import com.google.common.util.concurrent.SettableFuture;
|
||||||
import io.grpc.Attributes;
|
import io.grpc.Attributes;
|
||||||
import io.grpc.CallOptions;
|
import io.grpc.CallOptions;
|
||||||
import io.grpc.Context;
|
|
||||||
import io.grpc.Grpc;
|
import io.grpc.Grpc;
|
||||||
import io.grpc.Metadata;
|
import io.grpc.Metadata;
|
||||||
import io.grpc.MethodDescriptor;
|
import io.grpc.MethodDescriptor;
|
||||||
|
|
@ -127,7 +126,6 @@ public class NettyClientTransportTest {
|
||||||
|
|
||||||
@After
|
@After
|
||||||
public void teardown() throws Exception {
|
public void teardown() throws Exception {
|
||||||
Context.ROOT.attach();
|
|
||||||
for (NettyClientTransport transport : transports) {
|
for (NettyClientTransport transport : transports) {
|
||||||
transport.shutdown(Status.UNAVAILABLE);
|
transport.shutdown(Status.UNAVAILABLE);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue