LRN: Fix automake deps to allow -j* builds again
[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_NO
31
32 static void
33 task3 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
34 {
35   int *ok = cls;
36   /* t4 should be ready (albeit with lower priority) */
37   GNUNET_assert (1 == GNUNET_SCHEDULER_get_load (GNUNET_SCHEDULER_PRIORITY_COUNT));
38   GNUNET_assert (3 == *ok);
39   (*ok) = 4;
40 }
41
42
43 static void
44 task2 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
45 {
46   int *ok = cls;
47   GNUNET_assert (2 == *ok);
48   (*ok) = 3;
49   /* t3 will go before t4: higher priority */
50   GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_UI,
51                                       &task3, cls);
52 }
53
54 static void
55 task4 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
56 {
57   int *ok = cls;
58   GNUNET_assert (4 == *ok);
59   (*ok) = 5;
60 }
61
62 struct GNUNET_DISK_PipeHandle *p;
63 static const struct GNUNET_DISK_FileHandle *fds[2];
64
65
66 static void
67 taskWrt (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
68 {
69   static char c;
70   int *ok = cls;
71   GNUNET_assert (6 == *ok);
72   GNUNET_assert (GNUNET_NETWORK_fdset_handle_isset (tc->write_ready, fds[1]));
73   (*ok) = 7;
74   GNUNET_assert (1 == GNUNET_DISK_file_write (fds[1], &c, 1));
75 }
76
77
78 static void
79 taskNeverRun (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
80 {
81   GNUNET_assert (0);
82 }
83
84 static void
85 taskLast (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
86 {
87   int *ok = cls;
88   /* t4 should be ready (albeit with lower priority) */
89   GNUNET_assert (8 == *ok);
90   (*ok) = 0;
91 }
92
93 static void
94 taskRd (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
95 {
96   static char c;
97   int *ok = cls;
98   GNUNET_assert (7 == *ok);
99   GNUNET_assert (GNUNET_NETWORK_fdset_handle_isset (tc->read_ready, fds[0]));
100   GNUNET_assert (1 == GNUNET_DISK_file_read (fds[0], &c, 1));
101   (*ok) = 8;
102   GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
103                                       &taskLast, cls);
104   GNUNET_SCHEDULER_shutdown ();
105 }
106
107
108 static void
109 task5 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
110 {
111   int *ok = cls;
112   GNUNET_assert (5 == *ok);
113   (*ok) = 6;
114   p = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO);
115   GNUNET_assert (NULL != p);
116   fds[0] = GNUNET_DISK_pipe_handle (p, GNUNET_DISK_PIPE_END_READ);
117   fds[1] = GNUNET_DISK_pipe_handle (p, GNUNET_DISK_PIPE_END_WRITE);
118   GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
119                                   fds[0], &taskRd, cls);
120   GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
121                                    fds[1], &taskWrt, cls);
122 }
123
124
125 static void
126 task1 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
127 {
128   int *ok = cls;
129   GNUNET_SCHEDULER_TaskIdentifier t2;
130   GNUNET_SCHEDULER_TaskIdentifier t4;
131
132   GNUNET_assert (1 == *ok);
133   (*ok) = 2;
134   /* t2 will go first -- prereq for all */
135   t2 = GNUNET_SCHEDULER_add_after (GNUNET_SCHEDULER_NO_TASK, &task2, cls);
136   /* t4 will go after t2 ('add after') and after t3 (priority) */
137   t4 = GNUNET_SCHEDULER_add_after (t2, &task4, cls);
138   /* t5 will go last (after p4) */
139   GNUNET_SCHEDULER_add_after (t4, &task5, cls);
140 }
141
142
143
144 /**
145  * Main method, starts scheduler with task1,
146  * checks that "ok" is correct at the end.
147  */
148 static int
149 check ()
150 {
151   int ok;
152
153   ok = 1;
154   GNUNET_SCHEDULER_run (&task1, &ok);
155   return ok;
156 }
157
158
159 static void
160 taskShutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
161 {
162   int *ok = cls;
163   GNUNET_assert (1 == *ok);
164   *ok = 8;
165   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &taskLast, cls);
166   GNUNET_SCHEDULER_shutdown ();
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 checkShutdown ()
176 {
177   int ok;
178
179   ok = 1;
180   GNUNET_SCHEDULER_run (&taskShutdown, &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_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &taskLast, cls);
192   GNUNET_break (0 == PLIBC_KILL (getpid (), SIGTERM));
193 }
194
195
196 /**
197  * Main method, starts scheduler with task1,
198  * checks that "ok" is correct at the end.
199  */
200 static int
201 checkSignal ()
202 {
203   int ok;
204
205   ok = 1;
206   GNUNET_SCHEDULER_run (&taskSig, &ok);
207   return ok;
208 }
209
210
211 static void
212 taskCancel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
213 {
214   int *ok = cls;
215
216   GNUNET_assert (1 == *ok);
217   *ok = 0;
218   GNUNET_SCHEDULER_cancel (GNUNET_SCHEDULER_add_after (GNUNET_SCHEDULER_NO_TASK,
219                                                        &taskNeverRun, NULL));
220 }
221
222
223 /**
224  * Main method, starts scheduler with task1,
225  * checks that "ok" is correct at the end.
226  */
227 static int
228 checkCancel ()
229 {
230   int ok;
231
232   ok = 1;
233   GNUNET_SCHEDULER_run (&taskCancel, &ok);
234   return ok;
235 }
236
237
238
239 int
240 main (int argc, char *argv[])
241 {
242   int ret = 0;
243
244   GNUNET_log_setup ("test_scheduler", "WARNING", NULL);
245   ret += check ();
246 #ifndef MINGW
247   ret += checkSignal ();
248 #endif
249   ret += checkShutdown ();
250   ret += checkCancel ();
251   GNUNET_DISK_pipe_close (p);
252
253   return ret;
254 }
255
256 /* end of test_scheduler.c */