rcutils  master
C API providing common utilities and data structures.
fault_injection.h
Go to the documentation of this file.
1 // Copyright 2020 Open Source Robotics Foundation, Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef RCUTILS__TESTING__FAULT_INJECTION_H_
16 #define RCUTILS__TESTING__FAULT_INJECTION_H_
17 #include <stdbool.h>
18 #include <stdio.h>
19 #include <stdint.h>
20 
21 #include "rcutils/macros.h"
23 
24 #ifdef __cplusplus
25 extern "C"
26 {
27 #endif
28 
29 #define RCUTILS_FAULT_INJECTION_NEVER_FAIL -1
30 
31 #define RCUTILS_FAULT_INJECTION_FAIL_NOW 0
32 
35 bool
37 
61 void
62 rcutils_fault_injection_set_count(int_least64_t count);
63 
72 int_least64_t
74 
83 int_least64_t
85 
102 #define RCUTILS_FAULT_INJECTION_MAYBE_RETURN_ERROR(return_value_on_error) \
103  if (RCUTILS_FAULT_INJECTION_FAIL_NOW == _rcutils_fault_injection_maybe_fail()) { \
104  printf( \
105  "%s:%d Injecting fault and returning " #return_value_on_error "\n", __FILE__, __LINE__); \
106  return return_value_on_error; \
107  }
108 
126 #define RCUTILS_FAULT_INJECTION_MAYBE_FAIL(failure_code) \
127  if (RCUTILS_FAULT_INJECTION_FAIL_NOW == _rcutils_fault_injection_maybe_fail()) { \
128  printf( \
129  "%s:%d Injecting fault and executing " #failure_code "\n", __FILE__, __LINE__); \
130  failure_code; \
131  }
132 
160 #define RCUTILS_FAULT_INJECTION_TEST(code) \
161  do { \
162  int fault_injection_count = 0; \
163  do { \
164  rcutils_fault_injection_set_count(fault_injection_count++); \
165  code; \
166  } while (!rcutils_fault_injection_is_test_complete()); \
167  rcutils_fault_injection_set_count(RCUTILS_FAULT_INJECTION_NEVER_FAIL); \
168  } while (0)
169 
194 #define RCUTILS_NO_FAULT_INJECTION(code) \
195  do { \
196  int64_t no_fault_injection_count = rcutils_fault_injection_get_count(); \
197  rcutils_fault_injection_set_count(RCUTILS_FAULT_INJECTION_NEVER_FAIL); \
198  code; \
199  rcutils_fault_injection_set_count(no_fault_injection_count); \
200  } while (0)
201 
202 #ifdef __cplusplus
203 }
204 #endif
205 
206 #endif // RCUTILS__TESTING__FAULT_INJECTION_H_
rcutils_fault_injection_is_test_complete
bool rcutils_fault_injection_is_test_complete(void)
macros.h
RCUTILS_WARN_UNUSED
#define RCUTILS_WARN_UNUSED
A macro to make the compiler warn when the return value of a function is not used.
Definition: macros.h:27
visibility_control.h
_rcutils_fault_injection_maybe_fail
int_least64_t _rcutils_fault_injection_maybe_fail(void)
Implementation of fault injection decrementer.
RCUTILS_PUBLIC
#define RCUTILS_PUBLIC
Definition: visibility_control.h:23
rcutils_fault_injection_set_count
void rcutils_fault_injection_set_count(int_least64_t count)
Atomically set the fault injection counter.
rcutils_fault_injection_get_count
int_least64_t rcutils_fault_injection_get_count(void)
Atomically get the fault injection counter value.