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.
17 #include "test/core/end2end/end2end_tests.h"
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>
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"
35 #include "test/core/end2end/cq_verifier.h"
36 #include "test/core/end2end/tests/cancel_test_helpers.h"
38 static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); }
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);
52 static gpr_timespec n_seconds_from_now(int n) {
53 return grpc_timeout_seconds_to_deadline(n);
56 static gpr_timespec five_seconds_from_now(void) {
57 return n_seconds_from_now(5);
60 static void drain_cq(grpc_completion_queue* cq) {
63 ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr);
64 } while (ev.type != GRPC_QUEUE_SHUTDOWN);
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),
73 .type == GRPC_OP_COMPLETE);
74 grpc_server_destroy(f->server);
78 static void shutdown_client(grpc_end2end_test_fixture* f) {
79 if (!f->client) return;
80 grpc_channel_destroy(f->client);
84 static void end_test(grpc_end2end_test_fixture* f) {
88 grpc_completion_queue_shutdown(f->cq);
90 grpc_completion_queue_destroy(f->cq);
91 grpc_completion_queue_destroy(f->shutdown_cq);
94 // Tests perAttemptRecvTimeout:
95 // - 1 retry allowed for ABORTED status
96 // - both attempts do not receive a response until after perAttemptRecvTimeout
97 static void test_retry_per_attempt_recv_timeout_on_last_attempt(
98 grpc_end2end_test_config config) {
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;
121 grpc_channel_arg_integer_create(
122 const_cast<char*>(GRPC_ARG_EXPERIMENTAL_ENABLE_HEDGING), 1),
123 grpc_channel_arg_string_create(
124 const_cast<char*>(GRPC_ARG_SERVICE_CONFIG),
127 " \"methodConfig\": [ {\n"
129 " { \"service\": \"service\", \"method\": \"method\" }\n"
131 " \"retryPolicy\": {\n"
132 " \"maxAttempts\": 2,\n"
133 " \"initialBackoff\": \"1s\",\n"
134 " \"maxBackoff\": \"120s\",\n"
135 " \"backoffMultiplier\": 1.6,\n"
136 " \"perAttemptRecvTimeout\": \"2s\",\n"
137 " \"retryableStatusCodes\": [ \"ABORTED\" ]\n"
142 grpc_channel_args client_args = {GPR_ARRAY_SIZE(args), args};
143 grpc_end2end_test_fixture f =
144 begin_test(config, "retry", &client_args, nullptr);
146 cq_verifier* cqv = cq_verifier_create(f.cq);
148 gpr_timespec deadline = n_seconds_from_now(10);
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 grpc_metadata_array_init(&initial_metadata_recv);
155 grpc_metadata_array_init(&trailing_metadata_recv);
156 grpc_metadata_array_init(&request_metadata_recv);
157 grpc_call_details_init(&call_details);
159 memset(ops, 0, sizeof(ops));
161 op->op = GRPC_OP_SEND_INITIAL_METADATA;
162 op->data.send_initial_metadata.count = 0;
164 op->op = GRPC_OP_SEND_MESSAGE;
165 op->data.send_message.send_message = request_payload;
167 op->op = GRPC_OP_RECV_MESSAGE;
168 op->data.recv_message.recv_message = &response_payload_recv;
170 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
172 op->op = GRPC_OP_RECV_INITIAL_METADATA;
173 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
175 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
176 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
177 op->data.recv_status_on_client.status = &status;
178 op->data.recv_status_on_client.status_details = &details;
180 error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
182 GPR_ASSERT(GRPC_CALL_OK == error);
184 // Server gets a call but does not respond to the call.
186 grpc_server_request_call(f.server, &s0, &call_details,
187 &request_metadata_recv, f.cq, f.cq, tag(101));
188 GPR_ASSERT(GRPC_CALL_OK == error);
189 CQ_EXPECT_COMPLETION(cqv, tag(101), true);
192 // Make sure the "grpc-previous-rpc-attempts" header was not sent in the
194 for (size_t i = 0; i < request_metadata_recv.count; ++i) {
195 GPR_ASSERT(!grpc_slice_eq(request_metadata_recv.metadata[i].key,
196 GRPC_MDSTR_GRPC_PREVIOUS_RPC_ATTEMPTS));
199 grpc_metadata_array_destroy(&request_metadata_recv);
200 grpc_metadata_array_init(&request_metadata_recv);
201 grpc_call_details_destroy(&call_details);
202 grpc_call_details_init(&call_details);
204 // Server gets a second call, which it also does not respond to.
206 grpc_server_request_call(f.server, &s, &call_details,
207 &request_metadata_recv, f.cq, f.cq, tag(201));
208 GPR_ASSERT(GRPC_CALL_OK == error);
209 CQ_EXPECT_COMPLETION(cqv, tag(201), true);
212 // Now we can unref the first call.
215 // Make sure the "grpc-previous-rpc-attempts" header was sent in the retry.
216 bool found_retry_header = false;
217 for (size_t i = 0; i < request_metadata_recv.count; ++i) {
218 if (grpc_slice_eq(request_metadata_recv.metadata[i].key,
219 GRPC_MDSTR_GRPC_PREVIOUS_RPC_ATTEMPTS)) {
221 grpc_slice_eq(request_metadata_recv.metadata[i].value, GRPC_MDSTR_1));
222 found_retry_header = true;
226 GPR_ASSERT(found_retry_header);
228 // Client sees call completion.
229 CQ_EXPECT_COMPLETION(cqv, tag(1), true);
232 GPR_ASSERT(status == GRPC_STATUS_CANCELLED);
234 0 == grpc_slice_str_cmp(details, "retry perAttemptRecvTimeout exceeded"));
235 GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/service/method"));
236 GPR_ASSERT(0 == call_details.flags);
238 grpc_slice_unref(details);
239 grpc_metadata_array_destroy(&initial_metadata_recv);
240 grpc_metadata_array_destroy(&trailing_metadata_recv);
241 grpc_metadata_array_destroy(&request_metadata_recv);
242 grpc_call_details_destroy(&call_details);
243 grpc_byte_buffer_destroy(request_payload);
244 grpc_byte_buffer_destroy(response_payload);
245 grpc_byte_buffer_destroy(request_payload_recv);
246 grpc_byte_buffer_destroy(response_payload_recv);
251 cq_verifier_destroy(cqv);
254 config.tear_down_data(&f);
257 void retry_per_attempt_recv_timeout_on_last_attempt(
258 grpc_end2end_test_config config) {
259 GPR_ASSERT(config.feature_mask & FEATURE_MASK_SUPPORTS_CLIENT_CHANNEL);
260 test_retry_per_attempt_recv_timeout_on_last_attempt(config);
263 void retry_per_attempt_recv_timeout_on_last_attempt_pre_init(void) {}