Skip to content

Commit 1bbf43a

Browse files
committed
Add test for ticker suspend and resume
Unit test the functions ticker_suspend and ticker_resume.
1 parent 23e6d50 commit 1bbf43a

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

TESTS/mbed_hal/ticker/main.cpp

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2268,6 +2268,82 @@ static void test_match_interval_passed_table()
22682268
}
22692269
}
22702270

2271+
/**
2272+
* Check that suspend and resume work as expected
2273+
*
2274+
* Check the following
2275+
* -time does not change while suspended
2276+
* -restricted interface functions are not called
2277+
* -scheduling resumes correctly
2278+
*/
2279+
static void test_suspend_resume()
2280+
{
2281+
ticker_set_handler(&ticker_stub, NULL);
2282+
2283+
interface_stub.timestamp = 1000;
2284+
us_timestamp_t start = ticker_read_us(&ticker_stub);
2285+
TEST_ASSERT_EQUAL(1000, start);
2286+
2287+
/* Reset call count */
2288+
interface_stub.init_call = 0;
2289+
interface_stub.read_call = 0;
2290+
interface_stub.set_interrupt_call = 0;
2291+
2292+
/* Suspend the ticker */
2293+
ticker_suspend(&ticker_stub);
2294+
const timestamp_t suspend_time = queue_stub.present_time;
2295+
2296+
2297+
/* Simulate time passing */
2298+
interface_stub.timestamp = 1500;
2299+
us_timestamp_t next = ticker_read_us(&ticker_stub);
2300+
2301+
/* Time should not have passed and no calls to interface should have been made */
2302+
TEST_ASSERT_EQUAL(start, next);
2303+
TEST_ASSERT_EQUAL(0, interface_stub.init_call);
2304+
TEST_ASSERT_EQUAL(0, interface_stub.read_call);
2305+
TEST_ASSERT_EQUAL(0, interface_stub.disable_interrupt_call);
2306+
TEST_ASSERT_EQUAL(0, interface_stub.clear_interrupt_call);
2307+
TEST_ASSERT_EQUAL(0, interface_stub.set_interrupt_call);
2308+
TEST_ASSERT_EQUAL(0, interface_stub.fire_interrupt_call);
2309+
2310+
2311+
/* Simulate a reinit (time reset to 0) */
2312+
interface_stub.timestamp = 0;
2313+
next = ticker_read_us(&ticker_stub);
2314+
2315+
/* Time should not have passed and no calls to interface should have been made */
2316+
TEST_ASSERT_EQUAL(start, next);
2317+
TEST_ASSERT_EQUAL(0, interface_stub.init_call);
2318+
TEST_ASSERT_EQUAL(0, interface_stub.read_call);
2319+
TEST_ASSERT_EQUAL(0, interface_stub.disable_interrupt_call);
2320+
TEST_ASSERT_EQUAL(0, interface_stub.clear_interrupt_call);
2321+
TEST_ASSERT_EQUAL(0, interface_stub.set_interrupt_call);
2322+
TEST_ASSERT_EQUAL(0, interface_stub.fire_interrupt_call);
2323+
2324+
2325+
/* Insert an event in the past and future */
2326+
ticker_event_t event_past = { 0 };
2327+
const timestamp_t event_past_timestamp = suspend_time - 10;
2328+
ticker_insert_event_us(&ticker_stub, &event_past, event_past_timestamp, 0);
2329+
2330+
ticker_event_t event_future = { 0 };
2331+
const timestamp_t event_future_timestamp = suspend_time + 10;
2332+
ticker_insert_event_us(&ticker_stub, &event_future, event_future_timestamp, 0);
2333+
2334+
TEST_ASSERT_EQUAL(0, interface_stub.init_call);
2335+
TEST_ASSERT_EQUAL(0, interface_stub.read_call);
2336+
TEST_ASSERT_EQUAL(0, interface_stub.disable_interrupt_call);
2337+
TEST_ASSERT_EQUAL(0, interface_stub.clear_interrupt_call);
2338+
TEST_ASSERT_EQUAL(0, interface_stub.set_interrupt_call);
2339+
TEST_ASSERT_EQUAL(0, interface_stub.fire_interrupt_call);
2340+
2341+
/* Resume and verify everything starts again */
2342+
ticker_resume(&ticker_stub);
2343+
TEST_ASSERT_EQUAL(suspend_time, queue_stub.present_time);
2344+
TEST_ASSERT_EQUAL(1, interface_stub.fire_interrupt_call);
2345+
}
2346+
22712347
static const case_t cases[] = {
22722348
MAKE_TEST_CASE("ticker initialization", test_ticker_initialization),
22732349
MAKE_TEST_CASE(
@@ -2376,6 +2452,10 @@ static const case_t cases[] = {
23762452
MAKE_TEST_CASE(
23772453
"test_match_interval_passed_table",
23782454
test_match_interval_passed_table
2455+
),
2456+
MAKE_TEST_CASE(
2457+
"test_suspend_resume",
2458+
test_suspend_resume
23792459
)
23802460
};
23812461

0 commit comments

Comments
 (0)