Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 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 | |
| 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 | |
| 47 | enum { TIMEOUT = 200000 }; |
| 48 | |
| 49 | static void *tag(gpr_intptr t) { return (void *)t; } |
| 50 | |
| 51 | static 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 | |
| 63 | static gpr_timespec n_seconds_time(int n) { |
Craig Tiller | 8ad8a41 | 2015-02-25 08:36:40 -0800 | [diff] [blame^] | 64 | return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(n); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 65 | } |
| 66 | |
Craig Tiller | 32946d3 | 2015-01-15 11:37:30 -0800 | [diff] [blame] | 67 | static gpr_timespec five_seconds_time(void) { return n_seconds_time(5); } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 68 | |
| 69 | static 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 | |
| 80 | static 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 | |
| 87 | static 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 | |
| 93 | static 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 | |
| 105 | static void simple_request_body(grpc_end2end_test_fixture f) { |
| 106 | grpc_call *c; |
| 107 | grpc_call *s; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 108 | 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 Boeuf | 597a4f2 | 2015-02-23 15:57:14 -0800 | [diff] [blame] | 112 | c = grpc_channel_create_call_old(f.client, "/foo", "foo.test.google.fr", |
Craig Tiller | a7cac78 | 2015-02-02 10:16:30 -0800 | [diff] [blame] | 113 | deadline); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 114 | GPR_ASSERT(c); |
| 115 | |
| 116 | GPR_ASSERT(GRPC_CALL_OK == |
Craig Tiller | a7cac78 | 2015-02-02 10:16:30 -0800 | [diff] [blame] | 117 | grpc_call_invoke_old(c, f.client_cq, tag(2), tag(3), 0)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 118 | |
Craig Tiller | a7cac78 | 2015-02-02 10:16:30 -0800 | [diff] [blame] | 119 | GPR_ASSERT(GRPC_CALL_OK == grpc_call_writes_done_old(c, tag(4))); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 120 | cq_expect_finish_accepted(v_client, tag(4), GRPC_OP_OK); |
| 121 | cq_verify(v_client); |
| 122 | |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 123 | GPR_ASSERT(GRPC_CALL_OK == grpc_server_request_call_old(f.server, tag(100))); |
Craig Tiller | 8ad8a41 | 2015-02-25 08:36:40 -0800 | [diff] [blame^] | 124 | cq_expect_server_rpc_new(v_server, &s, tag(100), "/foo", "foo.test.google.fr", |
| 125 | deadline, NULL); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 126 | cq_verify(v_server); |
| 127 | |
Craig Tiller | a7cac78 | 2015-02-02 10:16:30 -0800 | [diff] [blame] | 128 | 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 Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 131 | cq_expect_client_metadata_read(v_client, tag(2), NULL); |
| 132 | cq_verify(v_client); |
| 133 | |
Craig Tiller | a7cac78 | 2015-02-02 10:16:30 -0800 | [diff] [blame] | 134 | GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_write_status_old( |
ctiller | 2845cad | 2014-12-15 15:14:12 -0800 | [diff] [blame] | 135 | s, GRPC_STATUS_UNIMPLEMENTED, "xyz", tag(5))); |
| 136 | cq_expect_finished_with_status(v_client, tag(3), GRPC_STATUS_UNIMPLEMENTED, |
| 137 | "xyz", NULL); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 138 | cq_verify(v_client); |
| 139 | |
| 140 | cq_expect_finish_accepted(v_server, tag(5), GRPC_OP_OK); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 141 | 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 | |
| 151 | static 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; |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 159 | int live_call; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 160 | gpr_timespec deadline; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 161 | cq_verifier *v_client; |
| 162 | cq_verifier *v_server; |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 163 | grpc_event *ev; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 164 | |
| 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 Boeuf | 597a4f2 | 2015-02-23 15:57:14 -0800 | [diff] [blame] | 185 | c1 = grpc_channel_create_call_old(f.client, "/alpha", "foo.test.google.fr", |
Craig Tiller | a7cac78 | 2015-02-02 10:16:30 -0800 | [diff] [blame] | 186 | deadline); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 187 | GPR_ASSERT(c1); |
Julien Boeuf | 597a4f2 | 2015-02-23 15:57:14 -0800 | [diff] [blame] | 188 | c2 = grpc_channel_create_call_old(f.client, "/beta", "foo.test.google.fr", |
Craig Tiller | a7cac78 | 2015-02-02 10:16:30 -0800 | [diff] [blame] | 189 | deadline); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 190 | GPR_ASSERT(c1); |
| 191 | |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 192 | GPR_ASSERT(GRPC_CALL_OK == grpc_server_request_call_old(f.server, tag(100))); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 193 | |
Craig Tiller | 40fc7a6 | 2015-01-13 16:11:58 -0800 | [diff] [blame] | 194 | GPR_ASSERT(GRPC_CALL_OK == |
Craig Tiller | a7cac78 | 2015-02-02 10:16:30 -0800 | [diff] [blame] | 195 | grpc_call_invoke_old(c1, f.client_cq, tag(301), tag(302), 0)); |
Craig Tiller | 40fc7a6 | 2015-01-13 16:11:58 -0800 | [diff] [blame] | 196 | GPR_ASSERT(GRPC_CALL_OK == |
Craig Tiller | a7cac78 | 2015-02-02 10:16:30 -0800 | [diff] [blame] | 197 | 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 Tiller | 80fa15c | 2015-01-13 16:10:49 -0800 | [diff] [blame] | 200 | |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 201 | ev = grpc_completion_queue_next( |
| 202 | f.client_cq, gpr_time_add(gpr_now(), gpr_time_from_seconds(10))); |
| 203 | GPR_ASSERT(ev); |
Craig Tiller | 80fa15c | 2015-01-13 16:10:49 -0800 | [diff] [blame] | 204 | GPR_ASSERT(ev->type == GRPC_FINISH_ACCEPTED); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 205 | 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 Tiller | fc84e7b | 2015-01-13 16:49:44 -0800 | [diff] [blame] | 208 | /* We'll get tag 303 or 403, we want 300, 400 */ |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 209 | live_call = ((int)(gpr_intptr) ev->tag) - 3; |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 210 | grpc_event_finish(ev); |
| 211 | |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 212 | cq_expect_server_rpc_new(v_server, &s1, tag(100), |
| 213 | live_call == 300 ? "/alpha" : "/beta", |
Julien Boeuf | 597a4f2 | 2015-02-23 15:57:14 -0800 | [diff] [blame] | 214 | "foo.test.google.fr", deadline, NULL); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 215 | cq_verify(v_server); |
| 216 | |
Craig Tiller | d7f2c2b | 2015-01-15 10:18:47 -0800 | [diff] [blame] | 217 | GPR_ASSERT(GRPC_CALL_OK == |
Craig Tiller | a7cac78 | 2015-02-02 10:16:30 -0800 | [diff] [blame] | 218 | 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)); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 220 | cq_expect_client_metadata_read(v_client, tag(live_call + 1), NULL); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 221 | cq_verify(v_client); |
| 222 | |
| 223 | GPR_ASSERT(GRPC_CALL_OK == |
Craig Tiller | a7cac78 | 2015-02-02 10:16:30 -0800 | [diff] [blame] | 224 | grpc_call_start_write_status_old(s1, GRPC_STATUS_UNIMPLEMENTED, |
| 225 | "xyz", tag(103))); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 226 | 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 */ |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 231 | cq_expect_finished_with_status(v_client, tag(live_call + 2), |
| 232 | GRPC_STATUS_UNIMPLEMENTED, "xyz", NULL); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 233 | cq_expect_finish_accepted(v_client, tag(live_call + 3), GRPC_OP_OK); |
Craig Tiller | bf44493 | 2015-01-13 17:13:08 -0800 | [diff] [blame] | 234 | live_call = (live_call == 300) ? 400 : 300; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 235 | cq_verify(v_client); |
| 236 | |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 237 | GPR_ASSERT(GRPC_CALL_OK == grpc_server_request_call_old(f.server, tag(200))); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 238 | cq_expect_server_rpc_new(v_server, &s2, tag(200), |
| 239 | live_call == 300 ? "/alpha" : "/beta", |
Julien Boeuf | 597a4f2 | 2015-02-23 15:57:14 -0800 | [diff] [blame] | 240 | "foo.test.google.fr", deadline, NULL); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 241 | cq_verify(v_server); |
| 242 | |
Craig Tiller | d7f2c2b | 2015-01-15 10:18:47 -0800 | [diff] [blame] | 243 | GPR_ASSERT(GRPC_CALL_OK == |
Craig Tiller | a7cac78 | 2015-02-02 10:16:30 -0800 | [diff] [blame] | 244 | 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)); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 246 | cq_expect_client_metadata_read(v_client, tag(live_call + 1), NULL); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 247 | cq_verify(v_client); |
| 248 | |
| 249 | GPR_ASSERT(GRPC_CALL_OK == |
Craig Tiller | a7cac78 | 2015-02-02 10:16:30 -0800 | [diff] [blame] | 250 | grpc_call_start_write_status_old(s2, GRPC_STATUS_UNIMPLEMENTED, |
| 251 | "xyz", tag(203))); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 252 | 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 | |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 256 | cq_expect_finished_with_status(v_client, tag(live_call + 2), |
| 257 | GRPC_STATUS_UNIMPLEMENTED, "xyz", NULL); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 258 | 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 | |
| 272 | void grpc_end2end_tests(grpc_end2end_test_config config) { |
| 273 | test_max_concurrent_streams(config); |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 274 | } |