Skip to content

Commit

Permalink
cleanup: added some more tests and dropped M_SRC_FORCE flag.
Browse files Browse the repository at this point in the history
Signed-off-by: Federico Di Pierro <[email protected]>
  • Loading branch information
FedeDP committed Jun 9, 2023
1 parent 5480e28 commit 3bf51bd
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 21 deletions.
1 change: 0 additions & 1 deletion Lib/core/public/module/mod.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 0 additions & 4 deletions Lib/core/src.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
3 changes: 0 additions & 3 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) ?
Expand Down
11 changes: 8 additions & 3 deletions tests/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down
110 changes: 101 additions & 9 deletions tests/test_mod.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
#include <fcntl.h>
#include <errno.h>
#include <sys/socket.h>
#ifdef __linux__
#include <sys/inotify.h>
#else
#include <sys/event.h>
#endif
#include <unistd.h>

static bool init_false(m_mod_t *mod);
static void mod_recv(m_mod_t *mod, const m_queue_t *const evts);
Expand Down Expand Up @@ -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);

Expand All @@ -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 */

Expand Down
6 changes: 5 additions & 1 deletion tests/test_mod.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 3bf51bd

Please sign in to comment.