Debian Codegraph @

test/core/iomgr/endpoint_tests.cc

raw
1 /*
2 *
3 * Copyright 2015 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 "test/core/iomgr/endpoint_tests.h"
20
21 #include <limits.h>
22 #include <stdbool.h>
23 #include <sys/types.h>
24
25 #include <grpc/slice.h>
26 #include <grpc/support/alloc.h>
27 #include <grpc/support/log.h>
28 #include <grpc/support/time.h>
29
30 #include "src/core/lib/gpr/useful.h"
31 #include "src/core/lib/gprpp/time.h"
32 #include "src/core/lib/slice/slice_internal.h"
33 #include "test/core/util/test_config.h"
34
35 /*
36 General test notes:
37
38 All tests which write data into an endpoint write i%256 into byte i, which
39 is verified by readers.
40
41 In general there are a few interesting things to vary which may lead to
42 exercising different codepaths in an implementation:
43 1. Total amount of data written to the endpoint
44 2. Size of slice allocations
45 3. Amount of data we read from or write to the endpoint at once
46
47 The tests here tend to parameterize these where applicable.
48
49 */
50
51 static gpr_mu* g_mu;
52 static grpc_pollset* g_pollset;
53
54 size_t count_slices(grpc_slice* slices, size_t nslices, int* current_data) {
55 size_t num_bytes = 0;
56 size_t i;
57 size_t j;
58 unsigned char* buf;
59 for (i = 0; i < nslices; ++i) {
60 buf = GRPC_SLICE_START_PTR(slices[i]);
61 for (j = 0; j < GRPC_SLICE_LENGTH(slices[i]); ++j) {
62 GPR_ASSERT(buf[j] == *current_data);
63 *current_data = (*current_data + 1) % 256;
64 }
65 num_bytes += GRPC_SLICE_LENGTH(slices[i]);
66 }
67 return num_bytes;
68 }
69
70 static grpc_endpoint_test_fixture begin_test(grpc_endpoint_test_config config,
71 const char* test_name,
72 size_t slice_size) {
73 gpr_log(GPR_INFO, "%s/%s", test_name, config.name);
74 return config.create_fixture(slice_size);
75 }
76
77 static void end_test(grpc_endpoint_test_config config) { config.clean_up(); }
78
79 static grpc_slice* allocate_blocks(size_t num_bytes, size_t slice_size,
80 size_t* num_blocks, uint8_t* current_data) {
81 size_t nslices = num_bytes / slice_size + (num_bytes % slice_size ? 1 : 0);
82 grpc_slice* slices =
83 static_cast<grpc_slice*>(gpr_malloc(sizeof(grpc_slice) * nslices));
84 size_t num_bytes_left = num_bytes;
85 size_t i;
86 size_t j;
87 unsigned char* buf;
88 *num_blocks = nslices;
89
90 for (i = 0; i < nslices; ++i) {
91 slices[i] = grpc_slice_malloc(slice_size > num_bytes_left ? num_bytes_left
92 : slice_size);
93 num_bytes_left -= GRPC_SLICE_LENGTH(slices[i]);
94 buf = GRPC_SLICE_START_PTR(slices[i]);
95 for (j = 0; j < GRPC_SLICE_LENGTH(slices[i]); ++j) {
96 buf[j] = *current_data;
97 (*current_data)++;
98 }
99 }
100 GPR_ASSERT(num_bytes_left == 0);
101 return slices;
102 }
103
104 struct read_and_write_test_state {
105 grpc_endpoint* read_ep;
106 grpc_endpoint* write_ep;
107 size_t target_bytes;
108 size_t bytes_read;
109 size_t current_write_size;
110 size_t bytes_written;
111 int current_read_data;
112 uint8_t current_write_data;
113 int read_done;
114 int write_done;
115 int max_write_frame_size;
116 grpc_slice_buffer incoming;
117 grpc_slice_buffer outgoing;
118 grpc_closure done_read;
119 grpc_closure done_write;
120 grpc_closure read_scheduler;
121 grpc_closure write_scheduler;
122 };
123
124 static void read_scheduler(void* data, grpc_error_handle /* error */) {
125 struct read_and_write_test_state* state =
126 static_cast<struct read_and_write_test_state*>(data);
127 grpc_endpoint_read(state->read_ep, &state->incoming, &state->done_read,
128 /*urgent=*/false, /*min_progress_size=*/1);
129 }
130
131 static void read_and_write_test_read_handler(void* data,
132 grpc_error_handle error) {
133 struct read_and_write_test_state* state =
134 static_cast<struct read_and_write_test_state*>(data);
135
136 state->bytes_read += count_slices(
137 state->incoming.slices, state->incoming.count, &state->current_read_data);
138 if (state->bytes_read == state->target_bytes || !error.ok()) {
139 gpr_log(GPR_DEBUG, "Read handler done");
140 gpr_mu_lock(g_mu);
141 state->read_done = 1 + (error.ok());
142 GRPC_LOG_IF_ERROR("pollset_kick", grpc_pollset_kick(g_pollset, nullptr));
143 gpr_mu_unlock(g_mu);
144 } else if (error.ok()) {
145 /* We perform many reads one after another. If grpc_endpoint_read and the
146 * read_handler are both run inline, we might end up growing the stack
147 * beyond the limit. Schedule the read on ExecCtx to avoid this. */
148 grpc_core::ExecCtx::Run(DEBUG_LOCATION, &state->read_scheduler,
149 absl::OkStatus());
150 }
151 }
152
153 static void write_scheduler(void* data, grpc_error_handle /* error */) {
154 struct read_and_write_test_state* state =
155 static_cast<struct read_and_write_test_state*>(data);
156 grpc_endpoint_write(state->write_ep, &state->outgoing, &state->done_write,
157 nullptr, /*max_frame_size=*/state->max_write_frame_size);
158 }
159
160 static void read_and_write_test_write_handler(void* data,
161 grpc_error_handle error) {
162 struct read_and_write_test_state* state =
163 static_cast<struct read_and_write_test_state*>(data);
164 grpc_slice* slices = nullptr;
165 size_t nslices;
166
167 if (error.ok()) {
168 state->bytes_written += state->current_write_size;
169 if (state->target_bytes - state->bytes_written <
170 state->current_write_size) {
171 state->current_write_size = state->target_bytes - state->bytes_written;
172 }
173 if (state->current_write_size != 0) {
174 slices = allocate_blocks(state->current_write_size, 8192, &nslices,
175 &state->current_write_data);
176 grpc_slice_buffer_reset_and_unref(&state->outgoing);
177 grpc_slice_buffer_addn(&state->outgoing, slices, nslices);
178 /* We perform many writes one after another. If grpc_endpoint_write and
179 * the write_handler are both run inline, we might end up growing the
180 * stack beyond the limit. Schedule the write on ExecCtx to avoid this. */
181 grpc_core::ExecCtx::Run(DEBUG_LOCATION, &state->write_scheduler,
182 absl::OkStatus());
183 gpr_free(slices);
184 return;
185 }
186 }
187
188 gpr_log(GPR_DEBUG, "Write handler done");
189 gpr_mu_lock(g_mu);
190 state->write_done = 1 + (error.ok());
191 GRPC_LOG_IF_ERROR("pollset_kick", grpc_pollset_kick(g_pollset, nullptr));
192 gpr_mu_unlock(g_mu);
193 }
194
195 /* Do both reading and writing using the grpc_endpoint API.
196
197 This also includes a test of the shutdown behavior.
198 */
199 static void read_and_write_test(grpc_endpoint_test_config config,
200 size_t num_bytes, size_t write_size,
201 size_t slice_size, int max_write_frame_size,
202 bool shutdown) {
203 struct read_and_write_test_state state;
204 grpc_endpoint_test_fixture f =
205 begin_test(config, "read_and_write_test", slice_size);
206 grpc_core::ExecCtx exec_ctx;
207 auto deadline = grpc_core::Timestamp::FromTimespecRoundUp(
208 grpc_timeout_seconds_to_deadline(300));
209 gpr_log(GPR_DEBUG,
210 "num_bytes=%" PRIuPTR " write_size=%" PRIuPTR " slice_size=%" PRIuPTR
211 " shutdown=%d",
212 num_bytes, write_size, slice_size, shutdown);
213
214 if (shutdown) {
215 gpr_log(GPR_INFO, "Start read and write shutdown test");
216 } else {
217 gpr_log(GPR_INFO,
218 "Start read and write test with %" PRIuPTR
219 " bytes, slice size %" PRIuPTR,
220 num_bytes, slice_size);
221 }
222
223 state.read_ep = f.client_ep;
224 state.write_ep = f.server_ep;
225 state.target_bytes = num_bytes;
226 state.bytes_read = 0;
227 state.current_write_size = write_size;
228 state.max_write_frame_size = max_write_frame_size;
229 state.bytes_written = 0;
230 state.read_done = 0;
231 state.write_done = 0;
232 state.current_read_data = 0;
233 state.current_write_data = 0;
234 GRPC_CLOSURE_INIT(&state.read_scheduler, read_scheduler, &state,
235 grpc_schedule_on_exec_ctx);
236 GRPC_CLOSURE_INIT(&state.done_read, read_and_write_test_read_handler, &state,
237 grpc_schedule_on_exec_ctx);
238 GRPC_CLOSURE_INIT(&state.write_scheduler, write_scheduler, &state,
239 grpc_schedule_on_exec_ctx);
240 GRPC_CLOSURE_INIT(&state.done_write, read_and_write_test_write_handler,
241 &state, grpc_schedule_on_exec_ctx);
242 grpc_slice_buffer_init(&state.outgoing);
243 grpc_slice_buffer_init(&state.incoming);
244
245 /* Get started by pretending an initial write completed */
246 /* NOTE: Sets up initial conditions so we can have the same write handler
247 for the first iteration as for later iterations. It does the right thing
248 even when bytes_written is unsigned. */
249 state.bytes_written -= state.current_write_size;
250 read_and_write_test_write_handler(&state, absl::OkStatus());
251 grpc_core::ExecCtx::Get()->Flush();
252
253 grpc_endpoint_read(state.read_ep, &state.incoming, &state.done_read,
254 /*urgent=*/false, /*min_progress_size=*/1);
255 if (shutdown) {
256 gpr_log(GPR_DEBUG, "shutdown read");
257 grpc_endpoint_shutdown(state.read_ep, GRPC_ERROR_CREATE("Test Shutdown"));
258 gpr_log(GPR_DEBUG, "shutdown write");
259 grpc_endpoint_shutdown(state.write_ep, GRPC_ERROR_CREATE("Test Shutdown"));
260 }
261 grpc_core::ExecCtx::Get()->Flush();
262
263 gpr_mu_lock(g_mu);
264 while (!state.read_done || !state.write_done) {
265 grpc_pollset_worker* worker = nullptr;
266 GPR_ASSERT(grpc_core::Timestamp::Now() < deadline);
267 GPR_ASSERT(GRPC_LOG_IF_ERROR(
268 "pollset_work", grpc_pollset_work(g_pollset, &worker, deadline)));
269 }
270 gpr_mu_unlock(g_mu);
271 grpc_core::ExecCtx::Get()->Flush();
272
273 end_test(config);
274 grpc_slice_buffer_destroy(&state.outgoing);
275 grpc_slice_buffer_destroy(&state.incoming);
276 grpc_endpoint_destroy(state.read_ep);
277 grpc_endpoint_destroy(state.write_ep);
278 }
279
280 static void inc_on_failure(void* arg, grpc_error_handle error) {
281 gpr_mu_lock(g_mu);
282 *static_cast<int*>(arg) += (!error.ok());
283 GPR_ASSERT(GRPC_LOG_IF_ERROR("kick", grpc_pollset_kick(g_pollset, nullptr)));
284 gpr_mu_unlock(g_mu);
285 }
286
287 static void wait_for_fail_count(int* fail_count, int want_fail_count) {
288 grpc_core::ExecCtx::Get()->Flush();
289 gpr_mu_lock(g_mu);
290 grpc_core::Timestamp deadline = grpc_core::Timestamp::FromTimespecRoundUp(
291 grpc_timeout_seconds_to_deadline(10));
292 while (grpc_core::Timestamp::Now() < deadline &&
293 *fail_count < want_fail_count) {
294 grpc_pollset_worker* worker = nullptr;
295 GPR_ASSERT(GRPC_LOG_IF_ERROR(
296 "pollset_work", grpc_pollset_work(g_pollset, &worker, deadline)));
297 gpr_mu_unlock(g_mu);
298 grpc_core::ExecCtx::Get()->Flush();
299 gpr_mu_lock(g_mu);
300 }
301 GPR_ASSERT(*fail_count == want_fail_count);
302 gpr_mu_unlock(g_mu);
303 }
304
305 static void multiple_shutdown_test(grpc_endpoint_test_config config) {
306 grpc_endpoint_test_fixture f =
307 begin_test(config, "multiple_shutdown_test", 128);
308 int fail_count = 0;
309 grpc_slice_buffer slice_buffer;
310 grpc_slice_buffer_init(&slice_buffer);
311
312 grpc_core::ExecCtx exec_ctx;
313 grpc_endpoint_add_to_pollset(f.client_ep, g_pollset);
314 grpc_endpoint_read(f.client_ep, &slice_buffer,
315 GRPC_CLOSURE_CREATE(inc_on_failure, &fail_count,
316 grpc_schedule_on_exec_ctx),
317 /*urgent=*/false, /*min_progress_size=*/1);
318 wait_for_fail_count(&fail_count, 0);
319 grpc_endpoint_shutdown(f.client_ep, GRPC_ERROR_CREATE("Test Shutdown"));
320 wait_for_fail_count(&fail_count, 1);
321 grpc_endpoint_read(f.client_ep, &slice_buffer,
322 GRPC_CLOSURE_CREATE(inc_on_failure, &fail_count,
323 grpc_schedule_on_exec_ctx),
324 /*urgent=*/false, /*min_progress_size=*/1);
325 wait_for_fail_count(&fail_count, 2);
326 grpc_slice_buffer_add(&slice_buffer, grpc_slice_from_copied_string("a"));
327 grpc_endpoint_write(f.client_ep, &slice_buffer,
328 GRPC_CLOSURE_CREATE(inc_on_failure, &fail_count,
329 grpc_schedule_on_exec_ctx),
330 nullptr, /*max_frame_size=*/INT_MAX);
331 wait_for_fail_count(&fail_count, 3);
332 grpc_endpoint_shutdown(f.client_ep, GRPC_ERROR_CREATE("Test Shutdown"));
333 wait_for_fail_count(&fail_count, 3);
334
335 grpc_slice_buffer_destroy(&slice_buffer);
336
337 grpc_endpoint_destroy(f.client_ep);
338 grpc_endpoint_destroy(f.server_ep);
339 }
340
341 void grpc_endpoint_tests(grpc_endpoint_test_config config,
342 grpc_pollset* pollset, gpr_mu* mu) {
343 size_t i;
344 g_pollset = pollset;
345 g_mu = mu;
346 multiple_shutdown_test(config);
347 for (int i = 1; i <= 10000; i = i * 10) {
348 read_and_write_test(config, 10000000, 100000, 8192, i, false);
349 read_and_write_test(config, 1000000, 100000, 1, i, false);
350 read_and_write_test(config, 100000000, 100000, 1, i, true);
351 }
352 for (i = 1; i < 1000; i = std::max(i + 1, i * 5 / 4)) {
353 read_and_write_test(config, 40320, i, i, i, false);
354 }
355 g_pollset = nullptr;
356 g_mu = nullptr;
357 }