From cbd9e4cb56142abc376f85236f57469ef4b14d0e Mon Sep 17 00:00:00 2001 From: Matt Klein Date: Thu, 19 Oct 2017 15:08:53 -0700 Subject: [PATCH] cleanup: remove gRPC rpc channel This is now replaced with the templated AsyncClient. Fixes https://github.com/envoyproxy/envoy/issues/1535 Signed-off-by: Matt Klein --- include/envoy/grpc/BUILD | 11 - include/envoy/grpc/rpc_channel.h | 79 ------ source/common/grpc/BUILD | 19 -- source/common/grpc/rpc_channel_impl.cc | 97 ------- source/common/grpc/rpc_channel_impl.h | 69 ----- test/common/grpc/BUILD | 14 - test/common/grpc/rpc_channel_impl_test.cc | 329 ---------------------- test/mocks/grpc/BUILD | 1 - test/mocks/grpc/mocks.cc | 6 - test/mocks/grpc/mocks.h | 23 -- 10 files changed, 648 deletions(-) delete mode 100644 include/envoy/grpc/rpc_channel.h delete mode 100644 source/common/grpc/rpc_channel_impl.cc delete mode 100644 source/common/grpc/rpc_channel_impl.h delete mode 100644 test/common/grpc/rpc_channel_impl_test.cc diff --git a/include/envoy/grpc/BUILD b/include/envoy/grpc/BUILD index f14584df47ee..ba7ad2b13a62 100644 --- a/include/envoy/grpc/BUILD +++ b/include/envoy/grpc/BUILD @@ -20,17 +20,6 @@ envoy_cc_library( ], ) -envoy_cc_library( - name = "rpc_channel_interface", - hdrs = ["rpc_channel.h"], - deps = [ - "//include/envoy/common:optional", - "//include/envoy/http:header_map_interface", - "//include/envoy/tracing:http_tracer_interface", - "//source/common/protobuf", - ], -) - envoy_cc_library( name = "status", hdrs = ["status.h"], diff --git a/include/envoy/grpc/rpc_channel.h b/include/envoy/grpc/rpc_channel.h deleted file mode 100644 index 62920a61d764..000000000000 --- a/include/envoy/grpc/rpc_channel.h +++ /dev/null @@ -1,79 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -#include "envoy/common/optional.h" -#include "envoy/common/pure.h" -#include "envoy/http/header_map.h" - -#include "common/protobuf/protobuf.h" - -namespace Envoy { -namespace Grpc { - -/** - * Callbacks for an individual grpc request. - */ -class RpcChannelCallbacks { -public: - virtual ~RpcChannelCallbacks() {} - - /** - * Called before the channel dispatches an HTTP/2 request. This can be used to customize the - * transport headers for the RPC. - */ - virtual void onPreRequestCustomizeHeaders(Http::HeaderMap& headers) PURE; - - /** - * Called when the request has succeeded and the response object is populated. - */ - virtual void onSuccess() PURE; - - /** - * Called when the request has failed. The response object has not been populated. - * @param grpc_status supplies the grpc_status for the error, if available. - * @param message supplies additional error information if available. - */ - virtual void onFailure(const Optional& grpc_status, const std::string& message) PURE; -}; - -/** - * A single active grpc request arbiter. This interface derives from - * Protobuf::RpcChannel. When mocking, CallMethod() can be overriden to accept - * the response message and the mock constructor can accept a RequestCallbacks - * object. An RpcChannel should be passed to the constructor of an RPC stub - * generated via protoc using the "option cc_generic_services = true;" option. - * It can be used for multiple service calls, but not concurrently. - * DEPRECATED: See https://github.com/envoyproxy/envoy/issues/1102 - */ -class RpcChannel : public Protobuf::RpcChannel { -public: - virtual ~RpcChannel() {} - - /** - * Cancel an inflight RPC. The Request can be used again to make another call if desired. - */ - virtual void cancel() PURE; -}; - -typedef std::unique_ptr RpcChannelPtr; - -/** - * Interface for creating new RPC channels. - */ -class RpcChannelFactory { -public: - virtual ~RpcChannelFactory() {} - - /** - * Create a new RPC channel given a set of callbacks. - */ - virtual RpcChannelPtr create(RpcChannelCallbacks& callbacks, - const Optional& timeout) PURE; -}; - -} // namespace Grpc -} // namespace Envoy diff --git a/source/common/grpc/BUILD b/source/common/grpc/BUILD index 6b002a7a8420..e54db22306a3 100644 --- a/source/common/grpc/BUILD +++ b/source/common/grpc/BUILD @@ -107,25 +107,6 @@ envoy_cc_library( ], ) -envoy_cc_library( - name = "rpc_channel_lib", - srcs = ["rpc_channel_impl.cc"], - hdrs = ["rpc_channel_impl.h"], - deps = [ - ":common_lib", - "//include/envoy/grpc:rpc_channel_interface", - "//include/envoy/upstream:cluster_manager_interface", - "//source/common/buffer:zero_copy_input_stream_lib", - "//source/common/common:assert_lib", - "//source/common/common:enum_to_int", - "//source/common/common:utility_lib", - "//source/common/http:headers_lib", - "//source/common/http:message_lib", - "//source/common/http:utility_lib", - "//source/common/protobuf", - ], -) - envoy_cc_library( name = "transcoder_input_stream_lib", srcs = ["transcoder_input_stream_impl.cc"], diff --git a/source/common/grpc/rpc_channel_impl.cc b/source/common/grpc/rpc_channel_impl.cc deleted file mode 100644 index e6efd5a511ef..000000000000 --- a/source/common/grpc/rpc_channel_impl.cc +++ /dev/null @@ -1,97 +0,0 @@ -#include "common/grpc/rpc_channel_impl.h" - -#include -#include - -#include "common/buffer/zero_copy_input_stream_impl.h" -#include "common/common/enum_to_int.h" -#include "common/common/utility.h" -#include "common/grpc/common.h" -#include "common/http/headers.h" -#include "common/http/message_impl.h" -#include "common/http/utility.h" -#include "common/protobuf/protobuf.h" - -namespace Envoy { -namespace Grpc { - -void RpcChannelImpl::cancel() { - http_request_->cancel(); - onComplete(); -} - -void RpcChannelImpl::CallMethod(const Protobuf::MethodDescriptor* method, Protobuf::RpcController*, - const Protobuf::Message* grpc_request, - Protobuf::Message* grpc_response, Protobuf::Closure*) { - ASSERT(!http_request_ && !grpc_method_ && !grpc_response_); - grpc_method_ = method; - grpc_response_ = grpc_response; - - // For proto3 messages this should always return true. - ASSERT(grpc_request->IsInitialized()); - - // This should be caught in configuration, and a request will fail normally anyway, but assert - // here for clarity. - ASSERT(cluster_->features() & Upstream::ClusterInfo::Features::HTTP2); - - Http::MessagePtr message = - Common::prepareHeaders(cluster_->name(), method->service()->full_name(), method->name()); - message->body() = Common::serializeBody(*grpc_request); - - callbacks_.onPreRequestCustomizeHeaders(message->headers()); - http_request_ = - cm_.httpAsyncClientForCluster(cluster_->name()).send(std::move(message), *this, timeout_); -} - -void RpcChannelImpl::incStat(bool success) { - Common::chargeStat(*cluster_, grpc_method_->service()->full_name(), grpc_method_->name(), - success); -} - -void RpcChannelImpl::onSuccess(Http::MessagePtr&& http_response) { - try { - Common::validateResponse(*http_response); - - // A gRPC response contains a 5 byte header. Currently we only support unary responses so we - // ignore the header. @see serializeBody(). - if (!http_response->body() || (http_response->body()->length() < 5)) { - throw Exception(Optional(), "bad serialized body"); - } - - http_response->body()->drain(5); - Buffer::ZeroCopyInputStreamImpl stream(std::move(http_response->body())); - if (!grpc_response_->ParseFromZeroCopyStream(&stream)) { - throw Exception(Optional(), "bad serialized body"); - } - - callbacks_.onSuccess(); - incStat(true); - onComplete(); - } catch (const Exception& e) { - onFailureWorker(e.grpc_status_, e.what()); - } -} - -void RpcChannelImpl::onFailureWorker(const Optional& grpc_status, - const std::string& message) { - callbacks_.onFailure(grpc_status, message); - incStat(false); - onComplete(); -} - -void RpcChannelImpl::onFailure(Http::AsyncClient::FailureReason reason) { - switch (reason) { - case Http::AsyncClient::FailureReason::Reset: - onFailureWorker(Optional(), "stream reset"); - break; - } -} - -void RpcChannelImpl::onComplete() { - http_request_ = nullptr; - grpc_method_ = nullptr; - grpc_response_ = nullptr; -} - -} // namespace Grpc -} // namespace Envoy diff --git a/source/common/grpc/rpc_channel_impl.h b/source/common/grpc/rpc_channel_impl.h deleted file mode 100644 index 865d16a14dc1..000000000000 --- a/source/common/grpc/rpc_channel_impl.h +++ /dev/null @@ -1,69 +0,0 @@ -#pragma once - -#include -#include -#include - -#include "envoy/grpc/rpc_channel.h" -#include "envoy/upstream/cluster_manager.h" - -#include "common/common/assert.h" -#include "common/protobuf/protobuf.h" - -namespace Envoy { -namespace Grpc { - -/** - * Concrete implementation of RpcChannel. This is not the most optimal interface but it's the - * best thing that works with the protoc generated generic rpc code. A code generator plugin - * would be optimal but that is total overkill. - * - * How to use: - * 1) Add "option cc_generic_services = true;" to proto definition. - * 2) Use the generated "Stub" service wrapper and pass an RpcChannelImpl to the constructor. - * 3) The service wrapper can be used to make a call for a single RPC. It can then be reused - * to make another call. If parallel calls need to be made, higher level interfaces will be - * needed. - * 4) Inflight RPCs can be safely cancelled using cancel(). - * 5) See GrpcRequestImplTest for an example. - * DEPRECATED: See https://github.com/envoyproxy/envoy/issues/1102 - */ -class RpcChannelImpl : public RpcChannel, public Http::AsyncClient::Callbacks { -public: - RpcChannelImpl(Upstream::ClusterManager& cm, const std::string& cluster, - RpcChannelCallbacks& callbacks, const Optional& timeout) - : cm_(cm), cluster_(cm.get(cluster)->info()), callbacks_(callbacks), timeout_(timeout) {} - - ~RpcChannelImpl() { ASSERT(!http_request_ && !grpc_method_ && !grpc_response_); } - - static Buffer::InstancePtr serializeBody(const Protobuf::Message& message); - - // Grpc::RpcChannel - void cancel() override; - - // Protobuf::RpcChannel - void CallMethod(const Protobuf::MethodDescriptor* method, Protobuf::RpcController* controller, - const Protobuf::Message* grpc_request, Protobuf::Message* grpc_response, - Protobuf::Closure* done_callback) override; - -private: - void incStat(bool success); - void onComplete(); - void onFailureWorker(const Optional& grpc_status, const std::string& message); - void onSuccessWorker(Http::Message& http_response); - - // Http::AsyncClient::Callbacks - void onSuccess(Http::MessagePtr&& http_response) override; - void onFailure(Http::AsyncClient::FailureReason reason) override; - - Upstream::ClusterManager& cm_; - Upstream::ClusterInfoConstSharedPtr cluster_; - Http::AsyncClient::Request* http_request_{}; - const Protobuf::MethodDescriptor* grpc_method_{}; - Protobuf::Message* grpc_response_{}; - RpcChannelCallbacks& callbacks_; - Optional timeout_; -}; - -} // namespace Grpc -} // namespace Envoy diff --git a/test/common/grpc/BUILD b/test/common/grpc/BUILD index 49233f378531..2330f7c412d2 100644 --- a/test/common/grpc/BUILD +++ b/test/common/grpc/BUILD @@ -86,20 +86,6 @@ envoy_cc_test( ], ) -envoy_cc_test( - name = "rpc_channel_impl_test", - srcs = ["rpc_channel_impl_test.cc"], - deps = [ - "//source/common/grpc:common_lib", - "//source/common/grpc:rpc_channel_lib", - "//source/common/http:message_lib", - "//test/mocks/grpc:grpc_mocks", - "//test/mocks/upstream:upstream_mocks", - "//test/proto:helloworld_proto", - "//test/test_common:utility_lib", - ], -) - envoy_cc_test( name = "transcoder_input_stream_test", srcs = ["transcoder_input_stream_test.cc"], diff --git a/test/common/grpc/rpc_channel_impl_test.cc b/test/common/grpc/rpc_channel_impl_test.cc deleted file mode 100644 index 496a6a58a76e..000000000000 --- a/test/common/grpc/rpc_channel_impl_test.cc +++ /dev/null @@ -1,329 +0,0 @@ -#include -#include -#include - -#include "common/grpc/common.h" -#include "common/grpc/rpc_channel_impl.h" -#include "common/http/message_impl.h" - -#include "test/mocks/grpc/mocks.h" -#include "test/mocks/upstream/mocks.h" -#include "test/proto/helloworld.pb.h" -#include "test/test_common/printers.h" -#include "test/test_common/utility.h" - -#include "gmock/gmock.h" -#include "gtest/gtest.h" - -using testing::Invoke; -using testing::Return; -using testing::_; - -namespace Envoy { -namespace Grpc { - -class GrpcRequestImplTest : public testing::Test { -public: - GrpcRequestImplTest() : http_async_client_request_(&cm_.async_client_) { - ON_CALL(*cm_.thread_local_cluster_.cluster_.info_, features()) - .WillByDefault(Return(Upstream::ClusterInfo::Features::HTTP2)); - } - - void expectNormalRequest( - const Optional timeout = Optional()) { - EXPECT_CALL(cm_, httpAsyncClientForCluster("fake_cluster")) - .WillOnce(ReturnRef(cm_.async_client_)); - EXPECT_CALL(cm_.async_client_, send_(_, _, timeout)) - .WillOnce(Invoke([&](Http::MessagePtr& request, Http::AsyncClient::Callbacks& callbacks, - Optional) -> Http::AsyncClient::Request* { - http_request_ = std::move(request); - http_callbacks_ = &callbacks; - return &http_async_client_request_; - })); - } - - NiceMock cm_; - MockRpcChannelCallbacks grpc_callbacks_; - RpcChannelImpl grpc_request_{cm_, "fake_cluster", grpc_callbacks_, - Optional()}; - helloworld::Greeter::Stub service_{&grpc_request_}; - Http::MockAsyncClientRequest http_async_client_request_; - Http::MessagePtr http_request_; - Http::AsyncClient::Callbacks* http_callbacks_{}; -}; - -TEST_F(GrpcRequestImplTest, NoError) { - expectNormalRequest(); - - helloworld::HelloRequest request; - request.set_name("a name"); - helloworld::HelloReply response; - Http::LowerCaseString header_key("foo"); - std::string header_value("bar"); - EXPECT_CALL(grpc_callbacks_, onPreRequestCustomizeHeaders(_)) - .WillOnce(Invoke([&](Http::HeaderMap& headers) -> void { - headers.addReference(header_key, header_value); - })); - service_.SayHello(nullptr, &request, &response, nullptr); - - Http::TestHeaderMapImpl expected_request_headers{{":method", "POST"}, - {":path", "/helloworld.Greeter/SayHello"}, - {":authority", "fake_cluster"}, - {"content-type", "application/grpc"}, - {"te", "trailers"}, - {"foo", "bar"}}; - - EXPECT_THAT(http_request_->headers(), HeaderMapEqualRef(&expected_request_headers)); - - Http::MessagePtr response_http_message(new Http::ResponseMessageImpl( - Http::HeaderMapPtr{new Http::TestHeaderMapImpl{{":status", "200"}}})); - helloworld::HelloReply inner_response; - inner_response.set_message("hello a name"); - response_http_message->body() = Common::serializeBody(inner_response); - response_http_message->trailers( - Http::HeaderMapPtr{new Http::TestHeaderMapImpl{{"grpc-status", "0"}}}); - - EXPECT_CALL(grpc_callbacks_, onSuccess()); - http_callbacks_->onSuccess(std::move(response_http_message)); - EXPECT_EQ(response.SerializeAsString(), inner_response.SerializeAsString()); - EXPECT_EQ(1UL, cm_.thread_local_cluster_.cluster_.info_->stats_store_ - .counter("grpc.helloworld.Greeter.SayHello.success") - .value()); -} - -TEST_F(GrpcRequestImplTest, Non200Response) { - expectNormalRequest(); - - helloworld::HelloRequest request; - request.set_name("a name"); - helloworld::HelloReply response; - EXPECT_CALL(grpc_callbacks_, onPreRequestCustomizeHeaders(_)); - service_.SayHello(nullptr, &request, &response, nullptr); - - Http::MessagePtr response_http_message(new Http::ResponseMessageImpl( - Http::HeaderMapPtr{new Http::TestHeaderMapImpl{{":status", "503"}}})); - - EXPECT_CALL(grpc_callbacks_, onFailure(Optional(), "non-200 response code")); - http_callbacks_->onSuccess(std::move(response_http_message)); - EXPECT_EQ(1UL, cm_.thread_local_cluster_.cluster_.info_->stats_store_ - .counter("grpc.helloworld.Greeter.SayHello.failure") - .value()); -} - -TEST_F(GrpcRequestImplTest, NoResponseTrailers) { - expectNormalRequest(); - - helloworld::HelloRequest request; - request.set_name("a name"); - helloworld::HelloReply response; - EXPECT_CALL(grpc_callbacks_, onPreRequestCustomizeHeaders(_)); - service_.SayHello(nullptr, &request, &response, nullptr); - - Http::MessagePtr response_http_message(new Http::ResponseMessageImpl( - Http::HeaderMapPtr{new Http::TestHeaderMapImpl{{":status", "200"}}})); - - EXPECT_CALL(grpc_callbacks_, onFailure(Optional(), "no response trailers")); - http_callbacks_->onSuccess(std::move(response_http_message)); -} - -TEST_F(GrpcRequestImplTest, BadGrpcStatusInHeaderOnlyResponse) { - expectNormalRequest(); - - helloworld::HelloRequest request; - request.set_name("a name"); - helloworld::HelloReply response; - EXPECT_CALL(grpc_callbacks_, onPreRequestCustomizeHeaders(_)); - service_.SayHello(nullptr, &request, &response, nullptr); - - Http::MessagePtr response_http_message(new Http::ResponseMessageImpl( - Http::HeaderMapPtr{new Http::TestHeaderMapImpl{{":status", "200"}, {"grpc-status", "foo"}}})); - - EXPECT_CALL(grpc_callbacks_, onFailure(Optional(), "bad grpc-status header")); - http_callbacks_->onSuccess(std::move(response_http_message)); -} - -TEST_F(GrpcRequestImplTest, HeaderOnlyFailure) { - expectNormalRequest(); - - helloworld::HelloRequest request; - request.set_name("a name"); - helloworld::HelloReply response; - EXPECT_CALL(grpc_callbacks_, onPreRequestCustomizeHeaders(_)); - service_.SayHello(nullptr, &request, &response, nullptr); - - Http::MessagePtr response_http_message( - new Http::ResponseMessageImpl(Http::HeaderMapPtr{new Http::TestHeaderMapImpl{ - {":status", "200"}, {"grpc-status", "3"}, {"grpc-message", "hello"}}})); - - EXPECT_CALL(grpc_callbacks_, onFailure(Optional(3), "hello")); - http_callbacks_->onSuccess(std::move(response_http_message)); -} - -TEST_F(GrpcRequestImplTest, BadGrpcStatusInResponse) { - expectNormalRequest(); - - helloworld::HelloRequest request; - request.set_name("a name"); - helloworld::HelloReply response; - EXPECT_CALL(grpc_callbacks_, onPreRequestCustomizeHeaders(_)); - service_.SayHello(nullptr, &request, &response, nullptr); - - Http::MessagePtr response_http_message(new Http::ResponseMessageImpl( - Http::HeaderMapPtr{new Http::TestHeaderMapImpl{{":status", "200"}}})); - response_http_message->trailers( - Http::HeaderMapPtr{new Http::TestHeaderMapImpl{{"grpc-status", ""}}}); - - EXPECT_CALL(grpc_callbacks_, onFailure(Optional(), "bad grpc-status trailer")); - http_callbacks_->onSuccess(std::move(response_http_message)); -} - -TEST_F(GrpcRequestImplTest, GrpcStatusNonZeroInResponse) { - expectNormalRequest(); - - helloworld::HelloRequest request; - request.set_name("a name"); - helloworld::HelloReply response; - EXPECT_CALL(grpc_callbacks_, onPreRequestCustomizeHeaders(_)); - service_.SayHello(nullptr, &request, &response, nullptr); - - Http::MessagePtr response_http_message(new Http::ResponseMessageImpl( - Http::HeaderMapPtr{new Http::TestHeaderMapImpl{{":status", "200"}}})); - response_http_message->trailers(Http::HeaderMapPtr{ - new Http::TestHeaderMapImpl{{"grpc-status", "1"}, {"grpc-message", "hello"}}}); - - EXPECT_CALL(grpc_callbacks_, onFailure(Optional(1), "hello")); - http_callbacks_->onSuccess(std::move(response_http_message)); -} - -TEST_F(GrpcRequestImplTest, ShortBodyInResponse) { - expectNormalRequest(); - - helloworld::HelloRequest request; - request.set_name("a name"); - helloworld::HelloReply response; - EXPECT_CALL(grpc_callbacks_, onPreRequestCustomizeHeaders(_)); - service_.SayHello(nullptr, &request, &response, nullptr); - - Http::MessagePtr response_http_message(new Http::ResponseMessageImpl( - Http::HeaderMapPtr{new Http::TestHeaderMapImpl{{":status", "200"}}})); - response_http_message->body().reset(new Buffer::OwnedImpl("aaa")); - response_http_message->trailers( - Http::HeaderMapPtr{new Http::TestHeaderMapImpl{{"grpc-status", "0"}}}); - - EXPECT_CALL(grpc_callbacks_, onFailure(Optional(), "bad serialized body")); - http_callbacks_->onSuccess(std::move(response_http_message)); -} - -TEST_F(GrpcRequestImplTest, EmptyBodyInResponse) { - expectNormalRequest(); - - helloworld::HelloRequest request; - request.set_name("a name"); - helloworld::HelloReply response; - EXPECT_CALL(grpc_callbacks_, onPreRequestCustomizeHeaders(_)); - service_.SayHello(nullptr, &request, &response, nullptr); - - Http::MessagePtr response_http_message(new Http::ResponseMessageImpl( - Http::HeaderMapPtr{new Http::TestHeaderMapImpl{{":status", "200"}}})); - helloworld::HelloReply empty_response; - response_http_message->body() = Common::serializeBody(empty_response); - EXPECT_EQ(response_http_message->body()->length(), 5); - response_http_message->trailers( - Http::HeaderMapPtr{new Http::TestHeaderMapImpl{{"grpc-status", "0"}}}); - - EXPECT_CALL(grpc_callbacks_, onSuccess()); - http_callbacks_->onSuccess(std::move(response_http_message)); - EXPECT_EQ(response.SerializeAsString(), empty_response.SerializeAsString()); -} - -TEST_F(GrpcRequestImplTest, BadMessageInResponse) { - expectNormalRequest(); - - helloworld::HelloRequest request; - request.set_name("a name"); - helloworld::HelloReply response; - EXPECT_CALL(grpc_callbacks_, onPreRequestCustomizeHeaders(_)); - service_.SayHello(nullptr, &request, &response, nullptr); - - Http::MessagePtr response_http_message(new Http::ResponseMessageImpl( - Http::HeaderMapPtr{new Http::TestHeaderMapImpl{{":status", "200"}}})); - response_http_message->body().reset(new Buffer::OwnedImpl("aaaaaaaa")); - response_http_message->trailers( - Http::HeaderMapPtr{new Http::TestHeaderMapImpl{{"grpc-status", "0"}}}); - - EXPECT_CALL(grpc_callbacks_, onFailure(Optional(), "bad serialized body")); - http_callbacks_->onSuccess(std::move(response_http_message)); -} - -TEST_F(GrpcRequestImplTest, HttpAsyncRequestFailure) { - expectNormalRequest(); - - helloworld::HelloRequest request; - request.set_name("a name"); - helloworld::HelloReply response; - EXPECT_CALL(grpc_callbacks_, onPreRequestCustomizeHeaders(_)); - service_.SayHello(nullptr, &request, &response, nullptr); - - EXPECT_CALL(grpc_callbacks_, onFailure(Optional(), "stream reset")); - http_callbacks_->onFailure(Http::AsyncClient::FailureReason::Reset); -} - -TEST_F(GrpcRequestImplTest, NoHttpAsyncRequest) { - EXPECT_CALL(cm_, httpAsyncClientForCluster("fake_cluster")) - .WillOnce(ReturnRef(cm_.async_client_)); - EXPECT_CALL(cm_.async_client_, send_(_, _, _)) - .WillOnce( - Invoke([&](Http::MessagePtr&, Http::AsyncClient::Callbacks& callbacks, - const Optional&) -> Http::AsyncClient::Request* { - callbacks.onSuccess(Http::MessagePtr{new Http::ResponseMessageImpl( - Http::HeaderMapPtr{new Http::TestHeaderMapImpl{{":status", "503"}}})}); - return nullptr; - })); - EXPECT_CALL(grpc_callbacks_, onFailure(Optional(), "non-200 response code")); - - helloworld::HelloRequest request; - request.set_name("a name"); - helloworld::HelloReply response; - EXPECT_CALL(grpc_callbacks_, onPreRequestCustomizeHeaders(_)); - service_.SayHello(nullptr, &request, &response, nullptr); -} - -TEST_F(GrpcRequestImplTest, Cancel) { - expectNormalRequest(); - - helloworld::HelloRequest request; - request.set_name("a name"); - helloworld::HelloReply response; - EXPECT_CALL(grpc_callbacks_, onPreRequestCustomizeHeaders(_)); - service_.SayHello(nullptr, &request, &response, nullptr); - - EXPECT_CALL(http_async_client_request_, cancel()); - grpc_request_.cancel(); -} - -TEST_F(GrpcRequestImplTest, RequestTimeoutSet) { - const Optional timeout(std::chrono::milliseconds(100)); - RpcChannelImpl grpc_request_timeout{cm_, "fake_cluster", grpc_callbacks_, timeout}; - helloworld::Greeter::Stub service_timeout{&grpc_request_timeout}; - expectNormalRequest(timeout); - helloworld::HelloRequest request; - request.set_name("a name"); - helloworld::HelloReply response; - EXPECT_CALL(grpc_callbacks_, onPreRequestCustomizeHeaders(_)); - service_timeout.SayHello(nullptr, &request, &response, nullptr); - - Http::MessagePtr response_http_message(new Http::ResponseMessageImpl( - Http::HeaderMapPtr{new Http::TestHeaderMapImpl{{":status", "200"}}})); - helloworld::HelloReply inner_response; - inner_response.set_message("hello a name"); - - response_http_message->body() = Common::serializeBody(inner_response); - response_http_message->trailers( - Http::HeaderMapPtr{new Http::TestHeaderMapImpl{{"grpc-status", "0"}}}); - - EXPECT_CALL(grpc_callbacks_, onSuccess()); - http_callbacks_->onSuccess(std::move(response_http_message)); -} - -} // namespace Grpc -} // namespace Envoy diff --git a/test/mocks/grpc/BUILD b/test/mocks/grpc/BUILD index 8e98d6e50d93..58b370e6dd37 100644 --- a/test/mocks/grpc/BUILD +++ b/test/mocks/grpc/BUILD @@ -14,6 +14,5 @@ envoy_cc_mock( hdrs = ["mocks.h"], deps = [ "//include/envoy/grpc:async_client_interface", - "//include/envoy/grpc:rpc_channel_interface", ], ) diff --git a/test/mocks/grpc/mocks.cc b/test/mocks/grpc/mocks.cc index d96a623acdd6..0b21c5cd5390 100644 --- a/test/mocks/grpc/mocks.cc +++ b/test/mocks/grpc/mocks.cc @@ -6,11 +6,5 @@ namespace Grpc { MockAsyncRequest::MockAsyncRequest() {} MockAsyncRequest::~MockAsyncRequest() {} -MockRpcChannelCallbacks::MockRpcChannelCallbacks() {} -MockRpcChannelCallbacks::~MockRpcChannelCallbacks() {} - -MockRpcChannel::MockRpcChannel() {} -MockRpcChannel::~MockRpcChannel() {} - } // namespace Grpc } // namespace Envoy diff --git a/test/mocks/grpc/mocks.h b/test/mocks/grpc/mocks.h index 321e6c954c89..efd90ecf5f71 100644 --- a/test/mocks/grpc/mocks.h +++ b/test/mocks/grpc/mocks.h @@ -4,7 +4,6 @@ #include #include "envoy/grpc/async_client.h" -#include "envoy/grpc/rpc_channel.h" #include "gmock/gmock.h" @@ -71,27 +70,5 @@ class MockAsyncClient : public AsyncClient { AsyncStreamCallbacks& callbacks)); }; -class MockRpcChannelCallbacks : public RpcChannelCallbacks { -public: - MockRpcChannelCallbacks(); - ~MockRpcChannelCallbacks(); - - MOCK_METHOD1(onPreRequestCustomizeHeaders, void(Http::HeaderMap& headers)); - MOCK_METHOD0(onSuccess, void()); - MOCK_METHOD2(onFailure, void(const Optional& grpc_status, const std::string& message)); -}; - -class MockRpcChannel : public RpcChannel { -public: - MockRpcChannel(); - ~MockRpcChannel(); - - MOCK_METHOD0(cancel, void()); - MOCK_METHOD5(CallMethod, - void(const Protobuf::MethodDescriptor* method, Protobuf::RpcController* controller, - const Protobuf::Message* request, Protobuf::Message* response, - Protobuf::Closure* done)); -}; - } // namespace Grpc } // namespace Envoy