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