Add iomux signal test
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
#include <check.h>
|
||||
#include <signal.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include "ucore/iomux_signal.h"
|
||||
|
||||
|
||||
static enum IOMUX_TYPE g_iomux_type = IOMUX_TYPE_DEFAULT;
|
||||
static int g_usr1_seen;
|
||||
static int g_usr2_seen;
|
||||
static void set_iomux_type_select(void)
|
||||
{
|
||||
g_iomux_type = IOMUX_TYPE_SELECT;
|
||||
g_usr2_seen = g_usr1_seen = 0;
|
||||
}
|
||||
|
||||
static void set_iomux_type_epoll(void)
|
||||
{
|
||||
g_iomux_type = IOMUX_TYPE_EPOLL;
|
||||
g_usr2_seen = g_usr1_seen = 0;
|
||||
}
|
||||
|
||||
|
||||
static void usr1_handler(struct IOMux *mux, int signo, void *cookie)
|
||||
{
|
||||
ck_assert_int_eq(signo, SIGUSR1);
|
||||
g_usr1_seen++;
|
||||
raise(SIGUSR2);
|
||||
}
|
||||
|
||||
static void usr2_handler(struct IOMux *mux, int signo, void *cookie)
|
||||
{
|
||||
int *val = cookie;
|
||||
int rc;
|
||||
|
||||
ck_assert_int_eq(signo, SIGUSR2);
|
||||
ck_assert_int_eq(*val, 0x1234);
|
||||
g_usr2_seen++;
|
||||
|
||||
rc = iomux_unregister_unixsignal(mux, SIGUSR2);
|
||||
ck_assert_int_eq(rc, 0);
|
||||
|
||||
rc = iomux_unregister_unixsignal(mux, SIGUSR1);
|
||||
ck_assert_int_eq(rc, 0);
|
||||
}
|
||||
|
||||
START_TEST (test_iomux_signal_simple)
|
||||
|
||||
|
||||
struct IOMux *mux = iomux_create(g_iomux_type);
|
||||
int rc;
|
||||
int cookie = 0x1234;
|
||||
|
||||
//can't handle SIGKILL
|
||||
rc = iomux_register_unixsignal(mux, SIGKILL, usr1_handler, NULL);
|
||||
ck_assert_int_ne(rc, 0);
|
||||
|
||||
rc = iomux_register_unixsignal(mux, SIGUSR1, usr1_handler, NULL);
|
||||
ck_assert_int_eq(rc, 0);
|
||||
|
||||
//not priorly registred
|
||||
rc = iomux_unregister_unixsignal(mux, SIGINT);
|
||||
ck_assert_int_ne(rc, 0);
|
||||
|
||||
rc = iomux_unregister_unixsignal(mux, SIGUSR1);
|
||||
ck_assert_int_eq(rc, 0);
|
||||
|
||||
rc = iomux_register_unixsignal(mux, SIGUSR1, usr1_handler, NULL);
|
||||
ck_assert_int_eq(rc, 0);
|
||||
|
||||
rc = iomux_register_unixsignal(mux, SIGUSR2, usr2_handler, &cookie);
|
||||
ck_assert_int_eq(rc, 0);
|
||||
|
||||
//already registred
|
||||
rc = iomux_register_unixsignal(mux, SIGUSR1, usr1_handler, NULL);
|
||||
ck_assert_int_ne(rc, 0);
|
||||
|
||||
raise(SIGUSR1);
|
||||
iomux_run(mux);
|
||||
|
||||
|
||||
iomux_delete(mux);
|
||||
|
||||
ck_assert_int_eq(g_usr1_seen, 1);
|
||||
ck_assert_int_eq(g_usr2_seen, 1);
|
||||
END_TEST
|
||||
|
||||
|
||||
Suite *iomux_signal_suite(void)
|
||||
{
|
||||
Suite *s = suite_create("iomux_signal");
|
||||
TCase *select_tc = tcase_create("iomux signal select tests");
|
||||
TCase *epoll_tc = tcase_create("iomux signal epoll tests");
|
||||
|
||||
tcase_add_test(select_tc, test_iomux_signal_simple);
|
||||
tcase_add_test(epoll_tc, test_iomux_signal_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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user