From 76bdbd4b2b9ed8bf9cc3dab387f8a948ce2cb0ac Mon Sep 17 00:00:00 2001 From: Vadim Yanitskiy Date: Tue, 19 Jul 2022 06:20:55 +0700 Subject: [PATCH] fsm: fix state_chg(): pass microseconds to osmo_timer_schedule() As was demonstrated in [1], osmo_fsm_inst_state_chg_ms() is broken. The problem is in state_chg(): this function is passing *milli*seconds to osmo_timer_schedule(), while it expects *micro*seconds. Change-Id: Ib0b6c3bdb56e4279df9e5ba7db16841645c452aa Related: [1] I5a35730a8448292b075aefafed897353678250f9 Fixes: I35b330e460e80bb67376c77e997e464439ac5397 Fixes: OS#5622 --- src/fsm.c | 7 +++++-- tests/fsm/fsm_test.c | 8 +++----- tests/fsm/fsm_test.err | 2 +- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/fsm.c b/src/fsm.c index 8b0b27beb..b51c5cbf6 100644 --- a/src/fsm.c +++ b/src/fsm.c @@ -685,8 +685,11 @@ static int state_chg(struct osmo_fsm_inst *fi, uint32_t new_state, if (!keep_timer || (keep_timer && !osmo_timer_pending(&fi->timer))) { fi->T = T; - if (timeout_ms) - osmo_timer_schedule(&fi->timer, timeout_ms / 1000, timeout_ms % 1000); + if (timeout_ms) { + osmo_timer_schedule(&fi->timer, + /* seconds */ (timeout_ms / 1000), + /* microseconds */ (timeout_ms % 1000) * 1000); + } } /* Call 'onenter' last, user might terminate FSM from there */ diff --git a/tests/fsm/fsm_test.c b/tests/fsm/fsm_test.c index ec31dd2e5..6fa0ae718 100644 --- a/tests/fsm/fsm_test.c +++ b/tests/fsm/fsm_test.c @@ -441,15 +441,13 @@ static void test_state_chg_Tms(void) OSMO_ASSERT(timeout_fired == -1); fake_time_passes(0, 350000); /* +350ms, 1s 100ms total */ - /* OSMO_ASSERT(timeout_fired == -1); */ - - /* FIXME: the timeout expires here, earlier than expected */ + OSMO_ASSERT(timeout_fired == -1); fake_time_passes(0, 200000); /* +200ms, 1s 300ms total */ - /* OSMO_ASSERT(timeout_fired == -1); */ + OSMO_ASSERT(timeout_fired == -1); fake_time_passes(0, 37000); /* +37ms, 1s 337ms total */ - /* OSMO_ASSERT(timeout_fired == 4242); */ + OSMO_ASSERT(timeout_fired == 4242); osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REQUEST, NULL); diff --git a/tests/fsm/fsm_test.err b/tests/fsm/fsm_test.err index f140d2e75..51bf5da3d 100644 --- a/tests/fsm/fsm_test.err +++ b/tests/fsm/fsm_test.err @@ -137,9 +137,9 @@ Test_FSM{NULL}: State change to ONE (T4242, 1337ms) Total time passed: 0.500000 s Total time passed: 0.750000 s Total time passed: 1.100000 s -Test_FSM{ONE}: Timeout of T4242 Total time passed: 1.300000 s Total time passed: 1.337000 s +Test_FSM{ONE}: Timeout of T4242 Test_FSM{ONE}: Terminating (cause = OSMO_FSM_TERM_REQUEST) Test_FSM{ONE}: Freeing instance Test_FSM{ONE}: Deallocated