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/>.
19 * @file util/test_scheduler.c
20 * @brief tests for the scheduler
23 #include "gnunet_util_lib.h"
26 static struct GNUNET_DISK_PipeHandle *p;
28 static const struct GNUNET_DISK_FileHandle *fds[2];
30 static struct GNUNET_SCHEDULER_Task *never_run_task;
38 /* t3 should be ready (albeit with lower priority) */
40 GNUNET_SCHEDULER_get_load (GNUNET_SCHEDULER_PRIORITY_COUNT));
41 GNUNET_assert (2 == *ok);
51 GNUNET_assert (3 == *ok);
61 const struct GNUNET_SCHEDULER_TaskContext *tc;
63 tc = GNUNET_SCHEDULER_get_task_context ();
64 GNUNET_assert (6 == *ok);
65 GNUNET_assert (GNUNET_NETWORK_fdset_handle_isset (tc->write_ready, fds[1]));
67 GNUNET_assert (1 == GNUNET_DISK_file_write (fds[1], &c, 1));
72 taskNeverRun (void *cls)
79 taskLastRd (void *cls)
83 GNUNET_assert (8 == *ok);
89 taskLastSig (void *cls)
93 GNUNET_SCHEDULER_cancel (never_run_task);
94 GNUNET_assert (9 == *ok);
100 taskLastShutdown (void *cls)
104 GNUNET_assert (10 == *ok);
114 const struct GNUNET_SCHEDULER_TaskContext *tc;
116 tc = GNUNET_SCHEDULER_get_task_context ();
117 GNUNET_assert (7 == *ok);
118 GNUNET_assert (GNUNET_NETWORK_fdset_handle_isset (tc->read_ready, fds[0]));
119 GNUNET_assert (1 == GNUNET_DISK_file_read (fds[0], &c, 1));
121 GNUNET_SCHEDULER_add_shutdown (&taskLastRd,
123 GNUNET_SCHEDULER_shutdown ();
132 GNUNET_assert (4 == *ok);
134 p = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO, GNUNET_NO);
135 GNUNET_assert (NULL != p);
136 fds[0] = GNUNET_DISK_pipe_handle (p, GNUNET_DISK_PIPE_END_READ);
137 fds[1] = GNUNET_DISK_pipe_handle (p, GNUNET_DISK_PIPE_END_WRITE);
138 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
142 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
154 GNUNET_assert (1 == *ok);
156 GNUNET_SCHEDULER_add_now (&task3, cls);
157 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_UI,
160 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
167 * Main method, starts scheduler with task1,
168 * checks that "ok" is correct at the end.
175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
176 "[Check scheduling]\n");
178 GNUNET_SCHEDULER_run (&task1, &ok);
184 taskShutdown (void *cls)
188 GNUNET_assert (1 == *ok);
190 GNUNET_SCHEDULER_add_shutdown (&taskLastShutdown, cls);
191 GNUNET_SCHEDULER_shutdown ();
196 * Main method, starts scheduler with task1,
197 * checks that "ok" is correct at the end.
204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
205 "[Check shutdown]\n");
207 GNUNET_SCHEDULER_run (&taskShutdown, &ok);
218 GNUNET_assert (1 == *ok);
220 GNUNET_SCHEDULER_add_shutdown (&taskLastSig, cls);
222 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
225 GNUNET_break (0 == PLIBC_KILL (getpid (),
231 * Main method, starts scheduler with task1,
232 * checks that "ok" is correct at the end.
239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
240 "[Check signal handling]\n");
242 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 ();
285 ret += checkSignal ();
287 ret += checkShutdown ();
288 GNUNET_DISK_pipe_close (p);
293 /* end of test_scheduler.c */