Skip to content

Commit

Permalink
Add RCUTILS_NO_FAULT_INJECTION() macro. (#295)
Browse files Browse the repository at this point in the history
Signed-off-by: Michel Hidalgo <[email protected]>
  • Loading branch information
hidmic authored Oct 1, 2020
1 parent 22d5e56 commit 58580e8
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions include/rcutils/testing/fault_injection.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,38 @@ _rcutils_fault_injection_maybe_fail(void);
rcutils_fault_injection_set_count(RCUTILS_FAULT_INJECTION_NEVER_FAIL); \
} while (0)

/**
* \def RCUTILS_NO_FAULT_INJECTION
*
* A convenience macro built around rcutils_fault_injection_set_count() to pause fault
* injection during `code` execution.
* This macro is intended to be used within RCUTILS_FAULT_INJECTION_TEST() blocks.
*
* `code` is executed within a do-while loop and therefore any variables declared within are in
* their own scope block.
*
* Here's a simple example:
* RCUTILS_FAULT_INJECTION_TEST({
* rcl_ret_t ret = rcl_init(argc, argv, options, context);
* if (RCL_RET_OK == ret)
* {
* RCUTILS_NO_FAULT_INJECTION({
* ret = rcl_shutdown(context);
* });
* }
* });
*
* In this example, on successful rcl_init(), rcl_shutdown() is called while ensuring that
* it will not fail due to fault injection.
*/
#define RCUTILS_NO_FAULT_INJECTION(code) \
do { \
int64_t no_fault_injection_count = rcutils_fault_injection_get_count(); \
rcutils_fault_injection_set_count(RCUTILS_FAULT_INJECTION_NEVER_FAIL); \
code; \
rcutils_fault_injection_set_count(no_fault_injection_count); \
} while (0)

#ifdef __cplusplus
}
#endif
Expand Down

0 comments on commit 58580e8

Please sign in to comment.