Skip to content

Commit 3bf51bd

Browse files
committed
cleanup: added some more tests and dropped M_SRC_FORCE flag.
Signed-off-by: Federico Di Pierro <[email protected]>
1 parent 5480e28 commit 3bf51bd

File tree

6 files changed

+114
-21
lines changed

6 files changed

+114
-21
lines changed

Lib/core/public/module/mod.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ typedef enum {
3434
M_SRC_AUTOFREE = 1 << 3, // Automatically free userdata upon source deregistation.
3535
M_SRC_ONESHOT = 1 << 4, // Run just once then automatically deregister source.
3636
M_SRC_DUP = 1 << 5, // Duplicate PubSub topic, source fd or source path.
37-
M_SRC_FORCE = 1 << 6, // Force the registration of a src, even if it is already existent (it deregisters previous src)
3837
M_SRC_FD_AUTOCLOSE = M_SRC_SHIFT(M_SRC_TYPE_FD, 1 << 0), // Automatically close fd upon deregistation.
3938
M_SRC_TMR_ABSOLUTE = M_SRC_SHIFT(M_SRC_TYPE_TMR, 1 << 0), // Absolute timer
4039
} m_src_flags;

Lib/core/src.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -373,10 +373,6 @@ int register_mod_src(m_mod_t *mod, m_src_types type, const void *src_data,
373373
return -EINVAL;
374374
}
375375
int ret = m_bst_insert(mod->srcs[type], src);
376-
if (ret == -EEXIST && flags & M_SRC_FORCE) {
377-
m_bst_remove(mod->srcs[type], src);
378-
ret = m_bst_insert(mod->srcs[type], src);
379-
}
380376
if (ret == 0) {
381377
/* If a src is registered at runtime, start receiving its events immediately */
382378
if (m_mod_is(mod, M_MOD_RUNNING)) {

TODO.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@
1010
- [ ] Properly fixup M_MOD_CONSUME_TOKEN() to only be called by external API (ie: user visible)
1111

1212
### Src
13-
- [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)?
1413
- [x] double check m_bst_insert/remove usage in src API + add unit tests!
15-
- [ ] Impl M_SRC_FORCE for topic too?
16-
- [ ] 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
1714

1815
#### Ctx
1916
- [ ] use pthread_setname_np() to store each context thread name (max 16chars len; drop ctx->name field) ?

tests/main.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,19 @@ int main(void) {
3535
*/
3636
cmocka_unit_test(test_mod_register_same_name),
3737

38-
/* Test modules_ API */
38+
/* Test ctx API */
3939
cmocka_unit_test(test_ctx_set_logger_NULL_logger),
4040
cmocka_unit_test(test_ctx_set_logger),
4141
cmocka_unit_test(test_ctx_quit_no_loop),
4242
cmocka_unit_test(test_ctx_dump),
4343

44+
/* Test module src api */
45+
cmocka_unit_test(test_mod_src_tmr),
46+
cmocka_unit_test(test_mod_src_sgn),
47+
cmocka_unit_test(test_mod_src_path),
48+
cmocka_unit_test(test_mod_src_pid),
49+
cmocka_unit_test(test_mod_src_thresh),
50+
4451
/* Test module state setters */
4552
cmocka_unit_test(test_mod_start_NULL_self),
4653
cmocka_unit_test(test_mod_start),
@@ -79,8 +86,6 @@ int main(void) {
7986
cmocka_unit_test(test_mod_rm_fd_NULL_self),
8087
cmocka_unit_test(test_mod_rm_fd),
8188

82-
cmocka_unit_test(test_mod_srcs),
83-
8489
/* Test module subscribe */
8590
cmocka_unit_test(test_mod_subscribe_NULL_topic),
8691
cmocka_unit_test(test_mod_subscribe_NULL_self),

tests/test_mod.c

Lines changed: 101 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77
#include <fcntl.h>
88
#include <errno.h>
99
#include <sys/socket.h>
10+
#ifdef __linux__
11+
#include <sys/inotify.h>
12+
#else
13+
#include <sys/event.h>
14+
#endif
15+
#include <unistd.h>
1016

1117
static bool init_false(m_mod_t *mod);
1218
static void mod_recv(m_mod_t *mod, const m_queue_t *const evts);
@@ -261,24 +267,18 @@ void test_mod_add_fd(void **state) {
261267
assert_true(ret == -EEXIST);
262268
}
263269

264-
265-
void test_mod_srcs(void **state) {
270+
void test_mod_src_tmr(void **state) {
266271
(void) state; /* unused */
267272

268-
// 1000s just to test
269273
const m_src_tmr_t my_tmr = {.ns = 5000 };
270274

271-
int ret = m_mod_src_register_tmr(test_mod, &my_tmr, M_SRC_FD_AUTOCLOSE, NULL);
275+
int ret = m_mod_src_register_tmr(test_mod, &my_tmr, 0, NULL);
272276
assert_true(ret == 0);
273277

274278
/* Try to register again, expect -EEXIST error */
275-
ret = m_mod_src_register_tmr(test_mod, &my_tmr, M_SRC_FD_AUTOCLOSE, NULL);
279+
ret = m_mod_src_register_tmr(test_mod, &my_tmr, 0, NULL);
276280
assert_true(ret == -EEXIST);
277281

278-
/* Register again, forcing the registration. */
279-
ret = m_mod_src_register_tmr(test_mod, &my_tmr, M_SRC_FD_AUTOCLOSE | M_SRC_FORCE, NULL);
280-
assert_true(ret == 0);
281-
282282
size_t len = m_mod_src_len(test_mod, M_SRC_TYPE_TMR);
283283
assert_true(len == 1);
284284

@@ -289,6 +289,98 @@ void test_mod_srcs(void **state) {
289289
assert_true(len == 0);
290290
}
291291

292+
void test_mod_src_sgn(void **state) {
293+
(void) state; /* unused */
294+
295+
const m_src_sgn_t my_sgn = {.signo = 10 };
296+
297+
int ret = m_mod_src_register_sgn(test_mod, &my_sgn, 0, NULL);
298+
assert_true(ret == 0);
299+
300+
/* Try to register again, expect -EEXIST error */
301+
ret = m_mod_src_register_sgn(test_mod, &my_sgn, 0, NULL);
302+
assert_true(ret == -EEXIST);
303+
304+
size_t len = m_mod_src_len(test_mod, M_SRC_TYPE_SGN);
305+
assert_true(len == 1);
306+
307+
ret = m_mod_src_deregister_sgn(test_mod, &my_sgn);
308+
assert_true(ret == 0);
309+
310+
len = m_mod_src_len(test_mod, M_SRC_TYPE_SGN);
311+
assert_true(len == 0);
312+
}
313+
314+
void test_mod_src_path(void **state) {
315+
(void) state; /* unused */
316+
317+
#ifdef __linux__
318+
const m_src_path_t my_path = {.path = "/tmp", .events = IN_CREATE };
319+
#else
320+
const m_src_path_t my_path = {.path = "/tmp", .events = NOTE_WRITE };
321+
#endif
322+
323+
int ret = m_mod_src_register_path(test_mod, &my_path, 0, NULL);
324+
assert_true(ret == 0);
325+
326+
/* Try to register again, expect -EEXIST error */
327+
ret = m_mod_src_register_path(test_mod, &my_path, 0, NULL);
328+
assert_true(ret == -EEXIST);
329+
330+
size_t len = m_mod_src_len(test_mod, M_SRC_TYPE_PATH);
331+
assert_true(len == 1);
332+
333+
ret = m_mod_src_deregister_path(test_mod, &my_path);
334+
assert_true(ret == 0);
335+
336+
len = m_mod_src_len(test_mod, M_SRC_TYPE_PATH);
337+
assert_true(len == 0);
338+
}
339+
340+
void test_mod_src_pid(void **state) {
341+
(void) state; /* unused */
342+
343+
const m_src_pid_t my_pid = {.pid = getpid(), .events = 0 };
344+
345+
int ret = m_mod_src_register_pid(test_mod, &my_pid, 0, NULL);
346+
assert_true(ret == 0);
347+
348+
/* Try to register again, expect -EEXIST error */
349+
ret = m_mod_src_register_pid(test_mod, &my_pid, 0, NULL);
350+
assert_true(ret == -EEXIST);
351+
352+
size_t len = m_mod_src_len(test_mod, M_SRC_TYPE_PID);
353+
assert_true(len == 1);
354+
355+
ret = m_mod_src_deregister_pid(test_mod, &my_pid);
356+
assert_true(ret == 0);
357+
358+
len = m_mod_src_len(test_mod, M_SRC_TYPE_PID);
359+
assert_true(len == 0);
360+
}
361+
362+
void test_mod_src_thresh(void **state) {
363+
(void) state; /* unused */
364+
365+
const m_src_thresh_t my_thresh = {.activity_freq = 10.0f };
366+
367+
int ret = m_mod_src_register_thresh(test_mod, &my_thresh, 0, NULL);
368+
assert_true(ret == 0);
369+
370+
/* Try to register again, expect -EEXIST error */
371+
ret = m_mod_src_register_thresh(test_mod, &my_thresh, 0, NULL);
372+
assert_true(ret == -EEXIST);
373+
374+
size_t len = m_mod_src_len(test_mod, M_SRC_TYPE_THRESH);
375+
assert_true(len == 1);
376+
377+
ret = m_mod_src_deregister_thresh(test_mod, &my_thresh);
378+
assert_true(ret == 0);
379+
380+
len = m_mod_src_len(test_mod, M_SRC_TYPE_THRESH);
381+
assert_true(len == 0);
382+
}
383+
292384
void test_mod_rm_wrong_fd(void **state) {
293385
(void) state; /* unused */
294386

tests/test_mod.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ void test_mod_unbecome(void **state);
2727
void test_mod_add_wrong_fd(void **state);
2828
void test_mod_add_fd_NULL_self(void **state);
2929
void test_mod_add_fd(void **state);
30-
void test_mod_srcs(void **state);
30+
void test_mod_src_tmr(void **state);
31+
void test_mod_src_sgn(void **state);
32+
void test_mod_src_path(void **state);
33+
void test_mod_src_pid(void **state);
34+
void test_mod_src_thresh(void **state);
3135
void test_mod_rm_wrong_fd(void **state);
3236
void test_mod_rm_wrong_fd_2(void **state);
3337
void test_mod_rm_fd_NULL_self(void **state);

0 commit comments

Comments
 (0)