blob: d390a954e5c5a89ea0cfe265389e6c07dc4e1ea6 [file] [log] [blame]
Craig Tiller434a36b2015-05-04 09:49:22 -07001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015 gRPC authors.
Craig Tiller434a36b2015-05-04 09:49:22 -07004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
Craig Tiller434a36b2015-05-04 09:49:22 -07008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Craig Tiller434a36b2015-05-04 09:49:22 -070010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
Craig Tiller434a36b2015-05-04 09:49:22 -070016 *
17 */
18
19#include "test/core/end2end/end2end_tests.h"
20
21#include <stdio.h>
22#include <string.h>
Craig Tiller434a36b2015-05-04 09:49:22 -070023
Craig Tiller434a36b2015-05-04 09:49:22 -070024#include <grpc/byte_buffer.h>
25#include <grpc/grpc.h>
26#include <grpc/support/alloc.h>
27#include <grpc/support/log.h>
Craig Tillerf62c4d52015-12-04 07:43:07 -080028#include <grpc/support/string_util.h>
Craig Tiller434a36b2015-05-04 09:49:22 -070029#include <grpc/support/time.h>
30#include <grpc/support/useful.h>
Craig Tillerf62c4d52015-12-04 07:43:07 -080031
Craig Tiller9533d042016-03-25 17:11:06 -070032#include "src/core/lib/support/string.h"
Craig Tiller434a36b2015-05-04 09:49:22 -070033#include "test/core/end2end/cq_verifier.h"
34
Craig Tillerbaa14a92017-11-03 09:09:36 -070035static void* tag(intptr_t t) { return (void*)t; }
Craig Tiller434a36b2015-05-04 09:49:22 -070036
37static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
Craig Tillerbaa14a92017-11-03 09:09:36 -070038 const char* test_name,
39 grpc_channel_args* client_args,
40 grpc_channel_args* server_args) {
Craig Tiller434a36b2015-05-04 09:49:22 -070041 grpc_end2end_test_fixture f;
Robbie Shade55a046a2017-01-25 15:14:28 -050042 gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name);
Craig Tiller434a36b2015-05-04 09:49:22 -070043 f = config.create_fixture(client_args, server_args);
Craig Tiller434a36b2015-05-04 09:49:22 -070044 config.init_server(&f, server_args);
Mark D. Rothe127a392016-10-27 08:27:15 -070045 config.init_client(&f, client_args);
Craig Tiller434a36b2015-05-04 09:49:22 -070046 return f;
47}
48
Chris Evansed2a5472017-03-27 17:34:51 -050049static gpr_timespec n_seconds_from_now(int n) {
Robbie Shadeca7effc2017-01-17 09:14:29 -050050 return grpc_timeout_seconds_to_deadline(n);
Craig Tiller434a36b2015-05-04 09:49:22 -070051}
52
Chris Evansed2a5472017-03-27 17:34:51 -050053static gpr_timespec five_seconds_from_now(void) {
54 return n_seconds_from_now(5);
55}
Craig Tiller434a36b2015-05-04 09:49:22 -070056
Craig Tillerbaa14a92017-11-03 09:09:36 -070057static void drain_cq(grpc_completion_queue* cq) {
Craig Tiller667ca402015-05-05 08:47:12 -070058 grpc_event ev;
Craig Tiller434a36b2015-05-04 09:49:22 -070059 do {
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080060 ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr);
Craig Tiller667ca402015-05-05 08:47:12 -070061 } while (ev.type != GRPC_QUEUE_SHUTDOWN);
Craig Tiller434a36b2015-05-04 09:49:22 -070062}
63
Craig Tillerbaa14a92017-11-03 09:09:36 -070064static void shutdown_server(grpc_end2end_test_fixture* f) {
Craig Tiller434a36b2015-05-04 09:49:22 -070065 if (!f->server) return;
Sree Kuchibhotla321881d2017-02-27 11:25:28 -080066 grpc_server_shutdown_and_notify(f->server, f->shutdown_cq, tag(1000));
67 GPR_ASSERT(grpc_completion_queue_pluck(f->shutdown_cq, tag(1000),
68 grpc_timeout_seconds_to_deadline(5),
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080069 nullptr)
Craig Tillerf40df232016-03-25 13:38:14 -070070 .type == GRPC_OP_COMPLETE);
Craig Tiller434a36b2015-05-04 09:49:22 -070071 grpc_server_destroy(f->server);
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080072 f->server = nullptr;
Craig Tiller434a36b2015-05-04 09:49:22 -070073}
74
Craig Tillerbaa14a92017-11-03 09:09:36 -070075static void shutdown_client(grpc_end2end_test_fixture* f) {
Craig Tiller434a36b2015-05-04 09:49:22 -070076 if (!f->client) return;
77 grpc_channel_destroy(f->client);
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080078 f->client = nullptr;
Craig Tiller434a36b2015-05-04 09:49:22 -070079}
80
Craig Tillerbaa14a92017-11-03 09:09:36 -070081static void end_test(grpc_end2end_test_fixture* f) {
Craig Tiller434a36b2015-05-04 09:49:22 -070082 shutdown_server(f);
83 shutdown_client(f);
84
Craig Tillerbc0ec332015-05-11 12:11:32 -070085 grpc_completion_queue_shutdown(f->cq);
86 drain_cq(f->cq);
87 grpc_completion_queue_destroy(f->cq);
Sree Kuchibhotla321881d2017-02-27 11:25:28 -080088 grpc_completion_queue_destroy(f->shutdown_cq);
Craig Tiller434a36b2015-05-04 09:49:22 -070089}
90
Muxi Yan56456c32016-10-30 23:11:11 -070091static void simple_request_body(grpc_end2end_test_config config,
92 grpc_end2end_test_fixture f) {
Craig Tillerbaa14a92017-11-03 09:09:36 -070093 grpc_call* c;
94 grpc_call* s;
95 cq_verifier* cqv = cq_verifier_create(f.cq);
Craig Tiller434a36b2015-05-04 09:49:22 -070096 grpc_op ops[6];
Craig Tillerbaa14a92017-11-03 09:09:36 -070097 grpc_op* op;
Craig Tiller434a36b2015-05-04 09:49:22 -070098 grpc_metadata_array initial_metadata_recv;
99 grpc_metadata_array trailing_metadata_recv;
100 grpc_metadata_array request_metadata_recv;
101 grpc_call_details call_details;
102 grpc_status_code status;
Nicolas "Pixel" Noble9a123df2015-07-29 23:45:08 +0200103 grpc_call_error error;
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800104 grpc_slice details;
Craig Tiller434a36b2015-05-04 09:49:22 -0700105 int was_cancelled = 2;
106
Chris Evansed2a5472017-03-27 17:34:51 -0500107 gpr_timespec deadline = five_seconds_from_now();
Muxi Yan5ebd3272016-10-31 07:27:07 -0700108 c = grpc_channel_create_call(
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800109 f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800110 grpc_slice_from_static_string("/foo"),
111 get_host_override_slice("foo.test.google.fr:1234", config), deadline,
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800112 nullptr);
Craig Tiller434a36b2015-05-04 09:49:22 -0700113 GPR_ASSERT(c);
114
115 grpc_metadata_array_init(&initial_metadata_recv);
116 grpc_metadata_array_init(&trailing_metadata_recv);
117 grpc_metadata_array_init(&request_metadata_recv);
118 grpc_call_details_init(&call_details);
119
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700120 memset(ops, 0, sizeof(ops));
Craig Tiller434a36b2015-05-04 09:49:22 -0700121 op = ops;
122 op->op = GRPC_OP_SEND_INITIAL_METADATA;
123 op->data.send_initial_metadata.count = 0;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700124 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800125 op->reserved = nullptr;
Craig Tiller434a36b2015-05-04 09:49:22 -0700126 op++;
127 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700128 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800129 op->reserved = nullptr;
Craig Tiller434a36b2015-05-04 09:49:22 -0700130 op++;
131 op->op = GRPC_OP_RECV_INITIAL_METADATA;
Mark D. Roth435f9f22017-01-25 12:53:54 -0800132 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700133 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800134 op->reserved = nullptr;
Craig Tiller434a36b2015-05-04 09:49:22 -0700135 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;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700140 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800141 op->reserved = nullptr;
Craig Tiller434a36b2015-05-04 09:49:22 -0700142 op++;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800143 error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), nullptr);
Nicolas "Pixel" Noble9a123df2015-07-29 23:45:08 +0200144 GPR_ASSERT(GRPC_CALL_OK == error);
Craig Tiller434a36b2015-05-04 09:49:22 -0700145
Craig Tillerd6c98df2015-08-18 09:33:44 -0700146 error =
147 grpc_server_request_call(f.server, &s, &call_details,
148 &request_metadata_recv, f.cq, f.cq, tag(101));
Nicolas "Pixel" Noble9a123df2015-07-29 23:45:08 +0200149 GPR_ASSERT(GRPC_CALL_OK == error);
Mark D. Roth7187ab92016-08-24 13:49:22 -0700150 CQ_EXPECT_COMPLETION(cqv, tag(101), 1);
Craig Tillerbc0ec332015-05-11 12:11:32 -0700151 cq_verify(cqv);
Craig Tiller434a36b2015-05-04 09:49:22 -0700152
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700153 memset(ops, 0, sizeof(ops));
Craig Tiller434a36b2015-05-04 09:49:22 -0700154 op = ops;
155 op->op = GRPC_OP_SEND_INITIAL_METADATA;
156 op->data.send_initial_metadata.count = 0;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700157 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800158 op->reserved = nullptr;
Craig Tiller434a36b2015-05-04 09:49:22 -0700159 op++;
160 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
161 op->data.send_status_from_server.trailing_metadata_count = 0;
162 op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED;
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800163 grpc_slice status_details = grpc_slice_from_static_string("xyz");
164 op->data.send_status_from_server.status_details = &status_details;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700165 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800166 op->reserved = nullptr;
Craig Tiller434a36b2015-05-04 09:49:22 -0700167 op++;
168 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
169 op->data.recv_close_on_server.cancelled = &was_cancelled;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700170 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800171 op->reserved = nullptr;
Craig Tiller434a36b2015-05-04 09:49:22 -0700172 op++;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800173 error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), nullptr);
Nicolas "Pixel" Noble9a123df2015-07-29 23:45:08 +0200174 GPR_ASSERT(GRPC_CALL_OK == error);
Craig Tiller434a36b2015-05-04 09:49:22 -0700175
Mark D. Roth7187ab92016-08-24 13:49:22 -0700176 CQ_EXPECT_COMPLETION(cqv, tag(102), 1);
177 CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
Craig Tillerbc0ec332015-05-11 12:11:32 -0700178 cq_verify(cqv);
Craig Tiller434a36b2015-05-04 09:49:22 -0700179
180 GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED);
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800181 GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz"));
182 GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo"));
Muxi Yan5ebd3272016-10-31 07:27:07 -0700183 validate_host_override_string("foo.test.google.fr:1234", call_details.host,
184 config);
Craig Tilleraea081f2015-06-11 14:19:33 -0700185 GPR_ASSERT(was_cancelled == 1);
Craig Tiller434a36b2015-05-04 09:49:22 -0700186
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800187 grpc_slice_unref(details);
Craig Tiller434a36b2015-05-04 09:49:22 -0700188 grpc_metadata_array_destroy(&initial_metadata_recv);
189 grpc_metadata_array_destroy(&trailing_metadata_recv);
190 grpc_metadata_array_destroy(&request_metadata_recv);
191 grpc_call_details_destroy(&call_details);
192
Craig Tillerdd36b152017-03-31 08:27:28 -0700193 grpc_call_unref(c);
194 grpc_call_unref(s);
Craig Tiller434a36b2015-05-04 09:49:22 -0700195
Craig Tillerfbf03c12016-07-07 13:10:09 -0700196 /* TODO(ctiller): this rate limits the test, and it should be removed when
197 retry has been implemented; until then cross-thread chatter
198 may result in some requests needing to be cancelled due to
199 seqno exhaustion. */
200 cq_verify_empty(cqv);
201
Craig Tillerbc0ec332015-05-11 12:11:32 -0700202 cq_verifier_destroy(cqv);
Craig Tiller434a36b2015-05-04 09:49:22 -0700203}
204
Craig Tiller9a576332015-06-17 10:21:49 -0700205static void test_invoke_10_simple_requests(grpc_end2end_test_config config,
206 int initial_sequence_number) {
Craig Tiller434a36b2015-05-04 09:49:22 -0700207 int i;
208 grpc_end2end_test_fixture f;
209 grpc_arg client_arg;
210 grpc_channel_args client_args;
Craig Tillerbaa14a92017-11-03 09:09:36 -0700211 char* name;
Craig Tiller434a36b2015-05-04 09:49:22 -0700212
213 client_arg.type = GRPC_ARG_INTEGER;
Yash Tibrewal40422d52017-11-06 14:39:17 -0800214 client_arg.key = const_cast<char*>(GRPC_ARG_HTTP2_INITIAL_SEQUENCE_NUMBER);
Craig Tiller434a36b2015-05-04 09:49:22 -0700215 client_arg.value.integer = initial_sequence_number;
216
217 client_args.num_args = 1;
218 client_args.args = &client_arg;
219
Craig Tillerf62c4d52015-12-04 07:43:07 -0800220 gpr_asprintf(&name, "test_invoke_requests first_seqno=%d",
221 initial_sequence_number);
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800222 f = begin_test(config, name, &client_args, nullptr);
Craig Tiller434a36b2015-05-04 09:49:22 -0700223 for (i = 0; i < 10; i++) {
Muxi Yan56456c32016-10-30 23:11:11 -0700224 simple_request_body(config, f);
Robbie Shade55a046a2017-01-25 15:14:28 -0500225 gpr_log(GPR_INFO, "Running test: Passed simple request %d", i);
Craig Tiller434a36b2015-05-04 09:49:22 -0700226 }
227 end_test(&f);
228 config.tear_down_data(&f);
Craig Tillerf62c4d52015-12-04 07:43:07 -0800229 gpr_free(name);
Craig Tiller434a36b2015-05-04 09:49:22 -0700230}
231
Craig Tiller0fe5ee72015-12-22 12:50:36 -0800232void high_initial_seqno(grpc_end2end_test_config config) {
Craig Tiller434a36b2015-05-04 09:49:22 -0700233 test_invoke_10_simple_requests(config, 16777213);
Craig Tillercfda2bd2015-05-04 10:18:44 -0700234 if (config.feature_mask & FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION) {
235 test_invoke_10_simple_requests(config, 2147483645);
236 }
Craig Tiller434a36b2015-05-04 09:49:22 -0700237}
Craig Tiller9e9edbc2016-04-04 10:38:49 -0700238
239void high_initial_seqno_pre_init(void) {}