channelz_sampler_test.cc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. *
  3. * Copyright 2016 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #include <grpc/support/port_platform.h>
  19. #include <stdlib.h>
  20. #include <unistd.h>
  21. #include <cstdlib>
  22. #include <iostream>
  23. #include <memory>
  24. #include <string>
  25. #include <thread>
  26. #include "gtest/gtest.h"
  27. #include <grpc/grpc.h>
  28. #include <grpc/support/alloc.h>
  29. #include <grpcpp/channel.h>
  30. #include <grpcpp/client_context.h>
  31. #include <grpcpp/create_channel.h>
  32. #include <grpcpp/ext/channelz_service_plugin.h>
  33. #include <grpcpp/grpcpp.h>
  34. #include <grpcpp/security/credentials.h>
  35. #include <grpcpp/security/server_credentials.h>
  36. #include <grpcpp/server.h>
  37. #include <grpcpp/server_builder.h>
  38. #include <grpcpp/server_context.h>
  39. #include "src/core/lib/gpr/env.h"
  40. #include "src/cpp/server/channelz/channelz_service.h"
  41. #include "src/proto/grpc/testing/test.grpc.pb.h"
  42. #include "test/core/util/test_config.h"
  43. #include "test/cpp/util/subprocess.h"
  44. #include "test/cpp/util/test_credentials_provider.h"
  45. static std::string g_root;
  46. namespace {
  47. using grpc::ClientContext;
  48. using grpc::Server;
  49. using grpc::ServerBuilder;
  50. using grpc::ServerContext;
  51. using grpc::Status;
  52. } // namespace
  53. // Test variables
  54. std::string server_address("0.0.0.0:10000");
  55. std::string custom_credentials_type("INSECURE_CREDENTIALS");
  56. std::string sampling_times = "2";
  57. std::string sampling_interval_seconds = "3";
  58. std::string output_json("output.json");
  59. // Creata an echo server
  60. class EchoServerImpl final : public grpc::testing::TestService::Service {
  61. Status EmptyCall(grpc::ServerContext* /*context*/,
  62. const grpc::testing::Empty* /*request*/,
  63. grpc::testing::Empty* /*response*/) override {
  64. return Status::OK;
  65. }
  66. };
  67. // Run client in a thread
  68. void RunClient(const std::string& client_id, gpr_event* done_ev) {
  69. grpc::ChannelArguments channel_args;
  70. std::shared_ptr<grpc::ChannelCredentials> channel_creds =
  71. grpc::testing::GetCredentialsProvider()->GetChannelCredentials(
  72. custom_credentials_type, &channel_args);
  73. std::unique_ptr<grpc::testing::TestService::Stub> stub =
  74. grpc::testing::TestService::NewStub(
  75. grpc::CreateChannel(server_address, channel_creds));
  76. gpr_log(GPR_INFO, "Client %s is echoing!", client_id.c_str());
  77. while (true) {
  78. if (gpr_event_wait(done_ev, grpc_timeout_seconds_to_deadline(1)) !=
  79. nullptr) {
  80. return;
  81. }
  82. grpc::testing::Empty request;
  83. grpc::testing::Empty response;
  84. ClientContext context;
  85. Status status = stub->EmptyCall(&context, request, &response);
  86. if (!status.ok()) {
  87. gpr_log(GPR_ERROR, "Client echo failed.");
  88. GPR_ASSERT(0);
  89. }
  90. }
  91. }
  92. // Create the channelz to test the connection to the server
  93. bool WaitForConnection(int wait_server_seconds) {
  94. grpc::ChannelArguments channel_args;
  95. std::shared_ptr<grpc::ChannelCredentials> channel_creds =
  96. grpc::testing::GetCredentialsProvider()->GetChannelCredentials(
  97. custom_credentials_type, &channel_args);
  98. auto channel = grpc::CreateChannel(server_address, channel_creds);
  99. return channel->WaitForConnected(
  100. grpc_timeout_seconds_to_deadline(wait_server_seconds));
  101. }
  102. // Test the channelz sampler
  103. TEST(ChannelzSamplerTest, SimpleTest) {
  104. // start server
  105. grpc::channelz::experimental::InitChannelzService();
  106. EchoServerImpl service;
  107. grpc::ServerBuilder builder;
  108. auto server_creds =
  109. grpc::testing::GetCredentialsProvider()->GetServerCredentials(
  110. custom_credentials_type);
  111. builder.AddListeningPort(server_address, server_creds);
  112. builder.RegisterService(&service);
  113. std::unique_ptr<Server> server(builder.BuildAndStart());
  114. gpr_log(GPR_INFO, "Server listening on %s", server_address.c_str());
  115. const int kWaitForServerSeconds = 10;
  116. ASSERT_TRUE(WaitForConnection(kWaitForServerSeconds));
  117. // client threads
  118. gpr_event done_ev1, done_ev2;
  119. gpr_event_init(&done_ev1);
  120. gpr_event_init(&done_ev2);
  121. std::thread client_thread_1(RunClient, "1", &done_ev1);
  122. std::thread client_thread_2(RunClient, "2", &done_ev2);
  123. // Run the channelz sampler
  124. grpc::SubProcess* test_driver = new grpc::SubProcess(
  125. {g_root + "/channelz_sampler", "--server_address=" + server_address,
  126. "--custom_credentials_type=" + custom_credentials_type,
  127. "--sampling_times=" + sampling_times,
  128. "--sampling_interval_seconds=" + sampling_interval_seconds,
  129. "--output_json=" + output_json});
  130. int status = test_driver->Join();
  131. if (WIFEXITED(status)) {
  132. if (WEXITSTATUS(status)) {
  133. gpr_log(GPR_ERROR,
  134. "Channelz sampler test test-runner exited with code %d",
  135. WEXITSTATUS(status));
  136. GPR_ASSERT(0); // log the line number of the assertion failure
  137. }
  138. } else if (WIFSIGNALED(status)) {
  139. gpr_log(GPR_ERROR, "Channelz sampler test test-runner ended from signal %d",
  140. WTERMSIG(status));
  141. GPR_ASSERT(0);
  142. } else {
  143. gpr_log(GPR_ERROR,
  144. "Channelz sampler test test-runner ended with unknown status %d",
  145. status);
  146. GPR_ASSERT(0);
  147. }
  148. delete test_driver;
  149. gpr_event_set(&done_ev1, reinterpret_cast<void*>(1));
  150. gpr_event_set(&done_ev2, reinterpret_cast<void*>(1));
  151. client_thread_1.join();
  152. client_thread_2.join();
  153. }
  154. int main(int argc, char** argv) {
  155. grpc::testing::TestEnvironment env(argc, argv);
  156. ::testing::InitGoogleTest(&argc, argv);
  157. std::string me = argv[0];
  158. auto lslash = me.rfind('/');
  159. if (lslash != std::string::npos) {
  160. g_root = me.substr(0, lslash);
  161. } else {
  162. g_root = ".";
  163. }
  164. int ret = RUN_ALL_TESTS();
  165. return ret;
  166. }