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/lib/channel/channel_args.h"
30 #include "src/core/lib/iomgr/exec_ctx.h"
31 #include "src/core/lib/slice/slice_internal.h"
32 #include "src/core/lib/transport/metadata.h"
33 #include "src/core/lib/transport/service_config.h"
34
35 #include "test/core/end2end/cq_verifier.h"
36 #include "test/core/end2end/tests/cancel_test_helpers.h"
37
tag(intptr_t t)38 static void* tag(intptr_t t) { return (void*)t; }
39
begin_test(grpc_end2end_test_config config,const char * test_name,cancellation_mode mode,bool use_service_config,grpc_channel_args * client_args,grpc_channel_args * server_args)40 static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
41 const char* test_name,
42 cancellation_mode mode,
43 bool use_service_config,
44 grpc_channel_args* client_args,
45 grpc_channel_args* server_args) {
46 grpc_end2end_test_fixture f;
47 gpr_log(GPR_INFO, "Running test: %s/%s/%s/%s", test_name, config.name,
48 mode.name, use_service_config ? "service_config" : "client_api");
49 f = config.create_fixture(client_args, server_args);
50 config.init_server(&f, server_args);
51 config.init_client(&f, client_args);
52 return f;
53 }
54
n_seconds_from_now(int n)55 static gpr_timespec n_seconds_from_now(int n) {
56 return grpc_timeout_seconds_to_deadline(n);
57 }
58
five_seconds_from_now(void)59 static gpr_timespec five_seconds_from_now(void) {
60 return n_seconds_from_now(5);
61 }
62
drain_cq(grpc_completion_queue * cq)63 static void drain_cq(grpc_completion_queue* cq) {
64 grpc_event ev;
65 do {
66 ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr);
67 } while (ev.type != GRPC_QUEUE_SHUTDOWN);
68 }
69
shutdown_server(grpc_end2end_test_fixture * f)70 static void shutdown_server(grpc_end2end_test_fixture* f) {
71 if (!f->server) return;
72 grpc_server_shutdown_and_notify(f->server, f->shutdown_cq, tag(1000));
73 GPR_ASSERT(grpc_completion_queue_pluck(f->shutdown_cq, tag(1000),
74 grpc_timeout_seconds_to_deadline(5),
75 nullptr)
76 .type == GRPC_OP_COMPLETE);
77 grpc_server_destroy(f->server);
78 f->server = nullptr;
79 }
80
shutdown_client(grpc_end2end_test_fixture * f)81 static void shutdown_client(grpc_end2end_test_fixture* f) {
82 if (!f->client) return;
83 grpc_channel_destroy(f->client);
84 f->client = nullptr;
85 }
86
end_test(grpc_end2end_test_fixture * f)87 static void end_test(grpc_end2end_test_fixture* f) {
88 shutdown_server(f);
89 shutdown_client(f);
90
91 grpc_completion_queue_shutdown(f->cq);
92 drain_cq(f->cq);
93 grpc_completion_queue_destroy(f->cq);
94 grpc_completion_queue_destroy(f->shutdown_cq);
95 }
96
97 /* Cancel after accept, no payload */
test_cancel_after_round_trip(grpc_end2end_test_config config,cancellation_mode mode,bool use_service_config)98 static void test_cancel_after_round_trip(grpc_end2end_test_config config,
99 cancellation_mode mode,
100 bool use_service_config) {
101 grpc_op ops[6];
102 grpc_op* op;
103 grpc_call* c;
104 grpc_call* s;
105 grpc_metadata_array initial_metadata_recv;
106 grpc_metadata_array trailing_metadata_recv;
107 grpc_metadata_array request_metadata_recv;
108 grpc_call_details call_details;
109 grpc_status_code status;
110 grpc_call_error error;
111 grpc_slice details;
112 grpc_byte_buffer* request_payload_recv = nullptr;
113 grpc_byte_buffer* response_payload_recv = nullptr;
114 grpc_slice request_payload_slice =
115 grpc_slice_from_copied_string("hello world");
116 grpc_slice response_payload_slice =
117 grpc_slice_from_copied_string("hello you");
118 grpc_byte_buffer* request_payload =
119 grpc_raw_byte_buffer_create(&request_payload_slice, 1);
120 grpc_byte_buffer* response_payload1 =
121 grpc_raw_byte_buffer_create(&response_payload_slice, 1);
122 grpc_byte_buffer* response_payload2 =
123 grpc_raw_byte_buffer_create(&response_payload_slice, 1);
124 int was_cancelled = 2;
125
126 grpc_channel_args* args = nullptr;
127 if (use_service_config) {
128 grpc_arg arg;
129 arg.type = GRPC_ARG_STRING;
130 arg.key = const_cast<char*>(GRPC_ARG_SERVICE_CONFIG);
131 arg.value.string = const_cast<char*>(
132 "{\n"
133 " \"methodConfig\": [ {\n"
134 " \"name\": [\n"
135 " { \"service\": \"service\", \"method\": \"method\" }\n"
136 " ],\n"
137 " \"timeout\": \"5s\"\n"
138 " } ]\n"
139 "}");
140 args = grpc_channel_args_copy_and_add(args, &arg, 1);
141 }
142
143 grpc_end2end_test_fixture f =
144 begin_test(config, "cancel_after_round_trip", mode, use_service_config,
145 args, nullptr);
146 cq_verifier* cqv = cq_verifier_create(f.cq);
147
148 gpr_timespec deadline = use_service_config
149 ? gpr_inf_future(GPR_CLOCK_MONOTONIC)
150 : five_seconds_from_now();
151 c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
152 grpc_slice_from_static_string("/service/method"),
153 nullptr, deadline, nullptr);
154 GPR_ASSERT(c);
155
156 grpc_metadata_array_init(&initial_metadata_recv);
157 grpc_metadata_array_init(&trailing_metadata_recv);
158 grpc_metadata_array_init(&request_metadata_recv);
159 grpc_call_details_init(&call_details);
160
161 memset(ops, 0, sizeof(ops));
162 op = ops;
163 op->op = GRPC_OP_SEND_INITIAL_METADATA;
164 op->data.send_initial_metadata.count = 0;
165 op->flags = 0;
166 op->reserved = nullptr;
167 op++;
168 op->op = GRPC_OP_SEND_MESSAGE;
169 op->data.send_message.send_message = request_payload;
170 op->flags = 0;
171 op->reserved = nullptr;
172 op++;
173 op->op = GRPC_OP_RECV_INITIAL_METADATA;
174 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
175 op->flags = 0;
176 op->reserved = nullptr;
177 op++;
178 op->op = GRPC_OP_RECV_MESSAGE;
179 op->data.recv_message.recv_message = &response_payload_recv;
180 op->flags = 0;
181 op->reserved = nullptr;
182 op++;
183 error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
184 nullptr);
185 GPR_ASSERT(GRPC_CALL_OK == error);
186
187 error =
188 grpc_server_request_call(f.server, &s, &call_details,
189 &request_metadata_recv, f.cq, f.cq, tag(101));
190 GPR_ASSERT(GRPC_CALL_OK == error);
191 CQ_EXPECT_COMPLETION(cqv, tag(101), 1);
192 cq_verify(cqv);
193
194 memset(ops, 0, sizeof(ops));
195 op = ops;
196 op->op = GRPC_OP_RECV_MESSAGE;
197 op->data.recv_message.recv_message = &request_payload_recv;
198 op->flags = 0;
199 op->reserved = nullptr;
200 op++;
201 op->op = GRPC_OP_SEND_INITIAL_METADATA;
202 op->data.send_initial_metadata.count = 0;
203 op->flags = 0;
204 op->reserved = nullptr;
205 op++;
206 op->op = GRPC_OP_SEND_MESSAGE;
207 op->data.send_message.send_message = response_payload1;
208 op->flags = 0;
209 op->reserved = nullptr;
210 op++;
211 error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(102),
212 nullptr);
213 GPR_ASSERT(GRPC_CALL_OK == error);
214
215 CQ_EXPECT_COMPLETION(cqv, tag(102), 1);
216 CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
217 cq_verify(cqv);
218
219 grpc_byte_buffer_destroy(request_payload_recv);
220 grpc_byte_buffer_destroy(response_payload_recv);
221 request_payload_recv = nullptr;
222 response_payload_recv = nullptr;
223
224 memset(ops, 0, sizeof(ops));
225 op = ops;
226 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
227 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
228 op->data.recv_status_on_client.status = &status;
229 op->data.recv_status_on_client.status_details = &details;
230 op->flags = 0;
231 op->reserved = nullptr;
232 op++;
233 op->op = GRPC_OP_RECV_MESSAGE;
234 op->data.recv_message.recv_message = &response_payload_recv;
235 op->flags = 0;
236 op->reserved = nullptr;
237 op++;
238 error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(2),
239 nullptr);
240 GPR_ASSERT(GRPC_CALL_OK == error);
241
242 GPR_ASSERT(GRPC_CALL_OK == mode.initiate_cancel(c, nullptr));
243
244 memset(ops, 0, sizeof(ops));
245 op = ops;
246 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
247 op->data.recv_close_on_server.cancelled = &was_cancelled;
248 op->flags = 0;
249 op->reserved = nullptr;
250 op++;
251 op->op = GRPC_OP_SEND_MESSAGE;
252 op->data.send_message.send_message = response_payload2;
253 op->flags = 0;
254 op->reserved = nullptr;
255 op++;
256 error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(103),
257 nullptr);
258 GPR_ASSERT(GRPC_CALL_OK == error);
259
260 CQ_EXPECT_COMPLETION(cqv, tag(2), 1);
261 CQ_EXPECT_COMPLETION(cqv, tag(103), 1);
262 cq_verify(cqv);
263
264 GPR_ASSERT(status == mode.expect_status || status == GRPC_STATUS_INTERNAL);
265 GPR_ASSERT(was_cancelled == 1);
266
267 grpc_metadata_array_destroy(&initial_metadata_recv);
268 grpc_metadata_array_destroy(&trailing_metadata_recv);
269 grpc_metadata_array_destroy(&request_metadata_recv);
270 grpc_call_details_destroy(&call_details);
271
272 grpc_byte_buffer_destroy(request_payload);
273 grpc_byte_buffer_destroy(response_payload1);
274 grpc_byte_buffer_destroy(response_payload2);
275 grpc_byte_buffer_destroy(request_payload_recv);
276 grpc_byte_buffer_destroy(response_payload_recv);
277 grpc_slice_unref(details);
278
279 grpc_call_unref(c);
280 grpc_call_unref(s);
281
282 if (args != nullptr) {
283 grpc_core::ExecCtx exec_ctx;
284 grpc_channel_args_destroy(args);
285 }
286
287 cq_verifier_destroy(cqv);
288 end_test(&f);
289 config.tear_down_data(&f);
290 }
291
cancel_after_round_trip(grpc_end2end_test_config config)292 void cancel_after_round_trip(grpc_end2end_test_config config) {
293 unsigned i;
294
295 for (i = 0; i < GPR_ARRAY_SIZE(cancellation_modes); i++) {
296 test_cancel_after_round_trip(config, cancellation_modes[i],
297 false /* use_service_config */);
298 if (config.feature_mask & FEATURE_MASK_SUPPORTS_CLIENT_CHANNEL &&
299 cancellation_modes[i].expect_status == GRPC_STATUS_DEADLINE_EXCEEDED) {
300 test_cancel_after_round_trip(config, cancellation_modes[i],
301 true /* use_service_config */);
302 }
303 }
304 }
305
cancel_after_round_trip_pre_init(void)306 void cancel_after_round_trip_pre_init(void) {}
307