aboutsummaryrefslogtreecommitdiff
path: root/src/util/test_scheduler.c
blob: 83319d2aa74243870855fe5bfbad7a9ab9b35909 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
     This file is part of GNUnet.
     Copyright (C) 2009 GNUnet e.V.

     GNUnet is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 3, or (at your
     option) any later version.

     GNUnet is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with GNUnet; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     Boston, MA 02110-1301, USA.
*/
/**
 * @file util/test_scheduler.c
 * @brief tests for the scheduler
 */
#include "platform.h"
#include "gnunet_util_lib.h"


static struct GNUNET_DISK_PipeHandle *p;

static const struct GNUNET_DISK_FileHandle *fds[2];

static struct GNUNET_SCHEDULER_Task *never_run_task;


static void
task2 (void *cls)
{
  int *ok = cls;

  /* t3 should be ready (albeit with lower priority) */
  GNUNET_assert (1 ==
                 GNUNET_SCHEDULER_get_load (GNUNET_SCHEDULER_PRIORITY_COUNT));
  GNUNET_assert (2 == *ok);
  (*ok) = 3;
}


static void
task3 (void *cls)
{
  int *ok = cls;

  GNUNET_assert (3 == *ok);
  (*ok) = 4;
}


static void
taskWrt (void *cls)
{
  static char c;
  int *ok = cls;
  const struct GNUNET_SCHEDULER_TaskContext *tc;

  tc = GNUNET_SCHEDULER_get_task_context ();
  GNUNET_assert (6 == *ok);
  GNUNET_assert (GNUNET_NETWORK_fdset_handle_isset (tc->write_ready, fds[1]));
  (*ok) = 7;
  GNUNET_assert (1 == GNUNET_DISK_file_write (fds[1], &c, 1));
}


static void
taskNeverRun (void *cls)
{
  GNUNET_assert (0);
}


static void
taskLastRd (void *cls)
{
  int *ok = cls;

  GNUNET_assert (8 == *ok);
  (*ok) = 0;
}


static void
taskLastSig (void *cls)
{
  int *ok = cls;

  GNUNET_SCHEDULER_cancel (never_run_task);
  GNUNET_assert (9 == *ok);
  (*ok) = 0;
}


static void
taskLastShutdown (void *cls)
{
  int *ok = cls;

  GNUNET_assert (10 == *ok);
  (*ok) = 0;
}


static void
taskRd (void *cls)
{
  static char c;
  int *ok = cls;
  const struct GNUNET_SCHEDULER_TaskContext *tc;

  tc = GNUNET_SCHEDULER_get_task_context ();
  GNUNET_assert (7 == *ok);
  GNUNET_assert (GNUNET_NETWORK_fdset_handle_isset (tc->read_ready, fds[0]));
  GNUNET_assert (1 == GNUNET_DISK_file_read (fds[0], &c, 1));
  (*ok) = 8;
  GNUNET_SCHEDULER_add_shutdown (&taskLastRd,
				 cls);
  GNUNET_SCHEDULER_shutdown ();
}


static void
task4 (void *cls)
{
  int *ok = cls;

  GNUNET_assert (4 == *ok);
  (*ok) = 6;
  p = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO, GNUNET_NO);
  GNUNET_assert (NULL != p);
  fds[0] = GNUNET_DISK_pipe_handle (p, GNUNET_DISK_PIPE_END_READ);
  fds[1] = GNUNET_DISK_pipe_handle (p, GNUNET_DISK_PIPE_END_WRITE);
  GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
				  fds[0],
				  &taskRd,
                                  cls);
  GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
				   fds[1],
                                   &taskWrt,
				   cls);
}


static void
task1 (void *cls)
{
  int *ok = cls;

  GNUNET_assert (1 == *ok);
  (*ok) = 2;
  GNUNET_SCHEDULER_add_now (&task3, cls);
  GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_UI,
				      &task2,
                                      cls);
  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
				&task4,
				cls);
}


/**
 * Main method, starts scheduler with task1,
 * checks that "ok" is correct at the end.
 */
static int
check ()
{
  int ok;

  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
	      "[Check scheduling]\n");
  ok = 1;
  GNUNET_SCHEDULER_run (&task1, &ok);
  return ok;
}


static void
taskShutdown (void *cls)
{
  int *ok = cls;

  GNUNET_assert (1 == *ok);
  *ok = 10;
  GNUNET_SCHEDULER_add_shutdown (&taskLastShutdown, cls);
  GNUNET_SCHEDULER_shutdown ();
}


/**
 * Main method, starts scheduler with task1,
 * checks that "ok" is correct at the end.
 */
static int
checkShutdown ()
{
  int ok;

  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
	      "[Check shutdown]\n");
  ok = 1;
  GNUNET_SCHEDULER_run (&taskShutdown, &ok);
  return ok;
}


#ifndef MINGW
static void
taskSig (void *cls)
{
  int *ok = cls;

  GNUNET_assert (1 == *ok);
  *ok = 9;
  GNUNET_SCHEDULER_add_shutdown (&taskLastSig, cls);
  never_run_task = 
    GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
				  &taskNeverRun,
				  NULL);
  GNUNET_break (0 == PLIBC_KILL (getpid (),
				 GNUNET_TERM_SIG));
}


/**
 * Main method, starts scheduler with task1,
 * checks that "ok" is correct at the end.
 */
static int
checkSignal ()
{
  int ok;

  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
	      "[Check signal handling]\n");
  ok = 1;
  GNUNET_SCHEDULER_run (&taskSig, &ok);
  return ok;
}
#endif


static void
taskCancel (void *cls)
{
  int *ok = cls;

  GNUNET_assert (1 == *ok);
  *ok = 0;
  GNUNET_SCHEDULER_cancel (GNUNET_SCHEDULER_add_now (&taskNeverRun, NULL));
}


/**
 * Main method, starts scheduler with task1,
 * checks that "ok" is correct at the end.
 */
static int
checkCancel ()
{
  int ok;

  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
	      "[Check task cancellation]\n");
  ok = 1;
  GNUNET_SCHEDULER_run (&taskCancel, &ok);
  return ok;
}


int
main (int argc, char *argv[])
{
  int ret = 0;

  GNUNET_log_setup ("test_scheduler", "WARNING", NULL);
  ret += check ();
  ret += checkCancel ();
#ifndef MINGW
  ret += checkSignal ();
#endif
  ret += checkShutdown ();
  GNUNET_DISK_pipe_close (p);

  return ret;
}

/* end of test_scheduler.c */