test-tcp-close-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 /* this test is Unix only */
23 #ifndef _WIN32
24 
25 #include "uv.h"
26 #include "task.h"
27 
28 #include <stdio.h>
29 #include <string.h>
30 
31 static struct sockaddr_in addr;
39 static unsigned int got_connections;
40 static unsigned int close_cb_called;
41 static unsigned int write_cb_called;
42 static unsigned int read_cb_called;
43 static unsigned int pending_incoming;
44 
45 static void close_cb(uv_handle_t* handle) {
47 }
48 
49 static void write_cb(uv_write_t* req, int status) {
50  ASSERT(status == 0);
52 }
53 
54 static void connect_cb(uv_connect_t* req, int status) {
55  unsigned int i;
56  uv_buf_t buf;
57  uv_stream_t* outgoing;
58 
59  if (req == &tcp_check_req) {
60  ASSERT(status != 0);
61 
62  /*
63  * Time to finish the test: close both the check and pending incoming
64  * connections
65  */
68  return;
69  }
70 
71  ASSERT(status == 0);
74  i = req - connect_reqs;
75 
76  buf = uv_buf_init("x", 1);
77  outgoing = (uv_stream_t*) &tcp_outgoing[i];
78  ASSERT(0 == uv_write(&write_reqs[i], outgoing, &buf, 1, write_cb));
79 }
80 
81 static void alloc_cb(uv_handle_t* handle, size_t size, uv_buf_t* buf) {
82  static char slab[1];
83  buf->base = slab;
84  buf->len = sizeof(slab);
85 }
86 
87 static void read_cb(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf) {
88  uv_loop_t* loop;
89  unsigned int i;
90 
93  ASSERT(0 == uv_read_stop(stream));
94  ASSERT(1 == nread);
95 
96  loop = stream->loop;
98 
99  /* Close all active incomings, except current one */
100  for (i = 0; i < got_connections; i++) {
101  if (i != pending_incoming)
103  }
104 
105  /* Close server, so no one will connect to it */
107 
108  /* Create new fd that should be one of the closed incomings */
109  ASSERT(0 == uv_tcp_init(loop, &tcp_check));
111  &tcp_check,
112  (const struct sockaddr*) &addr,
113  connect_cb));
115 }
116 
118  unsigned int i;
120 
122 
123  /* Ignore tcp_check connection */
125  return;
126 
127  /* Accept everyone */
129  ASSERT(0 == uv_tcp_init(server->loop, incoming));
131 
133  return;
134 
135  /* Once all clients are accepted - start reading */
136  for (i = 0; i < ARRAY_SIZE(tcp_incoming); i++) {
137  incoming = &tcp_incoming[i];
139  }
140 }
141 
142 TEST_IMPL(tcp_close_accept) {
143  unsigned int i;
144  uv_loop_t* loop;
145  uv_tcp_t* client;
146 
147  /*
148  * A little explanation of what goes on below:
149  *
150  * We'll create server and connect to it using two clients, each writing one
151  * byte once connected.
152  *
153  * When all clients will be accepted by server - we'll start reading from them
154  * and, on first client's first byte, will close second client and server.
155  * After that, we'll immediately initiate new connection to server using
156  * tcp_check handle (thus, reusing fd from second client).
157  *
158  * In this situation uv__io_poll()'s event list should still contain read
159  * event for second client, and, if not cleaned up properly, `tcp_check` will
160  * receive stale event of second incoming and invoke `connect_cb` with zero
161  * status.
162  */
163 
164  loop = uv_default_loop();
165  ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
166 
168  ASSERT(0 == uv_tcp_bind(&tcp_server, (const struct sockaddr*) &addr, 0));
171  connection_cb));
172 
173  for (i = 0; i < ARRAY_SIZE(tcp_outgoing); i++) {
174  client = tcp_outgoing + i;
175 
176  ASSERT(0 == uv_tcp_init(loop, client));
178  client,
179  (const struct sockaddr*) &addr,
180  connect_cb));
181  }
182 
184 
188  ASSERT(1 == read_cb_called);
189 
191  return 0;
192 }
193 
194 #else
195 
196 typedef int file_has_no_tests; /* ISO C forbids an empty translation unit. */
197 
198 #endif /* !_WIN32 */
async_greeter_server_with_graceful_shutdown.loop
loop
Definition: async_greeter_server_with_graceful_shutdown.py:59
write_reqs
static uv_write_t write_reqs[ARRAY_SIZE(tcp_outgoing)]
Definition: test-tcp-close-accept.c:38
slab
static char slab[1]
Definition: test-watcher-cross-stop.c:37
TEST_IMPL
TEST_IMPL(tcp_close_accept)
Definition: test-tcp-close-accept.c:142
incoming
static uv_pipe_t incoming[4]
Definition: test-pipe-sendmsg.c:38
ARRAY_SIZE
#define ARRAY_SIZE(array)
Definition: bloaty.cc:101
task.h
uv_connect_s
Definition: uv.h:580
tcp_check_req
static uv_connect_t tcp_check_req
Definition: test-tcp-close-accept.c:37
addr
static struct sockaddr_in addr
Definition: test-tcp-close-accept.c:31
client
Definition: examples/python/async_streaming/client.py:1
write_cb_called
static unsigned int write_cb_called
Definition: test-tcp-close-accept.c:41
string.h
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
ASSERT
#define ASSERT(expr)
Definition: task.h:102
status
absl::Status status
Definition: rls.cc:251
alloc_cb
static void alloc_cb(uv_handle_t *handle, size_t size, uv_buf_t *buf)
Definition: test-tcp-close-accept.c:81
close_cb
static void close_cb(uv_handle_t *handle)
Definition: test-tcp-close-accept.c:45
uv_run
UV_EXTERN int uv_run(uv_loop_t *, uv_run_mode mode)
Definition: unix/core.c:361
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
read_cb
static void read_cb(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf)
Definition: test-tcp-close-accept.c:87
client
static uv_tcp_t client
Definition: test-callback-stack.c:33
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
uv_ip4_addr
UV_EXTERN int uv_ip4_addr(const char *ip, int port, struct sockaddr_in *addr)
Definition: uv-common.c:221
connect_cb
static void connect_cb(uv_connect_t *req, int status)
Definition: test-tcp-close-accept.c:54
got_connections
static unsigned int got_connections
Definition: test-tcp-close-accept.c:39
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
tcp_check
static uv_tcp_t tcp_check
Definition: test-tcp-close-accept.c:36
uv_write
UV_EXTERN int uv_write(uv_write_t *req, uv_stream_t *handle, const uv_buf_t bufs[], unsigned int nbufs, uv_write_cb cb)
Definition: unix/stream.c:1492
req
static uv_connect_t req
Definition: test-connection-fail.c:30
tcp_incoming
static uv_tcp_t tcp_incoming[ARRAY_SIZE(tcp_outgoing)]
Definition: test-tcp-close-accept.c:34
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_tcp_s
Definition: uv.h:544
uv.h
MAKE_VALGRIND_HAPPY
#define MAKE_VALGRIND_HAPPY()
Definition: task.h:229
uv_buf_t
Definition: unix.h:121
close_cb_called
static unsigned int close_cb_called
Definition: test-tcp-close-accept.c:40
server
Definition: examples/python/async_streaming/server.py:1
connect_reqs
static uv_connect_t connect_reqs[ARRAY_SIZE(tcp_outgoing)]
Definition: test-tcp-close-accept.c:35
file_has_no_tests
int file_has_no_tests
Definition: test-fs-fd-hash.c:131
uv_buf_init
UV_EXTERN uv_buf_t uv_buf_init(char *base, unsigned int len)
Definition: uv-common.c:157
write_cb
static void write_cb(uv_write_t *req, int status)
Definition: test-tcp-close-accept.c:49
uv_write_s
Definition: uv.h:522
handle
static csh handle
Definition: test_arm_regression.c:16
uv_handle_s
Definition: uv.h:441
uv_loop_s
Definition: uv.h:1767
connection_cb
static void connection_cb(uv_stream_t *server, int status)
Definition: test-tcp-close-accept.c:117
read_cb_called
static unsigned int read_cb_called
Definition: test-tcp-close-accept.c:42
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
pending_incoming
static unsigned int pending_incoming
Definition: test-tcp-close-accept.c:43
uv_read_stop
UV_EXTERN int uv_read_stop(uv_stream_t *)
Definition: unix/stream.c:1590
tcp_outgoing
static uv_tcp_t tcp_outgoing[2]
Definition: test-tcp-close-accept.c:33
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
tcp_server
static uv_tcp_t tcp_server
Definition: test-tcp-close-accept.c:32
stream
voidpf stream
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136


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