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 task2 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
37 /* t3 should be ready (albeit with lower priority) */
39 GNUNET_SCHEDULER_get_load (GNUNET_SCHEDULER_PRIORITY_COUNT));
40 GNUNET_assert (2 == *ok);
46 task3 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
50 GNUNET_assert (3 == *ok);
54 struct GNUNET_DISK_PipeHandle *p;
55 static const struct GNUNET_DISK_FileHandle *fds[2];
59 taskWrt (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
64 GNUNET_assert (6 == *ok);
65 GNUNET_assert (GNUNET_NETWORK_fdset_handle_isset (tc->write_ready, fds[1]));
67 GNUNET_assert (1 == GNUNET_DISK_file_write (fds[1], &c, 1));
72 taskNeverRun (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
78 taskLast (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
82 /* t4 should be ready (albeit with lower priority) */
83 GNUNET_assert (8 == *ok);
88 taskRd (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
93 GNUNET_assert (7 == *ok);
94 GNUNET_assert (GNUNET_NETWORK_fdset_handle_isset (tc->read_ready, fds[0]));
95 GNUNET_assert (1 == GNUNET_DISK_file_read (fds[0], &c, 1));
97 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE, &taskLast,
99 GNUNET_SCHEDULER_shutdown ();
104 task4 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
108 GNUNET_assert (4 == *ok);
110 p = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO, GNUNET_NO);
111 GNUNET_assert (NULL != p);
112 fds[0] = GNUNET_DISK_pipe_handle (p, GNUNET_DISK_PIPE_END_READ);
113 fds[1] = GNUNET_DISK_pipe_handle (p, GNUNET_DISK_PIPE_END_WRITE);
114 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, fds[0], &taskRd,
116 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL, fds[1],
122 task1 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
126 GNUNET_assert (1 == *ok);
128 GNUNET_SCHEDULER_add_now (&task3, cls);
129 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_UI, &task2,
131 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &task4, cls);
137 * Main method, starts scheduler with task1,
138 * checks that "ok" is correct at the end.
146 GNUNET_SCHEDULER_run (&task1, &ok);
152 taskShutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
156 GNUNET_assert (1 == *ok);
158 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &taskLast, cls);
159 GNUNET_SCHEDULER_shutdown ();
164 * Main method, starts scheduler with task1,
165 * checks that "ok" is correct at the end.
173 GNUNET_SCHEDULER_run (&taskShutdown, &ok);
179 taskSig (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
183 GNUNET_assert (1 == *ok);
185 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &taskLast, cls);
186 GNUNET_break (0 == PLIBC_KILL (getpid (), SIGTERM));
191 * Main method, starts scheduler with task1,
192 * checks that "ok" is correct at the end.
200 GNUNET_SCHEDULER_run (&taskSig, &ok);
206 taskCancel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
210 GNUNET_assert (1 == *ok);
212 GNUNET_SCHEDULER_cancel (GNUNET_SCHEDULER_add_now
213 (&taskNeverRun, NULL));
218 * Main method, starts scheduler with task1,
219 * checks that "ok" is correct at the end.
227 GNUNET_SCHEDULER_run (&taskCancel, &ok);
234 main (int argc, char *argv[])
238 GNUNET_log_setup ("test_scheduler", "WARNING", NULL);
241 ret += checkSignal ();
243 ret += checkShutdown ();
244 ret += checkCancel ();
245 GNUNET_DISK_pipe_close (p);
250 /* end of test_scheduler.c */