From ef178304cb93ee660219e60b1a6553c1ace835de Mon Sep 17 00:00:00 2001 From: Eric Anderson Date: Mon, 20 Jun 2016 14:41:49 -0700 Subject: [PATCH] Revert "Eliminate MethodDescriptor from startCall and interceptCall for servers" This reverts commit 3df1446debe7e0da964b57306c2e42a74a8005b5. The commit was adding to the difficulty of integration for testing. By itself it isn't bad, so this is a temporary revert until the many other commits are absorbed and then it will be reapplied. This does have a manual edit for ClientCallsTest. --- .../proto/BenchmarkServiceGrpc.java | 8 +- .../benchmarks/proto/WorkerServiceGrpc.java | 10 +- .../benchmarks/netty/AbstractBenchmark.java | 25 +-- .../netty/HandlerRegistryBenchmark.java | 20 +- .../io/grpc/benchmarks/driver/LoadServer.java | 9 +- .../src/java_plugin/cpp/java_generator.cpp | 30 +-- compiler/src/test/golden/TestService.java.txt | 11 +- .../src/testLite/golden/TestService.java.txt | 11 +- .../src/testNano/golden/TestService.java.txt | 11 +- core/src/main/java/io/grpc/Contexts.java | 6 +- .../java/io/grpc/ForwardingServerCall.java | 21 +- .../main/java/io/grpc/HandlerRegistry.java | 2 - .../io/grpc/PartialForwardingServerCall.java | 4 +- core/src/main/java/io/grpc/ServerCall.java | 9 +- .../main/java/io/grpc/ServerCallHandler.java | 4 +- .../main/java/io/grpc/ServerInterceptor.java | 4 +- .../main/java/io/grpc/ServerInterceptors.java | 42 ++-- .../java/io/grpc/ServerMethodDefinition.java | 83 ++++++++ .../java/io/grpc/ServerServiceDefinition.java | 124 +++-------- .../main/java/io/grpc/ServiceDescriptor.java | 69 ------- .../internal/AbstractServerImplBuilder.java | 7 +- .../internal/InternalHandlerRegistry.java | 4 +- .../java/io/grpc/internal/ServerCallImpl.java | 7 +- .../java/io/grpc/internal/ServerImpl.java | 6 +- .../io/grpc/util/MutableHandlerRegistry.java | 6 +- core/src/test/java/io/grpc/ContextsTest.java | 20 +- .../java/io/grpc/ServerInterceptorsTest.java | 195 +++++++++--------- .../java/io/grpc/internal/ServerImplTest.java | 66 +++--- .../grpc/util/MutableHandlerRegistryTest.java | 112 ++++------ .../grpc/examples/helloworld/GreeterGrpc.java | 7 +- .../examples/routeguide/RouteGuideGrpc.java | 10 +- .../header/HeaderServerInterceptor.java | 6 +- .../examples/helloworld/HelloJsonServer.java | 8 +- .../grpc/io/grpc/grpclb/LoadBalancerGrpc.java | 7 +- .../integration/MetricsServiceGrpc.java | 8 +- .../integration/ReconnectServiceGrpc.java | 8 +- .../testing/integration/TestServiceGrpc.java | 12 +- .../integration/UnimplementedServiceGrpc.java | 7 +- .../integration/AbstractInteropTest.java | 4 +- .../testing/integration/CascadingTest.java | 7 +- .../testing/integration/CompressionTest.java | 5 +- .../integration/TransportCompressionTest.java | 6 +- .../grpc/io/grpc/health/v1/HealthGrpc.java | 7 +- .../main/java/io/grpc/stub/ServerCalls.java | 20 +- .../java/io/grpc/stub/ClientCallsTest.java | 7 +- .../java/io/grpc/stub/ServerCallsTest.java | 23 ++- .../main/java/io/grpc/testing/TestUtils.java | 19 +- 47 files changed, 428 insertions(+), 669 deletions(-) create mode 100644 core/src/main/java/io/grpc/ServerMethodDefinition.java delete mode 100644 core/src/main/java/io/grpc/ServiceDescriptor.java diff --git a/benchmarks/src/generated/main/grpc/io/grpc/benchmarks/proto/BenchmarkServiceGrpc.java b/benchmarks/src/generated/main/grpc/io/grpc/benchmarks/proto/BenchmarkServiceGrpc.java index 07f3e2d967..a88cf53af2 100644 --- a/benchmarks/src/generated/main/grpc/io/grpc/benchmarks/proto/BenchmarkServiceGrpc.java +++ b/benchmarks/src/generated/main/grpc/io/grpc/benchmarks/proto/BenchmarkServiceGrpc.java @@ -263,15 +263,9 @@ public class BenchmarkServiceGrpc { } } - public static io.grpc.ServiceDescriptor getServiceDescriptor() { - return new io.grpc.ServiceDescriptor(SERVICE_NAME, - METHOD_UNARY_CALL, - METHOD_STREAMING_CALL); - } - public static io.grpc.ServerServiceDefinition bindService( final BenchmarkService serviceImpl) { - return io.grpc.ServerServiceDefinition.builder(getServiceDescriptor()) + return io.grpc.ServerServiceDefinition.builder(SERVICE_NAME) .addMethod( METHOD_UNARY_CALL, asyncUnaryCall( diff --git a/benchmarks/src/generated/main/grpc/io/grpc/benchmarks/proto/WorkerServiceGrpc.java b/benchmarks/src/generated/main/grpc/io/grpc/benchmarks/proto/WorkerServiceGrpc.java index d7ff3a87e2..b56b420a1e 100644 --- a/benchmarks/src/generated/main/grpc/io/grpc/benchmarks/proto/WorkerServiceGrpc.java +++ b/benchmarks/src/generated/main/grpc/io/grpc/benchmarks/proto/WorkerServiceGrpc.java @@ -366,17 +366,9 @@ public class WorkerServiceGrpc { } } - public static io.grpc.ServiceDescriptor getServiceDescriptor() { - return new io.grpc.ServiceDescriptor(SERVICE_NAME, - METHOD_RUN_SERVER, - METHOD_RUN_CLIENT, - METHOD_CORE_COUNT, - METHOD_QUIT_WORKER); - } - public static io.grpc.ServerServiceDefinition bindService( final WorkerService serviceImpl) { - return io.grpc.ServerServiceDefinition.builder(getServiceDescriptor()) + return io.grpc.ServerServiceDefinition.builder(SERVICE_NAME) .addMethod( METHOD_RUN_SERVER, asyncBidiStreamingCall( diff --git a/benchmarks/src/jmh/java/io/grpc/benchmarks/netty/AbstractBenchmark.java b/benchmarks/src/jmh/java/io/grpc/benchmarks/netty/AbstractBenchmark.java index 2f6692b320..a6b9a9b015 100644 --- a/benchmarks/src/jmh/java/io/grpc/benchmarks/netty/AbstractBenchmark.java +++ b/benchmarks/src/jmh/java/io/grpc/benchmarks/netty/AbstractBenchmark.java @@ -41,7 +41,6 @@ import io.grpc.Server; import io.grpc.ServerCall; import io.grpc.ServerCallHandler; import io.grpc.ServerServiceDefinition; -import io.grpc.ServiceDescriptor; import io.grpc.Status; import io.grpc.benchmarks.ByteBufOutputMarshaller; import io.grpc.netty.NegotiationType; @@ -259,15 +258,13 @@ public abstract class AbstractBenchmark { // Server implementation of unary & streaming methods serverBuilder.addService( - ServerServiceDefinition.builder( - new ServiceDescriptor("benchmark", - unaryMethod, - pingPongMethod, - flowControlledStreaming)) - .addMethod(unaryMethod, new ServerCallHandler() { + ServerServiceDefinition.builder("benchmark") + .addMethod(unaryMethod, + new ServerCallHandler() { @Override public ServerCall.Listener startCall( - final ServerCall call, + MethodDescriptor method, + final ServerCall call, Metadata headers) { call.sendHeaders(new Metadata()); call.request(1); @@ -295,10 +292,12 @@ public abstract class AbstractBenchmark { }; } }) - .addMethod(pingPongMethod, new ServerCallHandler() { + .addMethod(pingPongMethod, + new ServerCallHandler() { @Override public ServerCall.Listener startCall( - final ServerCall call, + MethodDescriptor method, + final ServerCall call, Metadata headers) { call.sendHeaders(new Metadata()); call.request(1); @@ -328,10 +327,12 @@ public abstract class AbstractBenchmark { }; } }) - .addMethod(flowControlledStreaming, new ServerCallHandler() { + .addMethod(flowControlledStreaming, + new ServerCallHandler() { @Override public ServerCall.Listener startCall( - final ServerCall call, + MethodDescriptor method, + final ServerCall call, Metadata headers) { call.sendHeaders(new Metadata()); call.request(1); diff --git a/benchmarks/src/jmh/java/io/grpc/benchmarks/netty/HandlerRegistryBenchmark.java b/benchmarks/src/jmh/java/io/grpc/benchmarks/netty/HandlerRegistryBenchmark.java index 52918cdeb1..f16d19c595 100644 --- a/benchmarks/src/jmh/java/io/grpc/benchmarks/netty/HandlerRegistryBenchmark.java +++ b/benchmarks/src/jmh/java/io/grpc/benchmarks/netty/HandlerRegistryBenchmark.java @@ -31,13 +31,9 @@ package io.grpc.benchmarks.netty; -import io.grpc.Metadata; import io.grpc.MethodDescriptor; -import io.grpc.ServerCall; -import io.grpc.ServerCall.Listener; -import io.grpc.ServerCallHandler; +import io.grpc.ServerMethodDefinition; import io.grpc.ServerServiceDefinition; -import io.grpc.ServiceDescriptor; import io.grpc.util.MutableHandlerRegistry; import org.openjdk.jmh.annotations.Benchmark; @@ -84,21 +80,13 @@ public class HandlerRegistryBenchmark { fullMethodNames = new ArrayList(serviceCount * methodCountPerService); for (int serviceIndex = 0; serviceIndex < serviceCount; ++serviceIndex) { String serviceName = randomString(); - ServerServiceDefinition.Builder serviceBuilder = ServerServiceDefinition.builder( - new ServiceDescriptor(serviceName)); + ServerServiceDefinition.Builder serviceBuilder = ServerServiceDefinition.builder(serviceName); for (int methodIndex = 0; methodIndex < methodCountPerService; ++methodIndex) { String methodName = randomString(); - MethodDescriptor methodDescriptor = MethodDescriptor.create( + MethodDescriptor methodDescriptor = MethodDescriptor.create( MethodDescriptor.MethodType.UNKNOWN, MethodDescriptor.generateFullMethodName(serviceName, methodName), null, null); - serviceBuilder.addMethod(methodDescriptor, - new ServerCallHandler() { - @Override - public Listener startCall(ServerCall call, - Metadata headers) { - return null; - } - }); + serviceBuilder.addMethod(ServerMethodDefinition.create(methodDescriptor, null)); fullMethodNames.add(methodDescriptor.getFullMethodName()); } registry.addService(serviceBuilder.build()); diff --git a/benchmarks/src/main/java/io/grpc/benchmarks/driver/LoadServer.java b/benchmarks/src/main/java/io/grpc/benchmarks/driver/LoadServer.java index fd09ac20b0..6da2e90de2 100644 --- a/benchmarks/src/main/java/io/grpc/benchmarks/driver/LoadServer.java +++ b/benchmarks/src/main/java/io/grpc/benchmarks/driver/LoadServer.java @@ -40,7 +40,6 @@ import io.grpc.ServerBuilder; import io.grpc.ServerCall; import io.grpc.ServerCallHandler; import io.grpc.ServerServiceDefinition; -import io.grpc.ServiceDescriptor; import io.grpc.Status; import io.grpc.benchmarks.ByteBufOutputMarshaller; import io.grpc.benchmarks.Utils; @@ -142,8 +141,7 @@ final class LoadServer { if (config.getServerType() == Control.ServerType.ASYNC_GENERIC_SERVER) { serverBuilder.addService( ServerServiceDefinition - .builder(new ServiceDescriptor(BenchmarkServiceGrpc.SERVICE_NAME, - GENERIC_STREAMING_PING_PONG_METHOD)) + .builder(BenchmarkServiceGrpc.SERVICE_NAME) .addMethod(GENERIC_STREAMING_PING_PONG_METHOD, new GenericServiceCallHandler()) .build()); } else { @@ -232,10 +230,9 @@ final class LoadServer { } private class GenericServiceCallHandler implements ServerCallHandler { - @Override - public ServerCall.Listener startCall( - final ServerCall call, Metadata headers) { + public ServerCall.Listener startCall(MethodDescriptor method, + final ServerCall call, Metadata headers) { call.sendHeaders(new Metadata()); call.request(1); return new ServerCall.Listener() { diff --git a/compiler/src/java_plugin/cpp/java_generator.cpp b/compiler/src/java_plugin/cpp/java_generator.cpp index 292a6cfe98..de7279403d 100644 --- a/compiler/src/java_plugin/cpp/java_generator.cpp +++ b/compiler/src/java_plugin/cpp/java_generator.cpp @@ -851,31 +851,6 @@ static void PrintMethodHandlerClass(const ServiceDescriptor* service, p->Print("}\n\n"); } -static void PrintGetServiceDescriptorMethod(const ServiceDescriptor* service, - map* vars, - Printer* p, - bool generate_nano) { - (*vars)["service_name"] = service->name(); - p->Print( - *vars, - "public static $ServiceDescriptor$ getServiceDescriptor() {\n"); - p->Indent(); - p->Print(*vars, - "return new $ServiceDescriptor$(SERVICE_NAME"); - p->Indent(); - p->Indent(); - for (int i = 0; i < service->method_count(); ++i) { - const MethodDescriptor* method = service->method(i); - (*vars)["method_field_name"] = MethodPropertiesFieldName(method); - p->Print(*vars, ",\n$method_field_name$"); - } - p->Print(");\n"); - p->Outdent(); - p->Outdent(); - p->Outdent(); - p->Print("}\n\n"); -} - static void PrintBindServiceMethod(const ServiceDescriptor* service, map* vars, Printer* p, @@ -888,7 +863,7 @@ static void PrintBindServiceMethod(const ServiceDescriptor* service, p->Indent(); p->Print(*vars, "return " - "$ServerServiceDefinition$.builder(getServiceDescriptor())\n"); + "$ServerServiceDefinition$.builder(SERVICE_NAME)\n"); p->Indent(); p->Indent(); for (int i = 0; i < service->method_count(); ++i) { @@ -1019,7 +994,6 @@ static void PrintService(const ServiceDescriptor* service, PrintStub(service, vars, p, BLOCKING_CLIENT_IMPL, generate_nano); PrintStub(service, vars, p, FUTURE_CLIENT_IMPL, generate_nano); PrintMethodHandlerClass(service, vars, p, generate_nano); - PrintGetServiceDescriptorMethod(service, vars, p, generate_nano); PrintBindServiceMethod(service, vars, p, generate_nano); p->Outdent(); p->Print("}\n"); @@ -1076,8 +1050,6 @@ void GenerateService(const ServiceDescriptor* service, vars["BindableService"] = "io.grpc.BindableService"; vars["ServerServiceDefinition"] = "io.grpc.ServerServiceDefinition"; - vars["ServiceDescriptor"] = - "io.grpc.ServiceDescriptor"; vars["AbstractStub"] = "io.grpc.stub.AbstractStub"; vars["ImmutableList"] = "com.google.common.collect.ImmutableList"; vars["Collection"] = "java.util.Collection"; diff --git a/compiler/src/test/golden/TestService.java.txt b/compiler/src/test/golden/TestService.java.txt index d2db52d829..27da25da22 100644 --- a/compiler/src/test/golden/TestService.java.txt +++ b/compiler/src/test/golden/TestService.java.txt @@ -400,18 +400,9 @@ public class TestServiceGrpc { } } - public static io.grpc.ServiceDescriptor getServiceDescriptor() { - return new io.grpc.ServiceDescriptor(SERVICE_NAME, - METHOD_UNARY_CALL, - METHOD_STREAMING_OUTPUT_CALL, - METHOD_STREAMING_INPUT_CALL, - METHOD_FULL_BIDI_CALL, - METHOD_HALF_BIDI_CALL); - } - public static io.grpc.ServerServiceDefinition bindService( final TestService serviceImpl) { - return io.grpc.ServerServiceDefinition.builder(getServiceDescriptor()) + return io.grpc.ServerServiceDefinition.builder(SERVICE_NAME) .addMethod( METHOD_UNARY_CALL, asyncUnaryCall( diff --git a/compiler/src/testLite/golden/TestService.java.txt b/compiler/src/testLite/golden/TestService.java.txt index 7eb5472e22..dfd559f38c 100644 --- a/compiler/src/testLite/golden/TestService.java.txt +++ b/compiler/src/testLite/golden/TestService.java.txt @@ -400,18 +400,9 @@ public class TestServiceGrpc { } } - public static io.grpc.ServiceDescriptor getServiceDescriptor() { - return new io.grpc.ServiceDescriptor(SERVICE_NAME, - METHOD_UNARY_CALL, - METHOD_STREAMING_OUTPUT_CALL, - METHOD_STREAMING_INPUT_CALL, - METHOD_FULL_BIDI_CALL, - METHOD_HALF_BIDI_CALL); - } - public static io.grpc.ServerServiceDefinition bindService( final TestService serviceImpl) { - return io.grpc.ServerServiceDefinition.builder(getServiceDescriptor()) + return io.grpc.ServerServiceDefinition.builder(SERVICE_NAME) .addMethod( METHOD_UNARY_CALL, asyncUnaryCall( diff --git a/compiler/src/testNano/golden/TestService.java.txt b/compiler/src/testNano/golden/TestService.java.txt index 95462dbbe7..27104de755 100644 --- a/compiler/src/testNano/golden/TestService.java.txt +++ b/compiler/src/testNano/golden/TestService.java.txt @@ -478,18 +478,9 @@ public class TestServiceGrpc { } } - public static io.grpc.ServiceDescriptor getServiceDescriptor() { - return new io.grpc.ServiceDescriptor(SERVICE_NAME, - METHOD_UNARY_CALL, - METHOD_STREAMING_OUTPUT_CALL, - METHOD_STREAMING_INPUT_CALL, - METHOD_FULL_BIDI_CALL, - METHOD_HALF_BIDI_CALL); - } - public static io.grpc.ServerServiceDefinition bindService( final TestService serviceImpl) { - return io.grpc.ServerServiceDefinition.builder(getServiceDescriptor()) + return io.grpc.ServerServiceDefinition.builder(SERVICE_NAME) .addMethod( METHOD_UNARY_CALL, asyncUnaryCall( diff --git a/core/src/main/java/io/grpc/Contexts.java b/core/src/main/java/io/grpc/Contexts.java index 9f41323923..8035cfbe4d 100644 --- a/core/src/main/java/io/grpc/Contexts.java +++ b/core/src/main/java/io/grpc/Contexts.java @@ -53,6 +53,7 @@ public class Contexts { * the client. * * @param context to make {@link Context#current()}. + * @param method being requested by the client. * @param call used to send responses to client. * @param headers received from client. * @param next handler used to create the listener to be wrapped. @@ -60,13 +61,14 @@ public class Contexts { */ public static ServerCall.Listener interceptCall( Context context, - ServerCall call, + MethodDescriptor method, + ServerCall call, Metadata headers, ServerCallHandler next) { Context previous = context.attach(); try { return new ContextualizedServerCallListener( - next.startCall(call, headers), + next.startCall(method, call, headers), context); } finally { context.detach(previous); diff --git a/core/src/main/java/io/grpc/ForwardingServerCall.java b/core/src/main/java/io/grpc/ForwardingServerCall.java index 578a9a1b4b..1c8a9be632 100644 --- a/core/src/main/java/io/grpc/ForwardingServerCall.java +++ b/core/src/main/java/io/grpc/ForwardingServerCall.java @@ -34,13 +34,13 @@ package io.grpc; /** * A {@link ServerCall} which forwards all of it's methods to another {@link ServerCall}. */ -public abstract class ForwardingServerCall - extends PartialForwardingServerCall { +public abstract class ForwardingServerCall + extends PartialForwardingServerCall { /** * Returns the delegated {@code ServerCall}. */ @Override - protected abstract ServerCall delegate(); + protected abstract ServerCall delegate(); @Override public void sendMessage(RespT message) { @@ -51,23 +51,18 @@ public abstract class ForwardingServerCall * A simplified version of {@link ForwardingServerCall} where subclasses can pass in a {@link * ServerCall} as the delegate. */ - public abstract static class SimpleForwardingServerCall - extends ForwardingServerCall { + public abstract static class SimpleForwardingServerCall + extends ForwardingServerCall { - private final ServerCall delegate; + private final ServerCall delegate; - protected SimpleForwardingServerCall(ServerCall delegate) { + protected SimpleForwardingServerCall(ServerCall delegate) { this.delegate = delegate; } @Override - protected ServerCall delegate() { + protected ServerCall delegate() { return delegate; } - - @Override - public MethodDescriptor getMethodDescriptor() { - return delegate.getMethodDescriptor(); - } } } diff --git a/core/src/main/java/io/grpc/HandlerRegistry.java b/core/src/main/java/io/grpc/HandlerRegistry.java index 19c429f6d1..7aad4f65a7 100644 --- a/core/src/main/java/io/grpc/HandlerRegistry.java +++ b/core/src/main/java/io/grpc/HandlerRegistry.java @@ -31,8 +31,6 @@ package io.grpc; -import io.grpc.ServerServiceDefinition.ServerMethodDefinition; - import javax.annotation.Nullable; import javax.annotation.concurrent.ThreadSafe; diff --git a/core/src/main/java/io/grpc/PartialForwardingServerCall.java b/core/src/main/java/io/grpc/PartialForwardingServerCall.java index 88e0dd870c..aefd62882a 100644 --- a/core/src/main/java/io/grpc/PartialForwardingServerCall.java +++ b/core/src/main/java/io/grpc/PartialForwardingServerCall.java @@ -35,11 +35,11 @@ package io.grpc; * A {@link ServerCall} which forwards all of it's methods to another {@link ServerCall} which * may have a different onMessage() message type. */ -abstract class PartialForwardingServerCall extends ServerCall { +abstract class PartialForwardingServerCall extends ServerCall { /** * Returns the delegated {@code ServerCall}. */ - protected abstract ServerCall delegate(); + protected abstract ServerCall delegate(); @Override public void request(int numMessages) { diff --git a/core/src/main/java/io/grpc/ServerCall.java b/core/src/main/java/io/grpc/ServerCall.java index 37977f365c..c90b41d4b5 100644 --- a/core/src/main/java/io/grpc/ServerCall.java +++ b/core/src/main/java/io/grpc/ServerCall.java @@ -49,10 +49,9 @@ import javax.net.ssl.SSLSession; * *

Methods are guaranteed to be non-blocking. Implementations are not required to be thread-safe. * - * @param parsed type of request message. * @param parsed type of response message. */ -public abstract class ServerCall { +public abstract class ServerCall { /** * {@link Attributes.Key} for the remote address of server call attributes * {@link ServerCall#attributes()} @@ -227,10 +226,4 @@ public abstract class ServerCall { public Attributes attributes() { return Attributes.EMPTY; } - - - /** - * The {@link MethodDescriptor} for the call. - */ - public abstract MethodDescriptor getMethodDescriptor(); } diff --git a/core/src/main/java/io/grpc/ServerCallHandler.java b/core/src/main/java/io/grpc/ServerCallHandler.java index 8c98774c51..6197e848ea 100644 --- a/core/src/main/java/io/grpc/ServerCallHandler.java +++ b/core/src/main/java/io/grpc/ServerCallHandler.java @@ -47,10 +47,12 @@ public interface ServerCallHandler { * Implementations must not throw an exception if they started processing that may use {@code * call} on another thread. * + * @param method descriptor for the call * @param call object for responding to the remote client. * @return listener for processing incoming request messages for {@code call} */ ServerCall.Listener startCall( - ServerCall call, + MethodDescriptor method, + ServerCall call, Metadata headers); } diff --git a/core/src/main/java/io/grpc/ServerInterceptor.java b/core/src/main/java/io/grpc/ServerInterceptor.java index 160d2f681f..11afefb355 100644 --- a/core/src/main/java/io/grpc/ServerInterceptor.java +++ b/core/src/main/java/io/grpc/ServerInterceptor.java @@ -57,12 +57,14 @@ public interface ServerInterceptor { * Implementations must not throw an exception if they started processing that may use {@code * call} on another thread. * + * @param method descriptor for method * @param call object to receive response messages * @param next next processor in the interceptor chain * @return listener for processing incoming messages for {@code call}, never {@code null}. */ ServerCall.Listener interceptCall( - ServerCall call, + MethodDescriptor method, + ServerCall call, Metadata headers, ServerCallHandler next); } diff --git a/core/src/main/java/io/grpc/ServerInterceptors.java b/core/src/main/java/io/grpc/ServerInterceptors.java index 50af3b042a..0d4f2e5f06 100644 --- a/core/src/main/java/io/grpc/ServerInterceptors.java +++ b/core/src/main/java/io/grpc/ServerInterceptors.java @@ -33,8 +33,6 @@ package io.grpc; import com.google.common.base.Preconditions; -import io.grpc.ServerServiceDefinition.ServerMethodDefinition; - import java.io.BufferedInputStream; import java.io.InputStream; import java.util.ArrayList; @@ -122,7 +120,7 @@ public class ServerInterceptors { return serviceDef; } ServerServiceDefinition.Builder serviceDefBuilder - = ServerServiceDefinition.builder(serviceDef.getServiceDescriptor()); + = ServerServiceDefinition.builder(serviceDef.getName()); for (ServerMethodDefinition method : serviceDef.getMethods()) { wrapAndAddMethod(serviceDefBuilder, method, interceptors); } @@ -181,11 +179,8 @@ public class ServerInterceptors { public static ServerServiceDefinition useMarshalledMessages( final ServerServiceDefinition serviceDef, final MethodDescriptor.Marshaller marshaller) { - List> wrappedMethods = - new ArrayList>(); - List> wrappedDescriptors = - new ArrayList>(); - // Wrap the descriptors + final ServerServiceDefinition.Builder serviceBuilder = ServerServiceDefinition + .builder(serviceDef.getName()); for (final ServerMethodDefinition definition : serviceDef.getMethods()) { final MethodDescriptor originalMethodDescriptor = definition.getMethodDescriptor(); final MethodDescriptor wrappedMethodDescriptor = MethodDescriptor @@ -193,16 +188,7 @@ public class ServerInterceptors { originalMethodDescriptor.getFullMethodName(), marshaller, marshaller); - wrappedDescriptors.add(wrappedMethodDescriptor); - wrappedMethods.add(wrapMethod(definition, wrappedMethodDescriptor)); - } - // Build the new service descriptor - final ServerServiceDefinition.Builder serviceBuilder = ServerServiceDefinition - .builder(new ServiceDescriptor(serviceDef.getServiceDescriptor().getName(), - wrappedDescriptors)); - // Create the new service definiton. - for (ServerMethodDefinition definition : wrappedMethods) { - serviceBuilder.addMethod(definition); + serviceBuilder.addMethod(wrapMethod(definition, wrappedMethodDescriptor)); } return serviceBuilder.build(); } @@ -234,9 +220,10 @@ public class ServerInterceptors { @Override public ServerCall.Listener startCall( - ServerCall call, + MethodDescriptor method, + ServerCall call, Metadata headers) { - return interceptor.interceptCall(call, headers, callHandler); + return interceptor.interceptCall(method, call, headers, callHandler); } } @@ -257,12 +244,12 @@ public class ServerInterceptors { return new ServerCallHandler() { @Override public ServerCall.Listener startCall( - final ServerCall call, + final MethodDescriptor method, + final ServerCall call, final Metadata headers) { - final ServerCall unwrappedCall = - new PartialForwardingServerCall() { + final ServerCall unwrappedCall = new PartialForwardingServerCall() { @Override - protected ServerCall delegate() { + protected ServerCall delegate() { return call; } @@ -272,15 +259,10 @@ public class ServerInterceptors { final WRespT wrappedMessage = wrappedMethod.parseResponse(is); delegate().sendMessage(wrappedMessage); } - - @Override - public MethodDescriptor getMethodDescriptor() { - return originalMethod; - } }; final ServerCall.Listener originalListener = originalHandler - .startCall(unwrappedCall, headers); + .startCall(originalMethod, unwrappedCall, headers); return new PartialForwardingServerCallListener() { @Override diff --git a/core/src/main/java/io/grpc/ServerMethodDefinition.java b/core/src/main/java/io/grpc/ServerMethodDefinition.java new file mode 100644 index 0000000000..b136eb9733 --- /dev/null +++ b/core/src/main/java/io/grpc/ServerMethodDefinition.java @@ -0,0 +1,83 @@ +/* + * Copyright 2014, Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package io.grpc; + +/** + * Definition of a method exposed by a {@link Server}. + * + * @see ServerServiceDefinition + */ +@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1774") +public final class ServerMethodDefinition { + private final MethodDescriptor method; + private final ServerCallHandler handler; + + private ServerMethodDefinition(MethodDescriptor method, + ServerCallHandler handler) { + this.method = method; + this.handler = handler; + } + + /** + * Create a new instance. + * + * @param method the {@link MethodDescriptor} for this method. + * @param handler to dispatch calls to. + * @return a new instance. + */ + public static ServerMethodDefinition create( + MethodDescriptor method, + ServerCallHandler handler) { + return new ServerMethodDefinition(method, handler); + } + + /** The {@code MethodDescriptor} for this method. */ + public MethodDescriptor getMethodDescriptor() { + return method; + } + + /** Handler for incoming calls. */ + public ServerCallHandler getServerCallHandler() { + return handler; + } + + /** + * Create a new method definition with a different call handler. + * + * @param handler to bind to a cloned instance of this. + * @return a cloned instance of this with the new handler bound. + */ + public ServerMethodDefinition withServerCallHandler( + ServerCallHandler handler) { + return new ServerMethodDefinition(method, handler); + } +} diff --git a/core/src/main/java/io/grpc/ServerServiceDefinition.java b/core/src/main/java/io/grpc/ServerServiceDefinition.java index 8ab22c26cd..703f785454 100644 --- a/core/src/main/java/io/grpc/ServerServiceDefinition.java +++ b/core/src/main/java/io/grpc/ServerServiceDefinition.java @@ -43,27 +43,26 @@ import java.util.Map; /** Definition of a service to be exposed via a Server. */ public final class ServerServiceDefinition { - - public static Builder builder(ServiceDescriptor serviceDescriptor) { - return new Builder(serviceDescriptor); + @ExperimentalApi("https://github.com/grpc/grpc-java/issues/1774") + public static Builder builder(String serviceName) { + return new Builder(serviceName); } - private final ServiceDescriptor serviceDescriptor; + private final String name; private final ImmutableMap> methods; private ServerServiceDefinition( - ServiceDescriptor serviceDescriptor, Map> methods) { - this.serviceDescriptor = checkNotNull(serviceDescriptor); + String name, Map> methods) { + this.name = checkNotNull(name); this.methods = ImmutableMap.copyOf(methods); } - /** - * The descriptor for the service. - */ - public ServiceDescriptor getServiceDescriptor() { - return serviceDescriptor; + /** Simple name of the service. It is not an absolute path. */ + public String getName() { + return name; } + @ExperimentalApi("https://github.com/grpc/grpc-java/issues/1774") public Collection> getMethods() { return methods.values(); } @@ -71,24 +70,21 @@ public final class ServerServiceDefinition { /** * Look up a method by its fully qualified name. * - * @param methodName the fully qualified name without leading slash. E.g., "com.foo.Foo/Bar" + * @param name the fully qualified name without leading slash. E.g., "com.foo.Foo/Bar" */ @Internal - public ServerMethodDefinition getMethod(String methodName) { - return methods.get(methodName); + public ServerMethodDefinition getMethod(String name) { + return methods.get(name); } - /** - * Builder for constructing Service instances. - */ + /** Builder for constructing Service instances. */ public static final class Builder { - - private final ServiceDescriptor serviceDescriptor; + private final String serviceName; private final Map> methods = new HashMap>(); - private Builder(ServiceDescriptor serviceDescriptor) { - this.serviceDescriptor = serviceDescriptor; + private Builder(String serviceName) { + this.serviceName = serviceName; } /** @@ -97,6 +93,7 @@ public final class ServerServiceDefinition { * @param method the {@link MethodDescriptor} of this method. * @param handler handler for incoming calls */ + @ExperimentalApi("https://github.com/grpc/grpc-java/issues/1774") public Builder addMethod( MethodDescriptor method, ServerCallHandler handler) { return addMethod(ServerMethodDefinition.create( @@ -105,97 +102,22 @@ public final class ServerServiceDefinition { } /** Add a method to be supported by the service. */ + @ExperimentalApi("https://github.com/grpc/grpc-java/issues/1774") public Builder addMethod(ServerMethodDefinition def) { MethodDescriptor method = def.getMethodDescriptor(); checkArgument( - serviceDescriptor.getName().equals( - MethodDescriptor.extractFullServiceName(method.getFullMethodName())), + serviceName.equals(MethodDescriptor.extractFullServiceName(method.getFullMethodName())), "Service name mismatch. Expected service name: '%s'. Actual method name: '%s'.", - serviceDescriptor.getName(), method.getFullMethodName()); + this.serviceName, method.getFullMethodName()); String name = method.getFullMethodName(); checkState(!methods.containsKey(name), "Method by same name already registered: %s", name); methods.put(name, def); return this; } - /** - * Construct new ServerServiceDefinition. - */ + /** Construct new ServerServiceDefinition. */ public ServerServiceDefinition build() { - Map> tmpMethods = - new HashMap>(methods); - for (MethodDescriptor descriptorMethod : serviceDescriptor.getMethods()) { - ServerMethodDefinition removed = tmpMethods.remove( - descriptorMethod.getFullMethodName()); - if (removed == null) { - throw new IllegalStateException( - "No method bound for descriptor entry " + descriptorMethod.getFullMethodName()); - } - if (removed.getMethodDescriptor() != descriptorMethod) { - throw new IllegalStateException( - "Bound method for " + descriptorMethod.getFullMethodName() - + " not same instance as method in service descriptor"); - } - } - if (tmpMethods.size() > 0) { - throw new IllegalStateException( - "No entry in descriptor matching bound method " - + tmpMethods.values().iterator().next().getMethodDescriptor().getFullMethodName()); - } - return new ServerServiceDefinition(serviceDescriptor, methods); - } - } - - /** - * Definition of a method exposed by a {@link Server}. - */ - public static final class ServerMethodDefinition { - - private final MethodDescriptor method; - private final ServerCallHandler handler; - - private ServerMethodDefinition(MethodDescriptor method, - ServerCallHandler handler) { - this.method = method; - this.handler = handler; - } - - /** - * Create a new instance. - * - * @param method the {@link MethodDescriptor} for this method. - * @param handler to dispatch calls to. - * @return a new instance. - */ - public static ServerMethodDefinition create( - MethodDescriptor method, - ServerCallHandler handler) { - return new ServerMethodDefinition(method, handler); - } - - /** - * The {@code MethodDescriptor} for this method. - */ - public MethodDescriptor getMethodDescriptor() { - return method; - } - - /** - * Handler for incoming calls. - */ - public ServerCallHandler getServerCallHandler() { - return handler; - } - - /** - * Create a new method definition with a different call handler. - * - * @param handler to bind to a cloned instance of this. - * @return a cloned instance of this with the new handler bound. - */ - public ServerMethodDefinition withServerCallHandler( - ServerCallHandler handler) { - return new ServerMethodDefinition(method, handler); + return new ServerServiceDefinition(serviceName, methods); } } } diff --git a/core/src/main/java/io/grpc/ServiceDescriptor.java b/core/src/main/java/io/grpc/ServiceDescriptor.java deleted file mode 100644 index 3d0fc7167a..0000000000 --- a/core/src/main/java/io/grpc/ServiceDescriptor.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright 2016, Google Inc. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * Neither the name of Google Inc. nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -package io.grpc; - -import com.google.common.base.Preconditions; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; - -/** - * Descriptor for a service. - */ -public class ServiceDescriptor { - - private final String name; - private final List> methods; - - public ServiceDescriptor(String name, MethodDescriptor... methods) { - this(name, Arrays.asList(methods)); - } - - public ServiceDescriptor(String name, List> methods) { - this.name = Preconditions.checkNotNull(name); - this.methods = Collections.unmodifiableList(new ArrayList>(methods)); - } - - /** Simple name of the service. It is not an absolute path. */ - public String getName() { - return name; - } - - /** - * A list of {@link MethodDescriptor} instances describing the methods exposed by the service. - */ - public List> getMethods() { - return methods; - } -} diff --git a/core/src/main/java/io/grpc/internal/AbstractServerImplBuilder.java b/core/src/main/java/io/grpc/internal/AbstractServerImplBuilder.java index 6a7a8ad0ed..b82a917949 100644 --- a/core/src/main/java/io/grpc/internal/AbstractServerImplBuilder.java +++ b/core/src/main/java/io/grpc/internal/AbstractServerImplBuilder.java @@ -42,8 +42,8 @@ import io.grpc.DecompressorRegistry; import io.grpc.HandlerRegistry; import io.grpc.Internal; import io.grpc.ServerBuilder; +import io.grpc.ServerMethodDefinition; import io.grpc.ServerServiceDefinition; -import io.grpc.ServerServiceDefinition.ServerMethodDefinition; import java.util.concurrent.Executor; @@ -58,10 +58,7 @@ public abstract class AbstractServerImplBuilder { private static final HandlerRegistry EMPTY_FALLBACK_REGISTRY = new HandlerRegistry() { - - @Override - public ServerMethodDefinition lookupMethod(String methodName, - @Nullable String authority) { + @Override public ServerMethodDefinition lookupMethod(String method, String authority) { return null; } }; diff --git a/core/src/main/java/io/grpc/internal/InternalHandlerRegistry.java b/core/src/main/java/io/grpc/internal/InternalHandlerRegistry.java index 24c755da12..50f4ce9a25 100644 --- a/core/src/main/java/io/grpc/internal/InternalHandlerRegistry.java +++ b/core/src/main/java/io/grpc/internal/InternalHandlerRegistry.java @@ -33,8 +33,8 @@ package io.grpc.internal; import com.google.common.collect.ImmutableMap; +import io.grpc.ServerMethodDefinition; import io.grpc.ServerServiceDefinition; -import io.grpc.ServerServiceDefinition.ServerMethodDefinition; import java.util.HashMap; import javax.annotation.Nullable; @@ -57,7 +57,7 @@ final class InternalHandlerRegistry { new HashMap(); Builder addService(ServerServiceDefinition service) { - services.put(service.getServiceDescriptor().getName(), service); + services.put(service.getName(), service); return this; } diff --git a/core/src/main/java/io/grpc/internal/ServerCallImpl.java b/core/src/main/java/io/grpc/internal/ServerCallImpl.java index 6e0707b329..1854858ec4 100644 --- a/core/src/main/java/io/grpc/internal/ServerCallImpl.java +++ b/core/src/main/java/io/grpc/internal/ServerCallImpl.java @@ -58,7 +58,7 @@ import java.io.IOException; import java.io.InputStream; import java.util.List; -final class ServerCallImpl extends ServerCall { +final class ServerCallImpl extends ServerCall { private final ServerStream stream; private final MethodDescriptor method; private final Context.CancellableContext context; @@ -196,11 +196,6 @@ final class ServerCallImpl extends ServerCall { return stream.attributes(); } - @Override - public MethodDescriptor getMethodDescriptor() { - return method; - } - /** * All of these callbacks are assumed to called on an application thread, and the caller is * responsible for handling thrown exceptions. diff --git a/core/src/main/java/io/grpc/internal/ServerImpl.java b/core/src/main/java/io/grpc/internal/ServerImpl.java index 9d0422d516..49db3ea88b 100644 --- a/core/src/main/java/io/grpc/internal/ServerImpl.java +++ b/core/src/main/java/io/grpc/internal/ServerImpl.java @@ -47,7 +47,7 @@ import io.grpc.DecompressorRegistry; import io.grpc.HandlerRegistry; import io.grpc.Metadata; import io.grpc.ServerCall; -import io.grpc.ServerServiceDefinition.ServerMethodDefinition; +import io.grpc.ServerMethodDefinition; import io.grpc.Status; import java.io.IOException; @@ -404,8 +404,8 @@ public final class ServerImpl extends io.grpc.Server { ServerCallImpl call = new ServerCallImpl( stream, methodDef.getMethodDescriptor(), headers, context, decompressorRegistry, compressorRegistry); - ServerCall.Listener listener = - methodDef.getServerCallHandler().startCall(call, headers); + ServerCall.Listener listener = methodDef.getServerCallHandler() + .startCall(methodDef.getMethodDescriptor(), call, headers); if (listener == null) { throw new NullPointerException( "startCall() returned a null listener for method " + fullMethodName); diff --git a/core/src/main/java/io/grpc/util/MutableHandlerRegistry.java b/core/src/main/java/io/grpc/util/MutableHandlerRegistry.java index 5d3f52eebb..9a57f98117 100644 --- a/core/src/main/java/io/grpc/util/MutableHandlerRegistry.java +++ b/core/src/main/java/io/grpc/util/MutableHandlerRegistry.java @@ -34,8 +34,8 @@ package io.grpc.util; import io.grpc.ExperimentalApi; import io.grpc.HandlerRegistry; import io.grpc.MethodDescriptor; +import io.grpc.ServerMethodDefinition; import io.grpc.ServerServiceDefinition; -import io.grpc.ServerServiceDefinition.ServerMethodDefinition; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; @@ -57,11 +57,11 @@ public final class MutableHandlerRegistry extends HandlerRegistry { @Nullable public ServerServiceDefinition addService(ServerServiceDefinition service) { - return services.put(service.getServiceDescriptor().getName(), service); + return services.put(service.getName(), service); } public boolean removeService(ServerServiceDefinition service) { - return services.remove(service.getServiceDescriptor().getName(), service); + return services.remove(service.getName(), service); } /** diff --git a/core/src/test/java/io/grpc/ContextsTest.java b/core/src/test/java/io/grpc/ContextsTest.java index 8ea6271492..21ab32581b 100644 --- a/core/src/test/java/io/grpc/ContextsTest.java +++ b/core/src/test/java/io/grpc/ContextsTest.java @@ -66,7 +66,7 @@ public class ContextsTest { @SuppressWarnings("unchecked") private MethodDescriptor method = mock(MethodDescriptor.class); @SuppressWarnings("unchecked") - private ServerCall call = mock(ServerCall.class); + private ServerCall call = mock(ServerCall.class); private Metadata headers = new Metadata(); @Test @@ -101,11 +101,11 @@ public class ContextsTest { methodCalls.add(5); } }; - ServerCall.Listener wrapped = interceptCall(uniqueContext, call, headers, + ServerCall.Listener wrapped = interceptCall(uniqueContext, method, call, headers, new ServerCallHandler() { @Override - public ServerCall.Listener startCall( - ServerCall call, Metadata headers) { + public ServerCall.Listener startCall(MethodDescriptor method, + ServerCall call, Metadata headers) { assertSame(ContextsTest.this.method, method); assertSame(ContextsTest.this.call, call); assertSame(ContextsTest.this.headers, headers); @@ -128,10 +128,10 @@ public class ContextsTest { public void interceptCall_restoresIfNextThrows() { Context origContext = Context.current(); try { - interceptCall(uniqueContext, call, headers, new ServerCallHandler() { + interceptCall(uniqueContext, method, call, headers, new ServerCallHandler() { @Override - public ServerCall.Listener startCall( - ServerCall call, Metadata headers) { + public ServerCall.Listener startCall(MethodDescriptor method, + ServerCall call, Metadata headers) { throw new RuntimeException(); } }); @@ -165,11 +165,11 @@ public class ContextsTest { throw new RuntimeException(); } }; - ServerCall.Listener wrapped = interceptCall(uniqueContext, call, headers, + ServerCall.Listener wrapped = interceptCall(uniqueContext, method, call, headers, new ServerCallHandler() { @Override - public ServerCall.Listener startCall( - ServerCall call, Metadata headers) { + public ServerCall.Listener startCall(MethodDescriptor method, + ServerCall call, Metadata headers) { return listener; } }); diff --git a/core/src/test/java/io/grpc/ServerInterceptorsTest.java b/core/src/test/java/io/grpc/ServerInterceptorsTest.java index b3e8fa5188..af2f0d12cf 100644 --- a/core/src/test/java/io/grpc/ServerInterceptorsTest.java +++ b/core/src/test/java/io/grpc/ServerInterceptorsTest.java @@ -45,7 +45,6 @@ import static org.mockito.Mockito.verifyZeroInteractions; import io.grpc.MethodDescriptor.Marshaller; import io.grpc.MethodDescriptor.MethodType; import io.grpc.ServerCall.Listener; -import io.grpc.ServerServiceDefinition.ServerMethodDefinition; import org.junit.After; import org.junit.Before; @@ -65,40 +64,34 @@ import java.util.List; /** Unit tests for {@link ServerInterceptors}. */ @RunWith(JUnit4.class) public class ServerInterceptorsTest { - @Mock - private Marshaller requestMarshaller; - - @Mock - private Marshaller responseMarshaller; - - @Mock - private ServerCallHandler handler; - - @Mock - private ServerCall.Listener listener; - - private MethodDescriptor flowMethod; - - @Mock - private ServerCall call; - - private ServerServiceDefinition serviceDefinition; - - private final Metadata headers = new Metadata(); + @SuppressWarnings("unchecked") + private Marshaller requestMarshaller = mock(Marshaller.class); + @SuppressWarnings("unchecked") + private Marshaller responseMarshaller = mock(Marshaller.class); + @SuppressWarnings("unchecked") + private ServerCallHandler handler = mock(ServerCallHandler.class); + @Mock private ServerCall.Listener listener; + private MethodDescriptor method = MethodDescriptor.create( + MethodType.UNKNOWN, + "someRandom/Name", + requestMarshaller, + responseMarshaller); + @Mock private ServerCall call; + private ServerServiceDefinition serviceDefinition = ServerServiceDefinition.builder("basic") + .addMethod( + MethodDescriptor.create( + MethodType.UNKNOWN, "basic/flow", requestMarshaller, responseMarshaller), + handler).build(); + private Metadata headers = new Metadata(); /** Set up for test. */ @Before public void setUp() { MockitoAnnotations.initMocks(this); - flowMethod = MethodDescriptor.create( - MethodType.UNKNOWN, "basic/flow", requestMarshaller, responseMarshaller); - Mockito.when(handler.startCall( - Mockito.>any(), Mockito.any())) + Mockito.>any(), + Mockito.>any(), Mockito.any())) .thenReturn(listener); - - serviceDefinition = ServerServiceDefinition.builder(new ServiceDescriptor("basic", flowMethod)) - .addMethod(flowMethod, handler).build(); } /** Final checks for all tests. */ @@ -136,16 +129,17 @@ public class ServerInterceptorsTest { ServerServiceDefinition intercepted = ServerInterceptors.intercept(serviceDefinition, Arrays.asList(interceptor)); assertSame(listener, - getSoleMethod(intercepted).getServerCallHandler().startCall(call, headers)); - verify(interceptor).interceptCall(same(call), same(headers), anyCallHandler()); - verify(handler).startCall(call, headers); + getSoleMethod(intercepted).getServerCallHandler().startCall(method, call, headers)); + verify(interceptor).interceptCall( + same(method), same(call), same(headers), anyCallHandler()); + verify(handler).startCall(method, call, headers); verifyNoMoreInteractions(interceptor, handler); assertSame(listener, - getSoleMethod(intercepted).getServerCallHandler().startCall(call, headers)); + getSoleMethod(intercepted).getServerCallHandler().startCall(method, call, headers)); verify(interceptor, times(2)) - .interceptCall(same(call), same(headers), anyCallHandler()); - verify(handler, times(2)).startCall(call, headers); + .interceptCall(same(method), same(call), same(headers), anyCallHandler()); + verify(handler, times(2)).startCall(method, call, headers); verifyNoMoreInteractions(interceptor, handler); } @@ -153,22 +147,22 @@ public class ServerInterceptorsTest { public void correctHandlerCalled() { @SuppressWarnings("unchecked") ServerCallHandler handler2 = mock(ServerCallHandler.class); - MethodDescriptor flowMethod2 = MethodDescriptor - .create(MethodType.UNKNOWN, "basic/flow2", - requestMarshaller, responseMarshaller); - serviceDefinition = ServerServiceDefinition.builder( - new ServiceDescriptor("basic", flowMethod, flowMethod2)) - .addMethod(flowMethod, handler) - .addMethod(flowMethod2, handler2).build(); + serviceDefinition = ServerServiceDefinition.builder("basic") + .addMethod(MethodDescriptor.create(MethodType.UNKNOWN, "basic/flow", + requestMarshaller, responseMarshaller), handler) + .addMethod(MethodDescriptor.create(MethodType.UNKNOWN, "basic/flow2", + requestMarshaller, responseMarshaller), handler2).build(); ServerServiceDefinition intercepted = ServerInterceptors.intercept( serviceDefinition, Arrays.asList(new NoopInterceptor())); - getMethod(intercepted, "basic/flow").getServerCallHandler().startCall(call, headers); - verify(handler).startCall(call, headers); + getMethod(intercepted, "basic/flow").getServerCallHandler().startCall( + method, call, headers); + verify(handler).startCall(method, call, headers); verifyNoMoreInteractions(handler); - verifyNoMoreInteractions(handler2); + verifyZeroInteractions(handler2); - getMethod(intercepted, "basic/flow2").getServerCallHandler().startCall(call, headers); - verify(handler2).startCall(call, headers); + getMethod(intercepted, "basic/flow2").getServerCallHandler().startCall( + method, call, headers); + verify(handler2).startCall(method, call, headers); verifyNoMoreInteractions(handler); verifyNoMoreInteractions(handler2); } @@ -178,19 +172,20 @@ public class ServerInterceptorsTest { ServerInterceptor interceptor = new ServerInterceptor() { @Override public ServerCall.Listener interceptCall( - ServerCall call, + MethodDescriptor method, + ServerCall call, Metadata headers, ServerCallHandler next) { // Calling next twice is permitted, although should only rarely be useful. - assertSame(listener, next.startCall(call, headers)); - return next.startCall(call, headers); + assertSame(listener, next.startCall(method, call, headers)); + return next.startCall(method, call, headers); } }; ServerServiceDefinition intercepted = ServerInterceptors.intercept(serviceDefinition, interceptor); assertSame(listener, - getSoleMethod(intercepted).getServerCallHandler().startCall(call, headers)); - verify(handler, times(2)).startCall(same(call), same(headers)); + getSoleMethod(intercepted).getServerCallHandler().startCall(method, call, headers)); + verify(handler, times(2)).startCall(same(method), same(call), same(headers)); verifyNoMoreInteractions(handler); } @@ -200,7 +195,8 @@ public class ServerInterceptorsTest { handler = new ServerCallHandler() { @Override public ServerCall.Listener startCall( - ServerCall call, + MethodDescriptor method, + ServerCall call, Metadata headers) { order.add("handler"); return listener; @@ -209,30 +205,32 @@ public class ServerInterceptorsTest { ServerInterceptor interceptor1 = new ServerInterceptor() { @Override public ServerCall.Listener interceptCall( - ServerCall call, + MethodDescriptor method, + ServerCall call, Metadata headers, ServerCallHandler next) { order.add("i1"); - return next.startCall(call, headers); + return next.startCall(method, call, headers); } }; ServerInterceptor interceptor2 = new ServerInterceptor() { @Override public ServerCall.Listener interceptCall( - ServerCall call, + MethodDescriptor method, + ServerCall call, Metadata headers, ServerCallHandler next) { order.add("i2"); - return next.startCall(call, headers); + return next.startCall(method, call, headers); } }; - ServerServiceDefinition serviceDefinition = ServerServiceDefinition.builder( - new ServiceDescriptor("basic", flowMethod)) - .addMethod(flowMethod, handler).build(); + ServerServiceDefinition serviceDefinition = ServerServiceDefinition.builder("basic") + .addMethod(MethodDescriptor.create(MethodType.UNKNOWN, "basic/flow", + requestMarshaller, responseMarshaller), handler).build(); ServerServiceDefinition intercepted = ServerInterceptors.intercept( serviceDefinition, Arrays.asList(interceptor1, interceptor2)); assertSame(listener, - getSoleMethod(intercepted).getServerCallHandler().startCall(call, headers)); + getSoleMethod(intercepted).getServerCallHandler().startCall(method, call, headers)); assertEquals(Arrays.asList("i2", "i1", "handler"), order); } @@ -242,7 +240,8 @@ public class ServerInterceptorsTest { handler = new ServerCallHandler() { @Override public ServerCall.Listener startCall( - ServerCall call, + MethodDescriptor method, + ServerCall call, Metadata headers) { order.add("handler"); return listener; @@ -251,37 +250,41 @@ public class ServerInterceptorsTest { ServerInterceptor interceptor1 = new ServerInterceptor() { @Override public ServerCall.Listener interceptCall( - ServerCall call, + MethodDescriptor method, + ServerCall call, Metadata headers, ServerCallHandler next) { order.add("i1"); - return next.startCall(call, headers); + return next.startCall(method, call, headers); } }; ServerInterceptor interceptor2 = new ServerInterceptor() { @Override public ServerCall.Listener interceptCall( - ServerCall call, + MethodDescriptor method, + ServerCall call, Metadata headers, ServerCallHandler next) { order.add("i2"); - return next.startCall(call, headers); + return next.startCall(method, call, headers); } }; - ServerServiceDefinition serviceDefinition = ServerServiceDefinition.builder( - new ServiceDescriptor("basic", flowMethod)) - .addMethod(flowMethod, handler).build(); + ServerServiceDefinition serviceDefinition = ServerServiceDefinition.builder("basic") + .addMethod(MethodDescriptor.create(MethodType.UNKNOWN, "basic/flow", + requestMarshaller, responseMarshaller), handler).build(); ServerServiceDefinition intercepted = ServerInterceptors.interceptForward( serviceDefinition, interceptor1, interceptor2); assertSame(listener, - getSoleMethod(intercepted).getServerCallHandler().startCall(call, headers)); + getSoleMethod(intercepted).getServerCallHandler().startCall(method, call, headers)); assertEquals(Arrays.asList("i1", "i2", "handler"), order); } @Test public void argumentsPassed() { + final MethodDescriptor method2 = MethodDescriptor.create( + MethodType.UNKNOWN, "someOtherRandom/Method", requestMarshaller, responseMarshaller); @SuppressWarnings("unchecked") - final ServerCall call2 = mock(ServerCall.class); + final ServerCall call2 = mock(ServerCall.class); @SuppressWarnings("unchecked") final ServerCall.Listener listener2 = mock(ServerCall.Listener.class); @@ -289,20 +292,22 @@ public class ServerInterceptorsTest { @SuppressWarnings("unchecked") // Lot's of casting for no benefit. Not intended use. @Override public ServerCall.Listener interceptCall( - ServerCall call, + MethodDescriptor methodDescriptor, + ServerCall call, Metadata headers, ServerCallHandler next) { + assertSame(method, methodDescriptor); assertSame(call, ServerInterceptorsTest.this.call); assertSame(listener, - next.startCall((ServerCall)call2, headers)); + next.startCall((MethodDescriptor)method2, (ServerCall)call2, headers)); return (ServerCall.Listener) listener2; } }; ServerServiceDefinition intercepted = ServerInterceptors.intercept( serviceDefinition, Arrays.asList(interceptor)); assertSame(listener2, - getSoleMethod(intercepted).getServerCallHandler().startCall(call, headers)); - verify(handler).startCall(call2, headers); + getSoleMethod(intercepted).getServerCallHandler().startCall(method, call, headers)); + verify(handler).startCall(method2, call2, headers); } @Test @@ -323,7 +328,8 @@ public class ServerInterceptorsTest { ServerCallHandler handler2 = new ServerCallHandler() { @Override - public Listener startCall(final ServerCall call, + public Listener startCall(final MethodDescriptor method, + final ServerCall call, final Metadata headers) { return new Listener() { @Override @@ -335,20 +341,20 @@ public class ServerInterceptorsTest { } }; - MethodDescriptor wrappedMethod = MethodDescriptor - .create(MethodType.UNKNOWN, "basic/wrapped", - marshaller, marshaller); - ServerServiceDefinition serviceDef = ServerServiceDefinition.builder( - new ServiceDescriptor("basic", wrappedMethod)) - .addMethod(wrappedMethod, handler2).build(); + ServerServiceDefinition serviceDef = ServerServiceDefinition.builder("basic") + .addMethod( + MethodDescriptor.create( + MethodType.UNKNOWN, "basic/wrapped", marshaller, marshaller), + handler2).build(); ServerInterceptor interceptor1 = new ServerInterceptor() { @Override - public Listener interceptCall(ServerCall call, + public Listener interceptCall(MethodDescriptor method, + ServerCall call, Metadata headers, ServerCallHandler next) { - ServerCall interceptedCall = new ForwardingServerCall - .SimpleForwardingServerCall(call) { + ServerCall interceptedCall = new ForwardingServerCall + .SimpleForwardingServerCall(call) { @Override public void sendMessage(RespT message) { order.add("i1sendMessage"); @@ -358,7 +364,7 @@ public class ServerInterceptorsTest { }; ServerCall.Listener originalListener = next - .startCall(interceptedCall, headers); + .startCall(method, interceptedCall, headers); return new ForwardingServerCallListener .SimpleForwardingServerCallListener(originalListener) { @Override @@ -373,11 +379,12 @@ public class ServerInterceptorsTest { ServerInterceptor interceptor2 = new ServerInterceptor() { @Override - public Listener interceptCall(ServerCall call, + public Listener interceptCall(MethodDescriptor method, + ServerCall call, Metadata headers, ServerCallHandler next) { - ServerCall interceptedCall = new ForwardingServerCall - .SimpleForwardingServerCall(call) { + ServerCall interceptedCall = new ForwardingServerCall + .SimpleForwardingServerCall(call) { @Override public void sendMessage(RespT message) { order.add("i2sendMessage"); @@ -387,7 +394,7 @@ public class ServerInterceptorsTest { }; ServerCall.Listener originalListener = next - .startCall(interceptedCall, headers); + .startCall(method, interceptedCall, headers); return new ForwardingServerCallListener .SimpleForwardingServerCallListener(originalListener) { @Override @@ -407,11 +414,12 @@ public class ServerInterceptorsTest { .intercept(inputStreamMessageService, interceptor2); ServerMethodDefinition serverMethod = (ServerMethodDefinition) intercepted2.getMethod("basic/wrapped"); - ServerCall call2 = mock(ServerCall.class); + MethodDescriptor method2 = serverMethod.getMethodDescriptor(); + ServerCall call2 = mock(ServerCall.class); byte[] bytes = {}; serverMethod .getServerCallHandler() - .startCall(call2, headers) + .startCall(method2, call2, headers) .onMessage(new ByteArrayInputStream(bytes)); assertEquals( Arrays.asList("i2onMessage", "i1onMessage", "handler", "i1sendMessage", "i2sendMessage"), @@ -440,10 +448,11 @@ public class ServerInterceptorsTest { private static class NoopInterceptor implements ServerInterceptor { @Override public ServerCall.Listener interceptCall( - ServerCall call, + MethodDescriptor method, + ServerCall call, Metadata headers, ServerCallHandler next) { - return next.startCall(call, headers); + return next.startCall(method, call, headers); } } diff --git a/core/src/test/java/io/grpc/internal/ServerImplTest.java b/core/src/test/java/io/grpc/internal/ServerImplTest.java index e8a962cc7b..a9ea14144b 100644 --- a/core/src/test/java/io/grpc/internal/ServerImplTest.java +++ b/core/src/test/java/io/grpc/internal/ServerImplTest.java @@ -63,7 +63,6 @@ import io.grpc.MethodDescriptor.MethodType; import io.grpc.ServerCall; import io.grpc.ServerCallHandler; import io.grpc.ServerServiceDefinition; -import io.grpc.ServiceDescriptor; import io.grpc.Status; import io.grpc.StringMarshaller; import io.grpc.util.MutableHandlerRegistry; @@ -312,20 +311,19 @@ public class ServerImplTest { public void basicExchangeSuccessful() throws Exception { final Metadata.Key metadataKey = Metadata.Key.of("inception", Metadata.ASCII_STRING_MARSHALLER); - final AtomicReference> callReference - = new AtomicReference>(); - MethodDescriptor method = MethodDescriptor.create( - MethodType.UNKNOWN, "Waiter/serve", STRING_MARSHALLER, INTEGER_MARSHALLER); - fallbackRegistry.addService(ServerServiceDefinition.builder( - new ServiceDescriptor("Waiter", method)) + final AtomicReference> callReference + = new AtomicReference>(); + fallbackRegistry.addService(ServerServiceDefinition.builder("Waiter") .addMethod( - method, + MethodDescriptor.create( + MethodType.UNKNOWN, "Waiter/serve", STRING_MARSHALLER, INTEGER_MARSHALLER), new ServerCallHandler() { @Override public ServerCall.Listener startCall( - ServerCall call, + MethodDescriptor method, + ServerCall call, Metadata headers) { - assertEquals("Waiter/serve", call.getMethodDescriptor().getFullMethodName()); + assertEquals("Waiter/serve", method.getFullMethodName()); assertNotNull(call); assertNotNull(headers); assertEquals("value", headers.get(metadataKey)); @@ -343,7 +341,7 @@ public class ServerImplTest { assertNotNull(streamListener); executeBarrier(executor).await(); - ServerCall call = callReference.get(); + ServerCall call = callReference.get(); assertNotNull(call); String order = "Lots of pizza, please"; @@ -389,16 +387,15 @@ public class ServerImplTest { public void exceptionInStartCallPropagatesToStream() throws Exception { CyclicBarrier barrier = executeBarrier(executor); final Status status = Status.ABORTED.withDescription("Oh, no!"); - MethodDescriptor method = MethodDescriptor - .create(MethodType.UNKNOWN, "Waiter/serve", - STRING_MARSHALLER, INTEGER_MARSHALLER); - fallbackRegistry.addService(ServerServiceDefinition.builder( - new ServiceDescriptor("Waiter", method)) - .addMethod(method, + fallbackRegistry.addService(ServerServiceDefinition.builder("Waiter") + .addMethod( + MethodDescriptor.create(MethodType.UNKNOWN, "Waiter/serve", + STRING_MARSHALLER, INTEGER_MARSHALLER), new ServerCallHandler() { @Override public ServerCall.Listener startCall( - ServerCall call, + MethodDescriptor method, + ServerCall call, Metadata headers) { throw status.asRuntimeException(); } @@ -500,16 +497,15 @@ public class ServerImplTest { @Test public void testCallContextIsBoundInListenerCallbacks() throws Exception { - MethodDescriptor method = MethodDescriptor.create( - MethodType.UNKNOWN, "Waiter/serve", STRING_MARSHALLER, INTEGER_MARSHALLER); - fallbackRegistry.addService(ServerServiceDefinition.builder( - new ServiceDescriptor("Waiter", method)) + fallbackRegistry.addService(ServerServiceDefinition.builder("Waiter") .addMethod( - method, + MethodDescriptor.create( + MethodType.UNKNOWN, "Waiter/serve", STRING_MARSHALLER, INTEGER_MARSHALLER), new ServerCallHandler() { @Override public ServerCall.Listener startCall( - ServerCall call, + MethodDescriptor method, + ServerCall call, Metadata headers) { // Check that the current context is a descendant of SERVER_CONTEXT final Context initial = Context.current(); @@ -586,17 +582,17 @@ public class ServerImplTest { } }; - final AtomicReference> callReference - = new AtomicReference>(); - MethodDescriptor method = MethodDescriptor.create( - MethodType.UNKNOWN, "Waiter/serve", STRING_MARSHALLER, INTEGER_MARSHALLER); - fallbackRegistry.addService(ServerServiceDefinition.builder( - new ServiceDescriptor("Waiter", method)) - .addMethod(method, + final AtomicReference> callReference + = new AtomicReference>(); + fallbackRegistry.addService(ServerServiceDefinition.builder("Waiter") + .addMethod( + MethodDescriptor.create( + MethodType.UNKNOWN, "Waiter/serve", STRING_MARSHALLER, INTEGER_MARSHALLER), new ServerCallHandler() { @Override public ServerCall.Listener startCall( - ServerCall call, + MethodDescriptor method, + ServerCall call, Metadata headers) { callReference.set(call); return callListener; @@ -659,7 +655,7 @@ public class ServerImplTest { MethodDescriptor method1 = MethodDescriptor.create( MethodType.UNKNOWN, "Service1/Method1", STRING_MARSHALLER, INTEGER_MARSHALLER); registry = new InternalHandlerRegistry.Builder() - .addService(ServerServiceDefinition.builder(new ServiceDescriptor("Service1", method1)) + .addService(ServerServiceDefinition.builder("Service1") .addMethod(method1, callHandler).build()) .build(); transportServer = new SimpleServer(); @@ -675,8 +671,8 @@ public class ServerImplTest { // registry. transportListener.streamCreated(stream, "Service1/Method2", new Metadata()); - verify(callHandler, timeout(2000)).startCall(Matchers.>anyObject(), - Matchers.anyObject()); + verify(callHandler, timeout(2000)).startCall(same(method1), + Matchers.>anyObject(), Matchers.anyObject()); verify(fallbackRegistry, timeout(2000)).lookupMethod("Service1/Method2", null); verifyNoMoreInteractions(callHandler); verifyNoMoreInteractions(fallbackRegistry); diff --git a/core/src/test/java/io/grpc/util/MutableHandlerRegistryTest.java b/core/src/test/java/io/grpc/util/MutableHandlerRegistryTest.java index 4ae126e3fa..d3e9b71a34 100644 --- a/core/src/test/java/io/grpc/util/MutableHandlerRegistryTest.java +++ b/core/src/test/java/io/grpc/util/MutableHandlerRegistryTest.java @@ -31,90 +31,68 @@ package io.grpc.util; +import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.collect.Iterables.getOnlyElement; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; import io.grpc.MethodDescriptor.Marshaller; import io.grpc.MethodDescriptor.MethodType; import io.grpc.MethodDescriptor; import io.grpc.ServerCallHandler; +import io.grpc.ServerMethodDefinition; import io.grpc.ServerServiceDefinition; -import io.grpc.ServerServiceDefinition.ServerMethodDefinition; -import io.grpc.ServiceDescriptor; import org.junit.After; -import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; -import org.mockito.Mock; import org.mockito.Mockito; -import org.mockito.MockitoAnnotations; /** Unit tests for {@link MutableHandlerRegistry}. */ @RunWith(JUnit4.class) public class MutableHandlerRegistryTest { private MutableHandlerRegistry registry = new MutableHandlerRegistry(); - - @Mock - private Marshaller requestMarshaller; - - @Mock - private Marshaller responseMarshaller; - - @Mock - private ServerCallHandler flowHandler; - - @Mock - private ServerCallHandler coupleHandler; - - @Mock - private ServerCallHandler fewHandler; - - @Mock - private ServerCallHandler otherFlowHandler; - - private ServerServiceDefinition basicServiceDefinition; - private ServerServiceDefinition multiServiceDefinition; - + @SuppressWarnings("unchecked") + private Marshaller requestMarshaller = mock(Marshaller.class); + @SuppressWarnings("unchecked") + private Marshaller responseMarshaller = mock(Marshaller.class); + @SuppressWarnings("unchecked") + private ServerCallHandler handler = mock(ServerCallHandler.class); + private ServerServiceDefinition basicServiceDefinition = ServerServiceDefinition.builder("basic") + .addMethod( + MethodDescriptor.create(MethodType.UNKNOWN, "basic/flow", + requestMarshaller, responseMarshaller), + handler).build(); @SuppressWarnings("rawtypes") - private ServerMethodDefinition flowMethodDefinition; - - @Before - public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); - MethodDescriptor flowMethod = MethodDescriptor - .create(MethodType.UNKNOWN, "basic/flow", requestMarshaller, responseMarshaller); - basicServiceDefinition = ServerServiceDefinition.builder( - new ServiceDescriptor("basic", flowMethod)) - .addMethod(flowMethod, flowHandler) - .build(); - - MethodDescriptor coupleMethod = MethodDescriptor - .create(MethodType.UNKNOWN, "multi/couple", requestMarshaller, responseMarshaller); - MethodDescriptor fewMethod = MethodDescriptor - .create(MethodType.UNKNOWN, "multi/few", requestMarshaller, responseMarshaller); - multiServiceDefinition = ServerServiceDefinition.builder( - new ServiceDescriptor("multi", coupleMethod, fewMethod)) - .addMethod(coupleMethod, coupleHandler) - .addMethod(fewMethod, fewHandler) - .build(); - - flowMethodDefinition = getOnlyElement(basicServiceDefinition.getMethods()); - } + private ServerMethodDefinition flowMethodDefinition = + getOnlyElement(basicServiceDefinition.getMethods()); + private ServerServiceDefinition multiServiceDefinition = ServerServiceDefinition.builder("multi") + .addMethod( + MethodDescriptor.create(MethodType.UNKNOWN, "multi/couple", + requestMarshaller, responseMarshaller), + handler) + .addMethod( + MethodDescriptor.create(MethodType.UNKNOWN, "multi/few", + requestMarshaller, responseMarshaller), + handler).build(); + @SuppressWarnings("rawtypes") + private ServerMethodDefinition coupleMethodDefinition = + checkNotNull(multiServiceDefinition.getMethod("multi/couple")); + @SuppressWarnings("rawtypes") + private ServerMethodDefinition fewMethodDefinition = + checkNotNull(multiServiceDefinition.getMethod("multi/few")); /** Final checks for all tests. */ @After public void makeSureMocksUnused() { Mockito.verifyZeroInteractions(requestMarshaller); Mockito.verifyZeroInteractions(responseMarshaller); - Mockito.verifyNoMoreInteractions(flowHandler); - Mockito.verifyNoMoreInteractions(coupleHandler); - Mockito.verifyNoMoreInteractions(fewHandler); + Mockito.verifyZeroInteractions(handler); } @Test @@ -134,12 +112,12 @@ public class MutableHandlerRegistryTest { assertNull(registry.addService(basicServiceDefinition)); assertNull(registry.addService(multiServiceDefinition)); - ServerCallHandler handler = registry.lookupMethod("basic/flow").getServerCallHandler(); - assertSame(flowHandler, handler); - handler = registry.lookupMethod("multi/couple").getServerCallHandler(); - assertSame(coupleHandler, handler); - handler = registry.lookupMethod("multi/few").getServerCallHandler(); - assertSame(fewHandler, handler); + ServerMethodDefinition method = registry.lookupMethod("basic/flow"); + assertSame(flowMethodDefinition, method); + method = registry.lookupMethod("multi/couple"); + assertSame(coupleMethodDefinition, method); + method = registry.lookupMethod("multi/few"); + assertSame(fewMethodDefinition, method); } @Test @@ -156,11 +134,9 @@ public class MutableHandlerRegistryTest { public void replaceAndLookup() { assertNull(registry.addService(basicServiceDefinition)); assertNotNull(registry.lookupMethod("basic/flow")); - MethodDescriptor anotherMethod = MethodDescriptor - .create(MethodType.UNKNOWN, "basic/another", requestMarshaller, responseMarshaller); - ServerServiceDefinition replaceServiceDefinition = ServerServiceDefinition.builder( - new ServiceDescriptor("basic", anotherMethod)) - .addMethod(anotherMethod, flowHandler).build(); + ServerServiceDefinition replaceServiceDefinition = ServerServiceDefinition.builder("basic") + .addMethod(MethodDescriptor.create(MethodType.UNKNOWN, "basic/another", + requestMarshaller, responseMarshaller), handler).build(); ServerMethodDefinition anotherMethodDefinition = replaceServiceDefinition.getMethod("basic/another"); assertSame(basicServiceDefinition, registry.addService(replaceServiceDefinition)); @@ -191,8 +167,7 @@ public class MutableHandlerRegistryTest { @Test public void removeMissingNameConflictFails() { assertNull(registry.addService(basicServiceDefinition)); - assertFalse(registry.removeService(ServerServiceDefinition.builder( - new ServiceDescriptor("basic")).build())); + assertFalse(registry.removeService(ServerServiceDefinition.builder("basic").build())); } @Test @@ -217,7 +192,6 @@ public class MutableHandlerRegistryTest { public void addReturnsPrevious() { assertNull(registry.addService(basicServiceDefinition)); assertSame(basicServiceDefinition, - registry.addService(ServerServiceDefinition.builder( - new ServiceDescriptor("basic")).build())); + registry.addService(ServerServiceDefinition.builder("basic").build())); } } diff --git a/examples/src/generated/main/grpc/io/grpc/examples/helloworld/GreeterGrpc.java b/examples/src/generated/main/grpc/io/grpc/examples/helloworld/GreeterGrpc.java index 83b78eb6ed..2d8979cbd1 100644 --- a/examples/src/generated/main/grpc/io/grpc/examples/helloworld/GreeterGrpc.java +++ b/examples/src/generated/main/grpc/io/grpc/examples/helloworld/GreeterGrpc.java @@ -237,14 +237,9 @@ public class GreeterGrpc { } } - public static io.grpc.ServiceDescriptor getServiceDescriptor() { - return new io.grpc.ServiceDescriptor(SERVICE_NAME, - METHOD_SAY_HELLO); - } - public static io.grpc.ServerServiceDefinition bindService( final Greeter serviceImpl) { - return io.grpc.ServerServiceDefinition.builder(getServiceDescriptor()) + return io.grpc.ServerServiceDefinition.builder(SERVICE_NAME) .addMethod( METHOD_SAY_HELLO, asyncUnaryCall( diff --git a/examples/src/generated/main/grpc/io/grpc/examples/routeguide/RouteGuideGrpc.java b/examples/src/generated/main/grpc/io/grpc/examples/routeguide/RouteGuideGrpc.java index 17c50abd57..54441187b3 100644 --- a/examples/src/generated/main/grpc/io/grpc/examples/routeguide/RouteGuideGrpc.java +++ b/examples/src/generated/main/grpc/io/grpc/examples/routeguide/RouteGuideGrpc.java @@ -376,17 +376,9 @@ public class RouteGuideGrpc { } } - public static io.grpc.ServiceDescriptor getServiceDescriptor() { - return new io.grpc.ServiceDescriptor(SERVICE_NAME, - METHOD_GET_FEATURE, - METHOD_LIST_FEATURES, - METHOD_RECORD_ROUTE, - METHOD_ROUTE_CHAT); - } - public static io.grpc.ServerServiceDefinition bindService( final RouteGuide serviceImpl) { - return io.grpc.ServerServiceDefinition.builder(getServiceDescriptor()) + return io.grpc.ServerServiceDefinition.builder(SERVICE_NAME) .addMethod( METHOD_GET_FEATURE, asyncUnaryCall( diff --git a/examples/src/main/java/io/grpc/examples/header/HeaderServerInterceptor.java b/examples/src/main/java/io/grpc/examples/header/HeaderServerInterceptor.java index c9e9b4d80f..55f1a62920 100644 --- a/examples/src/main/java/io/grpc/examples/header/HeaderServerInterceptor.java +++ b/examples/src/main/java/io/grpc/examples/header/HeaderServerInterceptor.java @@ -33,6 +33,7 @@ package io.grpc.examples.header; import io.grpc.ForwardingServerCall.SimpleForwardingServerCall; import io.grpc.Metadata; +import io.grpc.MethodDescriptor; import io.grpc.ServerCall; import io.grpc.ServerCallHandler; import io.grpc.ServerInterceptor; @@ -52,11 +53,12 @@ public class HeaderServerInterceptor implements ServerInterceptor { @Override public ServerCall.Listener interceptCall( - ServerCall call, + MethodDescriptor method, + ServerCall call, final Metadata requestHeaders, ServerCallHandler next) { logger.info("header received from client:" + requestHeaders); - return next.startCall(new SimpleForwardingServerCall(call) { + return next.startCall(method, new SimpleForwardingServerCall(call) { @Override public void sendHeaders(Metadata responseHeaders) { responseHeaders.put(customHeadKey, "customRespondValue"); diff --git a/examples/src/main/java/io/grpc/examples/helloworld/HelloJsonServer.java b/examples/src/main/java/io/grpc/examples/helloworld/HelloJsonServer.java index fbad4e1e25..17fb29d633 100644 --- a/examples/src/main/java/io/grpc/examples/helloworld/HelloJsonServer.java +++ b/examples/src/main/java/io/grpc/examples/helloworld/HelloJsonServer.java @@ -113,10 +113,10 @@ public class HelloJsonServer { } private ServerServiceDefinition bindService(final Greeter serviceImpl) { - return io.grpc.ServerServiceDefinition - .builder(GreeterGrpc.getServiceDescriptor()) - .addMethod(HelloJsonClient.HelloJsonStub.METHOD_SAY_HELLO, - asyncUnaryCall( + return io.grpc.ServerServiceDefinition.builder(GreeterGrpc.SERVICE_NAME) + .addMethod( + HelloJsonClient.HelloJsonStub.METHOD_SAY_HELLO, + asyncUnaryCall( new UnaryMethod() { @Override public void invoke( diff --git a/grpclb/src/generated/main/grpc/io/grpc/grpclb/LoadBalancerGrpc.java b/grpclb/src/generated/main/grpc/io/grpc/grpclb/LoadBalancerGrpc.java index cae19bce8b..ff6cb5d1c0 100644 --- a/grpclb/src/generated/main/grpc/io/grpc/grpclb/LoadBalancerGrpc.java +++ b/grpclb/src/generated/main/grpc/io/grpc/grpclb/LoadBalancerGrpc.java @@ -196,14 +196,9 @@ public class LoadBalancerGrpc { } } - public static io.grpc.ServiceDescriptor getServiceDescriptor() { - return new io.grpc.ServiceDescriptor(SERVICE_NAME, - METHOD_BALANCE_LOAD); - } - public static io.grpc.ServerServiceDefinition bindService( final LoadBalancer serviceImpl) { - return io.grpc.ServerServiceDefinition.builder(getServiceDescriptor()) + return io.grpc.ServerServiceDefinition.builder(SERVICE_NAME) .addMethod( METHOD_BALANCE_LOAD, asyncBidiStreamingCall( diff --git a/interop-testing/src/generated/main/grpc/io/grpc/testing/integration/MetricsServiceGrpc.java b/interop-testing/src/generated/main/grpc/io/grpc/testing/integration/MetricsServiceGrpc.java index 5e6e22e25c..1aecf7888f 100644 --- a/interop-testing/src/generated/main/grpc/io/grpc/testing/integration/MetricsServiceGrpc.java +++ b/interop-testing/src/generated/main/grpc/io/grpc/testing/integration/MetricsServiceGrpc.java @@ -277,15 +277,9 @@ public class MetricsServiceGrpc { } } - public static io.grpc.ServiceDescriptor getServiceDescriptor() { - return new io.grpc.ServiceDescriptor(SERVICE_NAME, - METHOD_GET_ALL_GAUGES, - METHOD_GET_GAUGE); - } - public static io.grpc.ServerServiceDefinition bindService( final MetricsService serviceImpl) { - return io.grpc.ServerServiceDefinition.builder(getServiceDescriptor()) + return io.grpc.ServerServiceDefinition.builder(SERVICE_NAME) .addMethod( METHOD_GET_ALL_GAUGES, asyncServerStreamingCall( diff --git a/interop-testing/src/generated/main/grpc/io/grpc/testing/integration/ReconnectServiceGrpc.java b/interop-testing/src/generated/main/grpc/io/grpc/testing/integration/ReconnectServiceGrpc.java index 1359fea66a..5299bab20a 100644 --- a/interop-testing/src/generated/main/grpc/io/grpc/testing/integration/ReconnectServiceGrpc.java +++ b/interop-testing/src/generated/main/grpc/io/grpc/testing/integration/ReconnectServiceGrpc.java @@ -282,15 +282,9 @@ public class ReconnectServiceGrpc { } } - public static io.grpc.ServiceDescriptor getServiceDescriptor() { - return new io.grpc.ServiceDescriptor(SERVICE_NAME, - METHOD_START, - METHOD_STOP); - } - public static io.grpc.ServerServiceDefinition bindService( final ReconnectService serviceImpl) { - return io.grpc.ServerServiceDefinition.builder(getServiceDescriptor()) + return io.grpc.ServerServiceDefinition.builder(SERVICE_NAME) .addMethod( METHOD_START, asyncUnaryCall( diff --git a/interop-testing/src/generated/main/grpc/io/grpc/testing/integration/TestServiceGrpc.java b/interop-testing/src/generated/main/grpc/io/grpc/testing/integration/TestServiceGrpc.java index 6cafe400df..d25e29a91f 100644 --- a/interop-testing/src/generated/main/grpc/io/grpc/testing/integration/TestServiceGrpc.java +++ b/interop-testing/src/generated/main/grpc/io/grpc/testing/integration/TestServiceGrpc.java @@ -464,19 +464,9 @@ public class TestServiceGrpc { } } - public static io.grpc.ServiceDescriptor getServiceDescriptor() { - return new io.grpc.ServiceDescriptor(SERVICE_NAME, - METHOD_EMPTY_CALL, - METHOD_UNARY_CALL, - METHOD_STREAMING_OUTPUT_CALL, - METHOD_STREAMING_INPUT_CALL, - METHOD_FULL_DUPLEX_CALL, - METHOD_HALF_DUPLEX_CALL); - } - public static io.grpc.ServerServiceDefinition bindService( final TestService serviceImpl) { - return io.grpc.ServerServiceDefinition.builder(getServiceDescriptor()) + return io.grpc.ServerServiceDefinition.builder(SERVICE_NAME) .addMethod( METHOD_EMPTY_CALL, asyncUnaryCall( diff --git a/interop-testing/src/generated/main/grpc/io/grpc/testing/integration/UnimplementedServiceGrpc.java b/interop-testing/src/generated/main/grpc/io/grpc/testing/integration/UnimplementedServiceGrpc.java index 0992d00300..64b351dd1f 100644 --- a/interop-testing/src/generated/main/grpc/io/grpc/testing/integration/UnimplementedServiceGrpc.java +++ b/interop-testing/src/generated/main/grpc/io/grpc/testing/integration/UnimplementedServiceGrpc.java @@ -241,14 +241,9 @@ public class UnimplementedServiceGrpc { } } - public static io.grpc.ServiceDescriptor getServiceDescriptor() { - return new io.grpc.ServiceDescriptor(SERVICE_NAME, - METHOD_UNIMPLEMENTED_CALL); - } - public static io.grpc.ServerServiceDefinition bindService( final UnimplementedService serviceImpl) { - return io.grpc.ServerServiceDefinition.builder(getServiceDescriptor()) + return io.grpc.ServerServiceDefinition.builder(SERVICE_NAME) .addMethod( METHOD_UNIMPLEMENTED_CALL, asyncUnaryCall( diff --git a/interop-testing/src/main/java/io/grpc/testing/integration/AbstractInteropTest.java b/interop-testing/src/main/java/io/grpc/testing/integration/AbstractInteropTest.java index 62b1476eb3..2fcc27fe4a 100644 --- a/interop-testing/src/main/java/io/grpc/testing/integration/AbstractInteropTest.java +++ b/interop-testing/src/main/java/io/grpc/testing/integration/AbstractInteropTest.java @@ -111,8 +111,8 @@ public abstract class AbstractInteropTest { public static final Metadata.Key METADATA_KEY = ProtoUtils.keyForProto(Messages.SimpleContext.getDefaultInstance()); - private static final AtomicReference> serverCallCapture = - new AtomicReference>(); + private static final AtomicReference> serverCallCapture = + new AtomicReference>(); private static final AtomicReference requestHeadersCapture = new AtomicReference(); private static ScheduledExecutorService testServiceExecutor; diff --git a/interop-testing/src/test/java/io/grpc/testing/integration/CascadingTest.java b/interop-testing/src/test/java/io/grpc/testing/integration/CascadingTest.java index 1d969d2b6f..b8115beca6 100644 --- a/interop-testing/src/test/java/io/grpc/testing/integration/CascadingTest.java +++ b/interop-testing/src/test/java/io/grpc/testing/integration/CascadingTest.java @@ -36,6 +36,7 @@ import static org.junit.Assert.fail; import io.grpc.Context; import io.grpc.Metadata; +import io.grpc.MethodDescriptor; import io.grpc.ServerCall; import io.grpc.ServerCallHandler; import io.grpc.ServerInterceptor; @@ -206,7 +207,8 @@ public class CascadingTest { new ServerInterceptor() { @Override public ServerCall.Listener interceptCall( - final ServerCall call, + MethodDescriptor method, + final ServerCall call, Metadata headers, ServerCallHandler next) { // Respond with the headers but nothing else. @@ -262,7 +264,8 @@ public class CascadingTest { new ServerInterceptor() { @Override public ServerCall.Listener interceptCall( - final ServerCall call, + MethodDescriptor method, + final ServerCall call, Metadata headers, ServerCallHandler next) { // Respond with the headers but nothing else. diff --git a/interop-testing/src/test/java/io/grpc/testing/integration/CompressionTest.java b/interop-testing/src/test/java/io/grpc/testing/integration/CompressionTest.java index fa00039a4f..818f842d44 100644 --- a/interop-testing/src/test/java/io/grpc/testing/integration/CompressionTest.java +++ b/interop-testing/src/test/java/io/grpc/testing/integration/CompressionTest.java @@ -278,13 +278,14 @@ public class CompressionTest { private class ServerCompressorInterceptor implements ServerInterceptor { @Override public io.grpc.ServerCall.Listener interceptCall( - ServerCall call, Metadata headers, ServerCallHandler next) { + MethodDescriptor method, ServerCall call, Metadata headers, + ServerCallHandler next) { if (serverEncoding) { call.setCompression("fzip"); } call.setMessageCompression(enableServerMessageCompression); serverResponseHeaders = headers; - return next.startCall(call, headers); + return next.startCall(method, call, headers); } } diff --git a/interop-testing/src/test/java/io/grpc/testing/integration/TransportCompressionTest.java b/interop-testing/src/test/java/io/grpc/testing/integration/TransportCompressionTest.java index d6afc63f8e..b63b6b6f05 100644 --- a/interop-testing/src/test/java/io/grpc/testing/integration/TransportCompressionTest.java +++ b/interop-testing/src/test/java/io/grpc/testing/integration/TransportCompressionTest.java @@ -108,9 +108,9 @@ public class TransportCompressionTest extends AbstractInteropTest { .decompressorRegistry(decompressors), new ServerInterceptor() { @Override - public Listener interceptCall(ServerCall call, - Metadata headers, ServerCallHandler next) { - Listener listener = next.startCall(call, headers); + public Listener interceptCall(MethodDescriptor method, + ServerCall call, Metadata headers, ServerCallHandler next) { + Listener listener = next.startCall(method, call, headers); // TODO(carl-mastrangelo): check that encoding was set. call.setMessageCompression(true); return listener; diff --git a/services/src/generated/main/grpc/io/grpc/health/v1/HealthGrpc.java b/services/src/generated/main/grpc/io/grpc/health/v1/HealthGrpc.java index b5563c9195..c0cd40f2ea 100644 --- a/services/src/generated/main/grpc/io/grpc/health/v1/HealthGrpc.java +++ b/services/src/generated/main/grpc/io/grpc/health/v1/HealthGrpc.java @@ -216,14 +216,9 @@ public class HealthGrpc { } } - public static io.grpc.ServiceDescriptor getServiceDescriptor() { - return new io.grpc.ServiceDescriptor(SERVICE_NAME, - METHOD_CHECK); - } - public static io.grpc.ServerServiceDefinition bindService( final Health serviceImpl) { - return io.grpc.ServerServiceDefinition.builder(getServiceDescriptor()) + return io.grpc.ServerServiceDefinition.builder(SERVICE_NAME) .addMethod( METHOD_CHECK, asyncUnaryCall( diff --git a/stub/src/main/java/io/grpc/stub/ServerCalls.java b/stub/src/main/java/io/grpc/stub/ServerCalls.java index 5b9471a9c0..da37fd4501 100644 --- a/stub/src/main/java/io/grpc/stub/ServerCalls.java +++ b/stub/src/main/java/io/grpc/stub/ServerCalls.java @@ -127,10 +127,11 @@ public class ServerCalls { return new ServerCallHandler() { @Override public ServerCall.Listener startCall( - final ServerCall call, + MethodDescriptor methodDescriptor, + final ServerCall call, Metadata headers) { - final ServerCallStreamObserverImpl responseObserver = - new ServerCallStreamObserverImpl(call); + final ServerCallStreamObserverImpl responseObserver = + new ServerCallStreamObserverImpl(call); // We expect only 1 request, but we ask for 2 requests here so that if a misbehaving client // sends more than 1 requests, ServerCall will catch it. Note that disabling auto // inbound flow control has no effect on unary calls. @@ -189,10 +190,11 @@ public class ServerCalls { return new ServerCallHandler() { @Override public ServerCall.Listener startCall( - final ServerCall call, + MethodDescriptor methodDescriptor, + final ServerCall call, Metadata headers) { - final ServerCallStreamObserverImpl responseObserver = - new ServerCallStreamObserverImpl(call); + final ServerCallStreamObserverImpl responseObserver = + new ServerCallStreamObserverImpl(call); final StreamObserver requestObserver = method.invoke(responseObserver); responseObserver.freeze(); if (responseObserver.autoFlowControlEnabled) { @@ -247,9 +249,9 @@ public class ServerCalls { StreamObserver invoke(StreamObserver responseObserver); } - private static final class ServerCallStreamObserverImpl + private static final class ServerCallStreamObserverImpl extends ServerCallStreamObserver { - final ServerCall call; + final ServerCall call; volatile boolean cancelled; private boolean frozen; private boolean autoFlowControlEnabled = true; @@ -257,7 +259,7 @@ public class ServerCalls { private Runnable onReadyHandler; private Runnable onCancelHandler; - ServerCallStreamObserverImpl(ServerCall call) { + ServerCallStreamObserverImpl(ServerCall call) { this.call = call; } diff --git a/stub/src/test/java/io/grpc/stub/ClientCallsTest.java b/stub/src/test/java/io/grpc/stub/ClientCallsTest.java index 5e3d51cdb5..ca59628d71 100644 --- a/stub/src/test/java/io/grpc/stub/ClientCallsTest.java +++ b/stub/src/test/java/io/grpc/stub/ClientCallsTest.java @@ -46,7 +46,6 @@ import io.grpc.ClientCall; import io.grpc.Metadata; import io.grpc.MethodDescriptor; import io.grpc.ServerServiceDefinition; -import io.grpc.ServiceDescriptor; import io.grpc.Status; import io.grpc.inprocess.InProcessChannelBuilder; import io.grpc.inprocess.InProcessServerBuilder; @@ -260,8 +259,7 @@ public class ClientCallsTest { @Test public void inprocessTransportInboundFlowControl() throws Exception { final Semaphore semaphore = new Semaphore(1); - ServerServiceDefinition service = ServerServiceDefinition.builder( - new ServiceDescriptor("some", STREAMING_METHOD)) + ServerServiceDefinition service = ServerServiceDefinition.builder("some") .addMethod(STREAMING_METHOD, ServerCalls.asyncBidiStreamingCall( new ServerCalls.BidiStreamingMethod() { int iteration; @@ -340,8 +338,7 @@ public class ClientCallsTest { final CountDownLatch latch = new CountDownLatch(1); final Semaphore semaphore = new Semaphore(1); final List receivedMessages = new ArrayList(6); - ServerServiceDefinition service = ServerServiceDefinition.builder( - new ServiceDescriptor("some", STREAMING_METHOD)) + ServerServiceDefinition service = ServerServiceDefinition.builder("some") .addMethod(STREAMING_METHOD, ServerCalls.asyncBidiStreamingCall( new ServerCalls.BidiStreamingMethod() { @Override diff --git a/stub/src/test/java/io/grpc/stub/ServerCallsTest.java b/stub/src/test/java/io/grpc/stub/ServerCallsTest.java index 48d8f6235f..d92ce91c98 100644 --- a/stub/src/test/java/io/grpc/stub/ServerCallsTest.java +++ b/stub/src/test/java/io/grpc/stub/ServerCallsTest.java @@ -45,7 +45,6 @@ import io.grpc.MethodDescriptor; import io.grpc.ServerCall; import io.grpc.ServerCallHandler; import io.grpc.ServerServiceDefinition; -import io.grpc.ServiceDescriptor; import io.grpc.Status; import io.grpc.inprocess.InProcessChannelBuilder; import io.grpc.inprocess.InProcessServerBuilder; @@ -89,7 +88,7 @@ public class ServerCallsTest { new IntegerMarshaller(), new IntegerMarshaller()); @Mock - ServerCall serverCall; + ServerCall serverCall; @Before public void setUp() throws Exception { @@ -129,7 +128,7 @@ public class ServerCallsTest { } }); ServerCall.Listener callListener = - callHandler.startCall(serverCall, new Metadata()); + callHandler.startCall(STREAMING_METHOD, serverCall, new Metadata()); Mockito.when(serverCall.isReady()).thenReturn(true).thenReturn(false); Mockito.when(serverCall.isCancelled()).thenReturn(false).thenReturn(true); assertTrue(callObserver.get().isReady()); @@ -161,7 +160,7 @@ public class ServerCallsTest { } }); ServerCall.Listener callListener = - callHandler.startCall(serverCall, new Metadata()); + callHandler.startCall(STREAMING_METHOD, serverCall, new Metadata()); callListener.onMessage(1); try { callObserver.get().setOnCancelHandler(new Runnable() { @@ -189,7 +188,7 @@ public class ServerCallsTest { } }); ServerCall.Listener callListener = - callHandler.startCall(serverCall, new Metadata()); + callHandler.startCall(STREAMING_METHOD, serverCall, new Metadata()); callListener.onMessage(1); try { callObserver.get().setOnReadyHandler(new Runnable() { @@ -217,7 +216,7 @@ public class ServerCallsTest { } }); ServerCall.Listener callListener = - callHandler.startCall(serverCall, new Metadata()); + callHandler.startCall(STREAMING_METHOD, serverCall, new Metadata()); callListener.onMessage(1); try { callObserver.get().disableAutoInboundFlowControl(); @@ -241,7 +240,7 @@ public class ServerCallsTest { } }); ServerCall.Listener callListener = - callHandler.startCall(serverCall, new Metadata()); + callHandler.startCall(STREAMING_METHOD, serverCall, new Metadata()); callListener.onReady(); // Transport should not call this if nothing has been requested but forcing it here // to verify that message delivery does not trigger a call to request(1). @@ -262,7 +261,8 @@ public class ServerCallsTest { serverCallObserver.disableAutoInboundFlowControl(); } }); - callHandler.startCall(serverCall, new Metadata()); + ServerCall.Listener callListener = + callHandler.startCall(UNARY_METHOD, serverCall, new Metadata()); // Auto inbound flow-control always requests 2 messages for unary to detect a violation // of the unary semantic. Mockito.verify(serverCall, times(1)).request(2); @@ -271,6 +271,8 @@ public class ServerCallsTest { @Test public void onReadyHandlerCalledForUnaryRequest() throws Exception { final AtomicInteger onReadyCalled = new AtomicInteger(); + final AtomicReference> callObserver = + new AtomicReference>(); ServerCallHandler callHandler = ServerCalls.asyncServerStreamingCall( new ServerCalls.ServerStreamingMethod() { @@ -287,7 +289,7 @@ public class ServerCallsTest { } }); ServerCall.Listener callListener = - callHandler.startCall(serverCall, new Metadata()); + callHandler.startCall(STREAMING_METHOD, serverCall, new Metadata()); Mockito.when(serverCall.isReady()).thenReturn(true).thenReturn(false); Mockito.when(serverCall.isCancelled()).thenReturn(false).thenReturn(true); callListener.onReady(); @@ -307,8 +309,7 @@ public class ServerCallsTest { @Test public void inprocessTransportManualFlow() throws Exception { final Semaphore semaphore = new Semaphore(1); - ServerServiceDefinition service = ServerServiceDefinition.builder( - new ServiceDescriptor("some", STREAMING_METHOD)) + ServerServiceDefinition service = ServerServiceDefinition.builder("some") .addMethod(STREAMING_METHOD, ServerCalls.asyncBidiStreamingCall( new ServerCalls.BidiStreamingMethod() { int iteration; diff --git a/testing/src/main/java/io/grpc/testing/TestUtils.java b/testing/src/main/java/io/grpc/testing/TestUtils.java index 0a0771e134..bc9083c571 100644 --- a/testing/src/main/java/io/grpc/testing/TestUtils.java +++ b/testing/src/main/java/io/grpc/testing/TestUtils.java @@ -34,6 +34,7 @@ package io.grpc.testing; import io.grpc.ExperimentalApi; import io.grpc.ForwardingServerCall.SimpleForwardingServerCall; import io.grpc.Metadata; +import io.grpc.MethodDescriptor; import io.grpc.ServerCall; import io.grpc.ServerCallHandler; import io.grpc.ServerInterceptor; @@ -84,10 +85,12 @@ public class TestUtils { return new ServerInterceptor() { @Override public ServerCall.Listener interceptCall( - ServerCall call, + MethodDescriptor method, + ServerCall call, final Metadata requestHeaders, ServerCallHandler next) { - return next.startCall(new SimpleForwardingServerCall(call) { + return next.startCall(method, + new SimpleForwardingServerCall(call) { @Override public void sendHeaders(Metadata responseHeaders) { responseHeaders.merge(requestHeaders, keySet); @@ -114,11 +117,12 @@ public class TestUtils { return new ServerInterceptor() { @Override public ServerCall.Listener interceptCall( - ServerCall call, + MethodDescriptor method, + ServerCall call, Metadata requestHeaders, ServerCallHandler next) { headersCapture.set(requestHeaders); - return next.startCall(call, requestHeaders); + return next.startCall(method, call, requestHeaders); } }; } @@ -128,15 +132,16 @@ public class TestUtils { * {@link ServerCall#attributes()} */ public static ServerInterceptor recordServerCallInterceptor( - final AtomicReference> serverCallCapture) { + final AtomicReference> serverCallCapture) { return new ServerInterceptor() { @Override public ServerCall.Listener interceptCall( - ServerCall call, + MethodDescriptor method, + ServerCall call, Metadata requestHeaders, ServerCallHandler next) { serverCallCapture.set(call); - return next.startCall(call, requestHeaders); + return next.startCall(method, call, requestHeaders); } }; }