From 5b25f89d00433f1c11576d1ea19304797dc60d1b Mon Sep 17 00:00:00 2001 From: Dirk Engling Date: Sat, 31 Oct 2026 18:48:00 +0100 Subject: Refactor ot_workqueue out of ot_mutex, no behaviour change --- tests/runner.c | 2 +- tests/t_mutex.c | 107 ---------------------------------------------------- tests/t_workqueue.c | 107 ++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/test.h | 2 +- 4 files changed, 109 insertions(+), 109 deletions(-) delete mode 100644 tests/t_mutex.c create mode 100644 tests/t_workqueue.c (limited to 'tests') diff --git a/tests/runner.c b/tests/runner.c index 71a5e2a..38bfb86 100644 --- a/tests/runner.c +++ b/tests/runner.c @@ -27,7 +27,7 @@ static const struct module modules[] = {{"ot_urlencoded", test_urlencoded}, {"ot_buf", test_buf}, {"ot_ip", test_ip}, {"ot_socket", test_socket}, - {"ot_mutex", test_mutex}, + {"ot_workqueue", test_workqueue}, {"ot_batch", test_batch}, {"ot_fdstate", test_fdstate}, {"ot_event", test_event}, diff --git a/tests/t_mutex.c b/tests/t_mutex.c deleted file mode 100644 index 3307d70..0000000 --- a/tests/t_mutex.c +++ /dev/null @@ -1,107 +0,0 @@ -/* This software was written by Dirk Engling - It is considered beerware. Prost. Skol. Cheers or whatever. - - $id$ */ - -/* System */ -#include -#include - -/* Opentracker */ -#include "ot_mutex.h" -#include "test.h" - -/* A worker asks for a class of task and blocks until one turns up, so a task - whose class matches no worker is never claimed and whoever is waiting for - its answer waits forever. These cases pin the matching. */ - -static void case_task_goes_to_its_own_class(void) { - ot_tasktype asked; - ot_taskid taskid; - - test_case("a worker is handed the task of the class it asked for"); - CHECK_INT(mutex_workqueue_pushtask(41, TASK_FULLSCRAPE_TPB_ASCII), 0); - asked = TASK_FULLSCRAPE; - taskid = mutex_workqueue_poptask(&asked); - CHECK(taskid != 0); - - test_case("and the full task type, not just its class"); - CHECK_INT(asked, TASK_FULLSCRAPE_TPB_ASCII); - mutex_workqueue_canceltask(41); -} - -static void case_flags_survive_the_queue(void) { - ot_tasktype asked; - - test_case("flags outside the class mask are carried through"); - CHECK_INT(mutex_workqueue_pushtask(42, TASK_FULLSCRAPE_TPB_URLENCODED | TASK_FLAG_GZIP), 0); - asked = TASK_FULLSCRAPE; - CHECK(mutex_workqueue_poptask(&asked) != 0); - CHECK_INT(asked, TASK_FULLSCRAPE_TPB_URLENCODED | TASK_FLAG_GZIP); - mutex_workqueue_canceltask(42); -} - -static void case_classes_do_not_cross(void) { - ot_tasktype asked; - ot_taskid taskid; - - test_case("a stats worker passes over a fullscrape task"); - CHECK_INT(mutex_workqueue_pushtask(43, TASK_FULLSCRAPE_TPB_BINARY), 0); - CHECK_INT(mutex_workqueue_pushtask(44, TASK_STATS_PEERS), 0); - - asked = TASK_STATS; - taskid = mutex_workqueue_poptask(&asked); - CHECK(taskid != 0); - CHECK_INT(asked, TASK_STATS_PEERS); - - test_case("and leaves it for the one that wants it"); - asked = TASK_FULLSCRAPE; - taskid = mutex_workqueue_poptask(&asked); - CHECK(taskid != 0); - CHECK_INT(asked, TASK_FULLSCRAPE_TPB_BINARY); - - mutex_workqueue_canceltask(43); - mutex_workqueue_canceltask(44); -} - -static void case_every_task_gets_its_own_id(void) { - ot_tasktype asked; - ot_taskid first, second; - - test_case("two queued tasks are handed out under different ids"); - CHECK_INT(mutex_workqueue_pushtask(45, TASK_FULLSCRAPE), 0); - CHECK_INT(mutex_workqueue_pushtask(46, TASK_FULLSCRAPE), 0); - - asked = TASK_FULLSCRAPE; - first = mutex_workqueue_poptask(&asked); - asked = TASK_FULLSCRAPE; - second = mutex_workqueue_poptask(&asked); - CHECK(first != 0); - CHECK(second != 0); - CHECK(first != second); - - mutex_workqueue_canceltask(45); - mutex_workqueue_canceltask(46); -} - -static void case_cancelled_task_is_not_handed_out(void) { - ot_tasktype asked; - - test_case("a cancelled task is gone before any worker sees it"); - CHECK_INT(mutex_workqueue_pushtask(47, TASK_FULLSCRAPE_TPB_ASCII_PLUS), 0); - CHECK_INT(mutex_workqueue_pushtask(48, TASK_FULLSCRAPE), 0); - mutex_workqueue_canceltask(47); - - asked = TASK_FULLSCRAPE; - CHECK(mutex_workqueue_poptask(&asked) != 0); - CHECK_INT(asked, TASK_FULLSCRAPE); - mutex_workqueue_canceltask(48); -} - -void test_mutex(void) { - case_task_goes_to_its_own_class(); - case_flags_survive_the_queue(); - case_classes_do_not_cross(); - case_every_task_gets_its_own_id(); - case_cancelled_task_is_not_handed_out(); -} diff --git a/tests/t_workqueue.c b/tests/t_workqueue.c new file mode 100644 index 0000000..13e0b2e --- /dev/null +++ b/tests/t_workqueue.c @@ -0,0 +1,107 @@ +/* This software was written by Dirk Engling + It is considered beerware. Prost. Skol. Cheers or whatever. + + $id$ */ + +/* System */ +#include +#include + +/* Opentracker */ +#include "ot_workqueue.h" +#include "test.h" + +/* A worker asks for a class of task and blocks until one turns up, so a task + whose class matches no worker is never claimed and whoever is waiting for + its answer waits forever. These cases pin the matching. */ + +static void case_task_goes_to_its_own_class(void) { + ot_tasktype asked; + ot_taskid taskid; + + test_case("a worker is handed the task of the class it asked for"); + CHECK_INT(workqueue_pushtask(41, TASK_FULLSCRAPE_TPB_ASCII), 0); + asked = TASK_FULLSCRAPE; + taskid = workqueue_poptask(&asked); + CHECK(taskid != 0); + + test_case("and the full task type, not just its class"); + CHECK_INT(asked, TASK_FULLSCRAPE_TPB_ASCII); + workqueue_canceltask(41); +} + +static void case_flags_survive_the_queue(void) { + ot_tasktype asked; + + test_case("flags outside the class mask are carried through"); + CHECK_INT(workqueue_pushtask(42, TASK_FULLSCRAPE_TPB_URLENCODED | TASK_FLAG_GZIP), 0); + asked = TASK_FULLSCRAPE; + CHECK(workqueue_poptask(&asked) != 0); + CHECK_INT(asked, TASK_FULLSCRAPE_TPB_URLENCODED | TASK_FLAG_GZIP); + workqueue_canceltask(42); +} + +static void case_classes_do_not_cross(void) { + ot_tasktype asked; + ot_taskid taskid; + + test_case("a stats worker passes over a fullscrape task"); + CHECK_INT(workqueue_pushtask(43, TASK_FULLSCRAPE_TPB_BINARY), 0); + CHECK_INT(workqueue_pushtask(44, TASK_STATS_PEERS), 0); + + asked = TASK_STATS; + taskid = workqueue_poptask(&asked); + CHECK(taskid != 0); + CHECK_INT(asked, TASK_STATS_PEERS); + + test_case("and leaves it for the one that wants it"); + asked = TASK_FULLSCRAPE; + taskid = workqueue_poptask(&asked); + CHECK(taskid != 0); + CHECK_INT(asked, TASK_FULLSCRAPE_TPB_BINARY); + + workqueue_canceltask(43); + workqueue_canceltask(44); +} + +static void case_every_task_gets_its_own_id(void) { + ot_tasktype asked; + ot_taskid first, second; + + test_case("two queued tasks are handed out under different ids"); + CHECK_INT(workqueue_pushtask(45, TASK_FULLSCRAPE), 0); + CHECK_INT(workqueue_pushtask(46, TASK_FULLSCRAPE), 0); + + asked = TASK_FULLSCRAPE; + first = workqueue_poptask(&asked); + asked = TASK_FULLSCRAPE; + second = workqueue_poptask(&asked); + CHECK(first != 0); + CHECK(second != 0); + CHECK(first != second); + + workqueue_canceltask(45); + workqueue_canceltask(46); +} + +static void case_cancelled_task_is_not_handed_out(void) { + ot_tasktype asked; + + test_case("a cancelled task is gone before any worker sees it"); + CHECK_INT(workqueue_pushtask(47, TASK_FULLSCRAPE_TPB_ASCII_PLUS), 0); + CHECK_INT(workqueue_pushtask(48, TASK_FULLSCRAPE), 0); + workqueue_canceltask(47); + + asked = TASK_FULLSCRAPE; + CHECK(workqueue_poptask(&asked) != 0); + CHECK_INT(asked, TASK_FULLSCRAPE); + workqueue_canceltask(48); +} + +void test_workqueue(void) { + case_task_goes_to_its_own_class(); + case_flags_survive_the_queue(); + case_classes_do_not_cross(); + case_every_task_gets_its_own_id(); + case_cancelled_task_is_not_handed_out(); +} diff --git a/tests/test.h b/tests/test.h index 0eddc82..d58742c 100644 --- a/tests/test.h +++ b/tests/test.h @@ -86,7 +86,7 @@ void test_scan(void); void test_buf(void); void test_ip(void); void test_socket(void); -void test_mutex(void); +void test_workqueue(void); void test_batch(void); void test_fdstate(void); void test_event(void); -- cgit v1.3