Debian Codegraph @

test/core/iomgr/tcp_posix_test.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 "src/core/lib/channel/channel_args.h"
20 #include "src/core/lib/gprpp/time.h"
21 #include "src/core/lib/iomgr/port.h"
22
23 // This test won't work except with posix sockets enabled
24 #ifdef GRPC_POSIX_SOCKET_TCP
25
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <limits.h>
29 #include <string.h>
30 #include <sys/socket.h>
31 #include <sys/types.h>
32 #include <unistd.h>
33
34 #include <grpc/grpc.h>
35 #include <grpc/support/alloc.h>
36 #include <grpc/support/log.h>
37 #include <grpc/support/time.h>
38
39 #include "src/core/lib/event_engine/channel_args_endpoint_config.h"
40 #include "src/core/lib/experiments/experiments.h"
41 #include "src/core/lib/gpr/useful.h"
42 #include "src/core/lib/iomgr/buffer_list.h"
43 #include "src/core/lib/iomgr/ev_posix.h"
44 #include "src/core/lib/iomgr/sockaddr_posix.h"
45 #include "src/core/lib/iomgr/socket_utils_posix.h"
46 #include "src/core/lib/iomgr/tcp_posix.h"
47 #include "src/core/lib/slice/slice_internal.h"
48 #include "test/core/iomgr/endpoint_tests.h"
49 #include "test/core/util/test_config.h"
50
51 static gpr_mu* g_mu;
52 static grpc_pollset* g_pollset;
53
54 /*
55 General test notes:
56
57 All tests which write data into a socket write i%256 into byte i, which is
58 verified by readers.
59
60 In general there are a few interesting things to vary which may lead to
61 exercising different codepaths in an implementation:
62 1. Total amount of data written to the socket
63 2. Size of slice allocations
64 3. Amount of data we read from or write to the socket at once
65
66 The tests here tend to parameterize these where applicable.
67
68 */
69
70 static void create_sockets(int sv[2]) {
71 int flags;
72 GPR_ASSERT(socketpair(AF_UNIX, SOCK_STREAM, 0, sv) == 0);
73 flags = fcntl(sv[0], F_GETFL, 0);
74 GPR_ASSERT(fcntl(sv[0], F_SETFL, flags | O_NONBLOCK) == 0);
75 flags = fcntl(sv[1], F_GETFL, 0);
76 GPR_ASSERT(fcntl(sv[1], F_SETFL, flags | O_NONBLOCK) == 0);
77 }
78
79 static void create_inet_sockets(int sv[2]) {
80 /* Prepare listening socket */
81 struct sockaddr_in addr;
82 memset(&addr, 0, sizeof(struct sockaddr_in));
83 addr.sin_family = AF_INET;
84 int sock = socket(AF_INET, SOCK_STREAM, 0);
85 GPR_ASSERT(sock);
86 GPR_ASSERT(bind(sock, (sockaddr*)&addr, sizeof(sockaddr_in)) == 0);
87 listen(sock, 1);
88
89 /* Prepare client socket and connect to server */
90 socklen_t len = sizeof(sockaddr_in);
91 GPR_ASSERT(getsockname(sock, (sockaddr*)&addr, &len) == 0);
92
93 int client = socket(AF_INET, SOCK_STREAM, 0);
94 GPR_ASSERT(client);
95 int ret;
96 do {
97 ret = connect(client, reinterpret_cast<sockaddr*>(&addr),
98 sizeof(sockaddr_in));
99 } while (ret == -1 && errno == EINTR);
100
101 /* Accept client connection */
102 len = sizeof(socklen_t);
103 int server;
104 do {
105 server = accept(sock, reinterpret_cast<sockaddr*>(&addr), &len);
106 } while (server == -1 && errno == EINTR);
107 GPR_ASSERT(server != -1);
108
109 sv[0] = server;
110 sv[1] = client;
111 int flags = fcntl(sv[0], F_GETFL, 0);
112 GPR_ASSERT(fcntl(sv[0], F_SETFL, flags | O_NONBLOCK) == 0);
113 flags = fcntl(sv[1], F_GETFL, 0);
114 GPR_ASSERT(fcntl(sv[1], F_SETFL, flags | O_NONBLOCK) == 0);
115 }
116
117 static ssize_t fill_socket(int fd) {
118 ssize_t write_bytes;
119 ssize_t total_bytes = 0;
120 int i;
121 unsigned char buf[256];
122 for (i = 0; i < 256; ++i) {
123 buf[i] = static_cast<uint8_t>(i);
124 }
125 do {
126 write_bytes = write(fd, buf, 256);
127 if (write_bytes > 0) {
128 total_bytes += write_bytes;
129 }
130 } while (write_bytes >= 0 || errno == EINTR);
131 GPR_ASSERT(errno == EAGAIN);
132 return total_bytes;
133 }
134
135 static size_t fill_socket_partial(int fd, size_t bytes) {
136 ssize_t write_bytes;
137 size_t total_bytes = 0;
138 unsigned char* buf = static_cast<unsigned char*>(gpr_malloc(bytes));
139 unsigned i;
140 for (i = 0; i < bytes; ++i) {
141 buf[i] = static_cast<uint8_t>(i % 256);
142 }
143
144 do {
145 write_bytes = write(fd, buf, bytes - total_bytes);
146 if (write_bytes > 0) {
147 total_bytes += static_cast<size_t>(write_bytes);
148 }
149 } while ((write_bytes >= 0 || errno == EINTR) && bytes > total_bytes);
150
151 gpr_free(buf);
152
153 return total_bytes;
154 }
155
156 struct read_socket_state {
157 grpc_endpoint* ep;
158 size_t min_progress_size;
159 size_t read_bytes;
160 size_t target_read_bytes;
161 grpc_slice_buffer incoming;
162 grpc_closure read_cb;
163 };
164
165 static size_t count_slices(grpc_slice* slices, size_t nslices,
166 int* current_data) {
167 size_t num_bytes = 0;
168 unsigned i, j;
169 unsigned char* buf;
170 for (i = 0; i < nslices; ++i) {
171 buf = GRPC_SLICE_START_PTR(slices[i]);
172 for (j = 0; j < GRPC_SLICE_LENGTH(slices[i]); ++j) {
173 GPR_ASSERT(buf[j] == *current_data);
174 *current_data = (*current_data + 1) % 256;
175 }
176 num_bytes += GRPC_SLICE_LENGTH(slices[i]);
177 }
178 return num_bytes;
179 }
180
181 static void read_cb(void* user_data, grpc_error_handle error) {
182 struct read_socket_state* state =
183 static_cast<struct read_socket_state*>(user_data);
184 size_t read_bytes;
185 int current_data;
186
187 GPR_ASSERT(error.ok());
188
189 gpr_mu_lock(g_mu);
190 current_data = state->read_bytes % 256;
191 // The number of bytes read each time this callback is invoked must be >=
192 // the min_progress_size.
193 if (grpc_core::IsTcpFrameSizeTuningEnabled()) {
194 GPR_ASSERT(state->min_progress_size <= state->incoming.length);
195 }
196 read_bytes = count_slices(state->incoming.slices, state->incoming.count,
197 &current_data);
198 state->read_bytes += read_bytes;
199 gpr_log(GPR_INFO, "Read %" PRIuPTR " bytes of %" PRIuPTR, read_bytes,
200 state->target_read_bytes);
201 if (state->read_bytes >= state->target_read_bytes) {
202 GPR_ASSERT(
203 GRPC_LOG_IF_ERROR("kick", grpc_pollset_kick(g_pollset, nullptr)));
204 gpr_mu_unlock(g_mu);
205 } else {
206 gpr_mu_unlock(g_mu);
207 state->min_progress_size = state->target_read_bytes - state->read_bytes;
208 grpc_endpoint_read(state->ep, &state->incoming, &state->read_cb,
209 /*urgent=*/false, state->min_progress_size);
210 }
211 }
212
213 /* Write to a socket, then read from it using the grpc_tcp API. */
214 static void read_test(size_t num_bytes, size_t slice_size,
215 int min_progress_size) {
216 int sv[2];
217 grpc_endpoint* ep;
218 struct read_socket_state state;
219 size_t written_bytes;
220 grpc_core::Timestamp deadline = grpc_core::Timestamp::FromTimespecRoundUp(
221 grpc_timeout_seconds_to_deadline(20));
222 grpc_core::ExecCtx exec_ctx;
223
224 gpr_log(GPR_INFO, "Read test of size %" PRIuPTR ", slice size %" PRIuPTR,
225 num_bytes, slice_size);
226
227 create_sockets(sv);
228
229 grpc_arg a[2];
230 a[0].key = const_cast<char*>(GRPC_ARG_TCP_READ_CHUNK_SIZE);
231 a[0].type = GRPC_ARG_INTEGER,
232 a[0].value.integer = static_cast<int>(slice_size);
233 a[1].key = const_cast<char*>(GRPC_ARG_RESOURCE_QUOTA);
234 a[1].type = GRPC_ARG_POINTER;
235 a[1].value.pointer.p = grpc_resource_quota_create("test");
236 a[1].value.pointer.vtable = grpc_resource_quota_arg_vtable();
237 grpc_channel_args args = {GPR_ARRAY_SIZE(a), a};
238 ep = grpc_tcp_create(
239 grpc_fd_create(sv[1], "read_test", false),
240 TcpOptionsFromEndpointConfig(
241 grpc_event_engine::experimental::ChannelArgsEndpointConfig(
242 grpc_core::ChannelArgs::FromC(&args))),
243 "test");
244 grpc_endpoint_add_to_pollset(ep, g_pollset);
245
246 written_bytes = fill_socket_partial(sv[0], num_bytes);
247 gpr_log(GPR_INFO, "Wrote %" PRIuPTR " bytes", written_bytes);
248
249 state.ep = ep;
250 state.read_bytes = 0;
251 state.target_read_bytes = written_bytes;
252 state.min_progress_size =
253 std::min(min_progress_size, static_cast<int>(written_bytes));
254 grpc_slice_buffer_init(&state.incoming);
255 GRPC_CLOSURE_INIT(&state.read_cb, read_cb, &state, grpc_schedule_on_exec_ctx);
256
257 grpc_endpoint_read(ep, &state.incoming, &state.read_cb, /*urgent=*/false,
258 /*min_progress_size=*/state.min_progress_size);
259
260 gpr_mu_lock(g_mu);
261 while (state.read_bytes < state.target_read_bytes) {
262 grpc_pollset_worker* worker = nullptr;
263 GPR_ASSERT(GRPC_LOG_IF_ERROR(
264 "pollset_work", grpc_pollset_work(g_pollset, &worker, deadline)));
265 gpr_mu_unlock(g_mu);
266
267 gpr_mu_lock(g_mu);
268 }
269 GPR_ASSERT(state.read_bytes == state.target_read_bytes);
270 gpr_mu_unlock(g_mu);
271
272 grpc_slice_buffer_destroy(&state.incoming);
273 grpc_endpoint_destroy(ep);
274 grpc_resource_quota_unref(
275 static_cast<grpc_resource_quota*>(a[1].value.pointer.p));
276 }
277
278 /* Write to a socket until it fills up, then read from it using the grpc_tcp
279 API. */
280 static void large_read_test(size_t slice_size, int min_progress_size) {
281 int sv[2];
282 grpc_endpoint* ep;
283 struct read_socket_state state;
284 ssize_t written_bytes;
285 grpc_core::Timestamp deadline = grpc_core::Timestamp::FromTimespecRoundUp(
286 grpc_timeout_seconds_to_deadline(20));
287 grpc_core::ExecCtx exec_ctx;
288
289 gpr_log(GPR_INFO, "Start large read test, slice size %" PRIuPTR, slice_size);
290
291 create_sockets(sv);
292
293 grpc_arg a[2];
294 a[0].key = const_cast<char*>(GRPC_ARG_TCP_READ_CHUNK_SIZE);
295 a[0].type = GRPC_ARG_INTEGER;
296 a[0].value.integer = static_cast<int>(slice_size);
297 a[1].key = const_cast<char*>(GRPC_ARG_RESOURCE_QUOTA);
298 a[1].type = GRPC_ARG_POINTER;
299 a[1].value.pointer.p = grpc_resource_quota_create("test");
300 a[1].value.pointer.vtable = grpc_resource_quota_arg_vtable();
301 grpc_channel_args args = {GPR_ARRAY_SIZE(a), a};
302 ep = grpc_tcp_create(
303 grpc_fd_create(sv[1], "large_read_test", false),
304 TcpOptionsFromEndpointConfig(
305 grpc_event_engine::experimental::ChannelArgsEndpointConfig(
306 grpc_core::ChannelArgs::FromC(&args))),
307 "test");
308 grpc_endpoint_add_to_pollset(ep, g_pollset);
309
310 written_bytes = fill_socket(sv[0]);
311 gpr_log(GPR_INFO, "Wrote %" PRIuPTR " bytes", written_bytes);
312
313 state.ep = ep;
314 state.read_bytes = 0;
315 state.target_read_bytes = static_cast<size_t>(written_bytes);
316 state.min_progress_size =
317 std::min(min_progress_size, static_cast<int>(written_bytes));
318 grpc_slice_buffer_init(&state.incoming);
319 GRPC_CLOSURE_INIT(&state.read_cb, read_cb, &state, grpc_schedule_on_exec_ctx);
320
321 grpc_endpoint_read(ep, &state.incoming, &state.read_cb, /*urgent=*/false,
322 /*min_progress_size=*/state.min_progress_size);
323
324 gpr_mu_lock(g_mu);
325 while (state.read_bytes < state.target_read_bytes) {
326 grpc_pollset_worker* worker = nullptr;
327 GPR_ASSERT(GRPC_LOG_IF_ERROR(
328 "pollset_work", grpc_pollset_work(g_pollset, &worker, deadline)));
329 gpr_mu_unlock(g_mu);
330
331 gpr_mu_lock(g_mu);
332 }
333 GPR_ASSERT(state.read_bytes == state.target_read_bytes);
334 gpr_mu_unlock(g_mu);
335
336 grpc_slice_buffer_destroy(&state.incoming);
337 grpc_endpoint_destroy(ep);
338 grpc_resource_quota_unref(
339 static_cast<grpc_resource_quota*>(a[1].value.pointer.p));
340 }
341
342 struct write_socket_state {
343 grpc_endpoint* ep;
344 int write_done;
345 };
346
347 static grpc_slice* allocate_blocks(size_t num_bytes, size_t slice_size,
348 size_t* num_blocks, uint8_t* current_data) {
349 size_t nslices = num_bytes / slice_size + (num_bytes % slice_size ? 1u : 0u);
350 grpc_slice* slices =
351 static_cast<grpc_slice*>(gpr_malloc(sizeof(grpc_slice) * nslices));
352 size_t num_bytes_left = num_bytes;
353 unsigned i, j;
354 unsigned char* buf;
355 *num_blocks = nslices;
356
357 for (i = 0; i < nslices; ++i) {
358 slices[i] = grpc_slice_malloc(slice_size > num_bytes_left ? num_bytes_left
359 : slice_size);
360 num_bytes_left -= GRPC_SLICE_LENGTH(slices[i]);
361 buf = GRPC_SLICE_START_PTR(slices[i]);
362 for (j = 0; j < GRPC_SLICE_LENGTH(slices[i]); ++j) {
363 buf[j] = *current_data;
364 (*current_data)++;
365 }
366 }
367 GPR_ASSERT(num_bytes_left == 0);
368 return slices;
369 }
370
371 static void write_done(void* user_data /* write_socket_state */,
372 grpc_error_handle error) {
373 GPR_ASSERT(error.ok());
374 struct write_socket_state* state =
375 static_cast<struct write_socket_state*>(user_data);
376 gpr_mu_lock(g_mu);
377 state->write_done = 1;
378 GPR_ASSERT(
379 GRPC_LOG_IF_ERROR("pollset_kick", grpc_pollset_kick(g_pollset, nullptr)));
380 gpr_mu_unlock(g_mu);
381 }
382
383 void drain_socket_blocking(int fd, size_t num_bytes, size_t read_size) {
384 unsigned char* buf = static_cast<unsigned char*>(gpr_malloc(read_size));
385 ssize_t bytes_read;
386 size_t bytes_left = num_bytes;
387 int flags;
388 int current = 0;
389 int i;
390 grpc_core::ExecCtx exec_ctx;
391
392 flags = fcntl(fd, F_GETFL, 0);
393 GPR_ASSERT(fcntl(fd, F_SETFL, flags & ~O_NONBLOCK) == 0);
394
395 for (;;) {
396 grpc_pollset_worker* worker = nullptr;
397 gpr_mu_lock(g_mu);
398 GPR_ASSERT(GRPC_LOG_IF_ERROR(
399 "pollset_work",
400 grpc_pollset_work(g_pollset, &worker,
401 grpc_core::Timestamp::FromTimespecRoundUp(
402 grpc_timeout_milliseconds_to_deadline(10)))));
403 gpr_mu_unlock(g_mu);
404
405 do {
406 bytes_read =
407 read(fd, buf, bytes_left > read_size ? read_size : bytes_left);
408 } while (bytes_read < 0 && errno == EINTR);
409 GPR_ASSERT(bytes_read >= 0);
410 for (i = 0; i < bytes_read; ++i) {
411 GPR_ASSERT(buf[i] == current);
412 current = (current + 1) % 256;
413 }
414 bytes_left -= static_cast<size_t>(bytes_read);
415 if (bytes_left == 0) break;
416 }
417 flags = fcntl(fd, F_GETFL, 0);
418 GPR_ASSERT(fcntl(fd, F_SETFL, flags | O_NONBLOCK) == 0);
419
420 gpr_free(buf);
421 }
422
423 /* Verifier for timestamps callback for write_test */
424 void timestamps_verifier(void* arg, grpc_core::Timestamps* ts,
425 grpc_error_handle error) {
426 GPR_ASSERT(error.ok());
427 GPR_ASSERT(arg != nullptr);
428 GPR_ASSERT(ts->sendmsg_time.time.clock_type == GPR_CLOCK_REALTIME);
429 GPR_ASSERT(ts->scheduled_time.time.clock_type == GPR_CLOCK_REALTIME);
430 GPR_ASSERT(ts->acked_time.time.clock_type == GPR_CLOCK_REALTIME);
431 gpr_atm* done_timestamps = static_cast<gpr_atm*>(arg);
432 gpr_atm_rel_store(done_timestamps, static_cast<gpr_atm>(1));
433 }
434
435 /* Write to a socket using the grpc_tcp API, then drain it directly.
436 Note that if the write does not complete immediately we need to drain the
437 socket in parallel with the read. If collect_timestamps is true, it will
438 try to get timestamps for the write. */
439 static void write_test(size_t num_bytes, size_t slice_size,
440 bool collect_timestamps) {
441 int sv[2];
442 grpc_endpoint* ep;
443 struct write_socket_state state;
444 size_t num_blocks;
445 grpc_slice* slices;
446 uint8_t current_data = 0;
447 grpc_slice_buffer outgoing;
448 grpc_closure write_done_closure;
449 grpc_core::Timestamp deadline = grpc_core::Timestamp::FromTimespecRoundUp(
450 grpc_timeout_seconds_to_deadline(20));
451 grpc_core::ExecCtx exec_ctx;
452
453 if (collect_timestamps && !grpc_event_engine_can_track_errors()) {
454 return;
455 }
456
457 gpr_log(GPR_INFO,
458 "Start write test with %" PRIuPTR " bytes, slice size %" PRIuPTR,
459 num_bytes, slice_size);
460
461 if (collect_timestamps) {
462 create_inet_sockets(sv);
463 } else {
464 create_sockets(sv);
465 }
466
467 grpc_arg a[2];
468 a[0].key = const_cast<char*>(GRPC_ARG_TCP_READ_CHUNK_SIZE);
469 a[0].type = GRPC_ARG_INTEGER,
470 a[0].value.integer = static_cast<int>(slice_size);
471 a[1].key = const_cast<char*>(GRPC_ARG_RESOURCE_QUOTA);
472 a[1].type = GRPC_ARG_POINTER;
473 a[1].value.pointer.p = grpc_resource_quota_create("test");
474 a[1].value.pointer.vtable = grpc_resource_quota_arg_vtable();
475 grpc_channel_args args = {GPR_ARRAY_SIZE(a), a};
476 ep = grpc_tcp_create(
477 grpc_fd_create(sv[1], "write_test", collect_timestamps),
478 TcpOptionsFromEndpointConfig(
479 grpc_event_engine::experimental::ChannelArgsEndpointConfig(
480 grpc_core::ChannelArgs::FromC(&args))),
481 "test");
482 grpc_endpoint_add_to_pollset(ep, g_pollset);
483
484 state.ep = ep;
485 state.write_done = 0;
486
487 slices = allocate_blocks(num_bytes, slice_size, &num_blocks, &current_data);
488
489 grpc_slice_buffer_init(&outgoing);
490 grpc_slice_buffer_addn(&outgoing, slices, num_blocks);
491 GRPC_CLOSURE_INIT(&write_done_closure, write_done, &state,
492 grpc_schedule_on_exec_ctx);
493
494 gpr_atm done_timestamps;
495 gpr_atm_rel_store(&done_timestamps, static_cast<gpr_atm>(0));
496 grpc_endpoint_write(ep, &outgoing, &write_done_closure,
497 grpc_event_engine_can_track_errors() && collect_timestamps
498 ? &done_timestamps
499 : nullptr,
500 /*max_frame_size=*/INT_MAX);
501 drain_socket_blocking(sv[0], num_bytes, num_bytes);
502 exec_ctx.Flush();
503 gpr_mu_lock(g_mu);
504 for (;;) {
505 grpc_pollset_worker* worker = nullptr;
506 if (state.write_done &&
507 (!(grpc_event_engine_can_track_errors() && collect_timestamps) ||
508 gpr_atm_acq_load(&done_timestamps) == static_cast<gpr_atm>(1))) {
509 break;
510 }
511 GPR_ASSERT(GRPC_LOG_IF_ERROR(
512 "pollset_work", grpc_pollset_work(g_pollset, &worker, deadline)));
513 gpr_mu_unlock(g_mu);
514 exec_ctx.Flush();
515 gpr_mu_lock(g_mu);
516 }
517 gpr_mu_unlock(g_mu);
518
519 grpc_slice_buffer_destroy(&outgoing);
520 grpc_endpoint_destroy(ep);
521 gpr_free(slices);
522 grpc_resource_quota_unref(
523 static_cast<grpc_resource_quota*>(a[1].value.pointer.p));
524 }
525
526 void on_fd_released(void* arg, grpc_error_handle /*errors*/) {
527 int* done = static_cast<int*>(arg);
528 *done = 1;
529 GPR_ASSERT(
530 GRPC_LOG_IF_ERROR("pollset_kick", grpc_pollset_kick(g_pollset, nullptr)));
531 }
532
533 /* Do a read_test, then release fd and try to read/write again. Verify that
534 grpc_tcp_fd() is available before the fd is released. */
535 static void release_fd_test(size_t num_bytes, size_t slice_size) {
536 int sv[2];
537 grpc_endpoint* ep;
538 struct read_socket_state state;
539 size_t written_bytes;
540 int fd;
541 grpc_core::Timestamp deadline = grpc_core::Timestamp::FromTimespecRoundUp(
542 grpc_timeout_seconds_to_deadline(20));
543 grpc_core::ExecCtx exec_ctx;
544 grpc_closure fd_released_cb;
545 int fd_released_done = 0;
546 GRPC_CLOSURE_INIT(&fd_released_cb, &on_fd_released, &fd_released_done,
547 grpc_schedule_on_exec_ctx);
548
549 gpr_log(GPR_INFO,
550 "Release fd read_test of size %" PRIuPTR ", slice size %" PRIuPTR,
551 num_bytes, slice_size);
552
553 create_sockets(sv);
554
555 grpc_arg a[2];
556 a[0].key = const_cast<char*>(GRPC_ARG_TCP_READ_CHUNK_SIZE);
557 a[0].type = GRPC_ARG_INTEGER;
558 a[0].value.integer = static_cast<int>(slice_size);
559 a[1].key = const_cast<char*>(GRPC_ARG_RESOURCE_QUOTA);
560 a[1].type = GRPC_ARG_POINTER;
561 a[1].value.pointer.p = grpc_resource_quota_create("test");
562 a[1].value.pointer.vtable = grpc_resource_quota_arg_vtable();
563 grpc_channel_args args = {GPR_ARRAY_SIZE(a), a};
564 ep = grpc_tcp_create(
565 grpc_fd_create(sv[1], "read_test", false),
566 TcpOptionsFromEndpointConfig(
567 grpc_event_engine::experimental::ChannelArgsEndpointConfig(
568 grpc_core::ChannelArgs::FromC(&args))),
569 "test");
570 GPR_ASSERT(grpc_tcp_fd(ep) == sv[1] && sv[1] >= 0);
571 grpc_endpoint_add_to_pollset(ep, g_pollset);
572
573 written_bytes = fill_socket_partial(sv[0], num_bytes);
574 gpr_log(GPR_INFO, "Wrote %" PRIuPTR " bytes", written_bytes);
575
576 state.ep = ep;
577 state.read_bytes = 0;
578 state.target_read_bytes = written_bytes;
579 state.min_progress_size = 1;
580 grpc_slice_buffer_init(&state.incoming);
581 GRPC_CLOSURE_INIT(&state.read_cb, read_cb, &state, grpc_schedule_on_exec_ctx);
582
583 grpc_endpoint_read(ep, &state.incoming, &state.read_cb, /*urgent=*/false,
584 /*min_progress_size=*/state.min_progress_size);
585
586 gpr_mu_lock(g_mu);
587 while (state.read_bytes < state.target_read_bytes) {
588 grpc_pollset_worker* worker = nullptr;
589 GPR_ASSERT(GRPC_LOG_IF_ERROR(
590 "pollset_work", grpc_pollset_work(g_pollset, &worker, deadline)));
591 gpr_log(GPR_DEBUG, "wakeup: read=%" PRIdPTR " target=%" PRIdPTR,
592 state.read_bytes, state.target_read_bytes);
593 gpr_mu_unlock(g_mu);
594 grpc_core::ExecCtx::Get()->Flush();
595 gpr_mu_lock(g_mu);
596 }
597 GPR_ASSERT(state.read_bytes == state.target_read_bytes);
598 gpr_mu_unlock(g_mu);
599
600 grpc_slice_buffer_destroy(&state.incoming);
601 grpc_tcp_destroy_and_release_fd(ep, &fd, &fd_released_cb);
602 grpc_core::ExecCtx::Get()->Flush();
603 gpr_mu_lock(g_mu);
604 while (!fd_released_done) {
605 grpc_pollset_worker* worker = nullptr;
606 GPR_ASSERT(GRPC_LOG_IF_ERROR(
607 "pollset_work", grpc_pollset_work(g_pollset, &worker, deadline)));
608 gpr_log(GPR_DEBUG, "wakeup: fd_released_done=%d", fd_released_done);
609 }
610 gpr_mu_unlock(g_mu);
611 GPR_ASSERT(fd_released_done == 1);
612 GPR_ASSERT(fd == sv[1]);
613
614 written_bytes = fill_socket_partial(sv[0], num_bytes);
615 drain_socket_blocking(fd, written_bytes, written_bytes);
616 written_bytes = fill_socket_partial(fd, num_bytes);
617 drain_socket_blocking(sv[0], written_bytes, written_bytes);
618 close(fd);
619 grpc_resource_quota_unref(
620 static_cast<grpc_resource_quota*>(a[1].value.pointer.p));
621 }
622
623 void run_tests(void) {
624 size_t i = 0;
625 for (int i = 1; i <= 8192; i = i * 2) {
626 read_test(100, 8192, i);
627 read_test(10000, 8192, i);
628 read_test(10000, 137, i);
629 read_test(10000, 1, i);
630 large_read_test(8192, i);
631 large_read_test(1, i);
632 }
633 write_test(100, 8192, false);
634 write_test(100, 1, false);
635 write_test(100000, 8192, false);
636 write_test(100000, 1, false);
637 write_test(100000, 137, false);
638
639 write_test(100, 8192, true);
640 write_test(100, 1, true);
641 write_test(100000, 8192, true);
642 write_test(100000, 1, true);
643 write_test(100, 137, true);
644
645 for (i = 1; i < 1000; i = std::max(i + 1, i * 5 / 4)) {
646 write_test(40320, i, false);
647 write_test(40320, i, true);
648 }
649
650 release_fd_test(100, 8192);
651 }
652
653 static void clean_up(void) {}
654
655 static grpc_endpoint_test_fixture create_fixture_tcp_socketpair(
656 size_t slice_size) {
657 int sv[2];
658 grpc_endpoint_test_fixture f;
659 grpc_core::ExecCtx exec_ctx;
660
661 create_sockets(sv);
662 grpc_arg a[2];
663 a[0].key = const_cast<char*>(GRPC_ARG_TCP_READ_CHUNK_SIZE);
664 a[0].type = GRPC_ARG_INTEGER;
665 a[0].value.integer = static_cast<int>(slice_size);
666 a[1].key = const_cast<char*>(GRPC_ARG_RESOURCE_QUOTA);
667 a[1].type = GRPC_ARG_POINTER;
668 a[1].value.pointer.p = grpc_resource_quota_create("test");
669 a[1].value.pointer.vtable = grpc_resource_quota_arg_vtable();
670 grpc_channel_args args = {GPR_ARRAY_SIZE(a), a};
671 f.client_ep = grpc_tcp_create(
672 grpc_fd_create(sv[0], "fixture:client", false),
673 TcpOptionsFromEndpointConfig(
674 grpc_event_engine::experimental::ChannelArgsEndpointConfig(
675 grpc_core::ChannelArgs::FromC(&args))),
676 "test");
677 f.server_ep = grpc_tcp_create(
678 grpc_fd_create(sv[1], "fixture:server", false),
679 TcpOptionsFromEndpointConfig(
680 grpc_event_engine::experimental::ChannelArgsEndpointConfig(
681 grpc_core::ChannelArgs::FromC(&args))),
682 "test");
683 grpc_endpoint_add_to_pollset(f.client_ep, g_pollset);
684 grpc_endpoint_add_to_pollset(f.server_ep, g_pollset);
685 grpc_resource_quota_unref(
686 static_cast<grpc_resource_quota*>(a[1].value.pointer.p));
687
688 return f;
689 }
690
691 static grpc_endpoint_test_config configs[] = {
692 {"tcp/tcp_socketpair", create_fixture_tcp_socketpair, clean_up},
693 };
694
695 static void destroy_pollset(void* p, grpc_error_handle /*error*/) {
696 grpc_pollset_destroy(static_cast<grpc_pollset*>(p));
697 }
698
699 int main(int argc, char** argv) {
700 grpc_closure destroyed;
701 grpc::testing::TestEnvironment env(&argc, argv);
702 grpc_init();
703 grpc_core::grpc_tcp_set_write_timestamps_callback(timestamps_verifier);
704 {
705 grpc_core::ExecCtx exec_ctx;
706 g_pollset = static_cast<grpc_pollset*>(gpr_zalloc(grpc_pollset_size()));
707 grpc_pollset_init(g_pollset, &g_mu);
708 grpc_endpoint_tests(configs[0], g_pollset, g_mu);
709 run_tests();
710 GRPC_CLOSURE_INIT(&destroyed, destroy_pollset, g_pollset,
711 grpc_schedule_on_exec_ctx);
712 grpc_pollset_shutdown(g_pollset, &destroyed);
713
714 grpc_core::ExecCtx::Get()->Flush();
715 }
716 grpc_shutdown();
717 gpr_free(g_pollset);
718
719 return 0;
720 }
721
722 #else /* GRPC_POSIX_SOCKET_TCP */
723
724 int main(int argc, char** argv) { return 1; }
725
726 #endif /* GRPC_POSIX_SOCKET_TCP */