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 3, 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"
31 struct GNUNET_DISK_PipeHandle *p;
33 static const struct GNUNET_DISK_FileHandle *fds[2];
37 task2 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
41 /* t3 should be ready (albeit with lower priority) */
43 GNUNET_SCHEDULER_get_load (GNUNET_SCHEDULER_PRIORITY_COUNT));
44 GNUNET_assert (2 == *ok);
50 task3 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
54 GNUNET_assert (3 == *ok);
60 taskWrt (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
65 GNUNET_assert (6 == *ok);
66 GNUNET_assert (GNUNET_NETWORK_fdset_handle_isset (tc->write_ready, fds[1]));
68 GNUNET_assert (1 == GNUNET_DISK_file_write (fds[1], &c, 1));
73 taskNeverRun (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
80 taskLast (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
84 /* t4 should be ready (albeit with lower priority) */
85 GNUNET_assert (8 == *ok);
91 taskRd (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
96 GNUNET_assert (7 == *ok);
97 GNUNET_assert (GNUNET_NETWORK_fdset_handle_isset (tc->read_ready, fds[0]));
98 GNUNET_assert (1 == GNUNET_DISK_file_read (fds[0], &c, 1));
100 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE, &taskLast,
102 GNUNET_SCHEDULER_shutdown ();
107 task4 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
111 GNUNET_assert (4 == *ok);
113 p = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO, GNUNET_NO);
114 GNUNET_assert (NULL != p);
115 fds[0] = GNUNET_DISK_pipe_handle (p, GNUNET_DISK_PIPE_END_READ);
116 fds[1] = GNUNET_DISK_pipe_handle (p, GNUNET_DISK_PIPE_END_WRITE);
117 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, fds[0], &taskRd,
119 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL, fds[1],
125 task1 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
129 GNUNET_assert (1 == *ok);
131 GNUNET_SCHEDULER_add_now (&task3, cls);
132 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_UI, &task2,
134 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &task4, cls);
139 * Main method, starts scheduler with task1,
140 * checks that "ok" is correct at the end.
148 GNUNET_SCHEDULER_run (&task1, &ok);
154 taskShutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
158 GNUNET_assert (1 == *ok);
160 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &taskLast, cls);
161 GNUNET_SCHEDULER_shutdown ();
166 * Main method, starts scheduler with task1,
167 * checks that "ok" is correct at the end.
175 GNUNET_SCHEDULER_run (&taskShutdown, &ok);
181 taskSig (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
185 GNUNET_assert (1 == *ok);
187 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &taskLast, cls);
188 GNUNET_break (0 == PLIBC_KILL (getpid (), SIGTERM));
193 * Main method, starts scheduler with task1,
194 * checks that "ok" is correct at the end.
202 GNUNET_SCHEDULER_run (&taskSig, &ok);
208 taskCancel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
212 GNUNET_assert (1 == *ok);
214 GNUNET_SCHEDULER_cancel (GNUNET_SCHEDULER_add_now
215 (&taskNeverRun, NULL));
220 * Main method, starts scheduler with task1,
221 * checks that "ok" is correct at the end.
229 GNUNET_SCHEDULER_run (&taskCancel, &ok);
235 main (int argc, char *argv[])
239 GNUNET_log_setup ("test_scheduler", "WARNING", NULL);
242 ret += checkSignal ();
244 ret += checkShutdown ();
245 ret += checkCancel ();
246 GNUNET_DISK_pipe_close (p);
251 /* end of test_scheduler.c */