From ac218a7ed5e8e0474a18c4716fb7eb151a685b26 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Sat, 14 Jan 2012 00:11:27 +0100 Subject: [PATCH] test: add lots of refcount tests --- test/test-fs-event.c | 18 ----- test/test-list.h | 45 +++++++---- test/test-ref.c | 169 ++++++++++++++++++++++++++++++++++++++++++ test/test-tcp-close.c | 47 ------------ test/test-timer.c | 40 ---------- 5 files changed, 199 insertions(+), 120 deletions(-) diff --git a/test/test-fs-event.c b/test/test-fs-event.c index 8b52f67f..7f02e68a 100644 --- a/test/test-fs-event.c +++ b/test/test-fs-event.c @@ -308,21 +308,3 @@ TEST_IMPL(fs_event_immediate_close) { return 0; } - - -TEST_IMPL(fs_event_unref) { - uv_loop_t* loop; - int r; - - loop = uv_default_loop(); - - r = uv_fs_event_init(loop, &fs_event, ".", fs_event_fail, 0); - ASSERT(r == 0); - - uv_unref(loop); - - r = uv_run(loop); - ASSERT(r == 0); - - return 0; -} diff --git a/test/test-list.h b/test/test-list.h index 7d2ce0a1..5ff16230 100644 --- a/test/test-list.h +++ b/test/test-list.h @@ -25,8 +25,6 @@ TEST_DECLARE (ipc_listen_before_write) TEST_DECLARE (ipc_listen_after_write) TEST_DECLARE (tcp_ping_pong) TEST_DECLARE (tcp_ping_pong_v6) -TEST_DECLARE (tcp_ref) -TEST_DECLARE (tcp_ref2) TEST_DECLARE (pipe_ping_pong) TEST_DECLARE (delayed_accept) TEST_DECLARE (multiple_listen) @@ -63,18 +61,29 @@ TEST_DECLARE (shutdown_eof) TEST_DECLARE (callback_stack) TEST_DECLARE (error_message) TEST_DECLARE (timer) -TEST_DECLARE (timer_ref) -TEST_DECLARE (timer_ref2) TEST_DECLARE (timer_again) TEST_DECLARE (idle_starvation) TEST_DECLARE (loop_handles) +TEST_DECLARE (get_loadavg) TEST_DECLARE (ref) TEST_DECLARE (idle_ref) -TEST_DECLARE (get_loadavg) TEST_DECLARE (async_ref) TEST_DECLARE (prepare_ref) TEST_DECLARE (check_ref) TEST_DECLARE (unref_in_prepare_cb) +TEST_DECLARE (timer_ref) +TEST_DECLARE (timer_ref2) +TEST_DECLARE (fs_event_ref) +TEST_DECLARE (tcp_ref) +TEST_DECLARE (tcp_ref2) +TEST_DECLARE (tcp_ref3) +TEST_DECLARE (udp_ref) +TEST_DECLARE (udp_ref2) +TEST_DECLARE (udp_ref3) +TEST_DECLARE (pipe_ref) +TEST_DECLARE (pipe_ref2) +TEST_DECLARE (pipe_ref3) +TEST_DECLARE (process_ref) TEST_DECLARE (async) TEST_DECLARE (get_currentexe) TEST_DECLARE (cwd_and_chdir) @@ -112,7 +121,6 @@ TEST_DECLARE (fs_event_watch_file) TEST_DECLARE (fs_event_watch_file_current_dir) TEST_DECLARE (fs_event_no_callback_on_close) TEST_DECLARE (fs_event_immediate_close) -TEST_DECLARE (fs_event_unref) TEST_DECLARE (fs_readdir_empty_dir) TEST_DECLARE (fs_readdir_file) TEST_DECLARE (fs_open_dir) @@ -140,11 +148,6 @@ TASK_LIST_START TEST_ENTRY (ipc_listen_before_write) TEST_ENTRY (ipc_listen_after_write) - TEST_ENTRY (tcp_ref) - - TEST_ENTRY (tcp_ref2) - TEST_HELPER (tcp_ref2, tcp4_echo_server) - TEST_ENTRY (tcp_ping_pong) TEST_HELPER (tcp_ping_pong, tcp4_echo_server) @@ -201,9 +204,6 @@ TASK_LIST_START TEST_ENTRY (error_message) TEST_ENTRY (timer) - TEST_ENTRY (timer_ref) - TEST_ENTRY (timer_ref2) - TEST_ENTRY (timer_again) TEST_ENTRY (idle_starvation) @@ -214,6 +214,22 @@ TASK_LIST_START TEST_ENTRY (prepare_ref) TEST_ENTRY (check_ref) TEST_ENTRY (unref_in_prepare_cb) + TEST_ENTRY (timer_ref) + TEST_ENTRY (timer_ref2) + TEST_ENTRY (fs_event_ref) + TEST_ENTRY (tcp_ref) + TEST_ENTRY (tcp_ref2) + TEST_ENTRY (tcp_ref3) + TEST_HELPER (tcp_ref3, tcp4_echo_server) + TEST_ENTRY (udp_ref) + TEST_ENTRY (udp_ref2) + TEST_ENTRY (udp_ref3) + TEST_HELPER (udp_ref3, udp4_echo_server) + TEST_ENTRY (pipe_ref) + TEST_ENTRY (pipe_ref2) + TEST_ENTRY (pipe_ref3) + TEST_HELPER (pipe_ref3, pipe_echo_server) + TEST_ENTRY (process_ref) TEST_ENTRY (loop_handles) @@ -270,7 +286,6 @@ TASK_LIST_START TEST_ENTRY (fs_event_watch_file_current_dir) TEST_ENTRY (fs_event_no_callback_on_close) TEST_ENTRY (fs_event_immediate_close) - TEST_ENTRY (fs_event_unref) TEST_ENTRY (fs_readdir_empty_dir) TEST_ENTRY (fs_readdir_file) TEST_ENTRY (fs_open_dir) diff --git a/test/test-ref.c b/test/test-ref.c index 0083335f..2b8aabbc 100644 --- a/test/test-ref.c +++ b/test/test-ref.c @@ -22,6 +22,14 @@ #include "uv.h" #include "task.h" +#include +#include + + +static void fail_cb(void) { + FATAL("fail_cb should not have been called"); +} + TEST_IMPL(ref) { uv_run(uv_default_loop()); @@ -83,3 +91,164 @@ TEST_IMPL(unref_in_prepare_cb) { uv_run(uv_default_loop()); return 0; } + + +TEST_IMPL(timer_ref) { + uv_timer_t h; + uv_timer_init(uv_default_loop(), &h); + uv_unref(uv_default_loop()); + uv_run(uv_default_loop()); + return 0; +} + + +TEST_IMPL(timer_ref2) { + uv_timer_t h; + uv_timer_init(uv_default_loop(), &h); + uv_timer_start(&h, (uv_timer_cb) fail_cb, 42, 42); + uv_unref(uv_default_loop()); + uv_run(uv_default_loop()); + return 0; +} + + +TEST_IMPL(fs_event_ref) { + uv_fs_event_t h; + uv_fs_event_init(uv_default_loop(), &h, ".", (uv_fs_event_cb) fail_cb, 0); + uv_unref(uv_default_loop()); + uv_run(uv_default_loop()); + return 0; +} + + +TEST_IMPL(tcp_ref) { + uv_tcp_t h; + uv_tcp_init(uv_default_loop(), &h); + uv_unref(uv_default_loop()); + uv_run(uv_default_loop()); + return 0; +} + + +TEST_IMPL(tcp_ref2) { + uv_tcp_t h; + uv_tcp_init(uv_default_loop(), &h); + uv_listen((uv_stream_t*)&h, 128, (uv_connection_cb)fail_cb); + uv_unref(uv_default_loop()); + uv_run(uv_default_loop()); + return 0; +} + + +TEST_IMPL(tcp_ref3) { + struct sockaddr_in addr = uv_ip4_addr("127.0.0.1", TEST_PORT); + uv_connect_t req; + uv_tcp_t h; + uv_tcp_init(uv_default_loop(), &h); + uv_tcp_connect(&req, &h, addr, (uv_connect_cb)fail_cb); + uv_unref(uv_default_loop()); + uv_unref(uv_default_loop()); /* connect req refs the loop */ + uv_run(uv_default_loop()); + return 0; +} + + +TEST_IMPL(udp_ref) { + uv_udp_t h; + uv_udp_init(uv_default_loop(), &h); + uv_unref(uv_default_loop()); + uv_run(uv_default_loop()); + return 0; +} + + +TEST_IMPL(udp_ref2) { + struct sockaddr_in addr = uv_ip4_addr("127.0.0.1", TEST_PORT); + uv_udp_t h; + uv_udp_init(uv_default_loop(), &h); + uv_udp_bind(&h, addr, 0); + uv_udp_recv_start(&h, (uv_alloc_cb)fail_cb, (uv_udp_recv_cb)fail_cb); + uv_unref(uv_default_loop()); + uv_run(uv_default_loop()); + return 0; +} + + +TEST_IMPL(udp_ref3) { + struct sockaddr_in addr = uv_ip4_addr("127.0.0.1", TEST_PORT); + uv_buf_t buf = uv_buf_init("PING", 4); + uv_udp_send_t req; + uv_udp_t h; + + uv_udp_init(uv_default_loop(), &h); + uv_udp_send(&req, &h, &buf, 1, addr, (uv_udp_send_cb)fail_cb); + uv_unref(uv_default_loop()); + uv_unref(uv_default_loop()); /* send req refs the loop */ + uv_run(uv_default_loop()); + + return 0; +} + + +TEST_IMPL(pipe_ref) { + uv_pipe_t h; + uv_pipe_init(uv_default_loop(), &h, 0); + uv_unref(uv_default_loop()); + uv_run(uv_default_loop()); + return 0; +} + + +TEST_IMPL(pipe_ref2) { + uv_pipe_t h; + uv_pipe_init(uv_default_loop(), &h, 0); + uv_listen((uv_stream_t*)&h, 128, (uv_connection_cb)fail_cb); + uv_unref(uv_default_loop()); + uv_run(uv_default_loop()); + return 0; +} + + +TEST_IMPL(pipe_ref3) { + uv_connect_t req; + uv_pipe_t h; + uv_pipe_init(uv_default_loop(), &h, 0); + uv_pipe_connect(&req, &h, TEST_PIPENAME, (uv_connect_cb)fail_cb); + uv_unref(uv_default_loop()); + uv_unref(uv_default_loop()); /* connect req refs the loop */ + uv_run(uv_default_loop()); + return 0; +} + + +TEST_IMPL(process_ref) { + /* spawn_helper4 blocks indefinitely. */ + char *argv[] = { NULL, "spawn_helper4", NULL }; + uv_process_options_t options; + size_t exepath_size; + char exepath[256]; + uv_process_t h; + int r; + + memset(&options, 0, sizeof(options)); + exepath_size = sizeof(exepath); + + r = uv_exepath(exepath, &exepath_size); + ASSERT(r == 0); + + argv[0] = exepath; + options.file = exepath; + options.args = argv; + options.exit_cb = NULL; + + r = uv_spawn(uv_default_loop(), &h, options); + ASSERT(r == 0); + + uv_unref(uv_default_loop()); + uv_run(uv_default_loop()); + + r = uv_process_kill(&h, /* SIGTERM */ 15); + ASSERT(r == 0); + + return 0; +} diff --git a/test/test-tcp-close.c b/test/test-tcp-close.c index f5dd0a44..5da8a84f 100644 --- a/test/test-tcp-close.c +++ b/test/test-tcp-close.c @@ -127,50 +127,3 @@ TEST_IMPL(tcp_close) { return 0; } - - -TEST_IMPL(tcp_ref) { - uv_tcp_t never; - int r; - - /* A tcp just initialized should count as one reference. */ - r = uv_tcp_init(uv_default_loop(), &never); - ASSERT(r == 0); - - /* One unref should set the loop ref count to zero. */ - uv_unref(uv_default_loop()); - - /* Therefore this does not block */ - uv_run(uv_default_loop()); - - return 0; -} - - -static void never_cb(uv_connect_t* conn_req, int status) { - FATAL("never_cb should never be called"); -} - - -TEST_IMPL(tcp_ref2) { - uv_tcp_t never; - int r; - - /* A tcp just initialized should count as one reference. */ - r = uv_tcp_init(uv_default_loop(), &never); - ASSERT(r == 0); - - r = uv_tcp_connect(&connect_req, - &never, - uv_ip4_addr("127.0.0.1", TEST_PORT), - never_cb); - ASSERT(r == 0); - - /* One unref should set the loop ref count to zero. */ - uv_unref(uv_default_loop()); - - /* Therefore this does not block */ - uv_run(uv_default_loop()); - - return 0; -} diff --git a/test/test-timer.c b/test/test-timer.c index 87235a51..17bcb84b 100644 --- a/test/test-timer.c +++ b/test/test-timer.c @@ -130,43 +130,3 @@ TEST_IMPL(timer) { return 0; } - - -TEST_IMPL(timer_ref) { - uv_timer_t never; - int r; - - /* A timer just initialized should count as one reference. */ - r = uv_timer_init(uv_default_loop(), &never); - ASSERT(r == 0); - - /* One unref should set the loop ref count to zero. */ - uv_unref(uv_default_loop()); - - /* Therefore this does not block */ - uv_run(uv_default_loop()); - - return 0; -} - - -TEST_IMPL(timer_ref2) { - uv_timer_t never; - int r; - - /* A timer just initialized should count as one reference. */ - r = uv_timer_init(uv_default_loop(), &never); - ASSERT(r == 0); - - /* We start the timer, this should not effect the ref count. */ - r = uv_timer_start(&never, never_cb, 1000, 1000); - ASSERT(r == 0); - - /* One unref should set the loop ref count to zero. */ - uv_unref(uv_default_loop()); - - /* Therefore this does not block */ - uv_run(uv_default_loop()); - - return 0; -}