Major rewrite of the SilcThreadQueue API
[runtime.git] / lib / silcutil / tests / test_silcthreadqueue.c
1 /* SilcThreadQueue tests */
2
3 #include "silcruntime.h"
4
5 SilcSchedule schedule;
6 SilcThreadQueue queue;
7 SilcBool success = FALSE;
8
9 SILC_FSM_STATE(test_st_start);
10 SILC_FSM_STATE(test_st_wait);
11 SILC_FSM_STATE(test_st_thread_start);
12 SILC_FSM_STATE(test_st_finish);
13
14 SILC_FSM_STATE(test_st_start)
15 {
16   SilcFSMThread thread;
17
18   SILC_LOG_DEBUG(("test_st_start"));
19
20   queue = silc_thread_queue_alloc(1, FALSE);
21   if (!queue) {
22     silc_fsm_next(fsm, test_st_finish);
23     return SILC_FSM_CONTINUE;
24   }
25
26   thread = silc_fsm_thread_alloc(fsm, NULL, NULL, NULL, TRUE);
27   if (!thread) {
28     silc_fsm_next(fsm, test_st_finish);
29     return SILC_FSM_CONTINUE;
30   }
31
32   silc_fsm_start(thread, test_st_thread_start);
33   silc_fsm_set_state_context(fsm, thread);
34
35   silc_fsm_next(fsm, test_st_wait);
36   return SILC_FSM_YIELD;
37 }
38
39 SILC_FSM_STATE(test_st_wait)
40 {
41   void *data;
42
43   SILC_LOG_DEBUG(("Wait for data"));
44
45   /* Wait for data */
46   data = silc_thread_queue_pop(queue, 0, TRUE);
47   if (!data || data != (void *)100) {
48     silc_fsm_next(fsm, test_st_finish);
49     return SILC_FSM_CONTINUE;
50   }
51
52   success = TRUE;
53   silc_fsm_next(fsm, test_st_finish);
54   SILC_FSM_THREAD_WAIT(state_context);
55 }
56
57 SILC_FSM_STATE(test_st_thread_start)
58 {
59   silc_thread_queue_connect(queue);
60
61   sleep(1);
62
63   /* Send data */
64   SILC_LOG_DEBUG(("Send data"));
65   silc_thread_queue_push(queue, 0, (void *)100, FALSE);
66
67   silc_thread_queue_disconnect(queue);
68   return SILC_FSM_FINISH;
69 }
70
71 SILC_FSM_STATE(test_st_finish)
72 {
73   SILC_LOG_DEBUG(("test_st_finish"));
74
75   silc_thread_queue_disconnect(queue);
76
77   SILC_LOG_DEBUG(("Finish machine"));
78   return SILC_FSM_FINISH;
79 }
80
81 static void destructor(SilcFSM fsm, void *fsm_context,
82                        void *destructor_context)
83 {
84   SILC_LOG_DEBUG(("FSM destructor, stopping scheduler"));
85   silc_fsm_free(fsm);
86   silc_schedule_stop(schedule);
87 }
88
89 int main(int argc, char **argv)
90 {
91   SilcFSM fsm;
92
93   silc_runtime_init();
94
95   if (argc > 1 && !strcmp(argv[1], "-d")) {
96     silc_log_debug(TRUE);
97     silc_log_debug_hexdump(TRUE);
98     silc_log_set_debug_string("*thread*");
99   }
100
101   SILC_LOG_DEBUG(("Allocating scheduler"));
102   schedule = silc_schedule_init(0, NULL, NULL, NULL);
103   if (!schedule)
104     goto err;
105
106   SILC_LOG_DEBUG(("Allocating FSM context"));
107   fsm = silc_fsm_alloc(NULL, destructor, NULL, schedule);
108   if (!fsm)
109     goto err;
110   silc_fsm_start(fsm, test_st_start);
111
112   SILC_LOG_DEBUG(("Running scheduler"));
113   silc_schedule(schedule);
114
115   silc_schedule_uninit(schedule);
116
117  err:
118   SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
119   fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");
120
121   silc_runtime_uninit();
122
123   return !success;
124 }