diff options
author | Harald Welte <laforge@gnumonks.org> | 2017-04-16 17:26:30 +0200 |
---|---|---|
committer | Harald Welte <laforge@gnumonks.org> | 2017-04-27 09:50:47 +0200 |
commit | 31c0fef2fd5daa53617c05752411ec6446d8a82b (patch) | |
tree | 299c1643f09720801ae43b0936463e9d2d69b0a7 /tests | |
parent | f85861d6eb6ebc962bc710ac2d481536e6be7053 (diff) |
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
Diffstat (limited to 'tests')
-rw-r--r-- | tests/fsm/fsm_test.c | 40 | ||||
-rw-r--r-- | tests/fsm/fsm_test.err | 16 |
2 files changed, 45 insertions, 11 deletions
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 <osmocom/core/select.h> #include <osmocom/core/logging.h> #include <osmocom/core/fsm.h> +#include <osmocom/ctrl/control_if.h> 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 -[0;mTest FSM(my_id){NULL}: Allocated -[0;mTest FSM(my_id){NULL}: Received Event 1 -[0;mTest FSM(my_id){NULL}: Event 1 not permitted -[0;mTest FSM(my_id){NULL}: Received Event 0 -[0;mTest FSM(my_id){NULL}: state_chg to ONE -[0;mTest FSM(my_id){ONE}: Received Event 1 -[0;mTest FSM(my_id){ONE}: state_chg to TWO -[0;mTest FSM(my_id){TWO}: Timeout of T2342 +[0;mTest_FSM(my_id){NULL}: Allocated +[0;mTest_FSM(my_id){NULL}: Received Event 1 +[0;mTest_FSM(my_id){NULL}: Event 1 not permitted +[0;mTest_FSM(my_id){NULL}: Received Event 0 +[0;mTest_FSM(my_id){NULL}: state_chg to ONE +[0;mTest_FSM(my_id){ONE}: Received Event 1 +[0;mTest_FSM(my_id){ONE}: state_chg to TWO +[0;mTest_FSM(my_id){TWO}: Timeout of T2342 [0;mTimer [0;m
\ No newline at end of file |