• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "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 
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"
35 
tag(intptr_t t)36 static void* tag(intptr_t t) { return (void*)t; }
37 
begin_test(grpc_end2end_test_config config,const char * test_name,grpc_channel_args * client_args,grpc_channel_args * server_args)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);
47   return f;
48 }
49 
n_seconds_from_now(int n)50 static gpr_timespec n_seconds_from_now(int n) {
51   return grpc_timeout_seconds_to_deadline(n);
52 }
53 
five_seconds_from_now(void)54 static gpr_timespec five_seconds_from_now(void) {
55   return n_seconds_from_now(5);
56 }
57 
drain_cq(grpc_completion_queue * cq)58 static void drain_cq(grpc_completion_queue* cq) {
59   grpc_event ev;
60   do {
61     ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr);
62   } while (ev.type != GRPC_QUEUE_SHUTDOWN);
63 }
64 
shutdown_server(grpc_end2end_test_fixture * f)65 static void shutdown_server(grpc_end2end_test_fixture* f) {
66   if (!f->server) return;
67 
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);
73   f->server = nullptr;
74 }
75 
shutdown_client(grpc_end2end_test_fixture * f)76 static void shutdown_client(grpc_end2end_test_fixture* f) {
77   if (!f->client) return;
78   grpc_channel_destroy(f->client);
79   f->client = nullptr;
80 }
81 
end_test(grpc_end2end_test_fixture * f)82 static void end_test(grpc_end2end_test_fixture* f) {
83   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);
89   grpc_completion_queue_destroy(f->shutdown_cq);
90 }
91 
92 /* Client sends a request, server replies with a payload, then waits for the
93    keepalive watchdog timeouts before returning status. */
test_keepalive_timeout(grpc_end2end_test_config config)94 static void test_keepalive_timeout(grpc_end2end_test_config config) {
95   grpc_call* c;
96   grpc_call* s;
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);
101 
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};
114 
115   grpc_end2end_test_fixture f =
116       begin_test(config, "keepalive_timeout", &keepalive_args, nullptr);
117   cq_verifier* cqv = cq_verifier_create(f.cq);
118   grpc_op ops[6];
119   grpc_op* op;
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;
127   grpc_slice details;
128 
129   /* Disable ping ack to trigger the keepalive timeout */
130   grpc_set_disable_ping_ack(true);
131 
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,
135                                deadline, nullptr);
136   GPR_ASSERT(c);
137 
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);
142 
143   memset(ops, 0, sizeof(ops));
144   op = ops;
145   op->op = GRPC_OP_SEND_INITIAL_METADATA;
146   op->data.send_initial_metadata.count = 0;
147   op++;
148   op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
149   op++;
150   op->op = GRPC_OP_RECV_INITIAL_METADATA;
151   op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
152   op++;
153   op->op = GRPC_OP_RECV_MESSAGE;
154   op->data.recv_message.recv_message = &response_payload_recv;
155   op++;
156   error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
157                                 nullptr);
158   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++;
174   error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(102),
175                                 nullptr);
176   GPR_ASSERT(GRPC_CALL_OK == error);
177 
178   CQ_EXPECT_COMPLETION(cqv, tag(102), 1);
179   CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
180   cq_verify(cqv);
181 
182   memset(ops, 0, sizeof(ops));
183   op = 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;
188   op++;
189   error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(3),
190                                 nullptr);
191   GPR_ASSERT(GRPC_CALL_OK == error);
192 
193   CQ_EXPECT_COMPLETION(cqv, tag(3), 1);
194   cq_verify(cqv);
195 
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_INTERNAL);
199   GPR_ASSERT(0 == grpc_slice_str_cmp(details, "keepalive watchdog timeout"));
200   GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo"));
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 
211   grpc_call_unref(c);
212   grpc_call_unref(s);
213 
214   cq_verifier_destroy(cqv);
215 
216   grpc_byte_buffer_destroy(response_payload);
217   grpc_byte_buffer_destroy(response_payload_recv);
218 
219   end_test(&f);
220   config.tear_down_data(&f);
221 }
222 
keepalive_timeout(grpc_end2end_test_config config)223 void keepalive_timeout(grpc_end2end_test_config config) {
224   test_keepalive_timeout(config);
225 }
226 
keepalive_timeout_pre_init(void)227 void keepalive_timeout_pre_init(void) {}
228