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"
29 #define VERBOSE GNUNET_NO
32 task2 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
35 GNUNET_assert (2 == *ok);
40 task3 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
43 /* t4 should be ready (albeit with lower priority) */
44 GNUNET_assert (1 == GNUNET_SCHEDULER_get_load (tc->sched,
45 GNUNET_SCHEDULER_PRIORITY_COUNT));
46 GNUNET_assert (3 == *ok);
51 task4 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
54 GNUNET_assert (4 == *ok);
62 taskWrt (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
66 GNUNET_assert (6 == *ok);
67 GNUNET_assert (FD_ISSET (fds[1], tc->write_ready));
69 GNUNET_assert (1 == WRITE (fds[1], &c, 1));
70 GNUNET_break (0 == CLOSE (fds[1]));
75 taskNeverRun (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
81 taskLast (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
84 /* t4 should be ready (albeit with lower priority) */
85 GNUNET_assert (8 == *ok);
90 taskRd (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
94 GNUNET_assert (7 == *ok);
95 GNUNET_assert (FD_ISSET (fds[0], tc->read_ready));
96 GNUNET_assert (1 == READ (fds[0], &c, 1));
97 GNUNET_break (0 == CLOSE (fds[0]));
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 GNUNET_assert (0 == pipe (fds));
118 GNUNET_SCHEDULER_add_read (tc->sched,
120 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
121 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
122 GNUNET_TIME_UNIT_FOREVER_REL,
123 fds[0], &taskRd, cls);
124 GNUNET_SCHEDULER_add_write (tc->sched,
126 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
127 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
128 GNUNET_TIME_UNIT_FOREVER_REL,
129 fds[1], &taskWrt, cls);
134 task1 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
137 GNUNET_SCHEDULER_TaskIdentifier t2;
138 GNUNET_SCHEDULER_TaskIdentifier t3;
139 GNUNET_SCHEDULER_TaskIdentifier t4;
141 GNUNET_assert (1 == *ok);
143 /* t2 will go first -- prereq for all */
144 t2 = GNUNET_SCHEDULER_add_after (tc->sched,
146 GNUNET_SCHEDULER_PRIORITY_IDLE,
147 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
149 /* t3 will go before t4: higher priority */
150 t4 = GNUNET_SCHEDULER_add_after (tc->sched,
152 GNUNET_SCHEDULER_PRIORITY_IDLE,
154 t3 = GNUNET_SCHEDULER_add_delayed (tc->sched,
156 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
158 GNUNET_TIME_relative_get_zero (),
160 /* t4 will go first: lower prio, but prereq! */
161 GNUNET_SCHEDULER_add_after (tc->sched,
163 GNUNET_SCHEDULER_PRIORITY_UI, t4, &task5, cls);
169 * Main method, starts scheduler with task1,
170 * checks that "ok" is correct at the end.
178 GNUNET_SCHEDULER_run (&task1, &ok);
184 taskSig (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
187 GNUNET_assert (1 == *ok);
189 GNUNET_SCHEDULER_add_after (tc->sched,
191 GNUNET_SCHEDULER_PRIORITY_UI,
192 0, &taskNeverRun, NULL);
193 GNUNET_SCHEDULER_add_after (tc->sched,
195 GNUNET_SCHEDULER_PRIORITY_UI,
197 GNUNET_break (0 == PLIBC_KILL (getpid (), SIGTERM));
202 * Main method, starts scheduler with task1,
203 * checks that "ok" is correct at the end.
211 GNUNET_SCHEDULER_run (&taskSig, &ok);
219 taskCancel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
223 GNUNET_assert (1 == *ok);
225 GNUNET_SCHEDULER_cancel (tc->sched,
226 GNUNET_SCHEDULER_add_after (tc->sched,
228 GNUNET_SCHEDULER_PRIORITY_UI,
230 &taskNeverRun, NULL));
235 * Main method, starts scheduler with task1,
236 * checks that "ok" is correct at the end.
244 GNUNET_SCHEDULER_run (&taskCancel, &ok);
251 main (int argc, char *argv[])
255 GNUNET_log_setup ("test_scheduler", "WARNING", NULL);
257 ret += checkSignal ();
258 ret += checkCancel ();
263 /* end of test_scheduler.c */