Skip to content

Commit

Permalink
Fixed small bug in module_unbecome(). Added module_unbecome() and mod…
Browse files Browse the repository at this point in the history
…ules_ctx_dispatch() tests.
  • Loading branch information
FedeDP committed Mar 17, 2019
1 parent 7a383d8 commit aad0136
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Lib/module.c
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions TODO.md
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions tests/main.c
Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down
18 changes: 18 additions & 0 deletions tests/test_module.c
Expand Up @@ -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 */

Expand Down
2 changes: 2 additions & 0 deletions tests/test_module.h
Expand Up @@ -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);
Expand Down
24 changes: 24 additions & 0 deletions tests/test_modules.c
Expand Up @@ -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
}
3 changes: 3 additions & 0 deletions tests/test_modules.h
Expand Up @@ -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);

0 comments on commit aad0136

Please sign in to comment.