blob: 87b0ff084b4f8c02e3279e2bf949b0d795031324 [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) {
70 grpc_event *ev;
71 grpc_completion_type type;
72 do {
73 ev = grpc_completion_queue_next(cq, five_seconds_time());
74 GPR_ASSERT(ev);
75 type = ev->type;
76 grpc_event_finish(ev);
77 } while (type != GRPC_QUEUE_SHUTDOWN);
78}
79
80static void shutdown_server(grpc_end2end_test_fixture *f) {
81 if (!f->server) return;
82 grpc_server_shutdown(f->server);
83 grpc_server_destroy(f->server);
84 f->server = NULL;
85}
86
87static void shutdown_client(grpc_end2end_test_fixture *f) {
88 if (!f->client) return;
89 grpc_channel_destroy(f->client);
90 f->client = NULL;
91}
92
93static void end_test(grpc_end2end_test_fixture *f) {
94 shutdown_server(f);
95 shutdown_client(f);
96
97 grpc_completion_queue_shutdown(f->server_cq);
98 drain_cq(f->server_cq);
99 grpc_completion_queue_destroy(f->server_cq);
100 grpc_completion_queue_shutdown(f->client_cq);
101 drain_cq(f->client_cq);
102 grpc_completion_queue_destroy(f->client_cq);
103}
104
105static void simple_request_body(grpc_end2end_test_fixture f) {
106 grpc_call *c;
107 grpc_call *s;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800108 gpr_timespec deadline = five_seconds_time();
109 cq_verifier *v_client = cq_verifier_create(f.client_cq);
110 cq_verifier *v_server = cq_verifier_create(f.server_cq);
Craig Tiller8b1d9892015-04-28 14:28:19 -0700111 grpc_op ops[6];
112 grpc_op *op;
113 grpc_metadata_array initial_metadata_recv;
114 grpc_metadata_array trailing_metadata_recv;
115 grpc_metadata_array request_metadata_recv;
116 grpc_call_details call_details;
117 grpc_status_code status;
118 char *details = NULL;
119 size_t details_capacity = 0;
120 int was_cancelled = 2;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800121
Craig Tiller8b1d9892015-04-28 14:28:19 -0700122 c = grpc_channel_create_call(f.client, f.client_cq, "/foo",
123 "foo.test.google.fr:1234", deadline);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800124 GPR_ASSERT(c);
125
Craig Tiller8b1d9892015-04-28 14:28:19 -0700126 grpc_metadata_array_init(&initial_metadata_recv);
127 grpc_metadata_array_init(&trailing_metadata_recv);
128 grpc_metadata_array_init(&request_metadata_recv);
129 grpc_call_details_init(&call_details);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800130
Craig Tiller8b1d9892015-04-28 14:28:19 -0700131 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_CLOSE_FROM_CLIENT;
136 op++;
137 op->op = GRPC_OP_RECV_INITIAL_METADATA;
138 op->data.recv_initial_metadata = &initial_metadata_recv;
139 op++;
140 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
141 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
142 op->data.recv_status_on_client.status = &status;
143 op->data.recv_status_on_client.status_details = &details;
144 op->data.recv_status_on_client.status_details_capacity = &details_capacity;
145 op++;
146 GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(c, ops, op - ops, tag(1)));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800147
Craig Tiller8b1d9892015-04-28 14:28:19 -0700148 GPR_ASSERT(GRPC_CALL_OK == grpc_server_request_call(f.server, &s,
149 &call_details,
150 &request_metadata_recv,
151 f.server_cq, tag(101)));
152 cq_expect_completion(v_server, tag(101), GRPC_OP_OK);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800153 cq_verify(v_server);
154
Craig Tiller8b1d9892015-04-28 14:28:19 -0700155 op = ops;
156 op->op = GRPC_OP_SEND_INITIAL_METADATA;
157 op->data.send_initial_metadata.count = 0;
158 op++;
159 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
160 op->data.send_status_from_server.trailing_metadata_count = 0;
161 op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED;
162 op->data.send_status_from_server.status_details = "xyz";
163 op++;
164 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
165 op->data.recv_close_on_server.cancelled = &was_cancelled;
166 op++;
167 GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(s, ops, op - ops, tag(102)));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800168
Craig Tiller8b1d9892015-04-28 14:28:19 -0700169 cq_expect_completion(v_server, tag(102), GRPC_OP_OK);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800170 cq_verify(v_server);
171
Craig Tiller8b1d9892015-04-28 14:28:19 -0700172 cq_expect_completion(v_client, tag(1), GRPC_OP_OK);
173 cq_verify(v_client);
174
175 GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED);
176 GPR_ASSERT(0 == strcmp(details, "xyz"));
177 GPR_ASSERT(0 == strcmp(call_details.method, "/foo"));
178 GPR_ASSERT(0 == strcmp(call_details.host, "foo.test.google.fr:1234"));
179 GPR_ASSERT(was_cancelled == 0);
180
181 gpr_free(details);
182 grpc_metadata_array_destroy(&initial_metadata_recv);
183 grpc_metadata_array_destroy(&trailing_metadata_recv);
184 grpc_metadata_array_destroy(&request_metadata_recv);
185 grpc_call_details_destroy(&call_details);
186
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800187 grpc_call_destroy(c);
188 grpc_call_destroy(s);
189
190 cq_verifier_destroy(v_client);
191 cq_verifier_destroy(v_server);
192}
193
194static void test_max_concurrent_streams(grpc_end2end_test_config config) {
195 grpc_end2end_test_fixture f;
196 grpc_arg server_arg;
197 grpc_channel_args server_args;
198 grpc_call *c1;
199 grpc_call *c2;
200 grpc_call *s1;
201 grpc_call *s2;
ctiller58393c22015-01-07 14:03:30 -0800202 int live_call;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800203 gpr_timespec deadline;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800204 cq_verifier *v_client;
205 cq_verifier *v_server;
ctiller58393c22015-01-07 14:03:30 -0800206 grpc_event *ev;
Craig Tiller3b05a122015-04-28 15:14:40 -0700207 grpc_call_details call_details;
208 grpc_metadata_array request_metadata_recv;
209 grpc_metadata_array initial_metadata_recv1;
210 grpc_metadata_array trailing_metadata_recv1;
211 grpc_metadata_array initial_metadata_recv2;
212 grpc_metadata_array trailing_metadata_recv2;
213 grpc_status_code status1;
214 char *details1 = NULL;
215 size_t details_capacity1 = 0;
216 grpc_status_code status2;
217 char *details2 = NULL;
218 size_t details_capacity2 = 0;
Craig Tillerfe2b8352015-04-28 15:46:50 -0700219 grpc_op ops[6];
220 grpc_op *op;
221 int was_cancelled;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800222
223 server_arg.key = GRPC_ARG_MAX_CONCURRENT_STREAMS;
224 server_arg.type = GRPC_ARG_INTEGER;
225 server_arg.value.integer = 1;
226
227 server_args.num_args = 1;
228 server_args.args = &server_arg;
229
230 f = begin_test(config, __FUNCTION__, NULL, &server_args);
231 v_client = cq_verifier_create(f.client_cq);
232 v_server = cq_verifier_create(f.server_cq);
233
Craig Tiller3b05a122015-04-28 15:14:40 -0700234 grpc_metadata_array_init(&request_metadata_recv);
235
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800236 /* perform a ping-pong to ensure that settings have had a chance to round
237 trip */
238 simple_request_body(f);
239 /* perform another one to make sure that the one stream case still works */
240 simple_request_body(f);
241
242 /* start two requests - ensuring that the second is not accepted until
243 the first completes */
244 deadline = five_seconds_time();
Craig Tiller3b05a122015-04-28 15:14:40 -0700245 c1 = grpc_channel_create_call(f.client, f.client_cq, "/alpha",
246 "foo.test.google.fr:1234", deadline);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800247 GPR_ASSERT(c1);
Craig Tiller3b05a122015-04-28 15:14:40 -0700248 c2 = grpc_channel_create_call(f.client, f.client_cq, "/beta",
249 "foo.test.google.fr:1234", deadline);
250 GPR_ASSERT(c2);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800251
Craig Tiller3b05a122015-04-28 15:14:40 -0700252 GPR_ASSERT(GRPC_CALL_OK == grpc_server_request_call(f.server, &s1,
253 &call_details,
254 &request_metadata_recv,
255 f.server_cq, tag(101)));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800256
Craig Tiller3b05a122015-04-28 15:14:40 -0700257 op = ops;
258 op->op = GRPC_OP_SEND_INITIAL_METADATA;
259 op->data.send_initial_metadata.count = 0;
260 op++;
261 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
262 op++;
263 op->op = GRPC_OP_RECV_INITIAL_METADATA;
Craig Tillerfe2b8352015-04-28 15:46:50 -0700264 op->data.recv_initial_metadata = &initial_metadata_recv1;
Craig Tiller3b05a122015-04-28 15:14:40 -0700265 op++;
266 GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(c1, ops, op - ops, tag(301)));
267
268 op = ops;
269 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
Craig Tillerfe2b8352015-04-28 15:46:50 -0700270 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv1;
271 op->data.recv_status_on_client.status = &status1;
272 op->data.recv_status_on_client.status_details = &details1;
273 op->data.recv_status_on_client.status_details_capacity = &details_capacity1;
Craig Tiller3b05a122015-04-28 15:14:40 -0700274 op++;
275 GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(c1, ops, op - ops, tag(302)));
276
277 op = ops;
278 op->op = GRPC_OP_SEND_INITIAL_METADATA;
279 op->data.send_initial_metadata.count = 0;
280 op++;
281 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
282 op++;
283 op->op = GRPC_OP_RECV_INITIAL_METADATA;
Craig Tillerfe2b8352015-04-28 15:46:50 -0700284 op->data.recv_initial_metadata = &initial_metadata_recv2;
Craig Tiller3b05a122015-04-28 15:14:40 -0700285 op++;
286 GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(c2, ops, op - ops, tag(401)));
287
288 op = ops;
289 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
Craig Tillerfe2b8352015-04-28 15:46:50 -0700290 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv2;
291 op->data.recv_status_on_client.status = &status2;
292 op->data.recv_status_on_client.status_details = &details2;
293 op->data.recv_status_on_client.status_details_capacity = &details_capacity2;
Craig Tiller3b05a122015-04-28 15:14:40 -0700294 op++;
295 GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(c2, ops, op - ops, tag(402)));
Craig Tiller80fa15c2015-01-13 16:10:49 -0800296
ctiller58393c22015-01-07 14:03:30 -0800297 ev = grpc_completion_queue_next(
298 f.client_cq, gpr_time_add(gpr_now(), gpr_time_from_seconds(10)));
299 GPR_ASSERT(ev);
Craig Tiller3b05a122015-04-28 15:14:40 -0700300 GPR_ASSERT(ev->type == GRPC_OP_COMPLETE);
301 GPR_ASSERT(ev->data.op_complete == GRPC_OP_OK);
Craig Tillerfe2b8352015-04-28 15:46:50 -0700302 GPR_ASSERT(ev->tag == tag(301) || ev->tag == tag(401));
ctiller58393c22015-01-07 14:03:30 -0800303 /* The /alpha or /beta calls started above could be invoked (but NOT both);
304 * check this here */
Craig Tillerfc84e7b2015-01-13 16:49:44 -0800305 /* We'll get tag 303 or 403, we want 300, 400 */
Craig Tiller3b05a122015-04-28 15:14:40 -0700306 live_call = ((int)(gpr_intptr) ev->tag) - 1;
ctiller58393c22015-01-07 14:03:30 -0800307 grpc_event_finish(ev);
308
Craig Tiller3b05a122015-04-28 15:14:40 -0700309 cq_expect_completion(v_server, tag(100), GRPC_OP_OK);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800310 cq_verify(v_server);
311
Craig Tiller3b05a122015-04-28 15:14:40 -0700312 op = ops;
313 op->op = GRPC_OP_SEND_INITIAL_METADATA;
314 op->data.send_initial_metadata.count = 0;
315 op++;
316 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
317 op->data.recv_close_on_server.cancelled = &was_cancelled;
318 op++;
Craig Tillerfe2b8352015-04-28 15:46:50 -0700319 GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(s1, ops, op - ops, tag(102)));
Craig Tiller3b05a122015-04-28 15:14:40 -0700320
Craig Tillerfe2b8352015-04-28 15:46:50 -0700321 cq_expect_completion(v_client, tag(live_call + 1), GRPC_OP_OK);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800322 cq_verify(v_client);
323
Craig Tiller3b05a122015-04-28 15:14:40 -0700324 op = ops;
325 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
326 op->data.send_status_from_server.trailing_metadata_count = 0;
327 op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED;
328 op->data.send_status_from_server.status_details = "xyz";
329 op++;
Craig Tillerfe2b8352015-04-28 15:46:50 -0700330 GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(s1, ops, op - ops, tag(103)));
Craig Tiller3b05a122015-04-28 15:14:40 -0700331
332 cq_expect_completion(v_server, tag(103), GRPC_OP_OK);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800333 cq_verify(v_server);
334
335 /* first request is finished, we should be able to start the second */
Craig Tiller3b05a122015-04-28 15:14:40 -0700336 cq_expect_completion(v_client, tag(live_call + 2), GRPC_OP_OK);
Craig Tillerbf444932015-01-13 17:13:08 -0800337 live_call = (live_call == 300) ? 400 : 300;
Craig Tiller3b05a122015-04-28 15:14:40 -0700338 cq_expect_completion(v_client, tag(live_call + 3), GRPC_OP_OK);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800339 cq_verify(v_client);
340
Craig Tiller3b05a122015-04-28 15:14:40 -0700341 GPR_ASSERT(GRPC_CALL_OK == grpc_server_request_call(f.server, &s1,
342 &call_details,
343 &request_metadata_recv,
344 f.server_cq, tag(201)));
345 cq_expect_server_rpc_new(v_server, &s2, tag(201),
ctiller58393c22015-01-07 14:03:30 -0800346 live_call == 300 ? "/alpha" : "/beta",
Julien Boeuf597a4f22015-02-23 15:57:14 -0800347 "foo.test.google.fr", deadline, NULL);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800348 cq_verify(v_server);
349
Craig Tiller3b05a122015-04-28 15:14:40 -0700350 op = ops;
351 op->op = GRPC_OP_SEND_INITIAL_METADATA;
352 op->data.send_initial_metadata.count = 0;
353 op++;
354 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
355 op->data.recv_close_on_server.cancelled = &was_cancelled;
356 op++;
Craig Tillerfe2b8352015-04-28 15:46:50 -0700357 GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(s2, ops, op - ops, tag(202)));
Craig Tiller3b05a122015-04-28 15:14:40 -0700358
Craig Tillerfe2b8352015-04-28 15:46:50 -0700359 cq_expect_completion(v_client, tag(live_call + 1), GRPC_OP_OK);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800360 cq_verify(v_client);
361
Craig Tillerfe2b8352015-04-28 15:46:50 -0700362 op = ops;
363 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
364 op->data.send_status_from_server.trailing_metadata_count = 0;
365 op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED;
366 op->data.send_status_from_server.status_details = "xyz";
367 op++;
368 GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(s2, ops, op - ops, tag(203)));
369
370 cq_expect_completion(v_server, tag(203), GRPC_OP_OK);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800371 cq_verify(v_server);
372
Craig Tillerfe2b8352015-04-28 15:46:50 -0700373 cq_expect_completion(v_client, tag(live_call + 2), GRPC_OP_OK);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800374 cq_verify(v_client);
375
376 cq_verifier_destroy(v_client);
377 cq_verifier_destroy(v_server);
378
379 grpc_call_destroy(c1);
380 grpc_call_destroy(s1);
381 grpc_call_destroy(c2);
382 grpc_call_destroy(s2);
383
384 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}