1 /*
2 *
3 * Copyright 2018 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 <stdio.h>
20 #include <string.h>
21
22 #include <grpc/byte_buffer.h>
23 #include <grpc/grpc.h>
24 #include <grpc/support/alloc.h>
25 #include <grpc/support/log.h>
26 #include <grpc/support/string_util.h>
27 #include <grpc/support/time.h>
28
29 #include "src/core/lib/channel/channel_args.h"
30 #include "src/core/lib/gpr/string.h"
31 #include "src/core/lib/gpr/useful.h"
32 #include "src/core/lib/iomgr/exec_ctx.h"
33 #include "src/core/lib/transport/static_metadata.h"
34 #include "test/core/end2end/cq_verifier.h"
35 #include "test/core/end2end/end2end_tests.h"
36 #include "test/core/end2end/tests/cancel_test_helpers.h"
37
tag(intptr_t t)38 static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); }
39
begin_test(grpc_end2end_test_config config,const char * test_name,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 grpc_channel_args* client_args,
43 grpc_channel_args* server_args) {
44 grpc_end2end_test_fixture f;
45 gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name);
46 f = config.create_fixture(client_args, server_args);
47 config.init_server(&f, server_args);
48 config.init_client(&f, client_args);
49 return f;
50 }
51
n_seconds_from_now(int n)52 static gpr_timespec n_seconds_from_now(int n) {
53 return grpc_timeout_seconds_to_deadline(n);
54 }
55
five_seconds_from_now(void)56 static gpr_timespec five_seconds_from_now(void) {
57 return n_seconds_from_now(5);
58 }
59
drain_cq(grpc_completion_queue * cq)60 static void drain_cq(grpc_completion_queue* cq) {
61 grpc_event ev;
62 do {
63 ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr);
64 } while (ev.type != GRPC_QUEUE_SHUTDOWN);
65 }
66
shutdown_server(grpc_end2end_test_fixture * f)67 static void shutdown_server(grpc_end2end_test_fixture* f) {
68 if (!f->server) return;
69 grpc_server_shutdown_and_notify(f->server, f->shutdown_cq, tag(1000));
70 GPR_ASSERT(grpc_completion_queue_pluck(f->shutdown_cq, tag(1000),
71 grpc_timeout_seconds_to_deadline(5),
72 nullptr)
73 .type == GRPC_OP_COMPLETE);
74 grpc_server_destroy(f->server);
75 f->server = nullptr;
76 }
77
shutdown_client(grpc_end2end_test_fixture * f)78 static void shutdown_client(grpc_end2end_test_fixture* f) {
79 if (!f->client) return;
80 grpc_channel_destroy(f->client);
81 f->client = nullptr;
82 }
83
end_test(grpc_end2end_test_fixture * f)84 static void end_test(grpc_end2end_test_fixture* f) {
85 shutdown_server(f);
86 shutdown_client(f);
87
88 grpc_completion_queue_shutdown(f->cq);
89 drain_cq(f->cq);
90 grpc_completion_queue_destroy(f->cq);
91 grpc_completion_queue_destroy(f->shutdown_cq);
92 }
93
94 // Tests that we don't retry for non-retryable status codes, even if
95 // status is received before the recv_trailing_metadata op is started.
96 // - 1 retry allowed for ABORTED status
97 // - first attempt gets INVALID_ARGUMENT, so no retry is done
98 static void
test_retry_non_retriable_status_before_recv_trailing_metadata_started(grpc_end2end_test_config config)99 test_retry_non_retriable_status_before_recv_trailing_metadata_started(
100 grpc_end2end_test_config config) {
101 grpc_call* c;
102 grpc_call* s;
103 grpc_op ops[6];
104 grpc_op* op;
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_slice request_payload_slice = grpc_slice_from_static_string("foo");
110 grpc_slice response_payload_slice = grpc_slice_from_static_string("bar");
111 grpc_byte_buffer* request_payload =
112 grpc_raw_byte_buffer_create(&request_payload_slice, 1);
113 grpc_byte_buffer* response_payload =
114 grpc_raw_byte_buffer_create(&response_payload_slice, 1);
115 grpc_byte_buffer* request_payload_recv = nullptr;
116 grpc_byte_buffer* response_payload_recv = nullptr;
117 grpc_status_code status;
118 grpc_call_error error;
119 grpc_slice details;
120 int was_cancelled = 2;
121 char* peer;
122
123 grpc_arg args[] = {
124 grpc_channel_arg_string_create(
125 const_cast<char*>(GRPC_ARG_SERVICE_CONFIG),
126 const_cast<char*>(
127 "{\n"
128 " \"methodConfig\": [ {\n"
129 " \"name\": [\n"
130 " { \"service\": \"service\", \"method\": \"method\" }\n"
131 " ],\n"
132 " \"retryPolicy\": {\n"
133 " \"maxAttempts\": 2,\n"
134 " \"initialBackoff\": \"1s\",\n"
135 " \"maxBackoff\": \"120s\",\n"
136 " \"backoffMultiplier\": 1.6,\n"
137 " \"retryableStatusCodes\": [ \"ABORTED\" ]\n"
138 " }\n"
139 " } ]\n"
140 "}")),
141 };
142 grpc_channel_args client_args = {GPR_ARRAY_SIZE(args), args};
143 grpc_end2end_test_fixture f = begin_test(
144 config,
145 "retry_non_retriable_status_before_recv_trailing_metadata_started",
146 &client_args, nullptr);
147
148 cq_verifier* cqv = cq_verifier_create(f.cq);
149
150 gpr_timespec deadline = 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 peer = grpc_call_get_peer(c);
157 GPR_ASSERT(peer != nullptr);
158 gpr_log(GPR_DEBUG, "client_peer_before_call=%s", peer);
159 gpr_free(peer);
160
161 grpc_metadata_array_init(&initial_metadata_recv);
162 grpc_metadata_array_init(&trailing_metadata_recv);
163 grpc_metadata_array_init(&request_metadata_recv);
164 grpc_call_details_init(&call_details);
165 grpc_slice status_details = grpc_slice_from_static_string("xyz");
166
167 memset(ops, 0, sizeof(ops));
168 op = ops;
169 op->op = GRPC_OP_SEND_INITIAL_METADATA;
170 op->data.send_initial_metadata.count = 0;
171 op++;
172 op->op = GRPC_OP_SEND_MESSAGE;
173 op->data.send_message.send_message = request_payload;
174 op++;
175 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
176 op++;
177 op->op = GRPC_OP_RECV_INITIAL_METADATA;
178 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
179 op++;
180 error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
181 nullptr);
182 GPR_ASSERT(GRPC_CALL_OK == error);
183
184 error =
185 grpc_server_request_call(f.server, &s, &call_details,
186 &request_metadata_recv, f.cq, f.cq, tag(101));
187 GPR_ASSERT(GRPC_CALL_OK == error);
188 CQ_EXPECT_COMPLETION(cqv, tag(101), true);
189 cq_verify(cqv);
190
191 peer = grpc_call_get_peer(s);
192 GPR_ASSERT(peer != nullptr);
193 gpr_log(GPR_DEBUG, "server_peer=%s", peer);
194 gpr_free(peer);
195 peer = grpc_call_get_peer(c);
196 GPR_ASSERT(peer != nullptr);
197 gpr_log(GPR_DEBUG, "client_peer=%s", peer);
198 gpr_free(peer);
199
200 memset(ops, 0, sizeof(ops));
201 op = ops;
202 op->op = GRPC_OP_SEND_INITIAL_METADATA;
203 op->data.send_initial_metadata.count = 0;
204 op++;
205 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
206 op->data.send_status_from_server.trailing_metadata_count = 0;
207 op->data.send_status_from_server.status = GRPC_STATUS_INVALID_ARGUMENT;
208 op->data.send_status_from_server.status_details = &status_details;
209 op++;
210 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
211 op->data.recv_close_on_server.cancelled = &was_cancelled;
212 op++;
213 error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(102),
214 nullptr);
215 GPR_ASSERT(GRPC_CALL_OK == error);
216
217 CQ_EXPECT_COMPLETION(cqv, tag(102), true);
218 CQ_EXPECT_COMPLETION(cqv, tag(1), true);
219 cq_verify(cqv);
220
221 memset(ops, 0, sizeof(ops));
222 op = ops;
223 op->op = GRPC_OP_RECV_MESSAGE;
224 op->data.recv_message.recv_message = &response_payload_recv;
225 op++;
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++;
231 error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(2),
232 nullptr);
233 GPR_ASSERT(GRPC_CALL_OK == error);
234
235 CQ_EXPECT_COMPLETION(cqv, tag(2), true);
236 cq_verify(cqv);
237
238 GPR_ASSERT(status == GRPC_STATUS_INVALID_ARGUMENT);
239 GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz"));
240 GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/service/method"));
241 GPR_ASSERT(0 == call_details.flags);
242 GPR_ASSERT(was_cancelled == 0);
243
244 grpc_slice_unref(details);
245 grpc_metadata_array_destroy(&initial_metadata_recv);
246 grpc_metadata_array_destroy(&trailing_metadata_recv);
247 grpc_metadata_array_destroy(&request_metadata_recv);
248 grpc_call_details_destroy(&call_details);
249 grpc_byte_buffer_destroy(request_payload);
250 grpc_byte_buffer_destroy(response_payload);
251 grpc_byte_buffer_destroy(request_payload_recv);
252 grpc_byte_buffer_destroy(response_payload_recv);
253
254 grpc_call_unref(c);
255 grpc_call_unref(s);
256
257 cq_verifier_destroy(cqv);
258
259 end_test(&f);
260 config.tear_down_data(&f);
261 }
262
retry_non_retriable_status_before_recv_trailing_metadata_started(grpc_end2end_test_config config)263 void retry_non_retriable_status_before_recv_trailing_metadata_started(
264 grpc_end2end_test_config config) {
265 GPR_ASSERT(config.feature_mask & FEATURE_MASK_SUPPORTS_CLIENT_CHANNEL);
266 test_retry_non_retriable_status_before_recv_trailing_metadata_started(config);
267 }
268
retry_non_retriable_status_before_recv_trailing_metadata_started_pre_init()269 void retry_non_retriable_status_before_recv_trailing_metadata_started_pre_init() {
270 }
271