d59ed4e043066bea2b2a6e3e131e8def680aa8db
[silc.git] / lib / silcutil / tests / test_silcthread.c
1 /* SilcThreadPool tests */
2
3 #include "silc.h"
4
5 SilcSchedule schedule;
6
7 static void func(SilcSchedule schedule, void *context)
8 {
9   SILC_LOG_DEBUG(("func: %d", (int)context));
10   sleep(1);
11 }
12
13 SILC_TASK_CALLBACK(compl)
14 {
15   SILC_LOG_DEBUG(("completion: %d", (int)context));
16   if ((int)context == 0xff)
17     silc_schedule_stop(schedule);
18 }
19
20 int main(int argc, char **argv)
21 {
22   SilcBool success = FALSE;
23   SilcThreadPool tp;
24   int i;
25
26   if (argc > 1 && !strcmp(argv[1], "-d")) {
27     silc_log_debug(TRUE);
28     silc_log_quick(TRUE);
29     silc_log_debug_hexdump(TRUE);
30     silc_log_set_debug_string("*thread*");
31   }
32
33   schedule = silc_schedule_init(0, NULL, NULL);
34   if (!schedule)
35     goto err;
36
37   SILC_LOG_DEBUG(("Allocate thread pool"));
38   tp = silc_thread_pool_alloc(NULL, 2, 4, TRUE);
39   if (!tp)
40     goto err;
41   SILC_LOG_DEBUG(("Stop thread pool"));
42   silc_thread_pool_free(tp, TRUE);
43
44
45   SILC_LOG_DEBUG(("Allocate thread pool"));
46   tp = silc_thread_pool_alloc(NULL, 0, 2, FALSE);
47   if (!tp)
48     goto err;
49   for (i = 0; i < 6; i++) {
50     SILC_LOG_DEBUG(("Run thread %d", i + 1));
51     if (!silc_thread_pool_run(tp, TRUE, NULL, func, (void *) i + 1,
52                               compl, (void *)i + 1))
53       goto err;
54   }
55   sleep(6);
56   SILC_LOG_DEBUG(("Stop thread pool"));
57   silc_thread_pool_free(tp, TRUE);
58
59   SILC_LOG_DEBUG(("Allocate thread pool"));
60   tp = silc_thread_pool_alloc(NULL, 0, 2, TRUE);
61   if (!tp)
62     goto err;
63   for (i = 0; i < 2; i++) {
64     SILC_LOG_DEBUG(("Run thread %d", i + 1));
65     if (!silc_thread_pool_run(tp, FALSE, NULL, func, (void *) i + 1,
66                               compl, (void *)i + 1))
67       goto err;
68   }
69   if (silc_thread_pool_run(tp, FALSE, NULL, func, (void *)3,
70                            compl, (void *)3))
71     goto err;
72   sleep(3);
73   SILC_LOG_DEBUG(("Stop thread pool"));
74   silc_thread_pool_free(tp, TRUE);
75
76   SILC_LOG_DEBUG(("Allocate thread pool"));
77   tp = silc_thread_pool_alloc(NULL, 3, 20, TRUE);
78   if (!tp)
79     goto err;
80   for (i = 0; i < 8; i++) {
81     SILC_LOG_DEBUG(("Run thread %d", i + 1));
82     if (!silc_thread_pool_run(tp, FALSE, schedule, func, (void *) i + 1,
83                               compl, (void *)i + 1))
84       goto err;
85   }
86   if (!silc_thread_pool_run(tp, FALSE, schedule, func, (void *)0xff,
87                             compl, (void *)0xff))
88     goto err;
89   sleep(1);
90
91   silc_thread_pool_purge(tp);
92
93   silc_schedule(schedule);
94
95   SILC_LOG_DEBUG(("Stop thread pool"));
96   silc_thread_pool_free(tp, TRUE);
97
98   silc_schedule_uninit(schedule);
99   success = TRUE;
100
101  err:
102   SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
103   fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");
104
105   return success;
106 }