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 retrying a streaming RPC. This is the same as
97 // the basic retry test, except that the client sends two messages on the
98 // call before the initial attempt fails.
99 // FIXME: We should also test the case where the retry is committed after
100 // replaying 1 of 2 previously-completed send_message ops. However,
101 // there's no way to trigger that from an end2end test, because the
102 // replayed ops happen under the hood -- they are not surfaced to the
103 // C-core API, and therefore we have no way to inject the commit at the
104 // right point.
test_retry_streaming(grpc_end2end_test_config config)105 static void test_retry_streaming(grpc_end2end_test_config config) {
106 grpc_call* c;
107 grpc_call* s;
108 grpc_op ops[6];
109 grpc_op* op;
110 grpc_metadata_array initial_metadata_recv;
111 grpc_metadata_array trailing_metadata_recv;
112 grpc_metadata_array request_metadata_recv;
113 grpc_call_details call_details;
114 grpc_slice request_payload_slice = grpc_slice_from_static_string("foo");
115 grpc_slice request2_payload_slice = grpc_slice_from_static_string("bar");
116 grpc_slice request3_payload_slice = grpc_slice_from_static_string("baz");
117 grpc_slice response_payload_slice = grpc_slice_from_static_string("quux");
118 grpc_byte_buffer* request_payload =
119 grpc_raw_byte_buffer_create(&request_payload_slice, 1);
120 grpc_byte_buffer* request2_payload =
121 grpc_raw_byte_buffer_create(&request2_payload_slice, 1);
122 grpc_byte_buffer* request3_payload =
123 grpc_raw_byte_buffer_create(&request3_payload_slice, 1);
124 grpc_byte_buffer* response_payload =
125 grpc_raw_byte_buffer_create(&response_payload_slice, 1);
126 grpc_byte_buffer* request_payload_recv = nullptr;
127 grpc_byte_buffer* request2_payload_recv = nullptr;
128 grpc_byte_buffer* request3_payload_recv = nullptr;
129 grpc_byte_buffer* response_payload_recv = nullptr;
130 grpc_status_code status;
131 grpc_call_error error;
132 grpc_slice details;
133 int was_cancelled = 2;
134 char* peer;
135
136 grpc_arg arg;
137 arg.type = GRPC_ARG_STRING;
138 arg.key = const_cast<char*>(GRPC_ARG_SERVICE_CONFIG);
139 arg.value.string = const_cast<char*>(
140 "{\n"
141 " \"methodConfig\": [ {\n"
142 " \"name\": [\n"
143 " { \"service\": \"service\", \"method\": \"method\" }\n"
144 " ],\n"
145 " \"retryPolicy\": {\n"
146 " \"maxAttempts\": 3,\n"
147 " \"initialBackoff\": \"1s\",\n"
148 " \"maxBackoff\": \"120s\",\n"
149 " \"backoffMultiplier\": 1.6,\n"
150 " \"retryableStatusCodes\": [ \"ABORTED\" ]\n"
151 " }\n"
152 " } ]\n"
153 "}");
154 grpc_channel_args client_args = {1, &arg};
155 grpc_end2end_test_fixture f =
156 begin_test(config, "retry_streaming", &client_args, nullptr);
157
158 cq_verifier* cqv = cq_verifier_create(f.cq);
159
160 gpr_timespec deadline = five_seconds_from_now();
161 c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
162 grpc_slice_from_static_string("/service/method"),
163 nullptr, deadline, nullptr);
164 GPR_ASSERT(c);
165
166 peer = grpc_call_get_peer(c);
167 GPR_ASSERT(peer != nullptr);
168 gpr_log(GPR_DEBUG, "client_peer_before_call=%s", peer);
169 gpr_free(peer);
170
171 grpc_metadata_array_init(&initial_metadata_recv);
172 grpc_metadata_array_init(&trailing_metadata_recv);
173 grpc_metadata_array_init(&request_metadata_recv);
174 grpc_call_details_init(&call_details);
175 grpc_slice status_details = grpc_slice_from_static_string("xyz");
176
177 // Client starts a batch for receiving initial metadata, a message,
178 // and trailing metadata.
179 memset(ops, 0, sizeof(ops));
180 op = ops;
181 op->op = GRPC_OP_RECV_INITIAL_METADATA;
182 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
183 op++;
184 op->op = GRPC_OP_RECV_MESSAGE;
185 op->data.recv_message.recv_message = &response_payload_recv;
186 op++;
187 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
188 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
189 op->data.recv_status_on_client.status = &status;
190 op->data.recv_status_on_client.status_details = &details;
191 op++;
192 error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), nullptr);
193 GPR_ASSERT(GRPC_CALL_OK == error);
194
195 // Client sends initial metadata and a message.
196 memset(ops, 0, sizeof(ops));
197 op = ops;
198 op->op = GRPC_OP_SEND_INITIAL_METADATA;
199 op->data.send_initial_metadata.count = 0;
200 op++;
201 op->op = GRPC_OP_SEND_MESSAGE;
202 op->data.send_message.send_message = request_payload;
203 op++;
204 error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(2), nullptr);
205 GPR_ASSERT(GRPC_CALL_OK == error);
206 CQ_EXPECT_COMPLETION(cqv, tag(2), true);
207 cq_verify(cqv);
208
209 // Server gets a call with received initial metadata.
210 error =
211 grpc_server_request_call(f.server, &s, &call_details,
212 &request_metadata_recv, f.cq, f.cq, tag(101));
213 GPR_ASSERT(GRPC_CALL_OK == error);
214 CQ_EXPECT_COMPLETION(cqv, tag(101), true);
215 cq_verify(cqv);
216
217 peer = grpc_call_get_peer(s);
218 GPR_ASSERT(peer != nullptr);
219 gpr_log(GPR_DEBUG, "server_peer=%s", peer);
220 gpr_free(peer);
221 peer = grpc_call_get_peer(c);
222 GPR_ASSERT(peer != nullptr);
223 gpr_log(GPR_DEBUG, "client_peer=%s", peer);
224 gpr_free(peer);
225
226 // Server receives a message.
227 memset(ops, 0, sizeof(ops));
228 op = ops;
229 op->op = GRPC_OP_RECV_MESSAGE;
230 op->data.recv_message.recv_message = &request_payload_recv;
231 op++;
232 error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), nullptr);
233 GPR_ASSERT(GRPC_CALL_OK == error);
234 CQ_EXPECT_COMPLETION(cqv, tag(102), true);
235 cq_verify(cqv);
236
237 // Client sends a second message.
238 memset(ops, 0, sizeof(ops));
239 op = ops;
240 op->op = GRPC_OP_SEND_MESSAGE;
241 op->data.send_message.send_message = request2_payload;
242 op++;
243 error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(3), nullptr);
244 GPR_ASSERT(GRPC_CALL_OK == error);
245 CQ_EXPECT_COMPLETION(cqv, tag(3), true);
246 cq_verify(cqv);
247
248 // Server receives the second message.
249 memset(ops, 0, sizeof(ops));
250 op = ops;
251 op->op = GRPC_OP_RECV_MESSAGE;
252 op->data.recv_message.recv_message = &request2_payload_recv;
253 op++;
254 error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(103), nullptr);
255 GPR_ASSERT(GRPC_CALL_OK == error);
256 CQ_EXPECT_COMPLETION(cqv, tag(103), true);
257 cq_verify(cqv);
258
259 // Server sends both initial and trailing metadata.
260 memset(ops, 0, sizeof(ops));
261 op = ops;
262 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
263 op->data.recv_close_on_server.cancelled = &was_cancelled;
264 op++;
265 op->op = GRPC_OP_SEND_INITIAL_METADATA;
266 op->data.send_initial_metadata.count = 0;
267 op++;
268 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
269 op->data.send_status_from_server.trailing_metadata_count = 0;
270 op->data.send_status_from_server.status = GRPC_STATUS_ABORTED;
271 op->data.send_status_from_server.status_details = &status_details;
272 op++;
273 error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(104), nullptr);
274 GPR_ASSERT(GRPC_CALL_OK == error);
275 CQ_EXPECT_COMPLETION(cqv, tag(104), true);
276 cq_verify(cqv);
277
278 // Clean up from first attempt.
279 grpc_call_unref(s);
280 grpc_metadata_array_destroy(&request_metadata_recv);
281 grpc_metadata_array_init(&request_metadata_recv);
282 grpc_call_details_destroy(&call_details);
283 grpc_call_details_init(&call_details);
284 GPR_ASSERT(byte_buffer_eq_slice(request_payload_recv, request_payload_slice));
285 grpc_byte_buffer_destroy(request_payload_recv);
286 request_payload_recv = nullptr;
287 GPR_ASSERT(
288 byte_buffer_eq_slice(request2_payload_recv, request2_payload_slice));
289 grpc_byte_buffer_destroy(request2_payload_recv);
290 request2_payload_recv = nullptr;
291
292 // Server gets a second call (the retry).
293 error =
294 grpc_server_request_call(f.server, &s, &call_details,
295 &request_metadata_recv, f.cq, f.cq, tag(201));
296 GPR_ASSERT(GRPC_CALL_OK == error);
297 CQ_EXPECT_COMPLETION(cqv, tag(201), true);
298 cq_verify(cqv);
299
300 peer = grpc_call_get_peer(s);
301 GPR_ASSERT(peer != nullptr);
302 gpr_log(GPR_DEBUG, "server_peer=%s", peer);
303 gpr_free(peer);
304 peer = grpc_call_get_peer(c);
305 GPR_ASSERT(peer != nullptr);
306 gpr_log(GPR_DEBUG, "client_peer=%s", peer);
307 gpr_free(peer);
308
309 // Server receives a message.
310 memset(ops, 0, sizeof(ops));
311 op = ops;
312 op->op = GRPC_OP_RECV_MESSAGE;
313 op->data.recv_message.recv_message = &request_payload_recv;
314 op++;
315 error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(202), nullptr);
316 GPR_ASSERT(GRPC_CALL_OK == error);
317 CQ_EXPECT_COMPLETION(cqv, tag(202), true);
318 cq_verify(cqv);
319
320 // Server receives a second message.
321 memset(ops, 0, sizeof(ops));
322 op = ops;
323 op->op = GRPC_OP_RECV_MESSAGE;
324 op->data.recv_message.recv_message = &request2_payload_recv;
325 op++;
326 error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(203), nullptr);
327 GPR_ASSERT(GRPC_CALL_OK == error);
328 CQ_EXPECT_COMPLETION(cqv, tag(203), true);
329 cq_verify(cqv);
330
331 // Client sends a third message and a close.
332 memset(ops, 0, sizeof(ops));
333 op = ops;
334 op->op = GRPC_OP_SEND_MESSAGE;
335 op->data.send_message.send_message = request3_payload;
336 op++;
337 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
338 op++;
339 error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(4), nullptr);
340 GPR_ASSERT(GRPC_CALL_OK == error);
341 CQ_EXPECT_COMPLETION(cqv, tag(4), true);
342 cq_verify(cqv);
343
344 // Server receives a third message.
345 memset(ops, 0, sizeof(ops));
346 op = ops;
347 op->op = GRPC_OP_RECV_MESSAGE;
348 op->data.recv_message.recv_message = &request3_payload_recv;
349 op++;
350 error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(204), nullptr);
351 GPR_ASSERT(GRPC_CALL_OK == error);
352 CQ_EXPECT_COMPLETION(cqv, tag(204), true);
353 cq_verify(cqv);
354
355 // Server receives a close and sends initial metadata, a message, and
356 // trailing metadata.
357 memset(ops, 0, sizeof(ops));
358 op = ops;
359 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
360 op->data.recv_close_on_server.cancelled = &was_cancelled;
361 op++;
362 op->op = GRPC_OP_SEND_INITIAL_METADATA;
363 op->data.send_initial_metadata.count = 0;
364 op++;
365 op->op = GRPC_OP_SEND_MESSAGE;
366 op->data.send_message.send_message = response_payload;
367 op++;
368 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
369 op->data.send_status_from_server.trailing_metadata_count = 0;
370 // Returning a retriable code, but because we are also sending a
371 // message, the client will commit instead of retrying again.
372 op->data.send_status_from_server.status = GRPC_STATUS_ABORTED;
373 op->data.send_status_from_server.status_details = &status_details;
374 op++;
375 error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(205), nullptr);
376 GPR_ASSERT(GRPC_CALL_OK == error);
377 CQ_EXPECT_COMPLETION(cqv, tag(205), true);
378 CQ_EXPECT_COMPLETION(cqv, tag(1), true);
379 cq_verify(cqv);
380
381 GPR_ASSERT(status == GRPC_STATUS_ABORTED);
382 GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz"));
383 GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/service/method"));
384 GPR_ASSERT(0 == call_details.flags);
385 GPR_ASSERT(was_cancelled == 1);
386
387 grpc_slice_unref(details);
388 grpc_metadata_array_destroy(&initial_metadata_recv);
389 grpc_metadata_array_destroy(&trailing_metadata_recv);
390 grpc_metadata_array_destroy(&request_metadata_recv);
391 grpc_call_details_destroy(&call_details);
392 grpc_byte_buffer_destroy(request_payload);
393 grpc_byte_buffer_destroy(request2_payload);
394 grpc_byte_buffer_destroy(request3_payload);
395 grpc_byte_buffer_destroy(response_payload);
396 GPR_ASSERT(byte_buffer_eq_slice(request_payload_recv, request_payload_slice));
397 grpc_byte_buffer_destroy(request_payload_recv);
398 GPR_ASSERT(
399 byte_buffer_eq_slice(request2_payload_recv, request2_payload_slice));
400 grpc_byte_buffer_destroy(request2_payload_recv);
401 GPR_ASSERT(
402 byte_buffer_eq_slice(request3_payload_recv, request3_payload_slice));
403 grpc_byte_buffer_destroy(request3_payload_recv);
404 grpc_byte_buffer_destroy(response_payload_recv);
405
406 grpc_call_unref(c);
407 grpc_call_unref(s);
408
409 cq_verifier_destroy(cqv);
410
411 end_test(&f);
412 config.tear_down_data(&f);
413 }
414
retry_streaming(grpc_end2end_test_config config)415 void retry_streaming(grpc_end2end_test_config config) {
416 GPR_ASSERT(config.feature_mask & FEATURE_MASK_SUPPORTS_CLIENT_CHANNEL);
417 test_retry_streaming(config);
418 }
419
retry_streaming_pre_init(void)420 void retry_streaming_pre_init(void) {}
421