aboutsummaryrefslogtreecommitdiff
path: root/src/util/test_scheduler.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/test_scheduler.c')
-rw-r--r--src/util/test_scheduler.c293
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
28static struct GNUNET_DISK_PipeHandle *p;
29
30static const struct GNUNET_DISK_FileHandle *fds[2];
31
32static struct GNUNET_SCHEDULER_Task *never_run_task;
33
34
35static void
36task2 (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
48static void
49task3 (void *cls)
50{
51 int *ok = cls;
52
53 GNUNET_assert (3 == *ok);
54 (*ok) = 4;
55}
56
57
58static void
59taskWrt (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
73static void
74taskNeverRun (void *cls)
75{
76 GNUNET_assert (0);
77}
78
79
80static void
81taskLastRd (void *cls)
82{
83 int *ok = cls;
84
85 GNUNET_assert (8 == *ok);
86 (*ok) = 0;
87}
88
89
90static void
91taskLastSig (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
101static void
102taskLastShutdown (void *cls)
103{
104 int *ok = cls;
105
106 GNUNET_assert (10 == *ok);
107 (*ok) = 0;
108}
109
110
111static void
112taskRd (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
129static void
130task4 (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
151static void
152task1 (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 */
172static int
173check ()
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
185static void
186taskShutdown (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 */
201static int
202checkShutdown ()
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
214static void
215taskSig (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 */
236static int
237checkSignal (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
249static void
250taskCancel (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 */
264static int
265checkCancel ()
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
277int
278main (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 */