simple_delayed_request.cc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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_shutdown_and_notify(f->server, f->shutdown_cq, tag(1000));
  42. GPR_ASSERT(grpc_completion_queue_pluck(f->shutdown_cq, tag(1000),
  43. grpc_timeout_seconds_to_deadline(5),
  44. nullptr)
  45. .type == GRPC_OP_COMPLETE);
  46. grpc_server_destroy(f->server);
  47. f->server = nullptr;
  48. }
  49. static void shutdown_client(grpc_end2end_test_fixture* f) {
  50. if (!f->client) return;
  51. grpc_channel_destroy(f->client);
  52. f->client = nullptr;
  53. }
  54. static void end_test(grpc_end2end_test_fixture* f) {
  55. shutdown_server(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. grpc_completion_queue_destroy(f->shutdown_cq);
  61. }
  62. static void simple_delayed_request_body(grpc_end2end_test_config config,
  63. grpc_end2end_test_fixture* f,
  64. grpc_channel_args* client_args,
  65. grpc_channel_args* server_args,
  66. long /*delay_us*/) {
  67. grpc_call* c;
  68. grpc_call* s;
  69. cq_verifier* cqv = cq_verifier_create(f->cq);
  70. grpc_op ops[6];
  71. grpc_op* op;
  72. grpc_metadata_array initial_metadata_recv;
  73. grpc_metadata_array trailing_metadata_recv;
  74. grpc_metadata_array request_metadata_recv;
  75. grpc_call_details call_details;
  76. grpc_status_code status;
  77. grpc_call_error error;
  78. grpc_slice details;
  79. int was_cancelled = 2;
  80. config.init_client(f, client_args);
  81. config.init_server(f, server_args);
  82. gpr_timespec deadline = five_seconds_from_now();
  83. c = grpc_channel_create_call(f->client, nullptr, GRPC_PROPAGATE_DEFAULTS,
  84. f->cq, grpc_slice_from_static_string("/foo"),
  85. nullptr, deadline, nullptr);
  86. GPR_ASSERT(c);
  87. grpc_metadata_array_init(&initial_metadata_recv);
  88. grpc_metadata_array_init(&trailing_metadata_recv);
  89. grpc_metadata_array_init(&request_metadata_recv);
  90. grpc_call_details_init(&call_details);
  91. memset(ops, 0, sizeof(ops));
  92. op = ops;
  93. op->op = GRPC_OP_SEND_INITIAL_METADATA;
  94. op->data.send_initial_metadata.count = 0;
  95. op->flags = GRPC_INITIAL_METADATA_WAIT_FOR_READY;
  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_SEND_INITIAL_METADATA;
  126. op->data.send_initial_metadata.count = 0;
  127. op->flags = 0;
  128. op->reserved = nullptr;
  129. op++;
  130. op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
  131. op->data.send_status_from_server.trailing_metadata_count = 0;
  132. op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED;
  133. grpc_slice status_details = grpc_slice_from_static_string("xyz");
  134. op->data.send_status_from_server.status_details = &status_details;
  135. op->flags = 0;
  136. op->reserved = nullptr;
  137. op++;
  138. op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
  139. op->data.recv_close_on_server.cancelled = &was_cancelled;
  140. op->flags = 0;
  141. op->reserved = nullptr;
  142. op++;
  143. error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(102),
  144. nullptr);
  145. GPR_ASSERT(GRPC_CALL_OK == error);
  146. CQ_EXPECT_COMPLETION(cqv, tag(102), 1);
  147. CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
  148. cq_verify(cqv);
  149. GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED);
  150. GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz"));
  151. GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo"));
  152. GPR_ASSERT(was_cancelled == 0);
  153. grpc_slice_unref(details);
  154. grpc_metadata_array_destroy(&initial_metadata_recv);
  155. grpc_metadata_array_destroy(&trailing_metadata_recv);
  156. grpc_metadata_array_destroy(&request_metadata_recv);
  157. grpc_call_details_destroy(&call_details);
  158. grpc_call_unref(c);
  159. grpc_call_unref(s);
  160. cq_verifier_destroy(cqv);
  161. }
  162. static void test_simple_delayed_request_short(grpc_end2end_test_config config) {
  163. grpc_end2end_test_fixture f;
  164. grpc_channel_args client_args;
  165. grpc_arg arg_array[1];
  166. arg_array[0].type = GRPC_ARG_INTEGER;
  167. arg_array[0].key =
  168. const_cast<char*>("grpc.testing.fixed_reconnect_backoff_ms");
  169. arg_array[0].value.integer = 1000;
  170. client_args.args = arg_array;
  171. client_args.num_args = 1;
  172. gpr_log(GPR_INFO, "Running test: %s/%s", "test_simple_delayed_request_short",
  173. config.name);
  174. f = config.create_fixture(nullptr, nullptr);
  175. simple_delayed_request_body(config, &f, &client_args, nullptr, 100000);
  176. end_test(&f);
  177. config.tear_down_data(&f);
  178. }
  179. static void test_simple_delayed_request_long(grpc_end2end_test_config config) {
  180. grpc_end2end_test_fixture f;
  181. grpc_channel_args client_args;
  182. grpc_arg arg_array[1];
  183. arg_array[0].type = GRPC_ARG_INTEGER;
  184. arg_array[0].key =
  185. const_cast<char*>("grpc.testing.fixed_reconnect_backoff_ms");
  186. arg_array[0].value.integer = 1000;
  187. client_args.args = arg_array;
  188. client_args.num_args = 1;
  189. gpr_log(GPR_INFO, "Running test: %s/%s", "test_simple_delayed_request_long",
  190. config.name);
  191. f = config.create_fixture(nullptr, nullptr);
  192. /* This timeout should be longer than a single retry */
  193. simple_delayed_request_body(config, &f, &client_args, nullptr, 1500000);
  194. end_test(&f);
  195. config.tear_down_data(&f);
  196. }
  197. void simple_delayed_request(grpc_end2end_test_config config) {
  198. GPR_ASSERT(config.feature_mask & FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION);
  199. test_simple_delayed_request_short(config);
  200. test_simple_delayed_request_long(config);
  201. }
  202. void simple_delayed_request_pre_init(void) {}