health_service_end2end_test.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*
  2. *
  3. * Copyright 2016, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. #include <memory>
  34. #include <mutex>
  35. #include <thread>
  36. #include <vector>
  37. #include <grpc++/channel.h>
  38. #include <grpc++/client_context.h>
  39. #include <grpc++/create_channel.h>
  40. #include <grpc++/ext/health_check_service_server_builder_option.h>
  41. #include <grpc++/health_check_service_interface.h>
  42. #include <grpc++/server.h>
  43. #include <grpc++/server_builder.h>
  44. #include <grpc++/server_context.h>
  45. #include <grpc/grpc.h>
  46. #include <grpc/support/log.h>
  47. #include <gtest/gtest.h>
  48. #include "src/proto/grpc/health/v1/health.grpc.pb.h"
  49. #include "src/proto/grpc/testing/duplicate/echo_duplicate.grpc.pb.h"
  50. #include "src/proto/grpc/testing/echo.grpc.pb.h"
  51. #include "test/core/util/port.h"
  52. #include "test/core/util/test_config.h"
  53. #include "test/cpp/end2end/test_service_impl.h"
  54. using grpc::health::v1::Health;
  55. using grpc::health::v1::HealthCheckRequest;
  56. using grpc::health::v1::HealthCheckResponse;
  57. namespace grpc {
  58. namespace testing {
  59. namespace {
  60. // A sample sync implementation of the health checking service. This does the
  61. // same thing as the default one.
  62. class HealthCheckServiceImpl : public ::grpc::health::v1::Health::Service {
  63. public:
  64. Status Check(ServerContext* context, const HealthCheckRequest* request,
  65. HealthCheckResponse* response) override {
  66. std::lock_guard<std::mutex> lock(mu_);
  67. auto iter = status_map_.find(request->service());
  68. if (iter == status_map_.end()) {
  69. return Status(StatusCode::NOT_FOUND, "");
  70. }
  71. response->set_status(iter->second);
  72. return Status::OK;
  73. }
  74. void SetStatus(const grpc::string& service_name,
  75. HealthCheckResponse::ServingStatus status) {
  76. std::lock_guard<std::mutex> lock(mu_);
  77. status_map_[service_name] = status;
  78. }
  79. void SetAll(HealthCheckResponse::ServingStatus status) {
  80. std::lock_guard<std::mutex> lock(mu_);
  81. for (auto iter = status_map_.begin(); iter != status_map_.end(); ++iter) {
  82. iter->second = status;
  83. }
  84. }
  85. private:
  86. std::mutex mu_;
  87. std::map<const grpc::string, HealthCheckResponse::ServingStatus> status_map_;
  88. };
  89. // A custom implementation of the health checking service interface. This is
  90. // used to test that it prevents the server from creating a default service and
  91. // also serves as an example of how to override the default service.
  92. class CustomHealthCheckService : public HealthCheckServiceInterface {
  93. public:
  94. explicit CustomHealthCheckService(HealthCheckServiceImpl* impl)
  95. : impl_(impl) {
  96. impl_->SetStatus("", HealthCheckResponse::SERVING);
  97. }
  98. void SetServingStatus(const grpc::string& service_name,
  99. bool serving) override {
  100. impl_->SetStatus(service_name, serving ? HealthCheckResponse::SERVING
  101. : HealthCheckResponse::NOT_SERVING);
  102. }
  103. void SetServingStatus(bool serving) override {
  104. impl_->SetAll(serving ? HealthCheckResponse::SERVING
  105. : HealthCheckResponse::NOT_SERVING);
  106. }
  107. private:
  108. HealthCheckServiceImpl* impl_; // not owned
  109. };
  110. class HealthServiceEnd2endTest : public ::testing::Test {
  111. protected:
  112. HealthServiceEnd2endTest() {}
  113. void SetUpServer(bool register_sync_test_service,
  114. bool explicit_health_service,
  115. std::unique_ptr<HealthCheckServiceInterface> service) {
  116. int port = grpc_pick_unused_port_or_die();
  117. server_address_ << "localhost:" << port;
  118. bool register_sync_health_service_impl =
  119. explicit_health_service && service != nullptr;
  120. // Setup server
  121. ServerBuilder builder;
  122. if (explicit_health_service) {
  123. std::unique_ptr<ServerBuilderOption> option(
  124. new HealthCheckServiceServerBuilderOption(std::move(service)));
  125. builder.SetOption(std::move(option));
  126. }
  127. builder.AddListeningPort(server_address_.str(),
  128. grpc::InsecureServerCredentials());
  129. if (register_sync_test_service) {
  130. // Register a sync service.
  131. builder.RegisterService(&echo_test_service_);
  132. }
  133. if (register_sync_health_service_impl) {
  134. builder.RegisterService(&health_check_service_impl_);
  135. }
  136. cq_ = builder.AddCompletionQueue();
  137. server_ = builder.BuildAndStart();
  138. }
  139. void TearDown() override {
  140. if (server_) {
  141. server_->Shutdown();
  142. cq_->Shutdown();
  143. if (cq_thread_.joinable()) {
  144. cq_thread_.join();
  145. }
  146. }
  147. }
  148. void ResetStubs() {
  149. std::shared_ptr<Channel> channel =
  150. CreateChannel(server_address_.str(), InsecureChannelCredentials());
  151. hc_stub_ = grpc::health::v1::Health::NewStub(channel);
  152. }
  153. // When the expected_status is NOT OK, we do not care about the response.
  154. void SendHealthCheckRpc(const grpc::string& service_name,
  155. const Status& expected_status) {
  156. EXPECT_FALSE(expected_status.ok());
  157. SendHealthCheckRpc(service_name, expected_status,
  158. HealthCheckResponse::UNKNOWN);
  159. }
  160. void SendHealthCheckRpc(
  161. const grpc::string& service_name, const Status& expected_status,
  162. HealthCheckResponse::ServingStatus expected_serving_status) {
  163. HealthCheckRequest request;
  164. request.set_service(service_name);
  165. HealthCheckResponse response;
  166. ClientContext context;
  167. Status s = hc_stub_->Check(&context, request, &response);
  168. EXPECT_EQ(expected_status.error_code(), s.error_code());
  169. if (s.ok()) {
  170. EXPECT_EQ(expected_serving_status, response.status());
  171. }
  172. }
  173. void VerifyHealthCheckService() {
  174. HealthCheckServiceInterface* service = server_->GetHealthCheckService();
  175. EXPECT_TRUE(service != nullptr);
  176. const grpc::string kHealthyService("healthy_service");
  177. const grpc::string kUnhealthyService("unhealthy_service");
  178. const grpc::string kNotRegisteredService("not_registered");
  179. service->SetServingStatus(kHealthyService, true);
  180. service->SetServingStatus(kUnhealthyService, false);
  181. ResetStubs();
  182. SendHealthCheckRpc("", Status::OK, HealthCheckResponse::SERVING);
  183. SendHealthCheckRpc(kHealthyService, Status::OK,
  184. HealthCheckResponse::SERVING);
  185. SendHealthCheckRpc(kUnhealthyService, Status::OK,
  186. HealthCheckResponse::NOT_SERVING);
  187. SendHealthCheckRpc(kNotRegisteredService,
  188. Status(StatusCode::NOT_FOUND, ""));
  189. service->SetServingStatus(false);
  190. SendHealthCheckRpc("", Status::OK, HealthCheckResponse::NOT_SERVING);
  191. SendHealthCheckRpc(kHealthyService, Status::OK,
  192. HealthCheckResponse::NOT_SERVING);
  193. SendHealthCheckRpc(kUnhealthyService, Status::OK,
  194. HealthCheckResponse::NOT_SERVING);
  195. SendHealthCheckRpc(kNotRegisteredService,
  196. Status(StatusCode::NOT_FOUND, ""));
  197. }
  198. TestServiceImpl echo_test_service_;
  199. HealthCheckServiceImpl health_check_service_impl_;
  200. std::unique_ptr<Health::Stub> hc_stub_;
  201. std::unique_ptr<Server> server_;
  202. std::ostringstream server_address_;
  203. std::unique_ptr<ServerCompletionQueue> cq_;
  204. std::thread cq_thread_;
  205. };
  206. TEST_F(HealthServiceEnd2endTest, DefaultHealthServiceDisabled) {
  207. EnableDefaultHealthCheckService(false);
  208. EXPECT_FALSE(DefaultHealthCheckServiceEnabled());
  209. SetUpServer(true, false, nullptr);
  210. HealthCheckServiceInterface* default_service =
  211. server_->GetHealthCheckService();
  212. EXPECT_TRUE(default_service == nullptr);
  213. ResetStubs();
  214. SendHealthCheckRpc("", Status(StatusCode::UNIMPLEMENTED, ""));
  215. }
  216. // TEST_F(HealthServiceEnd2endTest, DefaultHealthService) {
  217. // EnableDefaultHealthCheckService(true);
  218. // EXPECT_TRUE(DefaultHealthCheckServiceEnabled());
  219. // SetUpServer(true, false, nullptr);
  220. // VerifyHealthCheckService();
  221. //
  222. // // The default service has a size limit of the service name.
  223. // const grpc::string kTooLongServiceName(201, 'x');
  224. // SendHealthCheckRpc(kTooLongServiceName,
  225. // Status(StatusCode::INVALID_ARGUMENT, ""));
  226. // }
  227. //
  228. // void LoopCompletionQueue(ServerCompletionQueue* cq) {
  229. // void* tag;
  230. // bool ok;
  231. // while (cq->Next(&tag, &ok)) {
  232. // abort(); // Nothing should come out of the cq.
  233. // }
  234. // gpr_log(GPR_ERROR, "returning from thread");
  235. // }
  236. //
  237. // TEST_F(HealthServiceEnd2endTest, DefaultHealthServiceAsync) {
  238. // EnableDefaultHealthCheckService(true);
  239. // EXPECT_TRUE(DefaultHealthCheckServiceEnabled());
  240. // SetUpServer(false, false, nullptr);
  241. // cq_thread_ = std::thread(LoopCompletionQueue, cq_.get());
  242. // VerifyHealthCheckService();
  243. //
  244. // // The default service has a size limit of the service name.
  245. // const grpc::string kTooLongServiceName(201, 'x');
  246. // SendHealthCheckRpc(kTooLongServiceName,
  247. // Status(StatusCode::INVALID_ARGUMENT, ""));
  248. // }
  249. //
  250. // // Provide an empty service to disable the default service.
  251. // TEST_F(HealthServiceEnd2endTest, ExplicitlyDisableViaOverride) {
  252. // EnableDefaultHealthCheckService(true);
  253. // EXPECT_TRUE(DefaultHealthCheckServiceEnabled());
  254. // std::unique_ptr<HealthCheckServiceInterface> empty_service;
  255. // SetUpServer(true, true, std::move(empty_service));
  256. // HealthCheckServiceInterface* service = server_->GetHealthCheckService();
  257. // EXPECT_TRUE(service == nullptr);
  258. //
  259. // ResetStubs();
  260. //
  261. // SendHealthCheckRpc("", Status(StatusCode::UNIMPLEMENTED, ""));
  262. // }
  263. //
  264. // // Provide an explicit override of health checking service interface.
  265. // TEST_F(HealthServiceEnd2endTest, ExplicitlyOverride) {
  266. // EnableDefaultHealthCheckService(true);
  267. // EXPECT_TRUE(DefaultHealthCheckServiceEnabled());
  268. // std::unique_ptr<HealthCheckServiceInterface> override_service(
  269. // new CustomHealthCheckService(&health_check_service_impl_));
  270. // HealthCheckServiceInterface* underlying_service = override_service.get();
  271. // SetUpServer(false, true, std::move(override_service));
  272. // HealthCheckServiceInterface* service = server_->GetHealthCheckService();
  273. // EXPECT_TRUE(service == underlying_service);
  274. //
  275. // ResetStubs();
  276. //
  277. // VerifyHealthCheckService();
  278. // }
  279. } // namespace
  280. } // namespace testing
  281. } // namespace grpc
  282. int main(int argc, char** argv) {
  283. grpc_test_init(argc, argv);
  284. ::testing::InitGoogleTest(&argc, argv);
  285. return RUN_ALL_TESTS();
  286. }