test-ref.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 
25 #include <stdlib.h>
26 #include <string.h>
27 
28 
32 
33 static char buffer[32767];
34 
35 static int req_cb_called;
36 static int connect_cb_called;
37 static int write_cb_called;
38 static int shutdown_cb_called;
39 static int close_cb_called;
40 
41 
42 static void close_cb(uv_handle_t* handle) {
44 }
45 
46 
47 static void do_close(void* handle) {
48  close_cb_called = 0;
50  ASSERT(close_cb_called == 0);
52  ASSERT(close_cb_called == 1);
53 }
54 
55 
56 static void fail_cb(void) {
57  FATAL("fail_cb should not have been called");
58 }
59 
60 
61 static void fail_cb2(void) {
62  ASSERT(0 && "fail_cb2 should not have been called");
63 }
64 
65 
66 static void req_cb(uv_handle_t* req, int status) {
67  req_cb_called++;
68 }
69 
70 
71 static void shutdown_cb(uv_shutdown_t* req, int status) {
72  ASSERT(req == &shutdown_req);
74 }
75 
76 
77 static void write_cb(uv_write_t* req, int status) {
78  ASSERT(req == &write_req);
81 }
82 
83 
86  ASSERT(req == &connect_req);
87  ASSERT(status == 0);
90 }
91 
92 
93 
95  ASSERT(req == &connect_req);
96  ASSERT(status == 0);
99 }
100 
101 
105  return 0;
106 }
107 
108 
109 TEST_IMPL(idle_ref) {
110  uv_idle_t h;
113  uv_unref((uv_handle_t*)&h);
115  do_close(&h);
117  return 0;
118 }
119 
120 
121 TEST_IMPL(async_ref) {
122  uv_async_t h;
123  uv_async_init(uv_default_loop(), &h, NULL);
124  uv_unref((uv_handle_t*)&h);
126  do_close(&h);
128  return 0;
129 }
130 
131 
132 TEST_IMPL(prepare_ref) {
133  uv_prepare_t h;
136  uv_unref((uv_handle_t*)&h);
138  do_close(&h);
140  return 0;
141 }
142 
143 
144 TEST_IMPL(check_ref) {
145  uv_check_t h;
148  uv_unref((uv_handle_t*)&h);
150  do_close(&h);
152  return 0;
153 }
154 
155 
156 static void prepare_cb(uv_prepare_t* h) {
157  ASSERT(h != NULL);
158  uv_unref((uv_handle_t*)h);
159 }
160 
161 
162 TEST_IMPL(unref_in_prepare_cb) {
163  uv_prepare_t h;
167  do_close(&h);
169  return 0;
170 }
171 
172 
173 TEST_IMPL(timer_ref) {
174  uv_timer_t h;
176  uv_unref((uv_handle_t*)&h);
178  do_close(&h);
180  return 0;
181 }
182 
183 
184 TEST_IMPL(timer_ref2) {
185  uv_timer_t h;
187  uv_timer_start(&h, (uv_timer_cb)fail_cb, 42, 42);
188  uv_unref((uv_handle_t*)&h);
190  do_close(&h);
192  return 0;
193 }
194 
195 
196 TEST_IMPL(fs_event_ref) {
197 #if defined(NO_FS_EVENTS)
198  RETURN_SKIP(NO_FS_EVENTS);
199 #endif
200  uv_fs_event_t h;
203  uv_unref((uv_handle_t*)&h);
205  do_close(&h);
207  return 0;
208 }
209 
210 
211 TEST_IMPL(fs_poll_ref) {
212  uv_fs_poll_t h;
214  uv_fs_poll_start(&h, NULL, ".", 999);
215  uv_unref((uv_handle_t*)&h);
217  do_close(&h);
219  return 0;
220 }
221 
222 
223 TEST_IMPL(tcp_ref) {
224  uv_tcp_t h;
226  uv_unref((uv_handle_t*)&h);
228  do_close(&h);
230  return 0;
231 }
232 
233 
234 TEST_IMPL(tcp_ref2) {
235  uv_tcp_t h;
238  uv_unref((uv_handle_t*)&h);
240  do_close(&h);
242  return 0;
243 }
244 
245 
246 TEST_IMPL(tcp_ref2b) {
247  uv_tcp_t h;
250  uv_unref((uv_handle_t*)&h);
253  ASSERT(close_cb_called == 1);
255  return 0;
256 }
257 
258 
259 TEST_IMPL(tcp_ref3) {
260  struct sockaddr_in addr;
261  uv_tcp_t h;
262  ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
265  &h,
266  (const struct sockaddr*) &addr,
268  uv_unref((uv_handle_t*)&h);
272  do_close(&h);
274  return 0;
275 }
276 
277 
278 TEST_IMPL(tcp_ref4) {
279  struct sockaddr_in addr;
280  uv_tcp_t h;
281  ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
284  &h,
285  (const struct sockaddr*) &addr,
287  uv_unref((uv_handle_t*)&h);
290  ASSERT(write_cb_called == 1);
292  do_close(&h);
294  return 0;
295 }
296 
297 
298 TEST_IMPL(udp_ref) {
299  uv_udp_t h;
301  uv_unref((uv_handle_t*)&h);
303  do_close(&h);
305  return 0;
306 }
307 
308 
309 TEST_IMPL(udp_ref2) {
310  struct sockaddr_in addr;
311  uv_udp_t h;
312  ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
314  uv_udp_bind(&h, (const struct sockaddr*) &addr, 0);
316  uv_unref((uv_handle_t*)&h);
318  do_close(&h);
320  return 0;
321 }
322 
323 
324 TEST_IMPL(udp_ref3) {
325  struct sockaddr_in addr;
326  uv_buf_t buf = uv_buf_init("PING", 4);
328  uv_udp_t h;
329 
330  ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
332  uv_udp_send(&req,
333  &h,
334  &buf,
335  1,
336  (const struct sockaddr*) &addr,
338  uv_unref((uv_handle_t*)&h);
340  ASSERT(req_cb_called == 1);
341  do_close(&h);
342 
344  return 0;
345 }
346 
347 
348 TEST_IMPL(pipe_ref) {
349  uv_pipe_t h;
350  uv_pipe_init(uv_default_loop(), &h, 0);
351  uv_unref((uv_handle_t*)&h);
353  do_close(&h);
355  return 0;
356 }
357 
358 
359 TEST_IMPL(pipe_ref2) {
360  uv_pipe_t h;
361  uv_pipe_init(uv_default_loop(), &h, 0);
363  uv_unref((uv_handle_t*)&h);
365  do_close(&h);
367  return 0;
368 }
369 
370 
371 TEST_IMPL(pipe_ref3) {
372  uv_pipe_t h;
373  uv_pipe_init(uv_default_loop(), &h, 0);
375  uv_unref((uv_handle_t*)&h);
379  do_close(&h);
381  return 0;
382 }
383 
384 
385 TEST_IMPL(pipe_ref4) {
386  uv_pipe_t h;
387  uv_pipe_init(uv_default_loop(), &h, 0);
389  uv_unref((uv_handle_t*)&h);
392  ASSERT(write_cb_called == 1);
394  do_close(&h);
396  return 0;
397 }
398 
399 
400 TEST_IMPL(process_ref) {
401  /* spawn_helper4 blocks indefinitely. */
402  char *argv[] = { NULL, "spawn_helper4", NULL };
404  size_t exepath_size;
405  char exepath[256];
406  uv_process_t h;
407  int r;
408 
409  memset(&options, 0, sizeof(options));
410  exepath_size = sizeof(exepath);
411 
413  ASSERT(r == 0);
414 
415  argv[0] = exepath;
416  options.file = exepath;
417  options.args = argv;
418  options.exit_cb = NULL;
419 
420  r = uv_spawn(uv_default_loop(), &h, &options);
421  ASSERT(r == 0);
422 
423  uv_unref((uv_handle_t*)&h);
425 
426  r = uv_process_kill(&h, /* SIGTERM */ 15);
427  ASSERT(r == 0);
428 
429  do_close(&h);
430 
432  return 0;
433 }
434 
435 
436 TEST_IMPL(has_ref) {
437  uv_idle_t h;
439  uv_ref((uv_handle_t*)&h);
440  ASSERT(uv_has_ref((uv_handle_t*)&h) == 1);
441  uv_unref((uv_handle_t*)&h);
442  ASSERT(uv_has_ref((uv_handle_t*)&h) == 0);
444  return 0;
445 }
fail_cb2
static void fail_cb2(void)
Definition: test-ref.c:61
uv_fs_event_s
Definition: uv.h:1533
uv_fs_poll_init
UV_EXTERN int uv_fs_poll_init(uv_loop_t *loop, uv_fs_poll_t *handle)
Definition: fs-poll.c:59
uv_process_options_s
Definition: uv.h:940
uv_process_s
Definition: uv.h:1037
req_cb
static void req_cb(uv_handle_t *req, int status)
Definition: test-ref.c:66
uv_prepare_init
UV_EXTERN int uv_prepare_init(uv_loop_t *, uv_prepare_t *prepare)
task.h
uv_pipe_connect
UV_EXTERN void uv_pipe_connect(uv_connect_t *req, uv_pipe_t *handle, const char *name, uv_connect_cb cb)
Definition: unix/pipe.c:173
memset
return memset(p, 0, total)
uv_pipe_init
UV_EXTERN int uv_pipe_init(uv_loop_t *, uv_pipe_t *handle, int ipc)
Definition: unix/pipe.c:33
uv_connect_s
Definition: uv.h:580
uv_prepare_s
Definition: uv.h:804
uv_shutdown_s
Definition: uv.h:417
string.h
options
double_dict options[]
Definition: capstone_test.c:55
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
uv_udp_send
UV_EXTERN int uv_udp_send(uv_udp_send_t *req, uv_udp_t *handle, const uv_buf_t bufs[], unsigned int nbufs, const struct sockaddr *addr, uv_udp_send_cb send_cb)
Definition: uv-common.c:408
ASSERT
#define ASSERT(expr)
Definition: task.h:102
status
absl::Status status
Definition: rls.cc:251
write_req
Definition: benchmark-tcp-write-batch.c:31
close_cb_called
static int close_cb_called
Definition: test-ref.c:39
uv_run
UV_EXTERN int uv_run(uv_loop_t *, uv_run_mode mode)
Definition: unix/core.c:361
shutdown_cb_called
static int shutdown_cb_called
Definition: test-ref.c:38
do_close
static void do_close(void *handle)
Definition: test-ref.c:47
fail_cb
static void fail_cb(void)
Definition: test-ref.c:56
uv_check_cb
void(* uv_check_cb)(uv_check_t *handle)
Definition: uv.h:321
uv_unref
UV_EXTERN void uv_unref(uv_handle_t *)
Definition: uv-common.c:522
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_alloc_cb
void(* uv_alloc_cb)(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf)
Definition: uv.h:306
uv_stream_s
Definition: uv.h:491
req_cb_called
static int req_cb_called
Definition: test-ref.c:35
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_req
static uv_connect_t connect_req
Definition: test-ref.c:31
exepath
static char exepath[1024]
Definition: benchmark-spawn.c:34
connect_and_shutdown
static void connect_and_shutdown(uv_connect_t *req, int status)
Definition: test-ref.c:94
TEST_IMPL
TEST_IMPL(ref)
Definition: test-ref.c:102
uv_udp_init
UV_EXTERN int uv_udp_init(uv_loop_t *, uv_udp_t *handle)
Definition: unix/udp.c:988
uv_default_loop
UV_EXTERN uv_loop_t * uv_default_loop(void)
Definition: uv-common.c:733
uv_check_s
Definition: uv.h:814
uv_async_s
Definition: uv.h:834
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
UV_RUN_DEFAULT
@ UV_RUN_DEFAULT
Definition: uv.h:254
uv_udp_s
Definition: uv.h:629
uv_shutdown
UV_PRIVATE_REQ_TYPES UV_EXTERN int uv_shutdown(uv_shutdown_t *req, uv_stream_t *handle, uv_shutdown_cb cb)
Definition: unix/stream.c:1259
uv_prepare_start
UV_EXTERN int uv_prepare_start(uv_prepare_t *prepare, uv_prepare_cb cb)
ref
unsigned ref
Definition: cxa_demangle.cpp:4909
uv_udp_send_cb
void(* uv_udp_send_cb)(uv_udp_send_t *req, int status)
Definition: uv.h:621
uv_tcp_init
UV_EXTERN int uv_tcp_init(uv_loop_t *, uv_tcp_t *handle)
Definition: unix/tcp.c:143
connect_and_write
static void connect_and_write(uv_connect_t *req, int status)
Definition: test-ref.c:84
uv_fs_poll_start
UV_EXTERN int uv_fs_poll_start(uv_fs_poll_t *handle, uv_fs_poll_cb poll_cb, const char *path, unsigned int interval)
Definition: fs-poll.c:66
uv_udp_recv_cb
void(* uv_udp_recv_cb)(uv_udp_t *handle, ssize_t nread, const uv_buf_t *buf, const struct sockaddr *addr, unsigned flags)
Definition: uv.h:622
close_cb
static void close_cb(uv_handle_t *handle)
Definition: test-ref.c:42
shutdown_req
static uv_shutdown_t shutdown_req
Definition: test-ref.c:30
uv_timer_s
Definition: uv.h:850
uv_tcp_s
Definition: uv.h:544
uv_spawn
UV_EXTERN int uv_spawn(uv_loop_t *loop, uv_process_t *handle, const uv_process_options_t *options)
Definition: unix/process.c:408
uv_exepath
UV_EXTERN int uv_exepath(char *buffer, size_t *size)
Definition: aix-common.c:79
uv_idle_init
UV_EXTERN int uv_idle_init(uv_loop_t *, uv_idle_t *idle)
uv_udp_send_s
Definition: uv.h:645
uv.h
uv_udp_recv_start
UV_EXTERN int uv_udp_recv_start(uv_udp_t *handle, uv_alloc_cb alloc_cb, uv_udp_recv_cb recv_cb)
Definition: uv-common.c:438
MAKE_VALGRIND_HAPPY
#define MAKE_VALGRIND_HAPPY()
Definition: task.h:229
FATAL
#define FATAL(msg)
Definition: task.h:88
exepath_size
static size_t exepath_size
Definition: benchmark-spawn.c:35
uv_async_init
UV_EXTERN int uv_async_init(uv_loop_t *, uv_async_t *async, uv_async_cb async_cb)
Definition: unix/async.c:44
uv_buf_t
Definition: unix.h:121
uv_ref
UV_EXTERN void uv_ref(uv_handle_t *)
Definition: uv-common.c:517
uv_udp_bind
UV_EXTERN int uv_udp_bind(uv_udp_t *handle, const struct sockaddr *addr, unsigned int flags)
Definition: uv-common.c:296
uv_idle_start
UV_EXTERN int uv_idle_start(uv_idle_t *idle, uv_idle_cb cb)
shutdown_cb
static void shutdown_cb(uv_shutdown_t *req, int status)
Definition: test-ref.c:71
uv_timer_cb
void(* uv_timer_cb)(uv_timer_t *handle)
Definition: uv.h:318
uv_fs_poll_s
Definition: uv.h:1544
write_cb
static void write_cb(uv_write_t *req, int status)
Definition: test-ref.c:77
uv_idle_s
Definition: uv.h:824
uv_connection_cb
void(* uv_connection_cb)(uv_stream_t *server, int status)
Definition: uv.h:315
uv_fs_event_cb
void(* uv_fs_event_cb)(uv_fs_event_t *handle, const char *filename, int events, int status)
Definition: uv.h:366
write_req
static uv_write_t write_req
Definition: test-ref.c:29
fix_build_deps.r
r
Definition: fix_build_deps.py:491
uv_pipe_s
Definition: uv.h:757
write_cb_called
static int write_cb_called
Definition: test-ref.c:37
uv_buf_init
UV_EXTERN uv_buf_t uv_buf_init(char *base, unsigned int len)
Definition: uv-common.c:157
uv_check_init
UV_EXTERN int uv_check_init(uv_loop_t *, uv_check_t *check)
uv_write_s
Definition: uv.h:522
uv_fs_event_start
UV_EXTERN int uv_fs_event_start(uv_fs_event_t *handle, uv_fs_event_cb cb, const char *path, unsigned int flags)
Definition: aix.c:727
RETURN_SKIP
#define RETURN_SKIP(explanation)
Definition: task.h:262
handle
static csh handle
Definition: test_arm_regression.c:16
uv_handle_s
Definition: uv.h:441
TEST_PIPENAME
#define TEST_PIPENAME
Definition: task.h:61
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
uv_has_ref
UV_EXTERN int uv_has_ref(const uv_handle_t *)
Definition: uv-common.c:527
uv_prepare_cb
void(* uv_prepare_cb)(uv_prepare_t *handle)
Definition: uv.h:320
uv_timer_init
UV_EXTERN int uv_timer_init(uv_loop_t *, uv_timer_t *handle)
Definition: timer.c:58
uv_fs_event_init
UV_EXTERN int uv_fs_event_init(uv_loop_t *loop, uv_fs_event_t *handle)
Definition: aix.c:717
uv_process_kill
UV_EXTERN int uv_process_kill(uv_process_t *, int signum)
Definition: unix/process.c:577
uv_idle_cb
void(* uv_idle_cb)(uv_idle_t *handle)
Definition: uv.h:322
buffer
static char buffer[32767]
Definition: test-ref.c:33
addr
struct sockaddr_in addr
Definition: libuv/docs/code/tcp-echo-server/main.c:10
prepare_cb
static void prepare_cb(uv_prepare_t *h)
Definition: test-ref.c:156
connect_cb_called
static int connect_cb_called
Definition: test-ref.c:36
uv_check_start
UV_EXTERN int uv_check_start(uv_check_t *check, uv_check_cb cb)


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:30