eaf74cb89088ab69f7640c151134d73ed34416f9
[silc.git] / lib / silcutil / tests / test_silcschedule.c
1 /* SilcSchedule tests */
2
3 #include "silcincludes.h"
4
5 typedef void (*Callback)(void *context);
6
7 #define NUM_TTASK 20
8 #ifdef FD_SETSIZE
9 #define NUM_FTASK FD_SETSIZE
10 #else
11 #define NUM_FTASK 250
12 #endif
13
14 SilcSchedule schedule;
15
16 SILC_TASK_CALLBACK(foo)
17 {
18
19 }
20
21 SILC_TASK_CALLBACK(cont)
22 {
23   int i;
24
25   SILC_LOG_DEBUG(("Adding %d fd tasks", NUM_FTASK - 10));
26
27   for (i = 0; i < NUM_FTASK - 10; i++)
28     silc_schedule_task_add_fd(schedule, i + 5, foo, (void *)(i + 5));
29 }
30
31 SILC_TASK_CALLBACK(timeout)
32 {
33   int i = (int)context;
34   SILC_LOG_DEBUG(("Timeout task %d", i));
35 }
36
37 SILC_TASK_CALLBACK(start)
38 {
39   int i;
40
41   SILC_LOG_DEBUG(("Adding %d timeout tasks", NUM_TTASK));
42
43   for (i = 0; i < NUM_TTASK; i++)
44     silc_schedule_task_add_timeout(schedule, timeout, (void *)i,
45         0, (i * 720391) & 999999);
46
47   silc_schedule_task_add_timeout(schedule, cont, (void *)i, 0, 100);
48 }
49
50 int main(int argc, char **argv)
51 {
52   SilcBool success = FALSE;
53
54   if (argc > 1 && !strcmp(argv[1], "-d")) {
55     silc_log_debug(TRUE);
56     silc_log_quick(TRUE);
57     silc_log_debug_hexdump(TRUE);
58     silc_log_set_debug_string("*sched*,*hash*");
59   }
60
61   SILC_LOG_DEBUG(("Allocating scheduler"));
62   schedule = silc_schedule_init(NUM_FTASK, NULL);
63   if (!schedule)
64     goto err;
65
66   silc_schedule_task_add_timeout(schedule, start, NULL, 0, 1);
67
68   SILC_LOG_DEBUG(("Running scheduler"));
69   silc_schedule(schedule);
70
71   silc_schedule_uninit(schedule);
72
73   success = TRUE;
74
75  err:
76   SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
77   fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");
78
79   return success;
80 }