daecebffc131a3d3f5f9142960ef6d2bc55c5dee
[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();
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, 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, (void *)100);
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   if (argc > 1 && !strcmp(argv[1], "-d")) {
94     silc_log_debug(TRUE);
95     silc_log_debug_hexdump(TRUE);
96     silc_log_set_debug_string("*thread*");
97   }
98
99   SILC_LOG_DEBUG(("Allocating scheduler"));
100   schedule = silc_schedule_init(0, NULL, NULL, NULL);
101   if (!schedule)
102     goto err;
103
104   SILC_LOG_DEBUG(("Allocating FSM context"));
105   fsm = silc_fsm_alloc(NULL, destructor, NULL, schedule);
106   if (!fsm)
107     goto err;
108   silc_fsm_start(fsm, test_st_start);
109
110   SILC_LOG_DEBUG(("Running scheduler"));
111   silc_schedule(schedule);
112
113   silc_schedule_uninit(schedule);
114
115  err:
116   SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
117   fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");
118
119   return !success;
120 }