simple_request.cc 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 <grpc/byte_buffer.h>
  22. #include <grpc/grpc.h>
  23. #include <grpc/support/alloc.h>
  24. #include <grpc/support/log.h>
  25. #include <grpc/support/time.h>
  26. #include "src/core/lib/debug/stats.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 check_peer(char* peer_name) {
  78. // If the peer name is a uds path, then check if it is filled
  79. if (strncmp(peer_name, "unix:/", strlen("unix:/")) == 0) {
  80. GPR_ASSERT(strncmp(peer_name, "unix:/tmp/grpc_fullstack_test.",
  81. strlen("unix:/tmp/grpc_fullstack_test.")) == 0);
  82. }
  83. }
  84. static void simple_request_body(grpc_end2end_test_config config,
  85. grpc_end2end_test_fixture f) {
  86. grpc_call* c;
  87. grpc_call* s;
  88. cq_verifier* cqv = cq_verifier_create(f.cq);
  89. grpc_op ops[6];
  90. grpc_op* op;
  91. grpc_metadata_array initial_metadata_recv;
  92. grpc_metadata_array trailing_metadata_recv;
  93. grpc_metadata_array request_metadata_recv;
  94. grpc_call_details call_details;
  95. grpc_status_code status;
  96. const char* error_string;
  97. grpc_call_error error;
  98. grpc_slice details;
  99. int was_cancelled = 2;
  100. char* peer;
  101. grpc_stats_data* before =
  102. static_cast<grpc_stats_data*>(gpr_malloc(sizeof(grpc_stats_data)));
  103. grpc_stats_data* after =
  104. static_cast<grpc_stats_data*>(gpr_malloc(sizeof(grpc_stats_data)));
  105. #if defined(GRPC_COLLECT_STATS) || !defined(NDEBUG)
  106. grpc_stats_collect(before);
  107. #endif /* defined(GRPC_COLLECT_STATS) || !defined(NDEBUG) */
  108. gpr_timespec deadline = five_seconds_from_now();
  109. c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
  110. grpc_slice_from_static_string("/foo"), nullptr,
  111. deadline, nullptr);
  112. GPR_ASSERT(c);
  113. peer = grpc_call_get_peer(c);
  114. GPR_ASSERT(peer != nullptr);
  115. gpr_log(GPR_DEBUG, "client_peer_before_call=%s", peer);
  116. gpr_free(peer);
  117. grpc_metadata_array_init(&initial_metadata_recv);
  118. grpc_metadata_array_init(&trailing_metadata_recv);
  119. grpc_metadata_array_init(&request_metadata_recv);
  120. grpc_call_details_init(&call_details);
  121. memset(ops, 0, sizeof(ops));
  122. op = ops;
  123. op->op = GRPC_OP_SEND_INITIAL_METADATA;
  124. op->data.send_initial_metadata.count = 0;
  125. op->flags = 0;
  126. op->reserved = nullptr;
  127. op++;
  128. op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
  129. op->flags = 0;
  130. op->reserved = nullptr;
  131. op++;
  132. op->op = GRPC_OP_RECV_INITIAL_METADATA;
  133. op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
  134. op->flags = 0;
  135. op->reserved = nullptr;
  136. op++;
  137. op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
  138. op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
  139. op->data.recv_status_on_client.status = &status;
  140. op->data.recv_status_on_client.status_details = &details;
  141. op->data.recv_status_on_client.error_string = &error_string;
  142. op->flags = 0;
  143. op->reserved = nullptr;
  144. op++;
  145. error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
  146. nullptr);
  147. GPR_ASSERT(GRPC_CALL_OK == error);
  148. error =
  149. grpc_server_request_call(f.server, &s, &call_details,
  150. &request_metadata_recv, f.cq, f.cq, tag(101));
  151. GPR_ASSERT(GRPC_CALL_OK == error);
  152. CQ_EXPECT_COMPLETION(cqv, tag(101), 1);
  153. cq_verify(cqv);
  154. peer = grpc_call_get_peer(s);
  155. GPR_ASSERT(peer != nullptr);
  156. gpr_log(GPR_DEBUG, "server_peer=%s", peer);
  157. check_peer(peer);
  158. gpr_free(peer);
  159. peer = grpc_call_get_peer(c);
  160. GPR_ASSERT(peer != nullptr);
  161. gpr_log(GPR_DEBUG, "client_peer=%s", peer);
  162. check_peer(peer);
  163. gpr_free(peer);
  164. memset(ops, 0, sizeof(ops));
  165. op = ops;
  166. op->op = GRPC_OP_SEND_INITIAL_METADATA;
  167. op->data.send_initial_metadata.count = 0;
  168. op->flags = 0;
  169. op->reserved = nullptr;
  170. op++;
  171. op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
  172. op->data.send_status_from_server.trailing_metadata_count = 0;
  173. op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED;
  174. grpc_slice status_details = grpc_slice_from_static_string("xyz");
  175. op->data.send_status_from_server.status_details = &status_details;
  176. op->flags = 0;
  177. op->reserved = nullptr;
  178. op++;
  179. op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
  180. op->data.recv_close_on_server.cancelled = &was_cancelled;
  181. op->flags = 0;
  182. op->reserved = nullptr;
  183. op++;
  184. error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(102),
  185. nullptr);
  186. GPR_ASSERT(GRPC_CALL_OK == error);
  187. CQ_EXPECT_COMPLETION(cqv, tag(102), 1);
  188. CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
  189. cq_verify(cqv);
  190. GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED);
  191. GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz"));
  192. // the following sanity check makes sure that the requested error string is
  193. // correctly populated by the core. It looks for certain substrings that are
  194. // not likely to change much. Some parts of the error, like time created,
  195. // obviously are not checked.
  196. GPR_ASSERT(nullptr != strstr(error_string, "xyz"));
  197. GPR_ASSERT(nullptr != strstr(error_string, "Error received from peer"));
  198. GPR_ASSERT(nullptr != strstr(error_string, "grpc_message"));
  199. GPR_ASSERT(nullptr != strstr(error_string, "grpc_status"));
  200. GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo"));
  201. GPR_ASSERT(0 == call_details.flags);
  202. GPR_ASSERT(was_cancelled == 0);
  203. grpc_slice_unref(details);
  204. gpr_free(const_cast<char*>(error_string));
  205. grpc_metadata_array_destroy(&initial_metadata_recv);
  206. grpc_metadata_array_destroy(&trailing_metadata_recv);
  207. grpc_metadata_array_destroy(&request_metadata_recv);
  208. grpc_call_details_destroy(&call_details);
  209. grpc_call_unref(c);
  210. grpc_call_unref(s);
  211. cq_verifier_destroy(cqv);
  212. int expected_calls = 1;
  213. if (config.feature_mask & FEATURE_MASK_SUPPORTS_REQUEST_PROXYING) {
  214. expected_calls *= 2;
  215. }
  216. #if defined(GRPC_COLLECT_STATS) || !defined(NDEBUG)
  217. grpc_stats_collect(after);
  218. gpr_log(GPR_DEBUG, "%s", grpc_stats_data_as_json(after).c_str());
  219. GPR_ASSERT(after->counters[GRPC_STATS_COUNTER_CLIENT_CALLS_CREATED] -
  220. before->counters[GRPC_STATS_COUNTER_CLIENT_CALLS_CREATED] ==
  221. expected_calls);
  222. GPR_ASSERT(after->counters[GRPC_STATS_COUNTER_SERVER_CALLS_CREATED] -
  223. before->counters[GRPC_STATS_COUNTER_SERVER_CALLS_CREATED] ==
  224. expected_calls);
  225. #endif /* defined(GRPC_COLLECT_STATS) || !defined(NDEBUG) */
  226. gpr_free(before);
  227. gpr_free(after);
  228. }
  229. static void test_invoke_simple_request(grpc_end2end_test_config config) {
  230. grpc_end2end_test_fixture f;
  231. f = begin_test(config, "test_invoke_simple_request", nullptr, nullptr);
  232. simple_request_body(config, f);
  233. end_test(&f);
  234. config.tear_down_data(&f);
  235. }
  236. static void test_invoke_10_simple_requests(grpc_end2end_test_config config) {
  237. int i;
  238. grpc_end2end_test_fixture f =
  239. begin_test(config, "test_invoke_10_simple_requests", nullptr, nullptr);
  240. for (i = 0; i < 10; i++) {
  241. simple_request_body(config, f);
  242. gpr_log(GPR_INFO, "Running test: Passed simple request %d", i);
  243. }
  244. end_test(&f);
  245. config.tear_down_data(&f);
  246. }
  247. void simple_request(grpc_end2end_test_config config) {
  248. int i;
  249. for (i = 0; i < 10; i++) {
  250. test_invoke_simple_request(config);
  251. }
  252. test_invoke_10_simple_requests(config);
  253. }
  254. void simple_request_pre_init(void) {}