• 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/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 can continue to send/recv messages on a streaming call
97 // after retries are committed.
test_retry_streaming_after_commit(grpc_end2end_test_config config)98 static void test_retry_streaming_after_commit(grpc_end2end_test_config config) {
99   grpc_call* c;
100   grpc_call* s;
101   grpc_op ops[6];
102   grpc_op* op;
103   grpc_metadata_array initial_metadata_recv;
104   grpc_metadata_array trailing_metadata_recv;
105   grpc_metadata_array request_metadata_recv;
106   grpc_call_details call_details;
107   grpc_slice request_payload_slice = grpc_slice_from_static_string("foo");
108   grpc_slice request2_payload_slice = grpc_slice_from_static_string("bar");
109   grpc_slice response_payload_slice = grpc_slice_from_static_string("baz");
110   grpc_slice response2_payload_slice = grpc_slice_from_static_string("quux");
111   grpc_byte_buffer* request_payload =
112       grpc_raw_byte_buffer_create(&request_payload_slice, 1);
113   grpc_byte_buffer* request2_payload =
114       grpc_raw_byte_buffer_create(&request2_payload_slice, 1);
115   grpc_byte_buffer* response_payload =
116       grpc_raw_byte_buffer_create(&response_payload_slice, 1);
117   grpc_byte_buffer* response2_payload =
118       grpc_raw_byte_buffer_create(&response2_payload_slice, 1);
119   grpc_byte_buffer* request_payload_recv = nullptr;
120   grpc_byte_buffer* request2_payload_recv = nullptr;
121   grpc_byte_buffer* response_payload_recv = nullptr;
122   grpc_byte_buffer* response2_payload_recv = nullptr;
123   grpc_status_code status;
124   grpc_call_error error;
125   grpc_slice details;
126   int was_cancelled = 2;
127   char* peer;
128 
129   grpc_arg arg;
130   arg.type = GRPC_ARG_STRING;
131   arg.key = const_cast<char*>(GRPC_ARG_SERVICE_CONFIG);
132   arg.value.string = const_cast<char*>(
133       "{\n"
134       "  \"methodConfig\": [ {\n"
135       "    \"name\": [\n"
136       "      { \"service\": \"service\", \"method\": \"method\" }\n"
137       "    ],\n"
138       "    \"retryPolicy\": {\n"
139       "      \"maxAttempts\": 3,\n"
140       "      \"initialBackoff\": \"1s\",\n"
141       "      \"maxBackoff\": \"120s\",\n"
142       "      \"backoffMultiplier\": 1.6,\n"
143       "      \"retryableStatusCodes\": [ \"ABORTED\" ]\n"
144       "    }\n"
145       "  } ]\n"
146       "}");
147   grpc_channel_args client_args = {1, &arg};
148   grpc_end2end_test_fixture f =
149       begin_test(config, "retry_streaming_after_commit", &client_args, nullptr);
150 
151   cq_verifier* cqv = cq_verifier_create(f.cq);
152 
153   gpr_timespec deadline = five_seconds_from_now();
154   c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
155                                grpc_slice_from_static_string("/service/method"),
156                                nullptr, deadline, nullptr);
157   GPR_ASSERT(c);
158 
159   peer = grpc_call_get_peer(c);
160   GPR_ASSERT(peer != nullptr);
161   gpr_log(GPR_DEBUG, "client_peer_before_call=%s", peer);
162   gpr_free(peer);
163 
164   grpc_metadata_array_init(&initial_metadata_recv);
165   grpc_metadata_array_init(&trailing_metadata_recv);
166   grpc_metadata_array_init(&request_metadata_recv);
167   grpc_call_details_init(&call_details);
168   grpc_slice status_details = grpc_slice_from_static_string("xyz");
169 
170   // Client starts a batch for receiving initial metadata and a message.
171   // This will commit retries.
172   memset(ops, 0, sizeof(ops));
173   op = ops;
174   op->op = GRPC_OP_RECV_INITIAL_METADATA;
175   op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
176   op++;
177   op->op = GRPC_OP_RECV_MESSAGE;
178   op->data.recv_message.recv_message = &response_payload_recv;
179   op++;
180   error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(2), nullptr);
181   GPR_ASSERT(GRPC_CALL_OK == error);
182 
183   // Client sends initial metadata and a message.
184   memset(ops, 0, sizeof(ops));
185   op = ops;
186   op->op = GRPC_OP_SEND_INITIAL_METADATA;
187   op->data.send_initial_metadata.count = 0;
188   op++;
189   op->op = GRPC_OP_SEND_MESSAGE;
190   op->data.send_message.send_message = request_payload;
191   op++;
192   error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(3), nullptr);
193   GPR_ASSERT(GRPC_CALL_OK == error);
194   CQ_EXPECT_COMPLETION(cqv, tag(3), true);
195   cq_verify(cqv);
196 
197   // Server gets a call with received initial metadata.
198   error =
199       grpc_server_request_call(f.server, &s, &call_details,
200                                &request_metadata_recv, f.cq, f.cq, tag(101));
201   GPR_ASSERT(GRPC_CALL_OK == error);
202   CQ_EXPECT_COMPLETION(cqv, tag(101), true);
203   cq_verify(cqv);
204 
205   peer = grpc_call_get_peer(s);
206   GPR_ASSERT(peer != nullptr);
207   gpr_log(GPR_DEBUG, "server_peer=%s", peer);
208   gpr_free(peer);
209   peer = grpc_call_get_peer(c);
210   GPR_ASSERT(peer != nullptr);
211   gpr_log(GPR_DEBUG, "client_peer=%s", peer);
212   gpr_free(peer);
213 
214   // Server receives a message.
215   memset(ops, 0, sizeof(ops));
216   op = ops;
217   op->op = GRPC_OP_RECV_MESSAGE;
218   op->data.recv_message.recv_message = &request_payload_recv;
219   op++;
220   error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), nullptr);
221   GPR_ASSERT(GRPC_CALL_OK == error);
222   CQ_EXPECT_COMPLETION(cqv, tag(102), true);
223   cq_verify(cqv);
224 
225   // Server sends initial metadata and a message.
226   memset(ops, 0, sizeof(ops));
227   op = ops;
228   op->op = GRPC_OP_SEND_INITIAL_METADATA;
229   op->data.send_initial_metadata.count = 0;
230   op++;
231   op->op = GRPC_OP_SEND_MESSAGE;
232   op->data.send_message.send_message = response_payload;
233   op++;
234   error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(103), nullptr);
235   GPR_ASSERT(GRPC_CALL_OK == error);
236   CQ_EXPECT_COMPLETION(cqv, tag(103), true);
237   // Client receives initial metadata and a message.
238   CQ_EXPECT_COMPLETION(cqv, tag(2), true);
239   cq_verify(cqv);
240 
241   // Client sends a second message and a close.
242   memset(ops, 0, sizeof(ops));
243   op = ops;
244   op->op = GRPC_OP_SEND_MESSAGE;
245   op->data.send_message.send_message = request2_payload;
246   op++;
247   op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
248   op++;
249   error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(4), nullptr);
250   GPR_ASSERT(GRPC_CALL_OK == error);
251   CQ_EXPECT_COMPLETION(cqv, tag(4), true);
252   cq_verify(cqv);
253 
254   // Server receives a second message.
255   memset(ops, 0, sizeof(ops));
256   op = ops;
257   op->op = GRPC_OP_RECV_MESSAGE;
258   op->data.recv_message.recv_message = &request2_payload_recv;
259   op++;
260   error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(104), nullptr);
261   GPR_ASSERT(GRPC_CALL_OK == error);
262   CQ_EXPECT_COMPLETION(cqv, tag(104), true);
263   cq_verify(cqv);
264 
265   // Server receives a close, sends a second message, and sends status.
266   memset(ops, 0, sizeof(ops));
267   op = ops;
268   op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
269   op->data.recv_close_on_server.cancelled = &was_cancelled;
270   op++;
271   op->op = GRPC_OP_SEND_MESSAGE;
272   op->data.send_message.send_message = response2_payload;
273   op++;
274   op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
275   op->data.send_status_from_server.trailing_metadata_count = 0;
276   // Returning a retriable code, but because retries are already
277   // committed, the client will not retry.
278   op->data.send_status_from_server.status = GRPC_STATUS_ABORTED;
279   op->data.send_status_from_server.status_details = &status_details;
280   op++;
281   error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(105), nullptr);
282   GPR_ASSERT(GRPC_CALL_OK == error);
283   CQ_EXPECT_COMPLETION(cqv, tag(105), true);
284   cq_verify(cqv);
285 
286   // Client receives a second message.
287   memset(ops, 0, sizeof(ops));
288   op = ops;
289   op->op = GRPC_OP_RECV_MESSAGE;
290   op->data.recv_message.recv_message = &response2_payload_recv;
291   op++;
292   error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(5), nullptr);
293   GPR_ASSERT(GRPC_CALL_OK == error);
294   CQ_EXPECT_COMPLETION(cqv, tag(5), true);
295   cq_verify(cqv);
296 
297   // Client receives status.
298   memset(ops, 0, sizeof(ops));
299   op = ops;
300   op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
301   op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
302   op->data.recv_status_on_client.status = &status;
303   op->data.recv_status_on_client.status_details = &details;
304   op++;
305   error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), nullptr);
306   GPR_ASSERT(GRPC_CALL_OK == error);
307   CQ_EXPECT_COMPLETION(cqv, tag(1), true);
308   cq_verify(cqv);
309 
310   GPR_ASSERT(status == GRPC_STATUS_ABORTED);
311   GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz"));
312   GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/service/method"));
313   GPR_ASSERT(0 == call_details.flags);
314   GPR_ASSERT(was_cancelled == 1);
315 
316   grpc_slice_unref(details);
317   grpc_metadata_array_destroy(&initial_metadata_recv);
318   grpc_metadata_array_destroy(&trailing_metadata_recv);
319   grpc_metadata_array_destroy(&request_metadata_recv);
320   grpc_call_details_destroy(&call_details);
321   grpc_byte_buffer_destroy(request_payload);
322   grpc_byte_buffer_destroy(request2_payload);
323   grpc_byte_buffer_destroy(response_payload);
324   grpc_byte_buffer_destroy(response2_payload);
325   GPR_ASSERT(byte_buffer_eq_slice(request_payload_recv, request_payload_slice));
326   grpc_byte_buffer_destroy(request_payload_recv);
327   GPR_ASSERT(
328       byte_buffer_eq_slice(request2_payload_recv, request2_payload_slice));
329   grpc_byte_buffer_destroy(request2_payload_recv);
330   GPR_ASSERT(
331       byte_buffer_eq_slice(response_payload_recv, response_payload_slice));
332   grpc_byte_buffer_destroy(response_payload_recv);
333   GPR_ASSERT(
334       byte_buffer_eq_slice(response2_payload_recv, response2_payload_slice));
335   grpc_byte_buffer_destroy(response2_payload_recv);
336   grpc_call_unref(c);
337   grpc_call_unref(s);
338 
339   cq_verifier_destroy(cqv);
340 
341   end_test(&f);
342   config.tear_down_data(&f);
343 }
344 
retry_streaming_after_commit(grpc_end2end_test_config config)345 void retry_streaming_after_commit(grpc_end2end_test_config config) {
346   GPR_ASSERT(config.feature_mask & FEATURE_MASK_SUPPORTS_CLIENT_CHANNEL);
347   test_retry_streaming_after_commit(config);
348 }
349 
retry_streaming_after_commit_pre_init(void)350 void retry_streaming_after_commit_pre_init(void) {}
351