2 This file is part of GNUnet.
3 Copyright (C) 2009 GNUnet e.V.
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.
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.
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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
21 * @file util/test_scheduler.c
22 * @brief tests for the scheduler
25 #include "gnunet_util_lib.h"
28 static struct GNUNET_DISK_PipeHandle *p;
30 static const struct GNUNET_DISK_FileHandle *fds[2];
32 static struct GNUNET_SCHEDULER_Task *never_run_task;
40 /* t3 should be ready (albeit with lower priority) */
42 GNUNET_SCHEDULER_get_load (GNUNET_SCHEDULER_PRIORITY_COUNT));
43 GNUNET_assert (2 == *ok);
53 GNUNET_assert (3 == *ok);
63 const struct GNUNET_SCHEDULER_TaskContext *tc;
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]));
69 GNUNET_assert (1 == GNUNET_DISK_file_write (fds[1], &c, 1));
74 taskNeverRun (void *cls)
81 taskLastRd (void *cls)
85 GNUNET_assert (8 == *ok);
91 taskLastSig (void *cls)
95 GNUNET_SCHEDULER_cancel (never_run_task);
96 GNUNET_assert (9 == *ok);
102 taskLastShutdown (void *cls)
106 GNUNET_assert (10 == *ok);
116 const struct GNUNET_SCHEDULER_TaskContext *tc;
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));
123 GNUNET_SCHEDULER_add_shutdown (&taskLastRd,
125 GNUNET_SCHEDULER_shutdown ();
134 GNUNET_assert (4 == *ok);
136 p = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO, GNUNET_NO);
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,
144 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
156 GNUNET_assert (1 == *ok);
158 GNUNET_SCHEDULER_add_now (&task3, cls);
159 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_UI,
162 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
169 * Main method, starts scheduler with task1,
170 * checks that "ok" is correct at the end.
177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
178 "[Check scheduling]\n");
180 GNUNET_SCHEDULER_run (&task1, &ok);
186 taskShutdown (void *cls)
190 GNUNET_assert (1 == *ok);
192 GNUNET_SCHEDULER_add_shutdown (&taskLastShutdown, cls);
193 GNUNET_SCHEDULER_shutdown ();
198 * Main method, starts scheduler with task1,
199 * checks that "ok" is correct at the end.
206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
207 "[Check shutdown]\n");
209 GNUNET_SCHEDULER_run (&taskShutdown, &ok);
220 GNUNET_assert (1 == *ok);
222 GNUNET_SCHEDULER_add_shutdown (&taskLastSig, cls);
224 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
227 GNUNET_break (0 == PLIBC_KILL (getpid (),
233 * Main method, starts scheduler with task1,
234 * checks that "ok" is correct at the end.
241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
242 "[Check signal handling]\n");
244 GNUNET_SCHEDULER_run (&taskSig, &ok);
251 taskCancel (void *cls)
255 GNUNET_assert (1 == *ok);
257 GNUNET_SCHEDULER_cancel (GNUNET_SCHEDULER_add_now (&taskNeverRun, NULL));
262 * Main method, starts scheduler with task1,
263 * checks that "ok" is correct at the end.
270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
271 "[Check task cancellation]\n");
273 GNUNET_SCHEDULER_run (&taskCancel, &ok);
279 main (int argc, char *argv[])
283 GNUNET_log_setup ("test_scheduler", "WARNING", NULL);
285 ret += checkCancel ();
287 ret += checkSignal ();
289 ret += checkShutdown ();
290 GNUNET_DISK_pipe_close (p);
295 /* end of test_scheduler.c */