sequential_connectivity_test.cc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 <vector>
  19. #include <grpc/grpc.h>
  20. #include <grpc/grpc_security.h>
  21. #include <grpc/impl/codegen/grpc_types.h>
  22. #include <grpc/support/alloc.h>
  23. #include <grpc/support/log.h>
  24. #include "src/core/lib/channel/channel_args.h"
  25. #include "src/core/lib/gprpp/host_port.h"
  26. #include "src/core/lib/gprpp/thd.h"
  27. #include "src/core/lib/iomgr/exec_ctx.h"
  28. #include "src/core/lib/iomgr/load_file.h"
  29. #include "test/core/util/port.h"
  30. #include "test/core/util/test_config.h"
  31. #define CA_CERT_PATH "src/core/tsi/test_creds/ca.pem"
  32. #define SERVER_CERT_PATH "src/core/tsi/test_creds/server1.pem"
  33. #define SERVER_KEY_PATH "src/core/tsi/test_creds/server1.key"
  34. typedef struct test_fixture {
  35. const char* name;
  36. void (*add_server_port)(grpc_server* server, const char* addr);
  37. // Have the creds here so all the channels will share the same one to enabled
  38. // subchannel sharing if needed.
  39. grpc_channel_credentials* creds;
  40. } test_fixture;
  41. #define NUM_CONNECTIONS 100
  42. typedef struct {
  43. grpc_server* server;
  44. grpc_completion_queue* cq;
  45. } server_thread_args;
  46. static void server_thread_func(void* args) {
  47. server_thread_args* a = static_cast<server_thread_args*>(args);
  48. grpc_event ev = grpc_completion_queue_next(
  49. a->cq, gpr_inf_future(GPR_CLOCK_REALTIME), nullptr);
  50. GPR_ASSERT(ev.type == GRPC_OP_COMPLETE);
  51. GPR_ASSERT(ev.tag == nullptr);
  52. GPR_ASSERT(ev.success == true);
  53. }
  54. static grpc_channel* create_test_channel(const char* addr,
  55. grpc_channel_credentials* creds,
  56. bool share_subchannel) {
  57. grpc_channel* channel = nullptr;
  58. std::vector<grpc_arg> args;
  59. args.push_back(grpc_channel_arg_integer_create(
  60. const_cast<char*>(GRPC_ARG_USE_LOCAL_SUBCHANNEL_POOL),
  61. !share_subchannel));
  62. if (creds != nullptr) {
  63. args.push_back(grpc_channel_arg_string_create(
  64. const_cast<char*>(GRPC_SSL_TARGET_NAME_OVERRIDE_ARG),
  65. const_cast<char*>("foo.test.google.fr")));
  66. }
  67. grpc_channel_args channel_args = {args.size(), args.data()};
  68. if (creds != nullptr) {
  69. channel = grpc_channel_create(addr, creds, &channel_args);
  70. } else {
  71. grpc_channel_credentials* insecure_creds =
  72. grpc_insecure_credentials_create();
  73. channel = grpc_channel_create(addr, insecure_creds, &channel_args);
  74. grpc_channel_credentials_release(insecure_creds);
  75. }
  76. return channel;
  77. }
  78. static void run_test(const test_fixture* fixture, bool share_subchannel) {
  79. gpr_log(GPR_INFO, "TEST: %s sharing subchannel: %d", fixture->name,
  80. share_subchannel);
  81. std::string addr =
  82. grpc_core::JoinHostPort("localhost", grpc_pick_unused_port_or_die());
  83. grpc_server* server = grpc_server_create(nullptr, nullptr);
  84. fixture->add_server_port(server, addr.c_str());
  85. grpc_completion_queue* server_cq =
  86. grpc_completion_queue_create_for_next(nullptr);
  87. grpc_server_register_completion_queue(server, server_cq, nullptr);
  88. grpc_server_start(server);
  89. server_thread_args sta = {server, server_cq};
  90. grpc_core::Thread server_thread("grpc_server", server_thread_func, &sta);
  91. server_thread.Start();
  92. grpc_completion_queue* cq = grpc_completion_queue_create_for_next(nullptr);
  93. grpc_channel* channels[NUM_CONNECTIONS];
  94. for (size_t i = 0; i < NUM_CONNECTIONS; i++) {
  95. channels[i] =
  96. create_test_channel(addr.c_str(), fixture->creds, share_subchannel);
  97. gpr_timespec connect_deadline = grpc_timeout_seconds_to_deadline(30);
  98. grpc_connectivity_state state;
  99. while ((state = grpc_channel_check_connectivity_state(channels[i], 1)) !=
  100. GRPC_CHANNEL_READY) {
  101. grpc_channel_watch_connectivity_state(channels[i], state,
  102. connect_deadline, cq, nullptr);
  103. grpc_event ev = grpc_completion_queue_next(
  104. cq, gpr_inf_future(GPR_CLOCK_REALTIME), nullptr);
  105. /* check that the watcher from "watch state" was free'd */
  106. GPR_ASSERT(grpc_channel_num_external_connectivity_watchers(channels[i]) ==
  107. 0);
  108. GPR_ASSERT(ev.type == GRPC_OP_COMPLETE);
  109. GPR_ASSERT(ev.tag == nullptr);
  110. GPR_ASSERT(ev.success == true);
  111. }
  112. }
  113. grpc_server_shutdown_and_notify(server, server_cq, nullptr);
  114. server_thread.Join();
  115. grpc_completion_queue_shutdown(server_cq);
  116. grpc_completion_queue_shutdown(cq);
  117. while (grpc_completion_queue_next(server_cq,
  118. gpr_inf_future(GPR_CLOCK_REALTIME), nullptr)
  119. .type != GRPC_QUEUE_SHUTDOWN) {
  120. }
  121. while (grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME),
  122. nullptr)
  123. .type != GRPC_QUEUE_SHUTDOWN) {
  124. }
  125. for (size_t i = 0; i < NUM_CONNECTIONS; i++) {
  126. grpc_channel_destroy(channels[i]);
  127. }
  128. grpc_server_destroy(server);
  129. grpc_completion_queue_destroy(server_cq);
  130. grpc_completion_queue_destroy(cq);
  131. }
  132. static void insecure_test_add_port(grpc_server* server, const char* addr) {
  133. grpc_server_credentials* server_creds =
  134. grpc_insecure_server_credentials_create();
  135. grpc_server_add_http2_port(server, addr, server_creds);
  136. grpc_server_credentials_release(server_creds);
  137. }
  138. static void secure_test_add_port(grpc_server* server, const char* addr) {
  139. grpc_slice cert_slice, key_slice;
  140. GPR_ASSERT(GRPC_LOG_IF_ERROR(
  141. "load_file", grpc_load_file(SERVER_CERT_PATH, 1, &cert_slice)));
  142. GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
  143. grpc_load_file(SERVER_KEY_PATH, 1, &key_slice)));
  144. const char* server_cert =
  145. reinterpret_cast<const char*> GRPC_SLICE_START_PTR(cert_slice);
  146. const char* server_key =
  147. reinterpret_cast<const char*> GRPC_SLICE_START_PTR(key_slice);
  148. grpc_ssl_pem_key_cert_pair pem_key_cert_pair = {server_key, server_cert};
  149. grpc_server_credentials* ssl_creds = grpc_ssl_server_credentials_create(
  150. nullptr, &pem_key_cert_pair, 1, 0, nullptr);
  151. grpc_slice_unref(cert_slice);
  152. grpc_slice_unref(key_slice);
  153. grpc_server_add_http2_port(server, addr, ssl_creds);
  154. grpc_server_credentials_release(ssl_creds);
  155. }
  156. int main(int argc, char** argv) {
  157. grpc::testing::TestEnvironment env(argc, argv);
  158. grpc_init();
  159. const test_fixture insecure_test = {
  160. "insecure",
  161. insecure_test_add_port,
  162. nullptr,
  163. };
  164. run_test(&insecure_test, /*share_subchannel=*/true);
  165. run_test(&insecure_test, /*share_subchannel=*/false);
  166. grpc_slice ca_slice;
  167. GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
  168. grpc_load_file(CA_CERT_PATH, 1, &ca_slice)));
  169. const char* test_root_cert =
  170. reinterpret_cast<const char*> GRPC_SLICE_START_PTR(ca_slice);
  171. grpc_channel_credentials* ssl_creds =
  172. grpc_ssl_credentials_create(test_root_cert, nullptr, nullptr, nullptr);
  173. grpc_slice_unref(ca_slice);
  174. const test_fixture secure_test = {
  175. "secure",
  176. secure_test_add_port,
  177. ssl_creds,
  178. };
  179. run_test(&secure_test, /*share_subchannel=*/true);
  180. run_test(&secure_test, /*share_subchannel=*/false);
  181. grpc_channel_credentials_release(ssl_creds);
  182. grpc_shutdown();
  183. }