diff options
-rw-r--r-- | include/grpc++/channel.h | 7 | ||||
-rw-r--r-- | src/cpp/client/channel_cc.cc | 30 | ||||
-rw-r--r-- | test/cpp/end2end/round_robin_end2end_test.cc | 9 |
3 files changed, 44 insertions, 2 deletions
diff --git a/include/grpc++/channel.h b/include/grpc++/channel.h index c8360282e7..ca269be218 100644 --- a/include/grpc++/channel.h +++ b/include/grpc++/channel.h @@ -57,6 +57,13 @@ class Channel final : public ChannelInterface, /// \a try_to_connect is set to true, try to connect. grpc_connectivity_state GetState(bool try_to_connect) override; + /// Returns the LB policy name, or the empty string if not yet available. + grpc::string GetLoadBalancingPolicyName() const; + + /// Returns the service config in JSON form, or the empty string if + /// not available. + grpc::string GetServiceConfigJSON() const; + private: template <class InputMessage, class OutputMessage> friend Status BlockingUnaryCall(ChannelInterface* channel, diff --git a/src/cpp/client/channel_cc.cc b/src/cpp/client/channel_cc.cc index 5f1d00d2b4..357d8317ad 100644 --- a/src/cpp/client/channel_cc.cc +++ b/src/cpp/client/channel_cc.cc @@ -48,6 +48,7 @@ #include <grpc++/support/time.h> #include <grpc/grpc.h> #include <grpc/slice.h> +#include <grpc/support/alloc.h> #include <grpc/support/log.h> #include "src/core/lib/profiling/timers.h" @@ -61,6 +62,35 @@ Channel::Channel(const grpc::string& host, grpc_channel* channel) Channel::~Channel() { grpc_channel_destroy(c_channel_); } +namespace { + +grpc::string GetChannelInfoField(grpc_channel* channel, + grpc_channel_info* channel_info, + char*** channel_info_field) { + char* value = NULL; + memset(channel_info, 0, sizeof(*channel_info)); + *channel_info_field = &value; + grpc_channel_get_info(channel, channel_info); + if (value == NULL) return ""; + grpc::string result = value; + gpr_free(value); + return result; +} + +} // namespace + +grpc::string Channel::GetLoadBalancingPolicyName() const { + grpc_channel_info channel_info; + return GetChannelInfoField(c_channel_, &channel_info, + &channel_info.lb_policy_name); +} + +grpc::string Channel::GetServiceConfigJSON() const { + grpc_channel_info channel_info; + return GetChannelInfoField(c_channel_, &channel_info, + &channel_info.service_config_json); +} + Call Channel::CreateCall(const RpcMethod& method, ClientContext* context, CompletionQueue* cq) { const bool kRegistered = method.channel_tag() && context->authority().empty(); diff --git a/test/cpp/end2end/round_robin_end2end_test.cc b/test/cpp/end2end/round_robin_end2end_test.cc index 76211cbdd3..cc340b96b3 100644 --- a/test/cpp/end2end/round_robin_end2end_test.cc +++ b/test/cpp/end2end/round_robin_end2end_test.cc @@ -109,9 +109,9 @@ class RoundRobinEnd2endTest : public ::testing::Test { uri << "127.0.0.1:" << servers_[i]->port_ << ","; } uri << "127.0.0.1:" << servers_[servers_.size() - 1]->port_; - std::shared_ptr<Channel> channel = + channel_ = CreateCustomChannel(uri.str(), InsecureChannelCredentials(), args); - stub_ = grpc::testing::EchoTestService::NewStub(channel); + stub_ = grpc::testing::EchoTestService::NewStub(channel_); } void SendRpc(int num_rpcs) { @@ -165,6 +165,7 @@ class RoundRobinEnd2endTest : public ::testing::Test { const grpc::string server_host_; CompletionQueue cli_cq_; + std::shared_ptr<Channel> channel_; std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_; std::vector<std::unique_ptr<ServerData>> servers_; }; @@ -186,6 +187,8 @@ TEST_F(RoundRobinEnd2endTest, PickFirst) { } } EXPECT_TRUE(found); + // Check LB policy name for the channel. + EXPECT_EQ("pick_first", channel_->GetLoadBalancingPolicyName()); } TEST_F(RoundRobinEnd2endTest, RoundRobin) { @@ -198,6 +201,8 @@ TEST_F(RoundRobinEnd2endTest, RoundRobin) { for (size_t i = 0; i < servers_.size(); ++i) { EXPECT_EQ(1, servers_[i]->service_.request_count()); } + // Check LB policy name for the channel. + EXPECT_EQ("round_robin", channel_->GetLoadBalancingPolicyName()); } } // namespace |