| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2017 gRPC authors. |
| 4 |
* |
| 5 |
* 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 |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* 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. |
| 16 |
* |
| 17 |
*/ |
| 18 |
|
| 19 |
#include <stdint.h> |
| 20 |
#include <string.h> |
| 21 |
|
| 22 |
#include <grpc/byte_buffer.h> |
| 23 |
#include <grpc/grpc.h> |
| 24 |
#include <grpc/impl/codegen/propagation_bits.h> |
| 25 |
#include <grpc/slice.h> |
| 26 |
#include <grpc/status.h> |
| 27 |
#include <grpc/support/log.h> |
| 28 |
#include <grpc/support/time.h> |
| 29 |
|
| 30 |
#include "src/core/ext/transport/chttp2/transport/frame_ping.h" |
| 31 |
#include "src/core/lib/gpr/useful.h" |
| 32 |
#include "src/core/lib/gprpp/global_config_generic.h" |
| 33 |
#include "src/core/lib/gprpp/memory.h" |
| 34 |
#include "src/core/lib/iomgr/port.h" |
| 35 |
#include "test/core/end2end/cq_verifier.h" |
| 36 |
#include "test/core/end2end/end2end_tests.h" |
| 37 |
#include "test/core/util/test_config.h" |
| 38 |
|
| 39 |
#ifdef GRPC_POSIX_SOCKET |
| 40 |
#include "src/core/lib/iomgr/ev_posix.h" |
| 41 |
#endif // GRPC_POSIX_SOCKET |
| 42 |
|
| 43 |
static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); } |
| 44 |
|
| 45 |
static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, |
| 46 |
const char* test_name, |
| 47 |
grpc_channel_args* client_args, |
| 48 |
grpc_channel_args* server_args) { |
| 49 |
grpc_end2end_test_fixture f; |
| 50 |
gpr_log(GPR_INFO, "%s/%s", test_name, config.name); |
| 51 |
f = config.create_fixture(client_args, server_args); |
| 52 |
config.init_server(&f, server_args); |
| 53 |
config.init_client(&f, client_args); |
| 54 |
return f; |
| 55 |
} |
| 56 |
|
| 57 |
static gpr_timespec n_seconds_from_now(int n) { |
| 58 |
return grpc_timeout_seconds_to_deadline(n); |
| 59 |
} |
| 60 |
|
| 61 |
static gpr_timespec five_seconds_from_now(void) { |
| 62 |
return n_seconds_from_now(5); |
| 63 |
} |
| 64 |
|
| 65 |
static void drain_cq(grpc_completion_queue* cq) { |
| 66 |
grpc_event ev; |
| 67 |
do { |
| 68 |
ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr); |
| 69 |
} while (ev.type != GRPC_QUEUE_SHUTDOWN); |
| 70 |
} |
| 71 |
|
| 72 |
static void shutdown_server(grpc_end2end_test_fixture* f) { |
| 73 |
if (!f->server) return; |
| 74 |
grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000)); |
| 75 |
grpc_event ev; |
| 76 |
do { |
| 77 |
ev = grpc_completion_queue_next(f->cq, grpc_timeout_seconds_to_deadline(5), |
| 78 |
nullptr); |
| 79 |
} while (ev.type != GRPC_OP_COMPLETE || ev.tag != tag(1000)); |
| 80 |
grpc_server_destroy(f->server); |
| 81 |
f->server = nullptr; |
| 82 |
} |
| 83 |
|
| 84 |
static void shutdown_client(grpc_end2end_test_fixture* f) { |
| 85 |
if (!f->client) return; |
| 86 |
grpc_channel_destroy(f->client); |
| 87 |
f->client = nullptr; |
| 88 |
} |
| 89 |
|
| 90 |
static void end_test(grpc_end2end_test_fixture* f) { |
| 91 |
shutdown_server(f); |
| 92 |
shutdown_client(f); |
| 93 |
|
| 94 |
grpc_completion_queue_shutdown(f->cq); |
| 95 |
drain_cq(f->cq); |
| 96 |
grpc_completion_queue_destroy(f->cq); |
| 97 |
} |
| 98 |
|
| 99 |
/* Client sends a request, then waits for the keepalive watchdog timeouts before |
| 100 |
* returning status. */ |
| 101 |
static void test_keepalive_timeout(grpc_end2end_test_config config) { |
| 102 |
grpc_call* c; |
| 103 |
|
| 104 |
grpc_arg keepalive_arg_elems[3]; |
| 105 |
keepalive_arg_elems[0].type = GRPC_ARG_INTEGER; |
| 106 |
keepalive_arg_elems[0].key = const_cast<char*>(GRPC_ARG_KEEPALIVE_TIME_MS); |
| 107 |
keepalive_arg_elems[0].value.integer = 10; |
| 108 |
keepalive_arg_elems[1].type = GRPC_ARG_INTEGER; |
| 109 |
keepalive_arg_elems[1].key = const_cast<char*>(GRPC_ARG_KEEPALIVE_TIMEOUT_MS); |
| 110 |
keepalive_arg_elems[1].value.integer = 0; |
| 111 |
keepalive_arg_elems[2].type = GRPC_ARG_INTEGER; |
| 112 |
keepalive_arg_elems[2].key = const_cast<char*>(GRPC_ARG_HTTP2_BDP_PROBE); |
| 113 |
keepalive_arg_elems[2].value.integer = 0; |
| 114 |
grpc_channel_args keepalive_args = {GPR_ARRAY_SIZE(keepalive_arg_elems), |
| 115 |
keepalive_arg_elems}; |
| 116 |
|
| 117 |
grpc_end2end_test_fixture f = |
| 118 |
begin_test(config, "keepalive_timeout", &keepalive_args, nullptr); |
| 119 |
grpc_core::CqVerifier cqv(f.cq); |
| 120 |
grpc_op ops[6]; |
| 121 |
grpc_op* op; |
| 122 |
grpc_metadata_array initial_metadata_recv; |
| 123 |
grpc_metadata_array trailing_metadata_recv; |
| 124 |
grpc_status_code status; |
| 125 |
grpc_call_error error; |
| 126 |
grpc_slice details; |
| 127 |
|
| 128 |
/* Disable ping ack to trigger the keepalive timeout */ |
| 129 |
grpc_set_disable_ping_ack(true); |
| 130 |
|
| 131 |
gpr_timespec deadline = five_seconds_from_now(); |
| 132 |
c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq, |
| 133 |
grpc_slice_from_static_string("/foo"), nullptr, |
| 134 |
deadline, nullptr); |
| 135 |
GPR_ASSERT(c); |
| 136 |
|
| 137 |
grpc_metadata_array_init(&initial_metadata_recv); |
| 138 |
grpc_metadata_array_init(&trailing_metadata_recv); |
| 139 |
|
| 140 |
memset(ops, 0, sizeof(ops)); |
| 141 |
op = ops; |
| 142 |
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
| 143 |
op->data.send_initial_metadata.count = 0; |
| 144 |
op++; |
| 145 |
op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; |
| 146 |
op++; |
| 147 |
op->op = GRPC_OP_RECV_INITIAL_METADATA; |
| 148 |
op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv; |
| 149 |
op++; |
| 150 |
op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
| 151 |
op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv; |
| 152 |
op->data.recv_status_on_client.status = &status; |
| 153 |
op->data.recv_status_on_client.status_details = &details; |
| 154 |
op++; |
| 155 |
error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1), |
| 156 |
nullptr); |
| 157 |
GPR_ASSERT(GRPC_CALL_OK == error); |
| 158 |
|
| 159 |
cqv.Expect(tag(1), true); |
| 160 |
cqv.Verify(); |
| 161 |
|
| 162 |
GPR_ASSERT(status == GRPC_STATUS_UNAVAILABLE); |
| 163 |
GPR_ASSERT(0 == grpc_slice_str_cmp(details, "keepalive watchdog timeout")); |
| 164 |
|
| 165 |
grpc_slice_unref(details); |
| 166 |
grpc_metadata_array_destroy(&initial_metadata_recv); |
| 167 |
grpc_metadata_array_destroy(&trailing_metadata_recv); |
| 168 |
|
| 169 |
grpc_call_unref(c); |
| 170 |
|
| 171 |
end_test(&f); |
| 172 |
config.tear_down_data(&f); |
| 173 |
} |
| 174 |
|
| 175 |
/* Verify that reads reset the keepalive ping timer. The client sends 30 pings |
| 176 |
* with a sleep of 10ms in between. It has a configured keepalive timer of |
| 177 |
* 200ms. In the success case, each ping ack should reset the keepalive timer so |
| 178 |
* that the keepalive ping is never sent. */ |
| 179 |
static void test_read_delays_keepalive(grpc_end2end_test_config config) { |
| 180 |
#ifdef GRPC_POSIX_SOCKET |
| 181 |
grpc_core::UniquePtr<char> poller = GPR_GLOBAL_CONFIG_GET(grpc_poll_strategy); |
| 182 |
/* It is hard to get the timing right for the polling engine poll. */ |
| 183 |
if ((0 == strcmp(poller.get(), "poll"))) { |
| 184 |
return; |
| 185 |
} |
| 186 |
#endif // GRPC_POSIX_SOCKET |
| 187 |
const int kPingIntervalMS = 100; |
| 188 |
grpc_arg keepalive_arg_elems[3]; |
| 189 |
keepalive_arg_elems[0].type = GRPC_ARG_INTEGER; |
| 190 |
keepalive_arg_elems[0].key = const_cast<char*>(GRPC_ARG_KEEPALIVE_TIME_MS); |
| 191 |
keepalive_arg_elems[0].value.integer = 20 * kPingIntervalMS; |
| 192 |
keepalive_arg_elems[1].type = GRPC_ARG_INTEGER; |
| 193 |
keepalive_arg_elems[1].key = const_cast<char*>(GRPC_ARG_KEEPALIVE_TIMEOUT_MS); |
| 194 |
keepalive_arg_elems[1].value.integer = 0; |
| 195 |
keepalive_arg_elems[2].type = GRPC_ARG_INTEGER; |
| 196 |
keepalive_arg_elems[2].key = const_cast<char*>(GRPC_ARG_HTTP2_BDP_PROBE); |
| 197 |
keepalive_arg_elems[2].value.integer = 0; |
| 198 |
grpc_channel_args keepalive_args = {GPR_ARRAY_SIZE(keepalive_arg_elems), |
| 199 |
keepalive_arg_elems}; |
| 200 |
grpc_end2end_test_fixture f = begin_test(config, "test_read_delays_keepalive", |
| 201 |
&keepalive_args, nullptr); |
| 202 |
/* Disable ping ack to trigger the keepalive timeout */ |
| 203 |
grpc_set_disable_ping_ack(true); |
| 204 |
grpc_call* c; |
| 205 |
grpc_call* s; |
| 206 |
grpc_core::CqVerifier cqv(f.cq); |
| 207 |
grpc_op ops[6]; |
| 208 |
grpc_op* op; |
| 209 |
grpc_metadata_array initial_metadata_recv; |
| 210 |
grpc_metadata_array trailing_metadata_recv; |
| 211 |
grpc_metadata_array request_metadata_recv; |
| 212 |
grpc_call_details call_details; |
| 213 |
grpc_status_code status; |
| 214 |
grpc_call_error error; |
| 215 |
grpc_slice details; |
| 216 |
int was_cancelled = 2; |
| 217 |
grpc_byte_buffer* request_payload; |
| 218 |
grpc_byte_buffer* request_payload_recv; |
| 219 |
grpc_byte_buffer* response_payload; |
| 220 |
grpc_byte_buffer* response_payload_recv; |
| 221 |
int i; |
| 222 |
grpc_slice request_payload_slice = |
| 223 |
grpc_slice_from_copied_string("hello world"); |
| 224 |
grpc_slice response_payload_slice = |
| 225 |
grpc_slice_from_copied_string("hello you"); |
| 226 |
|
| 227 |
gpr_timespec deadline = five_seconds_from_now(); |
| 228 |
c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq, |
| 229 |
grpc_slice_from_static_string("/foo"), nullptr, |
| 230 |
deadline, nullptr); |
| 231 |
GPR_ASSERT(c); |
| 232 |
|
| 233 |
grpc_metadata_array_init(&initial_metadata_recv); |
| 234 |
grpc_metadata_array_init(&trailing_metadata_recv); |
| 235 |
grpc_metadata_array_init(&request_metadata_recv); |
| 236 |
grpc_call_details_init(&call_details); |
| 237 |
|
| 238 |
memset(ops, 0, sizeof(ops)); |
| 239 |
op = ops; |
| 240 |
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
| 241 |
op->data.send_initial_metadata.count = 0; |
| 242 |
op->flags = 0; |
| 243 |
op->reserved = nullptr; |
| 244 |
op++; |
| 245 |
op->op = GRPC_OP_RECV_INITIAL_METADATA; |
| 246 |
op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv; |
| 247 |
op->flags = 0; |
| 248 |
op->reserved = nullptr; |
| 249 |
op++; |
| 250 |
op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
| 251 |
op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv; |
| 252 |
op->data.recv_status_on_client.status = &status; |
| 253 |
op->data.recv_status_on_client.status_details = &details; |
| 254 |
op->flags = 0; |
| 255 |
op->reserved = nullptr; |
| 256 |
op++; |
| 257 |
error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1), |
| 258 |
nullptr); |
| 259 |
GPR_ASSERT(GRPC_CALL_OK == error); |
| 260 |
|
| 261 |
error = |
| 262 |
grpc_server_request_call(f.server, &s, &call_details, |
| 263 |
&request_metadata_recv, f.cq, f.cq, tag(100)); |
| 264 |
GPR_ASSERT(GRPC_CALL_OK == error); |
| 265 |
cqv.Expect(tag(100), true); |
| 266 |
cqv.Verify(); |
| 267 |
|
| 268 |
memset(ops, 0, sizeof(ops)); |
| 269 |
op = ops; |
| 270 |
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
| 271 |
op->data.send_initial_metadata.count = 0; |
| 272 |
op->flags = 0; |
| 273 |
op->reserved = nullptr; |
| 274 |
op++; |
| 275 |
op->op = GRPC_OP_RECV_CLOSE_ON_SERVER; |
| 276 |
op->data.recv_close_on_server.cancelled = &was_cancelled; |
| 277 |
op->flags = 0; |
| 278 |
op->reserved = nullptr; |
| 279 |
op++; |
| 280 |
error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(101), |
| 281 |
nullptr); |
| 282 |
GPR_ASSERT(GRPC_CALL_OK == error); |
| 283 |
|
| 284 |
for (i = 0; i < 30; i++) { |
| 285 |
request_payload = grpc_raw_byte_buffer_create(&request_payload_slice, 1); |
| 286 |
response_payload = grpc_raw_byte_buffer_create(&response_payload_slice, 1); |
| 287 |
|
| 288 |
memset(ops, 0, sizeof(ops)); |
| 289 |
op = ops; |
| 290 |
op->op = GRPC_OP_SEND_MESSAGE; |
| 291 |
op->data.send_message.send_message = request_payload; |
| 292 |
op->flags = 0; |
| 293 |
op->reserved = nullptr; |
| 294 |
op++; |
| 295 |
op->op = GRPC_OP_RECV_MESSAGE; |
| 296 |
op->data.recv_message.recv_message = &response_payload_recv; |
| 297 |
op->flags = 0; |
| 298 |
op->reserved = nullptr; |
| 299 |
op++; |
| 300 |
error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(2), |
| 301 |
nullptr); |
| 302 |
GPR_ASSERT(GRPC_CALL_OK == error); |
| 303 |
|
| 304 |
memset(ops, 0, sizeof(ops)); |
| 305 |
op = ops; |
| 306 |
op->op = GRPC_OP_RECV_MESSAGE; |
| 307 |
op->data.recv_message.recv_message = &request_payload_recv; |
| 308 |
op->flags = 0; |
| 309 |
op->reserved = nullptr; |
| 310 |
op++; |
| 311 |
error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), |
| 312 |
tag(102), nullptr); |
| 313 |
GPR_ASSERT(GRPC_CALL_OK == error); |
| 314 |
cqv.Expect(tag(102), true); |
| 315 |
cqv.Verify(); |
| 316 |
|
| 317 |
memset(ops, 0, sizeof(ops)); |
| 318 |
op = ops; |
| 319 |
op->op = GRPC_OP_SEND_MESSAGE; |
| 320 |
op->data.send_message.send_message = response_payload; |
| 321 |
op->flags = 0; |
| 322 |
op->reserved = nullptr; |
| 323 |
op++; |
| 324 |
error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), |
| 325 |
tag(103), nullptr); |
| 326 |
GPR_ASSERT(GRPC_CALL_OK == error); |
| 327 |
cqv.Expect(tag(103), true); |
| 328 |
cqv.Expect(tag(2), true); |
| 329 |
cqv.Verify(); |
| 330 |
|
| 331 |
grpc_byte_buffer_destroy(request_payload); |
| 332 |
grpc_byte_buffer_destroy(response_payload); |
| 333 |
grpc_byte_buffer_destroy(request_payload_recv); |
| 334 |
grpc_byte_buffer_destroy(response_payload_recv); |
| 335 |
/* Sleep for a short interval to check if the client sends any pings */ |
| 336 |
gpr_sleep_until(grpc_timeout_milliseconds_to_deadline(kPingIntervalMS)); |
| 337 |
} |
| 338 |
|
| 339 |
grpc_slice_unref(request_payload_slice); |
| 340 |
grpc_slice_unref(response_payload_slice); |
| 341 |
|
| 342 |
memset(ops, 0, sizeof(ops)); |
| 343 |
op = ops; |
| 344 |
op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; |
| 345 |
op->flags = 0; |
| 346 |
op->reserved = nullptr; |
| 347 |
op++; |
| 348 |
error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(3), |
| 349 |
nullptr); |
| 350 |
GPR_ASSERT(GRPC_CALL_OK == error); |
| 351 |
|
| 352 |
memset(ops, 0, sizeof(ops)); |
| 353 |
op = ops; |
| 354 |
op->op = GRPC_OP_SEND_STATUS_FROM_SERVER; |
| 355 |
op->data.send_status_from_server.trailing_metadata_count = 0; |
| 356 |
op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED; |
| 357 |
grpc_slice status_details = grpc_slice_from_static_string("xyz"); |
| 358 |
op->data.send_status_from_server.status_details = &status_details; |
| 359 |
op->flags = 0; |
| 360 |
op->reserved = nullptr; |
| 361 |
op++; |
| 362 |
error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(104), |
| 363 |
nullptr); |
| 364 |
GPR_ASSERT(GRPC_CALL_OK == error); |
| 365 |
|
| 366 |
cqv.Expect(tag(1), true); |
| 367 |
cqv.Expect(tag(3), true); |
| 368 |
cqv.Expect(tag(101), true); |
| 369 |
cqv.Expect(tag(104), true); |
| 370 |
cqv.Verify(); |
| 371 |
|
| 372 |
grpc_call_unref(c); |
| 373 |
grpc_call_unref(s); |
| 374 |
|
| 375 |
grpc_metadata_array_destroy(&initial_metadata_recv); |
| 376 |
grpc_metadata_array_destroy(&trailing_metadata_recv); |
| 377 |
grpc_metadata_array_destroy(&request_metadata_recv); |
| 378 |
grpc_call_details_destroy(&call_details); |
| 379 |
grpc_slice_unref(details); |
| 380 |
|
| 381 |
end_test(&f); |
| 382 |
config.tear_down_data(&f); |
| 383 |
} |
| 384 |
|
| 385 |
void keepalive_timeout(grpc_end2end_test_config config) { |
| 386 |
test_keepalive_timeout(config); |
| 387 |
test_read_delays_keepalive(config); |
| 388 |
} |
| 389 |
|
| 390 |
void keepalive_timeout_pre_init(void) {} |