summaryrefslogtreecommitdiff
path: root/tests/t_workqueue.c
diff options
context:
space:
mode:
Diffstat (limited to 'tests/t_workqueue.c')
-rw-r--r--tests/t_workqueue.c157
1 files changed, 157 insertions, 0 deletions
diff --git a/tests/t_workqueue.c b/tests/t_workqueue.c
new file mode 100644
index 0000000..660d882
--- /dev/null
+++ b/tests/t_workqueue.c
@@ -0,0 +1,157 @@
1/* This software was written by Dirk Engling <erdgeist@erdgeist.org>
2 It is considered beerware. Prost. Skol. Cheers or whatever. */
3
4/* System */
5#include <stdlib.h>
6#include <string.h>
7
8/* Opentracker */
9#include "ot_fdstate.h"
10#include "ot_workqueue.h"
11#include "test.h"
12
13/* A worker asks for a class of task and blocks until one turns up, so a task
14 whose class matches no worker is never claimed and whoever is waiting for
15 its answer waits forever. These cases pin the matching. */
16
17static void case_task_goes_to_its_own_class(void) {
18 ot_tasktype asked;
19 ot_taskid taskid;
20
21 test_case("a worker is handed the task of the class it asked for");
22 CHECK_INT(workqueue_pushtask(41, TASK_FULLSCRAPE_TPB_ASCII), 0);
23 asked = TASK_FULLSCRAPE;
24 taskid = workqueue_poptask(&asked);
25 CHECK(taskid != 0);
26
27 test_case("and the full task type, not just its class");
28 CHECK_INT(asked, TASK_FULLSCRAPE_TPB_ASCII);
29 workqueue_canceltask(41);
30}
31
32static void case_flags_survive_the_queue(void) {
33 ot_tasktype asked;
34
35 test_case("flags outside the class mask are carried through");
36 CHECK_INT(workqueue_pushtask(42, TASK_FULLSCRAPE_TPB_URLENCODED | TASK_FLAG_GZIP), 0);
37 asked = TASK_FULLSCRAPE;
38 CHECK(workqueue_poptask(&asked) != 0);
39 CHECK_INT(asked, TASK_FULLSCRAPE_TPB_URLENCODED | TASK_FLAG_GZIP);
40 workqueue_canceltask(42);
41}
42
43static void case_classes_do_not_cross(void) {
44 ot_tasktype asked;
45 ot_taskid taskid;
46
47 test_case("a stats worker passes over a fullscrape task");
48 CHECK_INT(workqueue_pushtask(43, TASK_FULLSCRAPE_TPB_BINARY), 0);
49 CHECK_INT(workqueue_pushtask(44, TASK_STATS_PEERS), 0);
50
51 asked = TASK_STATS;
52 taskid = workqueue_poptask(&asked);
53 CHECK(taskid != 0);
54 CHECK_INT(asked, TASK_STATS_PEERS);
55
56 test_case("and leaves it for the one that wants it");
57 asked = TASK_FULLSCRAPE;
58 taskid = workqueue_poptask(&asked);
59 CHECK(taskid != 0);
60 CHECK_INT(asked, TASK_FULLSCRAPE_TPB_BINARY);
61
62 workqueue_canceltask(43);
63 workqueue_canceltask(44);
64}
65
66static void case_every_task_gets_its_own_id(void) {
67 ot_tasktype asked;
68 ot_taskid first, second;
69
70 test_case("two queued tasks are handed out under different ids");
71 CHECK_INT(workqueue_pushtask(45, TASK_FULLSCRAPE), 0);
72 CHECK_INT(workqueue_pushtask(46, TASK_FULLSCRAPE), 0);
73
74 asked = TASK_FULLSCRAPE;
75 first = workqueue_poptask(&asked);
76 asked = TASK_FULLSCRAPE;
77 second = workqueue_poptask(&asked);
78 CHECK(first != 0);
79 CHECK(second != 0);
80 CHECK(first != second);
81
82 workqueue_canceltask(45);
83 workqueue_canceltask(46);
84}
85
86static void case_cancelled_task_is_not_handed_out(void) {
87 ot_tasktype asked;
88
89 test_case("a cancelled task is gone before any worker sees it");
90 CHECK_INT(workqueue_pushtask(47, TASK_FULLSCRAPE_TPB_ASCII_PLUS), 0);
91 CHECK_INT(workqueue_pushtask(48, TASK_FULLSCRAPE), 0);
92 workqueue_canceltask(47);
93
94 asked = TASK_FULLSCRAPE;
95 CHECK(workqueue_poptask(&asked) != 0);
96 CHECK_INT(asked, TASK_FULLSCRAPE);
97 workqueue_canceltask(48);
98}
99
100/* Tasks are filed under a descriptor number, and the number is handed to the
101 next connection the moment this one closes. */
102
103static void case_cancel_takes_every_task_of_the_socket(void) {
104 ot_tasktype asked;
105
106 test_case("cancelling a socket leaves none of its tasks behind");
107 CHECK_INT(workqueue_pushtask(49, TASK_FULLSCRAPE_TPB_ASCII), 0);
108 CHECK_INT(workqueue_pushtask(49, TASK_FULLSCRAPE_TPB_BINARY), 0);
109 CHECK_INT(workqueue_pushtask(50, TASK_FULLSCRAPE), 0);
110 workqueue_canceltask(49);
111
112 asked = TASK_FULLSCRAPE;
113 CHECK(workqueue_poptask(&asked) != 0);
114 CHECK_INT(asked, TASK_FULLSCRAPE);
115 workqueue_canceltask(50);
116}
117
118static void case_result_names_the_connection_that_asked(void) {
119 ot_tasktype asked = TASK_STATS;
120 ot_taskid taskid;
121 struct iovec *iovector;
122 size_t iovec_entries;
123 int is_partial;
124 uint32_t asked_under, answered_under = ~0u;
125
126 CHECK_INT(fdstate_init(), 0);
127 asked_under = fdstate_open(51, NULL, 0);
128
129 test_case("a result comes back with the generation its task was pushed under");
130 CHECK_INT(workqueue_pushtask(51, TASK_STATS_PEERS), 0);
131 taskid = workqueue_poptask(&asked);
132 CHECK(taskid != 0);
133
134 /* The connection goes away without cancelling and the number is reused */
135 fdstate_close(51);
136 fdstate_open(51, NULL, 0);
137
138 CHECK_INT(workqueue_pushresult(taskid, 0, NULL), 0);
139 CHECK_INT(workqueue_popresult(&iovec_entries, &iovector, &is_partial, &answered_under), 51);
140 CHECK_INT(answered_under, asked_under);
141
142 test_case("which is no longer the one the descriptor carries");
143 CHECK(fdstate_generation(51) != answered_under);
144
145 fdstate_close(51);
146 fdstate_deinit();
147}
148
149void test_workqueue(void) {
150 case_task_goes_to_its_own_class();
151 case_flags_survive_the_queue();
152 case_classes_do_not_cross();
153 case_every_task_gets_its_own_id();
154 case_cancelled_task_is_not_handed_out();
155 case_cancel_takes_every_task_of_the_socket();
156 case_result_names_the_connection_that_asked();
157}