24 #include <gtest/gtest.h>
37 #include "src/proto/grpc/health/v1/health.grpc.pb.h"
38 #include "src/proto/grpc/testing/duplicate/echo_duplicate.grpc.pb.h"
39 #include "src/proto/grpc/testing/echo.grpc.pb.h"
45 using grpc::health::v1::Health;
46 using grpc::health::v1::HealthCheckRequest;
47 using grpc::health::v1::HealthCheckResponse;
56 class CustomHealthCheckService :
public HealthCheckServiceInterface {
58 explicit CustomHealthCheckService(HealthCheckServiceImpl* impl)
60 impl_->SetStatus(
"", HealthCheckResponse::SERVING);
62 void SetServingStatus(
const std::string& service_name,
63 bool serving)
override {
64 impl_->SetStatus(service_name, serving ? HealthCheckResponse::SERVING
65 : HealthCheckResponse::NOT_SERVING);
68 void SetServingStatus(
bool serving)
override {
69 impl_->SetAll(serving ? HealthCheckResponse::SERVING
70 : HealthCheckResponse::NOT_SERVING);
81 HealthServiceEnd2endTest() {}
83 void SetUpServer(
bool register_sync_test_service,
bool add_async_cq,
84 bool explicit_health_service,
85 std::unique_ptr<HealthCheckServiceInterface>
service) {
89 bool register_sync_health_service_impl =
90 explicit_health_service &&
service !=
nullptr;
94 if (explicit_health_service) {
95 std::unique_ptr<ServerBuilderOption>
option(
101 if (register_sync_test_service) {
105 if (register_sync_health_service_impl) {
114 void TearDown()
override {
117 if (
cq_ !=
nullptr) {
133 void SendHealthCheckRpc(
const std::string& service_name,
134 const Status& expected_status) {
136 SendHealthCheckRpc(service_name, expected_status,
137 HealthCheckResponse::UNKNOWN);
140 void SendHealthCheckRpc(
142 HealthCheckResponse::ServingStatus expected_serving_status) {
144 request.set_service(service_name);
148 EXPECT_EQ(expected_status.error_code(),
s.error_code());
154 void VerifyHealthCheckService() {
155 HealthCheckServiceInterface*
service =
server_->GetHealthCheckService();
157 const std::string kHealthyService(
"healthy_service");
158 const std::string kUnhealthyService(
"unhealthy_service");
159 const std::string kNotRegisteredService(
"not_registered");
160 service->SetServingStatus(kHealthyService,
true);
161 service->SetServingStatus(kUnhealthyService,
false);
165 SendHealthCheckRpc(
"",
Status::OK, HealthCheckResponse::SERVING);
166 SendHealthCheckRpc(kHealthyService,
Status::OK,
167 HealthCheckResponse::SERVING);
168 SendHealthCheckRpc(kUnhealthyService,
Status::OK,
169 HealthCheckResponse::NOT_SERVING);
170 SendHealthCheckRpc(kNotRegisteredService,
173 service->SetServingStatus(
false);
174 SendHealthCheckRpc(
"",
Status::OK, HealthCheckResponse::NOT_SERVING);
175 SendHealthCheckRpc(kHealthyService,
Status::OK,
176 HealthCheckResponse::NOT_SERVING);
177 SendHealthCheckRpc(kUnhealthyService,
Status::OK,
178 HealthCheckResponse::NOT_SERVING);
179 SendHealthCheckRpc(kNotRegisteredService,
183 void VerifyHealthCheckServiceStreaming() {
185 HealthCheckServiceInterface*
service =
server_->GetHealthCheckService();
189 request.set_service(kServiceName);
190 std::unique_ptr<grpc::ClientReaderInterface<HealthCheckResponse>>
reader =
198 service->SetServingStatus(kServiceName,
false);
203 service->SetServingStatus(kServiceName,
true);
217 void VerifyHealthCheckServiceShutdown() {
218 HealthCheckServiceInterface*
service =
server_->GetHealthCheckService();
220 const std::string kHealthyService(
"healthy_service");
221 const std::string kUnhealthyService(
"unhealthy_service");
222 const std::string kNotRegisteredService(
"not_registered");
223 const std::string kNewService(
"add_after_shutdown");
224 service->SetServingStatus(kHealthyService,
true);
225 service->SetServingStatus(kUnhealthyService,
false);
232 request.set_service(kHealthyService);
233 std::unique_ptr<grpc::ClientReaderInterface<HealthCheckResponse>>
reader =
240 SendHealthCheckRpc(
"",
Status::OK, HealthCheckResponse::SERVING);
241 SendHealthCheckRpc(kHealthyService,
Status::OK,
242 HealthCheckResponse::SERVING);
243 SendHealthCheckRpc(kUnhealthyService,
Status::OK,
244 HealthCheckResponse::NOT_SERVING);
245 SendHealthCheckRpc(kNotRegisteredService,
258 SendHealthCheckRpc(
"",
Status::OK, HealthCheckResponse::NOT_SERVING);
259 SendHealthCheckRpc(kHealthyService,
Status::OK,
260 HealthCheckResponse::NOT_SERVING);
261 SendHealthCheckRpc(kUnhealthyService,
Status::OK,
262 HealthCheckResponse::NOT_SERVING);
263 SendHealthCheckRpc(kNotRegisteredService,
267 service->SetServingStatus(kHealthyService,
true);
268 SendHealthCheckRpc(kHealthyService,
Status::OK,
269 HealthCheckResponse::NOT_SERVING);
273 service->SetServingStatus(kNewService,
true);
275 HealthCheckResponse::NOT_SERVING);
281 std::unique_ptr<ServerCompletionQueue>
cq_;
287 TEST_F(HealthServiceEnd2endTest, DefaultHealthServiceDisabled) {
290 SetUpServer(
true,
false,
false,
nullptr);
291 HealthCheckServiceInterface* default_service =
292 server_->GetHealthCheckService();
300 TEST_F(HealthServiceEnd2endTest, DefaultHealthService) {
303 SetUpServer(
true,
false,
false,
nullptr);
304 VerifyHealthCheckService();
305 VerifyHealthCheckServiceStreaming();
309 SendHealthCheckRpc(kTooLongServiceName,
313 TEST_F(HealthServiceEnd2endTest, DefaultHealthServiceShutdown) {
316 SetUpServer(
true,
false,
false,
nullptr);
317 VerifyHealthCheckServiceShutdown();
321 TEST_F(HealthServiceEnd2endTest, ExplicitlyDisableViaOverride) {
324 std::unique_ptr<HealthCheckServiceInterface> empty_service;
325 SetUpServer(
true,
false,
true,
std::move(empty_service));
326 HealthCheckServiceInterface*
service =
server_->GetHealthCheckService();
335 TEST_F(HealthServiceEnd2endTest, ExplicitlyOverride) {
338 std::unique_ptr<HealthCheckServiceInterface> override_service(
340 HealthCheckServiceInterface* underlying_service = override_service.get();
341 SetUpServer(
false,
false,
true,
std::move(override_service));
342 HealthCheckServiceInterface*
service =
server_->GetHealthCheckService();
347 VerifyHealthCheckService();
348 VerifyHealthCheckServiceStreaming();
351 TEST_F(HealthServiceEnd2endTest, ExplicitlyHealthServiceShutdown) {
354 std::unique_ptr<HealthCheckServiceInterface> override_service(
356 HealthCheckServiceInterface* underlying_service = override_service.get();
357 SetUpServer(
false,
false,
true,
std::move(override_service));
358 HealthCheckServiceInterface*
service =
server_->GetHealthCheckService();
363 VerifyHealthCheckServiceShutdown();
370 int main(
int argc,
char** argv) {