From 4134482fdbf376848c5b8f6092f2e25282123d15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nils=20O=2E=20Sel=C3=A5sdal?= Date: Mon, 11 May 2015 23:17:30 +0200 Subject: [PATCH] Add iomux tests --- test/test_iomux.c | 127 +++++++++++++++++++++++++++++++++++++++++++++ test/test_runner.c | 4 +- 2 files changed, 130 insertions(+), 1 deletion(-) create mode 100644 test/test_iomux.c diff --git a/test/test_iomux.c b/test/test_iomux.c new file mode 100644 index 0000000..ed4f10b --- /dev/null +++ b/test/test_iomux.c @@ -0,0 +1,127 @@ +#include +#include +#include +#include "ucore/timers.h" +#include "ucore/iomux.h" +#include "ucore/clock.h" + + +static enum IOMUX_TYPE g_iomux_type = IOMUX_TYPE_DEFAULT; + +void set_iomux_type_select(void) +{ + g_iomux_type = IOMUX_TYPE_SELECT; +} + +void set_iomux_type_epoll(void) +{ + g_iomux_type = IOMUX_TYPE_EPOLL; +} + +static void simple_rcb(struct IOMux *mux, struct IOMuxFD *fd, unsigned int event) +{ + char buf[64]; + ssize_t r; + + /** we should get here 2 times. first for reading the string, + * then for reading that the pipe has closed */ + + ck_assert_int_le(fd->cookie_int, 2); + fd->cookie_int++; + + r = read(fd->fd, buf, sizeof buf); + ck_assert(r >= 0); + + if (r > 0) { + ck_assert_int_eq(r, 5); + ck_assert(strncmp(buf, "Hello", 5) == 0); + } + + if (r == 0) { + int rc; + rc = iomux_unregister_fd(mux, fd); + ck_assert_int_eq(rc, 0); + close(fd->fd); + } +} + +static void simple_wcb(struct IOMux *mux, struct IOMuxFD *fd, unsigned int event) +{ + ssize_t written; + int rc; + + ck_assert_int_eq(fd->cookie_int, 0); + fd->cookie_int = 1; + + written = write(fd->fd, "Hello", 5); + ck_assert_int_eq(written, 5); + + rc = iomux_unregister_fd(mux, fd); + ck_assert_int_eq(rc, 0); + + close(fd->fd); +} + + +START_TEST (test_iomux_pipe_simple) + struct IOMux *mux; + struct IOMuxFD rfd; + struct IOMuxFD wfd; + /** + * Simple test that writes a small string to a pipe + * and reads it back. This assumes the small write/read + * can be done with just 1 write/read call. + */ + + int pipefd[2]; + int rc; + + mux = iomux_create(g_iomux_type); + + rc = pipe(pipefd); + ck_assert_int_eq(rc, 0); + + memset(&rfd, 0, sizeof rfd); + memset(&wfd, 0, sizeof wfd); + + rfd.fd = pipefd[0]; + rfd.what = MUX_EV_READ; + rfd.callback = simple_rcb; + + wfd.fd = pipefd[1]; + wfd.what = MUX_EV_WRITE; + wfd.callback = simple_wcb; + + rc = iomux_register_fd(mux, &rfd); + ck_assert_int_eq(rc, 0); + + rc = iomux_register_fd(mux, &wfd); + ck_assert_int_eq(rc, 0); + + rc = iomux_run(mux); + ck_assert_int_eq(rc, 0); + + iomux_delete(mux); + +END_TEST + + +Suite *iomux_suite(void) +{ + Suite *s = suite_create("iomux"); + TCase *select_tc = tcase_create("iomux select tests"); + TCase *epoll_tc = tcase_create("iomux epoll tests"); + + tcase_add_test(select_tc, test_iomux_pipe_simple); + tcase_add_test(epoll_tc, test_iomux_pipe_simple); + + tcase_add_checked_fixture(select_tc, set_iomux_type_select, NULL); + + tcase_add_checked_fixture(epoll_tc, set_iomux_type_epoll, NULL); + + suite_add_tcase(s, select_tc); + suite_add_tcase(s, epoll_tc); + + return s; +} + diff --git a/test/test_runner.c b/test/test_runner.c index b5a3691..f95502c 100644 --- a/test/test_runner.c +++ b/test/test_runner.c @@ -32,6 +32,7 @@ extern Suite *dstr_suite(void); extern Suite *val_str_suite(void); extern Suite *ticket_lock_suite(void); extern Suite *restart_counter_suite(void); +extern Suite *iomux_suite(void); static suite_func suites[] = { bitvec_suite, @@ -58,7 +59,8 @@ static suite_func suites[] = { dstr_suite, val_str_suite, ticket_lock_suite, - restart_counter_suite + restart_counter_suite, + iomux_suite }; void