1 /* SilcAsyncOperation tests */
3 #include "silcincludes.h"
7 typedef void (*Callback)(void *context);
13 SilcFSMSemaStruct 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_SEMA_POST(&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_sema_init(&f->sema, fsm, 0);
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)
84 SILC_LOG_DEBUG(("test_st_second"));
86 SILC_FSM_SEMA_TIMEDWAIT(&f->sema, 0, 1);
88 SILC_LOG_DEBUG(("Sema timedout, aborting async operation"));
90 silc_async_abort(f->op, NULL, NULL);
93 silc_fsm_next_later(fsm, test_st_finish, 2, 0);
97 SILC_FSM_STATE(test_st_finish)
99 SILC_LOG_DEBUG(("test_st_finish"));
101 SILC_LOG_DEBUG(("Finish machine"));
102 return SILC_FSM_FINISH;
105 static void destructor(SilcFSM fsm, void *fsm_context,
106 void *destructor_context)
108 SILC_LOG_DEBUG(("FSM destructor, stopping scheduler"));
110 silc_schedule_stop(schedule);
113 int main(int argc, char **argv)
115 SilcBool success = FALSE;
119 if (argc > 1 && !strcmp(argv[1], "-d")) {
120 silc_log_debug(TRUE);
121 silc_log_debug_hexdump(TRUE);
122 silc_log_set_debug_string("*async*");
125 SILC_LOG_DEBUG(("Allocating scheduler"));
126 schedule = silc_schedule_init(0, NULL);
128 f = silc_calloc(1, sizeof(*f));
132 SILC_LOG_DEBUG(("Allocating FSM context"));
133 fsm = silc_fsm_alloc(f, destructor, NULL, schedule);
136 silc_fsm_start(fsm, test_st_start);
139 SILC_LOG_DEBUG(("Running scheduler"));
140 silc_schedule(schedule);
145 silc_schedule_uninit(schedule);
151 SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
152 fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");