shutdown_finishes_calls.cc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
  28. const char* test_name,
  29. grpc_channel_args* client_args,
  30. grpc_channel_args* server_args) {
  31. grpc_end2end_test_fixture f;
  32. gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name);
  33. f = config.create_fixture(client_args, server_args);
  34. config.init_server(&f, server_args);
  35. config.init_client(&f, client_args);
  36. return f;
  37. }
  38. static gpr_timespec n_seconds_from_now(int n) {
  39. return grpc_timeout_seconds_to_deadline(n);
  40. }
  41. static gpr_timespec five_seconds_from_now(void) {
  42. return n_seconds_from_now(5);
  43. }
  44. static void drain_cq(grpc_completion_queue* cq) {
  45. grpc_event ev;
  46. do {
  47. ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr);
  48. } while (ev.type != GRPC_QUEUE_SHUTDOWN);
  49. }
  50. static void shutdown_client(grpc_end2end_test_fixture* f) {
  51. if (!f->client) return;
  52. grpc_channel_destroy(f->client);
  53. f->client = nullptr;
  54. }
  55. static void end_test(grpc_end2end_test_fixture* f) {
  56. shutdown_client(f);
  57. grpc_completion_queue_shutdown(f->cq);
  58. drain_cq(f->cq);
  59. grpc_completion_queue_destroy(f->cq);
  60. /* f->shutdown_cq is not used in this test */
  61. grpc_completion_queue_destroy(f->shutdown_cq);
  62. }
  63. static void test_early_server_shutdown_finishes_inflight_calls(
  64. grpc_end2end_test_config config) {
  65. grpc_call* c;
  66. grpc_call* s;
  67. grpc_end2end_test_fixture f =
  68. begin_test(config, "test_early_server_shutdown_finishes_inflight_calls",
  69. nullptr, nullptr);
  70. cq_verifier* cqv = cq_verifier_create(f.cq);
  71. grpc_op ops[6];
  72. grpc_op* op;
  73. grpc_metadata_array initial_metadata_recv;
  74. grpc_metadata_array trailing_metadata_recv;
  75. grpc_metadata_array request_metadata_recv;
  76. grpc_call_details call_details;
  77. grpc_status_code status;
  78. grpc_call_error error;
  79. grpc_slice details;
  80. int was_cancelled = 2;
  81. gpr_timespec deadline = five_seconds_from_now();
  82. c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
  83. grpc_slice_from_static_string("/foo"), nullptr,
  84. deadline, nullptr);
  85. GPR_ASSERT(c);
  86. grpc_metadata_array_init(&initial_metadata_recv);
  87. grpc_metadata_array_init(&trailing_metadata_recv);
  88. grpc_metadata_array_init(&request_metadata_recv);
  89. grpc_call_details_init(&call_details);
  90. memset(ops, 0, sizeof(ops));
  91. op = ops;
  92. op->op = GRPC_OP_SEND_INITIAL_METADATA;
  93. op->data.send_initial_metadata.count = 0;
  94. op->data.send_initial_metadata.metadata = nullptr;
  95. op->flags = 0;
  96. op->reserved = nullptr;
  97. op++;
  98. op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
  99. op->flags = 0;
  100. op->reserved = nullptr;
  101. op++;
  102. op->op = GRPC_OP_RECV_INITIAL_METADATA;
  103. op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
  104. op->flags = 0;
  105. op->reserved = nullptr;
  106. op++;
  107. op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
  108. op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
  109. op->data.recv_status_on_client.status = &status;
  110. op->data.recv_status_on_client.status_details = &details;
  111. op->flags = 0;
  112. op->reserved = nullptr;
  113. op++;
  114. error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
  115. nullptr);
  116. GPR_ASSERT(GRPC_CALL_OK == error);
  117. error =
  118. grpc_server_request_call(f.server, &s, &call_details,
  119. &request_metadata_recv, f.cq, f.cq, tag(101));
  120. GPR_ASSERT(GRPC_CALL_OK == error);
  121. CQ_EXPECT_COMPLETION(cqv, tag(101), 1);
  122. cq_verify(cqv);
  123. memset(ops, 0, sizeof(ops));
  124. op = ops;
  125. op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
  126. op->data.recv_close_on_server.cancelled = &was_cancelled;
  127. op->flags = 0;
  128. op->reserved = nullptr;
  129. op++;
  130. error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(102),
  131. nullptr);
  132. GPR_ASSERT(GRPC_CALL_OK == error);
  133. /* Make sure we don't shutdown the server while HTTP/2 PING frames are still
  134. * being exchanged on the newly established connection. It can lead to
  135. * failures when testing with HTTP proxy. See
  136. * https://github.com/grpc/grpc/issues/14471
  137. */
  138. gpr_sleep_until(n_seconds_from_now(1));
  139. /* shutdown and destroy the server */
  140. grpc_server_shutdown_and_notify(f.server, f.cq, tag(1000));
  141. grpc_server_cancel_all_calls(f.server);
  142. CQ_EXPECT_COMPLETION(cqv, tag(1000), 1);
  143. CQ_EXPECT_COMPLETION(cqv, tag(102), 1);
  144. CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
  145. cq_verify(cqv);
  146. grpc_server_destroy(f.server);
  147. GPR_ASSERT(status == GRPC_STATUS_UNAVAILABLE);
  148. GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo"));
  149. GPR_ASSERT(was_cancelled == 1);
  150. grpc_slice_unref(details);
  151. grpc_metadata_array_destroy(&initial_metadata_recv);
  152. grpc_metadata_array_destroy(&trailing_metadata_recv);
  153. grpc_metadata_array_destroy(&request_metadata_recv);
  154. grpc_call_details_destroy(&call_details);
  155. grpc_call_unref(c);
  156. grpc_call_unref(s);
  157. cq_verifier_destroy(cqv);
  158. end_test(&f);
  159. config.tear_down_data(&f);
  160. }
  161. void shutdown_finishes_calls(grpc_end2end_test_config config) {
  162. test_early_server_shutdown_finishes_inflight_calls(config);
  163. }
  164. void shutdown_finishes_calls_pre_init(void) {}