| 1 |
#include "fault.h" |
| 2 |
#include "munit.h" |
| 3 |
|
| 4 |
void test_fault_init(struct test_fault *f) |
| 5 |
{ |
| 6 |
f->countdown = -1; |
| 7 |
f->n = -1; |
| 8 |
f->enabled = false; |
| 9 |
} |
| 10 |
|
| 11 |
bool test_fault_tick(struct test_fault *f) |
| 12 |
{ |
| 13 |
if (MUNIT_UNLIKELY(!f->enabled)) { |
| 14 |
return false; |
| 15 |
} |
| 16 |
|
| 17 |
/* If the initial delay parameter was set to -1, then never fail. This |
| 18 |
* is the most common case. */ |
| 19 |
if (MUNIT_LIKELY(f->countdown < 0)) { |
| 20 |
return false; |
| 21 |
} |
| 22 |
|
| 23 |
/* If we did not yet reach 'delay' ticks, then just decrease the |
| 24 |
* countdown. |
| 25 |
*/ |
| 26 |
if (f->countdown > 0) { |
| 27 |
f->countdown--; |
| 28 |
return false; |
| 29 |
} |
| 30 |
|
| 31 |
munit_assert_int(f->countdown, ==, 0); |
| 32 |
|
| 33 |
/* We reached 'delay' ticks, let's see how many times we have to trigger |
| 34 |
* the fault, if any. */ |
| 35 |
|
| 36 |
if (f->n < 0) { |
| 37 |
/* Trigger the fault forever. */ |
| 38 |
return true; |
| 39 |
} |
| 40 |
|
| 41 |
if (f->n > 0) { |
| 42 |
/* Trigger the fault at least this time. */ |
| 43 |
f->n--; |
| 44 |
return true; |
| 45 |
} |
| 46 |
|
| 47 |
munit_assert_int(f->n, ==, 0); |
| 48 |
|
| 49 |
/* We reached 'repeat' ticks, let's stop triggering the fault. */ |
| 50 |
f->countdown--; |
| 51 |
|
| 52 |
return false; |
| 53 |
} |
| 54 |
|
| 55 |
void test_fault_config(struct test_fault *f, int delay, int repeat) |
| 56 |
{ |
| 57 |
f->countdown = delay; |
| 58 |
f->n = repeat; |
| 59 |
} |
| 60 |
|
| 61 |
void test_fault_enable(struct test_fault *f) |
| 62 |
{ |
| 63 |
f->enabled = true; |
| 64 |
} |