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/grpc.h>
26 #include <grpc/support/alloc.h>
27 #include <grpc/support/log.h>
28 #include <grpc/support/string_util.h>
29 #include <grpc/support/time.h>
30
31 #include "src/core/lib/channel/channel_args.h"
32 #include "src/core/lib/gpr/string.h"
33 #include "src/core/lib/gpr/useful.h"
34 #include "src/core/lib/iomgr/exec_ctx.h"
35 #include "src/core/lib/transport/static_metadata.h"
36
37 #include "test/core/end2end/cq_verifier.h"
38 #include "test/core/end2end/tests/cancel_test_helpers.h"
39
tag(intptr_t t)40 static void* tag(intptr_t t) { return (void*)t; }
41
begin_test(grpc_end2end_test_config config,const char * test_name,grpc_channel_args * client_args,grpc_channel_args * server_args)42 static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
43 const char* test_name,
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", test_name, config.name);
48 f = config.create_fixture(client_args, server_args);
49 config.init_server(&f, server_args);
50 config.init_client(&f, client_args);
51 return f;
52 }
53
n_seconds_from_now(int n)54 static gpr_timespec n_seconds_from_now(int n) {
55 return grpc_timeout_seconds_to_deadline(n);
56 }
57
five_seconds_from_now(void)58 static gpr_timespec five_seconds_from_now(void) {
59 return n_seconds_from_now(5);
60 }
61
drain_cq(grpc_completion_queue * cq)62 static void drain_cq(grpc_completion_queue* cq) {
63 grpc_event ev;
64 do {
65 ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr);
66 } while (ev.type != GRPC_QUEUE_SHUTDOWN);
67 }
68
shutdown_server(grpc_end2end_test_fixture * f)69 static void shutdown_server(grpc_end2end_test_fixture* f) {
70 if (!f->server) return;
71 grpc_server_shutdown_and_notify(f->server, f->shutdown_cq, tag(1000));
72 GPR_ASSERT(grpc_completion_queue_pluck(f->shutdown_cq, tag(1000),
73 grpc_timeout_seconds_to_deadline(5),
74 nullptr)
75 .type == GRPC_OP_COMPLETE);
76 grpc_server_destroy(f->server);
77 f->server = nullptr;
78 }
79
shutdown_client(grpc_end2end_test_fixture * f)80 static void shutdown_client(grpc_end2end_test_fixture* f) {
81 if (!f->client) return;
82 grpc_channel_destroy(f->client);
83 f->client = nullptr;
84 }
85
end_test(grpc_end2end_test_fixture * f)86 static void end_test(grpc_end2end_test_fixture* f) {
87 shutdown_server(f);
88 shutdown_client(f);
89
90 grpc_completion_queue_shutdown(f->cq);
91 drain_cq(f->cq);
92 grpc_completion_queue_destroy(f->cq);
93 grpc_completion_queue_destroy(f->shutdown_cq);
94 }
95
96 // Tests that we correctly clean up if the second attempt finishes
97 // before we have finished replaying all of the send ops.
test_retry_streaming_succeeds_before_replay_finished(grpc_end2end_test_config config)98 static void test_retry_streaming_succeeds_before_replay_finished(
99 grpc_end2end_test_config config) {
100 grpc_call* c;
101 grpc_call* s;
102 grpc_op ops[6];
103 grpc_op* op;
104 grpc_metadata_array initial_metadata_recv;
105 grpc_metadata_array trailing_metadata_recv;
106 grpc_metadata_array request_metadata_recv;
107 grpc_call_details call_details;
108 grpc_slice request_payload_slice = grpc_slice_from_static_string("foo");
109 grpc_slice request2_payload_slice = grpc_slice_from_static_string("bar");
110 grpc_slice request3_payload_slice = grpc_slice_from_static_string("baz");
111 grpc_slice response_payload_slice = grpc_slice_from_static_string("quux");
112 grpc_byte_buffer* request_payload =
113 grpc_raw_byte_buffer_create(&request_payload_slice, 1);
114 grpc_byte_buffer* request2_payload =
115 grpc_raw_byte_buffer_create(&request2_payload_slice, 1);
116 grpc_byte_buffer* request3_payload =
117 grpc_raw_byte_buffer_create(&request3_payload_slice, 1);
118 grpc_byte_buffer* response_payload =
119 grpc_raw_byte_buffer_create(&response_payload_slice, 1);
120 grpc_byte_buffer* request_payload_recv = nullptr;
121 grpc_byte_buffer* request2_payload_recv = nullptr;
122 grpc_byte_buffer* request3_payload_recv = nullptr;
123 grpc_byte_buffer* response_payload_recv = nullptr;
124 grpc_status_code status;
125 grpc_call_error error;
126 grpc_slice details;
127 int was_cancelled = 2;
128 char* peer;
129
130 grpc_arg arg;
131 arg.type = GRPC_ARG_STRING;
132 arg.key = const_cast<char*>(GRPC_ARG_SERVICE_CONFIG);
133 arg.value.string = const_cast<char*>(
134 "{\n"
135 " \"methodConfig\": [ {\n"
136 " \"name\": [\n"
137 " { \"service\": \"service\", \"method\": \"method\" }\n"
138 " ],\n"
139 " \"retryPolicy\": {\n"
140 " \"maxAttempts\": 3,\n"
141 " \"initialBackoff\": \"1s\",\n"
142 " \"maxBackoff\": \"120s\",\n"
143 " \"backoffMultiplier\": 1.6,\n"
144 " \"retryableStatusCodes\": [ \"ABORTED\" ]\n"
145 " }\n"
146 " } ]\n"
147 "}");
148 grpc_channel_args client_args = {1, &arg};
149 grpc_end2end_test_fixture f =
150 begin_test(config, "retry_streaming", &client_args, nullptr);
151
152 cq_verifier* cqv = cq_verifier_create(f.cq);
153
154 gpr_timespec deadline = five_seconds_from_now();
155 c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
156 grpc_slice_from_static_string("/service/method"),
157 nullptr, deadline, nullptr);
158 GPR_ASSERT(c);
159
160 peer = grpc_call_get_peer(c);
161 GPR_ASSERT(peer != nullptr);
162 gpr_log(GPR_DEBUG, "client_peer_before_call=%s", peer);
163 gpr_free(peer);
164
165 grpc_metadata_array_init(&initial_metadata_recv);
166 grpc_metadata_array_init(&trailing_metadata_recv);
167 grpc_metadata_array_init(&request_metadata_recv);
168 grpc_call_details_init(&call_details);
169 grpc_slice status_details = grpc_slice_from_static_string("xyz");
170
171 // Client starts a batch for receiving initial metadata, a message,
172 // and trailing metadata.
173 memset(ops, 0, sizeof(ops));
174 op = ops;
175 op->op = GRPC_OP_RECV_INITIAL_METADATA;
176 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
177 op++;
178 op->op = GRPC_OP_RECV_MESSAGE;
179 op->data.recv_message.recv_message = &response_payload_recv;
180 op++;
181 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
182 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
183 op->data.recv_status_on_client.status = &status;
184 op->data.recv_status_on_client.status_details = &details;
185 op++;
186 error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), nullptr);
187 GPR_ASSERT(GRPC_CALL_OK == error);
188
189 // Client sends initial metadata and a message.
190 memset(ops, 0, sizeof(ops));
191 op = ops;
192 op->op = GRPC_OP_SEND_INITIAL_METADATA;
193 op->data.send_initial_metadata.count = 0;
194 op++;
195 op->op = GRPC_OP_SEND_MESSAGE;
196 op->data.send_message.send_message = request_payload;
197 op++;
198 error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(2), nullptr);
199 GPR_ASSERT(GRPC_CALL_OK == error);
200 CQ_EXPECT_COMPLETION(cqv, tag(2), true);
201 cq_verify(cqv);
202
203 // Server gets a call with received initial metadata.
204 error =
205 grpc_server_request_call(f.server, &s, &call_details,
206 &request_metadata_recv, f.cq, f.cq, tag(101));
207 GPR_ASSERT(GRPC_CALL_OK == error);
208 CQ_EXPECT_COMPLETION(cqv, tag(101), true);
209 cq_verify(cqv);
210
211 peer = grpc_call_get_peer(s);
212 GPR_ASSERT(peer != nullptr);
213 gpr_log(GPR_DEBUG, "server_peer=%s", peer);
214 gpr_free(peer);
215 peer = grpc_call_get_peer(c);
216 GPR_ASSERT(peer != nullptr);
217 gpr_log(GPR_DEBUG, "client_peer=%s", peer);
218 gpr_free(peer);
219
220 // Server receives a message.
221 memset(ops, 0, sizeof(ops));
222 op = ops;
223 op->op = GRPC_OP_RECV_MESSAGE;
224 op->data.recv_message.recv_message = &request_payload_recv;
225 op++;
226 error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), nullptr);
227 GPR_ASSERT(GRPC_CALL_OK == error);
228 CQ_EXPECT_COMPLETION(cqv, tag(102), true);
229 cq_verify(cqv);
230
231 // Client sends a second message.
232 memset(ops, 0, sizeof(ops));
233 op = ops;
234 op->op = GRPC_OP_SEND_MESSAGE;
235 op->data.send_message.send_message = request2_payload;
236 op++;
237 error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(3), nullptr);
238 GPR_ASSERT(GRPC_CALL_OK == error);
239 CQ_EXPECT_COMPLETION(cqv, tag(3), true);
240 cq_verify(cqv);
241
242 // Server receives the second message.
243 memset(ops, 0, sizeof(ops));
244 op = ops;
245 op->op = GRPC_OP_RECV_MESSAGE;
246 op->data.recv_message.recv_message = &request2_payload_recv;
247 op++;
248 error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(103), nullptr);
249 GPR_ASSERT(GRPC_CALL_OK == error);
250 CQ_EXPECT_COMPLETION(cqv, tag(103), true);
251 cq_verify(cqv);
252
253 // Client sends a third message.
254 memset(ops, 0, sizeof(ops));
255 op = ops;
256 op->op = GRPC_OP_SEND_MESSAGE;
257 op->data.send_message.send_message = request3_payload;
258 op++;
259 error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(4), nullptr);
260 GPR_ASSERT(GRPC_CALL_OK == error);
261 CQ_EXPECT_COMPLETION(cqv, tag(4), true);
262 cq_verify(cqv);
263
264 // Server receives the third message.
265 memset(ops, 0, sizeof(ops));
266 op = ops;
267 op->op = GRPC_OP_RECV_MESSAGE;
268 op->data.recv_message.recv_message = &request3_payload_recv;
269 op++;
270 error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(104), nullptr);
271 GPR_ASSERT(GRPC_CALL_OK == error);
272 CQ_EXPECT_COMPLETION(cqv, tag(104), true);
273 cq_verify(cqv);
274
275 // Server sends both initial and trailing metadata.
276 memset(ops, 0, sizeof(ops));
277 op = ops;
278 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
279 op->data.recv_close_on_server.cancelled = &was_cancelled;
280 op++;
281 op->op = GRPC_OP_SEND_INITIAL_METADATA;
282 op->data.send_initial_metadata.count = 0;
283 op++;
284 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
285 op->data.send_status_from_server.trailing_metadata_count = 0;
286 op->data.send_status_from_server.status = GRPC_STATUS_ABORTED;
287 op->data.send_status_from_server.status_details = &status_details;
288 op++;
289 error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(105), nullptr);
290 GPR_ASSERT(GRPC_CALL_OK == error);
291 CQ_EXPECT_COMPLETION(cqv, tag(105), true);
292 cq_verify(cqv);
293
294 // Clean up from first attempt.
295 grpc_call_unref(s);
296 grpc_metadata_array_destroy(&request_metadata_recv);
297 grpc_metadata_array_init(&request_metadata_recv);
298 grpc_call_details_destroy(&call_details);
299 grpc_call_details_init(&call_details);
300 GPR_ASSERT(byte_buffer_eq_slice(request_payload_recv, request_payload_slice));
301 grpc_byte_buffer_destroy(request_payload_recv);
302 request_payload_recv = nullptr;
303 GPR_ASSERT(
304 byte_buffer_eq_slice(request2_payload_recv, request2_payload_slice));
305 grpc_byte_buffer_destroy(request2_payload_recv);
306 request2_payload_recv = nullptr;
307 GPR_ASSERT(
308 byte_buffer_eq_slice(request3_payload_recv, request3_payload_slice));
309 grpc_byte_buffer_destroy(request3_payload_recv);
310 request3_payload_recv = nullptr;
311
312 // Server gets a second call (the retry).
313 error =
314 grpc_server_request_call(f.server, &s, &call_details,
315 &request_metadata_recv, f.cq, f.cq, tag(201));
316 GPR_ASSERT(GRPC_CALL_OK == error);
317 CQ_EXPECT_COMPLETION(cqv, tag(201), true);
318 cq_verify(cqv);
319
320 peer = grpc_call_get_peer(s);
321 GPR_ASSERT(peer != nullptr);
322 gpr_log(GPR_DEBUG, "server_peer=%s", peer);
323 gpr_free(peer);
324 peer = grpc_call_get_peer(c);
325 GPR_ASSERT(peer != nullptr);
326 gpr_log(GPR_DEBUG, "client_peer=%s", peer);
327 gpr_free(peer);
328
329 // Server receives the first message (and does not receive any others).
330 memset(ops, 0, sizeof(ops));
331 op = ops;
332 op->op = GRPC_OP_RECV_MESSAGE;
333 op->data.recv_message.recv_message = &request_payload_recv;
334 op++;
335 error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(202), nullptr);
336 GPR_ASSERT(GRPC_CALL_OK == error);
337 CQ_EXPECT_COMPLETION(cqv, tag(202), true);
338 cq_verify(cqv);
339
340 // Server sends initial metadata, a message, and trailing metadata.
341 memset(ops, 0, sizeof(ops));
342 op = ops;
343 op->op = GRPC_OP_SEND_INITIAL_METADATA;
344 op->data.send_initial_metadata.count = 0;
345 op++;
346 op->op = GRPC_OP_SEND_MESSAGE;
347 op->data.send_message.send_message = response_payload;
348 op++;
349 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
350 op->data.send_status_from_server.trailing_metadata_count = 0;
351 // Returning a retriable code, but because we are also sending a
352 // message, the client will commit instead of retrying again.
353 op->data.send_status_from_server.status = GRPC_STATUS_ABORTED;
354 op->data.send_status_from_server.status_details = &status_details;
355 op++;
356 error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(205), nullptr);
357 GPR_ASSERT(GRPC_CALL_OK == error);
358 CQ_EXPECT_COMPLETION(cqv, tag(205), true);
359 CQ_EXPECT_COMPLETION(cqv, tag(1), true);
360 cq_verify(cqv);
361
362 GPR_ASSERT(status == GRPC_STATUS_ABORTED);
363 GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz"));
364 GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/service/method"));
365 GPR_ASSERT(0 == call_details.flags);
366 GPR_ASSERT(was_cancelled == 1);
367
368 grpc_slice_unref(details);
369 grpc_metadata_array_destroy(&initial_metadata_recv);
370 grpc_metadata_array_destroy(&trailing_metadata_recv);
371 grpc_metadata_array_destroy(&request_metadata_recv);
372 grpc_call_details_destroy(&call_details);
373 grpc_byte_buffer_destroy(request_payload);
374 grpc_byte_buffer_destroy(request2_payload);
375 grpc_byte_buffer_destroy(request3_payload);
376 grpc_byte_buffer_destroy(response_payload);
377 GPR_ASSERT(byte_buffer_eq_slice(request_payload_recv, request_payload_slice));
378 grpc_byte_buffer_destroy(request_payload_recv);
379 grpc_byte_buffer_destroy(response_payload_recv);
380
381 grpc_call_unref(c);
382 grpc_call_unref(s);
383
384 cq_verifier_destroy(cqv);
385
386 end_test(&f);
387 config.tear_down_data(&f);
388 }
389
retry_streaming_succeeds_before_replay_finished(grpc_end2end_test_config config)390 void retry_streaming_succeeds_before_replay_finished(
391 grpc_end2end_test_config config) {
392 GPR_ASSERT(config.feature_mask & FEATURE_MASK_SUPPORTS_CLIENT_CHANNEL);
393 test_retry_streaming_succeeds_before_replay_finished(config);
394 }
395
retry_streaming_succeeds_before_replay_finished_pre_init(void)396 void retry_streaming_succeeds_before_replay_finished_pre_init(void) {}
397