Skip to content

Commit aad0136

Browse files
committed
Fixed small bug in module_unbecome(). Added module_unbecome() and modules_ctx_dispatch() tests.
1 parent 7a383d8 commit aad0136

File tree

7 files changed

+61
-1
lines changed

7 files changed

+61
-1
lines changed

Lib/module.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ module_ret_code module_become(const self_t *self, const recv_cb new_recv) {
337337
module_ret_code module_unbecome(const self_t *self) {
338338
GET_MOD_IN_STATE(self, RUNNING);
339339

340-
if (stack_pop(mod->recvs) == STACK_OK) {
340+
if (stack_pop(mod->recvs) != NULL) {
341341
return MOD_OK;
342342
}
343343
return MOD_ERR;

TODO.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99

1010
### Fixes
1111
- [x] In stack_clear and map_clear, avoid unsetting dtor
12+
- [x] Fixed module_unbecome check (stack_pop return code) that would make it always return MOD_ERR
13+
14+
### Tests
15+
- [x] Add module_unbecome and module_dispatch tests
1216

1317
## Ideas
1418
- [ ] 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

tests/main.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ int main(void) {
5858
cmocka_unit_test(test_module_become_NULL_func),
5959
cmocka_unit_test(test_module_become),
6060

61+
/* Test module unbecome */
62+
cmocka_unit_test(test_module_unbecome_NULL_self),
63+
cmocka_unit_test(test_module_unbecome),
64+
6165
/* Test fd add/rm */
6266
cmocka_unit_test(test_module_add_wrong_fd),
6367
cmocka_unit_test(test_module_add_fd_NULL_self),
@@ -109,6 +113,11 @@ int main(void) {
109113
/* We have now 3 messages waiting for us (tell, publish, broadcast). Check. */
110114
cmocka_unit_test(test_modules_ctx_loop),
111115

116+
/* We have 0 messages now */
117+
cmocka_unit_test(test_modules_ctx_dispatch_NULL_param),
118+
cmocka_unit_test(test_modules_ctx_dispatch_NULL_ctx),
119+
cmocka_unit_test(test_modules_ctx_dispatch),
120+
112121
/* Test module topic deregister */
113122
cmocka_unit_test(test_deregister_topic_NULL_topic),
114123
cmocka_unit_test(test_deregister_topic_NULL_self),

tests/test_module.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,24 @@ void test_module_become(void **state) {
210210
assert_true(ret == MOD_OK);
211211
}
212212

213+
void test_module_unbecome_NULL_self(void **state) {
214+
(void) state; /* unused */
215+
216+
module_ret_code ret = module_unbecome(NULL);
217+
assert_false(ret == MOD_OK);
218+
}
219+
220+
void test_module_unbecome(void **state) {
221+
(void) state; /* unused */
222+
223+
module_ret_code ret = module_unbecome(self);
224+
assert_true(ret == MOD_OK);
225+
226+
/* We don't have any more recv in stack */
227+
ret = module_unbecome(self);
228+
assert_false(ret == MOD_OK);
229+
}
230+
213231
void test_module_add_wrong_fd(void **state) {
214232
(void) state; /* unused */
215233

tests/test_module.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ void test_module_set_userdata(void **state);
2424
void test_module_become_NULL_self(void **state);
2525
void test_module_become_NULL_func(void **state);
2626
void test_module_become(void **state);
27+
void test_module_unbecome_NULL_self(void **state);
28+
void test_module_unbecome(void **state);
2729
void test_module_add_wrong_fd(void **state);
2830
void test_module_add_fd_NULL_self(void **state);
2931
void test_module_add_fd(void **state);

tests/test_modules.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,27 @@ void test_modules_ctx_loop(void **state) {
7777
module_ret_code ret = modules_ctx_loop(CTX);
7878
assert_true(ret == 3);
7979
}
80+
81+
void test_modules_ctx_dispatch_NULL_param(void **state) {
82+
(void) state; /* unused */
83+
84+
module_ret_code ret = modules_ctx_dispatch(CTX, NULL);
85+
assert_false(ret == MOD_OK);
86+
}
87+
88+
void test_modules_ctx_dispatch_NULL_ctx(void **state) {
89+
(void) state; /* unused */
90+
91+
int r;
92+
module_ret_code ret = modules_ctx_dispatch(NULL, &r);
93+
assert_false(ret == MOD_OK);
94+
}
95+
96+
void test_modules_ctx_dispatch(void **state) {
97+
(void) state; /* unused */
98+
99+
int r;
100+
module_ret_code ret = modules_ctx_dispatch(CTX, &r);
101+
assert_true(ret == MOD_OK);
102+
assert_true(r == 0); // number of messages dispatched
103+
}

tests/test_modules.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,6 @@ void test_modules_ctx_quit_no_loop(void **state);
99
void test_modules_ctx_loop_NULL_ctx(void **state);
1010
void test_modules_ctx_loop_no_maxevents(void **state);
1111
void test_modules_ctx_loop(void **state);
12+
void test_modules_ctx_dispatch_NULL_param(void **state);
13+
void test_modules_ctx_dispatch_NULL_ctx(void **state);
14+
void test_modules_ctx_dispatch(void **state);

0 commit comments

Comments
 (0)