max_concurrent_streams.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*
  2. *
  3. * Copyright 2015, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. #include "test/core/end2end/end2end_tests.h"
  34. #include <stdio.h>
  35. #include <string.h>
  36. #include <grpc/byte_buffer.h>
  37. #include <grpc/support/alloc.h>
  38. #include <grpc/support/log.h>
  39. #include <grpc/support/time.h>
  40. #include <grpc/support/useful.h>
  41. #include "test/core/end2end/cq_verifier.h"
  42. enum { TIMEOUT = 200000 };
  43. static void *tag(gpr_intptr t) { return (void *)t; }
  44. static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
  45. const char *test_name,
  46. grpc_channel_args *client_args,
  47. grpc_channel_args *server_args) {
  48. grpc_end2end_test_fixture f;
  49. gpr_log(GPR_INFO, "%s/%s", test_name, config.name);
  50. f = config.create_fixture(client_args, server_args);
  51. config.init_client(&f, client_args);
  52. config.init_server(&f, server_args);
  53. return f;
  54. }
  55. static gpr_timespec n_seconds_time(int n) {
  56. return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(n);
  57. }
  58. static gpr_timespec five_seconds_time(void) { return n_seconds_time(5); }
  59. static void drain_cq(grpc_completion_queue *cq) {
  60. grpc_event *ev;
  61. grpc_completion_type type;
  62. do {
  63. ev = grpc_completion_queue_next(cq, five_seconds_time());
  64. GPR_ASSERT(ev);
  65. type = ev->type;
  66. grpc_event_finish(ev);
  67. } while (type != GRPC_QUEUE_SHUTDOWN);
  68. }
  69. static void shutdown_server(grpc_end2end_test_fixture *f) {
  70. if (!f->server) return;
  71. grpc_server_shutdown(f->server);
  72. grpc_server_destroy(f->server);
  73. f->server = NULL;
  74. }
  75. static void shutdown_client(grpc_end2end_test_fixture *f) {
  76. if (!f->client) return;
  77. grpc_channel_destroy(f->client);
  78. f->client = NULL;
  79. }
  80. static void end_test(grpc_end2end_test_fixture *f) {
  81. shutdown_server(f);
  82. shutdown_client(f);
  83. grpc_completion_queue_shutdown(f->cq);
  84. drain_cq(f->cq);
  85. grpc_completion_queue_destroy(f->cq);
  86. }
  87. static void simple_request_body(grpc_end2end_test_fixture f) {
  88. grpc_call *c;
  89. grpc_call *s;
  90. gpr_timespec deadline = five_seconds_time();
  91. cq_verifier *cqv = cq_verifier_create(f.cq);
  92. grpc_op ops[6];
  93. grpc_op *op;
  94. grpc_metadata_array initial_metadata_recv;
  95. grpc_metadata_array trailing_metadata_recv;
  96. grpc_metadata_array request_metadata_recv;
  97. grpc_call_details call_details;
  98. grpc_status_code status;
  99. char *details = NULL;
  100. size_t details_capacity = 0;
  101. int was_cancelled = 2;
  102. c = grpc_channel_create_call(f.client, f.cq, "/foo",
  103. "foo.test.google.fr:1234", deadline);
  104. GPR_ASSERT(c);
  105. grpc_metadata_array_init(&initial_metadata_recv);
  106. grpc_metadata_array_init(&trailing_metadata_recv);
  107. grpc_metadata_array_init(&request_metadata_recv);
  108. grpc_call_details_init(&call_details);
  109. op = ops;
  110. op->op = GRPC_OP_SEND_INITIAL_METADATA;
  111. op->data.send_initial_metadata.count = 0;
  112. op++;
  113. op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
  114. op++;
  115. op->op = GRPC_OP_RECV_INITIAL_METADATA;
  116. op->data.recv_initial_metadata = &initial_metadata_recv;
  117. op++;
  118. op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
  119. op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
  120. op->data.recv_status_on_client.status = &status;
  121. op->data.recv_status_on_client.status_details = &details;
  122. op->data.recv_status_on_client.status_details_capacity = &details_capacity;
  123. op++;
  124. GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(c, ops, op - ops, tag(1)));
  125. GPR_ASSERT(GRPC_CALL_OK ==
  126. grpc_server_request_call(f.server, &s, &call_details,
  127. &request_metadata_recv, f.cq,
  128. f.cq, tag(101)));
  129. cq_expect_completion(cqv, tag(101), GRPC_OP_OK);
  130. cq_verify(cqv);
  131. op = ops;
  132. op->op = GRPC_OP_SEND_INITIAL_METADATA;
  133. op->data.send_initial_metadata.count = 0;
  134. op++;
  135. op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
  136. op->data.send_status_from_server.trailing_metadata_count = 0;
  137. op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED;
  138. op->data.send_status_from_server.status_details = "xyz";
  139. op++;
  140. op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
  141. op->data.recv_close_on_server.cancelled = &was_cancelled;
  142. op++;
  143. GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(s, ops, op - ops, tag(102)));
  144. cq_expect_completion(cqv, tag(102), GRPC_OP_OK);
  145. cq_expect_completion(cqv, tag(1), GRPC_OP_OK);
  146. cq_verify(cqv);
  147. GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED);
  148. GPR_ASSERT(0 == strcmp(details, "xyz"));
  149. GPR_ASSERT(0 == strcmp(call_details.method, "/foo"));
  150. GPR_ASSERT(0 == strcmp(call_details.host, "foo.test.google.fr:1234"));
  151. GPR_ASSERT(was_cancelled == 0);
  152. gpr_free(details);
  153. grpc_metadata_array_destroy(&initial_metadata_recv);
  154. grpc_metadata_array_destroy(&trailing_metadata_recv);
  155. grpc_metadata_array_destroy(&request_metadata_recv);
  156. grpc_call_details_destroy(&call_details);
  157. grpc_call_destroy(c);
  158. grpc_call_destroy(s);
  159. cq_verifier_destroy(cqv);
  160. }
  161. static void test_max_concurrent_streams(grpc_end2end_test_config config) {
  162. grpc_end2end_test_fixture f;
  163. grpc_arg server_arg;
  164. grpc_channel_args server_args;
  165. grpc_call *c1;
  166. grpc_call *c2;
  167. grpc_call *s1;
  168. grpc_call *s2;
  169. int live_call;
  170. gpr_timespec deadline;
  171. cq_verifier *cqv;
  172. grpc_event *ev;
  173. grpc_call_details call_details;
  174. grpc_metadata_array request_metadata_recv;
  175. grpc_metadata_array initial_metadata_recv1;
  176. grpc_metadata_array trailing_metadata_recv1;
  177. grpc_metadata_array initial_metadata_recv2;
  178. grpc_metadata_array trailing_metadata_recv2;
  179. grpc_status_code status1;
  180. char *details1 = NULL;
  181. size_t details_capacity1 = 0;
  182. grpc_status_code status2;
  183. char *details2 = NULL;
  184. size_t details_capacity2 = 0;
  185. grpc_op ops[6];
  186. grpc_op *op;
  187. int was_cancelled;
  188. server_arg.key = GRPC_ARG_MAX_CONCURRENT_STREAMS;
  189. server_arg.type = GRPC_ARG_INTEGER;
  190. server_arg.value.integer = 1;
  191. server_args.num_args = 1;
  192. server_args.args = &server_arg;
  193. f = begin_test(config, __FUNCTION__, NULL, &server_args);
  194. cqv = cq_verifier_create(f.cq);
  195. grpc_metadata_array_init(&request_metadata_recv);
  196. grpc_metadata_array_init(&initial_metadata_recv1);
  197. grpc_metadata_array_init(&trailing_metadata_recv1);
  198. grpc_metadata_array_init(&initial_metadata_recv2);
  199. grpc_metadata_array_init(&trailing_metadata_recv2);
  200. grpc_call_details_init(&call_details);
  201. /* perform a ping-pong to ensure that settings have had a chance to round
  202. trip */
  203. simple_request_body(f);
  204. /* perform another one to make sure that the one stream case still works */
  205. simple_request_body(f);
  206. /* start two requests - ensuring that the second is not accepted until
  207. the first completes */
  208. deadline = n_seconds_time(10);
  209. c1 = grpc_channel_create_call(f.client, f.cq, "/alpha",
  210. "foo.test.google.fr:1234", deadline);
  211. GPR_ASSERT(c1);
  212. c2 = grpc_channel_create_call(f.client, f.cq, "/beta",
  213. "foo.test.google.fr:1234", deadline);
  214. GPR_ASSERT(c2);
  215. GPR_ASSERT(GRPC_CALL_OK ==
  216. grpc_server_request_call(f.server, &s1, &call_details,
  217. &request_metadata_recv, f.cq,
  218. f.cq, tag(101)));
  219. op = ops;
  220. op->op = GRPC_OP_SEND_INITIAL_METADATA;
  221. op->data.send_initial_metadata.count = 0;
  222. op++;
  223. op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
  224. op++;
  225. GPR_ASSERT(GRPC_CALL_OK ==
  226. grpc_call_start_batch(c1, ops, op - ops, tag(301)));
  227. op = ops;
  228. op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
  229. op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv1;
  230. op->data.recv_status_on_client.status = &status1;
  231. op->data.recv_status_on_client.status_details = &details1;
  232. op->data.recv_status_on_client.status_details_capacity = &details_capacity1;
  233. op++;
  234. op->op = GRPC_OP_RECV_INITIAL_METADATA;
  235. op->data.recv_initial_metadata = &initial_metadata_recv1;
  236. op++;
  237. GPR_ASSERT(GRPC_CALL_OK ==
  238. grpc_call_start_batch(c1, ops, op - ops, tag(302)));
  239. op = ops;
  240. op->op = GRPC_OP_SEND_INITIAL_METADATA;
  241. op->data.send_initial_metadata.count = 0;
  242. op++;
  243. op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
  244. op++;
  245. GPR_ASSERT(GRPC_CALL_OK ==
  246. grpc_call_start_batch(c2, ops, op - ops, tag(401)));
  247. op = ops;
  248. op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
  249. op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv2;
  250. op->data.recv_status_on_client.status = &status2;
  251. op->data.recv_status_on_client.status_details = &details2;
  252. op->data.recv_status_on_client.status_details_capacity = &details_capacity2;
  253. op++;
  254. op->op = GRPC_OP_RECV_INITIAL_METADATA;
  255. op->data.recv_initial_metadata = &initial_metadata_recv1;
  256. op++;
  257. GPR_ASSERT(GRPC_CALL_OK ==
  258. grpc_call_start_batch(c2, ops, op - ops, tag(402)));
  259. cq_expect_completion(cqv, tag(101), GRPC_OP_OK);
  260. cq_verify(cqv);
  261. ev = grpc_completion_queue_next(f.cq,
  262. GRPC_TIMEOUT_SECONDS_TO_DEADLINE(3));
  263. GPR_ASSERT(ev);
  264. GPR_ASSERT(ev->type == GRPC_OP_COMPLETE);
  265. GPR_ASSERT(ev->data.op_complete == GRPC_OP_OK);
  266. GPR_ASSERT(ev->tag == tag(301) || ev->tag == tag(401));
  267. /* The /alpha or /beta calls started above could be invoked (but NOT both);
  268. * check this here */
  269. /* We'll get tag 303 or 403, we want 300, 400 */
  270. live_call = ((int)(gpr_intptr)ev->tag) - 1;
  271. grpc_event_finish(ev);
  272. op = ops;
  273. op->op = GRPC_OP_SEND_INITIAL_METADATA;
  274. op->data.send_initial_metadata.count = 0;
  275. op++;
  276. op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
  277. op->data.recv_close_on_server.cancelled = &was_cancelled;
  278. op++;
  279. op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
  280. op->data.send_status_from_server.trailing_metadata_count = 0;
  281. op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED;
  282. op->data.send_status_from_server.status_details = "xyz";
  283. op++;
  284. GPR_ASSERT(GRPC_CALL_OK ==
  285. grpc_call_start_batch(s1, ops, op - ops, tag(102)));
  286. cq_expect_completion(cqv, tag(102), GRPC_OP_OK);
  287. cq_expect_completion(cqv, tag(live_call + 2), GRPC_OP_OK);
  288. /* first request is finished, we should be able to start the second */
  289. live_call = (live_call == 300) ? 400 : 300;
  290. cq_expect_completion(cqv, tag(live_call + 1), GRPC_OP_OK);
  291. cq_verify(cqv);
  292. GPR_ASSERT(GRPC_CALL_OK ==
  293. grpc_server_request_call(f.server, &s2, &call_details,
  294. &request_metadata_recv, f.cq,
  295. f.cq, tag(201)));
  296. cq_expect_completion(cqv, tag(201), GRPC_OP_OK);
  297. cq_verify(cqv);
  298. op = ops;
  299. op->op = GRPC_OP_SEND_INITIAL_METADATA;
  300. op->data.send_initial_metadata.count = 0;
  301. op++;
  302. op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
  303. op->data.recv_close_on_server.cancelled = &was_cancelled;
  304. op++;
  305. op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
  306. op->data.send_status_from_server.trailing_metadata_count = 0;
  307. op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED;
  308. op->data.send_status_from_server.status_details = "xyz";
  309. op++;
  310. GPR_ASSERT(GRPC_CALL_OK ==
  311. grpc_call_start_batch(s2, ops, op - ops, tag(202)));
  312. cq_expect_completion(cqv, tag(live_call + 2), GRPC_OP_OK);
  313. cq_expect_completion(cqv, tag(202), GRPC_OP_OK);
  314. cq_verify(cqv);
  315. cq_verifier_destroy(cqv);
  316. grpc_call_destroy(c1);
  317. grpc_call_destroy(s1);
  318. grpc_call_destroy(c2);
  319. grpc_call_destroy(s2);
  320. gpr_free(details1);
  321. gpr_free(details2);
  322. grpc_metadata_array_destroy(&initial_metadata_recv1);
  323. grpc_metadata_array_destroy(&trailing_metadata_recv1);
  324. grpc_metadata_array_destroy(&initial_metadata_recv2);
  325. grpc_metadata_array_destroy(&trailing_metadata_recv2);
  326. grpc_metadata_array_destroy(&request_metadata_recv);
  327. grpc_call_details_destroy(&call_details);
  328. end_test(&f);
  329. config.tear_down_data(&f);
  330. }
  331. void grpc_end2end_tests(grpc_end2end_test_config config) {
  332. test_max_concurrent_streams(config);
  333. }