Created SILC Runtime Toolkit git repository Part II.
[runtime.git] / lib / silcutil / tests / test_silcschedule.c
1 /* SilcSchedule tests */
2
3 #include "silcruntime.h"
4
5 typedef void (*Callback)(void *context);
6
7 #define NUM_TTASK 200
8 #ifdef FD_SETSIZE
9 #define NUM_FTASK FD_SETSIZE
10 #else
11 #define NUM_FTASK 250
12 #endif
13
14 SilcSchedule schedule;
15 int c = 0;
16
17 void notify_cb(SilcSchedule schedule, SilcBool added, SilcTask task,
18                SilcBool fd_task, SilcUInt32 fd, SilcTaskEvent event,
19                long sec, long usec, void *context)
20 {
21   SILC_LOG_DEBUG(("Notify cb, %s %s task, fd %d, sec %d usec %d",
22                   added ? "added" : "deleted", fd_task ? "fd" :"timeout",
23                   fd, sec, usec));
24 }
25
26 SILC_TASK_CALLBACK(foo)
27 {
28
29 }
30
31 SILC_TASK_CALLBACK(timeout)
32 {
33   int i = (int)context;
34   SILC_LOG_DEBUG(("Timeout task %d", i));
35
36   SILC_LOG_DEBUG(("Send 'timeout' signal"));
37   if (!silc_schedule_event_signal(NULL, "timeout", NULL))
38     SILC_LOG_DEBUG(("Error sending signal, error %d", silc_errno));
39 }
40
41 SILC_TASK_CALLBACK(cont2)
42 {
43 #ifdef SILC_DEBUG
44 //  silc_schedule_stats(schedule);
45 #endif /* SILC_DEBUG */
46
47   SILC_LOG_DEBUG(("Adding %d fd tasks", NUM_FTASK - 10));
48
49 #if 0
50   for (i = 0; i < NUM_FTASK - 10; i++)
51     silc_schedule_task_add_fd(schedule, i + 5, foo, (void *)(i + 5));
52 #endif
53
54   silc_schedule_event_signal(schedule, "interrupted", NULL, schedule);
55 }
56
57 SILC_TASK_CALLBACK(cont)
58 {
59   int i;
60
61 #ifdef SILC_DEBUG
62 //  silc_schedule_stats(schedule);
63 #endif /* SILC_DEBUG */
64
65   SILC_LOG_DEBUG(("Adding %d timeout tasks", NUM_TTASK / 3));
66   for (i = 0; i < NUM_TTASK / 3; i++)
67     silc_schedule_task_add_timeout(schedule, timeout, (void *)i, 0, 0);
68
69   silc_schedule_task_add_timeout(schedule, cont2, (void *)i, 0, 100);
70 }
71
72 SILC_TASK_CALLBACK(start)
73 {
74   int i;
75
76   SILC_LOG_DEBUG(("Adding %d timeout tasks", NUM_TTASK));
77
78 #if 0
79   for (i = 0; i < NUM_TTASK; i++)
80     silc_schedule_task_add_timeout(schedule, timeout, (void *)i,
81         i + (i & 9999), (i * 720391) & 999999);
82 #endif
83
84   for (i = 0; i < NUM_TTASK; i++)
85     silc_schedule_task_add_timeout(schedule, timeout, (void *)i, 0, 1);
86
87   silc_schedule_task_add_timeout(schedule, cont, (void *)i, 0, 100);
88 }
89
90 SILC_TASK_CALLBACK(interrupt)
91 {
92   SILC_LOG_DEBUG(("SIGINT signal, send 'interrupted' event signal"));
93   if (!silc_schedule_event_signal(schedule, "interrupted", NULL,
94                                   schedule))
95     SILC_LOG_DEBUG(("Error sending signal, error %d", silc_errno));
96 }
97
98 SILC_TASK_EVENT_CALLBACK(timeout_event_cb)
99 {
100   SILC_LOG_DEBUG(("timeout event signalled"));
101   if (c++ == 100) {
102     silc_schedule_task_del_event(NULL, "timeout");
103     return FALSE;
104   }
105
106   return TRUE;
107 }
108
109 SILC_TASK_EVENT_CALLBACK(interrupted_event)
110 {
111   SilcSchedule ptr = va_arg(va, void *);
112   SILC_LOG_DEBUG(("interrupted event signalled, ptr %p", ptr));
113   silc_schedule_event_disconnect(NULL, "interrupted", NULL,
114                                  interrupted_event, NULL);
115   silc_schedule_stop(schedule);
116   return TRUE;
117 }
118
119 int main(int argc, char **argv)
120 {
121   SilcBool success = FALSE;
122   SilcSchedule child, child2;
123   SilcTask timeout_event;
124
125   if (argc > 1 && !strcmp(argv[1], "-d")) {
126     silc_log_debug(TRUE);
127     silc_log_quick(TRUE);
128     silc_log_debug_hexdump(TRUE);
129     silc_log_set_debug_string("*sched*,*hash*,*errno*");
130   }
131
132   SILC_LOG_DEBUG(("Allocating scheduler"));
133   schedule = silc_schedule_init(NUM_FTASK, NULL, NULL, NULL);
134   if (!schedule)
135     goto err;
136   silc_schedule_set_notify(schedule, notify_cb, NULL);
137
138   SILC_LOG_DEBUG(("Allocate child scheduler"));
139   child = silc_schedule_init(0, NULL, NULL, schedule);
140   if (!child)
141     goto err;
142
143   SILC_LOG_DEBUG(("Allocate another child scheduler"));
144   child2 = silc_schedule_init(0, NULL, NULL, child);
145   if (!child2)
146     goto err;
147
148   SILC_LOG_DEBUG(("Add 'interrupted' event to child scheduler"));
149   if (!silc_schedule_task_add_event(child, "interrupted",
150                                     SILC_PARAM_PTR))
151     goto err;
152
153   SILC_LOG_DEBUG(("Add 'timeout' event to parent scheduler"));
154   timeout_event =
155     silc_schedule_task_add_event(schedule, "timeout");
156   if (!timeout_event)
157     goto err;
158
159   SILC_LOG_DEBUG(("Connect to 'interrupted' event in parent scheduler"));
160   if (!silc_schedule_event_connect(schedule, "interrupted", NULL,
161                                    interrupted_event, NULL))
162     goto err;
163
164   SILC_LOG_DEBUG(("Connect to 'timeout' event in child2 scheduler"));
165   if (!silc_schedule_event_connect(child2, NULL, timeout_event,
166                                    timeout_event_cb, NULL))
167     goto err;
168
169   SILC_LOG_DEBUG(("Add parent as global scheduler"));
170   silc_schedule_set_global(schedule);
171
172   silc_schedule_task_add_signal(schedule, SIGINT, interrupt, NULL);
173
174   if (!silc_schedule_task_add_timeout(schedule, start, NULL, 1, 0))
175     goto err;
176
177   SILC_LOG_DEBUG(("Running scheduler"));
178   silc_schedule(schedule);
179
180   silc_schedule_uninit(schedule);
181   silc_schedule_uninit(child);
182   silc_schedule_uninit(child2);
183
184   success = TRUE;
185
186  err:
187   SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
188   fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");
189
190   return !success;
191 }