Files
libucore/test/test_iomux.c
T
2015-05-12 19:31:15 +02:00

131 lines
2.7 KiB
C

#include <check.h>
#include <string.h>
#include <unistd.h>
#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++;
fail_if(event != MUX_EV_READ);
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;
fail_if(event != MUX_EV_WRITE);
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;
}