File: fault.c

package info (click to toggle)
raft 0.22.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,504 kB
  • sloc: ansic: 37,539; makefile: 264; sh: 77; python: 22
file content (69 lines) | stat: -rw-r--r-- 1,320 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
65
66
67
68
69
#include "fault.h"

#include "munit.h"

void FaultInit(struct Fault *f)
{
    f->countdown = -1;
    f->n = -1;
    f->paused = false;
}

bool FaultTick(struct Fault *f)
{
    if (MUNIT_UNLIKELY(f->paused)) {
        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 FaultConfig(struct Fault *f, int delay, int repeat)
{
    f->countdown = delay;
    f->n = repeat;
}

void FaultPause(struct Fault *f)
{
    f->paused = true;
}

void FaultResume(struct Fault *f)
{
    f->paused = false;
}