[{"data":1,"prerenderedAt":26},["ShallowReactive",2],{"page:\u002Fresource-library\u002Fembedded-test-driven-development-with-an-rtos":3},{"path":4,"layout":5,"title":6,"description":7,"ogImage":8,"post":9,"html":25},"\u002Fresource-library\u002Fembedded-test-driven-development-with-an-rtos","post","Embedded test-driven development with an RTOS","Test-driven development with an RTOS: the challenges a real-time operating system adds to embedded unit testing, and strategies that handle them.","\u002Fimages\u002Fresource-library\u002Fembedded-test-driven-development-with-an-rtos\u002Fc-source-vga-palette-code-social.jpg",{"author":10,"published":11,"updated":12,"tags":13,"image":23,"imageAlt":24},"Austin Glaser","2017-04-25",null,[14,17,20],{"name":15,"slug":16},"Testing and quality","testing",{"name":18,"slug":19},"Embedded systems and software","embedded-software",{"name":21,"slug":22},"Design for manufacture","design-for-manufacture","\u002Fimages\u002Fresource-library\u002Fembedded-test-driven-development-with-an-rtos\u002Fc-source-vga-palette-code.webp","Close-up of C source code for VGA palette register writes","\u003Cp>For complex embedded applications, it often makes sense to incorporate a \u003Ca\n  href=\"https:\u002F\u002Fen.wikipedia.org\u002Fwiki\u002FReal-time_operating_system\" target=\"_blank\"\n  rel=\"noopener\">real-time operating system\u003C\u002Fa> (RTOS). Introducing an RTOS into your system helps\n  manage that complexity, but it also comes at the cost of its own challenges.\u003C\u002Fp>\n\u003Cp>Whether or not an RTOS is appropriate for a given application is beyond the scope of this blog\n  post. We assume that you have already made that choice, and now need to manage the development and\n  testing cycle.\u003C\u002Fp>\n\u003Ch2>Challenges\u003C\u002Fh2>\n\u003Cp>Without an RTOS, most embedded applications use one of two major architectures.\u003C\u002Fp>\n\u003Cp>The first contains an infinite loop at the topmost level, which calls several \"work functions.\"\n  These functions generally drive internal state machines, in the process waiting for IO events and\n  performing calculations. I'll refer to this as a \"round-robin\" architecture.\u003C\u002Fp>\n\u003Cp>The second is slightly more complex. It uses a scheduler and some extra abstraction to provide\n  \"events\" (at regular times, when IO becomes available, etc.) to which various modules can react.\n  I'll refer to this as an \"event-driven\" architecture.\u003C\u002Fp>\n\u003Cp>Though round-robin and event-driven architectures require their own testing techniques, they\n  share one critical commonality -- both are \"run-to-completion\" models. That is, whenever a method\n  is invoked (a work function or an event handler), it does its work and then \u003Cem>completes\u003C\u002Fem>.\n  This is extremely convenient for testing, and fits the classic model where the test sets up a\n  certain state, calls the function under test, and then validates the changed state.\u003C\u002Fp>\n\u003Cp>In contrast, most (or all) RTOSes provide threading abstraction. An application developed for\n  this kind of architecture will generally dispatch several virtual threads, each of which waits in\n  its own infinite loop. To facilitate this multitasking, the OS provides blocking functions to wait\n  on various synchronization primitives and IO. This can be extremely convenient from a functional\n  standpoint -- but since most threads don't run to completion, it presents some issues to\n  testability. A test which naively calls the function for such a thread's function will lock up the\n  entire test suite, which is obviously unacceptable behavior.\u003C\u002Fp>\n\u003Ch2>Testing strategies\u003C\u002Fh2>\n\u003Cp>There are a number of possible ways to deal with this particular issue. Which you choose depends\n  on the needs of your particular application.\u003C\u002Fp>\n\u003Cp>Please keep in mind that the code examples below are exemplary only -- they're \u003Cem>not\u003C\u002Fem> ready\n  for a direct copy-paste into your project. You should read and understand them, and adapt them to\n  your particular circumstances.\u003C\u002Fp>\n\u003Ch3>Loop redefinition\u003C\u002Fh3>\n\u003Cp>In this case, we will simply modify the thread function so that it \u003Cem>doesn't\u003C\u002Fem> loop under\n  testing. This is the most obvious (and perhaps simple) way to deal with this issue, but it comes\n  with its share of disadvantages.\u003C\u002Fp>\n\u003Cp>Imagine we have the following simplistic thread:\u003C\u002Fp>\n\u003Cpre>\u003Ccode>\u002F* thing.c *\u002F\n\nvoid thing_thread(void * arg)\n{\n    thing_init(arg);\n\n    while (true) {\n        thing_work(arg);\n    }\n}\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cp>Now, consider the following modification:\u003C\u002Fp>\n\u003Cpre>\u003Ccode>\u002F* loop.h *\u002F\n\n#ifdef TEST\n#   define LOOP\n#else\n#   define LOOP while (true)\n#endif \u002F* ifdef TEST *\u002F\n\n\u002F* thing.c *\u002F\n\n#include \"loop.h\"\n\nvoid thing_thread(void * arg)\n{\n    thing_init(arg);\n\n    LOOP {\n        thing_work(arg);\n    }\n}\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cp>The redefinition of the \u003Ccode>LOOP\u003C\u002Fcode> macro allows us to call \u003Ccode>thing_thread()\u003C\u002Fcode>\n  with impunity from our tests.\u003C\u002Fp>\n\u003Cp>Here's a slightly more complex version, which allows control over the number of loops from the\n  test:\u003C\u002Fp>\n\u003Cpre>\u003Ccode>\u002F* loop.h *\u002F\n\n#ifdef TEST\nextern uint32_t __loop;\nextern uint32_t __n_loops;\n#   define LOOP for (__loop = 0; __loop &lt; __n_loops; __loop++)\n#   define N_LOOPS(N) __n_loops = (N)\n#else\n#   define LOOP while (true)\n#endif \u002F* ifdef TEST *\u002F\n\n\u002F* loop.c *\u002F\n\n#include \"loop.h\"\n\n#ifdef TEST\nuint32_t __loop;\nuint32_t __n_loop;\n#endif \u002F* ifdef TEST *\u002F\n\n\u002F* thing.c *\u002F\n\n#include \"loop.h\"\n\nvoid thing_thread(void * arg)\n{\n    thing_init(arg);\n\n    LOOP {\n        thing_work(arg);\n    }\n}\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cp>This overcomes one of the major disadvantages of the above -- namely, if\n  \u003Ccode>thing_init()\u003C\u002Fcode> changes the test state, it can be hard to make sure\n  \u003Ccode>thing_work()\u003C\u002Fcode> gets called with the right state if the loop only executes once. This is\n  still not a perfect solution, since it doesn't allow quite the same degree of flexibility as some\n  of the other solutions.\u003C\u002Fp>\n\u003Cp>\u003Cstrong>Pros:\u003C\u002Fstrong>\u003C\u002Fp>\n\u003Cul>\n\u003Cli>Extremely simple, just a couple of lines of code\u003C\u002Fli>\u003C\u002Ful>\n\u003Cp>\u003Cstrong>Cons:\u003C\u002Fstrong>\u003C\u002Fp>\n\u003Cul>\n\u003Cli>Production code is different from test code!\u003C\u002Fli>\n\u003Cli>Difficult to control state in the loop body (we always pass through\n  \u003Ccode>thing_init()\u003C\u002Fcode>)\u003C\u002Fli>\u003C\u002Ful>\n\u003Ch3>Loop breaking\u003C\u002Fh3>\n\u003Cp>The point of this strategy is to avoid the most egregious problem from the previous one: namely,\n  that production code gets polluted with test artifacts, and behaves differently when under\n  test!\u003C\u002Fp>\n\u003Cp>\u003Ccode>setjmp()\u003C\u002Fcode> and \u003Ccode>longjmp()\u003C\u002Fcode> are two C functions which provide non-local\n  control flow. A detailed tutorial is out of scope here, but what follows is a brief\n  introduction.\u003C\u002Fp>\n\u003Cp>\u003Ccode>setjmp()\u003C\u002Fcode> sets up a context to which \u003Ccode>longjmp()\u003C\u002Fcode> can later return\n  execution. In effect, a call of \u003Ccode>longjmp(buf, val)\u003C\u002Fcode> will not return locally, but will\n  cause the previous \u003Ccode>setjmp(buf)\u003C\u002Fcode> to return \u003Cem>again\u003C\u002Fem> with \u003Ccode>val\u003C\u002Fcode> as its\n  return code. The first time \u003Ccode>setjmp(buf)\u003C\u002Fcode> is called, it will return 0, allowing this\n  case to be distinguished. More detail can be found \u003Ca\n  href=\"http:\u002F\u002Fstackoverflow.com\u002Fquestions\u002F14685406\u002Fpractical-usage-of-setjmp-and-longjmp-in-c\"\n  target=\"_blank\" rel=\"noopener\">here\u003C\u002Fa>, or you can take a gander at the \u003Ca\n  href=\"http:\u002F\u002Fman7.org\u002Flinux\u002Fman-pages\u002Fman3\u002Flongjmp.3.html\" target=\"_blank\" rel=\"noopener\">man\n  pages\u003C\u002Fa>.\u003C\u002Fp>\n\u003Cp>This is primarily useful when your test already \"mocks\" some of the underlying functions that\n  it'll be using. Mocking itself is a topic for another blog post.\u003C\u002Fp>\n\u003Cp>Consider the first example again, this time with \u003Ccode>thing_work()\u003C\u002Fcode> elaborated a bit:\u003C\u002Fp>\n\u003Cpre>\u003Ccode>\u002F* io.h *\u002F\n\nchar io_get(void);\n\n\u002F* io.c *\u002F\n\nchar io_get(void)\n{\n    \u002F* Do blocking, hardware, and OS-dependent stuff that won't compile on\n     * our development machine *\u002F\n\n    return char_from_io;\n}\n\n\u002F* thing.c *\u002F\n\n#include \"io.h\"\n\nvoid thing_work(void * arg)\n{\n    \u002F* io_get() blocks *\u002F\n    char c = io_get();\n\n    thing_process(c);\n}\n\nvoid thing_thread(void * arg)\n{\n    thing_init(arg);\n\n    while (true) {\n        thing_work(arg);\n    }\n}\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cp>Now, we'll make some modifications to allow it to be tested:\u003C\u002Fp>\n\u003Cpre>\u003Ccode>\u002F* io.h *\u002F\n\nchar io_get(void);\n\n\u002F* io.c *\u002F\n\nchar io_get(void)\n{\n    \u002F* Do blocking, hardware, and OS-dependent stuff that won't compile on\n     * our development machine *\u002F\n\n    return char_from_io;\n}\n\n\u002F* fake_io.h *\u002F\n\n#include \"io.h\"\n\nextern jmp_buf io_get_buf;\n\nvoid io_get_bail_after(uint32_t n);\n\n\u002F* fake_io.c *\u002F\n\n#include &lt;setjmp.h&gt;\n\njmp_buf io_get_buf;\n\nstatic uint32_t io_get_total_calls;\nstatic uint32_t io_get_prev_calls;\n\nint io_get_bail_after(uint32_t n)\n{\n    io_get_prev_calls = 0;\n    io_get_total_calls = n;\n\n    return setjmp(io_get_buf);\n}\n\nchar io_get(void)\n{\n    if (io_get_total_calls &gt;= io_get_prev_calls) {\n        longjmp(io_get_buf, 1);\n    }\n\n    io_get_prev_calls += 1;\n\n    \u002F* Somewhere else, we'd allow the definition of these injected values *\u002F\n    return test_value;\n}\n\n\u002F* thing.c *\u002F\n\n#include \"io.h\"\n\nvoid thing_work(void * arg)\n{\n    \u002F* io_get() blocks *\u002F\n    char c = io_get();\n\n    thing_process(c);\n}\n\nvoid thing_thread(void * arg)\n{\n    thing_init(arg);\n\n    while (true) {\n        thing_work(arg);\n    }\n}\n\n\u002F* test_thing.c *\u002F\n\n#include \"thing.h\"\n#include \"fake_io.h\"\n\nvoid test_thing_does_stuff(void)\n{\n    \u002F* Set up test state *\u002F\n\n    if (io_get_bail_after(5) == 0) {\n        thing_thread(arg);\n    } else {\n        \u002F* Run done, check results *\u002F\n    }\n}\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cp>The above is considerably more complex than what we previously built up. But notice one key\n  point: the code in \u003Ccode>thing.c\u003C\u002Fcode>, \u003Ccode>io.h\u003C\u002Fcode>, and \u003Ccode>io.c\u003C\u002Fcode> is\n  \u003Cem>completely unchanged\u003C\u002Fem>. When the test for \u003Ccode>thing\u003C\u002Fcode> is built, of course,\n  \u003Ccode>io.c\u003C\u002Fcode> will not be included -- instead, \u003Ccode>fake_io.c\u003C\u002Fcode> will. But since the test\n  is not \u003Cem>about\u003C\u002Fem> how \u003Ccode>io.c\u003C\u002Fcode> works and instead about how \u003Ccode>thing.c\u003C\u002Fcode>\n  interacts with the API defined in \u003Ccode>io.h\u003C\u002Fcode>, this is often acceptable. In fact, it's often\n  critical to building tests that can compile and run outside the target hardware.\u003C\u002Fp>\n\u003Cp>As an aside, the above sort of thing is quite a bit easier when using CMock and CException, two\n  tools from the \u003Ca href=\"http:\u002F\u002Fwww.throwtheswitch.org\u002Fceedling\u002F\" target=\"_blank\"\n  rel=\"noopener\">Throw The Switch\u003C\u002Fa> collection. Thusly:\u003C\u002Fp>\n\u003Cpre>\u003Ccode>\u002F* io.h *\u002F\n\nchar io_get(void);\n\n\u002F* io.c *\u002F\n\nchar io_get(void)\n{\n    \u002F* Do blocking, hardware, and OS-dependent stuff that won't compile on\n     * our development machine *\u002F\n\n    return char_from_io;\n}\n\n\u002F* thing.c *\u002F\n\n#include \"io.h\"\n\nvoid thing_work(void * arg)\n{\n    \u002F* io_get() blocks *\u002F\n    char c = io_get();\n\n    thing_process(c);\n}\n\nvoid thing_thread(void * arg)\n{\n    thing_init(arg);\n\n    while (true) {\n        thing_work(arg);\n    }\n}\n\n\u002F* test_thing.c *\u002F\n\n#include \"thing.h\"\n\n#include \"CException.h\"\n#include \"mock_io.h\" \u002F* CMock will generate this and mock_io.c automatically *\u002F\n\nvoid test_thing_thread(void)\n{\n    io_get_ExpectAndReturn('f');\n    io_get_ExpectAndReturn('o');\n    io_get_ExpectAndReturn('o');\n    io_get_ExpectAndThrow(0xDEADBEEF);\n\n    CEXCEPTION_T e = CEXCEPTION_NONE;\n    Try {\n        thing_thread(arg);\n    } Catch (e) {\n    }\n\n    if (e != 0xDEADBEEF) {\n        \u002F* Test failed! Something got wonky and another (or no) exception\n         * thrown\n         *\n         * This test may be overly pedantic, since CMock will generally\n         * detect these cases as a failure to match the expected function\n         * call sequence.\n         *\u002F\n    }\n\n    \u002F* Check the rest of test state *\u002F\n}\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cp>Between CMock's automatic code generation for the mocked \u003Ccode>io\u003C\u002Fcode> module, and CException's\n  hiding of the ugly \u003Ccode>setjmp()\u003C\u002Fcode>\u002F\u003Ccode>longjmp()\u003C\u002Fcode> logic, we get the same result with\n  a great deal less cruft -- and a more readable test to boot!\u003C\u002Fp>\n\u003Cp>\u003Cstrong>Pros:\u003C\u002Fstrong>\u003C\u002Fp>\n\u003Cul>\n\u003Cli>Leaves production code and test code the same (within a given module)\u003C\u002Fli>\u003C\u002Ful>\n\u003Cp>\u003Cstrong>Cons:\u003C\u002Fstrong>\u003C\u002Fp>\n\u003Cul>\n\u003Cli>More complex, especially when not using a framework with appropriate tools\u003C\u002Fli>\n\u003Cli>Difficult to control state in the loop body (we always pass through\n  \u003Ccode>thing_init()\u003C\u002Fcode>)\u003C\u002Fli>\n\u003Cli>Non-local non-linear control flow can be \u003Ca\n  href=\"http:\u002F\u002Fthedailywtf.com\u002Farticles\u002FLongjmp--FOR-SPEED!!!\" target=\"_blank\"\n  rel=\"noopener\">terribly abused\u003C\u002Fa>, and many developers will shy away from it even more strongly\n  than \u003Ccode>goto\u003C\u002Fcode>\u003C\u002Fli>\u003C\u002Ful>\n\u003Ch3>Loop raising\u003C\u002Fh3>\n\u003Cp>This is a strategy which sidesteps the looping problem rather than dealing with it directly. With\n  that said, it can isolate loop-aware tests to one section of your project, and allow a significant\n  amount of code reuse.\u003C\u002Fp>\n\u003Cp>Again, we return to the original example:\u003C\u002Fp>\n\u003Cpre>\u003Ccode>\u002F* thing.c *\u002F\n\nvoid thing_thread(void * arg)\n{\n    thing_init(arg);\n\n    while (true) {\n        thing_work(arg);\n    }\n}\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cp>In the example below, we elevate the abstraction of a thread to that of a 'task', which has an\n  initialization and a body:\u003C\u002Fp>\n\u003Cpre>\u003Ccode>\u002F* task.h *\u002F\n\ntypedef void (*task_func_t)(void * context);\n\ntypedef struct {\n    task_func_t init;\n    task_func_t work;\n    void * context;\n} task_t;\n\nvoid task_start(task_t const * task);\n\n\u002F* task.c *\u002F\n\nstatic void task_thread(task_t const * task)\n{\n    task-&gt;init(task-&gt;context);\n\n    while (true) {\n        task-&gt;work(task-&gt;context);\n    }\n}\n\nvoid task_start(task_t const * task)\n{\n    \u002F* Do some os-dependent stuff to start task_thread as a thread, with\n    task as its argument *\u002F\n}\n\n\u002F* thing.h *\u002F\n\n#include \"task.h\"\n\nextern task_t const * thing_task;\n\n\u002F* thing.c *\u002F\n\nstatic void thing_init(void * context)\n{\n    \u002F* Do initialization *\u002F\n}\n\nstatic void thing_work(void * context)\n{\n    \u002F* Do periodic work *\u002F\n}\n\nstatic const task_t _thing_task = {\n    .init = thing_init,\n    .work = thing_work,\n    .context = something\n};\n\ntask_t const * thing_task = &amp;_thing_task;\n\n\u002F* test_thing.c *\u002F\n\n#include \"thing.h\"\n\n#include \"task.h\"\n\n\u002F* There would be several of these *\u002F\nvoid test_thing(void)\n{\n    \u002F* Set up test state, probably setting variables in thing_task-&gt;context *\u002F\n\n    thing_task-&gt;work(thing_task-&gt;context);\n\n    \u002F* Validate changed state *\u002F\n}\n\n\u002F* main.c *\u002F\n\n#include \"task.h\"\n#include \"thing.h\"\n\nvoid main(void)\n{\n    task_start(thing_task);\n\n    while (true) {\n        \u002F* Go to sleep, blink an LED, or something else *\u002F\n    }\n}\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cp>By adding the \u003Ccode>task\u003C\u002Fcode> module, we can actually \u003Cem>simplify\u003C\u002Fem> other modules which use\n  the standard init\u002Fwork infinite loop architecture. We separate logical portions of the\n  project.\u003C\u002Fp>\n\u003Cp>The test for the \u003Ccode>task\u003C\u002Fcode> module itself \u003Cem>will\u003C\u002Fem> have to deal with the same looping\n  problem, but it's both isolated and much simpler than a module doing meaningful work is. The test\n  for the \u003Ccode>thing\u003C\u002Fcode> module can focus on testing its functionality, calling\n  \u003Ccode>thing_work()\u003C\u002Fcode> as many or as few times as necessary without being forced to call\n  \u003Ccode>thing_init()\u003C\u002Fcode>. And the test for \u003Ccode>main\u003C\u002Fcode> can simply ensure that it starts\n  whatever tasks are necessary to form the final application.\u003C\u002Fp>\n\u003Cp>\u003Cstrong>Pros:\u003C\u002Fstrong>\u003C\u002Fp>\n\u003Cul>\n\u003Cli>Reasonably simple, depending on the \u003Ccode>task\u003C\u002Fcode> module. What I've laid out above is a\n  functional, if bare-bones, implementation, assuming an appropriate OS-dependent definition for\n  \u003Ccode>task_start()\u003C\u002Fcode>\u003C\u002Fli>\n\u003Cli>Leaves production code and test code the same (within a given module)\u003C\u002Fli>\n\u003Cli>No non-local control flow\u003C\u002Fli>\n\u003Cli>More straightforward control of test state\u003C\u002Fli>\u003C\u002Ful>\n\u003Cp>\u003Cstrong>Cons:\u003C\u002Fstrong>\u003C\u002Fp>\n\u003Cul>\n\u003Cli>Still requires another loop-management strategy for testing \u003Ccode>task\u003C\u002Fcode> and possibly\n  \u003Ccode>main\u003C\u002Fcode>\u003C\u002Fli>\n\u003Cli>\u003Cp>Requires function pointers, which can cause issues for stack-usage profiling and (possibly)\n  safety. It's worth noting, though, that most or all schedulers in C will operate based on function\n  pointers\u003C\u002Fp>\n\u003Cp>Keep in mind that as defined here, task deals with \u003Ccode>const\u003C\u002Fcode> pointers, which can\n  mitigate the danger of passing function pointers. Depending on your architecture and linker\n  settings, it's entirely possible that the entire \u003Ccode>thing_task\u003C\u002Fcode> struct could end up in\n  program memory, where accidental modification will be much harder\u003C\u002Fp>\u003C\u002Fli>\u003C\u002Ful>\n\u003Ch2>Conclusion\u003C\u002Fh2>\n\u003Cp>As you can see, this is a complex issue. Any strategy will have its share of tradeoffs, and if\n  you plan to implement any of them you'll have to think critically about what fits for your own\n  situation. Additionally, this is by no means an exhaustive list of possible strategies. Your\n  mileage may vary.\u003C\u002Fp>",1795194793298]