diff options
Diffstat (limited to 'src/lib/util/test_scheduler.c')
-rw-r--r-- | src/lib/util/test_scheduler.c | 294 |
1 files changed, 294 insertions, 0 deletions
diff --git a/src/lib/util/test_scheduler.c b/src/lib/util/test_scheduler.c new file mode 100644 index 000000000..4573518fd --- /dev/null +++ b/src/lib/util/test_scheduler.c | |||
@@ -0,0 +1,294 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2009 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | |||
18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | */ | ||
20 | /** | ||
21 | * @file util/test_scheduler.c | ||
22 | * @brief tests for the scheduler | ||
23 | */ | ||
24 | |||
25 | #include "platform.h" | ||
26 | #include "gnunet_util_lib.h" | ||
27 | |||
28 | |||
29 | static struct GNUNET_DISK_PipeHandle *p; | ||
30 | |||
31 | static const struct GNUNET_DISK_FileHandle *fds[2]; | ||
32 | |||
33 | static struct GNUNET_SCHEDULER_Task *never_run_task; | ||
34 | |||
35 | |||
36 | static void | ||
37 | task2 (void *cls) | ||
38 | { | ||
39 | int *ok = cls; | ||
40 | |||
41 | /* t3 should be ready (albeit with lower priority) */ | ||
42 | GNUNET_assert (1 == | ||
43 | GNUNET_SCHEDULER_get_load (GNUNET_SCHEDULER_PRIORITY_COUNT)); | ||
44 | GNUNET_assert (2 == *ok); | ||
45 | (*ok) = 3; | ||
46 | } | ||
47 | |||
48 | |||
49 | static void | ||
50 | task3 (void *cls) | ||
51 | { | ||
52 | int *ok = cls; | ||
53 | |||
54 | GNUNET_assert (3 == *ok); | ||
55 | (*ok) = 4; | ||
56 | } | ||
57 | |||
58 | |||
59 | static void | ||
60 | taskWrt (void *cls) | ||
61 | { | ||
62 | static char c; | ||
63 | int *ok = cls; | ||
64 | const struct GNUNET_SCHEDULER_TaskContext *tc; | ||
65 | |||
66 | tc = GNUNET_SCHEDULER_get_task_context (); | ||
67 | GNUNET_assert (6 == *ok); | ||
68 | GNUNET_assert (GNUNET_NETWORK_fdset_handle_isset (tc->write_ready, fds[1])); | ||
69 | (*ok) = 7; | ||
70 | GNUNET_assert (1 == GNUNET_DISK_file_write (fds[1], &c, 1)); | ||
71 | } | ||
72 | |||
73 | |||
74 | static void | ||
75 | taskNeverRun (void *cls) | ||
76 | { | ||
77 | GNUNET_assert (0); | ||
78 | } | ||
79 | |||
80 | |||
81 | static void | ||
82 | taskLastRd (void *cls) | ||
83 | { | ||
84 | int *ok = cls; | ||
85 | |||
86 | GNUNET_assert (8 == *ok); | ||
87 | (*ok) = 0; | ||
88 | } | ||
89 | |||
90 | |||
91 | static void | ||
92 | taskLastSig (void *cls) | ||
93 | { | ||
94 | int *ok = cls; | ||
95 | |||
96 | GNUNET_SCHEDULER_cancel (never_run_task); | ||
97 | GNUNET_assert (9 == *ok); | ||
98 | (*ok) = 0; | ||
99 | } | ||
100 | |||
101 | |||
102 | static void | ||
103 | taskLastShutdown (void *cls) | ||
104 | { | ||
105 | int *ok = cls; | ||
106 | |||
107 | GNUNET_assert (10 == *ok); | ||
108 | (*ok) = 0; | ||
109 | } | ||
110 | |||
111 | |||
112 | static void | ||
113 | taskRd (void *cls) | ||
114 | { | ||
115 | static char c; | ||
116 | int *ok = cls; | ||
117 | const struct GNUNET_SCHEDULER_TaskContext *tc; | ||
118 | |||
119 | tc = GNUNET_SCHEDULER_get_task_context (); | ||
120 | GNUNET_assert (7 == *ok); | ||
121 | GNUNET_assert (GNUNET_NETWORK_fdset_handle_isset (tc->read_ready, fds[0])); | ||
122 | GNUNET_assert (1 == GNUNET_DISK_file_read (fds[0], &c, 1)); | ||
123 | (*ok) = 8; | ||
124 | GNUNET_SCHEDULER_add_shutdown (&taskLastRd, | ||
125 | cls); | ||
126 | GNUNET_SCHEDULER_shutdown (); | ||
127 | } | ||
128 | |||
129 | |||
130 | static void | ||
131 | task4 (void *cls) | ||
132 | { | ||
133 | int *ok = cls; | ||
134 | |||
135 | GNUNET_assert (4 == *ok); | ||
136 | (*ok) = 6; | ||
137 | p = GNUNET_DISK_pipe (GNUNET_DISK_PF_NONE); | ||
138 | GNUNET_assert (NULL != p); | ||
139 | fds[0] = GNUNET_DISK_pipe_handle (p, GNUNET_DISK_PIPE_END_READ); | ||
140 | fds[1] = GNUNET_DISK_pipe_handle (p, GNUNET_DISK_PIPE_END_WRITE); | ||
141 | GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, | ||
142 | fds[0], | ||
143 | &taskRd, | ||
144 | cls); | ||
145 | GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL, | ||
146 | fds[1], | ||
147 | &taskWrt, | ||
148 | cls); | ||
149 | } | ||
150 | |||
151 | |||
152 | static void | ||
153 | task1 (void *cls) | ||
154 | { | ||
155 | int *ok = cls; | ||
156 | |||
157 | GNUNET_assert (1 == *ok); | ||
158 | (*ok) = 2; | ||
159 | GNUNET_SCHEDULER_add_now (&task3, cls); | ||
160 | GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_UI, | ||
161 | &task2, | ||
162 | cls); | ||
163 | GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, | ||
164 | &task4, | ||
165 | cls); | ||
166 | } | ||
167 | |||
168 | |||
169 | /** | ||
170 | * Main method, starts scheduler with task1, | ||
171 | * checks that "ok" is correct at the end. | ||
172 | */ | ||
173 | static int | ||
174 | check () | ||
175 | { | ||
176 | int ok; | ||
177 | |||
178 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
179 | "[Check scheduling]\n"); | ||
180 | ok = 1; | ||
181 | GNUNET_SCHEDULER_run (&task1, &ok); | ||
182 | return ok; | ||
183 | } | ||
184 | |||
185 | |||
186 | static void | ||
187 | taskShutdown (void *cls) | ||
188 | { | ||
189 | int *ok = cls; | ||
190 | |||
191 | GNUNET_assert (1 == *ok); | ||
192 | *ok = 10; | ||
193 | GNUNET_SCHEDULER_add_shutdown (&taskLastShutdown, cls); | ||
194 | GNUNET_SCHEDULER_shutdown (); | ||
195 | } | ||
196 | |||
197 | |||
198 | /** | ||
199 | * Main method, starts scheduler with task1, | ||
200 | * checks that "ok" is correct at the end. | ||
201 | */ | ||
202 | static int | ||
203 | checkShutdown () | ||
204 | { | ||
205 | int ok; | ||
206 | |||
207 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
208 | "[Check shutdown]\n"); | ||
209 | ok = 1; | ||
210 | GNUNET_SCHEDULER_run (&taskShutdown, &ok); | ||
211 | return ok; | ||
212 | } | ||
213 | |||
214 | |||
215 | static void | ||
216 | taskSig (void *cls) | ||
217 | { | ||
218 | int *ok = cls; | ||
219 | |||
220 | GNUNET_assert (1 == *ok); | ||
221 | *ok = 9; | ||
222 | GNUNET_SCHEDULER_add_shutdown (&taskLastSig, cls); | ||
223 | never_run_task = | ||
224 | GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply ( | ||
225 | GNUNET_TIME_UNIT_SECONDS, 5), | ||
226 | &taskNeverRun, | ||
227 | NULL); | ||
228 | GNUNET_break (0 == kill (getpid (), | ||
229 | GNUNET_TERM_SIG)); | ||
230 | } | ||
231 | |||
232 | |||
233 | /** | ||
234 | * Main method, starts scheduler with task1, | ||
235 | * checks that "ok" is correct at the end. | ||
236 | */ | ||
237 | static int | ||
238 | checkSignal (void) | ||
239 | { | ||
240 | int ok; | ||
241 | |||
242 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
243 | "[Check signal handling]\n"); | ||
244 | ok = 1; | ||
245 | GNUNET_SCHEDULER_run (&taskSig, &ok); | ||
246 | return ok; | ||
247 | } | ||
248 | |||
249 | |||
250 | static void | ||
251 | taskCancel (void *cls) | ||
252 | { | ||
253 | int *ok = cls; | ||
254 | |||
255 | GNUNET_assert (1 == *ok); | ||
256 | *ok = 0; | ||
257 | GNUNET_SCHEDULER_cancel (GNUNET_SCHEDULER_add_now (&taskNeverRun, NULL)); | ||
258 | } | ||
259 | |||
260 | |||
261 | /** | ||
262 | * Main method, starts scheduler with task1, | ||
263 | * checks that "ok" is correct at the end. | ||
264 | */ | ||
265 | static int | ||
266 | checkCancel () | ||
267 | { | ||
268 | int ok; | ||
269 | |||
270 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
271 | "[Check task cancellation]\n"); | ||
272 | ok = 1; | ||
273 | GNUNET_SCHEDULER_run (&taskCancel, &ok); | ||
274 | return ok; | ||
275 | } | ||
276 | |||
277 | |||
278 | int | ||
279 | main (int argc, char *argv[]) | ||
280 | { | ||
281 | int ret = 0; | ||
282 | |||
283 | GNUNET_log_setup ("test_scheduler", "WARNING", NULL); | ||
284 | ret += check (); | ||
285 | ret += checkCancel (); | ||
286 | ret += checkSignal (); | ||
287 | ret += checkShutdown (); | ||
288 | GNUNET_DISK_pipe_close (p); | ||
289 | |||
290 | return ret; | ||
291 | } | ||
292 | |||
293 | |||
294 | /* end of test_scheduler.c */ | ||