fix
[oweals/gnunet.git] / src / util / test_scheduler.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20 /**
21  * @file util/test_scheduler.c
22  * @brief tests for the scheduler
23  */
24 #include "platform.h"
25 #include "gnunet_common.h"
26 #include "gnunet_scheduler_lib.h"
27 #include "gnunet_time_lib.h"
28 #include "gnunet_disk_lib.h"
29
30 #define VERBOSE GNUNET_NO
31
32 static void
33 task3 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
34 {
35   int *ok = cls;
36   /* t4 should be ready (albeit with lower priority) */
37   GNUNET_assert (1 == GNUNET_SCHEDULER_get_load (tc->sched,
38                                                  GNUNET_SCHEDULER_PRIORITY_COUNT));
39   GNUNET_assert (3 == *ok);
40   (*ok) = 4;
41 }
42
43
44 static void
45 task2 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
46 {
47   int *ok = cls;
48   GNUNET_assert (2 == *ok);
49   (*ok) = 3;
50   /* t3 will go before t4: higher priority */
51   GNUNET_SCHEDULER_add_with_priority (tc->sched,
52                                       GNUNET_SCHEDULER_PRIORITY_UI,
53                                       &task3, cls);
54 }
55
56 static void
57 task4 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
58 {
59   int *ok = cls;
60   GNUNET_assert (4 == *ok);
61   (*ok) = 5;
62 }
63
64 struct GNUNET_DISK_PipeHandle *p;
65 static const struct GNUNET_DISK_FileHandle *fds[2];
66
67
68 static void
69 taskWrt (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
70 {
71   static char c;
72   int *ok = cls;
73   GNUNET_assert (6 == *ok);
74   GNUNET_assert (GNUNET_NETWORK_fdset_handle_isset (tc->write_ready, fds[1]));
75   (*ok) = 7;
76   GNUNET_assert (1 == GNUNET_DISK_file_write (fds[1], &c, 1));
77 }
78
79
80 static void
81 taskNeverRun (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
82 {
83   GNUNET_assert (0);
84 }
85
86 static void
87 taskLast (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
88 {
89   int *ok = cls;
90   /* t4 should be ready (albeit with lower priority) */
91   GNUNET_assert (8 == *ok);
92   (*ok) = 0;
93 }
94
95 static void
96 taskRd (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
97 {
98   static char c;
99   int *ok = cls;
100   GNUNET_assert (7 == *ok);
101   GNUNET_assert (GNUNET_NETWORK_fdset_handle_isset (tc->read_ready, fds[0]));
102   GNUNET_assert (1 == GNUNET_DISK_file_read (fds[0], &c, 1));
103   (*ok) = 8;
104   GNUNET_SCHEDULER_add_with_priority (tc->sched,
105                                       GNUNET_SCHEDULER_PRIORITY_IDLE,
106                                       &taskLast, cls);
107   GNUNET_SCHEDULER_shutdown (tc->sched);
108 }
109
110
111 static void
112 task5 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
113 {
114   int *ok = cls;
115   GNUNET_assert (5 == *ok);
116   (*ok) = 6;
117   p = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO);
118   GNUNET_assert (NULL != p);
119   fds[0] = GNUNET_DISK_pipe_handle (p, GNUNET_DISK_PIPE_END_READ);
120   fds[1] = GNUNET_DISK_pipe_handle (p, GNUNET_DISK_PIPE_END_WRITE);
121   GNUNET_SCHEDULER_add_read_file (tc->sched,
122                                   GNUNET_TIME_UNIT_FOREVER_REL,
123                                   fds[0], &taskRd, cls);
124   GNUNET_SCHEDULER_add_write_file (tc->sched,
125                                    GNUNET_TIME_UNIT_FOREVER_REL,
126                                    fds[1], &taskWrt, cls);
127 }
128
129
130 static void
131 task1 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
132 {
133   int *ok = cls;
134   GNUNET_SCHEDULER_TaskIdentifier t2;
135   GNUNET_SCHEDULER_TaskIdentifier t4;
136
137   GNUNET_assert (1 == *ok);
138   (*ok) = 2;
139   /* t2 will go first -- prereq for all */
140   t2 = GNUNET_SCHEDULER_add_after (tc->sched,
141                                    GNUNET_SCHEDULER_NO_TASK, &task2, cls);
142   /* t4 will go after t2 ('add after') and after t3 (priority) */
143   t4 = GNUNET_SCHEDULER_add_after (tc->sched, t2, &task4, cls);
144   /* t5 will go last (after p4) */
145   GNUNET_SCHEDULER_add_after (tc->sched, t4, &task5, cls);
146 }
147
148
149
150 /**
151  * Main method, starts scheduler with task1,
152  * checks that "ok" is correct at the end.
153  */
154 static int
155 check ()
156 {
157   int ok;
158
159   ok = 1;
160   GNUNET_SCHEDULER_run (&task1, &ok);
161   return ok;
162 }
163
164
165 static void
166 taskShutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
167 {
168   int *ok = cls;
169   GNUNET_assert (1 == *ok);
170   *ok = 8;
171   GNUNET_SCHEDULER_add_delayed (tc->sched,
172                                 GNUNET_TIME_UNIT_FOREVER_REL, &taskLast, cls);
173   GNUNET_SCHEDULER_shutdown (tc->sched);
174 }
175
176
177 /**
178  * Main method, starts scheduler with task1,
179  * checks that "ok" is correct at the end.
180  */
181 static int
182 checkShutdown ()
183 {
184   int ok;
185
186   ok = 1;
187   GNUNET_SCHEDULER_run (&taskShutdown, &ok);
188   return ok;
189 }
190
191
192 static void
193 taskSig (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
194 {
195   int *ok = cls;
196   GNUNET_assert (1 == *ok);
197   *ok = 8;
198   GNUNET_SCHEDULER_add_delayed (tc->sched,
199                                 GNUNET_TIME_UNIT_FOREVER_REL, &taskLast, cls);
200   GNUNET_break (0 == PLIBC_KILL (getpid (), SIGTERM));
201 }
202
203
204 /**
205  * Main method, starts scheduler with task1,
206  * checks that "ok" is correct at the end.
207  */
208 static int
209 checkSignal ()
210 {
211   int ok;
212
213   ok = 1;
214   GNUNET_SCHEDULER_run (&taskSig, &ok);
215   return ok;
216 }
217
218
219 static void
220 taskCancel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
221 {
222   int *ok = cls;
223
224   GNUNET_assert (1 == *ok);
225   *ok = 0;
226   GNUNET_SCHEDULER_cancel (tc->sched,
227                            GNUNET_SCHEDULER_add_after (tc->sched,
228                                                        GNUNET_SCHEDULER_NO_TASK,
229                                                        &taskNeverRun, NULL));
230 }
231
232
233 /**
234  * Main method, starts scheduler with task1,
235  * checks that "ok" is correct at the end.
236  */
237 static int
238 checkCancel ()
239 {
240   int ok;
241
242   ok = 1;
243   GNUNET_SCHEDULER_run (&taskCancel, &ok);
244   return ok;
245 }
246
247
248
249 int
250 main (int argc, char *argv[])
251 {
252   int ret = 0;
253
254   GNUNET_log_setup ("test_scheduler", "WARNING", NULL);
255   ret += check ();
256 #ifndef MINGW
257   ret += checkSignal ();
258 #endif
259   ret += checkShutdown ();
260   ret += checkCancel ();
261   GNUNET_DISK_pipe_close (p);
262
263   return ret;
264 }
265
266 /* end of test_scheduler.c */