diff options
-rwxr-xr-x | gyp_uv_link | 20 | ||||
-rw-r--r-- | include/uv_link.h | 3 | ||||
-rw-r--r-- | test/src/main.c | 22 | ||||
-rw-r--r-- | test/src/test-common.h | 22 | ||||
-rw-r--r-- | test/src/test-list.h | 16 | ||||
-rw-r--r-- | test/src/test-uv-link-source-t.c | 120 | ||||
-rw-r--r-- | test/test.gyp | 20 |
7 files changed, 209 insertions, 14 deletions
diff --git a/gyp_uv_link b/gyp_uv_link index e174485..73251d6 100755 --- a/gyp_uv_link +++ b/gyp_uv_link @@ -52,20 +52,14 @@ def run_gyp(args): if __name__ == '__main__': args = sys.argv[1:] - # GYP bug. - # On msvs it will crash if it gets an absolute path. - # On Mac/make it will crash if it doesn't get an absolute path. - if sys.platform == 'win32': - args.append(os.path.join(root, 'uv_link_t.gyp')) - common_fn = os.path.join(root, 'common.gypi') - options_fn = os.path.join(root, 'options.gypi') - # we force vs 2010 over 2008 which would otherwise be the default for gyp - if not os.environ.get('GYP_MSVS_VERSION'): - os.environ['GYP_MSVS_VERSION'] = '2010' - else: + if 'test' not in args: args.append(os.path.join(os.path.abspath(root), 'uv_link_t.gyp')) - common_fn = os.path.join(os.path.abspath(root), 'common.gypi') - options_fn = os.path.join(os.path.abspath(root), 'options.gypi') + else: + args.append(os.path.join(os.path.abspath(root), 'test/test.gyp')) + args = filter(lambda arg: arg != 'test', args) + + common_fn = os.path.join(os.path.abspath(root), 'common.gypi') + options_fn = os.path.join(os.path.abspath(root), 'options.gypi') if os.path.exists(common_fn): args.extend(['-I', common_fn]) diff --git a/include/uv_link.h b/include/uv_link.h index 2d2b744..f835fd2 100644 --- a/include/uv_link.h +++ b/include/uv_link.h @@ -1,7 +1,7 @@ #ifndef INCLUDE_UV_LINK_H_ #define INCLUDE_UV_LINK_H_ -#include <uv.h> +#include "uv.h" typedef struct uv_link_s uv_link_t; typedef struct uv_link_source_s uv_link_source_t; @@ -49,6 +49,7 @@ UV_EXTERN int uv_link_init(uv_loop_t* loop, uv_link_t* link); UV_EXTERN int uv_link_close(uv_link_t* link, uv_link_close_cb close_cb); UV_EXTERN int uv_link_chain(uv_link_t* from, uv_link_t* to); +UV_EXTERN int uv_link_unchain(uv_link_t* from, uv_link_t* to); /* Source */ diff --git a/test/src/main.c b/test/src/main.c new file mode 100644 index 0000000..716e721 --- /dev/null +++ b/test/src/main.c @@ -0,0 +1,22 @@ +#include <stdio.h> +#include <string.h> + +#include "test-list.h" + +#define TEST_SELECT(N) \ + if (strncmp(argv[1], #N, sizeof(#N) - 1) == 0) { \ + fprintf(stderr, "===== " #N " =====\n"); \ + TEST_FN(N)(); \ + return 0; \ + } + +int main(int argc, char** argv) { + if (argc != 2) + return -1; + + TEST_ENUM(TEST_SELECT) + + return -1; +} + +#undef TEST_SELECT diff --git a/test/src/test-common.h b/test/src/test-common.h new file mode 100644 index 0000000..10e40a2 --- /dev/null +++ b/test/src/test-common.h @@ -0,0 +1,22 @@ +#ifndef TEST_SRC_TEST_COMMON_H_ +#define TEST_SRC_TEST_COMMON_H_ + +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> + +#include "uv.h" +#include "uv_link.h" + +#include "test-list.h" + +#define CHECK(VALUE, MESSAGE) \ + do { \ + if ((VALUE)) break; \ + fprintf(stderr, "Assertion failure: " #MESSAGE "\n"); \ + } while (0); + +#define CHECK_EQ(A, B, MESSAGE) CHECK((A) == (B), MESSAGE) +#define CHECK_NE(A, B, MESSAGE) CHECK((A) != (B), MESSAGE) + +#endif /* TEST_SRC_TEST_COMMON_H_ */ diff --git a/test/src/test-list.h b/test/src/test-list.h new file mode 100644 index 0000000..4a1c8ff --- /dev/null +++ b/test/src/test-list.h @@ -0,0 +1,16 @@ +#ifndef TEST_SRC_TEST_LIST_H_ +#define TEST_SRC_TEST_LIST_H_ + +#define TEST_ENUM(V) \ + V(uv_link_source_t) \ + +#define TEST_DECL(N) void test__##N(); + +TEST_ENUM(TEST_DECL) + +#undef TEST_DECL + +#define TEST_FN(N) test__##N +#define TEST_IMPL(N) void test__##N() + +#endif /* TEST_SRC_TEST_LIST_H_ */ diff --git a/test/src/test-uv-link-source-t.c b/test/src/test-uv-link-source-t.c new file mode 100644 index 0000000..a1d91df --- /dev/null +++ b/test/src/test-uv-link-source-t.c @@ -0,0 +1,120 @@ +#include <sys/socket.h> +#include <unistd.h> + +#include "test-common.h" + +static int fds[2]; +static uv_loop_t* loop; +static uv_pipe_t pair_right; +static uv_link_source_t source; + +static int write_cb_called; +static int alloc_cb_called; +static int read_cb_called; + +static void read_one() { + char buf[1024]; + int err; + + do + err = read(fds[0], buf, sizeof(buf)); + while (err == -1 && errno == EINTR); + + CHECK_EQ(err, 1, "read() == 1"); + CHECK_EQ(buf[0], 'x', "data should match"); +} + +static void source_write_cb(uv_link_t* link, int status) { + CHECK_EQ(link, &source.link, "link == &source.link"); + + write_cb_called++; + + read_one(); +} + + +static void test_writes() { + uv_buf_t buf; + + /* .write() should work */ + buf = uv_buf_init("x", 1); + CHECK_EQ(source.link.write(&source.link, &buf, 1, NULL, source_write_cb), 0, + "source.link.write() should return 0"); + + CHECK_EQ(uv_run(loop, UV_RUN_DEFAULT), 0, "uv_run()"); + + CHECK_EQ(write_cb_called, 1, "source_write_cb() must be called"); + + /* .try_write() should work too */ + CHECK_EQ(source.link.try_write(&source.link, &buf, 1), 1, + "source.link.try_write() should return 1"); + read_one(); +} + + +static void source_alloc_cb(uv_link_t* link, + size_t suggested_size, + uv_buf_t* buf) { + static char storage[1024]; + + CHECK_EQ(link, &source.link, "link == &source.link"); + + alloc_cb_called++; + + buf->base = storage; + buf->len = sizeof(storage); +} + +static void source_read_cb(uv_link_t* link, + ssize_t nread, + const uv_buf_t* buf) { + CHECK_EQ(link, &source.link, "link == &source.link"); + + read_cb_called++; + + CHECK_EQ(nread, 1, "source_read_cb must read one byte"); + CHECK_EQ(buf->base[0], 'x', "source_read_cb must read correct one byte"); + + CHECK_EQ(source.link.read_stop(&source.link), 0, "source.link.read_stop()"); +} + + +static void test_reads() { + int err; + + /* alloc_cb/read_cb */ + source.link.alloc_cb = source_alloc_cb; + source.link.read_cb = source_read_cb; + + do + err = write(fds[0], "x", 1); + while (err == -1 && errno == EINTR); + CHECK_EQ(err, 1, "write() == 1"); + + CHECK_EQ(source.link.read_start(&source.link), 0, "source.link.read_start()"); + + CHECK_EQ(uv_run(loop, UV_RUN_DEFAULT), 0, "uv_run()"); + CHECK_EQ(alloc_cb_called, 1, "alloc_cb must be called once"); + CHECK_EQ(read_cb_called, 1, "read_cb must be called once"); +} + + +TEST_IMPL(uv_link_source_t) { + loop = uv_default_loop(); + CHECK_NE(loop, NULL, "uv_default_loop()"); + + CHECK_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, fds), 0, "socketpair()"); + + CHECK_EQ(uv_pipe_init(loop, &pair_right, 0), 0, "uv_pipe_init(pair_right)"); + CHECK_EQ(uv_pipe_open(&pair_right, fds[1]), 0, "uv_pipe_open(pair_right)"); + + CHECK_EQ(uv_link_source_init(loop, &source, (uv_stream_t*) &pair_right), 0, + "uv_link_source_init()"); + + test_writes(); + test_reads(); + + uv_close((uv_handle_t*) &pair_right, NULL); + + CHECK_EQ(close(fds[0]), 0, "close(fds[0])"); +} diff --git a/test/test.gyp b/test/test.gyp new file mode 100644 index 0000000..c63a046 --- /dev/null +++ b/test/test.gyp @@ -0,0 +1,20 @@ +{ + "targets": [{ + "target_name": "uv_link_t-test", + "type": "executable", + + "include_dirs": [ + "src" + ], + + "dependencies": [ + "deps/libuv/uv.gyp:libuv", + "../uv_link_t.gyp:uv_link_t" + ], + + "sources": [ + "src/main.c", + "src/test-uv-link-source-t.c", + ], + }], +} |