SILC FSM API changes.
[silc.git] / lib / silcutil / tests / test_silcfsm.c
index 9b7350074727dd5964e274e3a0c492e9f73f6d17..5432d418c9fc5e6179060515668c0a0083a99ec3 100644 (file)
@@ -1,6 +1,6 @@
 /* SILC FSM tests */
 
-#include "silcincludes.h"
+#include "silc.h"
 #include "silcfsm.h"
 
 typedef void (*Callback)(void *context);
@@ -11,27 +11,29 @@ typedef struct FooStruct *Foo;
 
 typedef struct {
   SilcFSMThreadStruct thread;
-  SilcFSMSemaStruct sema;
-  bool finished;
+  SilcFSMEventStruct sema;
+  SilcBool finished;
   int rounds;
   Foo f;
 } T;
 
 struct FooStruct {
-  bool error;
+  SilcBool error;
   SilcFSM fsm;
   SilcFSMThreadStruct thread;
   int timeout;
-  SilcFSMSemaStruct sema;
+  SilcFSMEventStruct sema;
   SilcSchedule schedule;
   Callback cb;
   void *cb_context;
   T threads[NUM_THREADS];
   T threads2[NUM_THREADS];
+  int c;
 };
 
 SILC_FSM_STATE(test_st_start);
 SILC_FSM_STATE(test_st_second);
+SILC_FSM_STATE(test_st_second_timeout);
 SILC_FSM_STATE(test_st_third);
 SILC_FSM_STATE(test_st_fourth);
 SILC_FSM_STATE(test_st_fifth);
@@ -73,17 +75,43 @@ SILC_FSM_STATE(test_st_start)
   /** Move to second state */
   SILC_LOG_DEBUG(("Move to next state"));
   silc_fsm_next(fsm, test_st_second);
-  return SILC_FSM_CONTINUE;
+  SILC_FSM_CONTINUE;
 }
 
 SILC_FSM_STATE(test_st_second)
 {
   SILC_LOG_DEBUG(("test_st_second"));
 
-  /** Move to third state, timeout */
+  /** Move to second timeout state, timeout */
   SILC_LOG_DEBUG(("Move to next state with 2 second timeout"));
-  silc_fsm_next_later(fsm, test_st_third, 2, 0);
-  return SILC_FSM_WAIT;
+  silc_fsm_next_later(fsm, test_st_second_timeout, 2, 0);
+  SILC_FSM_WAIT;
+}
+
+SILC_TASK_CALLBACK(test_second_timeout)
+{
+  Foo f = context;
+  SILC_LOG_DEBUG(("test_second_timeout"));
+
+  SILC_LOG_DEBUG(("Interrupt 3 second wait and continue immediately"));
+  f->c++;
+  silc_fsm_next(f->fsm, test_st_third);
+  silc_fsm_continue(f->fsm);
+}
+
+SILC_FSM_STATE(test_st_second_timeout)
+{
+  Foo f = fsm_context;
+
+  SILC_LOG_DEBUG(("test_st_second_timeout"));
+
+  /** Move to third state, timeout */
+  SILC_LOG_DEBUG(("Move to next state with 3 second timeout"));
+  SILC_LOG_DEBUG(("The timeout will be interrupted with silc_fsm_continue"));
+  silc_fsm_next_later(fsm, test_st_third, 3, 0);
+  silc_schedule_task_add_timeout(silc_fsm_get_schedule(fsm),
+                                test_second_timeout, f, 2, 500000);
+  SILC_FSM_WAIT;
 }
 
 static void async_call_cb(void *context)
@@ -99,6 +127,9 @@ SILC_FSM_STATE(test_st_third)
 
   SILC_LOG_DEBUG(("test_st_third"));
 
+  f->c++;
+  assert(f->c == 2);
+
   f->fsm = fsm;
 
   /** Wait async callback*/
@@ -116,14 +147,9 @@ SILC_FSM_STATE(test_st_fourth)
   f->timeout = 1;
 
   SILC_LOG_DEBUG(("Creating FSM thread"));
-  if (!silc_fsm_thread_init(&f->thread, fsm, f, NULL, NULL, FALSE)) {
-    /** Error creating thread */
-    SILC_LOG_DEBUG(("Error creating thread"));
-    f->error = TRUE;
-    silc_fsm_next(fsm, test_st_finish);
-    return SILC_FSM_CONTINUE;
-  }
+  silc_fsm_thread_init(&f->thread, fsm, f, NULL, NULL, FALSE);
   SILC_LOG_DEBUG(("Starting thread"));
+  /*** Start thread */
   silc_fsm_start(&f->thread, test_thread_st_start);
 
   /** Waiting thread to terminate */
@@ -141,7 +167,7 @@ SILC_FSM_STATE(test_thread_st_start)
   /** Move to final state, timeout */
   SILC_LOG_DEBUG(("Move to final state with %d second timeout", f->timeout));
   silc_fsm_next_later(fsm, test_thread_st_finish, f->timeout, 0);
-  return SILC_FSM_WAIT;
+  SILC_FSM_WAIT;
 }
 
 SILC_FSM_STATE(test_thread_st_finish)
@@ -149,7 +175,7 @@ SILC_FSM_STATE(test_thread_st_finish)
   SILC_LOG_DEBUG(("test_thread_st_finish"));
 
   SILC_LOG_DEBUG(("Finishing the thread"));
-  return SILC_FSM_FINISH;
+  SILC_FSM_FINISH;
 }
 
 SILC_FSM_STATE(test_st_fifth)
@@ -161,25 +187,19 @@ SILC_FSM_STATE(test_st_fifth)
 
   f->timeout = 7;
 
-  SILC_LOG_DEBUG(("Creating FSM semaphore"));
-  silc_fsm_sema_init(&f->sema, fsm, 0);
+  SILC_LOG_DEBUG(("Creating FSM event"));
+  silc_fsm_event_init(&f->sema, fsm);
 
   SILC_LOG_DEBUG(("Creating FSM thread"));
-  if (!silc_fsm_thread_init(&f->thread, fsm, f, NULL, NULL, TRUE)) {
-    /** Error creating real thread */
-    SILC_LOG_DEBUG(("Error creating thread"));
-    f->error = TRUE;
-    silc_fsm_next(fsm, test_st_finish);
-    return SILC_FSM_CONTINUE;
-  }
+  silc_fsm_thread_init(&f->thread, fsm, f, NULL, NULL, TRUE);
   SILC_LOG_DEBUG(("Starting thread"));
   silc_fsm_start(&f->thread, test_thread2_st_start);
 
   /** Waiting thread to terminate, timeout */
   SILC_LOG_DEBUG(("Waiting for thread to terminate for 5 seconds"));
   silc_fsm_next(fsm, test_st_sixth);
-  SILC_FSM_SEMA_TIMEDWAIT(&f->sema, 5, 0);
-  return SILC_FSM_CONTINUE;
+  SILC_FSM_EVENT_TIMEDWAIT(&f->sema, 5, 0, NULL);
+  SILC_FSM_CONTINUE;
 }
 
 SILC_FSM_STATE(test_thread2_st_start)
@@ -191,7 +211,7 @@ SILC_FSM_STATE(test_thread2_st_start)
   /** Move to final state, timeout */
   SILC_LOG_DEBUG(("Move to final state with %d second timeout", f->timeout));
   silc_fsm_next_later(fsm, test_thread2_st_finish, f->timeout, 0);
-  return SILC_FSM_WAIT;
+  SILC_FSM_WAIT;
 }
 
 SILC_FSM_STATE(test_thread2_st_finish)
@@ -200,10 +220,10 @@ SILC_FSM_STATE(test_thread2_st_finish)
   SILC_LOG_DEBUG(("test_thread2_st_finish"));
 
   SILC_LOG_DEBUG(("Post semaphore"));
-  SILC_FSM_SEMA_POST(&f->sema);
+  SILC_FSM_EVENT_SIGNAL(&f->sema);
 
   SILC_LOG_DEBUG(("Finishing the thread"));
-  return SILC_FSM_FINISH;
+  SILC_FSM_FINISH;
 }
 
 SILC_FSM_STATE(test_st_sixth)
@@ -215,7 +235,7 @@ SILC_FSM_STATE(test_st_sixth)
   /** Move to next state, timeout */
   SILC_LOG_DEBUG(("Continue to next state with 4 second timeout"));
   silc_fsm_next_later(fsm, test_st_seventh, 4, 0);
-  return SILC_FSM_WAIT;
+  SILC_FSM_WAIT;
 }
 
 SILC_FSM_STATE(test_thread3_st_start)
@@ -223,15 +243,15 @@ SILC_FSM_STATE(test_thread3_st_start)
   T *t = fsm_context;
 
   if (t->rounds == 0) {
-    SILC_FSM_SEMA_POST(&t->sema);
-    return SILC_FSM_FINISH;
+    SILC_FSM_EVENT_SIGNAL(&t->sema);
+    SILC_FSM_FINISH;
   }
 
   t->rounds--;
 
   /** Call in recursive */
   silc_fsm_next(fsm, test_thread3_st_start);
-  return SILC_FSM_CONTINUE;
+  SILC_FSM_CONTINUE;
 }
 
 SILC_FSM_STATE(test_st_seventh)
@@ -246,22 +266,15 @@ SILC_FSM_STATE(test_st_seventh)
   for (i = 0; i < NUM_THREADS; i++) {
     f->threads[i].rounds = 10;
     f->threads[i].f = f;
-    silc_fsm_sema_init(&f->threads[i].sema, fsm, 0);
-    if (!silc_fsm_thread_init(&f->threads[i].thread, fsm,
-                             &f->threads[i], NULL, NULL, FALSE)) {
-      /** Error creating thread */
-      SILC_LOG_DEBUG(("Error creating thread"));
-      f->error = TRUE;
-      silc_fsm_next(fsm, test_st_finish);
-      return SILC_FSM_CONTINUE;
-    }
-
+    silc_fsm_event_init(&f->threads[i].sema, fsm);
+    silc_fsm_thread_init(&f->threads[i].thread, fsm,
+                        &f->threads[i], NULL, NULL, FALSE);
     silc_fsm_start(&f->threads[i].thread, test_thread3_st_start);
   }
 
   /** Move to wait threads */
   silc_fsm_next(fsm, test_st_eighth);
-  return SILC_FSM_CONTINUE;
+  SILC_FSM_CONTINUE;
 }
 
 SILC_FSM_STATE(test_st_eighth)
@@ -271,19 +284,16 @@ SILC_FSM_STATE(test_st_eighth)
 
   for (i = 0; i < NUM_THREADS; i++) {
     if (f->threads[i].finished == FALSE) {
-      SILC_FSM_SEMA_WAIT(&f->threads[i].sema);
+      SILC_FSM_EVENT_WAIT(&f->threads[i].sema);
       f->threads[i].finished = TRUE;
     }
   }
 
-  for (i = 0; i < NUM_THREADS; i++)
-    silc_fsm_uninit(&f->threads[i].thread);
-
   SILC_LOG_DEBUG(("All %d threads terminated", NUM_THREADS));
 
   /** Move to next thread */
   silc_fsm_next(fsm, test_st_ninth);
-  return SILC_FSM_CONTINUE;
+  SILC_FSM_CONTINUE;
 }
 
 SILC_FSM_STATE(test_thread4_st_start)
@@ -291,15 +301,15 @@ SILC_FSM_STATE(test_thread4_st_start)
   T *t = fsm_context;
 
   if (t->rounds == 0) {
-    SILC_FSM_SEMA_POST(&t->sema);
-    return SILC_FSM_FINISH;
+    SILC_FSM_EVENT_SIGNAL(&t->sema);
+    SILC_FSM_FINISH;
   }
 
   t->rounds--;
 
   /** Call in recursive */
   silc_fsm_next(fsm, test_thread4_st_start);
-  return SILC_FSM_CONTINUE;
+  SILC_FSM_CONTINUE;
 }
 
 SILC_FSM_STATE(test_st_ninth)
@@ -309,29 +319,22 @@ SILC_FSM_STATE(test_st_ninth)
 
   SILC_LOG_DEBUG(("test_st_ninth"));
 
-  SILC_LOG_DEBUG(("Creating FSM semaphore"));
-  silc_fsm_sema_init(&f->sema, fsm, NUM_THREADS + 1);
+  SILC_LOG_DEBUG(("Creating FSM event"));
+  silc_fsm_event_init(&f->sema, fsm);
 
   SILC_LOG_DEBUG(("Creating %d real FSM threads", NUM_THREADS));
   for (i = 0; i < NUM_THREADS; i++) {
     f->threads2[i].rounds = 10;
     f->threads2[i].f = f;
-    silc_fsm_sema_init(&f->threads2[i].sema, fsm, 0);
-    if (!silc_fsm_thread_init(&f->threads2[i].thread, fsm,
-                             &f->threads2[i], NULL, NULL, TRUE)) {
-      /** Error creating real thread */
-      SILC_LOG_DEBUG(("Error creating real thread"));
-      f->error = TRUE;
-      silc_fsm_next(fsm, test_st_finish);
-      return SILC_FSM_CONTINUE;
-    }
-
+    silc_fsm_event_init(&f->threads2[i].sema, fsm);
+    silc_fsm_thread_init(&f->threads2[i].thread, fsm,
+                        &f->threads2[i], NULL, NULL, TRUE);
     silc_fsm_start(&f->threads2[i].thread, test_thread4_st_start);
   }
 
   /** Move to wait threads */
   silc_fsm_next(fsm, test_st_tenth);
-  return SILC_FSM_CONTINUE;
+  SILC_FSM_CONTINUE;
 }
 
 SILC_FSM_STATE(test_st_tenth)
@@ -341,18 +344,15 @@ SILC_FSM_STATE(test_st_tenth)
 
   for (i = 0; i < NUM_THREADS; i++)
     if (f->threads2[i].finished == FALSE) {
-      SILC_FSM_SEMA_WAIT(&f->threads2[i].sema);
+      SILC_FSM_EVENT_WAIT(&f->threads2[i].sema);
       f->threads2[i].finished = TRUE;
     }
 
-  for (i = 0; i < NUM_THREADS; i++)
-    silc_fsm_uninit(&f->threads2[i].thread);
-
   SILC_LOG_DEBUG(("All %d real threads terminated", NUM_THREADS));
 
   /** Finished successfully */
   silc_fsm_next_later(fsm, test_st_finish, 2, 0);
-  return SILC_FSM_WAIT;
+  SILC_FSM_WAIT;
 }
 
 SILC_FSM_STATE(test_st_finish)
@@ -360,7 +360,7 @@ SILC_FSM_STATE(test_st_finish)
   SILC_LOG_DEBUG(("test_st_finish"));
 
   SILC_LOG_DEBUG(("Finish machine"));
-  return SILC_FSM_FINISH;
+  SILC_FSM_FINISH;
 }
 
 static void destructor(SilcFSM fsm, void *fsm_context,
@@ -374,7 +374,7 @@ static void destructor(SilcFSM fsm, void *fsm_context,
 
 int main(int argc, char **argv)
 {
-  bool success = FALSE;
+  SilcBool success = FALSE;
   SilcSchedule schedule;
   SilcFSM fsm;
   Foo f;
@@ -395,7 +395,7 @@ int main(int argc, char **argv)
   f->schedule = schedule;
 
   SILC_LOG_DEBUG(("Allocating FSM context"));
-  fsm = silc_fsm_alloc(f, destructor, f, schedule);
+  f->fsm = fsm = silc_fsm_alloc(f, destructor, f, schedule);
   if (!fsm)
     goto err;
   silc_fsm_start(fsm, test_st_start);