diff options
| author | Dirk Engling <erdgeist@erdgeist.org> | 2026-10-31 18:48:00 +0100 |
|---|---|---|
| committer | Dirk Engling <erdgeist@erdgeist.org> | 2026-10-31 18:48:00 +0100 |
| commit | 5b25f89d00433f1c11576d1ea19304797dc60d1b (patch) | |
| tree | 3810c8585354f4795bd2f441b64f70e581855214 /tests/t_workqueue.c | |
| parent | 0bd368691eb0d9051725d76b2a3721bb902c919b (diff) | |
Refactor ot_workqueue out of ot_mutex, no behaviour change
Diffstat (limited to 'tests/t_workqueue.c')
| -rw-r--r-- | tests/t_workqueue.c | 107 |
1 files changed, 107 insertions, 0 deletions
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 @@ | |||
| 1 | /* This software was written by Dirk Engling <erdgeist@erdgeist.org> | ||
| 2 | It is considered beerware. Prost. Skol. Cheers or whatever. | ||
| 3 | |||
| 4 | $id$ */ | ||
| 5 | |||
| 6 | /* System */ | ||
| 7 | #include <stdlib.h> | ||
| 8 | #include <string.h> | ||
| 9 | |||
| 10 | /* Opentracker */ | ||
| 11 | #include "ot_workqueue.h" | ||
| 12 | #include "test.h" | ||
| 13 | |||
| 14 | /* A worker asks for a class of task and blocks until one turns up, so a task | ||
| 15 | whose class matches no worker is never claimed and whoever is waiting for | ||
| 16 | its answer waits forever. These cases pin the matching. */ | ||
| 17 | |||
| 18 | static void case_task_goes_to_its_own_class(void) { | ||
| 19 | ot_tasktype asked; | ||
| 20 | ot_taskid taskid; | ||
| 21 | |||
| 22 | test_case("a worker is handed the task of the class it asked for"); | ||
| 23 | CHECK_INT(workqueue_pushtask(41, TASK_FULLSCRAPE_TPB_ASCII), 0); | ||
| 24 | asked = TASK_FULLSCRAPE; | ||
| 25 | taskid = workqueue_poptask(&asked); | ||
| 26 | CHECK(taskid != 0); | ||
| 27 | |||
| 28 | test_case("and the full task type, not just its class"); | ||
| 29 | CHECK_INT(asked, TASK_FULLSCRAPE_TPB_ASCII); | ||
| 30 | workqueue_canceltask(41); | ||
| 31 | } | ||
| 32 | |||
| 33 | static void case_flags_survive_the_queue(void) { | ||
| 34 | ot_tasktype asked; | ||
| 35 | |||
| 36 | test_case("flags outside the class mask are carried through"); | ||
| 37 | CHECK_INT(workqueue_pushtask(42, TASK_FULLSCRAPE_TPB_URLENCODED | TASK_FLAG_GZIP), 0); | ||
| 38 | asked = TASK_FULLSCRAPE; | ||
| 39 | CHECK(workqueue_poptask(&asked) != 0); | ||
| 40 | CHECK_INT(asked, TASK_FULLSCRAPE_TPB_URLENCODED | TASK_FLAG_GZIP); | ||
| 41 | workqueue_canceltask(42); | ||
| 42 | } | ||
| 43 | |||
| 44 | static void case_classes_do_not_cross(void) { | ||
| 45 | ot_tasktype asked; | ||
| 46 | ot_taskid taskid; | ||
| 47 | |||
| 48 | test_case("a stats worker passes over a fullscrape task"); | ||
| 49 | CHECK_INT(workqueue_pushtask(43, TASK_FULLSCRAPE_TPB_BINARY), 0); | ||
| 50 | CHECK_INT(workqueue_pushtask(44, TASK_STATS_PEERS), 0); | ||
| 51 | |||
| 52 | asked = TASK_STATS; | ||
| 53 | taskid = workqueue_poptask(&asked); | ||
| 54 | CHECK(taskid != 0); | ||
| 55 | CHECK_INT(asked, TASK_STATS_PEERS); | ||
| 56 | |||
| 57 | test_case("and leaves it for the one that wants it"); | ||
| 58 | asked = TASK_FULLSCRAPE; | ||
| 59 | taskid = workqueue_poptask(&asked); | ||
| 60 | CHECK(taskid != 0); | ||
| 61 | CHECK_INT(asked, TASK_FULLSCRAPE_TPB_BINARY); | ||
| 62 | |||
| 63 | workqueue_canceltask(43); | ||
| 64 | workqueue_canceltask(44); | ||
| 65 | } | ||
| 66 | |||
| 67 | static void case_every_task_gets_its_own_id(void) { | ||
| 68 | ot_tasktype asked; | ||
| 69 | ot_taskid first, second; | ||
| 70 | |||
| 71 | test_case("two queued tasks are handed out under different ids"); | ||
| 72 | CHECK_INT(workqueue_pushtask(45, TASK_FULLSCRAPE), 0); | ||
| 73 | CHECK_INT(workqueue_pushtask(46, TASK_FULLSCRAPE), 0); | ||
| 74 | |||
| 75 | asked = TASK_FULLSCRAPE; | ||
| 76 | first = workqueue_poptask(&asked); | ||
| 77 | asked = TASK_FULLSCRAPE; | ||
| 78 | second = workqueue_poptask(&asked); | ||
| 79 | CHECK(first != 0); | ||
| 80 | CHECK(second != 0); | ||
| 81 | CHECK(first != second); | ||
| 82 | |||
| 83 | workqueue_canceltask(45); | ||
| 84 | workqueue_canceltask(46); | ||
| 85 | } | ||
| 86 | |||
| 87 | static void case_cancelled_task_is_not_handed_out(void) { | ||
| 88 | ot_tasktype asked; | ||
| 89 | |||
| 90 | test_case("a cancelled task is gone before any worker sees it"); | ||
| 91 | CHECK_INT(workqueue_pushtask(47, TASK_FULLSCRAPE_TPB_ASCII_PLUS), 0); | ||
| 92 | CHECK_INT(workqueue_pushtask(48, TASK_FULLSCRAPE), 0); | ||
| 93 | workqueue_canceltask(47); | ||
| 94 | |||
| 95 | asked = TASK_FULLSCRAPE; | ||
| 96 | CHECK(workqueue_poptask(&asked) != 0); | ||
| 97 | CHECK_INT(asked, TASK_FULLSCRAPE); | ||
| 98 | workqueue_canceltask(48); | ||
| 99 | } | ||
| 100 | |||
| 101 | void test_workqueue(void) { | ||
| 102 | case_task_goes_to_its_own_class(); | ||
| 103 | case_flags_survive_the_queue(); | ||
| 104 | case_classes_do_not_cross(); | ||
| 105 | case_every_task_gets_its_own_id(); | ||
| 106 | case_cancelled_task_is_not_handed_out(); | ||
| 107 | } | ||
