Added SILC Thread Queue API
[silc.git] / lib / silcutil / tests / test_silctimer.c
1 /* SilcTimer tests */
2
3 #include "silc.h"
4
5 SilcSchedule schedule;
6 SilcTimerStruct timer;
7 SilcBool success = FALSE;
8
9 SILC_TASK_CALLBACK(check);
10
11 SILC_TASK_CALLBACK(restart)
12 {
13   SILC_LOG_DEBUG(("Timer is %s", silc_timer_is_running(&timer) ?
14                   "running" : "stopped"));
15   SILC_LOG_DEBUG(("Restart timer"));
16   silc_timer_continue(&timer);
17   silc_schedule_task_add_timeout(schedule, check, NULL, 1, 0);
18 }
19
20 SILC_TASK_CALLBACK(check)
21 {
22   SilcUInt64 sec;
23   SilcUInt32 usec;
24   SilcTimeStruct t, st;
25   char ts[32];
26
27   SILC_LOG_DEBUG(("Timer is %s", silc_timer_is_running(&timer) ?
28                   "running" : "stopped"));
29
30   silc_timer_value(&timer, &sec, &usec);
31   SILC_LOG_DEBUG(("Timer elapsed: %llu secs, %lu usec", sec, usec));
32
33   if (sec == 5) {
34     SILC_LOG_DEBUG(("Stop timer"));
35     silc_timer_stop(&timer);
36
37     silc_timer_value(&timer, &sec, &usec);
38     SILC_LOG_DEBUG(("Timer elapsed: %llu secs, %lu usec", sec, usec));
39
40     silc_timer_start_time(&timer, &st);
41     silc_timer_value_time(&timer, &t);
42     silc_time_universal_string(&st, ts, sizeof(ts));
43     SILC_LOG_DEBUG(("Start time: %s", ts));    
44     silc_time_universal_string(&t, ts, sizeof(ts));
45     SILC_LOG_DEBUG(("End time: %s", ts));    
46
47     success = TRUE;
48     silc_schedule_stop(schedule);
49     return;
50   }
51
52   if (sec == 2) {
53     SILC_LOG_DEBUG(("Stopping timer, sleep 3 seconds"));
54     silc_timer_stop(&timer);
55     silc_schedule_task_add_timeout(schedule, restart, NULL, 3, 0);
56     return;
57   }
58
59   silc_schedule_task_add_timeout(schedule, check, NULL, 0, 500000);
60 }
61
62
63 int main(int argc, char **argv)
64 {
65   if (argc > 1 && !strcmp(argv[1], "-d")) {
66     silc_log_debug(TRUE);
67     silc_log_quick(TRUE);
68     silc_log_debug_hexdump(TRUE);
69     silc_log_set_debug_string("*timer*,*errno*");
70   }
71
72   schedule = silc_schedule_init(0, NULL, NULL);
73   if (!schedule)
74     goto err;
75
76   silc_timer_synchronize(&timer);
77   SILC_LOG_DEBUG(("sync_diff: %d", timer.sync_diff));
78   SILC_LOG_DEBUG(("sync_tdiff: %d", timer.sync_tdiff));
79
80   SILC_LOG_DEBUG(("Start timer"));
81   silc_timer_start(&timer);
82
83   silc_schedule_task_add_timeout(schedule, check, NULL, 1, 0);
84
85   silc_schedule(schedule);
86
87   silc_schedule_uninit(schedule);
88
89  err:
90   SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
91   fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");
92
93   return success;
94 }