blob: 56d81a1130a2e233faa198005f4dda25dec031e4 [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>
38#include <unistd.h>
39
40#include <grpc/byte_buffer.h>
41#include <grpc/support/alloc.h>
42#include <grpc/support/log.h>
43#include <grpc/support/time.h>
44#include <grpc/support/useful.h>
45#include "test/core/end2end/cq_verifier.h"
46
47enum { TIMEOUT = 200000 };
48
49static void *tag(gpr_intptr t) { return (void *)t; }
50
51static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
52 const char *test_name,
53 grpc_channel_args *client_args,
54 grpc_channel_args *server_args) {
55 grpc_end2end_test_fixture f;
56 gpr_log(GPR_INFO, "%s/%s", test_name, config.name);
57 f = config.create_fixture(client_args, server_args);
58 config.init_client(&f, client_args);
59 config.init_server(&f, server_args);
60 return f;
61}
62
63static gpr_timespec n_seconds_time(int n) {
Craig Tiller8ad8a412015-02-25 08:36:40 -080064 return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(n);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080065}
66
Craig Tiller32946d32015-01-15 11:37:30 -080067static gpr_timespec five_seconds_time(void) { return n_seconds_time(5); }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080068
69static void drain_cq(grpc_completion_queue *cq) {
Craig Tiller64be9f72015-05-04 14:53:51 -070070 grpc_event ev;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080071 do {
72 ev = grpc_completion_queue_next(cq, five_seconds_time());
Craig Tiller64be9f72015-05-04 14:53:51 -070073 } while (ev.type != GRPC_QUEUE_SHUTDOWN);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080074}
75
76static void shutdown_server(grpc_end2end_test_fixture *f) {
77 if (!f->server) return;
78 grpc_server_shutdown(f->server);
79 grpc_server_destroy(f->server);
80 f->server = NULL;
81}
82
83static void shutdown_client(grpc_end2end_test_fixture *f) {
84 if (!f->client) return;
85 grpc_channel_destroy(f->client);
86 f->client = NULL;
87}
88
89static void end_test(grpc_end2end_test_fixture *f) {
90 shutdown_server(f);
91 shutdown_client(f);
92
93 grpc_completion_queue_shutdown(f->server_cq);
94 drain_cq(f->server_cq);
95 grpc_completion_queue_destroy(f->server_cq);
96 grpc_completion_queue_shutdown(f->client_cq);
97 drain_cq(f->client_cq);
98 grpc_completion_queue_destroy(f->client_cq);
99}
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();
105 cq_verifier *v_client = cq_verifier_create(f.client_cq);
106 cq_verifier *v_server = cq_verifier_create(f.server_cq);
Craig Tiller8b1d9892015-04-28 14:28:19 -0700107 grpc_op ops[6];
108 grpc_op *op;
109 grpc_metadata_array initial_metadata_recv;
110 grpc_metadata_array trailing_metadata_recv;
111 grpc_metadata_array request_metadata_recv;
112 grpc_call_details call_details;
113 grpc_status_code status;
114 char *details = NULL;
115 size_t details_capacity = 0;
116 int was_cancelled = 2;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800117
Craig Tiller8b1d9892015-04-28 14:28:19 -0700118 c = grpc_channel_create_call(f.client, f.client_cq, "/foo",
119 "foo.test.google.fr:1234", deadline);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800120 GPR_ASSERT(c);
121
Craig Tiller8b1d9892015-04-28 14:28:19 -0700122 grpc_metadata_array_init(&initial_metadata_recv);
123 grpc_metadata_array_init(&trailing_metadata_recv);
124 grpc_metadata_array_init(&request_metadata_recv);
125 grpc_call_details_init(&call_details);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800126
Craig Tiller8b1d9892015-04-28 14:28:19 -0700127 op = ops;
128 op->op = GRPC_OP_SEND_INITIAL_METADATA;
129 op->data.send_initial_metadata.count = 0;
130 op++;
131 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
132 op++;
133 op->op = GRPC_OP_RECV_INITIAL_METADATA;
134 op->data.recv_initial_metadata = &initial_metadata_recv;
135 op++;
136 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
137 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
138 op->data.recv_status_on_client.status = &status;
139 op->data.recv_status_on_client.status_details = &details;
140 op->data.recv_status_on_client.status_details_capacity = &details_capacity;
141 op++;
142 GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(c, ops, op - ops, tag(1)));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800143
Craig Tiller8b1d9892015-04-28 14:28:19 -0700144 GPR_ASSERT(GRPC_CALL_OK == grpc_server_request_call(f.server, &s,
145 &call_details,
146 &request_metadata_recv,
147 f.server_cq, tag(101)));
Craig Tiller64be9f72015-05-04 14:53:51 -0700148 cq_expect_completion(v_server, tag(101), 1);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800149 cq_verify(v_server);
150
Craig Tiller8b1d9892015-04-28 14:28:19 -0700151 op = ops;
152 op->op = GRPC_OP_SEND_INITIAL_METADATA;
153 op->data.send_initial_metadata.count = 0;
154 op++;
155 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
156 op->data.send_status_from_server.trailing_metadata_count = 0;
157 op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED;
158 op->data.send_status_from_server.status_details = "xyz";
159 op++;
160 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
161 op->data.recv_close_on_server.cancelled = &was_cancelled;
162 op++;
163 GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(s, ops, op - ops, tag(102)));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800164
Craig Tiller64be9f72015-05-04 14:53:51 -0700165 cq_expect_completion(v_server, tag(102), 1);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800166 cq_verify(v_server);
167
Craig Tiller64be9f72015-05-04 14:53:51 -0700168 cq_expect_completion(v_client, tag(1), 1);
Craig Tiller8b1d9892015-04-28 14:28:19 -0700169 cq_verify(v_client);
170
171 GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED);
172 GPR_ASSERT(0 == strcmp(details, "xyz"));
173 GPR_ASSERT(0 == strcmp(call_details.method, "/foo"));
174 GPR_ASSERT(0 == strcmp(call_details.host, "foo.test.google.fr:1234"));
175 GPR_ASSERT(was_cancelled == 0);
176
177 gpr_free(details);
178 grpc_metadata_array_destroy(&initial_metadata_recv);
179 grpc_metadata_array_destroy(&trailing_metadata_recv);
180 grpc_metadata_array_destroy(&request_metadata_recv);
181 grpc_call_details_destroy(&call_details);
182
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800183 grpc_call_destroy(c);
184 grpc_call_destroy(s);
185
186 cq_verifier_destroy(v_client);
187 cq_verifier_destroy(v_server);
188}
189
190static void test_max_concurrent_streams(grpc_end2end_test_config config) {
191 grpc_end2end_test_fixture f;
192 grpc_arg server_arg;
193 grpc_channel_args server_args;
194 grpc_call *c1;
195 grpc_call *c2;
196 grpc_call *s1;
197 grpc_call *s2;
ctiller58393c22015-01-07 14:03:30 -0800198 int live_call;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800199 gpr_timespec deadline;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800200 cq_verifier *v_client;
201 cq_verifier *v_server;
Craig Tiller64be9f72015-05-04 14:53:51 -0700202 grpc_event ev;
Craig Tiller3b05a122015-04-28 15:14:40 -0700203 grpc_call_details call_details;
204 grpc_metadata_array request_metadata_recv;
205 grpc_metadata_array initial_metadata_recv1;
206 grpc_metadata_array trailing_metadata_recv1;
207 grpc_metadata_array initial_metadata_recv2;
208 grpc_metadata_array trailing_metadata_recv2;
209 grpc_status_code status1;
210 char *details1 = NULL;
211 size_t details_capacity1 = 0;
212 grpc_status_code status2;
213 char *details2 = NULL;
214 size_t details_capacity2 = 0;
Craig Tillerfe2b8352015-04-28 15:46:50 -0700215 grpc_op ops[6];
216 grpc_op *op;
217 int was_cancelled;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800218
219 server_arg.key = GRPC_ARG_MAX_CONCURRENT_STREAMS;
220 server_arg.type = GRPC_ARG_INTEGER;
221 server_arg.value.integer = 1;
222
223 server_args.num_args = 1;
224 server_args.args = &server_arg;
225
226 f = begin_test(config, __FUNCTION__, NULL, &server_args);
227 v_client = cq_verifier_create(f.client_cq);
228 v_server = cq_verifier_create(f.server_cq);
229
Craig Tiller3b05a122015-04-28 15:14:40 -0700230 grpc_metadata_array_init(&request_metadata_recv);
Craig Tillerf1071202015-04-30 13:29:48 -0700231 grpc_metadata_array_init(&initial_metadata_recv1);
232 grpc_metadata_array_init(&trailing_metadata_recv1);
233 grpc_metadata_array_init(&initial_metadata_recv2);
234 grpc_metadata_array_init(&trailing_metadata_recv2);
235 grpc_call_details_init(&call_details);
Craig Tiller3b05a122015-04-28 15:14:40 -0700236
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800237 /* perform a ping-pong to ensure that settings have had a chance to round
238 trip */
239 simple_request_body(f);
240 /* perform another one to make sure that the one stream case still works */
241 simple_request_body(f);
242
243 /* start two requests - ensuring that the second is not accepted until
244 the first completes */
Craig Tillerf1071202015-04-30 13:29:48 -0700245 deadline = n_seconds_time(10);
Craig Tiller3b05a122015-04-28 15:14:40 -0700246 c1 = grpc_channel_create_call(f.client, f.client_cq, "/alpha",
Craig Tiller9f3675f2015-04-29 13:56:33 -0700247 "foo.test.google.fr:1234", deadline);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800248 GPR_ASSERT(c1);
Craig Tiller3b05a122015-04-28 15:14:40 -0700249 c2 = grpc_channel_create_call(f.client, f.client_cq, "/beta",
Craig Tiller9f3675f2015-04-29 13:56:33 -0700250 "foo.test.google.fr:1234", deadline);
Craig Tiller3b05a122015-04-28 15:14:40 -0700251 GPR_ASSERT(c2);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800252
Craig Tiller3b05a122015-04-28 15:14:40 -0700253 GPR_ASSERT(GRPC_CALL_OK == grpc_server_request_call(f.server, &s1,
254 &call_details,
255 &request_metadata_recv,
256 f.server_cq, tag(101)));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800257
Craig Tiller3b05a122015-04-28 15:14:40 -0700258 op = ops;
259 op->op = GRPC_OP_SEND_INITIAL_METADATA;
260 op->data.send_initial_metadata.count = 0;
261 op++;
262 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
263 op++;
Craig Tiller9f3675f2015-04-29 13:56:33 -0700264 GPR_ASSERT(GRPC_CALL_OK ==
265 grpc_call_start_batch(c1, ops, op - ops, tag(301)));
Craig Tiller3b05a122015-04-28 15:14:40 -0700266
267 op = ops;
268 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
Craig Tillerfe2b8352015-04-28 15:46:50 -0700269 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv1;
270 op->data.recv_status_on_client.status = &status1;
271 op->data.recv_status_on_client.status_details = &details1;
272 op->data.recv_status_on_client.status_details_capacity = &details_capacity1;
Craig Tiller3b05a122015-04-28 15:14:40 -0700273 op++;
Craig Tiller06eb5ae2015-04-30 13:48:58 -0700274 op->op = GRPC_OP_RECV_INITIAL_METADATA;
275 op->data.recv_initial_metadata = &initial_metadata_recv1;
276 op++;
Craig Tiller9f3675f2015-04-29 13:56:33 -0700277 GPR_ASSERT(GRPC_CALL_OK ==
278 grpc_call_start_batch(c1, ops, op - ops, tag(302)));
Craig Tiller3b05a122015-04-28 15:14:40 -0700279
280 op = ops;
281 op->op = GRPC_OP_SEND_INITIAL_METADATA;
282 op->data.send_initial_metadata.count = 0;
283 op++;
284 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
285 op++;
Craig Tiller9f3675f2015-04-29 13:56:33 -0700286 GPR_ASSERT(GRPC_CALL_OK ==
287 grpc_call_start_batch(c2, ops, op - ops, tag(401)));
Craig Tiller3b05a122015-04-28 15:14:40 -0700288
289 op = ops;
290 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
Craig Tillerfe2b8352015-04-28 15:46:50 -0700291 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv2;
292 op->data.recv_status_on_client.status = &status2;
293 op->data.recv_status_on_client.status_details = &details2;
294 op->data.recv_status_on_client.status_details_capacity = &details_capacity2;
Craig Tiller3b05a122015-04-28 15:14:40 -0700295 op++;
Craig Tiller06eb5ae2015-04-30 13:48:58 -0700296 op->op = GRPC_OP_RECV_INITIAL_METADATA;
297 op->data.recv_initial_metadata = &initial_metadata_recv1;
298 op++;
Craig Tiller9f3675f2015-04-29 13:56:33 -0700299 GPR_ASSERT(GRPC_CALL_OK ==
300 grpc_call_start_batch(c2, ops, op - ops, tag(402)));
Craig Tiller80fa15c2015-01-13 16:10:49 -0800301
Craig Tiller64be9f72015-05-04 14:53:51 -0700302 cq_expect_completion(v_server, tag(101), 1);
Craig Tiller06eb5ae2015-04-30 13:48:58 -0700303 cq_verify(v_server);
304
Craig Tillerf1071202015-04-30 13:29:48 -0700305 ev = grpc_completion_queue_next(f.client_cq,
306 GRPC_TIMEOUT_SECONDS_TO_DEADLINE(3));
Craig Tiller64be9f72015-05-04 14:53:51 -0700307 GPR_ASSERT(ev.type == GRPC_OP_COMPLETE);
308 GPR_ASSERT(ev.success);
309 GPR_ASSERT(ev.tag == tag(301) || ev.tag == tag(401));
ctiller58393c22015-01-07 14:03:30 -0800310 /* The /alpha or /beta calls started above could be invoked (but NOT both);
311 * check this here */
Craig Tillerfc84e7b2015-01-13 16:49:44 -0800312 /* We'll get tag 303 or 403, we want 300, 400 */
Craig Tiller64be9f72015-05-04 14:53:51 -0700313 live_call = ((int)(gpr_intptr)ev.tag) - 1;
ctiller58393c22015-01-07 14:03:30 -0800314
Craig Tiller3b05a122015-04-28 15:14:40 -0700315 op = ops;
316 op->op = GRPC_OP_SEND_INITIAL_METADATA;
317 op->data.send_initial_metadata.count = 0;
318 op++;
319 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
320 op->data.recv_close_on_server.cancelled = &was_cancelled;
321 op++;
Craig Tiller3b05a122015-04-28 15:14:40 -0700322 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
323 op->data.send_status_from_server.trailing_metadata_count = 0;
324 op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED;
325 op->data.send_status_from_server.status_details = "xyz";
326 op++;
Craig Tiller9f3675f2015-04-29 13:56:33 -0700327 GPR_ASSERT(GRPC_CALL_OK ==
Craig Tiller06eb5ae2015-04-30 13:48:58 -0700328 grpc_call_start_batch(s1, ops, op - ops, tag(102)));
Craig Tiller3b05a122015-04-28 15:14:40 -0700329
Craig Tiller64be9f72015-05-04 14:53:51 -0700330 cq_expect_completion(v_server, tag(102), 1);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800331 cq_verify(v_server);
332
Craig Tiller64be9f72015-05-04 14:53:51 -0700333 cq_expect_completion(v_client, tag(live_call + 2), 1);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800334 /* first request is finished, we should be able to start the second */
Craig Tillerbf444932015-01-13 17:13:08 -0800335 live_call = (live_call == 300) ? 400 : 300;
Craig Tiller64be9f72015-05-04 14:53:51 -0700336 cq_expect_completion(v_client, tag(live_call + 1), 1);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800337 cq_verify(v_client);
338
Craig Tillerabd4cc92015-04-29 14:17:32 -0700339 GPR_ASSERT(GRPC_CALL_OK == grpc_server_request_call(f.server, &s2,
Craig Tiller3b05a122015-04-28 15:14:40 -0700340 &call_details,
341 &request_metadata_recv,
342 f.server_cq, tag(201)));
Craig Tiller64be9f72015-05-04 14:53:51 -0700343 cq_expect_completion(v_server, tag(201), 1);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800344 cq_verify(v_server);
345
Craig Tiller3b05a122015-04-28 15:14:40 -0700346 op = ops;
347 op->op = GRPC_OP_SEND_INITIAL_METADATA;
348 op->data.send_initial_metadata.count = 0;
349 op++;
350 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
351 op->data.recv_close_on_server.cancelled = &was_cancelled;
352 op++;
Craig Tillerfe2b8352015-04-28 15:46:50 -0700353 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
354 op->data.send_status_from_server.trailing_metadata_count = 0;
355 op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED;
356 op->data.send_status_from_server.status_details = "xyz";
357 op++;
Craig Tiller9f3675f2015-04-29 13:56:33 -0700358 GPR_ASSERT(GRPC_CALL_OK ==
Craig Tiller06eb5ae2015-04-30 13:48:58 -0700359 grpc_call_start_batch(s2, ops, op - ops, tag(202)));
360
Craig Tiller64be9f72015-05-04 14:53:51 -0700361 cq_expect_completion(v_client, tag(live_call + 2), 1);
Craig Tiller06eb5ae2015-04-30 13:48:58 -0700362 cq_verify(v_client);
Craig Tillerfe2b8352015-04-28 15:46:50 -0700363
Craig Tiller64be9f72015-05-04 14:53:51 -0700364 cq_expect_completion(v_server, tag(202), 1);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800365 cq_verify(v_server);
366
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800367 cq_verifier_destroy(v_client);
368 cq_verifier_destroy(v_server);
369
370 grpc_call_destroy(c1);
371 grpc_call_destroy(s1);
372 grpc_call_destroy(c2);
373 grpc_call_destroy(s2);
374
Craig Tiller5fe7e5d2015-05-01 10:52:35 -0700375 gpr_free(details1);
376 gpr_free(details2);
377 grpc_metadata_array_destroy(&initial_metadata_recv1);
378 grpc_metadata_array_destroy(&trailing_metadata_recv1);
379 grpc_metadata_array_destroy(&initial_metadata_recv2);
380 grpc_metadata_array_destroy(&trailing_metadata_recv2);
381 grpc_metadata_array_destroy(&request_metadata_recv);
382 grpc_call_details_destroy(&call_details);
383
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800384 end_test(&f);
385 config.tear_down_data(&f);
386}
387
388void grpc_end2end_tests(grpc_end2end_test_config config) {
389 test_max_concurrent_streams(config);
Craig Tiller190d3602015-02-18 09:23:38 -0800390}