blob: d204fbc9d7272b1bc38c7fa09022c2ab4303fc96 [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) {
64 return gpr_time_add(gpr_now(), gpr_time_from_micros(GPR_US_PER_SEC * n));
65}
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);
111
Julien Boeuf54b21922015-02-04 16:39:35 -0800112 c = grpc_channel_create_call_old(f.client, "/foo", "foo.test.google.com",
Craig Tillera7cac782015-02-02 10:16:30 -0800113 deadline);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800114 GPR_ASSERT(c);
115
116 GPR_ASSERT(GRPC_CALL_OK ==
Craig Tillera7cac782015-02-02 10:16:30 -0800117 grpc_call_invoke_old(c, f.client_cq, tag(2), tag(3), 0));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800118
Craig Tillera7cac782015-02-02 10:16:30 -0800119 GPR_ASSERT(GRPC_CALL_OK == grpc_call_writes_done_old(c, tag(4)));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800120 cq_expect_finish_accepted(v_client, tag(4), GRPC_OP_OK);
121 cq_verify(v_client);
122
Craig Tillercce17ac2015-01-20 09:29:28 -0800123 GPR_ASSERT(GRPC_CALL_OK == grpc_server_request_call_old(f.server, tag(100)));
Julien Boeuf54b21922015-02-04 16:39:35 -0800124 cq_expect_server_rpc_new(v_server, &s, tag(100), "/foo",
125 "foo.test.google.com", deadline, NULL);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800126 cq_verify(v_server);
127
Craig Tillera7cac782015-02-02 10:16:30 -0800128 GPR_ASSERT(GRPC_CALL_OK ==
129 grpc_call_server_accept_old(s, f.server_cq, tag(102)));
130 GPR_ASSERT(GRPC_CALL_OK == grpc_call_server_end_initial_metadata_old(s, 0));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800131 cq_expect_client_metadata_read(v_client, tag(2), NULL);
132 cq_verify(v_client);
133
Craig Tillera7cac782015-02-02 10:16:30 -0800134 GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_write_status_old(
ctiller2845cad2014-12-15 15:14:12 -0800135 s, GRPC_STATUS_UNIMPLEMENTED, "xyz", tag(5)));
136 cq_expect_finished_with_status(v_client, tag(3), GRPC_STATUS_UNIMPLEMENTED,
137 "xyz", NULL);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800138 cq_verify(v_client);
139
140 cq_expect_finish_accepted(v_server, tag(5), GRPC_OP_OK);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800141 cq_expect_finished(v_server, tag(102), NULL);
142 cq_verify(v_server);
143
144 grpc_call_destroy(c);
145 grpc_call_destroy(s);
146
147 cq_verifier_destroy(v_client);
148 cq_verifier_destroy(v_server);
149}
150
151static void test_max_concurrent_streams(grpc_end2end_test_config config) {
152 grpc_end2end_test_fixture f;
153 grpc_arg server_arg;
154 grpc_channel_args server_args;
155 grpc_call *c1;
156 grpc_call *c2;
157 grpc_call *s1;
158 grpc_call *s2;
ctiller58393c22015-01-07 14:03:30 -0800159 int live_call;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800160 gpr_timespec deadline;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800161 cq_verifier *v_client;
162 cq_verifier *v_server;
ctiller58393c22015-01-07 14:03:30 -0800163 grpc_event *ev;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800164
165 server_arg.key = GRPC_ARG_MAX_CONCURRENT_STREAMS;
166 server_arg.type = GRPC_ARG_INTEGER;
167 server_arg.value.integer = 1;
168
169 server_args.num_args = 1;
170 server_args.args = &server_arg;
171
172 f = begin_test(config, __FUNCTION__, NULL, &server_args);
173 v_client = cq_verifier_create(f.client_cq);
174 v_server = cq_verifier_create(f.server_cq);
175
176 /* perform a ping-pong to ensure that settings have had a chance to round
177 trip */
178 simple_request_body(f);
179 /* perform another one to make sure that the one stream case still works */
180 simple_request_body(f);
181
182 /* start two requests - ensuring that the second is not accepted until
183 the first completes */
184 deadline = five_seconds_time();
Julien Boeuf54b21922015-02-04 16:39:35 -0800185 c1 = grpc_channel_create_call_old(f.client, "/alpha", "foo.test.google.com",
Craig Tillera7cac782015-02-02 10:16:30 -0800186 deadline);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800187 GPR_ASSERT(c1);
Julien Boeuf54b21922015-02-04 16:39:35 -0800188 c2 = grpc_channel_create_call_old(f.client, "/beta", "foo.test.google.com",
Craig Tillera7cac782015-02-02 10:16:30 -0800189 deadline);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800190 GPR_ASSERT(c1);
191
Craig Tillercce17ac2015-01-20 09:29:28 -0800192 GPR_ASSERT(GRPC_CALL_OK == grpc_server_request_call_old(f.server, tag(100)));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800193
Craig Tiller40fc7a62015-01-13 16:11:58 -0800194 GPR_ASSERT(GRPC_CALL_OK ==
Craig Tillera7cac782015-02-02 10:16:30 -0800195 grpc_call_invoke_old(c1, f.client_cq, tag(301), tag(302), 0));
Craig Tiller40fc7a62015-01-13 16:11:58 -0800196 GPR_ASSERT(GRPC_CALL_OK ==
Craig Tillera7cac782015-02-02 10:16:30 -0800197 grpc_call_invoke_old(c2, f.client_cq, tag(401), tag(402), 0));
198 GPR_ASSERT(GRPC_CALL_OK == grpc_call_writes_done_old(c1, tag(303)));
199 GPR_ASSERT(GRPC_CALL_OK == grpc_call_writes_done_old(c2, tag(303)));
Craig Tiller80fa15c2015-01-13 16:10:49 -0800200
ctiller58393c22015-01-07 14:03:30 -0800201 ev = grpc_completion_queue_next(
202 f.client_cq, gpr_time_add(gpr_now(), gpr_time_from_seconds(10)));
203 GPR_ASSERT(ev);
Craig Tiller80fa15c2015-01-13 16:10:49 -0800204 GPR_ASSERT(ev->type == GRPC_FINISH_ACCEPTED);
ctiller58393c22015-01-07 14:03:30 -0800205 GPR_ASSERT(ev->data.invoke_accepted == GRPC_OP_OK);
206 /* The /alpha or /beta calls started above could be invoked (but NOT both);
207 * check this here */
Craig Tillerfc84e7b2015-01-13 16:49:44 -0800208 /* We'll get tag 303 or 403, we want 300, 400 */
Craig Tillercce17ac2015-01-20 09:29:28 -0800209 live_call = ((int)(gpr_intptr) ev->tag) - 3;
ctiller58393c22015-01-07 14:03:30 -0800210 grpc_event_finish(ev);
211
ctiller58393c22015-01-07 14:03:30 -0800212 cq_expect_server_rpc_new(v_server, &s1, tag(100),
213 live_call == 300 ? "/alpha" : "/beta",
Julien Boeuf54b21922015-02-04 16:39:35 -0800214 "foo.test.google.com", deadline, NULL);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800215 cq_verify(v_server);
216
Craig Tillerd7f2c2b2015-01-15 10:18:47 -0800217 GPR_ASSERT(GRPC_CALL_OK ==
Craig Tillera7cac782015-02-02 10:16:30 -0800218 grpc_call_server_accept_old(s1, f.server_cq, tag(102)));
219 GPR_ASSERT(GRPC_CALL_OK == grpc_call_server_end_initial_metadata_old(s1, 0));
ctiller58393c22015-01-07 14:03:30 -0800220 cq_expect_client_metadata_read(v_client, tag(live_call + 1), NULL);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800221 cq_verify(v_client);
222
223 GPR_ASSERT(GRPC_CALL_OK ==
Craig Tillera7cac782015-02-02 10:16:30 -0800224 grpc_call_start_write_status_old(s1, GRPC_STATUS_UNIMPLEMENTED,
225 "xyz", tag(103)));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800226 cq_expect_finish_accepted(v_server, tag(103), GRPC_OP_OK);
227 cq_expect_finished(v_server, tag(102), NULL);
228 cq_verify(v_server);
229
230 /* first request is finished, we should be able to start the second */
ctiller58393c22015-01-07 14:03:30 -0800231 cq_expect_finished_with_status(v_client, tag(live_call + 2),
232 GRPC_STATUS_UNIMPLEMENTED, "xyz", NULL);
ctiller58393c22015-01-07 14:03:30 -0800233 cq_expect_finish_accepted(v_client, tag(live_call + 3), GRPC_OP_OK);
Craig Tillerbf444932015-01-13 17:13:08 -0800234 live_call = (live_call == 300) ? 400 : 300;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800235 cq_verify(v_client);
236
Craig Tillercce17ac2015-01-20 09:29:28 -0800237 GPR_ASSERT(GRPC_CALL_OK == grpc_server_request_call_old(f.server, tag(200)));
ctiller58393c22015-01-07 14:03:30 -0800238 cq_expect_server_rpc_new(v_server, &s2, tag(200),
239 live_call == 300 ? "/alpha" : "/beta",
Julien Boeuf54b21922015-02-04 16:39:35 -0800240 "foo.test.google.com", deadline, NULL);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800241 cq_verify(v_server);
242
Craig Tillerd7f2c2b2015-01-15 10:18:47 -0800243 GPR_ASSERT(GRPC_CALL_OK ==
Craig Tillera7cac782015-02-02 10:16:30 -0800244 grpc_call_server_accept_old(s2, f.server_cq, tag(202)));
245 GPR_ASSERT(GRPC_CALL_OK == grpc_call_server_end_initial_metadata_old(s2, 0));
ctiller58393c22015-01-07 14:03:30 -0800246 cq_expect_client_metadata_read(v_client, tag(live_call + 1), NULL);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800247 cq_verify(v_client);
248
249 GPR_ASSERT(GRPC_CALL_OK ==
Craig Tillera7cac782015-02-02 10:16:30 -0800250 grpc_call_start_write_status_old(s2, GRPC_STATUS_UNIMPLEMENTED,
251 "xyz", tag(203)));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800252 cq_expect_finish_accepted(v_server, tag(203), GRPC_OP_OK);
253 cq_expect_finished(v_server, tag(202), NULL);
254 cq_verify(v_server);
255
ctiller58393c22015-01-07 14:03:30 -0800256 cq_expect_finished_with_status(v_client, tag(live_call + 2),
257 GRPC_STATUS_UNIMPLEMENTED, "xyz", NULL);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800258 cq_verify(v_client);
259
260 cq_verifier_destroy(v_client);
261 cq_verifier_destroy(v_server);
262
263 grpc_call_destroy(c1);
264 grpc_call_destroy(s1);
265 grpc_call_destroy(c2);
266 grpc_call_destroy(s2);
267
268 end_test(&f);
269 config.tear_down_data(&f);
270}
271
272void grpc_end2end_tests(grpc_end2end_test_config config) {
273 test_max_concurrent_streams(config);
Craig Tiller06059952015-02-18 08:34:56 -0800274}