test-poll-oob.c
Go to the documentation of this file.
1 /* Copyright libuv project contributors. All rights reserved.
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to
5  * deal in the Software without restriction, including without limitation the
6  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7  * sell copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19  * IN THE SOFTWARE.
20  */
21 
22 #if !defined(_WIN32)
23 
24 #include "uv.h"
25 #include "task.h"
26 
27 #include <errno.h>
28 #include <sys/socket.h>
29 #include <sys/ioctl.h>
30 #include <unistd.h>
31 #include <string.h>
32 
36 static uv_poll_t poll_req[2];
37 static uv_idle_t idle;
40 static int ticks;
41 static const int kMaxTicks = 10;
42 static int cli_pr_check = 0;
43 static int cli_rd_check = 0;
44 static int srv_rd_check = 0;
45 
46 static int got_eagain(void) {
47  return errno == EAGAIN
48  || errno == EINPROGRESS
49 #ifdef EWOULDBLOCK
50  || errno == EWOULDBLOCK
51 #endif
52  ;
53 }
54 
55 static void idle_cb(uv_idle_t* idle) {
56  uv_sleep(100);
57  if (++ticks < kMaxTicks)
58  return;
59 
64  uv_close((uv_handle_t*) &peer_handle, NULL);
65  uv_close((uv_handle_t*) idle, NULL);
66 }
67 
68 static void poll_cb(uv_poll_t* handle, int status, int events) {
69  char buffer[5];
70  int n;
71  int fd;
72 
73  ASSERT(0 == uv_fileno((uv_handle_t*)handle, &fd));
74  memset(buffer, 0, 5);
75 
76  if (events & UV_PRIORITIZED) {
77  do
78  n = recv(client_fd, &buffer, 5, MSG_OOB);
79  while (n == -1 && errno == EINTR);
80  ASSERT(n >= 0 || errno != EINVAL);
81  cli_pr_check = 1;
82  ASSERT(0 == uv_poll_stop(&poll_req[0]));
83  ASSERT(0 == uv_poll_start(&poll_req[0],
85  poll_cb));
86  }
87  if (events & UV_READABLE) {
88  if (fd == client_fd) {
89  do
90  n = recv(client_fd, &buffer, 5, 0);
91  while (n == -1 && errno == EINTR);
92  ASSERT(n >= 0 || errno != EINVAL);
93  if (cli_rd_check == 1) {
94  ASSERT(strncmp(buffer, "world", n) == 0);
95  ASSERT(5 == n);
96  cli_rd_check = 2;
97  }
98  if (cli_rd_check == 0) {
99  ASSERT(n == 4);
100  ASSERT(strncmp(buffer, "hello", n) == 0);
101  cli_rd_check = 1;
102  do {
103  do
104  n = recv(server_fd, &buffer, 5, 0);
105  while (n == -1 && errno == EINTR);
106  if (n > 0) {
107  ASSERT(n == 5);
108  ASSERT(strncmp(buffer, "world", n) == 0);
109  cli_rd_check = 2;
110  }
111  } while (n > 0);
112 
113  ASSERT(got_eagain());
114  }
115  }
116  if (fd == server_fd) {
117  do
118  n = recv(server_fd, &buffer, 3, 0);
119  while (n == -1 && errno == EINTR);
120  ASSERT(n >= 0 || errno != EINVAL);
121  ASSERT(3 == n);
122  ASSERT(strncmp(buffer, "foo", n) == 0);
123  srv_rd_check = 1;
124  uv_poll_stop(&poll_req[1]);
125  }
126  }
127  if (events & UV_WRITABLE) {
128  do {
129  n = send(client_fd, "foo", 3, 0);
130  } while (n < 0 && errno == EINTR);
131  ASSERT(3 == n);
132  }
133 }
134 
136  int r;
137 
138  ASSERT(0 == status);
143  ASSERT(0 == uv_poll_start(&poll_req[0],
145  poll_cb));
146  ASSERT(0 == uv_poll_start(&poll_req[1],
147  UV_READABLE,
148  poll_cb));
149  do {
150  r = send(server_fd, "hello", 5, MSG_OOB);
151  } while (r < 0 && errno == EINTR);
152  ASSERT(5 == r);
153 
154  do {
155  r = send(server_fd, "world", 5, 0);
156  } while (r < 0 && errno == EINTR);
157  ASSERT(5 == r);
158 
159  ASSERT(0 == uv_idle_start(&idle, idle_cb));
160 }
161 
162 
163 TEST_IMPL(poll_oob) {
164  struct sockaddr_in addr;
165  int r = 0;
166  uv_loop_t* loop;
167 
168  ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
169  loop = uv_default_loop();
170 
174  ASSERT(0 == uv_idle_init(loop, &idle));
175  ASSERT(0 == uv_tcp_bind(&server_handle, (const struct sockaddr*) &addr, 0));
177 
178  /* Ensure two separate packets */
180 
181  client_fd = socket(PF_INET, SOCK_STREAM, 0);
182  ASSERT(client_fd >= 0);
183  do {
184  errno = 0;
185  r = connect(client_fd, (const struct sockaddr*)&addr, sizeof(addr));
186  } while (r == -1 && errno == EINTR);
187  ASSERT(r == 0);
188 
190 
191  ASSERT(ticks == kMaxTicks);
192 
193  /* Did client receive the POLLPRI message */
194  ASSERT(cli_pr_check == 1);
195  /* Did client receive the POLLIN message */
196  ASSERT(cli_rd_check == 2);
197  /* Could we write with POLLOUT and did the server receive our POLLOUT message
198  * through POLLIN.
199  */
200  ASSERT(srv_rd_check == 1);
201 
203  return 0;
204 }
205 
206 #else
207 
208 typedef int file_has_no_tests; /* ISO C forbids an empty translation unit. */
209 
210 #endif
async_greeter_server_with_graceful_shutdown.loop
loop
Definition: async_greeter_server_with_graceful_shutdown.py:59
idle
static uv_idle_t idle
Definition: test-poll-oob.c:37
task.h
UV_PRIORITIZED
@ UV_PRIORITIZED
Definition: uv.h:793
memset
return memset(p, 0, total)
connection_cb
static void connection_cb(uv_stream_t *handle, int status)
Definition: test-poll-oob.c:135
server_fd
static uv_os_fd_t server_fd
Definition: test-poll-oob.c:39
UV_WRITABLE
@ UV_WRITABLE
Definition: uv.h:791
uv_poll_init_socket
UV_EXTERN int uv_poll_init_socket(uv_loop_t *loop, uv_poll_t *handle, uv_os_sock_t socket)
Definition: unix/poll.c:96
string.h
uv_listen
UV_EXTERN int uv_listen(uv_stream_t *stream, int backlog, uv_connection_cb cb)
Definition: unix/stream.c:656
ASSERT
#define ASSERT(expr)
Definition: task.h:102
status
absl::Status status
Definition: rls.cc:251
poll_cb
static void poll_cb(uv_poll_t *handle, int status, int events)
Definition: test-poll-oob.c:68
uv_run
UV_EXTERN int uv_run(uv_loop_t *, uv_run_mode mode)
Definition: unix/core.c:361
uv_poll_start
UV_EXTERN int uv_poll_start(uv_poll_t *handle, int events, uv_poll_cb cb)
Definition: unix/poll.c:118
uv_tcp_bind
UV_EXTERN int uv_tcp_bind(uv_tcp_t *handle, const struct sockaddr *addr, unsigned int flags)
Definition: uv-common.c:277
cli_rd_check
static int cli_rd_check
Definition: test-poll-oob.c:43
TEST_PORT
#define TEST_PORT
Definition: task.h:53
uv_close
UV_EXTERN void uv_close(uv_handle_t *handle, uv_close_cb close_cb)
Definition: unix/core.c:112
uv_stream_s
Definition: uv.h:491
uv_ip4_addr
UV_EXTERN int uv_ip4_addr(const char *ip, int port, struct sockaddr_in *addr)
Definition: uv-common.c:221
TEST_IMPL
TEST_IMPL(poll_oob)
Definition: test-poll-oob.c:163
uv_default_loop
UV_EXTERN uv_loop_t * uv_default_loop(void)
Definition: uv-common.c:733
client_fd
static uv_os_fd_t client_fd
Definition: test-poll-oob.c:38
srv_rd_check
static int srv_rd_check
Definition: test-poll-oob.c:44
UV_RUN_DEFAULT
@ UV_RUN_DEFAULT
Definition: uv.h:254
uv_tcp_init
UV_EXTERN int uv_tcp_init(uv_loop_t *, uv_tcp_t *handle)
Definition: unix/tcp.c:143
uv_accept
UV_EXTERN int uv_accept(uv_stream_t *server, uv_stream_t *client)
Definition: unix/stream.c:591
buffer
char buffer[1024]
Definition: libuv/docs/code/idle-compute/main.c:8
client_handle
static uv_tcp_t client_handle
Definition: test-poll-oob.c:34
UV_READABLE
@ UV_READABLE
Definition: uv.h:790
uv_sleep
UV_EXTERN void uv_sleep(unsigned int msec)
Definition: unix/core.c:1521
server_handle
static uv_tcp_t server_handle
Definition: test-poll-oob.c:33
idle_cb
static void idle_cb(uv_idle_t *idle)
Definition: test-poll-oob.c:55
n
int n
Definition: abseil-cpp/absl/container/btree_test.cc:1080
uv_tcp_s
Definition: uv.h:544
peer_handle
static uv_tcp_t peer_handle
Definition: test-poll-oob.c:35
uv_idle_init
UV_EXTERN int uv_idle_init(uv_loop_t *, uv_idle_t *idle)
kMaxTicks
static const int kMaxTicks
Definition: test-poll-oob.c:41
uv.h
MAKE_VALGRIND_HAPPY
#define MAKE_VALGRIND_HAPPY()
Definition: task.h:229
uv_fileno
UV_EXTERN int uv_fileno(const uv_handle_t *handle, uv_os_fd_t *fd)
Definition: unix/core.c:755
uv_idle_start
UV_EXTERN int uv_idle_start(uv_idle_t *idle, uv_idle_cb cb)
uv_poll_s
Definition: uv.h:783
uv_os_fd_t
int uv_os_fd_t
Definition: unix.h:128
cli_pr_check
static int cli_pr_check
Definition: test-poll-oob.c:42
file_has_no_tests
int file_has_no_tests
Definition: test-fs-fd-hash.c:131
uv_idle_s
Definition: uv.h:824
fix_build_deps.r
r
Definition: fix_build_deps.py:491
uv_poll_stop
UV_EXTERN int uv_poll_stop(uv_poll_t *handle)
Definition: unix/poll.c:111
uv_tcp_nodelay
UV_EXTERN int uv_tcp_nodelay(uv_tcp_t *handle, int enable)
Definition: unix/tcp.c:407
got_eagain
static int got_eagain(void)
Definition: test-poll-oob.c:46
handle
static csh handle
Definition: test_arm_regression.c:16
uv_handle_s
Definition: uv.h:441
test_server.socket
socket
Definition: test_server.py:65
uv_loop_s
Definition: uv.h:1767
ticks
static int ticks
Definition: test-poll-oob.c:40
poll_req
static uv_poll_t poll_req[2]
Definition: test-poll-oob.c:36
addr
struct sockaddr_in addr
Definition: libuv/docs/code/tcp-echo-server/main.c:10
errno.h


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:26