From 86e99fbbb24d368bd7919ea24c4992d31772225c Mon Sep 17 00:00:00 2001 From: Xiaobing Zhou Date: Mon, 17 Jul 2017 17:00:23 -0700 Subject: [PATCH] HBASE-18400. [C++] ConnectionId Equals/Hash should consider service_name --- hbase-native-client/connection/connection-id.h | 7 +++++- .../connection/connection-pool-test.cc | 26 ++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/hbase-native-client/connection/connection-id.h b/hbase-native-client/connection/connection-id.h index 73811036e4..059469f843 100644 --- a/hbase-native-client/connection/connection-id.h +++ b/hbase-native-client/connection/connection-id.h @@ -39,6 +39,10 @@ class ConnectionId { const std::string &service_name) : user_(user), service_name_(service_name), host_(host), port_(port) {} + ConnectionId(const std::string &host, uint16_t port, + const std::string &service_name) + : user_(security::User::defaultUser()), service_name_(service_name), host_(host), port_(port) {} + virtual ~ConnectionId() = default; std::shared_ptr user() const { return user_; } @@ -59,7 +63,7 @@ struct ConnectionIdEquals { bool operator()(const std::shared_ptr &lhs, const std::shared_ptr &rhs) const { return userEquals(lhs->user(), rhs->user()) && lhs->host() == rhs->host() && - lhs->port() == rhs->port(); + lhs->port() == rhs->port() && lhs->service_name() == rhs->service_name(); } private: @@ -78,6 +82,7 @@ struct ConnectionIdHash { boost::hash_combine(h, ci->user() == nullptr ? 0 : ci->user()->user_name()); boost::hash_combine(h, ci->host()); boost::hash_combine(h, ci->port()); + boost::hash_combine(h, ci->service_name()); return h; } }; diff --git a/hbase-native-client/connection/connection-pool-test.cc b/hbase-native-client/connection/connection-pool-test.cc index 04ec7f143d..63f774bf17 100644 --- a/hbase-native-client/connection/connection-pool-test.cc +++ b/hbase-native-client/connection/connection-pool-test.cc @@ -101,3 +101,29 @@ TEST(TestConnectionPool, TestOnlyCreateMultipleDispose) { auto remote_id2 = std::make_shared(hostname_two, port); auto result_two = cp.GetConnection(remote_id2); } + +TEST(TestConnectionPool, TestCreateOneConnectionForOneService) { + std::string hostname{"hostname"}; + uint32_t port{999}; + std::string service1{"service1"}; + std::string service2{"service2"}; + + auto mock_boot = std::make_shared(); + auto mock_service = std::make_shared(); + auto mock_cf = std::make_shared(); + + EXPECT_CALL((*mock_cf), Connect(_, _, _)).Times(2).WillRepeatedly(Return(mock_service)); + EXPECT_CALL((*mock_cf), MakeBootstrap()).Times(2).WillRepeatedly(Return(mock_boot)); + ConnectionPool cp{mock_cf}; + + { + auto remote_id = std::make_shared(hostname, port, service1); + auto result_one = cp.GetConnection(remote_id); + auto remote_id2 = std::make_shared(hostname, port, service2); + auto result_two = cp.GetConnection(remote_id2); + } + auto remote_id = std::make_shared(hostname, port, service1); + auto result_one = cp.GetConnection(remote_id); + auto remote_id2 = std::make_shared(hostname, port, service2); + auto result_two = cp.GetConnection(remote_id2); +} -- 2.11.0 (Apple Git-81)