| 1 |
/** |
| 2 |
* Helpers for setting up a standalone raft instance with a libuv transport. |
| 3 |
*/ |
| 4 |
|
| 5 |
#ifndef TEST_RAFT_H |
| 6 |
#define TEST_RAFT_H |
| 7 |
|
| 8 |
#include <raft.h> |
| 9 |
#include <raft/uv.h> |
| 10 |
#include <uv.h> |
| 11 |
|
| 12 |
#include "../../src/fsm.h" |
| 13 |
#include "../../src/transport.h" |
| 14 |
#include "fs.h" |
| 15 |
#include "logger.h" |
| 16 |
#include "munit.h" |
| 17 |
#include "uv.h" |
| 18 |
|
| 19 |
#define FIXTURE_RAFT \ |
| 20 |
char *dir; \ |
| 21 |
struct uv_loop_s loop; \ |
| 22 |
struct raft_uv_transport raft_transport; \ |
| 23 |
struct raft_io raft_io; \ |
| 24 |
struct raft_fsm fsm; \ |
| 25 |
struct raft raft |
| 26 |
|
| 27 |
#define SETUP_RAFT \ |
| 28 |
{ \ |
| 29 |
int rv2; \ |
| 30 |
f->dir = test_dir_setup(); \ |
| 31 |
test_uv_setup(params, &f->loop); \ |
| 32 |
rv2 = raftProxyInit(&f->raft_transport, &f->loop); \ |
| 33 |
munit_assert_int(rv2, ==, 0); \ |
| 34 |
rv2 = raft_uv_init(&f->raft_io, &f->loop, f->dir, \ |
| 35 |
&f->raft_transport); \ |
| 36 |
munit_assert_int(rv2, ==, 0); \ |
| 37 |
rv2 = fsm__init(&f->fsm, &f->config, &f->registry); \ |
| 38 |
munit_assert_int(rv2, ==, 0); \ |
| 39 |
rv2 = raft_init(&f->raft, &f->raft_io, &f->fsm, 1, "1"); \ |
| 40 |
munit_assert_int(rv2, ==, 0); \ |
| 41 |
} |
| 42 |
|
| 43 |
#define TEAR_DOWN_RAFT \ |
| 44 |
{ \ |
| 45 |
raft_close(&f->raft, NULL); \ |
| 46 |
test_uv_stop(&f->loop); \ |
| 47 |
raft_uv_close(&f->raft_io); \ |
| 48 |
fsm__close(&f->fsm); \ |
| 49 |
test_uv_tear_down(&f->loop); \ |
| 50 |
raftProxyClose(&f->raft_transport); \ |
| 51 |
test_dir_tear_down(f->dir); \ |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Bootstrap the fixture raft instance with a configuration containing only |
| 56 |
* itself. |
| 57 |
*/ |
| 58 |
#define RAFT_BOOTSTRAP \ |
| 59 |
{ \ |
| 60 |
struct raft_configuration configuration; \ |
| 61 |
int rv2; \ |
| 62 |
raft_configuration_init(&configuration); \ |
| 63 |
rv2 = raft_configuration_add(&configuration, 1, "1", \ |
| 64 |
RAFT_VOTER); \ |
| 65 |
munit_assert_int(rv2, ==, 0); \ |
| 66 |
rv2 = raft_bootstrap(&f->raft, &configuration); \ |
| 67 |
munit_assert_int(rv2, ==, 0); \ |
| 68 |
raft_configuration_close(&configuration); \ |
| 69 |
} |
| 70 |
|
| 71 |
#define RAFT_START \ |
| 72 |
{ \ |
| 73 |
int rv2; \ |
| 74 |
rv2 = raft_start(&f->raft); \ |
| 75 |
munit_assert_int(rv2, ==, 0); \ |
| 76 |
} |
| 77 |
|
| 78 |
#endif /* TEST_RAFT_H */ |