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)
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);
219 GNUNET_assert(1 == *ok);
221 GNUNET_SCHEDULER_add_shutdown(&taskLastSig, cls);
223 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5),
226 GNUNET_break(0 == kill(getpid(),
232 * Main method, starts scheduler with task1,
233 * checks that "ok" is correct at the end.
240 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
241 "[Check signal handling]\n");
243 GNUNET_SCHEDULER_run(&taskSig, &ok);
249 taskCancel(void *cls)
253 GNUNET_assert(1 == *ok);
255 GNUNET_SCHEDULER_cancel(GNUNET_SCHEDULER_add_now(&taskNeverRun, NULL));
260 * Main method, starts scheduler with task1,
261 * checks that "ok" is correct at the end.
268 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
269 "[Check task cancellation]\n");
271 GNUNET_SCHEDULER_run(&taskCancel, &ok);
277 main(int argc, char *argv[])
281 GNUNET_log_setup("test_scheduler", "WARNING", NULL);
283 ret += checkCancel();
284 ret += checkSignal();
285 ret += checkShutdown();
286 GNUNET_DISK_pipe_close(p);
291 /* end of test_scheduler.c */