File: test_uv_timer.c

package info (click to toggle)
dqlite 1.18.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,372 kB
  • sloc: ansic: 57,583; makefile: 336; sh: 243
file content (115 lines) | stat: -rw-r--r-- 2,525 bytes parent folder | download
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include "../../../src/raft/uv.h"
#include "../../lib/runner.h"
#include "../lib/uv.h"

#include <unistd.h>

/******************************************************************************
 *
 * Fixture with a libuv-based raft_io instance.
 *
 *****************************************************************************/

struct fixture
{
    FIXTURE_UV_DEPS;
    FIXTURE_UV;
};

/******************************************************************************
 *
 * Set up and tear down.
 *
 *****************************************************************************/

static void *setUp(const MunitParameter params[], void *user_data)
{
    struct fixture *f = munit_malloc(sizeof *f);
    SETUP_UV_DEPS;
    SETUP_UV;
    return f;
}

static void tearDown(void *data)
{
    struct fixture *f = data;
    if (f == NULL) {
        return;
    }
    TEAR_DOWN_UV;
    TEAR_DOWN_UV_DEPS;
    free(f);
}

SUITE(timer)

struct test_timer {
    struct raft_timer timer;
    int target, count;
    bool success;
};

void callback(struct raft_timer *t) {
    struct test_timer *timer = (struct test_timer *)t;
    timer->count++;
    if (timer->count >= timer->target) {
        timer->success = true;
    }
}

TEST(timer, once, setUp, tearDown, 0, NULL)
{
    int rv;
    struct fixture *f = data;
    struct test_timer timer = {
        .target = 1,
    };

    rv = UvTimerStart(&f->io, &timer.timer, 100, 0, callback);
    munit_assert_int(rv, ==, 0);
    LOOP_RUN_UNTIL(&timer.success);

    rv = UvTimerStop(&f->io, &timer.timer);
    munit_assert_int(rv, ==, 0);

    return MUNIT_OK;
}

TEST(timer, repeated, setUp, tearDown, 0, NULL)
{
    int rv;
    struct fixture *f = data;
    struct test_timer timer = {
        .target = 5,
    };
    rv = UvTimerStart(&f->io, &timer.timer, 100, 100, callback);
    munit_assert_int(rv, ==, 0);
    LOOP_RUN_UNTIL(&timer.success);

    rv = UvTimerStop(&f->io, &timer.timer);
    munit_assert_int(rv, ==, 0);

    return MUNIT_OK;
}

TEST(timer, stop, setUp, tearDown, 0, NULL)
{
    int rv;
    struct fixture *f = data;
    struct test_timer timer = {
        .target = 2,
    };
    rv = UvTimerStart(&f->io, &timer.timer, 100, 100, callback);
    munit_assert_int(rv, ==, 0);
    LOOP_RUN_UNTIL(&timer.success);

    rv = UvTimerStop(&f->io, &timer.timer);
    munit_assert_int(rv, ==, 0);

    rv = uv_run(&f->loop, UV_RUN_ONCE);
    if (rv < 0) {
        munit_errorf("uv_run: %s (%d)", uv_strerror(rv), rv);
    }
    munit_assert_int(rv, ==, 0);
    return MUNIT_OK;
}