3 * Copyright 2017 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "test/core/end2end/end2end_tests.h"
24 #include <grpc/byte_buffer.h>
25 #include <grpc/support/alloc.h>
26 #include <grpc/support/log.h>
27 #include <grpc/support/time.h>
29 #include "src/core/ext/transport/chttp2/transport/frame_ping.h"
30 #include "src/core/lib/channel/channel_args.h"
31 #include "src/core/lib/gpr/env.h"
32 #include "src/core/lib/gpr/useful.h"
33 #include "src/core/lib/iomgr/exec_ctx.h"
34 #include "test/core/end2end/cq_verifier.h"
36 static void* tag(intptr_t t) { return (void*)t; }
38 static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
39 const char* test_name,
40 grpc_channel_args* client_args,
41 grpc_channel_args* server_args) {
42 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);
50 static gpr_timespec n_seconds_from_now(int n) {
51 return grpc_timeout_seconds_to_deadline(n);
54 static gpr_timespec five_seconds_from_now(void) {
55 return n_seconds_from_now(5);
58 static void drain_cq(grpc_completion_queue* cq) {
61 ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr);
62 } while (ev.type != GRPC_QUEUE_SHUTDOWN);
65 static void shutdown_server(grpc_end2end_test_fixture* f) {
66 if (!f->server) return;
68 grpc_server_shutdown_and_notify(f->server, f->shutdown_cq, tag(1000));
69 GPR_ASSERT(grpc_completion_queue_pluck(f->shutdown_cq, tag(1000),
70 five_seconds_from_now(), nullptr)
71 .type == GRPC_OP_COMPLETE);
72 grpc_server_destroy(f->server);
76 static void shutdown_client(grpc_end2end_test_fixture* f) {
77 if (!f->client) return;
78 grpc_channel_destroy(f->client);
82 static void end_test(grpc_end2end_test_fixture* f) {
86 grpc_completion_queue_shutdown(f->cq);
88 grpc_completion_queue_destroy(f->cq);
89 grpc_completion_queue_destroy(f->shutdown_cq);
92 /* Client sends a request, server replies with a payload, then waits for the
93 keepalive watchdog timeouts before returning status. */
94 static void test_keepalive_timeout(grpc_end2end_test_config config) {
97 grpc_slice response_payload_slice =
98 grpc_slice_from_copied_string("hello world");
99 grpc_byte_buffer* response_payload =
100 grpc_raw_byte_buffer_create(&response_payload_slice, 1);
102 grpc_arg keepalive_arg_elems[3];
103 keepalive_arg_elems[0].type = GRPC_ARG_INTEGER;
104 keepalive_arg_elems[0].key = const_cast<char*>(GRPC_ARG_KEEPALIVE_TIME_MS);
105 keepalive_arg_elems[0].value.integer = 3500;
106 keepalive_arg_elems[1].type = GRPC_ARG_INTEGER;
107 keepalive_arg_elems[1].key = const_cast<char*>(GRPC_ARG_KEEPALIVE_TIMEOUT_MS);
108 keepalive_arg_elems[1].value.integer = 0;
109 keepalive_arg_elems[2].type = GRPC_ARG_INTEGER;
110 keepalive_arg_elems[2].key = const_cast<char*>(GRPC_ARG_HTTP2_BDP_PROBE);
111 keepalive_arg_elems[2].value.integer = 0;
112 grpc_channel_args keepalive_args = {GPR_ARRAY_SIZE(keepalive_arg_elems),
113 keepalive_arg_elems};
115 grpc_end2end_test_fixture f =
116 begin_test(config, "keepalive_timeout", &keepalive_args, nullptr);
117 cq_verifier* cqv = cq_verifier_create(f.cq);
120 grpc_metadata_array initial_metadata_recv;
121 grpc_metadata_array trailing_metadata_recv;
122 grpc_metadata_array request_metadata_recv;
123 grpc_byte_buffer* response_payload_recv = nullptr;
124 grpc_call_details call_details;
125 grpc_status_code status;
126 grpc_call_error error;
129 /* Disable ping ack to trigger the keepalive timeout */
130 grpc_set_disable_ping_ack(true);
132 gpr_timespec deadline = five_seconds_from_now();
133 c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
134 grpc_slice_from_static_string("/foo"), nullptr,
138 grpc_metadata_array_init(&initial_metadata_recv);
139 grpc_metadata_array_init(&trailing_metadata_recv);
140 grpc_metadata_array_init(&request_metadata_recv);
141 grpc_call_details_init(&call_details);
143 memset(ops, 0, sizeof(ops));
145 op->op = GRPC_OP_SEND_INITIAL_METADATA;
146 op->data.send_initial_metadata.count = 0;
148 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
150 op->op = GRPC_OP_RECV_INITIAL_METADATA;
151 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
153 op->op = GRPC_OP_RECV_MESSAGE;
154 op->data.recv_message.recv_message = &response_payload_recv;
156 error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
158 GPR_ASSERT(GRPC_CALL_OK == error);
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);
166 memset(ops, 0, sizeof(ops));
168 op->op = GRPC_OP_SEND_INITIAL_METADATA;
169 op->data.send_initial_metadata.count = 0;
171 op->op = GRPC_OP_SEND_MESSAGE;
172 op->data.send_message.send_message = response_payload;
174 error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(102),
176 GPR_ASSERT(GRPC_CALL_OK == error);
178 CQ_EXPECT_COMPLETION(cqv, tag(102), 1);
179 CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
182 memset(ops, 0, sizeof(ops));
184 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
185 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
186 op->data.recv_status_on_client.status = &status;
187 op->data.recv_status_on_client.status_details = &details;
189 error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(3),
191 GPR_ASSERT(GRPC_CALL_OK == error);
193 CQ_EXPECT_COMPLETION(cqv, tag(3), 1);
196 char* details_str = grpc_slice_to_c_string(details);
197 char* method_str = grpc_slice_to_c_string(call_details.method);
198 GPR_ASSERT(status == GRPC_STATUS_UNAVAILABLE);
199 GPR_ASSERT(0 == grpc_slice_str_cmp(details, "keepalive watchdog timeout"));
200 GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo"));
202 gpr_free(details_str);
203 gpr_free(method_str);
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);
214 cq_verifier_destroy(cqv);
216 grpc_byte_buffer_destroy(response_payload);
217 grpc_byte_buffer_destroy(response_payload_recv);
220 config.tear_down_data(&f);
223 /* Verify that reads reset the keepalive ping timer. The client sends 30 pings
224 * with a sleep of 10ms in between. It has a configured keepalive timer of
225 * 200ms. In the success case, each ping ack should reset the keepalive timer so
226 * that the keepalive ping is never sent. */
227 static void test_read_delays_keepalive(grpc_end2end_test_config config) {
228 char* poller = gpr_getenv("GRPC_POLL_STRATEGY");
229 /* It is hard to get the timing right for the polling engines poll and poll-cv
231 if (poller != nullptr &&
232 (0 == strcmp(poller, "poll-cv") || 0 == strcmp(poller, "poll"))) {
237 const int kPingIntervalMS = 100;
238 grpc_arg keepalive_arg_elems[3];
239 keepalive_arg_elems[0].type = GRPC_ARG_INTEGER;
240 keepalive_arg_elems[0].key = const_cast<char*>(GRPC_ARG_KEEPALIVE_TIME_MS);
241 keepalive_arg_elems[0].value.integer = 20 * kPingIntervalMS;
242 keepalive_arg_elems[1].type = GRPC_ARG_INTEGER;
243 keepalive_arg_elems[1].key = const_cast<char*>(GRPC_ARG_KEEPALIVE_TIMEOUT_MS);
244 keepalive_arg_elems[1].value.integer = 0;
245 keepalive_arg_elems[2].type = GRPC_ARG_INTEGER;
246 keepalive_arg_elems[2].key = const_cast<char*>(GRPC_ARG_HTTP2_BDP_PROBE);
247 keepalive_arg_elems[2].value.integer = 0;
248 grpc_channel_args keepalive_args = {GPR_ARRAY_SIZE(keepalive_arg_elems),
249 keepalive_arg_elems};
250 grpc_end2end_test_fixture f = begin_test(config, "test_read_delays_keepalive",
251 &keepalive_args, nullptr);
252 /* Disable ping ack to trigger the keepalive timeout */
253 grpc_set_disable_ping_ack(true);
256 cq_verifier* cqv = cq_verifier_create(f.cq);
259 grpc_metadata_array initial_metadata_recv;
260 grpc_metadata_array trailing_metadata_recv;
261 grpc_metadata_array request_metadata_recv;
262 grpc_call_details call_details;
263 grpc_status_code status;
264 grpc_call_error error;
266 int was_cancelled = 2;
267 grpc_byte_buffer* request_payload;
268 grpc_byte_buffer* request_payload_recv;
269 grpc_byte_buffer* response_payload;
270 grpc_byte_buffer* response_payload_recv;
272 grpc_slice request_payload_slice =
273 grpc_slice_from_copied_string("hello world");
274 grpc_slice response_payload_slice =
275 grpc_slice_from_copied_string("hello you");
277 gpr_timespec deadline = five_seconds_from_now();
278 c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
279 grpc_slice_from_static_string("/foo"), nullptr,
283 grpc_metadata_array_init(&initial_metadata_recv);
284 grpc_metadata_array_init(&trailing_metadata_recv);
285 grpc_metadata_array_init(&request_metadata_recv);
286 grpc_call_details_init(&call_details);
288 memset(ops, 0, sizeof(ops));
290 op->op = GRPC_OP_SEND_INITIAL_METADATA;
291 op->data.send_initial_metadata.count = 0;
293 op->reserved = nullptr;
295 op->op = GRPC_OP_RECV_INITIAL_METADATA;
296 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
298 op->reserved = nullptr;
300 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
301 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
302 op->data.recv_status_on_client.status = &status;
303 op->data.recv_status_on_client.status_details = &details;
305 op->reserved = nullptr;
307 error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
309 GPR_ASSERT(GRPC_CALL_OK == error);
312 grpc_server_request_call(f.server, &s, &call_details,
313 &request_metadata_recv, f.cq, f.cq, tag(100));
314 GPR_ASSERT(GRPC_CALL_OK == error);
315 CQ_EXPECT_COMPLETION(cqv, tag(100), 1);
318 memset(ops, 0, sizeof(ops));
320 op->op = GRPC_OP_SEND_INITIAL_METADATA;
321 op->data.send_initial_metadata.count = 0;
323 op->reserved = nullptr;
325 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
326 op->data.recv_close_on_server.cancelled = &was_cancelled;
328 op->reserved = nullptr;
330 error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(101),
332 GPR_ASSERT(GRPC_CALL_OK == error);
334 for (i = 0; i < 30; i++) {
335 request_payload = grpc_raw_byte_buffer_create(&request_payload_slice, 1);
336 response_payload = grpc_raw_byte_buffer_create(&response_payload_slice, 1);
338 memset(ops, 0, sizeof(ops));
340 op->op = GRPC_OP_SEND_MESSAGE;
341 op->data.send_message.send_message = request_payload;
343 op->reserved = nullptr;
345 op->op = GRPC_OP_RECV_MESSAGE;
346 op->data.recv_message.recv_message = &response_payload_recv;
348 op->reserved = nullptr;
350 error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(2),
352 GPR_ASSERT(GRPC_CALL_OK == error);
354 memset(ops, 0, sizeof(ops));
356 op->op = GRPC_OP_RECV_MESSAGE;
357 op->data.recv_message.recv_message = &request_payload_recv;
359 op->reserved = nullptr;
361 error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops),
363 GPR_ASSERT(GRPC_CALL_OK == error);
364 CQ_EXPECT_COMPLETION(cqv, tag(102), 1);
367 memset(ops, 0, sizeof(ops));
369 op->op = GRPC_OP_SEND_MESSAGE;
370 op->data.send_message.send_message = response_payload;
372 op->reserved = nullptr;
374 error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops),
376 GPR_ASSERT(GRPC_CALL_OK == error);
377 CQ_EXPECT_COMPLETION(cqv, tag(103), 1);
378 CQ_EXPECT_COMPLETION(cqv, tag(2), 1);
381 grpc_byte_buffer_destroy(request_payload);
382 grpc_byte_buffer_destroy(response_payload);
383 grpc_byte_buffer_destroy(request_payload_recv);
384 grpc_byte_buffer_destroy(response_payload_recv);
385 /* Sleep for a short interval to check if the client sends any pings */
386 gpr_sleep_until(grpc_timeout_milliseconds_to_deadline(kPingIntervalMS));
389 grpc_slice_unref(request_payload_slice);
390 grpc_slice_unref(response_payload_slice);
392 memset(ops, 0, sizeof(ops));
394 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
396 op->reserved = nullptr;
398 error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(3),
400 GPR_ASSERT(GRPC_CALL_OK == error);
402 memset(ops, 0, sizeof(ops));
404 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
405 op->data.send_status_from_server.trailing_metadata_count = 0;
406 op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED;
407 grpc_slice status_details = grpc_slice_from_static_string("xyz");
408 op->data.send_status_from_server.status_details = &status_details;
410 op->reserved = nullptr;
412 error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(104),
414 GPR_ASSERT(GRPC_CALL_OK == error);
416 CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
417 CQ_EXPECT_COMPLETION(cqv, tag(3), 1);
418 CQ_EXPECT_COMPLETION(cqv, tag(101), 1);
419 CQ_EXPECT_COMPLETION(cqv, tag(104), 1);
425 cq_verifier_destroy(cqv);
427 grpc_metadata_array_destroy(&initial_metadata_recv);
428 grpc_metadata_array_destroy(&trailing_metadata_recv);
429 grpc_metadata_array_destroy(&request_metadata_recv);
430 grpc_call_details_destroy(&call_details);
431 grpc_slice_unref(details);
434 config.tear_down_data(&f);
437 void keepalive_timeout(grpc_end2end_test_config config) {
438 test_keepalive_timeout(config);
439 test_read_delays_keepalive(config);
442 void keepalive_timeout_pre_init(void) {}