high_initial_seqno.cc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 <string>
  21. #include "absl/strings/str_cat.h"
  22. #include <grpc/byte_buffer.h>
  23. #include <grpc/grpc.h>
  24. #include <grpc/support/alloc.h>
  25. #include <grpc/support/log.h>
  26. #include <grpc/support/time.h>
  27. #include "src/core/lib/gpr/string.h"
  28. #include "test/core/end2end/cq_verifier.h"
  29. #include "test/core/end2end/end2end_tests.h"
  30. static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); }
  31. static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
  32. const char* test_name,
  33. grpc_channel_args* client_args,
  34. grpc_channel_args* server_args) {
  35. grpc_end2end_test_fixture f;
  36. gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name);
  37. f = config.create_fixture(client_args, server_args);
  38. config.init_server(&f, server_args);
  39. config.init_client(&f, client_args);
  40. return f;
  41. }
  42. static gpr_timespec n_seconds_from_now(int n) {
  43. return grpc_timeout_seconds_to_deadline(n);
  44. }
  45. static gpr_timespec five_seconds_from_now(void) {
  46. return n_seconds_from_now(5);
  47. }
  48. static void drain_cq(grpc_completion_queue* cq) {
  49. grpc_event ev;
  50. do {
  51. ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr);
  52. } while (ev.type != GRPC_QUEUE_SHUTDOWN);
  53. }
  54. static void shutdown_server(grpc_end2end_test_fixture* f) {
  55. if (!f->server) return;
  56. grpc_server_shutdown_and_notify(f->server, f->shutdown_cq, tag(1000));
  57. GPR_ASSERT(grpc_completion_queue_pluck(f->shutdown_cq, tag(1000),
  58. grpc_timeout_seconds_to_deadline(5),
  59. nullptr)
  60. .type == GRPC_OP_COMPLETE);
  61. grpc_server_destroy(f->server);
  62. f->server = nullptr;
  63. }
  64. static void shutdown_client(grpc_end2end_test_fixture* f) {
  65. if (!f->client) return;
  66. grpc_channel_destroy(f->client);
  67. f->client = nullptr;
  68. }
  69. static void end_test(grpc_end2end_test_fixture* f) {
  70. shutdown_server(f);
  71. shutdown_client(f);
  72. grpc_completion_queue_shutdown(f->cq);
  73. drain_cq(f->cq);
  74. grpc_completion_queue_destroy(f->cq);
  75. grpc_completion_queue_destroy(f->shutdown_cq);
  76. }
  77. static void simple_request_body(grpc_end2end_test_config /*config*/,
  78. grpc_end2end_test_fixture f) {
  79. grpc_call* c;
  80. grpc_call* s;
  81. cq_verifier* cqv = cq_verifier_create(f.cq);
  82. grpc_op ops[6];
  83. grpc_op* op;
  84. grpc_metadata_array initial_metadata_recv;
  85. grpc_metadata_array trailing_metadata_recv;
  86. grpc_metadata_array request_metadata_recv;
  87. grpc_call_details call_details;
  88. grpc_status_code status;
  89. grpc_call_error error;
  90. grpc_slice details;
  91. int was_cancelled = 2;
  92. gpr_timespec deadline = five_seconds_from_now();
  93. c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
  94. grpc_slice_from_static_string("/foo"), nullptr,
  95. deadline, nullptr);
  96. GPR_ASSERT(c);
  97. grpc_metadata_array_init(&initial_metadata_recv);
  98. grpc_metadata_array_init(&trailing_metadata_recv);
  99. grpc_metadata_array_init(&request_metadata_recv);
  100. grpc_call_details_init(&call_details);
  101. memset(ops, 0, sizeof(ops));
  102. op = ops;
  103. op->op = GRPC_OP_SEND_INITIAL_METADATA;
  104. op->data.send_initial_metadata.count = 0;
  105. op->flags = 0;
  106. op->reserved = nullptr;
  107. op++;
  108. op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
  109. op->flags = 0;
  110. op->reserved = nullptr;
  111. op++;
  112. op->op = GRPC_OP_RECV_INITIAL_METADATA;
  113. op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
  114. op->flags = 0;
  115. op->reserved = nullptr;
  116. op++;
  117. op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
  118. op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
  119. op->data.recv_status_on_client.status = &status;
  120. op->data.recv_status_on_client.status_details = &details;
  121. op->flags = 0;
  122. op->reserved = nullptr;
  123. op++;
  124. error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
  125. nullptr);
  126. GPR_ASSERT(GRPC_CALL_OK == error);
  127. error =
  128. grpc_server_request_call(f.server, &s, &call_details,
  129. &request_metadata_recv, f.cq, f.cq, tag(101));
  130. GPR_ASSERT(GRPC_CALL_OK == error);
  131. CQ_EXPECT_COMPLETION(cqv, tag(101), 1);
  132. cq_verify(cqv);
  133. memset(ops, 0, sizeof(ops));
  134. op = ops;
  135. op->op = GRPC_OP_SEND_INITIAL_METADATA;
  136. op->data.send_initial_metadata.count = 0;
  137. op->flags = 0;
  138. op->reserved = nullptr;
  139. op++;
  140. op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
  141. op->data.send_status_from_server.trailing_metadata_count = 0;
  142. op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED;
  143. grpc_slice status_details = grpc_slice_from_static_string("xyz");
  144. op->data.send_status_from_server.status_details = &status_details;
  145. op->flags = 0;
  146. op->reserved = nullptr;
  147. op++;
  148. op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
  149. op->data.recv_close_on_server.cancelled = &was_cancelled;
  150. op->flags = 0;
  151. op->reserved = nullptr;
  152. op++;
  153. error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(102),
  154. nullptr);
  155. GPR_ASSERT(GRPC_CALL_OK == error);
  156. CQ_EXPECT_COMPLETION(cqv, tag(102), 1);
  157. CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
  158. cq_verify(cqv);
  159. GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED);
  160. GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz"));
  161. GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo"));
  162. GPR_ASSERT(was_cancelled == 0);
  163. grpc_slice_unref(details);
  164. grpc_metadata_array_destroy(&initial_metadata_recv);
  165. grpc_metadata_array_destroy(&trailing_metadata_recv);
  166. grpc_metadata_array_destroy(&request_metadata_recv);
  167. grpc_call_details_destroy(&call_details);
  168. grpc_call_unref(c);
  169. grpc_call_unref(s);
  170. /* TODO(ctiller): this rate limits the test, and it should be removed when
  171. retry has been implemented; until then cross-thread chatter
  172. may result in some requests needing to be cancelled due to
  173. seqno exhaustion. */
  174. cq_verify_empty(cqv);
  175. cq_verifier_destroy(cqv);
  176. }
  177. static void test_invoke_10_simple_requests(grpc_end2end_test_config config,
  178. int initial_sequence_number) {
  179. int i;
  180. grpc_end2end_test_fixture f;
  181. grpc_arg client_arg;
  182. grpc_channel_args client_args;
  183. client_arg.type = GRPC_ARG_INTEGER;
  184. client_arg.key = const_cast<char*>(GRPC_ARG_HTTP2_INITIAL_SEQUENCE_NUMBER);
  185. client_arg.value.integer = initial_sequence_number;
  186. client_args.num_args = 1;
  187. client_args.args = &client_arg;
  188. std::string name = absl::StrCat("test_invoke_requests first_seqno=",
  189. initial_sequence_number);
  190. f = begin_test(config, name.c_str(), &client_args, nullptr);
  191. for (i = 0; i < 10; i++) {
  192. simple_request_body(config, f);
  193. gpr_log(GPR_INFO, "Running test: Passed simple request %d", i);
  194. }
  195. end_test(&f);
  196. config.tear_down_data(&f);
  197. }
  198. void high_initial_seqno(grpc_end2end_test_config config) {
  199. test_invoke_10_simple_requests(config, 16777213);
  200. if (config.feature_mask & FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION) {
  201. test_invoke_10_simple_requests(config, 2147483645);
  202. }
  203. }
  204. void high_initial_seqno_pre_init(void) {}