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"
30 #define VERBOSE GNUNET_NO
33 task3 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
36 /* t4 should be ready (albeit with lower priority) */
37 GNUNET_assert (1 == GNUNET_SCHEDULER_get_load (GNUNET_SCHEDULER_PRIORITY_COUNT));
38 GNUNET_assert (3 == *ok);
44 task2 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
47 GNUNET_assert (2 == *ok);
49 /* t3 will go before t4: higher priority */
50 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_UI,
55 task4 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
58 GNUNET_assert (4 == *ok);
62 struct GNUNET_DISK_PipeHandle *p;
63 static const struct GNUNET_DISK_FileHandle *fds[2];
67 taskWrt (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
71 GNUNET_assert (6 == *ok);
72 GNUNET_assert (GNUNET_NETWORK_fdset_handle_isset (tc->write_ready, fds[1]));
74 GNUNET_assert (1 == GNUNET_DISK_file_write (fds[1], &c, 1));
79 taskNeverRun (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
85 taskLast (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
88 /* t4 should be ready (albeit with lower priority) */
89 GNUNET_assert (8 == *ok);
94 taskRd (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
98 GNUNET_assert (7 == *ok);
99 GNUNET_assert (GNUNET_NETWORK_fdset_handle_isset (tc->read_ready, fds[0]));
100 GNUNET_assert (1 == GNUNET_DISK_file_read (fds[0], &c, 1));
102 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
104 GNUNET_SCHEDULER_shutdown ();
109 task5 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
112 GNUNET_assert (5 == *ok);
114 p = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO);
115 GNUNET_assert (NULL != p);
116 fds[0] = GNUNET_DISK_pipe_handle (p, GNUNET_DISK_PIPE_END_READ);
117 fds[1] = GNUNET_DISK_pipe_handle (p, GNUNET_DISK_PIPE_END_WRITE);
118 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
119 fds[0], &taskRd, cls);
120 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
121 fds[1], &taskWrt, cls);
126 task1 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
129 GNUNET_SCHEDULER_TaskIdentifier t2;
130 GNUNET_SCHEDULER_TaskIdentifier t4;
132 GNUNET_assert (1 == *ok);
134 /* t2 will go first -- prereq for all */
135 t2 = GNUNET_SCHEDULER_add_after (GNUNET_SCHEDULER_NO_TASK, &task2, cls);
136 /* t4 will go after t2 ('add after') and after t3 (priority) */
137 t4 = GNUNET_SCHEDULER_add_after (t2, &task4, cls);
138 /* t5 will go last (after p4) */
139 GNUNET_SCHEDULER_add_after (t4, &task5, cls);
145 * Main method, starts scheduler with task1,
146 * checks that "ok" is correct at the end.
154 GNUNET_SCHEDULER_run (&task1, &ok);
160 taskShutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
163 GNUNET_assert (1 == *ok);
165 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &taskLast, cls);
166 GNUNET_SCHEDULER_shutdown ();
171 * Main method, starts scheduler with task1,
172 * checks that "ok" is correct at the end.
180 GNUNET_SCHEDULER_run (&taskShutdown, &ok);
186 taskSig (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
189 GNUNET_assert (1 == *ok);
191 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &taskLast, cls);
192 GNUNET_break (0 == PLIBC_KILL (getpid (), SIGTERM));
197 * Main method, starts scheduler with task1,
198 * checks that "ok" is correct at the end.
206 GNUNET_SCHEDULER_run (&taskSig, &ok);
212 taskCancel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
216 GNUNET_assert (1 == *ok);
218 GNUNET_SCHEDULER_cancel (GNUNET_SCHEDULER_add_after (GNUNET_SCHEDULER_NO_TASK,
219 &taskNeverRun, NULL));
224 * Main method, starts scheduler with task1,
225 * checks that "ok" is correct at the end.
233 GNUNET_SCHEDULER_run (&taskCancel, &ok);
240 main (int argc, char *argv[])
244 GNUNET_log_setup ("test_scheduler", "WARNING", NULL);
247 ret += checkSignal ();
249 ret += checkShutdown ();
250 ret += checkCancel ();
251 GNUNET_DISK_pipe_close (p);
256 /* end of test_scheduler.c */