-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraft_test_utils.cc
221 lines (195 loc) · 6.1 KB
/
raft_test_utils.cc
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include <sstream>
#include <fcntl.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/times.h>
#include "raft_test_utils.h"
unit_test_suite* unit_test_suite::instance() {
static unit_test_suite _instance;
return &_instance;
}
unit_test_case* unit_test_suite::register_test_case(const char* part, const char* name, unit_test_case* obj) {
std::string spart(part);
std::string sname(name);
for (auto &part : cases) {
if (part.first == spart) {
part.second.push_back({sname, obj});
return obj;
}
}
std::vector<std::pair<std::string, unit_test_case*>> part_cases;
part_cases.push_back({sname, obj});
cases.push_back({spart, part_cases});
return nullptr;
}
bool unit_test_suite::run_case(const std::string &part, const std::string &name, unit_test_case* obj) {
if (is_counting) {
count++;
return true;
}
std::cout << "Test (" << part << "." << name << "): " << std::string(obj->message) << std::endl;
static int clktck = -1;
if (clktck == -1) clktck = sysconf(_SC_CLK_TCK);
clock_t start, end;
tms start_t, end_t;
start = times(&start_t);
obj->body();
end = times(&end_t);
std::cout << "Pass (" << part << "." << name << "). wall-time: " << (double)(end-start) / (double)clktck
<< "s, user-time: " << (double)(end_t.tms_utime - start_t.tms_utime) /(double)clktck
<< "s, sys-time: " << (double)(end_t.tms_stime - start_t.tms_stime) / (double)clktck << "s" << std::endl;
passed++;
return true;
}
bool unit_test_suite::run_part_case(const std::string &spart, const std::string &sname) {
for (auto &part : cases) {
if (part.first == spart) {
for (auto &part_cases : part.second) {
if (sname == "*" || sname == part_cases.first) {
run_case(spart, part_cases.first, part_cases.second);
}
}
}
}
return true;
}
bool unit_test_suite::run(int argc, char** argv) {
auto f = [=]() {
if (argc == 1) {
run_all();
} else if (argc == 2) {
std::string spart(argv[1]);
run_part_case(spart, "*");
} else {
std::string spart(argv[1]), sname(argv[2]);
run_part_case(spart, sname);
}
};
is_counting = true;
count = 0;
passed = 0;
f();
std::cout << "Running " << count << " Tests ..." << std::endl;
is_counting = false;
static int clktck = -1;
if (clktck == -1) clktck = sysconf(_SC_CLK_TCK);
clock_t start, end;
tms start_t, end_t;
start = times(&start_t);
f();
end = times(&end_t);
std::cout << "Pass " << passed << "/" << count << " tests. wall-time: " << (double)(end-start) / (double)clktck
<< "s, user-time: " << (double)(end_t.tms_utime - start_t.tms_utime) /(double)clktck
<< "s, sys-time: " << (double)(end_t.tms_stime - start_t.tms_stime) / (double)clktck << "s" << std::endl;
return true;
}
bool unit_test_suite::run_all() {
for (auto &parts : cases) {
run_part_case(parts.first, "*");
}
return true;
}
marshall& operator<<(marshall &m, const list_command& cmd) {
m << cmd.value;
return m;
}
unmarshall& operator>>(unmarshall &u, list_command& cmd) {
u >> cmd.value;
return u;
}
list_state_machine::list_state_machine() {
store.push_back(0);
num_append_logs = 0;
// Because the log is start from 1, so we push back a value to align with the raft log.
}
void list_state_machine::apply_log(raft_command &cmd) {
std::unique_lock<std::mutex> lock(mtx);
const list_command& list_cmd = dynamic_cast<const list_command&>(cmd);
store.push_back(list_cmd.value);
num_append_logs++;
}
void list_state_machine::apply_snapshot(const std::vector<char>& snapshot) {
std::unique_lock<std::mutex> lock(mtx);
std::string str;
str.assign(snapshot.begin(), snapshot.end());
std::stringstream ss(str);
store = std::vector<int>();
int size;
ss >> size;
for (int i = 0; i < size; i++) {
int temp;
ss >> temp;
store.push_back(temp);
}
}
std::vector<char> list_state_machine::snapshot() {
std::unique_lock<std::mutex> lock(mtx);
std::vector<char> data;
std::stringstream ss;
ss << (int)store.size();
for (auto value : store)
ss << ' ' << value ;
std::string str = ss.str();
data.assign(str.begin(), str.end());
return data;
}
std::vector<rpcs*> create_random_rpc_servers(int num) {
std::vector<rpcs*> res(num);
static int port = 3536;
for (int i = 0; i < num; i++) {
res[i] = new rpcs(port); // use random port
port++;
}
return res;
}
std::vector<rpcc*> create_rpc_clients(const std::vector<rpcs*> &servers) {
int num = servers.size();
std::vector<rpcc*> res(num);
for (int i = 0; i < num; i++) {
struct sockaddr_in sin;
make_sockaddr(std::to_string(servers[i]->port()).c_str(), &sin);
res[i] = new rpcc(sin);
int ret = res[i]->bind();
ASSERT(ret >= 0, "bind fail " << ret);
}
return res;
}
void mssleep(int ms) {
usleep(ms * 1000);
}
int remove_directory(const char *path) {
DIR *d = opendir(path);
size_t path_len = strlen(path);
int r = -1;
if (d) {
struct dirent *p;
r = 0;
while (!r && (p=readdir(d))) {
int r2 = -1;
char *buf;
size_t len;
/* Skip the names "." and ".." as we don't want to recurse on them. */
if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, ".."))
continue;
len = path_len + strlen(p->d_name) + 2;
buf = (char*)malloc(len);
if (buf) {
struct stat statbuf;
snprintf(buf, len, "%s/%s", path, p->d_name);
if (!stat(buf, &statbuf)) {
if (S_ISDIR(statbuf.st_mode))
r2 = remove_directory(buf);
else
r2 = unlink(buf);
}
free(buf);
}
r = r2;
}
closedir(d);
}
if (!r)
r = rmdir(path);
return r;
}