tcp_client_posix_test.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. *
  3. * Copyright 2015, 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 "src/core/lib/iomgr/tcp_client.h"
  34. #include <errno.h>
  35. #include <netinet/in.h>
  36. #include <string.h>
  37. #include <sys/socket.h>
  38. #include <unistd.h>
  39. #include <grpc/grpc.h>
  40. #include <grpc/support/alloc.h>
  41. #include <grpc/support/log.h>
  42. #include <grpc/support/time.h>
  43. #include "src/core/lib/iomgr/iomgr.h"
  44. #include "src/core/lib/iomgr/pollset_set.h"
  45. #include "src/core/lib/iomgr/socket_utils_posix.h"
  46. #include "src/core/lib/iomgr/timer.h"
  47. #include "test/core/util/test_config.h"
  48. static grpc_pollset_set *g_pollset_set;
  49. static gpr_mu *g_mu;
  50. static grpc_pollset *g_pollset;
  51. static int g_connections_complete = 0;
  52. static grpc_endpoint *g_connecting = NULL;
  53. static gpr_timespec test_deadline(void) {
  54. return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(10);
  55. }
  56. static void finish_connection() {
  57. gpr_mu_lock(g_mu);
  58. g_connections_complete++;
  59. GPR_ASSERT(
  60. GRPC_LOG_IF_ERROR("pollset_kick", grpc_pollset_kick(g_pollset, NULL)));
  61. gpr_mu_unlock(g_mu);
  62. }
  63. static void must_succeed(grpc_exec_ctx *exec_ctx, void *arg,
  64. grpc_error *error) {
  65. GPR_ASSERT(g_connecting != NULL);
  66. GPR_ASSERT(error == GRPC_ERROR_NONE);
  67. grpc_endpoint_shutdown(exec_ctx, g_connecting);
  68. grpc_endpoint_destroy(exec_ctx, g_connecting);
  69. g_connecting = NULL;
  70. finish_connection();
  71. }
  72. static void must_fail(grpc_exec_ctx *exec_ctx, void *arg, grpc_error *error) {
  73. GPR_ASSERT(g_connecting == NULL);
  74. GPR_ASSERT(error != GRPC_ERROR_NONE);
  75. finish_connection();
  76. }
  77. void test_succeeds(void) {
  78. struct sockaddr_in addr;
  79. socklen_t addr_len = sizeof(addr);
  80. int svr_fd;
  81. int r;
  82. int connections_complete_before;
  83. grpc_closure done;
  84. grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
  85. grpc_tcp_client_connect_args tcp_client_connect_args;
  86. gpr_log(GPR_DEBUG, "test_succeeds");
  87. memset(&addr, 0, sizeof(addr));
  88. addr.sin_family = AF_INET;
  89. /* create a dummy server */
  90. svr_fd = socket(AF_INET, SOCK_STREAM, 0);
  91. GPR_ASSERT(svr_fd >= 0);
  92. GPR_ASSERT(0 == bind(svr_fd, (struct sockaddr *)&addr, addr_len));
  93. GPR_ASSERT(0 == listen(svr_fd, 1));
  94. gpr_mu_lock(g_mu);
  95. connections_complete_before = g_connections_complete;
  96. gpr_mu_unlock(g_mu);
  97. /* connect to it */
  98. GPR_ASSERT(getsockname(svr_fd, (struct sockaddr *)&addr, &addr_len) == 0);
  99. grpc_closure_init(&done, must_succeed, NULL);
  100. tcp_client_connect_args.interested_parties = g_pollset_set;
  101. tcp_client_connect_args.addr = (struct sockaddr *)&addr;
  102. tcp_client_connect_args.addr_len = addr_len;
  103. tcp_client_connect_args.deadline = gpr_inf_future(GPR_CLOCK_REALTIME);
  104. tcp_client_connect_args.channel_args = NULL;
  105. grpc_tcp_client_connect(&exec_ctx, &done, &g_connecting,
  106. &tcp_client_connect_args);
  107. /* await the connection */
  108. do {
  109. addr_len = sizeof(addr);
  110. r = accept(svr_fd, (struct sockaddr *)&addr, &addr_len);
  111. } while (r == -1 && errno == EINTR);
  112. GPR_ASSERT(r >= 0);
  113. close(r);
  114. gpr_mu_lock(g_mu);
  115. while (g_connections_complete == connections_complete_before) {
  116. grpc_pollset_worker *worker = NULL;
  117. GPR_ASSERT(GRPC_LOG_IF_ERROR(
  118. "pollset_work",
  119. grpc_pollset_work(&exec_ctx, g_pollset, &worker,
  120. gpr_now(GPR_CLOCK_MONOTONIC),
  121. GRPC_TIMEOUT_SECONDS_TO_DEADLINE(5))));
  122. gpr_mu_unlock(g_mu);
  123. grpc_exec_ctx_flush(&exec_ctx);
  124. gpr_mu_lock(g_mu);
  125. }
  126. gpr_mu_unlock(g_mu);
  127. grpc_exec_ctx_finish(&exec_ctx);
  128. }
  129. void test_fails(void) {
  130. struct sockaddr_in addr;
  131. socklen_t addr_len = sizeof(addr);
  132. int connections_complete_before;
  133. grpc_closure done;
  134. grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
  135. grpc_tcp_client_connect_args tcp_client_connect_args;
  136. gpr_log(GPR_DEBUG, "test_fails");
  137. memset(&addr, 0, sizeof(addr));
  138. addr.sin_family = AF_INET;
  139. gpr_mu_lock(g_mu);
  140. connections_complete_before = g_connections_complete;
  141. gpr_mu_unlock(g_mu);
  142. /* connect to a broken address */
  143. grpc_closure_init(&done, must_fail, NULL);
  144. tcp_client_connect_args.interested_parties = g_pollset_set;
  145. tcp_client_connect_args.addr = (struct sockaddr *)&addr;
  146. tcp_client_connect_args.addr_len = addr_len;
  147. tcp_client_connect_args.deadline = gpr_inf_future(GPR_CLOCK_REALTIME);
  148. tcp_client_connect_args.channel_args = NULL;
  149. grpc_tcp_client_connect(&exec_ctx, &done, &g_connecting,
  150. &tcp_client_connect_args);
  151. gpr_mu_lock(g_mu);
  152. /* wait for the connection callback to finish */
  153. while (g_connections_complete == connections_complete_before) {
  154. grpc_pollset_worker *worker = NULL;
  155. gpr_timespec now = gpr_now(GPR_CLOCK_MONOTONIC);
  156. gpr_timespec polling_deadline = test_deadline();
  157. if (!grpc_timer_check(&exec_ctx, now, &polling_deadline)) {
  158. GPR_ASSERT(GRPC_LOG_IF_ERROR(
  159. "pollset_work", grpc_pollset_work(&exec_ctx, g_pollset, &worker, now,
  160. polling_deadline)));
  161. }
  162. gpr_mu_unlock(g_mu);
  163. grpc_exec_ctx_flush(&exec_ctx);
  164. gpr_mu_lock(g_mu);
  165. }
  166. gpr_mu_unlock(g_mu);
  167. grpc_exec_ctx_finish(&exec_ctx);
  168. }
  169. static void destroy_pollset(grpc_exec_ctx *exec_ctx, void *p,
  170. grpc_error *error) {
  171. grpc_pollset_destroy(p);
  172. }
  173. int main(int argc, char **argv) {
  174. grpc_closure destroyed;
  175. grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
  176. grpc_test_init(argc, argv);
  177. grpc_init();
  178. g_pollset_set = grpc_pollset_set_create();
  179. g_pollset = gpr_malloc(grpc_pollset_size());
  180. grpc_pollset_init(g_pollset, &g_mu);
  181. grpc_pollset_set_add_pollset(&exec_ctx, g_pollset_set, g_pollset);
  182. grpc_exec_ctx_finish(&exec_ctx);
  183. test_succeeds();
  184. gpr_log(GPR_ERROR, "End of first test");
  185. test_fails();
  186. grpc_pollset_set_destroy(g_pollset_set);
  187. grpc_closure_init(&destroyed, destroy_pollset, g_pollset);
  188. grpc_pollset_shutdown(&exec_ctx, g_pollset, &destroyed);
  189. grpc_exec_ctx_finish(&exec_ctx);
  190. grpc_shutdown();
  191. gpr_free(g_pollset);
  192. return 0;
  193. }