CFStreamClientTests.mm 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. *
  3. * Copyright 2018 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. #import <XCTest/XCTest.h>
  19. #include "src/core/lib/iomgr/port.h"
  20. #ifdef GRPC_CFSTREAM
  21. #include <netinet/in.h>
  22. #include <grpc/grpc.h>
  23. #include <grpc/impl/codegen/sync.h>
  24. #include <grpc/support/sync.h>
  25. #include "src/core/lib/address_utils/parse_address.h"
  26. #include "src/core/lib/address_utils/sockaddr_utils.h"
  27. #include "src/core/lib/iomgr/endpoint.h"
  28. #include "src/core/lib/iomgr/resolve_address.h"
  29. #include "src/core/lib/iomgr/tcp_client.h"
  30. #include "src/core/lib/resource_quota/api.h"
  31. #include "test/core/util/test_config.h"
  32. static gpr_mu g_mu;
  33. static int g_connections_complete = 0;
  34. static grpc_endpoint* g_connecting = nullptr;
  35. static void finish_connection() {
  36. gpr_mu_lock(&g_mu);
  37. g_connections_complete++;
  38. gpr_mu_unlock(&g_mu);
  39. }
  40. static void must_succeed(void* arg, grpc_error_handle error) {
  41. GPR_ASSERT(g_connecting != nullptr);
  42. GPR_ASSERT(error == GRPC_ERROR_NONE);
  43. grpc_endpoint_shutdown(g_connecting, GRPC_ERROR_CREATE_FROM_STATIC_STRING("must_succeed called"));
  44. grpc_endpoint_destroy(g_connecting);
  45. g_connecting = nullptr;
  46. finish_connection();
  47. }
  48. static void must_fail(void* arg, grpc_error_handle error) {
  49. GPR_ASSERT(g_connecting == nullptr);
  50. GPR_ASSERT(error != GRPC_ERROR_NONE);
  51. NSLog(@"%s", grpc_error_std_string(error).c_str());
  52. finish_connection();
  53. }
  54. @interface CFStreamClientTests : XCTestCase
  55. @end
  56. @implementation CFStreamClientTests
  57. + (void)setUp {
  58. grpc_init();
  59. gpr_mu_init(&g_mu);
  60. }
  61. + (void)tearDown {
  62. grpc_shutdown();
  63. }
  64. - (void)testSucceeds {
  65. grpc_resolved_address resolved_addr;
  66. struct sockaddr_in* addr = reinterpret_cast<struct sockaddr_in*>(resolved_addr.addr);
  67. int svr_fd;
  68. int r;
  69. int connections_complete_before;
  70. grpc_closure done;
  71. grpc_core::ExecCtx exec_ctx;
  72. gpr_log(GPR_DEBUG, "test_succeeds");
  73. GPR_ASSERT(grpc_string_to_sockaddr(&resolved_addr, "127.0.0.1", 0) == GRPC_ERROR_NONE);
  74. /* create a phony server */
  75. svr_fd = socket(AF_INET, SOCK_STREAM, 0);
  76. GPR_ASSERT(svr_fd >= 0);
  77. GPR_ASSERT(0 == bind(svr_fd, (struct sockaddr*)addr, (socklen_t)resolved_addr.len));
  78. GPR_ASSERT(0 == listen(svr_fd, 1));
  79. gpr_mu_lock(&g_mu);
  80. connections_complete_before = g_connections_complete;
  81. gpr_mu_unlock(&g_mu);
  82. /* connect to it */
  83. GPR_ASSERT(getsockname(svr_fd, (struct sockaddr*)addr, (socklen_t*)&resolved_addr.len) == 0);
  84. GRPC_CLOSURE_INIT(&done, must_succeed, nullptr, grpc_schedule_on_exec_ctx);
  85. const grpc_channel_args* args =
  86. grpc_core::CoreConfiguration::Get().channel_args_preconditioning().PreconditionChannelArgs(
  87. nullptr);
  88. grpc_tcp_client_connect(&done, &g_connecting, nullptr, args, &resolved_addr,
  89. grpc_core::Timestamp::InfFuture());
  90. grpc_channel_args_destroy(args);
  91. /* await the connection */
  92. do {
  93. resolved_addr.len = sizeof(addr);
  94. r = accept(svr_fd, reinterpret_cast<struct sockaddr*>(addr),
  95. reinterpret_cast<socklen_t*>(&resolved_addr.len));
  96. } while (r == -1 && errno == EINTR);
  97. GPR_ASSERT(r >= 0);
  98. close(r);
  99. grpc_core::ExecCtx::Get()->Flush();
  100. /* wait for the connection callback to finish */
  101. gpr_mu_lock(&g_mu);
  102. NSDate* deadline = [NSDate dateWithTimeIntervalSinceNow:5];
  103. while (connections_complete_before == g_connections_complete) {
  104. gpr_mu_unlock(&g_mu);
  105. [[NSRunLoop mainRunLoop] runMode:NSDefaultRunLoopMode beforeDate:deadline];
  106. gpr_mu_lock(&g_mu);
  107. }
  108. XCTAssertGreaterThan(g_connections_complete, connections_complete_before);
  109. gpr_mu_unlock(&g_mu);
  110. }
  111. - (void)testFails {
  112. grpc_core::ExecCtx exec_ctx;
  113. grpc_resolved_address resolved_addr;
  114. struct sockaddr_in* addr = reinterpret_cast<struct sockaddr_in*>(resolved_addr.addr);
  115. int connections_complete_before;
  116. grpc_closure done;
  117. int svr_fd;
  118. gpr_log(GPR_DEBUG, "test_fails");
  119. GPR_ASSERT(grpc_string_to_sockaddr(&resolved_addr, "127.0.0.1", 0) == GRPC_ERROR_NONE);
  120. svr_fd = socket(AF_INET, SOCK_STREAM, 0);
  121. GPR_ASSERT(svr_fd >= 0);
  122. GPR_ASSERT(0 == bind(svr_fd, (struct sockaddr*)addr, (socklen_t)resolved_addr.len));
  123. GPR_ASSERT(0 == listen(svr_fd, 1));
  124. GPR_ASSERT(getsockname(svr_fd, (struct sockaddr*)addr, (socklen_t*)&resolved_addr.len) == 0);
  125. close(svr_fd);
  126. gpr_mu_lock(&g_mu);
  127. connections_complete_before = g_connections_complete;
  128. gpr_mu_unlock(&g_mu);
  129. /* connect to a broken address */
  130. GRPC_CLOSURE_INIT(&done, must_fail, nullptr, grpc_schedule_on_exec_ctx);
  131. const grpc_channel_args* args =
  132. grpc_core::CoreConfiguration::Get().channel_args_preconditioning().PreconditionChannelArgs(
  133. nullptr);
  134. grpc_tcp_client_connect(&done, &g_connecting, nullptr, args, &resolved_addr,
  135. grpc_core::Timestamp::InfFuture());
  136. grpc_channel_args_destroy(args);
  137. grpc_core::ExecCtx::Get()->Flush();
  138. /* wait for the connection callback to finish */
  139. gpr_mu_lock(&g_mu);
  140. NSDate* deadline = [NSDate dateWithTimeIntervalSinceNow:5];
  141. while (g_connections_complete == connections_complete_before) {
  142. gpr_mu_unlock(&g_mu);
  143. [[NSRunLoop mainRunLoop] runMode:NSDefaultRunLoopMode beforeDate:deadline];
  144. gpr_mu_lock(&g_mu);
  145. }
  146. XCTAssertGreaterThan(g_connections_complete, connections_complete_before);
  147. gpr_mu_unlock(&g_mu);
  148. }
  149. @end
  150. #else // GRPC_CFSTREAM
  151. // Phony test suite
  152. @interface CFStreamClientTests : XCTestCase
  153. @end
  154. @implementation CFStreamClientTests
  155. - (void)setUp {
  156. [super setUp];
  157. }
  158. - (void)tearDown {
  159. [super tearDown];
  160. }
  161. @end
  162. #endif // GRPC_CFSTREAM