Mocking a timer callback

382 views
Skip to first unread message

Pradeepa Senanayake

unread,
Jul 13, 2016, 1:35:18 AM7/13/16
to cpputest
Hello,

I have the following scenario to mock which is in a compilation unit called 'EventHandler'.

I have a function called 'RegisterTimer()'

This will register an RTOS timer using RTOS functions. We need to provide it with a callback function which will get called when the timer has expired.

That call back function will call another 'SubmitEvent()' function.

Both RegisterTimer() and SubmitEvent() will be exposed to outside through a header file. That is because SubmitEvent can be called by some other parts of the application too.

There is no implementation benefit for making the timer callback function global. Therefore, I can make it static and put it inside the same compilation unit EventHandler.

But if I want to test what is happening when the callback is called, I will have to make the callback accessible to outside or I can separate the timer callback from EventHandler compilation unit and make a new compilation unit for callbacks alone. Then I can expose the callback through a header file to other parts of the firmware.

Usually what is the best way to handle this? Whether to make the callback static or not is the main question...


James Grenning

unread,
Jul 14, 2016, 9:26:45 AM7/14/16
to cpputest

Hi

Sounds like you want to test you callback function and not the callback mechanism burried in the RTOS.

To test the callback function, just call it.

If the callback is a static function, and you are unit testing, you can have a fake RegisterTimer() function that saves the callback function so you can get to it for test.

Take a look at TDD next to the RTOS articles
https://wingman-sw.com/articles/tdd-next-to-the-rtos



James Grenning -- Author of Test-Driven Development for Embedded C
wingman-sw.com
@jwgrenning
TDD for Embedded C
facebook.com/wingman.sw
Mobile: +1 847-630-0998

--
You received this message because you are subscribed to the Google Groups "cpputest" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cpputest+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages