Skip to content

Commit 7939ddf

Browse files
committed
Added a couple of tests.
1 parent 4d74942 commit 7939ddf

File tree

5 files changed

+24
-4
lines changed

5 files changed

+24
-4
lines changed

TODO.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ module_ref should then return a new self_t* object, add it to a stack in module.
3030
- [x] Use attribute format where needed
3131
- [x] Use calloc where needed
3232
- [x] Module register should avoid memleaks when it fails with NOMEM.
33+
- [x] Added modules_ctx_loop test
34+
- [x] Added module pubsub recv test
3335

3436
### Stack API
3537
- [x] Add _clear function

tests/main.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ int main(void) {
106106
cmocka_unit_test(test_module_broadcast_wrong_size),
107107
cmocka_unit_test(test_module_broadcast),
108108

109+
/* We have now 3 messages waiting for us (tell, publish, broadcast). Check. */
110+
cmocka_unit_test(test_modules_ctx_loop),
111+
109112
/* Test module topic deregister */
110113
cmocka_unit_test(test_deregister_topic_NULL_topic),
111114
cmocka_unit_test(test_deregister_topic_NULL_self),

tests/test_module.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "test_module.h"
22
#include <module/module.h>
3+
#include <module/modules.h>
34
#include <unistd.h>
45
#include <sys/stat.h>
56
#include <fcntl.h>
@@ -352,7 +353,7 @@ void test_module_tell_wrong_size(void **state) {
352353
void test_module_tell(void **state) {
353354
(void) state; /* unused */
354355

355-
module_ret_code ret = module_tell(self, testSelf, "hi!", strlen("hi!"));
356+
module_ret_code ret = module_tell(self, testSelf, "hi1!", strlen("hi!"));
356357
assert_true(ret == MOD_OK);
357358
}
358359

@@ -394,7 +395,7 @@ void test_module_publish_wrong_size(void **state) {
394395
void test_module_publish(void **state) {
395396
(void) state; /* unused */
396397

397-
module_ret_code ret = module_publish(self, "topic", "hi!", strlen("hi!"));
398+
module_ret_code ret = module_publish(self, "topic", "hi2!", strlen("hi!"));
398399
assert_true(ret == MOD_OK);
399400
}
400401

@@ -422,7 +423,7 @@ void test_module_broadcast_wrong_size(void **state) {
422423
void test_module_broadcast(void **state) {
423424
(void) state; /* unused */
424425

425-
module_ret_code ret = module_broadcast(self, "hi!", strlen("hi!"));
426+
module_ret_code ret = module_broadcast(self, "hi3!", strlen("hi!"));
426427
assert_true(ret == MOD_OK);
427428
}
428429

@@ -479,7 +480,13 @@ static bool evaluate(void) {
479480
}
480481

481482
static void recv(const msg_t *msg, const void *userdata) {
482-
483+
static int ctr = 0;
484+
if (msg->is_pubsub && msg->pubsub_msg->type == USER) {
485+
ctr++;
486+
if (!strcmp((char *)msg->pubsub_msg->message, "hi3!")) {
487+
modules_ctx_quit(CTX, ctr);
488+
}
489+
}
483490
}
484491

485492
static void destroy(void) {

tests/test_modules.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,10 @@ void test_modules_ctx_loop_no_maxevents(void **state) {
7070
module_ret_code ret = modules_ctx_loop_events(CTX, 0);
7171
assert_false(ret == MOD_OK);
7272
}
73+
74+
void test_modules_ctx_loop(void **state) {
75+
(void) state; /* unused */
76+
77+
module_ret_code ret = modules_ctx_loop(CTX);
78+
assert_true(ret == 3);
79+
}

tests/test_modules.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ void test_modules_ctx_quit_NULL_ctx(void **state);
88
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);
11+
void test_modules_ctx_loop(void **state);

0 commit comments

Comments
 (0)