-fix time assertion introduce in last patch
[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_util_lib.h"
26
27
28 struct GNUNET_DISK_PipeHandle *p;
29
30 static const struct GNUNET_DISK_FileHandle *fds[2];
31
32
33 static void
34 task2 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
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, const struct GNUNET_SCHEDULER_TaskContext *tc)
48 {
49   int *ok = cls;
50
51   GNUNET_assert (3 == *ok);
52   (*ok) = 4;
53 }
54
55
56 static void
57 taskWrt (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
58 {
59   static char c;
60   int *ok = cls;
61
62   GNUNET_assert (6 == *ok);
63   GNUNET_assert (GNUNET_NETWORK_fdset_handle_isset (tc->write_ready, fds[1]));
64   (*ok) = 7;
65   GNUNET_assert (1 == GNUNET_DISK_file_write (fds[1], &c, 1));
66 }
67
68
69 static void
70 taskNeverRun (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
71 {
72   GNUNET_assert (0);
73 }
74
75
76 static void
77 taskLast (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
78 {
79   int *ok = cls;
80
81   /* t4 should be ready (albeit with lower priority) */
82   GNUNET_assert (8 == *ok);
83   (*ok) = 0;
84 }
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  * Main method, starts scheduler with task1,
137  * checks that "ok" is correct at the end.
138  */
139 static int
140 check ()
141 {
142   int ok;
143
144   ok = 1;
145   GNUNET_SCHEDULER_run (&task1, &ok);
146   return ok;
147 }
148
149
150 static void
151 taskShutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
152 {
153   int *ok = cls;
154
155   GNUNET_assert (1 == *ok);
156   *ok = 8;
157   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &taskLast, cls);
158   GNUNET_SCHEDULER_shutdown ();
159 }
160
161
162 /**
163  * Main method, starts scheduler with task1,
164  * checks that "ok" is correct at the end.
165  */
166 static int
167 checkShutdown ()
168 {
169   int ok;
170
171   ok = 1;
172   GNUNET_SCHEDULER_run (&taskShutdown, &ok);
173   return ok;
174 }
175
176
177 #ifndef MINGW
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 (), GNUNET_TERM_SIG));
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 #endif
204
205
206 static void
207 taskCancel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
208 {
209   int *ok = cls;
210
211   GNUNET_assert (1 == *ok);
212   *ok = 0;
213   GNUNET_SCHEDULER_cancel (GNUNET_SCHEDULER_add_now
214                            (&taskNeverRun, NULL));
215 }
216
217
218 /**
219  * Main method, starts scheduler with task1,
220  * checks that "ok" is correct at the end.
221  */
222 static int
223 checkCancel ()
224 {
225   int ok;
226
227   ok = 1;
228   GNUNET_SCHEDULER_run (&taskCancel, &ok);
229   return ok;
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 */