blob: c0771b8bd206e23ba85c5d2e7082a85e4a2d740c [file] [log] [blame]
Yuchen Zeng990d9fe2017-02-26 16:54:18 -08001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2017 gRPC authors.
Yuchen Zeng990d9fe2017-02-26 16:54:18 -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
Yuchen Zeng990d9fe2017-02-26 16:54:18 -08008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Yuchen Zeng990d9fe2017-02-26 16:54:18 -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.
Yuchen Zeng990d9fe2017-02-26 16:54:18 -080016 *
17 */
18
19#include "test/core/end2end/end2end_tests.h"
20
21#include <stdio.h>
22#include <string.h>
23
24#include <grpc/byte_buffer.h>
25#include <grpc/support/alloc.h>
26#include <grpc/support/log.h>
27#include <grpc/support/time.h>
Vijay Paid4d0a302018-01-25 13:24:03 -080028
Yuchen Zeng3d43da72017-03-27 11:33:21 -070029#include "src/core/ext/transport/chttp2/transport/frame_ping.h"
Yuchen Zeng990d9fe2017-02-26 16:54:18 -080030#include "src/core/lib/channel/channel_args.h"
Mark D. Rothdbdf4952018-01-18 11:21:12 -080031#include "src/core/lib/gpr/env.h"
Vijay Paid4d0a302018-01-25 13:24:03 -080032#include "src/core/lib/gpr/useful.h"
Yuchen Zeng990d9fe2017-02-26 16:54:18 -080033#include "src/core/lib/iomgr/exec_ctx.h"
Yuchen Zeng990d9fe2017-02-26 16:54:18 -080034#include "test/core/end2end/cq_verifier.h"
35
Craig Tillerbaa14a92017-11-03 09:09:36 -070036static void* tag(intptr_t t) { return (void*)t; }
Yuchen Zeng990d9fe2017-02-26 16:54:18 -080037
38static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
Craig Tillerbaa14a92017-11-03 09:09:36 -070039 const char* test_name,
40 grpc_channel_args* client_args,
41 grpc_channel_args* server_args) {
Yuchen Zeng990d9fe2017-02-26 16:54:18 -080042 grpc_end2end_test_fixture f;
43 gpr_log(GPR_INFO, "%s/%s", test_name, config.name);
44 f = config.create_fixture(client_args, server_args);
45 config.init_server(&f, server_args);
46 config.init_client(&f, client_args);
47 return f;
48}
49
Chris Evansed2a5472017-03-27 17:34:51 -050050static gpr_timespec n_seconds_from_now(int n) {
Yuchen Zengefdf5a32017-02-26 23:29:54 -080051 return grpc_timeout_seconds_to_deadline(n);
Yuchen Zeng990d9fe2017-02-26 16:54:18 -080052}
53
Chris Evansed2a5472017-03-27 17:34:51 -050054static gpr_timespec five_seconds_from_now(void) {
55 return n_seconds_from_now(5);
56}
Yuchen Zeng990d9fe2017-02-26 16:54:18 -080057
Craig Tillerbaa14a92017-11-03 09:09:36 -070058static void drain_cq(grpc_completion_queue* cq) {
Yuchen Zeng990d9fe2017-02-26 16:54:18 -080059 grpc_event ev;
60 do {
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080061 ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr);
Yuchen Zeng990d9fe2017-02-26 16:54:18 -080062 } while (ev.type != GRPC_QUEUE_SHUTDOWN);
63}
64
Craig Tillerbaa14a92017-11-03 09:09:36 -070065static void shutdown_server(grpc_end2end_test_fixture* f) {
Yuchen Zeng990d9fe2017-02-26 16:54:18 -080066 if (!f->server) return;
Sree Kuchibhotlaa6ff1032017-04-06 13:09:17 -070067
Sree Kuchibhotla6b45d012017-03-02 16:37:59 -080068 grpc_server_shutdown_and_notify(f->server, f->shutdown_cq, tag(1000));
69 GPR_ASSERT(grpc_completion_queue_pluck(f->shutdown_cq, tag(1000),
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080070 five_seconds_from_now(), nullptr)
Chris Evansed2a5472017-03-27 17:34:51 -050071 .type == GRPC_OP_COMPLETE);
Yuchen Zeng990d9fe2017-02-26 16:54:18 -080072 grpc_server_destroy(f->server);
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080073 f->server = nullptr;
Yuchen Zeng990d9fe2017-02-26 16:54:18 -080074}
75
Craig Tillerbaa14a92017-11-03 09:09:36 -070076static void shutdown_client(grpc_end2end_test_fixture* f) {
Yuchen Zeng990d9fe2017-02-26 16:54:18 -080077 if (!f->client) return;
78 grpc_channel_destroy(f->client);
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080079 f->client = nullptr;
Yuchen Zeng990d9fe2017-02-26 16:54:18 -080080}
81
Craig Tillerbaa14a92017-11-03 09:09:36 -070082static void end_test(grpc_end2end_test_fixture* f) {
Yuchen Zeng990d9fe2017-02-26 16:54:18 -080083 shutdown_server(f);
84 shutdown_client(f);
85
86 grpc_completion_queue_shutdown(f->cq);
87 drain_cq(f->cq);
88 grpc_completion_queue_destroy(f->cq);
Sree Kuchibhotla6b45d012017-03-02 16:37:59 -080089 grpc_completion_queue_destroy(f->shutdown_cq);
Yuchen Zeng990d9fe2017-02-26 16:54:18 -080090}
91
92/* Client sends a request, server replies with a payload, then waits for the
93 keepalive watchdog timeouts before returning status. */
94static void test_keepalive_timeout(grpc_end2end_test_config config) {
Craig Tillerbaa14a92017-11-03 09:09:36 -070095 grpc_call* c;
96 grpc_call* s;
Yuchen Zeng990d9fe2017-02-26 16:54:18 -080097 grpc_slice response_payload_slice =
98 grpc_slice_from_copied_string("hello world");
Craig Tillerbaa14a92017-11-03 09:09:36 -070099 grpc_byte_buffer* response_payload =
Yuchen Zeng990d9fe2017-02-26 16:54:18 -0800100 grpc_raw_byte_buffer_create(&response_payload_slice, 1);
Yuchen Zeng990d9fe2017-02-26 16:54:18 -0800101
Yash Tibrewal34a57d02017-10-23 15:33:21 -0700102 grpc_arg keepalive_arg_elems[3];
103 keepalive_arg_elems[0].type = GRPC_ARG_INTEGER;
Yash Tibrewal40422d52017-11-06 14:39:17 -0800104 keepalive_arg_elems[0].key = const_cast<char*>(GRPC_ARG_KEEPALIVE_TIME_MS);
Muxi Yan236bec52018-03-21 15:45:21 -0700105 keepalive_arg_elems[0].value.integer = 3500;
Yash Tibrewal34a57d02017-10-23 15:33:21 -0700106 keepalive_arg_elems[1].type = GRPC_ARG_INTEGER;
Yash Tibrewal40422d52017-11-06 14:39:17 -0800107 keepalive_arg_elems[1].key = const_cast<char*>(GRPC_ARG_KEEPALIVE_TIMEOUT_MS);
Yash Tibrewal34a57d02017-10-23 15:33:21 -0700108 keepalive_arg_elems[1].value.integer = 0;
109 keepalive_arg_elems[2].type = GRPC_ARG_INTEGER;
Yash Tibrewal40422d52017-11-06 14:39:17 -0800110 keepalive_arg_elems[2].key = const_cast<char*>(GRPC_ARG_HTTP2_BDP_PROBE);
Yash Tibrewal34a57d02017-10-23 15:33:21 -0700111 keepalive_arg_elems[2].value.integer = 0;
112 grpc_channel_args keepalive_args = {GPR_ARRAY_SIZE(keepalive_arg_elems),
113 keepalive_arg_elems};
Yuchen Zeng990d9fe2017-02-26 16:54:18 -0800114
115 grpc_end2end_test_fixture f =
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800116 begin_test(config, "keepalive_timeout", &keepalive_args, nullptr);
Craig Tillerbaa14a92017-11-03 09:09:36 -0700117 cq_verifier* cqv = cq_verifier_create(f.cq);
Yuchen Zeng990d9fe2017-02-26 16:54:18 -0800118 grpc_op ops[6];
Craig Tillerbaa14a92017-11-03 09:09:36 -0700119 grpc_op* op;
Yuchen Zeng990d9fe2017-02-26 16:54:18 -0800120 grpc_metadata_array initial_metadata_recv;
121 grpc_metadata_array trailing_metadata_recv;
122 grpc_metadata_array request_metadata_recv;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800123 grpc_byte_buffer* response_payload_recv = nullptr;
Yuchen Zeng990d9fe2017-02-26 16:54:18 -0800124 grpc_call_details call_details;
125 grpc_status_code status;
126 grpc_call_error error;
127 grpc_slice details;
128
Yuchen Zeng3d43da72017-03-27 11:33:21 -0700129 /* Disable ping ack to trigger the keepalive timeout */
130 grpc_set_disable_ping_ack(true);
131
Chris Evansed2a5472017-03-27 17:34:51 -0500132 gpr_timespec deadline = five_seconds_from_now();
Yuchen Zeng990d9fe2017-02-26 16:54:18 -0800133 c = grpc_channel_create_call(
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800134 f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
Yuchen Zeng990d9fe2017-02-26 16:54:18 -0800135 grpc_slice_from_static_string("/foo"),
136 get_host_override_slice("foo.test.google.fr:1234", config), deadline,
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800137 nullptr);
Yuchen Zeng990d9fe2017-02-26 16:54:18 -0800138 GPR_ASSERT(c);
139
140 grpc_metadata_array_init(&initial_metadata_recv);
141 grpc_metadata_array_init(&trailing_metadata_recv);
142 grpc_metadata_array_init(&request_metadata_recv);
143 grpc_call_details_init(&call_details);
144
145 memset(ops, 0, sizeof(ops));
146 op = ops;
147 op->op = GRPC_OP_SEND_INITIAL_METADATA;
148 op->data.send_initial_metadata.count = 0;
149 op++;
150 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
151 op++;
152 op->op = GRPC_OP_RECV_INITIAL_METADATA;
153 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
154 op++;
155 op->op = GRPC_OP_RECV_MESSAGE;
156 op->data.recv_message.recv_message = &response_payload_recv;
157 op++;
Noah Eisen4d20a662018-02-09 09:34:04 -0800158 error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
159 nullptr);
Yuchen Zeng990d9fe2017-02-26 16:54:18 -0800160 GPR_ASSERT(GRPC_CALL_OK == error);
161
162 GPR_ASSERT(GRPC_CALL_OK == grpc_server_request_call(
163 f.server, &s, &call_details,
164 &request_metadata_recv, f.cq, f.cq, tag(101)));
165 CQ_EXPECT_COMPLETION(cqv, tag(101), 1);
166 cq_verify(cqv);
167
168 memset(ops, 0, sizeof(ops));
169 op = ops;
170 op->op = GRPC_OP_SEND_INITIAL_METADATA;
171 op->data.send_initial_metadata.count = 0;
172 op++;
173 op->op = GRPC_OP_SEND_MESSAGE;
174 op->data.send_message.send_message = response_payload;
175 op++;
Noah Eisen4d20a662018-02-09 09:34:04 -0800176 error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(102),
177 nullptr);
Yuchen Zeng990d9fe2017-02-26 16:54:18 -0800178 GPR_ASSERT(GRPC_CALL_OK == error);
179
180 CQ_EXPECT_COMPLETION(cqv, tag(102), 1);
Yuchen Zeng990d9fe2017-02-26 16:54:18 -0800181 CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
182 cq_verify(cqv);
183
184 memset(ops, 0, sizeof(ops));
185 op = ops;
186 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
187 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
188 op->data.recv_status_on_client.status = &status;
189 op->data.recv_status_on_client.status_details = &details;
190 op++;
Noah Eisen4d20a662018-02-09 09:34:04 -0800191 error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(3),
192 nullptr);
Yuchen Zeng990d9fe2017-02-26 16:54:18 -0800193 GPR_ASSERT(GRPC_CALL_OK == error);
194
195 CQ_EXPECT_COMPLETION(cqv, tag(3), 1);
196 cq_verify(cqv);
197
Craig Tillerbaa14a92017-11-03 09:09:36 -0700198 char* details_str = grpc_slice_to_c_string(details);
199 char* method_str = grpc_slice_to_c_string(call_details.method);
Craig Tiller85516af2017-10-11 18:16:21 -0700200 GPR_ASSERT(status == GRPC_STATUS_INTERNAL);
Yuchen Zeng990d9fe2017-02-26 16:54:18 -0800201 GPR_ASSERT(0 == grpc_slice_str_cmp(details, "keepalive watchdog timeout"));
202 GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo"));
203 validate_host_override_string("foo.test.google.fr:1234", call_details.host,
204 config);
205
206 gpr_free(details_str);
207 gpr_free(method_str);
208
209 grpc_slice_unref(details);
210 grpc_metadata_array_destroy(&initial_metadata_recv);
211 grpc_metadata_array_destroy(&trailing_metadata_recv);
212 grpc_metadata_array_destroy(&request_metadata_recv);
213 grpc_call_details_destroy(&call_details);
214
Craig Tillerdd36b152017-03-31 08:27:28 -0700215 grpc_call_unref(c);
216 grpc_call_unref(s);
Yuchen Zeng990d9fe2017-02-26 16:54:18 -0800217
218 cq_verifier_destroy(cqv);
219
220 grpc_byte_buffer_destroy(response_payload);
221 grpc_byte_buffer_destroy(response_payload_recv);
222
Yuchen Zeng990d9fe2017-02-26 16:54:18 -0800223 end_test(&f);
224 config.tear_down_data(&f);
225}
226
227void keepalive_timeout(grpc_end2end_test_config config) {
228 test_keepalive_timeout(config);
229}
230
231void keepalive_timeout_pre_init(void) {}