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