Skip to content
This repository has been archived by the owner on May 4, 2018. It is now read-only.

Commit

Permalink
Tests: verify that uv_write and uv_shutdown ref the event loop
Browse files Browse the repository at this point in the history
  • Loading branch information
piscisaureus committed Mar 9, 2012
1 parent ec97ba8 commit fb65d74
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 4 deletions.
12 changes: 12 additions & 0 deletions test/test-list.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,16 @@ TEST_DECLARE (fs_event_ref)
TEST_DECLARE (tcp_ref)
TEST_DECLARE (tcp_ref2)
TEST_DECLARE (tcp_ref3)
TEST_DECLARE (tcp_ref4)
TEST_DECLARE (tcp_ref5)
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 (pipe_ref4)
TEST_DECLARE (pipe_ref5)
TEST_DECLARE (process_ref)
TEST_DECLARE (async)
TEST_DECLARE (get_currentexe)
Expand Down Expand Up @@ -228,6 +232,10 @@ TASK_LIST_START
TEST_ENTRY (tcp_ref2)
TEST_ENTRY (tcp_ref3)
TEST_HELPER (tcp_ref3, tcp4_echo_server)
TEST_ENTRY (tcp_ref4)
TEST_HELPER (tcp_ref4, tcp4_echo_server)
TEST_ENTRY (tcp_ref5)
TEST_HELPER (tcp_ref5, tcp4_echo_server)
TEST_ENTRY (udp_ref)
TEST_ENTRY (udp_ref2)
TEST_ENTRY (udp_ref3)
Expand All @@ -236,6 +244,10 @@ TASK_LIST_START
TEST_ENTRY (pipe_ref2)
TEST_ENTRY (pipe_ref3)
TEST_HELPER (pipe_ref3, pipe_echo_server)
TEST_ENTRY (pipe_ref4)
TEST_HELPER (pipe_ref4, pipe_echo_server)
TEST_ENTRY (pipe_ref5)
TEST_HELPER (pipe_ref5, pipe_echo_server)
TEST_ENTRY (process_ref)

TEST_ENTRY (loop_handles)
Expand Down
76 changes: 72 additions & 4 deletions test/test-ref.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,39 @@
#include <string.h>


static uv_write_t write_req;
static uv_shutdown_t shutdown_req;
static uv_connect_t connect_req;

static char buffer[32767];


static void fail_cb(void) {
FATAL("fail_cb should not have been called");
}


static void write_unref_cb(uv_connect_t* req, int status) {
uv_buf_t buf = uv_buf_init(buffer, sizeof buffer);

ASSERT(req == &connect_req);
ASSERT(status == 0);

uv_write(&write_req, req->handle, &buf, 1, (uv_write_cb) fail_cb);
uv_unref(uv_default_loop()); // uv_write refs the loop
}



static void shutdown_unref_cb(uv_connect_t* req, int status) {
ASSERT(req == &connect_req);
ASSERT(status == 0);

uv_shutdown(&shutdown_req, req->handle, (uv_shutdown_cb) fail_cb);
uv_unref(uv_default_loop()); // uv_shutdown refs the loop
}


TEST_IMPL(ref) {
uv_run(uv_default_loop());
return 0;
Expand Down Expand Up @@ -142,17 +170,38 @@ TEST_IMPL(tcp_ref2) {

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_tcp_connect(&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(tcp_ref4) {
struct sockaddr_in addr = uv_ip4_addr("127.0.0.1", TEST_PORT);
uv_tcp_t h;
uv_tcp_init(uv_default_loop(), &h);
uv_tcp_connect(&connect_req, &h, addr, write_unref_cb);
uv_unref(uv_default_loop());
uv_run(uv_default_loop());
return 0;
}


TEST_IMPL(tcp_ref5) {
struct sockaddr_in addr = uv_ip4_addr("127.0.0.1", TEST_PORT);
uv_tcp_t h;
uv_tcp_init(uv_default_loop(), &h);
uv_tcp_connect(&connect_req, &h, addr, shutdown_unref_cb);
uv_unref(uv_default_loop());
uv_run(uv_default_loop());
return 0;
}


TEST_IMPL(udp_ref) {
uv_udp_t h;
uv_udp_init(uv_default_loop(), &h);
Expand Down Expand Up @@ -210,17 +259,36 @@ TEST_IMPL(pipe_ref2) {


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_pipe_connect(&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(pipe_ref4) {
uv_pipe_t h;
uv_pipe_init(uv_default_loop(), &h, 0);
uv_pipe_connect(&connect_req, &h, TEST_PIPENAME, write_unref_cb);
uv_unref(uv_default_loop());
uv_run(uv_default_loop());
return 0;
}


TEST_IMPL(pipe_ref5) {
uv_pipe_t h;
uv_pipe_init(uv_default_loop(), &h, 0);
uv_pipe_connect(&connect_req, &h, TEST_PIPENAME, shutdown_unref_cb);
uv_unref(uv_default_loop());
uv_run(uv_default_loop());
return 0;
}


TEST_IMPL(process_ref) {
/* spawn_helper4 blocks indefinitely. */
char *argv[] = { NULL, "spawn_helper4", NULL };
Expand Down

0 comments on commit fb65d74

Please sign in to comment.