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 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.
16 * @file util/test_scheduler.c
17 * @brief tests for the scheduler
20 #include "gnunet_util_lib.h"
23 static struct GNUNET_DISK_PipeHandle *p;
25 static const struct GNUNET_DISK_FileHandle *fds[2];
27 static struct GNUNET_SCHEDULER_Task *never_run_task;
35 /* t3 should be ready (albeit with lower priority) */
37 GNUNET_SCHEDULER_get_load (GNUNET_SCHEDULER_PRIORITY_COUNT));
38 GNUNET_assert (2 == *ok);
48 GNUNET_assert (3 == *ok);
58 const struct GNUNET_SCHEDULER_TaskContext *tc;
60 tc = GNUNET_SCHEDULER_get_task_context ();
61 GNUNET_assert (6 == *ok);
62 GNUNET_assert (GNUNET_NETWORK_fdset_handle_isset (tc->write_ready, fds[1]));
64 GNUNET_assert (1 == GNUNET_DISK_file_write (fds[1], &c, 1));
69 taskNeverRun (void *cls)
76 taskLastRd (void *cls)
80 GNUNET_assert (8 == *ok);
86 taskLastSig (void *cls)
90 GNUNET_SCHEDULER_cancel (never_run_task);
91 GNUNET_assert (9 == *ok);
97 taskLastShutdown (void *cls)
101 GNUNET_assert (10 == *ok);
111 const struct GNUNET_SCHEDULER_TaskContext *tc;
113 tc = GNUNET_SCHEDULER_get_task_context ();
114 GNUNET_assert (7 == *ok);
115 GNUNET_assert (GNUNET_NETWORK_fdset_handle_isset (tc->read_ready, fds[0]));
116 GNUNET_assert (1 == GNUNET_DISK_file_read (fds[0], &c, 1));
118 GNUNET_SCHEDULER_add_shutdown (&taskLastRd,
120 GNUNET_SCHEDULER_shutdown ();
129 GNUNET_assert (4 == *ok);
131 p = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO, GNUNET_NO);
132 GNUNET_assert (NULL != p);
133 fds[0] = GNUNET_DISK_pipe_handle (p, GNUNET_DISK_PIPE_END_READ);
134 fds[1] = GNUNET_DISK_pipe_handle (p, GNUNET_DISK_PIPE_END_WRITE);
135 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
139 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
151 GNUNET_assert (1 == *ok);
153 GNUNET_SCHEDULER_add_now (&task3, cls);
154 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_UI,
157 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
164 * Main method, starts scheduler with task1,
165 * checks that "ok" is correct at the end.
172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
173 "[Check scheduling]\n");
175 GNUNET_SCHEDULER_run (&task1, &ok);
181 taskShutdown (void *cls)
185 GNUNET_assert (1 == *ok);
187 GNUNET_SCHEDULER_add_shutdown (&taskLastShutdown, cls);
188 GNUNET_SCHEDULER_shutdown ();
193 * Main method, starts scheduler with task1,
194 * checks that "ok" is correct at the end.
201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
202 "[Check shutdown]\n");
204 GNUNET_SCHEDULER_run (&taskShutdown, &ok);
215 GNUNET_assert (1 == *ok);
217 GNUNET_SCHEDULER_add_shutdown (&taskLastSig, cls);
219 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
222 GNUNET_break (0 == PLIBC_KILL (getpid (),
228 * Main method, starts scheduler with task1,
229 * checks that "ok" is correct at the end.
236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
237 "[Check signal handling]\n");
239 GNUNET_SCHEDULER_run (&taskSig, &ok);
246 taskCancel (void *cls)
250 GNUNET_assert (1 == *ok);
252 GNUNET_SCHEDULER_cancel (GNUNET_SCHEDULER_add_now (&taskNeverRun, NULL));
257 * Main method, starts scheduler with task1,
258 * checks that "ok" is correct at the end.
265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
266 "[Check task cancellation]\n");
268 GNUNET_SCHEDULER_run (&taskCancel, &ok);
274 main (int argc, char *argv[])
278 GNUNET_log_setup ("test_scheduler", "WARNING", NULL);
280 ret += checkCancel ();
282 ret += checkSignal ();
284 ret += checkShutdown ();
285 GNUNET_DISK_pipe_close (p);
290 /* end of test_scheduler.c */