From 08f25f3ce14c8ec1094885cf2ece72f95943dd7c Mon Sep 17 00:00:00 2001 From: Michel Hidalgo Date: Wed, 30 Sep 2020 17:10:54 -0300 Subject: [PATCH 1/2] Add RCUTILS_NO_FAULT_INJECTION() macro. Signed-off-by: Michel Hidalgo --- include/rcutils/testing/fault_injection.h | 32 +++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/include/rcutils/testing/fault_injection.h b/include/rcutils/testing/fault_injection.h index 34558320..0c35de6a 100644 --- a/include/rcutils/testing/fault_injection.h +++ b/include/rcutils/testing/fault_injection.h @@ -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(count); \ + } while (0) + #ifdef __cplusplus } #endif From c9c33b2f28a571f94a293df36b900d16b010d9e1 Mon Sep 17 00:00:00 2001 From: Michel Hidalgo Date: Wed, 30 Sep 2020 17:56:06 -0300 Subject: [PATCH 2/2] Fix bad variable name. Signed-off-by: Michel Hidalgo --- include/rcutils/testing/fault_injection.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/rcutils/testing/fault_injection.h b/include/rcutils/testing/fault_injection.h index 0c35de6a..d397fd74 100644 --- a/include/rcutils/testing/fault_injection.h +++ b/include/rcutils/testing/fault_injection.h @@ -196,7 +196,7 @@ _rcutils_fault_injection_maybe_fail(void); 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(count); \ + rcutils_fault_injection_set_count(no_fault_injection_count); \ } while (0) #ifdef __cplusplus