From 31c0fef2fd5daa53617c05752411ec6446d8a82b Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Sun, 16 Apr 2017 17:26:30 +0200 Subject: control_if: Add control interface commands for FSMs This allows programmatic access to introspection of FSM instances, which is quite handy from e.g. external test cases: Send a message to the code, then use the CTRL interface to check if that message has triggered the right kind of state transition. Change-Id: I0f80340ee9c61c88962fdd6764a6098a844d0d1e --- tests/fsm/fsm_test.c | 40 +++++++++++++++++++++++++++++++++++++--- tests/fsm/fsm_test.err | 16 ++++++++-------- 2 files changed, 45 insertions(+), 11 deletions(-) (limited to 'tests/fsm') diff --git a/tests/fsm/fsm_test.c b/tests/fsm/fsm_test.c index c3ab91c2..eea8b225 100644 --- a/tests/fsm/fsm_test.c +++ b/tests/fsm/fsm_test.c @@ -8,6 +8,7 @@ #include #include #include +#include enum { DMAIN, @@ -83,15 +84,38 @@ static struct osmo_fsm_state test_fsm_states[] = { }; static struct osmo_fsm fsm = { - .name = "Test FSM", + .name = "Test_FSM", .states = test_fsm_states, .num_states = ARRAY_SIZE(test_fsm_states), .log_subsys = DMAIN, }; +static struct ctrl_handle *g_ctrl; + +static struct ctrl_cmd *exec_ctrl_cmd(const char *cmdstr) +{ + struct ctrl_cmd *cmd; + return ctrl_cmd_exec_from_string(g_ctrl, cmdstr); + OSMO_ASSERT(cmd); + return cmd; +} + +static void assert_cmd_reply(const char *cmdstr, const char *expres) +{ + struct ctrl_cmd *cmd; + + cmd = exec_ctrl_cmd(cmdstr); + if (strcmp(cmd->reply, expres)) { + fprintf(stderr, "Reply '%s' doesn't match expected '%s'\n", cmd->reply, expres); + OSMO_ASSERT(0); + } + talloc_free(cmd); +} + static struct osmo_fsm_inst *foo(void) { struct osmo_fsm_inst *fi; + struct ctrl_cmd *cmd; LOGP(DMAIN, LOGL_INFO, "Checking FSM allocation\n"); fi = osmo_fsm_inst_alloc(&fsm, g_ctx, NULL, LOGL_DEBUG, "my_id"); @@ -100,20 +124,30 @@ static struct osmo_fsm_inst *foo(void) OSMO_ASSERT(!strncmp(osmo_fsm_inst_name(fi), fsm.name, strlen(fsm.name))); OSMO_ASSERT(fi->state == ST_NULL); OSMO_ASSERT(fi->log_level == LOGL_DEBUG); + assert_cmd_reply("GET 1 fsm.Test_FSM.id.my_id.state", "NULL"); + assert_cmd_reply("GET 1 fsm.Test_FSM.id.my_id.timer", "0,0,0"); /* Try invalid state transition */ osmo_fsm_inst_dispatch(fi, EV_B, (void *) 42); OSMO_ASSERT(fi->state == ST_NULL); + assert_cmd_reply("GET 1 fsm.Test_FSM.id.my_id.state", "NULL"); + /* Legitimate state transition */ osmo_fsm_inst_dispatch(fi, EV_A, (void *) 23); OSMO_ASSERT(fi->state == ST_ONE); + assert_cmd_reply("GET 1 fsm.Test_FSM.id.my_id.state", "ONE"); /* Legitimate transition with timer */ fsm.timer_cb = test_fsm_tmr_cb; osmo_fsm_inst_dispatch(fi, EV_B, (void *) 42); OSMO_ASSERT(fi->state == ST_TWO); + assert_cmd_reply("GET 1 fsm.Test_FSM.id.my_id.state", "TWO"); + cmd = exec_ctrl_cmd("GET 2 fsm.Test_FSM.id.my_id.dump"); + const char *exp = "'Test_FSM(my_id)','my_id','DEBUG','TWO',2342,timeout_sec="; + OSMO_ASSERT(!strncmp(cmd->reply, exp, strlen(exp))); + talloc_free(cmd); return fi; } @@ -142,7 +176,7 @@ int main(int argc, char **argv) stderr_target = log_target_create_stderr(); log_add_target(stderr_target); log_set_print_filename(stderr_target, 0); - + g_ctrl = ctrl_handle_alloc(NULL, NULL, NULL); g_ctx = NULL; OSMO_ASSERT(osmo_fsm_find_by_name(fsm.name) == NULL); @@ -152,7 +186,7 @@ int main(int argc, char **argv) OSMO_ASSERT(osmo_fsm_inst_find_by_name(&fsm, "my_id") == NULL); finst = foo(); OSMO_ASSERT(osmo_fsm_inst_find_by_id(&fsm, "my_id") == finst); - OSMO_ASSERT(osmo_fsm_inst_find_by_name(&fsm, "Test FSM(my_id)") == finst); + OSMO_ASSERT(osmo_fsm_inst_find_by_name(&fsm, "Test_FSM(my_id)") == finst); while (1) { osmo_select_main(0); diff --git a/tests/fsm/fsm_test.err b/tests/fsm/fsm_test.err index 6f031be8..17a2f2e0 100644 --- a/tests/fsm/fsm_test.err +++ b/tests/fsm/fsm_test.err @@ -1,11 +1,11 @@ Checking FSM allocation -Test FSM(my_id){NULL}: Allocated -Test FSM(my_id){NULL}: Received Event 1 -Test FSM(my_id){NULL}: Event 1 not permitted -Test FSM(my_id){NULL}: Received Event 0 -Test FSM(my_id){NULL}: state_chg to ONE -Test FSM(my_id){ONE}: Received Event 1 -Test FSM(my_id){ONE}: state_chg to TWO -Test FSM(my_id){TWO}: Timeout of T2342 +Test_FSM(my_id){NULL}: Allocated +Test_FSM(my_id){NULL}: Received Event 1 +Test_FSM(my_id){NULL}: Event 1 not permitted +Test_FSM(my_id){NULL}: Received Event 0 +Test_FSM(my_id){NULL}: state_chg to ONE +Test_FSM(my_id){ONE}: Received Event 1 +Test_FSM(my_id){ONE}: state_chg to TWO +Test_FSM(my_id){TWO}: Timeout of T2342 Timer  \ No newline at end of file -- cgit v1.2.3