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