disappearing_server.cc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 <stdio.h>
  19. #include <string.h>
  20. #include <grpc/byte_buffer.h>
  21. #include <grpc/support/alloc.h>
  22. #include <grpc/support/log.h>
  23. #include <grpc/support/time.h>
  24. #include "test/core/end2end/cq_verifier.h"
  25. #include "test/core/end2end/end2end_tests.h"
  26. static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); }
  27. static gpr_timespec n_seconds_from_now(int n) {
  28. return grpc_timeout_seconds_to_deadline(n);
  29. }
  30. static gpr_timespec five_seconds_from_now(void) {
  31. return n_seconds_from_now(5);
  32. }
  33. static void drain_cq(grpc_completion_queue* cq) {
  34. grpc_event ev;
  35. do {
  36. ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr);
  37. } while (ev.type != GRPC_QUEUE_SHUTDOWN);
  38. }
  39. static void shutdown_server(grpc_end2end_test_fixture* f) {
  40. if (!f->server) return;
  41. grpc_server_destroy(f->server);
  42. f->server = nullptr;
  43. }
  44. static void shutdown_client(grpc_end2end_test_fixture* f) {
  45. if (!f->client) return;
  46. grpc_channel_destroy(f->client);
  47. f->client = nullptr;
  48. }
  49. static void end_test(grpc_end2end_test_fixture* f) {
  50. shutdown_server(f);
  51. shutdown_client(f);
  52. grpc_completion_queue_shutdown(f->cq);
  53. drain_cq(f->cq);
  54. grpc_completion_queue_destroy(f->cq);
  55. /* Note: shutdown_cq was unused in this test */
  56. grpc_completion_queue_destroy(f->shutdown_cq);
  57. }
  58. static void do_request_and_shutdown_server(grpc_end2end_test_config /*config*/,
  59. grpc_end2end_test_fixture* f,
  60. cq_verifier* cqv) {
  61. grpc_call* c;
  62. grpc_call* s;
  63. grpc_op ops[6];
  64. grpc_op* op;
  65. grpc_metadata_array initial_metadata_recv;
  66. grpc_metadata_array trailing_metadata_recv;
  67. grpc_metadata_array request_metadata_recv;
  68. grpc_call_details call_details;
  69. grpc_status_code status;
  70. grpc_call_error error;
  71. grpc_slice details;
  72. int was_cancelled = 2;
  73. gpr_timespec deadline = five_seconds_from_now();
  74. c = grpc_channel_create_call(f->client, nullptr, GRPC_PROPAGATE_DEFAULTS,
  75. f->cq, grpc_slice_from_static_string("/foo"),
  76. nullptr, deadline, nullptr);
  77. GPR_ASSERT(c);
  78. grpc_metadata_array_init(&initial_metadata_recv);
  79. grpc_metadata_array_init(&trailing_metadata_recv);
  80. grpc_metadata_array_init(&request_metadata_recv);
  81. grpc_call_details_init(&call_details);
  82. memset(ops, 0, sizeof(ops));
  83. op = ops;
  84. op->op = GRPC_OP_SEND_INITIAL_METADATA;
  85. op->data.send_initial_metadata.count = 0;
  86. op->flags = 0;
  87. op->reserved = nullptr;
  88. op++;
  89. op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
  90. op->flags = 0;
  91. op->reserved = nullptr;
  92. op++;
  93. op->op = GRPC_OP_RECV_INITIAL_METADATA;
  94. op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
  95. op->flags = 0;
  96. op->reserved = nullptr;
  97. op++;
  98. op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
  99. op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
  100. op->data.recv_status_on_client.status = &status;
  101. op->data.recv_status_on_client.status_details = &details;
  102. op->flags = 0;
  103. op->reserved = nullptr;
  104. op++;
  105. error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
  106. nullptr);
  107. GPR_ASSERT(GRPC_CALL_OK == error);
  108. error =
  109. grpc_server_request_call(f->server, &s, &call_details,
  110. &request_metadata_recv, f->cq, f->cq, tag(101));
  111. GPR_ASSERT(GRPC_CALL_OK == error);
  112. CQ_EXPECT_COMPLETION(cqv, tag(101), 1);
  113. cq_verify(cqv);
  114. /* should be able to shut down the server early
  115. - and still complete the request */
  116. grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000));
  117. memset(ops, 0, sizeof(ops));
  118. op = ops;
  119. op->op = GRPC_OP_SEND_INITIAL_METADATA;
  120. op->data.send_initial_metadata.count = 0;
  121. op->flags = 0;
  122. op->reserved = nullptr;
  123. op++;
  124. op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
  125. op->data.send_status_from_server.trailing_metadata_count = 0;
  126. op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED;
  127. grpc_slice status_details = grpc_slice_from_static_string("xyz");
  128. op->data.send_status_from_server.status_details = &status_details;
  129. op->flags = 0;
  130. op->reserved = nullptr;
  131. op++;
  132. op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
  133. op->data.recv_close_on_server.cancelled = &was_cancelled;
  134. op->flags = 0;
  135. op->reserved = nullptr;
  136. op++;
  137. error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(102),
  138. nullptr);
  139. GPR_ASSERT(GRPC_CALL_OK == error);
  140. CQ_EXPECT_COMPLETION(cqv, tag(102), 1);
  141. CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
  142. CQ_EXPECT_COMPLETION(cqv, tag(1000), 1);
  143. cq_verify(cqv);
  144. /* Please refer https://github.com/grpc/grpc/issues/21221 for additional
  145. * details.
  146. * TODO(yashykt@) - The following line should be removeable after C-Core
  147. * correctly handles GOAWAY frames. Internal Reference b/135458602. If this
  148. * test remains flaky even after this, an alternative fix would be to send a
  149. * request when the server is in the shut down state.
  150. */
  151. cq_verify_empty(cqv);
  152. GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED);
  153. GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz"));
  154. GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo"));
  155. GPR_ASSERT(was_cancelled == 0);
  156. grpc_slice_unref(details);
  157. grpc_metadata_array_destroy(&initial_metadata_recv);
  158. grpc_metadata_array_destroy(&trailing_metadata_recv);
  159. grpc_metadata_array_destroy(&request_metadata_recv);
  160. grpc_call_details_destroy(&call_details);
  161. grpc_call_unref(c);
  162. grpc_call_unref(s);
  163. }
  164. static void disappearing_server_test(grpc_end2end_test_config config) {
  165. grpc_end2end_test_fixture f = config.create_fixture(nullptr, nullptr);
  166. cq_verifier* cqv = cq_verifier_create(f.cq);
  167. gpr_log(GPR_INFO, "Running test: %s/%s", "disappearing_server_test",
  168. config.name);
  169. config.init_client(&f, nullptr);
  170. config.init_server(&f, nullptr);
  171. do_request_and_shutdown_server(config, &f, cqv);
  172. /* now destroy and recreate the server */
  173. config.init_server(&f, nullptr);
  174. do_request_and_shutdown_server(config, &f, cqv);
  175. cq_verifier_destroy(cqv);
  176. end_test(&f);
  177. config.tear_down_data(&f);
  178. }
  179. void disappearing_server(grpc_end2end_test_config config) {
  180. GPR_ASSERT(config.feature_mask & FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION);
  181. #ifndef GPR_WINDOWS /* b/148110727 for more details */
  182. disappearing_server_test(config);
  183. #endif /* GPR_WINDOWS */
  184. }
  185. void disappearing_server_pre_init(void) {}