freertos-addons icon indicating copy to clipboard operation
freertos-addons copied to clipboard

FreeRTOS linux port and GoogleTest

Open marcobergamin opened this issue 5 years ago • 5 comments

Hello,

I have added your posix port to my codebase because I wanted to do some TDD using GoogleTest. The first test runs just fine: at the end of the unit tests I can destroy all my tasks and call vTaskEndSheduler().

The problem is that vTaskEndSheduler(), beside stopping the scheduler, it also destroys some variables statically initialized (a pair of mutexes and something else). I've tried to add a new function to initialize again the static variables declared in port.c with the idea of calling it at the beginning of each unit test, without success.

As workaround I could use the GoogleTest --filter functionality to execute each test in a dedicated process, but I don't like this option a lot.

Any idea?

Thanks, Marco

marcobergamin avatar Jul 30 '20 07:07 marcobergamin

I am interesting in using this project in the same way - testing a FreeRTOS codebase on Linux using the port. Did you find a solution to this issue?

matt-duke avatar Jan 19 '22 18:01 matt-duke

There was an extensive conversation regarding vTaskEndScheduler() a while back. The summary is that this is a limitation of FreeRTOS itself. FreeRTOS never really expects that vTaskEndScheduler() will actually be called, which makes sense if you've ever coded a small embedded system.

Reference: https://github.com/michaelbecker/freertos-addons/issues/14#issuecomment-408680073

michaelbecker avatar Jan 19 '22 19:01 michaelbecker

I am interesting in using this project in the same way - testing a FreeRTOS codebase on Linux using the port. Did you find a solution to this issue?

I actually have found a solution. Here the code: https://gist.github.com/marcobergamin/2e77003e7efa2a891a6726b14e8c61fe

Please note that I have just manually copied the code from an offline machine, it could contains typos (if you find them, please leave a comment).

I have built that "alternative" gtest_main for FreeRTOS based on a discussion thread I had found online (unfortunately I have lost the link to the discussion).

I can tell you that with the latest GCC/Posix port it works very well IF:

  1. you don't create threads not managed by the FreeRTOS scheduler (in other words: use only vTaskCreate).
  2. you close all the tasks created inside a unit test at its end.

Link to the POSIX port I am using: https://github.com/FreeRTOS/FreeRTOS-Kernel/tree/main/portable/ThirdParty/GCC/Posix

marcobergamin avatar Jan 20 '22 21:01 marcobergamin

Thanks Marco, that looks very promising! Only 2 typos: <cstint> should be <cstdint> on line 8 and gtest_main function should be renamed gtest_task. I like your approach here. I was originally planning to start and stop FreeRTOS in the setup and teardown of each test case, but it looks like this takes a slightly different approach where the scheduler runs for the entire test suite. For my purposes, that should be ok.

matt-duke avatar Jan 20 '22 22:01 matt-duke

@matt-duke I'm very sorry for the late answer, I've completely forgot to answer your message. First of all thanks for pointing out those typos, I've fixed the gist. Does this solution work well for you so far?

marcobergamin avatar Feb 09 '22 20:02 marcobergamin

Closing because this is not a FreeRTOS add-on issue. This is a known issue with FreeRTOS itself.

michaelbecker avatar Jul 07 '23 14:07 michaelbecker