blob: 0ad224f5793a7629233a2b16ddaa2a15de680d47 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015 gRPC authors.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 *
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
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080010 *
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.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080016 *
17 */
18
19#include "test/core/end2end/end2end_tests.h"
20
21#include <stdio.h>
22#include <string.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080023
24#include <grpc/byte_buffer.h>
25#include <grpc/support/alloc.h>
26#include <grpc/support/log.h>
27#include <grpc/support/time.h>
28#include <grpc/support/useful.h>
29#include "test/core/end2end/cq_verifier.h"
30
Craig Tillerbaa14a92017-11-03 09:09:36 -070031static void* tag(intptr_t t) { return (void*)t; }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080032
Chris Evansed2a5472017-03-27 17:34:51 -050033static gpr_timespec n_seconds_from_now(int n) {
Robbie Shadeca7effc2017-01-17 09:14:29 -050034 return grpc_timeout_seconds_to_deadline(n);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080035}
36
Chris Evansed2a5472017-03-27 17:34:51 -050037static gpr_timespec five_seconds_from_now(void) {
38 return n_seconds_from_now(5);
39}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080040
Craig Tillerbaa14a92017-11-03 09:09:36 -070041static void drain_cq(grpc_completion_queue* cq) {
Craig Tiller64be9f72015-05-04 14:53:51 -070042 grpc_event ev;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080043 do {
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080044 ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr);
Craig Tiller64be9f72015-05-04 14:53:51 -070045 } while (ev.type != GRPC_QUEUE_SHUTDOWN);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080046}
47
Craig Tillerbaa14a92017-11-03 09:09:36 -070048static void shutdown_server(grpc_end2end_test_fixture* f) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080049 if (!f->server) return;
Sree Kuchibhotla321881d2017-02-27 11:25:28 -080050 grpc_server_shutdown_and_notify(f->server, f->shutdown_cq, tag(1000));
51 GPR_ASSERT(grpc_completion_queue_pluck(f->shutdown_cq, tag(1000),
52 grpc_timeout_seconds_to_deadline(5),
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080053 nullptr)
Craig Tillerf40df232016-03-25 13:38:14 -070054 .type == GRPC_OP_COMPLETE);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080055 grpc_server_destroy(f->server);
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080056 f->server = nullptr;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080057}
58
Craig Tillerbaa14a92017-11-03 09:09:36 -070059static void shutdown_client(grpc_end2end_test_fixture* f) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080060 if (!f->client) return;
61 grpc_channel_destroy(f->client);
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080062 f->client = nullptr;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080063}
64
Craig Tillerbaa14a92017-11-03 09:09:36 -070065static void end_test(grpc_end2end_test_fixture* f) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080066 shutdown_server(f);
67 shutdown_client(f);
68
Craig Tillerbc0ec332015-05-11 12:11:32 -070069 grpc_completion_queue_shutdown(f->cq);
70 drain_cq(f->cq);
71 grpc_completion_queue_destroy(f->cq);
Sree Kuchibhotla321881d2017-02-27 11:25:28 -080072 grpc_completion_queue_destroy(f->shutdown_cq);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080073}
74
75static void simple_delayed_request_body(grpc_end2end_test_config config,
Craig Tillerbaa14a92017-11-03 09:09:36 -070076 grpc_end2end_test_fixture* f,
77 grpc_channel_args* client_args,
78 grpc_channel_args* server_args,
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080079 long delay_us) {
Craig Tillerbaa14a92017-11-03 09:09:36 -070080 grpc_call* c;
81 grpc_call* s;
82 cq_verifier* cqv = cq_verifier_create(f->cq);
Craig Tillerc58330a2015-02-05 13:42:46 -080083 grpc_op ops[6];
Craig Tillerbaa14a92017-11-03 09:09:36 -070084 grpc_op* op;
Craig Tillerc58330a2015-02-05 13:42:46 -080085 grpc_metadata_array initial_metadata_recv;
86 grpc_metadata_array trailing_metadata_recv;
87 grpc_metadata_array request_metadata_recv;
88 grpc_call_details call_details;
89 grpc_status_code status;
Nicolas "Pixel" Noble9a123df2015-07-29 23:45:08 +020090 grpc_call_error error;
Craig Tiller7c70b6c2017-01-23 07:48:42 -080091 grpc_slice details;
Craig Tillerc58330a2015-02-05 13:42:46 -080092 int was_cancelled = 2;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080093
Mark D. Rothe127a392016-10-27 08:27:15 -070094 config.init_client(f, client_args);
Muxi Yan938f0992017-02-06 00:04:27 -080095 config.init_server(f, server_args);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080096
Chris Evansed2a5472017-03-27 17:34:51 -050097 gpr_timespec deadline = five_seconds_from_now();
Muxi Yan5ebd3272016-10-31 07:27:07 -070098 c = grpc_channel_create_call(
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080099 f->client, nullptr, GRPC_PROPAGATE_DEFAULTS, f->cq,
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800100 grpc_slice_from_static_string("/foo"),
101 get_host_override_slice("foo.test.google.fr:1234", config), deadline,
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800102 nullptr);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800103 GPR_ASSERT(c);
104
Craig Tillerc58330a2015-02-05 13:42:46 -0800105 grpc_metadata_array_init(&initial_metadata_recv);
106 grpc_metadata_array_init(&trailing_metadata_recv);
107 grpc_metadata_array_init(&request_metadata_recv);
108 grpc_call_details_init(&call_details);
109
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700110 memset(ops, 0, sizeof(ops));
Craig Tillerc58330a2015-02-05 13:42:46 -0800111 op = ops;
112 op->op = GRPC_OP_SEND_INITIAL_METADATA;
113 op->data.send_initial_metadata.count = 0;
Mark D. Roth8bc8a832016-10-06 08:11:29 -0700114 op->flags = GRPC_INITIAL_METADATA_WAIT_FOR_READY;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800115 op->reserved = nullptr;
Craig Tillerc58330a2015-02-05 13:42:46 -0800116 op++;
117 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700118 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800119 op->reserved = nullptr;
Craig Tillerc58330a2015-02-05 13:42:46 -0800120 op++;
121 op->op = GRPC_OP_RECV_INITIAL_METADATA;
Mark D. Roth435f9f22017-01-25 12:53:54 -0800122 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700123 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800124 op->reserved = nullptr;
Craig Tillerc58330a2015-02-05 13:42:46 -0800125 op++;
126 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
127 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
128 op->data.recv_status_on_client.status = &status;
129 op->data.recv_status_on_client.status_details = &details;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700130 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800131 op->reserved = nullptr;
Craig Tillerc58330a2015-02-05 13:42:46 -0800132 op++;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800133 error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), nullptr);
Nicolas "Pixel" Noble9a123df2015-07-29 23:45:08 +0200134 GPR_ASSERT(GRPC_CALL_OK == error);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800135
Craig Tillerd6c98df2015-08-18 09:33:44 -0700136 error =
137 grpc_server_request_call(f->server, &s, &call_details,
138 &request_metadata_recv, f->cq, f->cq, tag(101));
Nicolas "Pixel" Noble9a123df2015-07-29 23:45:08 +0200139 GPR_ASSERT(GRPC_CALL_OK == error);
Mark D. Roth7187ab92016-08-24 13:49:22 -0700140 CQ_EXPECT_COMPLETION(cqv, tag(101), 1);
Craig Tillerbc0ec332015-05-11 12:11:32 -0700141 cq_verify(cqv);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800142
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700143 memset(ops, 0, sizeof(ops));
Craig Tillerc58330a2015-02-05 13:42:46 -0800144 op = ops;
145 op->op = GRPC_OP_SEND_INITIAL_METADATA;
146 op->data.send_initial_metadata.count = 0;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700147 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800148 op->reserved = nullptr;
Craig Tillerc58330a2015-02-05 13:42:46 -0800149 op++;
150 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
151 op->data.send_status_from_server.trailing_metadata_count = 0;
152 op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED;
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800153 grpc_slice status_details = grpc_slice_from_static_string("xyz");
154 op->data.send_status_from_server.status_details = &status_details;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700155 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800156 op->reserved = nullptr;
Craig Tillerc58330a2015-02-05 13:42:46 -0800157 op++;
158 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
159 op->data.recv_close_on_server.cancelled = &was_cancelled;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700160 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800161 op->reserved = nullptr;
Craig Tillerc58330a2015-02-05 13:42:46 -0800162 op++;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800163 error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), nullptr);
Nicolas "Pixel" Noble9a123df2015-07-29 23:45:08 +0200164 GPR_ASSERT(GRPC_CALL_OK == error);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800165
Mark D. Roth7187ab92016-08-24 13:49:22 -0700166 CQ_EXPECT_COMPLETION(cqv, tag(102), 1);
167 CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
Craig Tillerbc0ec332015-05-11 12:11:32 -0700168 cq_verify(cqv);
Craig Tillerc58330a2015-02-05 13:42:46 -0800169
170 GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED);
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800171 GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz"));
172 GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo"));
Muxi Yan5ebd3272016-10-31 07:27:07 -0700173 validate_host_override_string("foo.test.google.fr:1234", call_details.host,
174 config);
Craig Tilleraea081f2015-06-11 14:19:33 -0700175 GPR_ASSERT(was_cancelled == 1);
Craig Tillerc58330a2015-02-05 13:42:46 -0800176
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800177 grpc_slice_unref(details);
Craig Tillerc58330a2015-02-05 13:42:46 -0800178 grpc_metadata_array_destroy(&initial_metadata_recv);
179 grpc_metadata_array_destroy(&trailing_metadata_recv);
180 grpc_metadata_array_destroy(&request_metadata_recv);
181 grpc_call_details_destroy(&call_details);
182
Craig Tillerdd36b152017-03-31 08:27:28 -0700183 grpc_call_unref(c);
184 grpc_call_unref(s);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800185
Craig Tillerbc0ec332015-05-11 12:11:32 -0700186 cq_verifier_destroy(cqv);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800187}
188
189static void test_simple_delayed_request_short(grpc_end2end_test_config config) {
190 grpc_end2end_test_fixture f;
David Garcia Quintas1edfb952016-11-22 17:15:34 -0800191 grpc_channel_args client_args;
192 grpc_arg arg_array[1];
193 arg_array[0].type = GRPC_ARG_INTEGER;
Yash Tibrewal34a57d02017-10-23 15:33:21 -0700194 arg_array[0].key =
Yash Tibrewal40422d52017-11-06 14:39:17 -0800195 const_cast<char*>("grpc.testing.fixed_reconnect_backoff_ms");
David Garcia Quintas1edfb952016-11-22 17:15:34 -0800196 arg_array[0].value.integer = 1000;
197 client_args.args = arg_array;
198 client_args.num_args = 1;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800199
Robbie Shade55a046a2017-01-25 15:14:28 -0500200 gpr_log(GPR_INFO, "Running test: %s/%s", "test_simple_delayed_request_short",
201 config.name);
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800202 f = config.create_fixture(nullptr, nullptr);
David Garcia Quintas1edfb952016-11-22 17:15:34 -0800203
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800204 simple_delayed_request_body(config, &f, &client_args, nullptr, 100000);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800205 end_test(&f);
206 config.tear_down_data(&f);
207}
208
209static void test_simple_delayed_request_long(grpc_end2end_test_config config) {
210 grpc_end2end_test_fixture f;
David Garcia Quintas1edfb952016-11-22 17:15:34 -0800211 grpc_channel_args client_args;
212 grpc_arg arg_array[1];
213 arg_array[0].type = GRPC_ARG_INTEGER;
Yash Tibrewal34a57d02017-10-23 15:33:21 -0700214 arg_array[0].key =
Yash Tibrewal40422d52017-11-06 14:39:17 -0800215 const_cast<char*>("grpc.testing.fixed_reconnect_backoff_ms");
David Garcia Quintas1edfb952016-11-22 17:15:34 -0800216 arg_array[0].value.integer = 1000;
217 client_args.args = arg_array;
218 client_args.num_args = 1;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800219
Robbie Shade55a046a2017-01-25 15:14:28 -0500220 gpr_log(GPR_INFO, "Running test: %s/%s", "test_simple_delayed_request_long",
221 config.name);
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800222 f = config.create_fixture(nullptr, nullptr);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800223 /* This timeout should be longer than a single retry */
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800224 simple_delayed_request_body(config, &f, &client_args, nullptr, 1500000);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800225 end_test(&f);
226 config.tear_down_data(&f);
227}
228
Craig Tiller0fe5ee72015-12-22 12:50:36 -0800229void simple_delayed_request(grpc_end2end_test_config config) {
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700230 GPR_ASSERT(config.feature_mask & FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION);
231 test_simple_delayed_request_short(config);
232 test_simple_delayed_request_long(config);
Craig Tiller190d3602015-02-18 09:23:38 -0800233}
Craig Tiller9e9edbc2016-04-04 10:38:49 -0700234
235void simple_delayed_request_pre_init(void) {}