blob: c5b716157a6fc917d08c1c5837cece4740dc9e3a [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * 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
34#include "test/core/end2end/end2end_tests.h"
35
36#include <stdio.h>
37#include <string.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080038
39#include <grpc/byte_buffer.h>
40#include <grpc/support/alloc.h>
41#include <grpc/support/log.h>
42#include <grpc/support/time.h>
43#include <grpc/support/useful.h>
44#include "test/core/end2end/cq_verifier.h"
45
46enum { TIMEOUT = 200000 };
47
48static void *tag(gpr_intptr t) { return (void *)t; }
49
50static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
51 const char *test_name,
52 grpc_channel_args *client_args,
53 grpc_channel_args *server_args) {
54 grpc_end2end_test_fixture f;
55 gpr_log(GPR_INFO, "%s/%s", test_name, config.name);
56 f = config.create_fixture(client_args, server_args);
57 config.init_client(&f, client_args);
58 config.init_server(&f, server_args);
59 return f;
60}
61
62static gpr_timespec n_seconds_time(int n) {
Craig Tiller8ad8a412015-02-25 08:36:40 -080063 return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(n);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080064}
65
Craig Tiller32946d32015-01-15 11:37:30 -080066static gpr_timespec five_seconds_time(void) { return n_seconds_time(5); }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080067
68static void drain_cq(grpc_completion_queue *cq) {
69 grpc_event *ev;
70 grpc_completion_type type;
71 do {
72 ev = grpc_completion_queue_next(cq, five_seconds_time());
73 GPR_ASSERT(ev);
74 type = ev->type;
75 grpc_event_finish(ev);
76 } while (type != GRPC_QUEUE_SHUTDOWN);
77}
78
79static void shutdown_server(grpc_end2end_test_fixture *f) {
80 if (!f->server) return;
81 grpc_server_shutdown(f->server);
82 grpc_server_destroy(f->server);
83 f->server = NULL;
84}
85
86static void shutdown_client(grpc_end2end_test_fixture *f) {
87 if (!f->client) return;
88 grpc_channel_destroy(f->client);
89 f->client = NULL;
90}
91
92static void end_test(grpc_end2end_test_fixture *f) {
93 shutdown_server(f);
94 shutdown_client(f);
95
Craig Tillerbc0ec332015-05-11 12:11:32 -070096 grpc_completion_queue_shutdown(f->cq);
97 drain_cq(f->cq);
98 grpc_completion_queue_destroy(f->cq);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080099}
100
101static void simple_request_body(grpc_end2end_test_fixture f) {
102 grpc_call *c;
103 grpc_call *s;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800104 gpr_timespec deadline = five_seconds_time();
Craig Tillerbc0ec332015-05-11 12:11:32 -0700105 cq_verifier *cqv = cq_verifier_create(f.cq);
Craig Tiller8b1d9892015-04-28 14:28:19 -0700106 grpc_op ops[6];
107 grpc_op *op;
108 grpc_metadata_array initial_metadata_recv;
109 grpc_metadata_array trailing_metadata_recv;
110 grpc_metadata_array request_metadata_recv;
111 grpc_call_details call_details;
112 grpc_status_code status;
113 char *details = NULL;
114 size_t details_capacity = 0;
115 int was_cancelled = 2;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800116
Craig Tillerbc0ec332015-05-11 12:11:32 -0700117 c = grpc_channel_create_call(f.client, f.cq, "/foo",
Craig Tiller8b1d9892015-04-28 14:28:19 -0700118 "foo.test.google.fr:1234", deadline);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800119 GPR_ASSERT(c);
120
Craig Tiller8b1d9892015-04-28 14:28:19 -0700121 grpc_metadata_array_init(&initial_metadata_recv);
122 grpc_metadata_array_init(&trailing_metadata_recv);
123 grpc_metadata_array_init(&request_metadata_recv);
124 grpc_call_details_init(&call_details);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800125
Craig Tiller8b1d9892015-04-28 14:28:19 -0700126 op = ops;
127 op->op = GRPC_OP_SEND_INITIAL_METADATA;
128 op->data.send_initial_metadata.count = 0;
129 op++;
130 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
131 op++;
132 op->op = GRPC_OP_RECV_INITIAL_METADATA;
133 op->data.recv_initial_metadata = &initial_metadata_recv;
134 op++;
135 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
136 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
137 op->data.recv_status_on_client.status = &status;
138 op->data.recv_status_on_client.status_details = &details;
139 op->data.recv_status_on_client.status_details_capacity = &details_capacity;
140 op++;
141 GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(c, ops, op - ops, tag(1)));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800142
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700143 GPR_ASSERT(GRPC_CALL_OK ==
144 grpc_server_request_call(f.server, &s, &call_details,
Craig Tillerbc0ec332015-05-11 12:11:32 -0700145 &request_metadata_recv, f.cq,
146 f.cq, tag(101)));
147 cq_expect_completion(cqv, tag(101), GRPC_OP_OK);
148 cq_verify(cqv);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800149
Craig Tiller8b1d9892015-04-28 14:28:19 -0700150 op = ops;
151 op->op = GRPC_OP_SEND_INITIAL_METADATA;
152 op->data.send_initial_metadata.count = 0;
153 op++;
154 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
155 op->data.send_status_from_server.trailing_metadata_count = 0;
156 op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED;
157 op->data.send_status_from_server.status_details = "xyz";
158 op++;
159 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
160 op->data.recv_close_on_server.cancelled = &was_cancelled;
161 op++;
162 GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(s, ops, op - ops, tag(102)));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800163
Craig Tillerbc0ec332015-05-11 12:11:32 -0700164 cq_expect_completion(cqv, tag(102), GRPC_OP_OK);
165 cq_expect_completion(cqv, tag(1), GRPC_OP_OK);
166 cq_verify(cqv);
Craig Tiller8b1d9892015-04-28 14:28:19 -0700167
168 GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED);
169 GPR_ASSERT(0 == strcmp(details, "xyz"));
170 GPR_ASSERT(0 == strcmp(call_details.method, "/foo"));
171 GPR_ASSERT(0 == strcmp(call_details.host, "foo.test.google.fr:1234"));
172 GPR_ASSERT(was_cancelled == 0);
173
174 gpr_free(details);
175 grpc_metadata_array_destroy(&initial_metadata_recv);
176 grpc_metadata_array_destroy(&trailing_metadata_recv);
177 grpc_metadata_array_destroy(&request_metadata_recv);
178 grpc_call_details_destroy(&call_details);
179
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800180 grpc_call_destroy(c);
181 grpc_call_destroy(s);
182
Craig Tillerbc0ec332015-05-11 12:11:32 -0700183 cq_verifier_destroy(cqv);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800184}
185
186static void test_max_concurrent_streams(grpc_end2end_test_config config) {
187 grpc_end2end_test_fixture f;
188 grpc_arg server_arg;
189 grpc_channel_args server_args;
190 grpc_call *c1;
191 grpc_call *c2;
192 grpc_call *s1;
193 grpc_call *s2;
ctiller58393c22015-01-07 14:03:30 -0800194 int live_call;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800195 gpr_timespec deadline;
Craig Tillerbc0ec332015-05-11 12:11:32 -0700196 cq_verifier *cqv;
ctiller58393c22015-01-07 14:03:30 -0800197 grpc_event *ev;
Craig Tiller3b05a122015-04-28 15:14:40 -0700198 grpc_call_details call_details;
199 grpc_metadata_array request_metadata_recv;
200 grpc_metadata_array initial_metadata_recv1;
201 grpc_metadata_array trailing_metadata_recv1;
202 grpc_metadata_array initial_metadata_recv2;
203 grpc_metadata_array trailing_metadata_recv2;
204 grpc_status_code status1;
205 char *details1 = NULL;
206 size_t details_capacity1 = 0;
207 grpc_status_code status2;
208 char *details2 = NULL;
209 size_t details_capacity2 = 0;
Craig Tillerfe2b8352015-04-28 15:46:50 -0700210 grpc_op ops[6];
211 grpc_op *op;
212 int was_cancelled;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800213
214 server_arg.key = GRPC_ARG_MAX_CONCURRENT_STREAMS;
215 server_arg.type = GRPC_ARG_INTEGER;
216 server_arg.value.integer = 1;
217
218 server_args.num_args = 1;
219 server_args.args = &server_arg;
220
221 f = begin_test(config, __FUNCTION__, NULL, &server_args);
Craig Tillerbc0ec332015-05-11 12:11:32 -0700222 cqv = cq_verifier_create(f.cq);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800223
Craig Tiller3b05a122015-04-28 15:14:40 -0700224 grpc_metadata_array_init(&request_metadata_recv);
Craig Tillerf1071202015-04-30 13:29:48 -0700225 grpc_metadata_array_init(&initial_metadata_recv1);
226 grpc_metadata_array_init(&trailing_metadata_recv1);
227 grpc_metadata_array_init(&initial_metadata_recv2);
228 grpc_metadata_array_init(&trailing_metadata_recv2);
229 grpc_call_details_init(&call_details);
Craig Tiller3b05a122015-04-28 15:14:40 -0700230
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800231 /* perform a ping-pong to ensure that settings have had a chance to round
232 trip */
233 simple_request_body(f);
234 /* perform another one to make sure that the one stream case still works */
235 simple_request_body(f);
236
237 /* start two requests - ensuring that the second is not accepted until
238 the first completes */
Craig Tillerf1071202015-04-30 13:29:48 -0700239 deadline = n_seconds_time(10);
Craig Tillerbc0ec332015-05-11 12:11:32 -0700240 c1 = grpc_channel_create_call(f.client, f.cq, "/alpha",
Craig Tiller9f3675f2015-04-29 13:56:33 -0700241 "foo.test.google.fr:1234", deadline);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800242 GPR_ASSERT(c1);
Craig Tillerbc0ec332015-05-11 12:11:32 -0700243 c2 = grpc_channel_create_call(f.client, f.cq, "/beta",
Craig Tiller9f3675f2015-04-29 13:56:33 -0700244 "foo.test.google.fr:1234", deadline);
Craig Tiller3b05a122015-04-28 15:14:40 -0700245 GPR_ASSERT(c2);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800246
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700247 GPR_ASSERT(GRPC_CALL_OK ==
248 grpc_server_request_call(f.server, &s1, &call_details,
Craig Tillerbc0ec332015-05-11 12:11:32 -0700249 &request_metadata_recv, f.cq,
250 f.cq, tag(101)));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800251
Craig Tiller3b05a122015-04-28 15:14:40 -0700252 op = ops;
253 op->op = GRPC_OP_SEND_INITIAL_METADATA;
254 op->data.send_initial_metadata.count = 0;
255 op++;
256 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
257 op++;
Craig Tiller9f3675f2015-04-29 13:56:33 -0700258 GPR_ASSERT(GRPC_CALL_OK ==
259 grpc_call_start_batch(c1, ops, op - ops, tag(301)));
Craig Tiller3b05a122015-04-28 15:14:40 -0700260
261 op = ops;
262 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
Craig Tillerfe2b8352015-04-28 15:46:50 -0700263 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv1;
264 op->data.recv_status_on_client.status = &status1;
265 op->data.recv_status_on_client.status_details = &details1;
266 op->data.recv_status_on_client.status_details_capacity = &details_capacity1;
Craig Tiller3b05a122015-04-28 15:14:40 -0700267 op++;
Craig Tiller06eb5ae2015-04-30 13:48:58 -0700268 op->op = GRPC_OP_RECV_INITIAL_METADATA;
269 op->data.recv_initial_metadata = &initial_metadata_recv1;
270 op++;
Craig Tiller9f3675f2015-04-29 13:56:33 -0700271 GPR_ASSERT(GRPC_CALL_OK ==
272 grpc_call_start_batch(c1, ops, op - ops, tag(302)));
Craig Tiller3b05a122015-04-28 15:14:40 -0700273
274 op = ops;
275 op->op = GRPC_OP_SEND_INITIAL_METADATA;
276 op->data.send_initial_metadata.count = 0;
277 op++;
278 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
279 op++;
Craig Tiller9f3675f2015-04-29 13:56:33 -0700280 GPR_ASSERT(GRPC_CALL_OK ==
281 grpc_call_start_batch(c2, ops, op - ops, tag(401)));
Craig Tiller3b05a122015-04-28 15:14:40 -0700282
283 op = ops;
284 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
Craig Tillerfe2b8352015-04-28 15:46:50 -0700285 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv2;
286 op->data.recv_status_on_client.status = &status2;
287 op->data.recv_status_on_client.status_details = &details2;
288 op->data.recv_status_on_client.status_details_capacity = &details_capacity2;
Craig Tiller3b05a122015-04-28 15:14:40 -0700289 op++;
Craig Tiller06eb5ae2015-04-30 13:48:58 -0700290 op->op = GRPC_OP_RECV_INITIAL_METADATA;
291 op->data.recv_initial_metadata = &initial_metadata_recv1;
292 op++;
Craig Tiller9f3675f2015-04-29 13:56:33 -0700293 GPR_ASSERT(GRPC_CALL_OK ==
294 grpc_call_start_batch(c2, ops, op - ops, tag(402)));
Craig Tiller80fa15c2015-01-13 16:10:49 -0800295
Craig Tillerbc0ec332015-05-11 12:11:32 -0700296 cq_expect_completion(cqv, tag(101), GRPC_OP_OK);
297 cq_verify(cqv);
Craig Tiller06eb5ae2015-04-30 13:48:58 -0700298
Craig Tillerbc0ec332015-05-11 12:11:32 -0700299 ev = grpc_completion_queue_next(f.cq,
Craig Tillerf1071202015-04-30 13:29:48 -0700300 GRPC_TIMEOUT_SECONDS_TO_DEADLINE(3));
ctiller58393c22015-01-07 14:03:30 -0800301 GPR_ASSERT(ev);
Craig Tiller3b05a122015-04-28 15:14:40 -0700302 GPR_ASSERT(ev->type == GRPC_OP_COMPLETE);
303 GPR_ASSERT(ev->data.op_complete == GRPC_OP_OK);
Craig Tillerfe2b8352015-04-28 15:46:50 -0700304 GPR_ASSERT(ev->tag == tag(301) || ev->tag == tag(401));
ctiller58393c22015-01-07 14:03:30 -0800305 /* The /alpha or /beta calls started above could be invoked (but NOT both);
306 * check this here */
Craig Tillerfc84e7b2015-01-13 16:49:44 -0800307 /* We'll get tag 303 or 403, we want 300, 400 */
Craig Tiller9f3675f2015-04-29 13:56:33 -0700308 live_call = ((int)(gpr_intptr)ev->tag) - 1;
ctiller58393c22015-01-07 14:03:30 -0800309 grpc_event_finish(ev);
310
Craig Tiller3b05a122015-04-28 15:14:40 -0700311 op = ops;
312 op->op = GRPC_OP_SEND_INITIAL_METADATA;
313 op->data.send_initial_metadata.count = 0;
314 op++;
315 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
316 op->data.recv_close_on_server.cancelled = &was_cancelled;
317 op++;
Craig Tiller3b05a122015-04-28 15:14:40 -0700318 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
319 op->data.send_status_from_server.trailing_metadata_count = 0;
320 op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED;
321 op->data.send_status_from_server.status_details = "xyz";
322 op++;
Craig Tiller9f3675f2015-04-29 13:56:33 -0700323 GPR_ASSERT(GRPC_CALL_OK ==
Craig Tiller06eb5ae2015-04-30 13:48:58 -0700324 grpc_call_start_batch(s1, ops, op - ops, tag(102)));
Craig Tiller3b05a122015-04-28 15:14:40 -0700325
Craig Tillerbc0ec332015-05-11 12:11:32 -0700326 cq_expect_completion(cqv, tag(102), GRPC_OP_OK);
327 cq_expect_completion(cqv, tag(live_call + 2), GRPC_OP_OK);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800328 /* first request is finished, we should be able to start the second */
Craig Tillerbf444932015-01-13 17:13:08 -0800329 live_call = (live_call == 300) ? 400 : 300;
Craig Tillerbc0ec332015-05-11 12:11:32 -0700330 cq_expect_completion(cqv, tag(live_call + 1), GRPC_OP_OK);
331 cq_verify(cqv);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800332
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700333 GPR_ASSERT(GRPC_CALL_OK ==
334 grpc_server_request_call(f.server, &s2, &call_details,
Craig Tillerbc0ec332015-05-11 12:11:32 -0700335 &request_metadata_recv, f.cq,
336 f.cq, tag(201)));
337 cq_expect_completion(cqv, tag(201), GRPC_OP_OK);
338 cq_verify(cqv);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800339
Craig Tiller3b05a122015-04-28 15:14:40 -0700340 op = ops;
341 op->op = GRPC_OP_SEND_INITIAL_METADATA;
342 op->data.send_initial_metadata.count = 0;
343 op++;
344 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
345 op->data.recv_close_on_server.cancelled = &was_cancelled;
346 op++;
Craig Tillerfe2b8352015-04-28 15:46:50 -0700347 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
348 op->data.send_status_from_server.trailing_metadata_count = 0;
349 op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED;
350 op->data.send_status_from_server.status_details = "xyz";
351 op++;
Craig Tiller9f3675f2015-04-29 13:56:33 -0700352 GPR_ASSERT(GRPC_CALL_OK ==
Craig Tiller06eb5ae2015-04-30 13:48:58 -0700353 grpc_call_start_batch(s2, ops, op - ops, tag(202)));
354
Craig Tillerbc0ec332015-05-11 12:11:32 -0700355 cq_expect_completion(cqv, tag(live_call + 2), GRPC_OP_OK);
356 cq_expect_completion(cqv, tag(202), GRPC_OP_OK);
357 cq_verify(cqv);
Craig Tillerfe2b8352015-04-28 15:46:50 -0700358
Craig Tillerbc0ec332015-05-11 12:11:32 -0700359 cq_verifier_destroy(cqv);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800360
361 grpc_call_destroy(c1);
362 grpc_call_destroy(s1);
363 grpc_call_destroy(c2);
364 grpc_call_destroy(s2);
365
Craig Tiller5fe7e5d2015-05-01 10:52:35 -0700366 gpr_free(details1);
367 gpr_free(details2);
368 grpc_metadata_array_destroy(&initial_metadata_recv1);
369 grpc_metadata_array_destroy(&trailing_metadata_recv1);
370 grpc_metadata_array_destroy(&initial_metadata_recv2);
371 grpc_metadata_array_destroy(&trailing_metadata_recv2);
372 grpc_metadata_array_destroy(&request_metadata_recv);
373 grpc_call_details_destroy(&call_details);
374
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800375 end_test(&f);
376 config.tear_down_data(&f);
377}
378
379void grpc_end2end_tests(grpc_end2end_test_config config) {
380 test_max_concurrent_streams(config);
Craig Tiller190d3602015-02-18 09:23:38 -0800381}