Debian Codegraph @

test/core/end2end/tests/retry_cancellation.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 <string>
23
24 #include "absl/strings/str_cat.h"
25
26 #include <grpc/byte_buffer.h>
27 #include <grpc/grpc.h>
28 #include <grpc/impl/codegen/propagation_bits.h>
29 #include <grpc/slice.h>
30 #include <grpc/status.h>
31 #include <grpc/support/alloc.h>
32 #include <grpc/support/log.h>
33
34 #include "src/core/lib/channel/channel_args.h"
35 #include "src/core/lib/gpr/useful.h"
36 #include "test/core/end2end/cq_verifier.h"
37 #include "test/core/end2end/end2end_tests.h"
38 #include "test/core/end2end/tests/cancel_test_helpers.h"
39 #include "test/core/util/test_config.h"
40
41 static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); }
42
43 static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
44 const char* test_name,
45 grpc_channel_args* client_args,
46 grpc_channel_args* server_args) {
47 grpc_end2end_test_fixture f;
48 gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name);
49 f = config.create_fixture(client_args, server_args);
50 config.init_server(&f, server_args);
51 config.init_client(&f, client_args);
52 return f;
53 }
54
55 static gpr_timespec n_seconds_from_now(int n) {
56 return grpc_timeout_seconds_to_deadline(n);
57 }
58
59 static gpr_timespec five_seconds_from_now(void) {
60 return n_seconds_from_now(5);
61 }
62
63 static void drain_cq(grpc_completion_queue* cq) {
64 grpc_event ev;
65 do {
66 ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr);
67 } while (ev.type != GRPC_QUEUE_SHUTDOWN);
68 }
69
70 static void shutdown_server(grpc_end2end_test_fixture* f) {
71 if (!f->server) return;
72 grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000));
73 grpc_event ev;
74 do {
75 ev = grpc_completion_queue_next(f->cq, grpc_timeout_seconds_to_deadline(5),
76 nullptr);
77 } while (ev.type != GRPC_OP_COMPLETE || ev.tag != tag(1000));
78 grpc_server_destroy(f->server);
79 f->server = nullptr;
80 }
81
82 static void shutdown_client(grpc_end2end_test_fixture* f) {
83 if (!f->client) return;
84 grpc_channel_destroy(f->client);
85 f->client = nullptr;
86 }
87
88 static void end_test(grpc_end2end_test_fixture* f) {
89 shutdown_server(f);
90 shutdown_client(f);
91
92 grpc_completion_queue_shutdown(f->cq);
93 drain_cq(f->cq);
94 grpc_completion_queue_destroy(f->cq);
95 }
96
97 // Tests retry cancellation.
98 static void test_retry_cancellation(grpc_end2end_test_config config,
99 cancellation_mode mode) {
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 response_payload_slice = grpc_slice_from_static_string("bar");
110 grpc_byte_buffer* request_payload =
111 grpc_raw_byte_buffer_create(&request_payload_slice, 1);
112 grpc_byte_buffer* response_payload =
113 grpc_raw_byte_buffer_create(&response_payload_slice, 1);
114 grpc_byte_buffer* request_payload_recv = nullptr;
115 grpc_byte_buffer* response_payload_recv = nullptr;
116 grpc_status_code status;
117 grpc_call_error error;
118 grpc_slice details;
119 int was_cancelled = 2;
120 char* peer;
121
122 grpc_arg args[] = {
123 grpc_channel_arg_string_create(
124 const_cast<char*>(GRPC_ARG_SERVICE_CONFIG),
125 const_cast<char*>(
126 "{\n"
127 " \"methodConfig\": [ {\n"
128 " \"name\": [\n"
129 " { \"service\": \"service\", \"method\": \"method\" }\n"
130 " ],\n"
131 " \"retryPolicy\": {\n"
132 " \"maxAttempts\": 5,\n"
133 " \"initialBackoff\": \"1s\",\n"
134 " \"maxBackoff\": \"120s\",\n"
135 " \"backoffMultiplier\": 1.6,\n"
136 " \"retryableStatusCodes\": [ \"ABORTED\" ]\n"
137 " },\n"
138 " \"timeout\": \"10s\"\n"
139 " } ]\n"
140 "}")),
141 };
142 grpc_channel_args client_args = {GPR_ARRAY_SIZE(args), args};
143 std::string name = absl::StrCat("retry_cancellation/", mode.name);
144 grpc_end2end_test_fixture f =
145 begin_test(config, name.c_str(), &client_args, nullptr);
146
147 grpc_core::CqVerifier cqv(f.cq);
148
149 gpr_timespec deadline = five_seconds_from_now();
150 c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
151 grpc_slice_from_static_string("/service/method"),
152 nullptr, deadline, nullptr);
153 GPR_ASSERT(c);
154
155 peer = grpc_call_get_peer(c);
156 GPR_ASSERT(peer != nullptr);
157 gpr_log(GPR_DEBUG, "client_peer_before_call=%s", peer);
158 gpr_free(peer);
159
160 grpc_metadata_array_init(&initial_metadata_recv);
161 grpc_metadata_array_init(&trailing_metadata_recv);
162 grpc_metadata_array_init(&request_metadata_recv);
163 grpc_call_details_init(&call_details);
164 grpc_slice status_details = grpc_slice_from_static_string("xyz");
165
166 // Client starts a batch with all 6 ops.
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_RECV_MESSAGE;
176 op->data.recv_message.recv_message = &response_payload_recv;
177 op++;
178 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
179 op++;
180 op->op = GRPC_OP_RECV_INITIAL_METADATA;
181 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
182 op++;
183 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
184 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
185 op->data.recv_status_on_client.status = &status;
186 op->data.recv_status_on_client.status_details = &details;
187 op++;
188 error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
189 nullptr);
190 GPR_ASSERT(GRPC_CALL_OK == error);
191
192 // Server gets a call and fails with retryable status.
193 error =
194 grpc_server_request_call(f.server, &s, &call_details,
195 &request_metadata_recv, f.cq, f.cq, tag(101));
196 GPR_ASSERT(GRPC_CALL_OK == error);
197 cqv.Expect(tag(101), true);
198 cqv.Verify();
199
200 peer = grpc_call_get_peer(s);
201 GPR_ASSERT(peer != nullptr);
202 gpr_log(GPR_DEBUG, "server_peer=%s", peer);
203 gpr_free(peer);
204 peer = grpc_call_get_peer(c);
205 GPR_ASSERT(peer != nullptr);
206 gpr_log(GPR_DEBUG, "client_peer=%s", peer);
207 gpr_free(peer);
208
209 memset(ops, 0, sizeof(ops));
210 op = ops;
211 op->op = GRPC_OP_SEND_INITIAL_METADATA;
212 op->data.send_initial_metadata.count = 0;
213 op++;
214 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
215 op->data.send_status_from_server.trailing_metadata_count = 0;
216 op->data.send_status_from_server.status = GRPC_STATUS_ABORTED;
217 op->data.send_status_from_server.status_details = &status_details;
218 op++;
219 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
220 op->data.recv_close_on_server.cancelled = &was_cancelled;
221 op++;
222 error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(102),
223 nullptr);
224 GPR_ASSERT(GRPC_CALL_OK == error);
225
226 cqv.Expect(tag(102), true);
227 cqv.Verify();
228
229 grpc_call_unref(s);
230 grpc_metadata_array_destroy(&request_metadata_recv);
231 grpc_metadata_array_init(&request_metadata_recv);
232 grpc_call_details_destroy(&call_details);
233 grpc_call_details_init(&call_details);
234
235 // Server gets a second call (the retry).
236 error =
237 grpc_server_request_call(f.server, &s, &call_details,
238 &request_metadata_recv, f.cq, f.cq, tag(201));
239 GPR_ASSERT(GRPC_CALL_OK == error);
240 cqv.Expect(tag(201), true);
241 cqv.Verify();
242
243 // Initiate cancellation.
244 GPR_ASSERT(GRPC_CALL_OK == mode.initiate_cancel(c, nullptr));
245
246 cqv.Expect(tag(1), true);
247 cqv.Verify();
248
249 GPR_ASSERT(status == mode.expect_status);
250 GPR_ASSERT(was_cancelled == 0);
251
252 grpc_slice_unref(details);
253 grpc_metadata_array_destroy(&initial_metadata_recv);
254 grpc_metadata_array_destroy(&trailing_metadata_recv);
255 grpc_metadata_array_destroy(&request_metadata_recv);
256 grpc_call_details_destroy(&call_details);
257 grpc_byte_buffer_destroy(request_payload);
258 grpc_byte_buffer_destroy(response_payload);
259 grpc_byte_buffer_destroy(request_payload_recv);
260 grpc_byte_buffer_destroy(response_payload_recv);
261
262 grpc_call_unref(c);
263 grpc_call_unref(s);
264
265 end_test(&f);
266 config.tear_down_data(&f);
267 }
268
269 void retry_cancellation(grpc_end2end_test_config config) {
270 GPR_ASSERT(config.feature_mask & FEATURE_MASK_SUPPORTS_CLIENT_CHANNEL);
271 for (size_t i = 0; i < GPR_ARRAY_SIZE(cancellation_modes); ++i) {
272 test_retry_cancellation(config, cancellation_modes[i]);
273 }
274 }
275
276 void retry_cancellation_pre_init(void) {}