Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2014, Google Inc. |
| 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) { |
| 64 | return gpr_time_add(gpr_now(), gpr_time_from_micros(GPR_US_PER_SEC * n)); |
| 65 | } |
| 66 | |
| 67 | static gpr_timespec five_seconds_time() { return n_seconds_time(5); } |
| 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 | |
| 112 | c = grpc_channel_create_call(f.client, "/foo", "test.google.com", deadline); |
| 113 | GPR_ASSERT(c); |
| 114 | |
| 115 | GPR_ASSERT(GRPC_CALL_OK == |
Craig Tiller | 80fa15c | 2015-01-13 16:10:49 -0800 | [diff] [blame^] | 116 | grpc_call_invoke(c, f.client_cq, tag(2), tag(3), 0)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 117 | |
| 118 | GPR_ASSERT(GRPC_CALL_OK == grpc_call_writes_done(c, tag(4))); |
| 119 | cq_expect_finish_accepted(v_client, tag(4), GRPC_OP_OK); |
| 120 | cq_verify(v_client); |
| 121 | |
| 122 | GPR_ASSERT(GRPC_CALL_OK == grpc_server_request_call(f.server, tag(100))); |
| 123 | cq_expect_server_rpc_new(v_server, &s, tag(100), "/foo", "test.google.com", |
| 124 | deadline, NULL); |
| 125 | cq_verify(v_server); |
| 126 | |
| 127 | GPR_ASSERT(GRPC_CALL_OK == grpc_call_accept(s, f.server_cq, tag(102), 0)); |
| 128 | cq_expect_client_metadata_read(v_client, tag(2), NULL); |
| 129 | cq_verify(v_client); |
| 130 | |
ctiller | 2845cad | 2014-12-15 15:14:12 -0800 | [diff] [blame] | 131 | GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_write_status( |
| 132 | s, GRPC_STATUS_UNIMPLEMENTED, "xyz", tag(5))); |
| 133 | cq_expect_finished_with_status(v_client, tag(3), GRPC_STATUS_UNIMPLEMENTED, |
| 134 | "xyz", NULL); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 135 | cq_verify(v_client); |
| 136 | |
| 137 | cq_expect_finish_accepted(v_server, tag(5), GRPC_OP_OK); |
| 138 | cq_verify(v_server); |
| 139 | cq_expect_finished(v_server, tag(102), NULL); |
| 140 | cq_verify(v_server); |
| 141 | |
| 142 | grpc_call_destroy(c); |
| 143 | grpc_call_destroy(s); |
| 144 | |
| 145 | cq_verifier_destroy(v_client); |
| 146 | cq_verifier_destroy(v_server); |
| 147 | } |
| 148 | |
| 149 | static void test_max_concurrent_streams(grpc_end2end_test_config config) { |
| 150 | grpc_end2end_test_fixture f; |
| 151 | grpc_arg server_arg; |
| 152 | grpc_channel_args server_args; |
| 153 | grpc_call *c1; |
| 154 | grpc_call *c2; |
| 155 | grpc_call *s1; |
| 156 | grpc_call *s2; |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 157 | int live_call; |
| 158 | grpc_call *live_call_obj; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 159 | gpr_timespec deadline; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 160 | cq_verifier *v_client; |
| 161 | cq_verifier *v_server; |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 162 | grpc_event *ev; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 163 | |
| 164 | server_arg.key = GRPC_ARG_MAX_CONCURRENT_STREAMS; |
| 165 | server_arg.type = GRPC_ARG_INTEGER; |
| 166 | server_arg.value.integer = 1; |
| 167 | |
| 168 | server_args.num_args = 1; |
| 169 | server_args.args = &server_arg; |
| 170 | |
| 171 | f = begin_test(config, __FUNCTION__, NULL, &server_args); |
| 172 | v_client = cq_verifier_create(f.client_cq); |
| 173 | v_server = cq_verifier_create(f.server_cq); |
| 174 | |
| 175 | /* perform a ping-pong to ensure that settings have had a chance to round |
| 176 | trip */ |
| 177 | simple_request_body(f); |
| 178 | /* perform another one to make sure that the one stream case still works */ |
| 179 | simple_request_body(f); |
| 180 | |
| 181 | /* start two requests - ensuring that the second is not accepted until |
| 182 | the first completes */ |
| 183 | deadline = five_seconds_time(); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 184 | c1 = |
| 185 | grpc_channel_create_call(f.client, "/alpha", "test.google.com", deadline); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 186 | GPR_ASSERT(c1); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 187 | c2 = grpc_channel_create_call(f.client, "/beta", "test.google.com", deadline); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 188 | GPR_ASSERT(c1); |
| 189 | |
| 190 | GPR_ASSERT(GRPC_CALL_OK == grpc_server_request_call(f.server, tag(100))); |
| 191 | |
Craig Tiller | 80fa15c | 2015-01-13 16:10:49 -0800 | [diff] [blame^] | 192 | GPR_ASSERT(GRPC_CALL_OK == grpc_call_invoke(c1, f.client_cq, |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 193 | tag(301), tag(302), 0)); |
Craig Tiller | 80fa15c | 2015-01-13 16:10:49 -0800 | [diff] [blame^] | 194 | GPR_ASSERT(GRPC_CALL_OK == grpc_call_invoke(c2, f.client_cq, |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 195 | tag(401), tag(402), 0)); |
Craig Tiller | 80fa15c | 2015-01-13 16:10:49 -0800 | [diff] [blame^] | 196 | GPR_ASSERT(GRPC_CALL_OK == grpc_call_writes_done(c1, tag(303))); |
| 197 | GPR_ASSERT(GRPC_CALL_OK == grpc_call_writes_done(c2, tag(303))); |
| 198 | |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 199 | ev = grpc_completion_queue_next( |
| 200 | f.client_cq, gpr_time_add(gpr_now(), gpr_time_from_seconds(10))); |
| 201 | GPR_ASSERT(ev); |
Craig Tiller | 80fa15c | 2015-01-13 16:10:49 -0800 | [diff] [blame^] | 202 | GPR_ASSERT(ev->type == GRPC_FINISH_ACCEPTED); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 203 | GPR_ASSERT(ev->data.invoke_accepted == GRPC_OP_OK); |
| 204 | /* The /alpha or /beta calls started above could be invoked (but NOT both); |
| 205 | * check this here */ |
| 206 | live_call = (int)(gpr_intptr)ev->tag; |
| 207 | live_call_obj = live_call == 300 ? c1 : c2; |
| 208 | grpc_event_finish(ev); |
| 209 | |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 210 | cq_expect_server_rpc_new(v_server, &s1, tag(100), |
| 211 | live_call == 300 ? "/alpha" : "/beta", |
| 212 | "test.google.com", deadline, NULL); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 213 | cq_verify(v_server); |
| 214 | |
| 215 | GPR_ASSERT(GRPC_CALL_OK == grpc_call_accept(s1, f.server_cq, tag(102), 0)); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 216 | cq_expect_client_metadata_read(v_client, tag(live_call + 1), NULL); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 217 | cq_verify(v_client); |
| 218 | |
| 219 | GPR_ASSERT(GRPC_CALL_OK == |
ctiller | 2845cad | 2014-12-15 15:14:12 -0800 | [diff] [blame] | 220 | grpc_call_start_write_status(s1, GRPC_STATUS_UNIMPLEMENTED, "xyz", |
| 221 | tag(103))); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 222 | cq_expect_finish_accepted(v_server, tag(103), GRPC_OP_OK); |
| 223 | cq_expect_finished(v_server, tag(102), NULL); |
| 224 | cq_verify(v_server); |
| 225 | |
| 226 | /* first request is finished, we should be able to start the second */ |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 227 | cq_expect_finished_with_status(v_client, tag(live_call + 2), |
| 228 | GRPC_STATUS_UNIMPLEMENTED, "xyz", NULL); |
| 229 | live_call = (live_call == 300) ? 400 : 300; |
| 230 | live_call_obj = live_call == 300 ? c1 : c2; |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 231 | cq_expect_finish_accepted(v_client, tag(live_call + 3), GRPC_OP_OK); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 232 | cq_verify(v_client); |
| 233 | |
| 234 | GPR_ASSERT(GRPC_CALL_OK == grpc_server_request_call(f.server, tag(200))); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 235 | cq_expect_server_rpc_new(v_server, &s2, tag(200), |
| 236 | live_call == 300 ? "/alpha" : "/beta", |
| 237 | "test.google.com", deadline, NULL); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 238 | cq_verify(v_server); |
| 239 | |
| 240 | GPR_ASSERT(GRPC_CALL_OK == grpc_call_accept(s2, f.server_cq, tag(202), 0)); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 241 | cq_expect_client_metadata_read(v_client, tag(live_call + 1), NULL); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 242 | cq_verify(v_client); |
| 243 | |
| 244 | GPR_ASSERT(GRPC_CALL_OK == |
ctiller | 2845cad | 2014-12-15 15:14:12 -0800 | [diff] [blame] | 245 | grpc_call_start_write_status(s2, GRPC_STATUS_UNIMPLEMENTED, "xyz", |
| 246 | tag(203))); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 247 | cq_expect_finish_accepted(v_server, tag(203), GRPC_OP_OK); |
| 248 | cq_expect_finished(v_server, tag(202), NULL); |
| 249 | cq_verify(v_server); |
| 250 | |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 251 | cq_expect_finished_with_status(v_client, tag(live_call + 2), |
| 252 | GRPC_STATUS_UNIMPLEMENTED, "xyz", NULL); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 253 | cq_verify(v_client); |
| 254 | |
| 255 | cq_verifier_destroy(v_client); |
| 256 | cq_verifier_destroy(v_server); |
| 257 | |
| 258 | grpc_call_destroy(c1); |
| 259 | grpc_call_destroy(s1); |
| 260 | grpc_call_destroy(c2); |
| 261 | grpc_call_destroy(s2); |
| 262 | |
| 263 | end_test(&f); |
| 264 | config.tear_down_data(&f); |
| 265 | } |
| 266 | |
| 267 | void grpc_end2end_tests(grpc_end2end_test_config config) { |
| 268 | test_max_concurrent_streams(config); |
| 269 | } |