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