blob: 822565510f3d3a2f287fc36afc6bf867957ee45d [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>
28#include <grpc/support/useful.h>
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"
31#include "src/core/lib/iomgr/exec_ctx.h"
32#include "src/core/lib/support/env.h"
33#include "test/core/end2end/cq_verifier.h"
34
Craig Tillerbaa14a92017-11-03 09:09:36 -070035static void* tag(intptr_t t) { return (void*)t; }
Yuchen Zeng990d9fe2017-02-26 16:54:18 -080036
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) {
Yuchen Zeng990d9fe2017-02-26 16:54:18 -080041 grpc_end2end_test_fixture f;
42 gpr_log(GPR_INFO, "%s/%s", test_name, config.name);
43 f = config.create_fixture(client_args, server_args);
44 config.init_server(&f, server_args);
45 config.init_client(&f, client_args);
46 return f;
47}
48
Chris Evansed2a5472017-03-27 17:34:51 -050049static gpr_timespec n_seconds_from_now(int n) {
Yuchen Zengefdf5a32017-02-26 23:29:54 -080050 return grpc_timeout_seconds_to_deadline(n);
Yuchen Zeng990d9fe2017-02-26 16:54:18 -080051}
52
Chris Evansed2a5472017-03-27 17:34:51 -050053static gpr_timespec five_seconds_from_now(void) {
54 return n_seconds_from_now(5);
55}
Yuchen Zeng990d9fe2017-02-26 16:54:18 -080056
Craig Tillerbaa14a92017-11-03 09:09:36 -070057static void drain_cq(grpc_completion_queue* cq) {
Yuchen Zeng990d9fe2017-02-26 16:54:18 -080058 grpc_event ev;
59 do {
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080060 ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr);
Yuchen Zeng990d9fe2017-02-26 16:54:18 -080061 } while (ev.type != GRPC_QUEUE_SHUTDOWN);
62}
63
Craig Tillerbaa14a92017-11-03 09:09:36 -070064static void shutdown_server(grpc_end2end_test_fixture* f) {
Yuchen Zeng990d9fe2017-02-26 16:54:18 -080065 if (!f->server) return;
Sree Kuchibhotlaa6ff1032017-04-06 13:09:17 -070066
Sree Kuchibhotla6b45d012017-03-02 16:37:59 -080067 grpc_server_shutdown_and_notify(f->server, f->shutdown_cq, tag(1000));
68 GPR_ASSERT(grpc_completion_queue_pluck(f->shutdown_cq, tag(1000),
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080069 five_seconds_from_now(), nullptr)
Chris Evansed2a5472017-03-27 17:34:51 -050070 .type == GRPC_OP_COMPLETE);
Yuchen Zeng990d9fe2017-02-26 16:54:18 -080071 grpc_server_destroy(f->server);
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080072 f->server = nullptr;
Yuchen Zeng990d9fe2017-02-26 16:54:18 -080073}
74
Craig Tillerbaa14a92017-11-03 09:09:36 -070075static void shutdown_client(grpc_end2end_test_fixture* f) {
Yuchen Zeng990d9fe2017-02-26 16:54:18 -080076 if (!f->client) return;
77 grpc_channel_destroy(f->client);
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080078 f->client = nullptr;
Yuchen Zeng990d9fe2017-02-26 16:54:18 -080079}
80
Craig Tillerbaa14a92017-11-03 09:09:36 -070081static void end_test(grpc_end2end_test_fixture* f) {
Yuchen Zeng990d9fe2017-02-26 16:54:18 -080082 shutdown_server(f);
83 shutdown_client(f);
84
85 grpc_completion_queue_shutdown(f->cq);
86 drain_cq(f->cq);
87 grpc_completion_queue_destroy(f->cq);
Sree Kuchibhotla6b45d012017-03-02 16:37:59 -080088 grpc_completion_queue_destroy(f->shutdown_cq);
Yuchen Zeng990d9fe2017-02-26 16:54:18 -080089}
90
91/* Client sends a request, server replies with a payload, then waits for the
92 keepalive watchdog timeouts before returning status. */
93static void test_keepalive_timeout(grpc_end2end_test_config config) {
Craig Tillerbaa14a92017-11-03 09:09:36 -070094 grpc_call* c;
95 grpc_call* s;
Yuchen Zeng990d9fe2017-02-26 16:54:18 -080096 grpc_slice response_payload_slice =
97 grpc_slice_from_copied_string("hello world");
Craig Tillerbaa14a92017-11-03 09:09:36 -070098 grpc_byte_buffer* response_payload =
Yuchen Zeng990d9fe2017-02-26 16:54:18 -080099 grpc_raw_byte_buffer_create(&response_payload_slice, 1);
Yuchen Zeng990d9fe2017-02-26 16:54:18 -0800100
Yash Tibrewal34a57d02017-10-23 15:33:21 -0700101 grpc_arg keepalive_arg_elems[3];
102 keepalive_arg_elems[0].type = GRPC_ARG_INTEGER;
Yash Tibrewal40422d52017-11-06 14:39:17 -0800103 keepalive_arg_elems[0].key = const_cast<char*>(GRPC_ARG_KEEPALIVE_TIME_MS);
Yash Tibrewal34a57d02017-10-23 15:33:21 -0700104 keepalive_arg_elems[0].value.integer = 1500;
105 keepalive_arg_elems[1].type = GRPC_ARG_INTEGER;
Yash Tibrewal40422d52017-11-06 14:39:17 -0800106 keepalive_arg_elems[1].key = const_cast<char*>(GRPC_ARG_KEEPALIVE_TIMEOUT_MS);
Yash Tibrewal34a57d02017-10-23 15:33:21 -0700107 keepalive_arg_elems[1].value.integer = 0;
108 keepalive_arg_elems[2].type = GRPC_ARG_INTEGER;
Yash Tibrewal40422d52017-11-06 14:39:17 -0800109 keepalive_arg_elems[2].key = const_cast<char*>(GRPC_ARG_HTTP2_BDP_PROBE);
Yash Tibrewal34a57d02017-10-23 15:33:21 -0700110 keepalive_arg_elems[2].value.integer = 0;
111 grpc_channel_args keepalive_args = {GPR_ARRAY_SIZE(keepalive_arg_elems),
112 keepalive_arg_elems};
Yuchen Zeng990d9fe2017-02-26 16:54:18 -0800113
114 grpc_end2end_test_fixture f =
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800115 begin_test(config, "keepalive_timeout", &keepalive_args, nullptr);
Craig Tillerbaa14a92017-11-03 09:09:36 -0700116 cq_verifier* cqv = cq_verifier_create(f.cq);
Yuchen Zeng990d9fe2017-02-26 16:54:18 -0800117 grpc_op ops[6];
Craig Tillerbaa14a92017-11-03 09:09:36 -0700118 grpc_op* op;
Yuchen Zeng990d9fe2017-02-26 16:54:18 -0800119 grpc_metadata_array initial_metadata_recv;
120 grpc_metadata_array trailing_metadata_recv;
121 grpc_metadata_array request_metadata_recv;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800122 grpc_byte_buffer* response_payload_recv = nullptr;
Yuchen Zeng990d9fe2017-02-26 16:54:18 -0800123 grpc_call_details call_details;
124 grpc_status_code status;
125 grpc_call_error error;
126 grpc_slice details;
127
Yuchen Zeng3d43da72017-03-27 11:33:21 -0700128 /* Disable ping ack to trigger the keepalive timeout */
129 grpc_set_disable_ping_ack(true);
130
Chris Evansed2a5472017-03-27 17:34:51 -0500131 gpr_timespec deadline = five_seconds_from_now();
Yuchen Zeng990d9fe2017-02-26 16:54:18 -0800132 c = grpc_channel_create_call(
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800133 f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
Yuchen Zeng990d9fe2017-02-26 16:54:18 -0800134 grpc_slice_from_static_string("/foo"),
135 get_host_override_slice("foo.test.google.fr:1234", config), deadline,
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800136 nullptr);
Yuchen Zeng990d9fe2017-02-26 16:54:18 -0800137 GPR_ASSERT(c);
138
139 grpc_metadata_array_init(&initial_metadata_recv);
140 grpc_metadata_array_init(&trailing_metadata_recv);
141 grpc_metadata_array_init(&request_metadata_recv);
142 grpc_call_details_init(&call_details);
143
144 memset(ops, 0, sizeof(ops));
145 op = ops;
146 op->op = GRPC_OP_SEND_INITIAL_METADATA;
147 op->data.send_initial_metadata.count = 0;
148 op++;
149 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
150 op++;
151 op->op = GRPC_OP_RECV_INITIAL_METADATA;
152 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
153 op++;
154 op->op = GRPC_OP_RECV_MESSAGE;
155 op->data.recv_message.recv_message = &response_payload_recv;
156 op++;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800157 error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), nullptr);
Yuchen Zeng990d9fe2017-02-26 16:54:18 -0800158 GPR_ASSERT(GRPC_CALL_OK == error);
159
160 GPR_ASSERT(GRPC_CALL_OK == grpc_server_request_call(
161 f.server, &s, &call_details,
162 &request_metadata_recv, f.cq, f.cq, tag(101)));
163 CQ_EXPECT_COMPLETION(cqv, tag(101), 1);
164 cq_verify(cqv);
165
166 memset(ops, 0, sizeof(ops));
167 op = ops;
168 op->op = GRPC_OP_SEND_INITIAL_METADATA;
169 op->data.send_initial_metadata.count = 0;
170 op++;
171 op->op = GRPC_OP_SEND_MESSAGE;
172 op->data.send_message.send_message = response_payload;
173 op++;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800174 error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), nullptr);
Yuchen Zeng990d9fe2017-02-26 16:54:18 -0800175 GPR_ASSERT(GRPC_CALL_OK == error);
176
177 CQ_EXPECT_COMPLETION(cqv, tag(102), 1);
Yuchen Zeng990d9fe2017-02-26 16:54:18 -0800178 CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
179 cq_verify(cqv);
180
181 memset(ops, 0, sizeof(ops));
182 op = ops;
183 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
184 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
185 op->data.recv_status_on_client.status = &status;
186 op->data.recv_status_on_client.status_details = &details;
187 op++;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800188 error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(3), nullptr);
Yuchen Zeng990d9fe2017-02-26 16:54:18 -0800189 GPR_ASSERT(GRPC_CALL_OK == error);
190
191 CQ_EXPECT_COMPLETION(cqv, tag(3), 1);
192 cq_verify(cqv);
193
Craig Tillerbaa14a92017-11-03 09:09:36 -0700194 char* details_str = grpc_slice_to_c_string(details);
195 char* method_str = grpc_slice_to_c_string(call_details.method);
Craig Tiller85516af2017-10-11 18:16:21 -0700196 GPR_ASSERT(status == GRPC_STATUS_INTERNAL);
Yuchen Zeng990d9fe2017-02-26 16:54:18 -0800197 GPR_ASSERT(0 == grpc_slice_str_cmp(details, "keepalive watchdog timeout"));
198 GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo"));
199 validate_host_override_string("foo.test.google.fr:1234", call_details.host,
200 config);
201
202 gpr_free(details_str);
203 gpr_free(method_str);
204
205 grpc_slice_unref(details);
206 grpc_metadata_array_destroy(&initial_metadata_recv);
207 grpc_metadata_array_destroy(&trailing_metadata_recv);
208 grpc_metadata_array_destroy(&request_metadata_recv);
209 grpc_call_details_destroy(&call_details);
210
Craig Tillerdd36b152017-03-31 08:27:28 -0700211 grpc_call_unref(c);
212 grpc_call_unref(s);
Yuchen Zeng990d9fe2017-02-26 16:54:18 -0800213
214 cq_verifier_destroy(cqv);
215
216 grpc_byte_buffer_destroy(response_payload);
217 grpc_byte_buffer_destroy(response_payload_recv);
218
Yuchen Zeng990d9fe2017-02-26 16:54:18 -0800219 end_test(&f);
220 config.tear_down_data(&f);
221}
222
223void keepalive_timeout(grpc_end2end_test_config config) {
224 test_keepalive_timeout(config);
225}
226
227void keepalive_timeout_pre_init(void) {}