From 3bf51bdcfa70fb655d61b8c8cdf83dac5692ea1a Mon Sep 17 00:00:00 2001 From: Federico Di Pierro Date: Fri, 9 Jun 2023 17:07:52 +0200 Subject: [PATCH] cleanup: added some more tests and dropped M_SRC_FORCE flag. Signed-off-by: Federico Di Pierro --- Lib/core/public/module/mod.h | 1 - Lib/core/src.c | 4 -- TODO.md | 3 - tests/main.c | 11 +++- tests/test_mod.c | 110 ++++++++++++++++++++++++++++++++--- tests/test_mod.h | 6 +- 6 files changed, 114 insertions(+), 21 deletions(-) diff --git a/Lib/core/public/module/mod.h b/Lib/core/public/module/mod.h index f8b228cf..e0ec948e 100644 --- a/Lib/core/public/module/mod.h +++ b/Lib/core/public/module/mod.h @@ -34,7 +34,6 @@ typedef enum { M_SRC_AUTOFREE = 1 << 3, // Automatically free userdata upon source deregistation. M_SRC_ONESHOT = 1 << 4, // Run just once then automatically deregister source. M_SRC_DUP = 1 << 5, // Duplicate PubSub topic, source fd or source path. - M_SRC_FORCE = 1 << 6, // Force the registration of a src, even if it is already existent (it deregisters previous src) M_SRC_FD_AUTOCLOSE = M_SRC_SHIFT(M_SRC_TYPE_FD, 1 << 0), // Automatically close fd upon deregistation. M_SRC_TMR_ABSOLUTE = M_SRC_SHIFT(M_SRC_TYPE_TMR, 1 << 0), // Absolute timer } m_src_flags; diff --git a/Lib/core/src.c b/Lib/core/src.c index 3498f152..f2544faa 100644 --- a/Lib/core/src.c +++ b/Lib/core/src.c @@ -373,10 +373,6 @@ int register_mod_src(m_mod_t *mod, m_src_types type, const void *src_data, return -EINVAL; } int ret = m_bst_insert(mod->srcs[type], src); - if (ret == -EEXIST && flags & M_SRC_FORCE) { - m_bst_remove(mod->srcs[type], src); - ret = m_bst_insert(mod->srcs[type], src); - } if (ret == 0) { /* If a src is registered at runtime, start receiving its events immediately */ if (m_mod_is(mod, M_MOD_RUNNING)) { diff --git a/TODO.md b/TODO.md index 8dc318d3..58b4bf27 100644 --- a/TODO.md +++ b/TODO.md @@ -10,10 +10,7 @@ - [ ] Properly fixup M_MOD_CONSUME_TOKEN() to only be called by external API (ie: user visible) ### Src -- [x] add an M_SRC_FORCE flag to register_mod_src to force register a src even if the same is already existing (deregistering the old one)? - [x] double check m_bst_insert/remove usage in src API + add unit tests! -- [ ] Impl M_SRC_FORCE for topic too? -- [ ] add a poll_refresh_src API that calls: epoll_ctl with EPOLL_CTL_MOD, or kqueue ADD to refresh an event trigger, instead of removing and adding back the event #### Ctx - [ ] use pthread_setname_np() to store each context thread name (max 16chars len; drop ctx->name field) ? diff --git a/tests/main.c b/tests/main.c index 2833bfe2..c8fb430a 100644 --- a/tests/main.c +++ b/tests/main.c @@ -35,12 +35,19 @@ int main(void) { */ cmocka_unit_test(test_mod_register_same_name), - /* Test modules_ API */ + /* Test ctx API */ cmocka_unit_test(test_ctx_set_logger_NULL_logger), cmocka_unit_test(test_ctx_set_logger), cmocka_unit_test(test_ctx_quit_no_loop), cmocka_unit_test(test_ctx_dump), + /* Test module src api */ + cmocka_unit_test(test_mod_src_tmr), + cmocka_unit_test(test_mod_src_sgn), + cmocka_unit_test(test_mod_src_path), + cmocka_unit_test(test_mod_src_pid), + cmocka_unit_test(test_mod_src_thresh), + /* Test module state setters */ cmocka_unit_test(test_mod_start_NULL_self), cmocka_unit_test(test_mod_start), @@ -79,8 +86,6 @@ int main(void) { cmocka_unit_test(test_mod_rm_fd_NULL_self), cmocka_unit_test(test_mod_rm_fd), - cmocka_unit_test(test_mod_srcs), - /* Test module subscribe */ cmocka_unit_test(test_mod_subscribe_NULL_topic), cmocka_unit_test(test_mod_subscribe_NULL_self), diff --git a/tests/test_mod.c b/tests/test_mod.c index 0094ebb6..91372903 100644 --- a/tests/test_mod.c +++ b/tests/test_mod.c @@ -7,6 +7,12 @@ #include #include #include +#ifdef __linux__ +#include +#else +#include +#endif +#include static bool init_false(m_mod_t *mod); static void mod_recv(m_mod_t *mod, const m_queue_t *const evts); @@ -261,24 +267,18 @@ void test_mod_add_fd(void **state) { assert_true(ret == -EEXIST); } - -void test_mod_srcs(void **state) { +void test_mod_src_tmr(void **state) { (void) state; /* unused */ - // 1000s just to test const m_src_tmr_t my_tmr = {.ns = 5000 }; - int ret = m_mod_src_register_tmr(test_mod, &my_tmr, M_SRC_FD_AUTOCLOSE, NULL); + int ret = m_mod_src_register_tmr(test_mod, &my_tmr, 0, NULL); assert_true(ret == 0); /* Try to register again, expect -EEXIST error */ - ret = m_mod_src_register_tmr(test_mod, &my_tmr, M_SRC_FD_AUTOCLOSE, NULL); + ret = m_mod_src_register_tmr(test_mod, &my_tmr, 0, NULL); assert_true(ret == -EEXIST); - /* Register again, forcing the registration. */ - ret = m_mod_src_register_tmr(test_mod, &my_tmr, M_SRC_FD_AUTOCLOSE | M_SRC_FORCE, NULL); - assert_true(ret == 0); - size_t len = m_mod_src_len(test_mod, M_SRC_TYPE_TMR); assert_true(len == 1); @@ -289,6 +289,98 @@ void test_mod_srcs(void **state) { assert_true(len == 0); } +void test_mod_src_sgn(void **state) { + (void) state; /* unused */ + + const m_src_sgn_t my_sgn = {.signo = 10 }; + + int ret = m_mod_src_register_sgn(test_mod, &my_sgn, 0, NULL); + assert_true(ret == 0); + + /* Try to register again, expect -EEXIST error */ + ret = m_mod_src_register_sgn(test_mod, &my_sgn, 0, NULL); + assert_true(ret == -EEXIST); + + size_t len = m_mod_src_len(test_mod, M_SRC_TYPE_SGN); + assert_true(len == 1); + + ret = m_mod_src_deregister_sgn(test_mod, &my_sgn); + assert_true(ret == 0); + + len = m_mod_src_len(test_mod, M_SRC_TYPE_SGN); + assert_true(len == 0); +} + +void test_mod_src_path(void **state) { + (void) state; /* unused */ + + #ifdef __linux__ + const m_src_path_t my_path = {.path = "/tmp", .events = IN_CREATE }; + #else + const m_src_path_t my_path = {.path = "/tmp", .events = NOTE_WRITE }; + #endif + + int ret = m_mod_src_register_path(test_mod, &my_path, 0, NULL); + assert_true(ret == 0); + + /* Try to register again, expect -EEXIST error */ + ret = m_mod_src_register_path(test_mod, &my_path, 0, NULL); + assert_true(ret == -EEXIST); + + size_t len = m_mod_src_len(test_mod, M_SRC_TYPE_PATH); + assert_true(len == 1); + + ret = m_mod_src_deregister_path(test_mod, &my_path); + assert_true(ret == 0); + + len = m_mod_src_len(test_mod, M_SRC_TYPE_PATH); + assert_true(len == 0); +} + +void test_mod_src_pid(void **state) { + (void) state; /* unused */ + + const m_src_pid_t my_pid = {.pid = getpid(), .events = 0 }; + + int ret = m_mod_src_register_pid(test_mod, &my_pid, 0, NULL); + assert_true(ret == 0); + + /* Try to register again, expect -EEXIST error */ + ret = m_mod_src_register_pid(test_mod, &my_pid, 0, NULL); + assert_true(ret == -EEXIST); + + size_t len = m_mod_src_len(test_mod, M_SRC_TYPE_PID); + assert_true(len == 1); + + ret = m_mod_src_deregister_pid(test_mod, &my_pid); + assert_true(ret == 0); + + len = m_mod_src_len(test_mod, M_SRC_TYPE_PID); + assert_true(len == 0); +} + +void test_mod_src_thresh(void **state) { + (void) state; /* unused */ + + const m_src_thresh_t my_thresh = {.activity_freq = 10.0f }; + + int ret = m_mod_src_register_thresh(test_mod, &my_thresh, 0, NULL); + assert_true(ret == 0); + + /* Try to register again, expect -EEXIST error */ + ret = m_mod_src_register_thresh(test_mod, &my_thresh, 0, NULL); + assert_true(ret == -EEXIST); + + size_t len = m_mod_src_len(test_mod, M_SRC_TYPE_THRESH); + assert_true(len == 1); + + ret = m_mod_src_deregister_thresh(test_mod, &my_thresh); + assert_true(ret == 0); + + len = m_mod_src_len(test_mod, M_SRC_TYPE_THRESH); + assert_true(len == 0); +} + void test_mod_rm_wrong_fd(void **state) { (void) state; /* unused */ diff --git a/tests/test_mod.h b/tests/test_mod.h index 01475575..12b8450b 100644 --- a/tests/test_mod.h +++ b/tests/test_mod.h @@ -27,7 +27,11 @@ void test_mod_unbecome(void **state); void test_mod_add_wrong_fd(void **state); void test_mod_add_fd_NULL_self(void **state); void test_mod_add_fd(void **state); -void test_mod_srcs(void **state); +void test_mod_src_tmr(void **state); +void test_mod_src_sgn(void **state); +void test_mod_src_path(void **state); +void test_mod_src_pid(void **state); +void test_mod_src_thresh(void **state); void test_mod_rm_wrong_fd(void **state); void test_mod_rm_wrong_fd_2(void **state); void test_mod_rm_fd_NULL_self(void **state);