test-delayed-accept.c
Go to the documentation of this file.
1 /* Copyright Joyent, Inc. and other Node 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 #include "uv.h"
23 #include "task.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 
27 static int connection_cb_called = 0;
28 static int do_accept_called = 0;
29 static int close_cb_called = 0;
30 static int connect_cb_called = 0;
31 
32 
33 static void alloc_cb(uv_handle_t* handle, size_t size, uv_buf_t* buf) {
34  buf->base = malloc(size);
35  buf->len = size;
36 }
37 
38 
39 static void close_cb(uv_handle_t* handle) {
40  ASSERT(handle != NULL);
41 
42  free(handle);
43 
45 }
46 
47 
50  uv_tcp_t* accepted_handle = (uv_tcp_t*)malloc(sizeof *accepted_handle);
51  int r;
52 
53  ASSERT(timer_handle != NULL);
54  ASSERT(accepted_handle != NULL);
55 
56  r = uv_tcp_init(uv_default_loop(), accepted_handle);
57  ASSERT(r == 0);
58 
59  server = (uv_tcp_t*)timer_handle->data;
60  r = uv_accept((uv_stream_t*)server, (uv_stream_t*)accepted_handle);
61  ASSERT(r == 0);
62 
64 
65  /* Immediately close the accepted handle. */
66  uv_close((uv_handle_t*)accepted_handle, close_cb);
67 
68  /* After accepting the two clients close the server handle */
69  if (do_accept_called == 2) {
71  }
72 
73  /* Dispose the timer. */
75 }
76 
77 
78 static void connection_cb(uv_stream_t* tcp, int status) {
79  int r;
81 
82  ASSERT(status == 0);
83 
84  timer_handle = (uv_timer_t*)malloc(sizeof *timer_handle);
85  ASSERT(timer_handle != NULL);
86 
87  /* Accept the client after 1 second */
89  ASSERT(r == 0);
90 
91  timer_handle->data = tcp;
92 
94  ASSERT(r == 0);
95 
97 }
98 
99 
100 static void start_server(void) {
101  struct sockaddr_in addr;
102  uv_tcp_t* server = (uv_tcp_t*)malloc(sizeof *server);
103  int r;
104 
105  ASSERT(0 == uv_ip4_addr("0.0.0.0", TEST_PORT, &addr));
106  ASSERT(server != NULL);
107 
109  ASSERT(r == 0);
110  r = uv_tcp_bind(server, (const struct sockaddr*) &addr, 0);
111  ASSERT(r == 0);
112 
114  ASSERT(r == 0);
115 }
116 
117 
118 static void read_cb(uv_stream_t* tcp, ssize_t nread, const uv_buf_t* buf) {
119  /* The server will not send anything, it should close gracefully. */
120 
121  if (buf->base) {
122  free(buf->base);
123  }
124 
125  if (nread >= 0) {
126  ASSERT(nread == 0);
127  } else {
128  ASSERT(tcp != NULL);
129  ASSERT(nread == UV_EOF);
131  }
132 }
133 
134 
135 static void connect_cb(uv_connect_t* req, int status) {
136  int r;
137 
138  ASSERT(req != NULL);
139  ASSERT(status == 0);
140 
141  /* Not that the server will send anything, but otherwise we'll never know
142  * when the server closes the connection. */
144  ASSERT(r == 0);
145 
147 
148  free(req);
149 }
150 
151 
152 static void client_connect(void) {
153  struct sockaddr_in addr;
154  uv_tcp_t* client = (uv_tcp_t*)malloc(sizeof *client);
155  uv_connect_t* connect_req = malloc(sizeof *connect_req);
156  int r;
157 
158  ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
159  ASSERT(client != NULL);
160  ASSERT(connect_req != NULL);
161 
163  ASSERT(r == 0);
164 
166  client,
167  (const struct sockaddr*) &addr,
168  connect_cb);
169  ASSERT(r == 0);
170 }
171 
172 
173 
174 TEST_IMPL(delayed_accept) {
175  start_server();
176 
177  client_connect();
178  client_connect();
179 
181 
183  ASSERT(do_accept_called == 2);
185  ASSERT(close_cb_called == 7);
186 
188  return 0;
189 }
alloc_cb
static void alloc_cb(uv_handle_t *handle, size_t size, uv_buf_t *buf)
Definition: test-delayed-accept.c:33
task.h
uv_connect_s
Definition: uv.h:580
connect_cb
static void connect_cb(uv_connect_t *req, int status)
Definition: test-delayed-accept.c:135
client
Definition: examples/python/async_streaming/client.py:1
buf
voidpf void * buf
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
uv_listen
UV_EXTERN int uv_listen(uv_stream_t *stream, int backlog, uv_connection_cb cb)
Definition: unix/stream.c:656
uv_connect_s::handle
uv_stream_t * handle
Definition: uv.h:583
ASSERT
#define ASSERT(expr)
Definition: task.h:102
tcp
static uv_tcp_t tcp
Definition: test-connection-fail.c:29
start_server
static void start_server(void)
Definition: test-delayed-accept.c:100
status
absl::Status status
Definition: rls.cc:251
client_connect
static void client_connect(void)
Definition: test-delayed-accept.c:152
uv_run
UV_EXTERN int uv_run(uv_loop_t *, uv_run_mode mode)
Definition: unix/core.c:361
do_accept_called
static int do_accept_called
Definition: test-delayed-accept.c:28
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
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_tcp_connect
UV_EXTERN int uv_tcp_connect(uv_connect_t *req, uv_tcp_t *handle, const struct sockaddr *addr, uv_connect_cb cb)
Definition: uv-common.c:315
read_cb
static void read_cb(uv_stream_t *tcp, ssize_t nread, const uv_buf_t *buf)
Definition: test-delayed-accept.c:118
uv_ip4_addr
UV_EXTERN int uv_ip4_addr(const char *ip, int port, struct sockaddr_in *addr)
Definition: uv-common.c:221
server
std::unique_ptr< Server > server
Definition: channelz_service_test.cc:330
uv_default_loop
UV_EXTERN uv_loop_t * uv_default_loop(void)
Definition: uv-common.c:733
ssize_t
intptr_t ssize_t
Definition: win.h:27
req
static uv_connect_t req
Definition: test-connection-fail.c:30
UV_RUN_DEFAULT
@ UV_RUN_DEFAULT
Definition: uv.h:254
uv_read_start
UV_EXTERN int uv_read_start(uv_stream_t *, uv_alloc_cb alloc_cb, uv_read_cb read_cb)
Definition: unix/stream.c:1555
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
uv_timer_s
Definition: uv.h:850
uv_tcp_s
Definition: uv.h:544
uv.h
MAKE_VALGRIND_HAPPY
#define MAKE_VALGRIND_HAPPY()
Definition: task.h:229
connection_cb_called
static int connection_cb_called
Definition: test-delayed-accept.c:27
uv_buf_t
Definition: unix.h:121
do_accept
static void do_accept(uv_timer_t *timer_handle)
Definition: test-delayed-accept.c:48
connect_req
static uv_connect_t connect_req
Definition: benchmark-tcp-write-batch.c:39
server
Definition: examples/python/async_streaming/server.py:1
fix_build_deps.r
r
Definition: fix_build_deps.py:491
TEST_IMPL
TEST_IMPL(delayed_accept)
Definition: test-delayed-accept.c:174
close_cb
static void close_cb(uv_handle_t *handle)
Definition: test-delayed-accept.c:39
handle
static csh handle
Definition: test_arm_regression.c:16
uv_handle_s
Definition: uv.h:441
uv_timer_start
UV_EXTERN int uv_timer_start(uv_timer_t *handle, uv_timer_cb cb, uint64_t timeout, uint64_t repeat)
Definition: timer.c:66
connection_cb
static void connection_cb(uv_stream_t *tcp, int status)
Definition: test-delayed-accept.c:78
uv_timer_init
UV_EXTERN int uv_timer_init(uv_loop_t *, uv_timer_t *handle)
Definition: timer.c:58
close_cb_called
static int close_cb_called
Definition: test-delayed-accept.c:29
timer_handle
static uv_timer_t timer_handle
Definition: benchmark-loop-count.c:32
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
connect_cb_called
static int connect_cb_called
Definition: test-delayed-accept.c:30
addr
struct sockaddr_in addr
Definition: libuv/docs/code/tcp-echo-server/main.c:10


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