Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
complete test-ipc
  • Loading branch information
Igor Zinkovsky committed Oct 4, 2011
1 parent 7de7ff3 commit cbca726
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 24 deletions.
57 changes: 51 additions & 6 deletions test/run-tests.c
Expand Up @@ -49,10 +49,52 @@ int main(int argc, char **argv) {
}


static uv_tcp_t server;
static uv_pipe_t channel;
static uv_tcp_t tcp_server;
static uv_write_t conn_notify_req;
static int close_cb_called;
static int connection_accepted;


static void close_cb(uv_handle_t* handle) {
close_cb_called++;
}


void conn_notify_write_cb(uv_write_t* req, int status) {
uv_close((uv_handle_t*)&tcp_server, close_cb);
uv_close((uv_handle_t*)&channel, close_cb);
}


static void ipc_on_connection(uv_stream_t* server, int status) {
int r;
uv_buf_t buf;
uv_tcp_t conn;

if (!connection_accepted) {
/*
* Accept the connection and close it. Also let the other
* side know.
*/
ASSERT(status == 0);
ASSERT((uv_stream_t*)&tcp_server == server);

r = uv_tcp_init(server->loop, &conn);
ASSERT(r == 0);

r = uv_accept(server, (uv_stream_t*)&conn);
ASSERT(r == 0);

uv_close((uv_handle_t*)&conn, close_cb);

buf = uv_buf_init("accepted_connection\n", 20);
r = uv_write2(&conn_notify_req, (uv_stream_t*)&channel, &buf, 1,
NULL, conn_notify_write_cb);
ASSERT(r == 0);

connection_accepted = 1;
}
}


Expand All @@ -63,7 +105,7 @@ static int ipc_helper() {
* data is transfered over the channel. XXX edit this comment after handle
* transfer is added.
*/
uv_pipe_t channel;

uv_write_t write_req;
int r;
uv_buf_t buf;
Expand All @@ -73,23 +115,26 @@ static int ipc_helper() {

uv_pipe_open(&channel, 0);

r = uv_tcp_init(uv_default_loop(), &server);
r = uv_tcp_init(uv_default_loop(), &tcp_server);
ASSERT(r == 0);

r = uv_tcp_bind(&server, uv_ip4_addr("0.0.0.0", TEST_PORT));
r = uv_tcp_bind(&tcp_server, uv_ip4_addr("0.0.0.0", TEST_PORT));
ASSERT(r == 0);

r = uv_listen((uv_stream_t*)&server, 12, ipc_on_connection);
r = uv_listen((uv_stream_t*)&tcp_server, 12, ipc_on_connection);
ASSERT(r == 0);

buf = uv_buf_init("hello\n", 6);
r = uv_write2(&write_req, (uv_stream_t*)&channel, &buf, 1,
(uv_stream_t*)&server, NULL);
(uv_stream_t*)&tcp_server, NULL);
ASSERT(r == 0);

r = uv_run(uv_default_loop());
ASSERT(r == 0);

ASSERT(connection_accepted == 1);
ASSERT(close_cb_called == 3);

return 0;
}

Expand Down
118 changes: 100 additions & 18 deletions test/test-ipc.c
Expand Up @@ -33,12 +33,46 @@ static uv_tcp_t tcp_server;

static int exit_cb_called;
static int read2_cb_called;
static int local_conn_accepted;
static int remote_conn_accepted;
static int tcp_server_listening;

static uv_write_t write_req;

typedef struct {
uv_connect_t conn_req;
uv_tcp_t conn;
} tcp_conn;

#define CONN_COUNT 100


static void close_server_conn_cb(uv_handle_t* handle) {
free(handle);
}


static void ipc_on_connection(uv_stream_t* server, int status) {
ASSERT(status == 0);
ASSERT((uv_stream_t*)&tcp_server == server);
uv_tcp_t* conn;
int r;

if (!local_conn_accepted) {
/* Accept the connection and close it. Also and close the server. */
ASSERT(status == 0);
ASSERT((uv_stream_t*)&tcp_server == server);

conn = malloc(sizeof(*conn));
ASSERT(conn);
r = uv_tcp_init(server->loop, conn);
ASSERT(r == 0);

r = uv_accept(server, (uv_stream_t*)conn);
ASSERT(r == 0);

uv_close((uv_handle_t*)conn, close_server_conn_cb);
uv_close((uv_handle_t*)server, NULL);
local_conn_accepted = 1;
}
}


Expand All @@ -55,6 +89,39 @@ static uv_buf_t on_alloc(uv_handle_t* handle, size_t suggested_size) {
}


static void close_client_conn_cb(uv_handle_t* handle) {
tcp_conn* p = (tcp_conn*)handle->data;
free(p);
}


static void connect_cb(uv_connect_t* req, int status) {
uv_close((uv_handle_t*)req->handle, close_client_conn_cb);
}


static void make_many_connections() {
tcp_conn* conn;
struct sockaddr_in addr;
int r, i;

for (i = 0; i < CONN_COUNT; i++) {
conn = malloc(sizeof(*conn));
ASSERT(conn);

r = uv_tcp_init(uv_default_loop(), &conn->conn);
ASSERT(r == 0);

addr = uv_ip4_addr("127.0.0.1", TEST_PORT);

r = uv_tcp_connect(&conn->conn_req, (uv_tcp_t*)&conn->conn, addr, connect_cb);
ASSERT(r == 0);

conn->conn.data = conn;
}
}


static void on_read(uv_pipe_t* pipe, ssize_t nread, uv_buf_t buf,
uv_handle_type pending) {
int r;
Expand All @@ -78,27 +145,40 @@ static void on_read(uv_pipe_t* pipe, ssize_t nread, uv_buf_t buf,
abort();
}

ASSERT(nread > 0 && buf.base && pending != UV_UNKNOWN_HANDLE);
read2_cb_called++;
fprintf(stderr, "got %d bytes\n", (int)nread);

if (!tcp_server_listening) {
ASSERT(nread > 0 && buf.base && pending != UV_UNKNOWN_HANDLE);
read2_cb_called++;

/* Accept the pending TCP server, and start listening on it. */
ASSERT(pending == UV_TCP);
r = uv_tcp_init(uv_default_loop(), &tcp_server);
ASSERT(r == 0);
/* Accept the pending TCP server, and start listening on it. */
ASSERT(pending == UV_TCP);
r = uv_tcp_init(uv_default_loop(), &tcp_server);
ASSERT(r == 0);

r = uv_accept((uv_stream_t*)pipe, (uv_stream_t*)&tcp_server);
ASSERT(r == 0);
r = uv_accept((uv_stream_t*)pipe, (uv_stream_t*)&tcp_server);
ASSERT(r == 0);

r = uv_listen((uv_stream_t*)&tcp_server, 12, ipc_on_connection);
ASSERT(r == 0);
r = uv_listen((uv_stream_t*)&tcp_server, 12, ipc_on_connection);
ASSERT(r == 0);

/* Make sure that the expected data is correctly multiplexed. */
ASSERT(memcmp("hello\n", buf.base, nread) == 0);
fprintf(stderr, "got %d bytes\n", (int)nread);
tcp_server_listening = 1;

outbuf = uv_buf_init("world\n", 6);
r = uv_write(&write_req, (uv_stream_t*)pipe, &outbuf, 1, NULL);
ASSERT(r == 0);
/* Make sure that the expected data is correctly multiplexed. */
ASSERT(memcmp("hello\n", buf.base, nread) == 0);

outbuf = uv_buf_init("world\n", 6);
r = uv_write(&write_req, (uv_stream_t*)pipe, &outbuf, 1, NULL);
ASSERT(r == 0);

/* Create a bunch of connections to get both servers to accept. */
make_many_connections();
} else if (memcmp("accepted_connection\n", buf.base, nread) == 0) {
/* Remote server has accepted a connection. Close the channel. */
ASSERT(pending == UV_UNKNOWN_HANDLE);
remote_conn_accepted = 1;
uv_close((uv_handle_t*)&channel, NULL);
}

free(buf.base);
}
Expand Down Expand Up @@ -133,6 +213,8 @@ TEST_IMPL(ipc) {
r = uv_run(uv_default_loop());
ASSERT(r == 0);

ASSERT(local_conn_accepted == 1);
ASSERT(remote_conn_accepted == 1);
ASSERT(read2_cb_called == 1);
ASSERT(exit_cb_called == 1);
return 0;
Expand Down

0 comments on commit cbca726

Please sign in to comment.