diff --git a/Lib/module.c b/Lib/module.c index a273dad..3ed0299 100644 --- a/Lib/module.c +++ b/Lib/module.c @@ -337,7 +337,7 @@ module_ret_code module_become(const self_t *self, const recv_cb new_recv) { module_ret_code module_unbecome(const self_t *self) { GET_MOD_IN_STATE(self, RUNNING); - if (stack_pop(mod->recvs) == STACK_OK) { + if (stack_pop(mod->recvs) != NULL) { return MOD_OK; } return MOD_ERR; diff --git a/TODO.md b/TODO.md index 715a6ea..ae7121d 100644 --- a/TODO.md +++ b/TODO.md @@ -9,6 +9,10 @@ ### Fixes - [x] In stack_clear and map_clear, avoid unsetting dtor +- [x] Fixed module_unbecome check (stack_pop return code) that would make it always return MOD_ERR + +### Tests +- [x] Add module_unbecome and module_dispatch tests ## Ideas - [ ] Let contexts talk together? Eg: broadcast(msg, bool global) to send a message to all modules in every context; module_publish message in another context? etc etc diff --git a/tests/main.c b/tests/main.c index bb03e27..9c1dca7 100644 --- a/tests/main.c +++ b/tests/main.c @@ -58,6 +58,10 @@ int main(void) { cmocka_unit_test(test_module_become_NULL_func), cmocka_unit_test(test_module_become), + /* Test module unbecome */ + cmocka_unit_test(test_module_unbecome_NULL_self), + cmocka_unit_test(test_module_unbecome), + /* Test fd add/rm */ cmocka_unit_test(test_module_add_wrong_fd), cmocka_unit_test(test_module_add_fd_NULL_self), @@ -109,6 +113,11 @@ int main(void) { /* We have now 3 messages waiting for us (tell, publish, broadcast). Check. */ cmocka_unit_test(test_modules_ctx_loop), + /* We have 0 messages now */ + cmocka_unit_test(test_modules_ctx_dispatch_NULL_param), + cmocka_unit_test(test_modules_ctx_dispatch_NULL_ctx), + cmocka_unit_test(test_modules_ctx_dispatch), + /* Test module topic deregister */ cmocka_unit_test(test_deregister_topic_NULL_topic), cmocka_unit_test(test_deregister_topic_NULL_self), diff --git a/tests/test_module.c b/tests/test_module.c index efd47be..85a19ed 100644 --- a/tests/test_module.c +++ b/tests/test_module.c @@ -210,6 +210,24 @@ void test_module_become(void **state) { assert_true(ret == MOD_OK); } +void test_module_unbecome_NULL_self(void **state) { + (void) state; /* unused */ + + module_ret_code ret = module_unbecome(NULL); + assert_false(ret == MOD_OK); +} + +void test_module_unbecome(void **state) { + (void) state; /* unused */ + + module_ret_code ret = module_unbecome(self); + assert_true(ret == MOD_OK); + + /* We don't have any more recv in stack */ + ret = module_unbecome(self); + assert_false(ret == MOD_OK); +} + void test_module_add_wrong_fd(void **state) { (void) state; /* unused */ diff --git a/tests/test_module.h b/tests/test_module.h index 955858f..d21ee3d 100644 --- a/tests/test_module.h +++ b/tests/test_module.h @@ -24,6 +24,8 @@ void test_module_set_userdata(void **state); void test_module_become_NULL_self(void **state); void test_module_become_NULL_func(void **state); void test_module_become(void **state); +void test_module_unbecome_NULL_self(void **state); +void test_module_unbecome(void **state); void test_module_add_wrong_fd(void **state); void test_module_add_fd_NULL_self(void **state); void test_module_add_fd(void **state); diff --git a/tests/test_modules.c b/tests/test_modules.c index 4bcad15..fcb7979 100644 --- a/tests/test_modules.c +++ b/tests/test_modules.c @@ -77,3 +77,27 @@ void test_modules_ctx_loop(void **state) { module_ret_code ret = modules_ctx_loop(CTX); assert_true(ret == 3); } + +void test_modules_ctx_dispatch_NULL_param(void **state) { + (void) state; /* unused */ + + module_ret_code ret = modules_ctx_dispatch(CTX, NULL); + assert_false(ret == MOD_OK); +} + +void test_modules_ctx_dispatch_NULL_ctx(void **state) { + (void) state; /* unused */ + + int r; + module_ret_code ret = modules_ctx_dispatch(NULL, &r); + assert_false(ret == MOD_OK); +} + +void test_modules_ctx_dispatch(void **state) { + (void) state; /* unused */ + + int r; + module_ret_code ret = modules_ctx_dispatch(CTX, &r); + assert_true(ret == MOD_OK); + assert_true(r == 0); // number of messages dispatched +} diff --git a/tests/test_modules.h b/tests/test_modules.h index b419d01..80013a9 100644 --- a/tests/test_modules.h +++ b/tests/test_modules.h @@ -9,3 +9,6 @@ void test_modules_ctx_quit_no_loop(void **state); void test_modules_ctx_loop_NULL_ctx(void **state); void test_modules_ctx_loop_no_maxevents(void **state); void test_modules_ctx_loop(void **state); +void test_modules_ctx_dispatch_NULL_param(void **state); +void test_modules_ctx_dispatch_NULL_ctx(void **state); +void test_modules_ctx_dispatch(void **state);