Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RCUTILS_NO_FAULT_INJECTION() macro. #295

Merged
merged 2 commits into from
Oct 1, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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