File: fault.c

package info (click to toggle)
dqlite 1.18.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,364 kB
  • sloc: ansic: 57,460; makefile: 336; sh: 243
file content (64 lines) | stat: -rw-r--r-- 1,175 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "fault.h"
#include "munit.h"

void test_fault_init(struct test_fault *f)
{
	f->countdown = -1;
	f->n = -1;
	f->enabled = false;
}

bool test_fault_tick(struct test_fault *f)
{
	if (MUNIT_UNLIKELY(!f->enabled)) {
		return false;
	}

	/* If the initial delay parameter was set to -1, then never fail. This
	 * is the most common case. */
	if (MUNIT_LIKELY(f->countdown < 0)) {
		return false;
	}

	/* If we did not yet reach 'delay' ticks, then just decrease the
	 * countdown.
	 */
	if (f->countdown > 0) {
		f->countdown--;
		return false;
	}

	munit_assert_int(f->countdown, ==, 0);

	/* We reached 'delay' ticks, let's see how many times we have to trigger
	 * the fault, if any. */

	if (f->n < 0) {
		/* Trigger the fault forever. */
		return true;
	}

	if (f->n > 0) {
		/* Trigger the fault at least this time. */
		f->n--;
		return true;
	}

	munit_assert_int(f->n, ==, 0);

	/* We reached 'repeat' ticks, let's stop triggering the fault. */
	f->countdown--;

	return false;
}

void test_fault_config(struct test_fault *f, int delay, int repeat)
{
	f->countdown = delay;
	f->n = repeat;
}

void test_fault_enable(struct test_fault *f)
{
	f->enabled = true;
}