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

Commit

Permalink
test: test cross-process handle send/recv
Browse files Browse the repository at this point in the history
  • Loading branch information
bnoordhuis committed Mar 9, 2012
1 parent 71eea07 commit e190162
Show file tree
Hide file tree
Showing 5 changed files with 241 additions and 12 deletions.
5 changes: 5 additions & 0 deletions test/run-tests.c
Expand Up @@ -271,6 +271,11 @@ static int maybe_run_test(int argc, char **argv) {
return ipc_helper(1);
}

if (strcmp(argv[1], "ipc_send_recv_helper") == 0) {
int ipc_send_recv_helper(void); /* See test-ipc-send-recv.c */
return ipc_send_recv_helper();
}

if (strcmp(argv[1], "stdio_over_pipes_helper") == 0) {
return stdio_over_pipes_helper();
}
Expand Down
208 changes: 208 additions & 0 deletions test/test-ipc-send-recv.c
@@ -0,0 +1,208 @@
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/

#include "uv.h"
#include "task.h"

#include <stdio.h>
#include <string.h>

/* See test-ipc.ctx */
void spawn_helper(uv_pipe_t* channel,
uv_process_t* process,
const char* helper);

union handles {
uv_handle_t handle;
uv_stream_t stream;
uv_pipe_t pipe;
uv_tcp_t tcp;
uv_tty_t tty;
};

struct echo_ctx {
uv_pipe_t channel;
uv_write_t write_req;
uv_handle_type expected_type;
union handles send;
union handles recv;
};

static struct echo_ctx ctx;
static int num_recv_handles;


static uv_buf_t alloc_cb(uv_handle_t* handle, size_t suggested_size) {
/* we're not actually reading anything so a small buffer is okay */
static char buf[8];
return uv_buf_init(buf, sizeof(buf));
}


static void recv_cb(uv_pipe_t* handle,
ssize_t nread,
uv_buf_t buf,
uv_handle_type pending) {
int r;

ASSERT(pending == ctx.expected_type);
ASSERT(handle == &ctx.channel);
ASSERT(nread >= 0);

if (pending == UV_NAMED_PIPE)
r = uv_pipe_init(ctx.channel.loop, &ctx.recv.pipe, 0);
else if (pending == UV_TCP)
r = uv_tcp_init(ctx.channel.loop, &ctx.recv.tcp);
else
abort();
ASSERT(r == 0);

r = uv_accept((uv_stream_t*)&ctx.channel, &ctx.recv.stream);
ASSERT(r == 0);

uv_close((uv_handle_t*)&ctx.channel, NULL);
uv_close(&ctx.send.handle, NULL);
uv_close(&ctx.recv.handle, NULL);
num_recv_handles++;
}


static int run_test(void) {
uv_process_t process;
uv_buf_t buf;
int r;

spawn_helper(&ctx.channel, &process, "ipc_send_recv_helper");

buf = uv_buf_init(".", 1);
r = uv_write2(&ctx.write_req,
(uv_stream_t*)&ctx.channel,
&buf, 1,
&ctx.send.stream,
NULL);
ASSERT(r == 0);

r = uv_read2_start((uv_stream_t*)&ctx.channel, alloc_cb, recv_cb);
ASSERT(r == 0);

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

ASSERT(num_recv_handles == 1);

return 0;
}


TEST_IMPL(ipc_send_recv_pipe) {
int r;

ctx.expected_type = UV_NAMED_PIPE;

r = uv_pipe_init(uv_default_loop(), &ctx.send.pipe, 1);
ASSERT(r == 0);

r = uv_pipe_bind(&ctx.send.pipe, TEST_PIPENAME);
ASSERT(r == 0);

return run_test();
}


TEST_IMPL(ipc_send_recv_tcp) {
int r;

ctx.expected_type = UV_TCP;

r = uv_tcp_init(uv_default_loop(), &ctx.send.tcp);
ASSERT(r == 0);

r = uv_tcp_bind(&ctx.send.tcp, uv_ip4_addr("127.0.0.1", TEST_PORT));
ASSERT(r == 0);

return run_test();
}


/* Everything here runs in a child process. */

static void write2_cb(uv_write_t* req, int status) {
ASSERT(status == 0);
uv_close(&ctx.recv.handle, NULL);
uv_close((uv_handle_t*)&ctx.channel, NULL);
}


static void read2_cb(uv_pipe_t* handle,
ssize_t nread,
uv_buf_t buf,
uv_handle_type pending) {
int r;

ASSERT(pending == UV_NAMED_PIPE || pending == UV_TCP);
ASSERT(handle == &ctx.channel);
ASSERT(nread >= 0);

buf = uv_buf_init(".", 1);

if (pending == UV_NAMED_PIPE)
r = uv_pipe_init(ctx.channel.loop, &ctx.recv.pipe, 0);
else if (pending == UV_TCP)
r = uv_tcp_init(ctx.channel.loop, &ctx.recv.tcp);
else
abort();
ASSERT(r == 0);

r = uv_accept((uv_stream_t*)handle, &ctx.recv.stream);
ASSERT(r == 0);

r = uv_write2(&ctx.write_req,
(uv_stream_t*)&ctx.channel,
&buf, 1,
&ctx.recv.stream,
write2_cb);
ASSERT(r == 0);
}


/* stdin is a duplex channel over which a handle is sent.
* We receive it and send it back where it came from.
*/
int ipc_send_recv_helper(void) {
int r;

memset(&ctx, 0, sizeof(ctx));

r = uv_pipe_init(uv_default_loop(), &ctx.channel, 1);
ASSERT(r == 0);

uv_pipe_open(&ctx.channel, 0);
ASSERT(uv_is_readable((uv_stream_t*)&ctx.channel));
ASSERT(uv_is_writable((uv_stream_t*)&ctx.channel));

r = uv_read2_start((uv_stream_t*)&ctx.channel, alloc_cb, read2_cb);
ASSERT(r == 0);

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

return 0;
}
35 changes: 23 additions & 12 deletions test/test-ipc.c
Expand Up @@ -25,9 +25,6 @@
#include <stdio.h>
#include <string.h>

static char exepath[1024];
static size_t exepath_size = 1024;
static char* args[3];
static uv_pipe_t channel;
static uv_tcp_t tcp_server;

Expand Down Expand Up @@ -184,31 +181,44 @@ static void on_read(uv_pipe_t* pipe, ssize_t nread, uv_buf_t buf,
}


int run_ipc_test(const char* helper) {
int r;
void spawn_helper(uv_pipe_t* channel,
uv_process_t* process,
const char* helper) {
uv_process_options_t options;
uv_process_t process;
size_t exepath_size;
char exepath[1024];
char* args[3];
int r;

r = uv_pipe_init(uv_default_loop(), &channel, 1);
r = uv_pipe_init(uv_default_loop(), channel, 1);
ASSERT(r == 0);
ASSERT(channel.ipc);

memset(&options, 0, sizeof(uv_process_options_t));
ASSERT(channel->ipc);

exepath_size = sizeof(exepath);
r = uv_exepath(exepath, &exepath_size);
ASSERT(r == 0);

exepath[exepath_size] = '\0';
args[0] = exepath;
args[1] = (char*)helper;
args[2] = NULL;

memset(&options, 0, sizeof(options));
options.file = exepath;
options.args = args;
options.exit_cb = exit_cb;
options.stdin_stream = &channel;
options.stdin_stream = channel;

r = uv_spawn(uv_default_loop(), &process, options);
r = uv_spawn(uv_default_loop(), process, options);
ASSERT(r == 0);
}


static int run_ipc_test(const char* helper) {
uv_process_t process;
int r;

spawn_helper(&channel, &process, helper);
uv_read2_start((uv_stream_t*)&channel, on_alloc, on_read);

r = uv_run(uv_default_loop());
Expand All @@ -218,6 +228,7 @@ int run_ipc_test(const char* helper) {
ASSERT(remote_conn_accepted == 1);
ASSERT(read2_cb_called == 1);
ASSERT(exit_cb_called == 1);

return 0;
}

Expand Down
4 changes: 4 additions & 0 deletions test/test-list.h
Expand Up @@ -24,6 +24,8 @@ TEST_DECLARE (tty)
TEST_DECLARE (stdio_over_pipes)
TEST_DECLARE (ipc_listen_before_write)
TEST_DECLARE (ipc_listen_after_write)
TEST_DECLARE (ipc_send_recv_pipe)
TEST_DECLARE (ipc_send_recv_tcp)
TEST_DECLARE (tcp_ping_pong)
TEST_DECLARE (tcp_ping_pong_v6)
TEST_DECLARE (pipe_ping_pong)
Expand Down Expand Up @@ -174,6 +176,8 @@ TASK_LIST_START
TEST_ENTRY (stdio_over_pipes)
TEST_ENTRY (ipc_listen_before_write)
TEST_ENTRY (ipc_listen_after_write)
TEST_ENTRY (ipc_send_recv_pipe)
TEST_ENTRY (ipc_send_recv_tcp)

TEST_ENTRY (tcp_ping_pong)
TEST_HELPER (tcp_ping_pong, tcp4_echo_server)
Expand Down
1 change: 1 addition & 0 deletions uv.gyp
Expand Up @@ -309,6 +309,7 @@
'test/test-hrtime.c',
'test/test-idle.c',
'test/test-ipc.c',
'test/test-ipc-send-recv.c',
'test/test-list.h',
'test/test-loop-handles.c',
'test/test-multiple-listen.c',
Expand Down

0 comments on commit e190162

Please sign in to comment.