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 void keepalive_timeout(grpc_end2end_test_config config) {
224 test_keepalive_timeout(config);
227 void keepalive_timeout_pre_init(void) {}