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