2 This file is part of GNUnet.
3 (C) 2009 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
21 * @file util/test_scheduler.c
22 * @brief tests for the scheduler
25 #include "gnunet_common.h"
26 #include "gnunet_scheduler_lib.h"
27 #include "gnunet_time_lib.h"
28 #include "gnunet_disk_lib.h"
30 #define VERBOSE GNUNET_NO
33 task2 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
36 GNUNET_assert (2 == *ok);
41 task3 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
44 /* t4 should be ready (albeit with lower priority) */
45 GNUNET_assert (1 == GNUNET_SCHEDULER_get_load (tc->sched,
46 GNUNET_SCHEDULER_PRIORITY_COUNT));
47 GNUNET_assert (3 == *ok);
52 task4 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
55 GNUNET_assert (4 == *ok);
59 struct GNUNET_DISK_PipeHandle *p;
60 static struct GNUNET_DISK_FileHandle *fds[2];
64 taskWrt (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
68 GNUNET_assert (6 == *ok);
69 GNUNET_assert (GNUNET_NETWORK_fdset_handle_isset (tc->write_ready, fds[1]));
71 GNUNET_assert (1 == GNUNET_DISK_file_write (fds[1], &c, 1));
76 taskNeverRun (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
82 taskLast (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
85 /* t4 should be ready (albeit with lower priority) */
86 GNUNET_assert (8 == *ok);
91 taskRd (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
95 GNUNET_assert (7 == *ok);
96 GNUNET_assert (GNUNET_NETWORK_fdset_handle_isset (tc->read_ready, fds[0]));
97 GNUNET_assert (1 == GNUNET_DISK_file_read (fds[0], &c, 1));
99 GNUNET_SCHEDULER_add_after (tc->sched,
101 GNUNET_SCHEDULER_PRIORITY_UI,
102 0, &taskNeverRun, NULL);
103 GNUNET_SCHEDULER_add_after (tc->sched,
105 GNUNET_SCHEDULER_PRIORITY_IDLE,
107 GNUNET_SCHEDULER_shutdown (tc->sched);
112 task5 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
115 GNUNET_assert (5 == *ok);
117 p = GNUNET_DISK_pipe (GNUNET_NO);
118 GNUNET_assert (NULL != p);
119 fds[0] = GNUNET_DISK_pipe_handle (p, 0);
120 fds[1] = GNUNET_DISK_pipe_handle (p, 1);
121 GNUNET_SCHEDULER_add_read_file (tc->sched,
123 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
124 GNUNET_SCHEDULER_NO_TASK,
125 GNUNET_TIME_UNIT_FOREVER_REL,
126 fds[0], &taskRd, cls);
127 GNUNET_SCHEDULER_add_write_file (tc->sched,
129 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
130 GNUNET_SCHEDULER_NO_TASK,
131 GNUNET_TIME_UNIT_FOREVER_REL,
132 fds[1], &taskWrt, cls);
137 task1 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
140 GNUNET_SCHEDULER_TaskIdentifier t2;
141 GNUNET_SCHEDULER_TaskIdentifier t3;
142 GNUNET_SCHEDULER_TaskIdentifier t4;
144 GNUNET_assert (1 == *ok);
146 /* t2 will go first -- prereq for all */
147 t2 = GNUNET_SCHEDULER_add_after (tc->sched,
149 GNUNET_SCHEDULER_PRIORITY_IDLE,
150 GNUNET_SCHEDULER_NO_TASK,
152 /* t3 will go before t4: higher priority */
153 t4 = GNUNET_SCHEDULER_add_after (tc->sched,
155 GNUNET_SCHEDULER_PRIORITY_IDLE,
157 t3 = GNUNET_SCHEDULER_add_delayed (tc->sched,
159 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
161 GNUNET_TIME_relative_get_zero (),
163 /* t4 will go first: lower prio, but prereq! */
164 GNUNET_SCHEDULER_add_after (tc->sched,
166 GNUNET_SCHEDULER_PRIORITY_UI, t4, &task5, cls);
172 * Main method, starts scheduler with task1,
173 * checks that "ok" is correct at the end.
181 GNUNET_SCHEDULER_run (&task1, &ok);
187 taskSig (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
190 GNUNET_assert (1 == *ok);
192 GNUNET_SCHEDULER_add_after (tc->sched,
194 GNUNET_SCHEDULER_PRIORITY_UI,
195 0, &taskNeverRun, NULL);
196 GNUNET_SCHEDULER_add_after (tc->sched,
198 GNUNET_SCHEDULER_PRIORITY_UI,
200 GNUNET_break (0 == PLIBC_KILL (getpid (), SIGTERM));
205 * Main method, starts scheduler with task1,
206 * checks that "ok" is correct at the end.
214 GNUNET_SCHEDULER_run (&taskSig, &ok);
222 taskCancel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
226 GNUNET_assert (1 == *ok);
228 GNUNET_SCHEDULER_cancel (tc->sched,
229 GNUNET_SCHEDULER_add_after (tc->sched,
231 GNUNET_SCHEDULER_PRIORITY_UI,
233 &taskNeverRun, NULL));
238 * Main method, starts scheduler with task1,
239 * checks that "ok" is correct at the end.
247 GNUNET_SCHEDULER_run (&taskCancel, &ok);
254 main (int argc, char *argv[])
258 GNUNET_log_setup ("test_scheduler", "WARNING", NULL);
260 ret += checkSignal ();
261 ret += checkCancel ();
262 GNUNET_DISK_pipe_close (p);
267 /* end of test_scheduler.c */