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_util_lib.h"
28 struct GNUNET_DISK_PipeHandle *p;
30 static const struct GNUNET_DISK_FileHandle *fds[2];
34 task2 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
38 /* t3 should be ready (albeit with lower priority) */
40 GNUNET_SCHEDULER_get_load (GNUNET_SCHEDULER_PRIORITY_COUNT));
41 GNUNET_assert (2 == *ok);
47 task3 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
51 GNUNET_assert (3 == *ok);
57 taskWrt (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
62 GNUNET_assert (6 == *ok);
63 GNUNET_assert (GNUNET_NETWORK_fdset_handle_isset (tc->write_ready, fds[1]));
65 GNUNET_assert (1 == GNUNET_DISK_file_write (fds[1], &c, 1));
70 taskNeverRun (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
77 taskLast (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
81 /* t4 should be ready (albeit with lower priority) */
82 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);
136 * Main method, starts scheduler with task1,
137 * checks that "ok" is correct at the end.
145 GNUNET_SCHEDULER_run (&task1, &ok);
151 taskShutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
155 GNUNET_assert (1 == *ok);
157 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &taskLast, cls);
158 GNUNET_SCHEDULER_shutdown ();
163 * Main method, starts scheduler with task1,
164 * checks that "ok" is correct at the end.
172 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);
207 taskCancel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
211 GNUNET_assert (1 == *ok);
213 GNUNET_SCHEDULER_cancel (GNUNET_SCHEDULER_add_now
214 (&taskNeverRun, NULL));
219 * Main method, starts scheduler with task1,
220 * checks that "ok" is correct at the end.
228 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 */