connectivity.cc 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. *
  3. * Copyright 2015 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/log.h>
  19. #include <grpc/support/sync.h>
  20. #include <grpc/support/time.h>
  21. #include "src/core/lib/gprpp/thd.h"
  22. #include "test/core/end2end/cq_verifier.h"
  23. #include "test/core/end2end/end2end_tests.h"
  24. static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); }
  25. typedef struct {
  26. gpr_event started;
  27. grpc_channel* channel;
  28. grpc_completion_queue* cq;
  29. } child_events;
  30. struct CallbackContext {
  31. grpc_completion_queue_functor functor;
  32. gpr_event finished;
  33. explicit CallbackContext(void (*cb)(grpc_completion_queue_functor* functor,
  34. int success)) {
  35. functor.functor_run = cb;
  36. functor.inlineable = false;
  37. gpr_event_init(&finished);
  38. }
  39. };
  40. static void child_thread(void* arg) {
  41. child_events* ce = static_cast<child_events*>(arg);
  42. grpc_event ev;
  43. gpr_event_set(&ce->started, reinterpret_cast<void*>(1));
  44. gpr_log(GPR_DEBUG, "verifying");
  45. ev = grpc_completion_queue_next(ce->cq, gpr_inf_future(GPR_CLOCK_MONOTONIC),
  46. nullptr);
  47. GPR_ASSERT(ev.type == GRPC_OP_COMPLETE);
  48. GPR_ASSERT(ev.tag == tag(1));
  49. GPR_ASSERT(ev.success == 0);
  50. }
  51. static void test_connectivity(grpc_end2end_test_config config) {
  52. grpc_end2end_test_fixture f = config.create_fixture(nullptr, nullptr);
  53. grpc_connectivity_state state;
  54. cq_verifier* cqv = cq_verifier_create(f.cq);
  55. child_events ce;
  56. grpc_channel_args client_args;
  57. grpc_arg arg_array[1];
  58. arg_array[0].type = GRPC_ARG_INTEGER;
  59. arg_array[0].key =
  60. const_cast<char*>("grpc.testing.fixed_reconnect_backoff_ms");
  61. arg_array[0].value.integer = 1000;
  62. client_args.args = arg_array;
  63. client_args.num_args = 1;
  64. config.init_client(&f, &client_args);
  65. ce.channel = f.client;
  66. ce.cq = f.cq;
  67. gpr_event_init(&ce.started);
  68. grpc_core::Thread thd("grpc_connectivity", child_thread, &ce);
  69. thd.Start();
  70. gpr_event_wait(&ce.started, gpr_inf_future(GPR_CLOCK_MONOTONIC));
  71. /* channels should start life in IDLE, and stay there */
  72. GPR_ASSERT(grpc_channel_check_connectivity_state(f.client, 0) ==
  73. GRPC_CHANNEL_IDLE);
  74. gpr_sleep_until(grpc_timeout_milliseconds_to_deadline(100));
  75. GPR_ASSERT(grpc_channel_check_connectivity_state(f.client, 0) ==
  76. GRPC_CHANNEL_IDLE);
  77. /* start watching for a change */
  78. gpr_log(GPR_DEBUG, "watching");
  79. grpc_channel_watch_connectivity_state(
  80. f.client, GRPC_CHANNEL_IDLE, gpr_now(GPR_CLOCK_MONOTONIC), f.cq, tag(1));
  81. /* eventually the child thread completion should trigger */
  82. thd.Join();
  83. /* check that we're still in idle, and start connecting */
  84. GPR_ASSERT(grpc_channel_check_connectivity_state(f.client, 1) ==
  85. GRPC_CHANNEL_IDLE);
  86. /* start watching for a change */
  87. grpc_channel_watch_connectivity_state(f.client, GRPC_CHANNEL_IDLE,
  88. grpc_timeout_seconds_to_deadline(3),
  89. f.cq, tag(2));
  90. /* and now the watch should trigger */
  91. CQ_EXPECT_COMPLETION(cqv, tag(2), 1);
  92. cq_verify(cqv);
  93. state = grpc_channel_check_connectivity_state(f.client, 0);
  94. GPR_ASSERT(state == GRPC_CHANNEL_TRANSIENT_FAILURE ||
  95. state == GRPC_CHANNEL_CONNECTING);
  96. /* quickly followed by a transition to TRANSIENT_FAILURE */
  97. grpc_channel_watch_connectivity_state(f.client, GRPC_CHANNEL_CONNECTING,
  98. grpc_timeout_seconds_to_deadline(3),
  99. f.cq, tag(3));
  100. CQ_EXPECT_COMPLETION(cqv, tag(3), 1);
  101. cq_verify(cqv);
  102. state = grpc_channel_check_connectivity_state(f.client, 0);
  103. GPR_ASSERT(state == GRPC_CHANNEL_TRANSIENT_FAILURE ||
  104. state == GRPC_CHANNEL_CONNECTING);
  105. gpr_log(GPR_DEBUG, "*** STARTING SERVER ***");
  106. /* now let's bring up a server to connect to */
  107. config.init_server(&f, nullptr);
  108. gpr_log(GPR_DEBUG, "*** STARTED SERVER ***");
  109. /* we'll go through some set of transitions (some might be missed), until
  110. READY is reached */
  111. while (state != GRPC_CHANNEL_READY) {
  112. grpc_channel_watch_connectivity_state(
  113. f.client, state, grpc_timeout_seconds_to_deadline(3), f.cq, tag(4));
  114. CQ_EXPECT_COMPLETION(cqv, tag(4), 1);
  115. cq_verify(cqv);
  116. state = grpc_channel_check_connectivity_state(f.client, 0);
  117. GPR_ASSERT(state == GRPC_CHANNEL_READY ||
  118. state == GRPC_CHANNEL_CONNECTING ||
  119. state == GRPC_CHANNEL_TRANSIENT_FAILURE);
  120. }
  121. /* bring down the server again */
  122. /* we should go immediately to TRANSIENT_FAILURE */
  123. gpr_log(GPR_DEBUG, "*** SHUTTING DOWN SERVER ***");
  124. grpc_channel_watch_connectivity_state(f.client, GRPC_CHANNEL_READY,
  125. grpc_timeout_seconds_to_deadline(3),
  126. f.cq, tag(5));
  127. grpc_server_shutdown_and_notify(f.server, f.cq, tag(0xdead));
  128. CQ_EXPECT_COMPLETION(cqv, tag(5), 1);
  129. CQ_EXPECT_COMPLETION(cqv, tag(0xdead), 1);
  130. cq_verify(cqv);
  131. state = grpc_channel_check_connectivity_state(f.client, 0);
  132. GPR_ASSERT(state == GRPC_CHANNEL_TRANSIENT_FAILURE ||
  133. state == GRPC_CHANNEL_CONNECTING || state == GRPC_CHANNEL_IDLE);
  134. /* cleanup server */
  135. grpc_server_destroy(f.server);
  136. gpr_log(GPR_DEBUG, "*** SHUTDOWN SERVER ***");
  137. grpc_channel_destroy(f.client);
  138. grpc_completion_queue_shutdown(f.cq);
  139. grpc_completion_queue_destroy(f.cq);
  140. /* shutdown_cq is not used in this test */
  141. grpc_completion_queue_destroy(f.shutdown_cq);
  142. config.tear_down_data(&f);
  143. cq_verifier_destroy(cqv);
  144. }
  145. static void cb_watch_connectivity(grpc_completion_queue_functor* functor,
  146. int success) {
  147. CallbackContext* cb_ctx = reinterpret_cast<CallbackContext*>(functor);
  148. gpr_log(GPR_DEBUG, "cb_watch_connectivity called, verifying");
  149. /* callback must not have errors */
  150. GPR_ASSERT(success != 0);
  151. gpr_event_set(&cb_ctx->finished, reinterpret_cast<void*>(1));
  152. }
  153. static void cb_shutdown(grpc_completion_queue_functor* functor,
  154. int /*success*/) {
  155. CallbackContext* cb_ctx = reinterpret_cast<CallbackContext*>(functor);
  156. gpr_log(GPR_DEBUG, "cb_shutdown called, nothing to do");
  157. gpr_event_set(&cb_ctx->finished, reinterpret_cast<void*>(1));
  158. }
  159. static void test_watch_connectivity_cq_callback(
  160. grpc_end2end_test_config config) {
  161. CallbackContext cb_ctx(cb_watch_connectivity);
  162. CallbackContext cb_shutdown_ctx(cb_shutdown);
  163. grpc_completion_queue* cq;
  164. grpc_end2end_test_fixture f = config.create_fixture(nullptr, nullptr);
  165. config.init_client(&f, nullptr);
  166. /* start connecting */
  167. grpc_channel_check_connectivity_state(f.client, 1);
  168. /* create the cq callback */
  169. cq = grpc_completion_queue_create_for_callback(&cb_shutdown_ctx.functor,
  170. nullptr);
  171. /* start watching for any change, cb is immediately called
  172. * and no dead lock should be raised */
  173. grpc_channel_watch_connectivity_state(f.client, GRPC_CHANNEL_IDLE,
  174. grpc_timeout_seconds_to_deadline(3), cq,
  175. &cb_ctx.functor);
  176. /* we just check that the callback was executed once notifying a connection
  177. * transition */
  178. GPR_ASSERT(gpr_event_wait(&cb_ctx.finished,
  179. gpr_inf_future(GPR_CLOCK_MONOTONIC)) != nullptr);
  180. /* shutdown, since shutdown cb might be executed in a background thread
  181. * we actively wait till is executed. */
  182. grpc_completion_queue_shutdown(cq);
  183. gpr_event_wait(&cb_shutdown_ctx.finished,
  184. gpr_inf_future(GPR_CLOCK_MONOTONIC));
  185. /* cleanup */
  186. grpc_channel_destroy(f.client);
  187. grpc_completion_queue_destroy(cq);
  188. /* shutdown_cq and cq are not used in this test */
  189. grpc_completion_queue_destroy(f.cq);
  190. grpc_completion_queue_destroy(f.shutdown_cq);
  191. config.tear_down_data(&f);
  192. }
  193. void connectivity(grpc_end2end_test_config config) {
  194. GPR_ASSERT(config.feature_mask & FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION);
  195. test_connectivity(config);
  196. test_watch_connectivity_cq_callback(config);
  197. }
  198. void connectivity_pre_init(void) {}