1 /* SilcAsyncOperation tests */
7 typedef void (*Callback)(void *context);
13 SilcFSMEventStruct sema;
14 SilcAsyncOperation op;
20 SILC_FSM_STATE(test_st_start);
21 SILC_FSM_STATE(test_st_second);
22 SILC_FSM_STATE(test_st_finish);
24 SILC_TASK_CALLBACK(async_call_timeout)
27 SILC_LOG_DEBUG(("******Async call cb, continuing FSM"));
28 silc_async_free(f->op);
32 static void async_abort(SilcAsyncOperation op, void *context)
35 SILC_LOG_DEBUG(("Async operation aborted"));
36 silc_schedule_task_del_by_context(schedule, f);
37 silc_schedule_task_del_by_callback(schedule, async_call_timeout);
41 static SilcAsyncOperation async_call(Callback cb, void *context)
45 SILC_LOG_DEBUG(("Async call"));
48 f->cb_context = context;
49 f->op = silc_async_alloc(async_abort, NULL, f);
51 silc_schedule_task_add(schedule, 0, async_call_timeout, f, 2, 1,
57 static void async_call_cb(void *context)
60 SILC_LOG_DEBUG(("*******Callback, signal and continue to next state"));
62 SILC_FSM_EVENT_SIGNAL(&f->sema);
63 SILC_FSM_CALL_CONTINUE(f->fsm);
66 SILC_FSM_STATE(test_st_start)
70 SILC_LOG_DEBUG(("test_st_start"));
72 silc_fsm_event_init(&f->sema, fsm);
74 /** Wait async callback */
75 SILC_LOG_DEBUG(("Call async call"));
76 silc_fsm_next_later(fsm, test_st_second, 1, 0);
77 SILC_FSM_CALL((f->op = async_call(async_call_cb, f)));
80 SILC_FSM_STATE(test_st_second)
85 SILC_LOG_DEBUG(("test_st_second"));
87 SILC_FSM_EVENT_TIMEDWAIT(&f->sema, 0, 1, &timedout);
89 if (timedout == TRUE) {
90 SILC_LOG_DEBUG(("Sema timedout, aborting async operation"));
92 silc_async_abort(f->op, NULL, NULL);
96 silc_fsm_next_later(fsm, test_st_finish, 2, 0);
100 SILC_FSM_STATE(test_st_finish)
102 SILC_LOG_DEBUG(("test_st_finish"));
104 SILC_LOG_DEBUG(("Finish machine"));
105 return SILC_FSM_FINISH;
108 static void destructor(SilcFSM fsm, void *fsm_context,
109 void *destructor_context)
111 SILC_LOG_DEBUG(("FSM destructor, stopping scheduler"));
113 silc_schedule_stop(schedule);
116 int main(int argc, char **argv)
118 SilcBool success = FALSE;
122 if (argc > 1 && !strcmp(argv[1], "-d")) {
123 silc_log_debug(TRUE);
124 silc_log_debug_hexdump(TRUE);
125 silc_log_set_debug_string("*async*,*errno*");
128 SILC_LOG_DEBUG(("Allocating scheduler"));
129 schedule = silc_schedule_init(0, NULL, NULL);
131 f = silc_calloc(1, sizeof(*f));
135 SILC_LOG_DEBUG(("Allocating FSM context"));
136 fsm = silc_fsm_alloc(f, destructor, NULL, schedule);
139 silc_fsm_start(fsm, test_st_start);
142 SILC_LOG_DEBUG(("Running scheduler"));
143 silc_schedule(schedule);
148 silc_schedule_uninit(schedule);
154 SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
155 fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");