LRN: Use GNUNET_EXTRA_LOGGING to manage compile-time logging calls
[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_EXTRA_LOGGING
31
32 static void
33 task3 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
34 {
35   int *ok = cls;
36
37   /* t4 should be ready (albeit with lower priority) */
38   GNUNET_assert (1 ==
39                  GNUNET_SCHEDULER_get_load (GNUNET_SCHEDULER_PRIORITY_COUNT));
40   GNUNET_assert (3 == *ok);
41   (*ok) = 4;
42 }
43
44
45 static void
46 task2 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
47 {
48   int *ok = cls;
49
50   GNUNET_assert (2 == *ok);
51   (*ok) = 3;
52   /* t3 will go before t4: higher priority */
53   GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_UI, &task3,
54                                       cls);
55 }
56
57 static void
58 task4 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
59 {
60   int *ok = cls;
61
62   GNUNET_assert (4 == *ok);
63   (*ok) = 5;
64 }
65
66 struct GNUNET_DISK_PipeHandle *p;
67 static const struct GNUNET_DISK_FileHandle *fds[2];
68
69
70 static void
71 taskWrt (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
72 {
73   static char c;
74   int *ok = cls;
75
76   GNUNET_assert (6 == *ok);
77   GNUNET_assert (GNUNET_NETWORK_fdset_handle_isset (tc->write_ready, fds[1]));
78   (*ok) = 7;
79   GNUNET_assert (1 == GNUNET_DISK_file_write (fds[1], &c, 1));
80 }
81
82
83 static void
84 taskNeverRun (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
85 {
86   GNUNET_assert (0);
87 }
88
89 static void
90 taskLast (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
91 {
92   int *ok = cls;
93
94   /* t4 should be ready (albeit with lower priority) */
95   GNUNET_assert (8 == *ok);
96   (*ok) = 0;
97 }
98
99 static void
100 taskRd (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
101 {
102   static char c;
103   int *ok = cls;
104
105   GNUNET_assert (7 == *ok);
106   GNUNET_assert (GNUNET_NETWORK_fdset_handle_isset (tc->read_ready, fds[0]));
107   GNUNET_assert (1 == GNUNET_DISK_file_read (fds[0], &c, 1));
108   (*ok) = 8;
109   GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE, &taskLast,
110                                       cls);
111   GNUNET_SCHEDULER_shutdown ();
112 }
113
114
115 static void
116 task5 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
117 {
118   int *ok = cls;
119
120   GNUNET_assert (5 == *ok);
121   (*ok) = 6;
122   p = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO);
123   GNUNET_assert (NULL != p);
124   fds[0] = GNUNET_DISK_pipe_handle (p, GNUNET_DISK_PIPE_END_READ);
125   fds[1] = GNUNET_DISK_pipe_handle (p, GNUNET_DISK_PIPE_END_WRITE);
126   GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, fds[0], &taskRd,
127                                   cls);
128   GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL, fds[1],
129                                    &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 t4;
139
140   GNUNET_assert (1 == *ok);
141   (*ok) = 2;
142   /* t2 will go first -- prereq for all */
143   t2 = GNUNET_SCHEDULER_add_after (GNUNET_SCHEDULER_NO_TASK, &task2, cls);
144   /* t4 will go after t2 ('add after') and after t3 (priority) */
145   t4 = GNUNET_SCHEDULER_add_after (t2, &task4, cls);
146   /* t5 will go last (after p4) */
147   GNUNET_SCHEDULER_add_after (t4, &task5, cls);
148 }
149
150
151
152 /**
153  * Main method, starts scheduler with task1,
154  * checks that "ok" is correct at the end.
155  */
156 static int
157 check ()
158 {
159   int ok;
160
161   ok = 1;
162   GNUNET_SCHEDULER_run (&task1, &ok);
163   return ok;
164 }
165
166
167 static void
168 taskShutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
169 {
170   int *ok = cls;
171
172   GNUNET_assert (1 == *ok);
173   *ok = 8;
174   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &taskLast, cls);
175   GNUNET_SCHEDULER_shutdown ();
176 }
177
178
179 /**
180  * Main method, starts scheduler with task1,
181  * checks that "ok" is correct at the end.
182  */
183 static int
184 checkShutdown ()
185 {
186   int ok;
187
188   ok = 1;
189   GNUNET_SCHEDULER_run (&taskShutdown, &ok);
190   return ok;
191 }
192
193
194 static void
195 taskSig (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
196 {
197   int *ok = cls;
198
199   GNUNET_assert (1 == *ok);
200   *ok = 8;
201   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &taskLast, cls);
202   GNUNET_break (0 == PLIBC_KILL (getpid (), SIGTERM));
203 }
204
205
206 /**
207  * Main method, starts scheduler with task1,
208  * checks that "ok" is correct at the end.
209  */
210 static int
211 checkSignal ()
212 {
213   int ok;
214
215   ok = 1;
216   GNUNET_SCHEDULER_run (&taskSig, &ok);
217   return ok;
218 }
219
220
221 static void
222 taskCancel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
223 {
224   int *ok = cls;
225
226   GNUNET_assert (1 == *ok);
227   *ok = 0;
228   GNUNET_SCHEDULER_cancel (GNUNET_SCHEDULER_add_after
229                            (GNUNET_SCHEDULER_NO_TASK, &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 */