2 // Copyright 2017 gRPC authors.
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
22 #include "absl/strings/str_cat.h"
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/time.h>
30 #include "src/core/lib/channel/channel_args.h"
31 #include "src/core/lib/gpr/string.h"
32 #include "src/core/lib/gpr/useful.h"
33 #include "src/core/lib/iomgr/exec_ctx.h"
34 #include "src/core/lib/transport/static_metadata.h"
35 #include "test/core/end2end/cq_verifier.h"
36 #include "test/core/end2end/end2end_tests.h"
37 #include "test/core/end2end/tests/cancel_test_helpers.h"
39 static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); }
41 static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
42 const char* test_name,
43 grpc_channel_args* client_args,
44 grpc_channel_args* server_args) {
45 grpc_end2end_test_fixture f;
46 gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name);
47 f = config.create_fixture(client_args, server_args);
48 config.init_server(&f, server_args);
49 config.init_client(&f, client_args);
53 static gpr_timespec n_seconds_from_now(int n) {
54 return grpc_timeout_seconds_to_deadline(n);
57 static gpr_timespec five_seconds_from_now(void) {
58 return n_seconds_from_now(5);
61 static void drain_cq(grpc_completion_queue* cq) {
64 ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr);
65 } while (ev.type != GRPC_QUEUE_SHUTDOWN);
68 static void shutdown_server(grpc_end2end_test_fixture* f) {
69 if (!f->server) return;
70 grpc_server_shutdown_and_notify(f->server, f->shutdown_cq, tag(1000));
71 GPR_ASSERT(grpc_completion_queue_pluck(f->shutdown_cq, tag(1000),
72 grpc_timeout_seconds_to_deadline(5),
74 .type == GRPC_OP_COMPLETE);
75 grpc_server_destroy(f->server);
79 static void shutdown_client(grpc_end2end_test_fixture* f) {
80 if (!f->client) return;
81 grpc_channel_destroy(f->client);
85 static void end_test(grpc_end2end_test_fixture* f) {
89 grpc_completion_queue_shutdown(f->cq);
91 grpc_completion_queue_destroy(f->cq);
92 grpc_completion_queue_destroy(f->shutdown_cq);
95 // Tests retry cancellation during backoff.
96 static void test_retry_cancel_during_delay(grpc_end2end_test_config config,
97 cancellation_mode mode) {
102 grpc_metadata_array initial_metadata_recv;
103 grpc_metadata_array trailing_metadata_recv;
104 grpc_metadata_array request_metadata_recv;
105 grpc_call_details call_details;
106 grpc_slice request_payload_slice = grpc_slice_from_static_string("foo");
107 grpc_slice response_payload_slice = grpc_slice_from_static_string("bar");
108 grpc_byte_buffer* request_payload =
109 grpc_raw_byte_buffer_create(&request_payload_slice, 1);
110 grpc_byte_buffer* response_payload =
111 grpc_raw_byte_buffer_create(&response_payload_slice, 1);
112 grpc_byte_buffer* request_payload_recv = nullptr;
113 grpc_byte_buffer* response_payload_recv = nullptr;
114 grpc_status_code status;
115 grpc_call_error error;
117 int was_cancelled = 2;
121 grpc_channel_arg_string_create(
122 const_cast<char*>(GRPC_ARG_SERVICE_CONFIG),
125 " \"methodConfig\": [ {\n"
127 " { \"service\": \"service\", \"method\": \"method\" }\n"
129 " \"retryPolicy\": {\n"
130 " \"maxAttempts\": 3,\n"
131 " \"initialBackoff\": \"10s\",\n"
132 " \"maxBackoff\": \"120s\",\n"
133 " \"backoffMultiplier\": 1.6,\n"
134 " \"retryableStatusCodes\": [ \"ABORTED\" ]\n"
136 " \"timeout\": \"5s\"\n"
140 grpc_channel_args client_args = {GPR_ARRAY_SIZE(args), args};
141 std::string name = absl::StrCat("retry_cancel_during_delay/", mode.name);
142 grpc_end2end_test_fixture f =
143 begin_test(config, name.c_str(), &client_args, nullptr);
145 cq_verifier* cqv = cq_verifier_create(f.cq);
147 gpr_timespec expect_finish_before = n_seconds_from_now(10);
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);
154 peer = grpc_call_get_peer(c);
155 GPR_ASSERT(peer != nullptr);
156 gpr_log(GPR_DEBUG, "client_peer_before_call=%s", peer);
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");
165 // Client starts a batch with all 6 ops.
166 memset(ops, 0, sizeof(ops));
168 op->op = GRPC_OP_SEND_INITIAL_METADATA;
169 op->data.send_initial_metadata.count = 0;
171 op->op = GRPC_OP_SEND_MESSAGE;
172 op->data.send_message.send_message = request_payload;
174 op->op = GRPC_OP_RECV_MESSAGE;
175 op->data.recv_message.recv_message = &response_payload_recv;
177 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
179 op->op = GRPC_OP_RECV_INITIAL_METADATA;
180 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
182 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
183 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
184 op->data.recv_status_on_client.status = &status;
185 op->data.recv_status_on_client.status_details = &details;
187 error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
189 GPR_ASSERT(GRPC_CALL_OK == error);
191 // Server gets a call and fails with retryable status.
193 grpc_server_request_call(f.server, &s, &call_details,
194 &request_metadata_recv, f.cq, f.cq, tag(101));
195 GPR_ASSERT(GRPC_CALL_OK == error);
196 CQ_EXPECT_COMPLETION(cqv, tag(101), true);
199 peer = grpc_call_get_peer(s);
200 GPR_ASSERT(peer != nullptr);
201 gpr_log(GPR_DEBUG, "server_peer=%s", peer);
203 peer = grpc_call_get_peer(c);
204 GPR_ASSERT(peer != nullptr);
205 gpr_log(GPR_DEBUG, "client_peer=%s", peer);
208 memset(ops, 0, sizeof(ops));
210 op->op = GRPC_OP_SEND_INITIAL_METADATA;
211 op->data.send_initial_metadata.count = 0;
213 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
214 op->data.send_status_from_server.trailing_metadata_count = 0;
215 op->data.send_status_from_server.status = GRPC_STATUS_ABORTED;
216 op->data.send_status_from_server.status_details = &status_details;
218 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
219 op->data.recv_close_on_server.cancelled = &was_cancelled;
221 error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(102),
223 GPR_ASSERT(GRPC_CALL_OK == error);
225 CQ_EXPECT_COMPLETION(cqv, tag(102), true);
229 grpc_metadata_array_destroy(&request_metadata_recv);
230 grpc_metadata_array_init(&request_metadata_recv);
231 grpc_call_details_destroy(&call_details);
232 grpc_call_details_init(&call_details);
234 // Server should never get a second call, because the initial retry
235 // delay is longer than the call's deadline.
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 cq_verify_empty(cqv);
242 // Initiate cancellation.
243 GPR_ASSERT(GRPC_CALL_OK == mode.initiate_cancel(c, nullptr));
245 CQ_EXPECT_COMPLETION(cqv, tag(1), true);
248 // Make sure we didn't wait the full deadline before failing.
250 GPR_INFO, "Expect completion before: %s",
251 absl::FormatTime(grpc_core::ToAbslTime(expect_finish_before)).c_str());
252 GPR_ASSERT(gpr_time_cmp(gpr_now(GPR_CLOCK_MONOTONIC), expect_finish_before) <
255 gpr_log(GPR_INFO, "status=%d expected=%d", status, mode.expect_status);
256 GPR_ASSERT(status == mode.expect_status);
257 GPR_ASSERT(was_cancelled == 0);
259 grpc_slice_unref(details);
260 grpc_metadata_array_destroy(&initial_metadata_recv);
261 grpc_metadata_array_destroy(&trailing_metadata_recv);
262 grpc_metadata_array_destroy(&request_metadata_recv);
263 grpc_call_details_destroy(&call_details);
264 grpc_byte_buffer_destroy(request_payload);
265 grpc_byte_buffer_destroy(response_payload);
266 grpc_byte_buffer_destroy(request_payload_recv);
267 grpc_byte_buffer_destroy(response_payload_recv);
271 cq_verifier_destroy(cqv);
274 config.tear_down_data(&f);
277 void retry_cancel_during_delay(grpc_end2end_test_config config) {
278 GPR_ASSERT(config.feature_mask & FEATURE_MASK_SUPPORTS_CLIENT_CHANNEL);
279 for (size_t i = 0; i < GPR_ARRAY_SIZE(cancellation_modes); ++i) {
280 test_retry_cancel_during_delay(config, cancellation_modes[i]);
284 void retry_cancel_during_delay_pre_init(void) {}