Debian Codegraph @

test/core/end2end/tests/retry_recv_initial_metadata.cc

raw
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 <stdint.h>
20 #include <string.h>
21
22 #include <grpc/byte_buffer.h>
23 #include <grpc/grpc.h>
24 #include <grpc/impl/codegen/propagation_bits.h>
25 #include <grpc/slice.h>
26 #include <grpc/status.h>
27 #include <grpc/support/alloc.h>
28 #include <grpc/support/log.h>
29
30 #include "src/core/lib/channel/channel_args.h"
31 #include "src/core/lib/gpr/useful.h"
32 #include "test/core/end2end/cq_verifier.h"
33 #include "test/core/end2end/end2end_tests.h"
34 #include "test/core/util/test_config.h"
35
36 static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); }
37
38 static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
39 const char* test_name,
40 grpc_channel_args* client_args,
41 grpc_channel_args* server_args) {
42 grpc_end2end_test_fixture f;
43 gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name);
44 f = config.create_fixture(client_args, server_args);
45 config.init_server(&f, server_args);
46 config.init_client(&f, client_args);
47 return f;
48 }
49
50 static gpr_timespec n_seconds_from_now(int n) {
51 return grpc_timeout_seconds_to_deadline(n);
52 }
53
54 static gpr_timespec five_seconds_from_now(void) {
55 return n_seconds_from_now(5);
56 }
57
58 static void drain_cq(grpc_completion_queue* cq) {
59 grpc_event ev;
60 do {
61 ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr);
62 } while (ev.type != GRPC_QUEUE_SHUTDOWN);
63 }
64
65 static void shutdown_server(grpc_end2end_test_fixture* f) {
66 if (!f->server) return;
67 grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000));
68 grpc_event ev;
69 do {
70 ev = grpc_completion_queue_next(f->cq, grpc_timeout_seconds_to_deadline(5),
71 nullptr);
72 } while (ev.type != GRPC_OP_COMPLETE || ev.tag != tag(1000));
73 grpc_server_destroy(f->server);
74 f->server = nullptr;
75 }
76
77 static void shutdown_client(grpc_end2end_test_fixture* f) {
78 if (!f->client) return;
79 grpc_channel_destroy(f->client);
80 f->client = nullptr;
81 }
82
83 static void end_test(grpc_end2end_test_fixture* f) {
84 shutdown_server(f);
85 shutdown_client(f);
86
87 grpc_completion_queue_shutdown(f->cq);
88 drain_cq(f->cq);
89 grpc_completion_queue_destroy(f->cq);
90 }
91
92 // Tests that receiving initial metadata commits the call.
93 // - 1 retry allowed for ABORTED status
94 // - first attempt receives initial metadata before trailing metadata,
95 // so no retry is done even though status was ABORTED
96 static void test_retry_recv_initial_metadata(grpc_end2end_test_config config) {
97 grpc_call* c;
98 grpc_call* s;
99 grpc_op ops[6];
100 grpc_op* op;
101 grpc_metadata_array initial_metadata_recv;
102 grpc_metadata_array trailing_metadata_recv;
103 grpc_metadata_array request_metadata_recv;
104 grpc_call_details call_details;
105 grpc_slice request_payload_slice = grpc_slice_from_static_string("foo");
106 grpc_slice response_payload_slice = grpc_slice_from_static_string("bar");
107 grpc_byte_buffer* request_payload =
108 grpc_raw_byte_buffer_create(&request_payload_slice, 1);
109 grpc_byte_buffer* response_payload =
110 grpc_raw_byte_buffer_create(&response_payload_slice, 1);
111 grpc_byte_buffer* request_payload_recv = nullptr;
112 grpc_byte_buffer* response_payload_recv = nullptr;
113 grpc_status_code status;
114 grpc_call_error error;
115 grpc_slice details;
116 int was_cancelled = 2;
117 char* peer;
118 grpc_metadata initial_metadata_from_server = {
119 grpc_slice_from_static_string("key1"),
120 grpc_slice_from_static_string("val1"),
121 {{nullptr, nullptr, nullptr, nullptr}}};
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 =
144 begin_test(config, "retry_recv_initial_metadata", &client_args, nullptr);
145
146 grpc_core::CqVerifier cqv(f.cq);
147
148 gpr_timespec deadline = five_seconds_from_now();
149 c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
150 grpc_slice_from_static_string("/service/method"),
151 nullptr, deadline, nullptr);
152 GPR_ASSERT(c);
153
154 peer = grpc_call_get_peer(c);
155 GPR_ASSERT(peer != nullptr);
156 gpr_log(GPR_DEBUG, "client_peer_before_call=%s", peer);
157 gpr_free(peer);
158
159 grpc_metadata_array_init(&initial_metadata_recv);
160 grpc_metadata_array_init(&trailing_metadata_recv);
161 grpc_metadata_array_init(&request_metadata_recv);
162 grpc_call_details_init(&call_details);
163 grpc_slice status_details = grpc_slice_from_static_string("xyz");
164
165 memset(ops, 0, sizeof(ops));
166 op = ops;
167 op->op = GRPC_OP_SEND_INITIAL_METADATA;
168 op->data.send_initial_metadata.count = 0;
169 op++;
170 op->op = GRPC_OP_SEND_MESSAGE;
171 op->data.send_message.send_message = request_payload;
172 op++;
173 op->op = GRPC_OP_RECV_MESSAGE;
174 op->data.recv_message.recv_message = &response_payload_recv;
175 op++;
176 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
177 op++;
178 op->op = GRPC_OP_RECV_INITIAL_METADATA;
179 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_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, static_cast<size_t>(op - ops), tag(1),
187 nullptr);
188 GPR_ASSERT(GRPC_CALL_OK == error);
189
190 error =
191 grpc_server_request_call(f.server, &s, &call_details,
192 &request_metadata_recv, f.cq, f.cq, tag(101));
193 GPR_ASSERT(GRPC_CALL_OK == error);
194 cqv.Expect(tag(101), true);
195 cqv.Verify();
196
197 peer = grpc_call_get_peer(s);
198 GPR_ASSERT(peer != nullptr);
199 gpr_log(GPR_DEBUG, "server_peer=%s", peer);
200 gpr_free(peer);
201 peer = grpc_call_get_peer(c);
202 GPR_ASSERT(peer != nullptr);
203 gpr_log(GPR_DEBUG, "client_peer=%s", peer);
204 gpr_free(peer);
205
206 // Server sends initial metadata in its own batch, before sending
207 // trailing metadata.
208 // Ideally, this would not require actually sending any metadata
209 // entries, but we do so to avoid sporadic failures in the proxy
210 // tests, where the proxy may wind up combining the batches, depending
211 // on timing. Sending a metadata entry ensures that the transport
212 // won't send a Trailers-Only response, even if the batches are combined.
213 memset(ops, 0, sizeof(ops));
214 op = ops;
215 op->op = GRPC_OP_SEND_INITIAL_METADATA;
216 op->data.send_initial_metadata.count = 1;
217 op->data.send_initial_metadata.metadata = &initial_metadata_from_server;
218 op++;
219 error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(102),
220 nullptr);
221 GPR_ASSERT(GRPC_CALL_OK == error);
222
223 cqv.Expect(tag(102), true);
224 cqv.Verify();
225
226 memset(ops, 0, sizeof(ops));
227 op = ops;
228 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
229 op->data.send_status_from_server.trailing_metadata_count = 0;
230 op->data.send_status_from_server.status = GRPC_STATUS_ABORTED;
231 op->data.send_status_from_server.status_details = &status_details;
232 op++;
233 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
234 op->data.recv_close_on_server.cancelled = &was_cancelled;
235 op++;
236 error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(103),
237 nullptr);
238 GPR_ASSERT(GRPC_CALL_OK == error);
239
240 cqv.Expect(tag(103), true);
241 cqv.Expect(tag(1), true);
242 cqv.Verify();
243
244 GPR_ASSERT(status == GRPC_STATUS_ABORTED);
245 GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz"));
246 GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/service/method"));
247 GPR_ASSERT(was_cancelled == 0);
248
249 grpc_slice_unref(details);
250 grpc_metadata_array_destroy(&initial_metadata_recv);
251 grpc_metadata_array_destroy(&trailing_metadata_recv);
252 grpc_metadata_array_destroy(&request_metadata_recv);
253 grpc_call_details_destroy(&call_details);
254 grpc_byte_buffer_destroy(request_payload);
255 grpc_byte_buffer_destroy(response_payload);
256 grpc_byte_buffer_destroy(request_payload_recv);
257 grpc_byte_buffer_destroy(response_payload_recv);
258
259 grpc_call_unref(c);
260 grpc_call_unref(s);
261
262 end_test(&f);
263 config.tear_down_data(&f);
264 }
265
266 void retry_recv_initial_metadata(grpc_end2end_test_config config) {
267 GPR_ASSERT(config.feature_mask & FEATURE_MASK_SUPPORTS_CLIENT_CHANNEL);
268 test_retry_recv_initial_metadata(config);
269 }
270
271 void retry_recv_initial_metadata_pre_init(void) {}