Pigweed
 
Loading...
Searching...
No Matches
pw::thread::test::TestThreadContext Class Reference

#include <test_thread_context.h>

Public Member Functions

 TestThreadContext (const TestThreadContext &)=delete
 
TestThreadContextoperator= (const TestThreadContext &)=delete
 
const Optionsoptions () const
 

Detailed Description

TestThreadContext is a facade class for creating threads for unit tests in a platform independent way. To use it, set pw_thread_TEST_THREAD_CONTEXT_BACKEND to a backend that implements the pw::thread::test::backend::TestThreadContextNative class.

To create a thread for unit testing, instantiate a TestThreadContext, then call options() to obtain a pw::thread::Options. Use that Options to start a Thread. Users must ensure the context's lifespan outlives the thread it creates. Recycling or destroying the context is only allowed if join() is called on the thread first.

pw::Thread test_thread(context.options(), ExampleThreadFunction);
Definition: thread.h:65
Definition: test_thread_context.h:50
const Options & options() const
Definition: test_thread_context.h:61

Threads created with TestThreadContext cannot be configured in any way. Backends should create threads with sufficient resources to execute typical unit tests. Tests for complex scenarios or interactions where e.g. priority matters are not portable, and TestThreadContext may not work for them. Non-portable tests may include backend-specific headers and instantiate thread options for their platforms as required.

Note
Developers should structure their logic so it can be tested without spawning a thread. Unit tests should avoid spawning threads unless absolutely necessary.
Warning
Threads using the TestThreadContext may only be detached if the context has a static lifetime, meaning the context is both never re-used and not destroyed before the end of the lifetime of the application.

Member Function Documentation

◆ options()

const Options & pw::thread::test::TestThreadContext::options ( ) const
inline

pw::thread::test::TestThreadContext returns a pw::thread::Options associated with the this object, which can be used to contruct a thread.

Returns
The default options for testing thread.

The documentation for this class was generated from the following file: