Debian Codegraph @

test/lib/cluster.h

raw
1 /**
2 * Helpers to setup a raft cluster in test fixtures.
3 *
4 * Each raft instance will use its own cowsql FSM, which in turn will be created
5 * using its own config, registry and logger.
6 *
7 * The fixture will also register a VFS and a SQLite replication object for each
8 * raft instance, using "test<i>" as registration name, where <i> is the raft
9 * instance index.
10 *
11 * This fixture is meant to be used as base-line fixture for most higher-level
12 * tests.
13 */
14
15 #ifndef TEST_CLUSTER_H
16 #define TEST_CLUSTER_H
17
18 #include <raft.h>
19 #include <raft/fixture.h>
20
21 #include "../../src/config.h"
22 #include "../../src/fsm.h"
23 #include "../../src/registry.h"
24 #include "../../src/vfs.h"
25
26 #include "../lib/fs.h"
27 #include "../lib/heap.h"
28 #include "../lib/logger.h"
29 #include "../lib/sqlite.h"
30
31 #define N_SERVERS 3
32
33 #define V1 0
34 #define V2 1
35
36 struct server
37 {
38 struct logger logger;
39 struct config config;
40 sqlite3_vfs vfs;
41 struct registry registry;
42 };
43
44 #define FIXTURE_CLUSTER \
45 struct server servers[N_SERVERS]; \
46 struct raft_fsm fsms[N_SERVERS]; \
47 struct raft_fixture cluster;
48
49 #define SETUP_CLUSTER(VERSION) \
50 { \
51 struct raft_configuration _configuration; \
52 unsigned _i; \
53 int _rv; \
54 SETUP_HEAP; \
55 SETUP_SQLITE; \
56 _rv = raft_fixture_init(&f->cluster); \
57 munit_assert_int(_rv, ==, 0); \
58 for (_i = 0; _i < N_SERVERS; _i++) { \
59 SETUP_SERVER(_i, VERSION); \
60 raft_fixture_grow(&f->cluster, &f->fsms[_i]); \
61 } \
62 _rv = raft_fixture_configuration(&f->cluster, N_SERVERS, \
63 &_configuration); \
64 munit_assert_int(_rv, ==, 0); \
65 _rv = raft_fixture_bootstrap(&f->cluster, &_configuration); \
66 munit_assert_int(_rv, ==, 0); \
67 raft_configuration_close(&_configuration); \
68 _rv = raft_fixture_start(&f->cluster); \
69 munit_assert_int(_rv, ==, 0); \
70 }
71
72 #define SETUP_SERVER(I, VERSION) \
73 { \
74 struct server *_s = &f->servers[I]; \
75 struct raft_fsm *_fsm = &f->fsms[I]; \
76 char address[16]; \
77 int _rc; \
78 \
79 test_logger_setup(params, &_s->logger); \
80 \
81 sprintf(address, "%d", I + 1); \
82 \
83 _rc = config__init(&_s->config, I + 1, address); \
84 munit_assert_int(_rc, ==, 0); \
85 \
86 registry__init(&_s->registry, &_s->config); \
87 \
88 _rc = VfsInit(&_s->vfs, _s->config.name); \
89 munit_assert_int(_rc, ==, 0); \
90 _rc = sqlite3_vfs_register(&_s->vfs, 0); \
91 munit_assert_int(_rc, ==, 0); \
92 \
93 _rc = fsm__init(_fsm, &_s->config, &_s->registry); \
94 munit_assert_int(_rc, ==, 0); \
95 }
96
97 #define TEAR_DOWN_CLUSTER \
98 { \
99 int _i; \
100 for (_i = 0; _i < N_SERVERS; _i++) { \
101 TEAR_DOWN_SERVER(_i); \
102 } \
103 raft_fixture_close(&f->cluster); \
104 TEAR_DOWN_SQLITE; \
105 TEAR_DOWN_HEAP; \
106 }
107
108 #define TEAR_DOWN_SERVER(I) \
109 { \
110 struct server *s = &f->servers[I]; \
111 struct raft_fsm *fsm = &f->fsms[I]; \
112 fsm__close(fsm); \
113 registry__close(&s->registry); \
114 sqlite3_vfs_unregister(&s->vfs); \
115 VfsClose(&s->vfs); \
116 config__close(&s->config); \
117 test_logger_tear_down(&s->logger); \
118 }
119
120 #define CLUSTER_CONFIG(I) &f->servers[I].config
121 #define CLUSTER_LOGGER(I) &f->servers[I].logger
122 #define CLUSTER_LEADER(I) &f->servers[I].leader
123 #define CLUSTER_REGISTRY(I) &f->servers[I].registry
124 #define CLUSTER_RAFT(I) raft_fixture_get(&f->cluster, I)
125 #define CLUSTER_LAST_INDEX(I) raft_last_index(CLUSTER_RAFT(I))
126 #define CLUSTER_DISCONNECT(I, J) raft_fixture_disconnect(&f->cluster, I, J)
127 #define CLUSTER_RECONNECT(I, J) raft_fixture_reconnect(&f->cluster, I, J)
128
129 #define CLUSTER_ELECT(I) raft_fixture_elect(&f->cluster, I)
130 #define CLUSTER_DEPOSE raft_fixture_depose(&f->cluster)
131 #define CLUSTER_APPLIED(N) \
132 { \
133 int _i; \
134 for (_i = 0; _i < N_SERVERS; _i++) { \
135 bool done; \
136 done = raft_fixture_step_until_applied(&f->cluster, \
137 _i, N, 1000); \
138 munit_assert_true(done); \
139 } \
140 }
141
142 #define CLUSTER_STEP raft_fixture_step(&f->cluster)
143
144 #define CLUSTER_SNAPSHOT_THRESHOLD(I, N) \
145 raft_set_snapshot_threshold(CLUSTER_RAFT(I), N)
146 #define CLUSTER_SNAPSHOT_TRAILING(I, N) \
147 raft_set_snapshot_trailing(CLUSTER_RAFT(I), N)
148
149 #endif /* TEST_CLUSTER_H */