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