Debian Codegraph @

test/core/end2end/tests/retry_unref_before_recv.cc

raw
1 //
2 // Copyright 2017 gRPC authors.
3 //
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
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
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.
15 //
16
17 #include <stdint.h>
18 #include <string.h>
19
20 #include <grpc/byte_buffer.h>
21 #include <grpc/grpc.h>
22 #include <grpc/impl/codegen/propagation_bits.h>
23 #include <grpc/slice.h>
24 #include <grpc/status.h>
25 #include <grpc/support/log.h>
26
27 #include "src/core/lib/channel/channel_args.h"
28 #include "src/core/lib/gpr/useful.h"
29 #include "test/core/end2end/cq_verifier.h"
30 #include "test/core/end2end/end2end_tests.h"
31 #include "test/core/util/test_config.h"
32
33 static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); }
34
35 static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
36 const char* test_name,
37 grpc_channel_args* client_args,
38 grpc_channel_args* server_args) {
39 grpc_end2end_test_fixture f;
40 gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name);
41 f = config.create_fixture(client_args, server_args);
42 config.init_server(&f, server_args);
43 config.init_client(&f, client_args);
44 return f;
45 }
46
47 static gpr_timespec n_seconds_from_now(int n) {
48 return grpc_timeout_seconds_to_deadline(n);
49 }
50
51 static gpr_timespec five_seconds_from_now(void) {
52 return n_seconds_from_now(5);
53 }
54
55 static void drain_cq(grpc_completion_queue* cq) {
56 grpc_event ev;
57 do {
58 ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr);
59 } while (ev.type != GRPC_QUEUE_SHUTDOWN);
60 }
61
62 static void shutdown_server(grpc_end2end_test_fixture* f) {
63 if (!f->server) return;
64 grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000));
65 grpc_event ev;
66 do {
67 ev = grpc_completion_queue_next(f->cq, grpc_timeout_seconds_to_deadline(5),
68 nullptr);
69 } while (ev.type != GRPC_OP_COMPLETE || ev.tag != tag(1000));
70 grpc_server_destroy(f->server);
71 f->server = nullptr;
72 }
73
74 static void shutdown_client(grpc_end2end_test_fixture* f) {
75 if (!f->client) return;
76 grpc_channel_destroy(f->client);
77 f->client = nullptr;
78 }
79
80 static void end_test(grpc_end2end_test_fixture* f) {
81 shutdown_server(f);
82 shutdown_client(f);
83
84 grpc_completion_queue_shutdown(f->cq);
85 drain_cq(f->cq);
86 grpc_completion_queue_destroy(f->cq);
87 }
88
89 // Tests that we can unref a call while recv ops are started but before
90 // they complete. This ensures that we don't drop callbacks or cause a
91 // memory leak.
92 static void test_retry_unref_before_recv(grpc_end2end_test_config config) {
93 grpc_call* c;
94 grpc_call* s;
95 grpc_op ops[6];
96 grpc_op* op;
97 grpc_metadata_array initial_metadata_recv;
98 grpc_metadata_array request_metadata_recv;
99 grpc_call_details call_details;
100 grpc_slice request_payload_slice = grpc_slice_from_static_string("foo");
101 grpc_slice response_payload_slice = grpc_slice_from_static_string("bar");
102 grpc_byte_buffer* request_payload =
103 grpc_raw_byte_buffer_create(&request_payload_slice, 1);
104 grpc_byte_buffer* response_payload =
105 grpc_raw_byte_buffer_create(&response_payload_slice, 1);
106 grpc_byte_buffer* request_payload_recv = nullptr;
107 grpc_byte_buffer* response_payload_recv = nullptr;
108 grpc_call_error error;
109 int was_cancelled = 2;
110
111 grpc_arg args[] = {
112 grpc_channel_arg_string_create(
113 const_cast<char*>(GRPC_ARG_SERVICE_CONFIG),
114 const_cast<char*>(
115 "{\n"
116 " \"methodConfig\": [ {\n"
117 " \"name\": [\n"
118 " { \"service\": \"service\", \"method\": \"method\" }\n"
119 " ],\n"
120 " \"retryPolicy\": {\n"
121 " \"maxAttempts\": 3,\n"
122 " \"initialBackoff\": \"1s\",\n"
123 " \"maxBackoff\": \"120s\",\n"
124 " \"backoffMultiplier\": 1.6,\n"
125 " \"retryableStatusCodes\": [ \"ABORTED\" ]\n"
126 " }\n"
127 " } ]\n"
128 "}")),
129 };
130 grpc_channel_args client_args = {GPR_ARRAY_SIZE(args), args};
131 grpc_end2end_test_fixture f =
132 begin_test(config, "retry_unref_before_recv", &client_args, nullptr);
133
134 grpc_core::CqVerifier cqv(f.cq);
135
136 gpr_timespec deadline = five_seconds_from_now();
137 c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
138 grpc_slice_from_static_string("/service/method"),
139 nullptr, deadline, nullptr);
140 GPR_ASSERT(c);
141
142 grpc_metadata_array_init(&initial_metadata_recv);
143 grpc_metadata_array_init(&request_metadata_recv);
144 grpc_call_details_init(&call_details);
145 grpc_slice status_details = grpc_slice_from_static_string("xyz");
146
147 // Client starts send ops.
148 memset(ops, 0, sizeof(ops));
149 op = ops;
150 op->op = GRPC_OP_SEND_INITIAL_METADATA;
151 op->data.send_initial_metadata.count = 0;
152 op++;
153 op->op = GRPC_OP_SEND_MESSAGE;
154 op->data.send_message.send_message = request_payload;
155 op++;
156 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
157 op++;
158 error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
159 nullptr);
160 GPR_ASSERT(GRPC_CALL_OK == error);
161
162 // Client starts recv_initial_metadata and recv_message, but not
163 // recv_trailing_metadata.
164 memset(ops, 0, sizeof(ops));
165 op = ops;
166 op->op = GRPC_OP_RECV_INITIAL_METADATA;
167 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
168 op++;
169 op->op = GRPC_OP_RECV_MESSAGE;
170 op->data.recv_message.recv_message = &response_payload_recv;
171 op++;
172 error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(2),
173 nullptr);
174 GPR_ASSERT(GRPC_CALL_OK == error);
175
176 // Server gets a call and client send ops complete.
177 error =
178 grpc_server_request_call(f.server, &s, &call_details,
179 &request_metadata_recv, f.cq, f.cq, tag(101));
180 GPR_ASSERT(GRPC_CALL_OK == error);
181 cqv.Expect(tag(1), true);
182 cqv.Expect(tag(101), true);
183 cqv.Verify();
184
185 // Client unrefs the call without starting recv_trailing_metadata.
186 // This should trigger a cancellation.
187 grpc_call_unref(c);
188
189 // Server immediately sends FAILED_PRECONDITION status (not retriable).
190 // This forces the retry filter to start a recv_trailing_metadata op
191 // internally, since the application hasn't started it yet.
192 memset(ops, 0, sizeof(ops));
193 op = ops;
194 op->op = GRPC_OP_SEND_INITIAL_METADATA;
195 op->data.send_initial_metadata.count = 0;
196 op++;
197 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
198 op->data.send_status_from_server.trailing_metadata_count = 0;
199 op->data.send_status_from_server.status = GRPC_STATUS_FAILED_PRECONDITION;
200 op->data.send_status_from_server.status_details = &status_details;
201 op++;
202 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
203 op->data.recv_close_on_server.cancelled = &was_cancelled;
204 op++;
205 error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(102),
206 nullptr);
207 GPR_ASSERT(GRPC_CALL_OK == error);
208
209 // Server ops complete and client recv ops complete.
210 cqv.Expect(tag(2), false); // Failure!
211 cqv.Expect(tag(102), true);
212 cqv.Verify();
213
214 GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/service/method"));
215 // Note: Not checking the value of was_cancelled here, because it will
216 // be flaky, depending on whether the server sent its response before
217 // the client sent its cancellation.
218
219 grpc_call_unref(s);
220 grpc_metadata_array_destroy(&request_metadata_recv);
221 grpc_metadata_array_init(&request_metadata_recv);
222 grpc_call_details_destroy(&call_details);
223 grpc_call_details_init(&call_details);
224
225 grpc_metadata_array_destroy(&initial_metadata_recv);
226 grpc_metadata_array_destroy(&request_metadata_recv);
227 grpc_call_details_destroy(&call_details);
228 grpc_byte_buffer_destroy(request_payload);
229 grpc_byte_buffer_destroy(response_payload);
230 grpc_byte_buffer_destroy(request_payload_recv);
231 grpc_byte_buffer_destroy(response_payload_recv);
232
233 end_test(&f);
234 config.tear_down_data(&f);
235 }
236
237 void retry_unref_before_recv(grpc_end2end_test_config config) {
238 GPR_ASSERT(config.feature_mask & FEATURE_MASK_SUPPORTS_CLIENT_CHANNEL);
239 test_retry_unref_before_recv(config);
240 }
241
242 void retry_unref_before_recv_pre_init(void) {}