diff --git a/test/run-tests.c b/test/run-tests.c index a050172e..c4a70dc9 100644 --- a/test/run-tests.c +++ b/test/run-tests.c @@ -38,13 +38,20 @@ int main(int argc, char **argv) { platform_init(argc, argv); - if (argc == 2 && strcmp(argv[1], "spawn_helper1") == 0) { - return 1; - } - switch (argc) { case 1: return run_tests(TEST_TIMEOUT, 0); - case 2: return run_test(argv[1], TEST_TIMEOUT, 0); + case 2: { + if (strcmp(argv[1], "spawn_helper1") == 0) { + return 1; + } + + if (strcmp(argv[1], "spawn_helper2") == 0) { + printf("hello world\n"); + return 1; + } + + return run_test(argv[1], TEST_TIMEOUT, 0); + } case 3: return run_test_part(argv[1], argv[2]); default: LOGF("Too many arguments.\n"); diff --git a/test/test-list.h b/test/test-list.h index 741cac2e..99d59e55 100644 --- a/test/test-list.h +++ b/test/test-list.h @@ -64,6 +64,7 @@ TEST_DECLARE (getsockname) TEST_DECLARE (fail_always) TEST_DECLARE (pass_always) TEST_DECLARE (spawn_exit_code) +TEST_DECLARE (spawn_stdout) HELPER_DECLARE (tcp4_echo_server) HELPER_DECLARE (tcp6_echo_server) HELPER_DECLARE (pipe_echo_server) @@ -142,6 +143,7 @@ TASK_LIST_START TEST_ENTRY (getsockname) TEST_ENTRY (spawn_exit_code) + TEST_ENTRY (spawn_stdout) #if 0 /* These are for testing the test runner. */ diff --git a/test/test-spawn.c b/test/test-spawn.c index 9690b610..9449af7c 100644 --- a/test/test-spawn.c +++ b/test/test-spawn.c @@ -26,12 +26,23 @@ static int close_cb_called; static int exit_cb_called; +static uv_process_t process; +static uv_process_options_t options = { 0 }; +static char exepath[1024]; +static size_t exepath_size = 1024; +static char* args[3]; + +#define OUTPUT_SIZE 1024 +static char output[OUTPUT_SIZE]; +static int output_used; + static void close_cb(uv_handle_t* handle) { printf("close_cb\n"); close_cb_called++; } + static void exit_cb(uv_process_t* process, int exit_status, int term_signal) { printf("exit_cb\n"); exit_cb_called++; @@ -41,29 +52,51 @@ static void exit_cb(uv_process_t* process, int exit_status, int term_signal) { } -TEST_IMPL(spawn_exit_code) { - int r; - char exepath[1024]; - size_t exepath_size = 1024; - uv_process_t process; - uv_process_options_t options = { 0 }; - /* Note spawn_helper1 defined in test/run-tests.c */ - char* args[3] = { NULL, "spawn_helper1", NULL }; +uv_buf_t on_alloc(uv_stream_t* tcp, size_t suggested_size) { + uv_buf_t buf; + buf.base = output + output_used; + buf.len = OUTPUT_SIZE - output_used; + return buf; +} - r = uv_exepath(exepath, &exepath_size); + +void on_read(uv_stream_t* tcp, ssize_t nread, uv_buf_t buf) { + uv_err_t err = uv_last_error(); + + if (nread > 0) { + output_used += nread; + } else if (nread < 0) { + if (err.code == UV_EOF) { + uv_close((uv_handle_t*)tcp, close_cb); + } + } +} + + +static void init_process_options(char* test) { + /* Note spawn_helper1 defined in test/run-tests.c */ + int r = uv_exepath(exepath, &exepath_size); ASSERT(r == 0); exepath[exepath_size] = '\0'; - options.file = exepath; args[0] = exepath; - + args[1] = test; + args[2] = NULL; + options.file = exepath; options.args = args; options.exit_cb = exit_cb; +} + + +TEST_IMPL(spawn_exit_code) { + int r; uv_init(); + init_process_options("spawn_helper1"); + r = uv_spawn(&process, options); ASSERT(r == 0); - + r = uv_run(); ASSERT(r == 0); @@ -72,3 +105,30 @@ TEST_IMPL(spawn_exit_code) { return 0; } + + +TEST_IMPL(spawn_stdout) { + int r; + uv_pipe_t out; + + uv_init(); + + init_process_options("spawn_helper2"); + options.stdout_stream = &out; + + r = uv_spawn(&process, options); + ASSERT(r == 0); + + r = uv_read_start((uv_stream_t*) &out, on_alloc, on_read); + ASSERT(r == 0); + + r = uv_run(); + ASSERT(r == 0); + + ASSERT(exit_cb_called == 1); + ASSERT(close_cb_called == 2); /* Once for process once for the pipe. */ + ASSERT(strcmp("hello world\n", output) == 0); + + return 0; +} +