-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfiber_testsuite.cpp
More file actions
292 lines (219 loc) · 8.1 KB
/
Copy pathfiber_testsuite.cpp
File metadata and controls
292 lines (219 loc) · 8.1 KB
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// Copyright 2018 Stephan Menzel. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#include "mredis/AsyncClient.hpp"
#include "tools/Log.hpp"
#include "tools/Random.hpp"
#include <boost/config.hpp>
#include <boost/asio.hpp>
#include <boost/chrono.hpp>
#include <boost/program_options.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/fiber/all.hpp>
#ifndef _WIN32
#include <sys/resource.h>
#endif
#include <iostream>
#include <cstdlib>
using namespace moose::mredis;
using namespace moose::tools;
std::string server_ip_string;
short port = 6379;
/* This tries to be a fibered stress test for mredis.
Aim is to spot race conditions and problems by having a scenario
with multiple fibers using a single asyncclient instance
*/
struct SetterFiber {
public:
SetterFiber(AsyncClient &n_redis, const unsigned int n_max)
: m_error(false)
, m_stopped(false)
, m_max_value(n_max)
, m_current_value(0)
, m_redis(n_redis) {
}
void stop() noexcept {
m_stopped = true;
}
void operator()() noexcept {
try {
while (!m_stopped && (m_current_value < m_max_value)) {
// I want to wait for incr's response even though I don't need it, to avoid
// just brute force filling the Q without much sense
std::unique_ptr<boost::fibers::promise<boost::int64_t> > prm(new boost::fibers::promise<boost::int64_t>);
boost::fibers::promise<boost::int64_t> *stolen_ptr = prm.get();
m_redis.incr("setter_test_1", [this, stolen_ptr](const RedisMessage &msg) {
if (is_error(msg)) {
std::cerr << "Error setting value: " << std::endl;
std::cerr << boost::diagnostic_information(boost::get<redis_error>(msg)) << std::endl;
this->m_error = true;
redis_error ex = boost::get<redis_error>(msg);
stolen_ptr->set_exception(std::make_exception_ptr(ex));
} else {
stolen_ptr->set_value(boost::get<boost::int64_t>(msg));
}
});
boost::fibers::future<boost::int64_t> fv = prm->get_future();
// Now I think this wait_for would imply a yield... Meaning that other fiber will take over while this one waits
if (fv.wait_for(std::chrono::seconds(3)) == boost::fibers::future_status::timeout) {
std::cerr << "Timeout setting value" << std::endl;
m_error = true;
}
// INCR returns value after increment
if (fv.get() != ++m_current_value) {
std::cerr << "Value not incremented correctly" << std::endl;
m_error = true;
}
}
} catch (const moose_error &merr) {
std::cerr << "Exception caught by SetterFiber: " << boost::diagnostic_information(merr) << std::endl;
m_error = true;
}
}
bool m_error;
private:
bool m_stopped;
const unsigned int m_max_value;
unsigned int m_current_value;
AsyncClient &m_redis;
};
struct GetterFiber {
public:
GetterFiber(AsyncClient &n_redis)
: m_error(false)
, m_stopped(false)
, m_redis(n_redis) {
}
void stop() noexcept {
m_stopped = true;
}
void operator()() noexcept {
try {
boost::this_fiber::sleep_for(std::chrono::milliseconds(10));
boost::int64_t val = 0;
while (!m_stopped) {
std::unique_ptr<boost::fibers::promise<boost::int64_t> > prm(new boost::fibers::promise<boost::int64_t>);
boost::fibers::promise<boost::int64_t> *stolen_ptr = prm.get();
const boost::chrono::high_resolution_clock::time_point start = boost::chrono::high_resolution_clock::now();
m_redis.get("setter_test_1", [this, stolen_ptr](const RedisMessage &msg) {
if (is_error(msg)) {
std::cerr << "Error getting value: " << std::endl;
std::cerr << boost::diagnostic_information(boost::get<redis_error>(msg)) << std::endl;
this->m_error = true;
redis_error ex = boost::get<redis_error>(msg);
stolen_ptr->set_exception(std::make_exception_ptr(ex));
} else if (is_null(msg)) {
std::cout << "Null response, value not set yet" << std::endl;
stolen_ptr->set_value(0);
} else if (is_string(msg)) {
stolen_ptr->set_value(boost::lexical_cast<boost::int64_t>(boost::get<std::string>(msg)));
} else {
stolen_ptr->set_value(boost::get<boost::int64_t>(msg));
}
});
boost::fibers::future<boost::int64_t> fv = prm->get_future();
// Now I think this wait_for would imply a yield... Meaning that other fiber will take over while this one waits
if (fv.wait_for(std::chrono::seconds(3)) == boost::fibers::future_status::timeout) {
std::cerr << "Timeout getting value" << std::endl;
m_error = true;
continue;
}
// Get the value it returned. This should be available now
if (!fv.valid()) {
std::cerr << "Future invalid after wait" << std::endl;
m_error = true;
continue;
}
const boost::int64_t current = fv.get();
const boost::chrono::high_resolution_clock::time_point finish = boost::chrono::high_resolution_clock::now();
if (current < val) {
std::cerr << "Semantic error. Value decreased" << std::endl;
m_error = true;
} else {
// Only randomly output the value so we see things are happening but don't
// botch down the slow windows console
if (current % 500 == 0) {
std::cout << "got after " << (finish - start) << " -> " << current << std::endl;
}
val = current;
}
}
} catch (const moose_error &merr) {
std::cerr << "Exception caught by GetterFiber: " << boost::diagnostic_information(merr) << std::endl;
m_error = true;
}
}
bool m_error;
private:
bool m_stopped;
AsyncClient &m_redis;
};
void testcase_1_get_and_set() {
std::cout << "Running Testcase 1 - Getter and Setter in fibers..." << std::endl;
AsyncClient client(server_ip_string);
client.connect();
// Clean slate
client.del("setter_test_1");
// continuously increase a value in this fiber
SetterFiber setter(client, 10000);
boost::fibers::fiber sf(std::ref(setter));
// get the value in this fiber
GetterFiber getter(client);
boost::fibers::fiber gf(std::ref(getter));
// This would allow the scheduler to start running the fibers. I guess
// boost::this_fiber::yield();
// Wait for the setter to increase 10000 times
std::cout << "Joining setter fiber" << std::endl;
sf.join();
// Tell the getter to stop
getter.stop();
// Wait for it to stop
std::cout << "Joining getter fiber" << std::endl;
gf.join();
// Cleanup
std::cout << "Cleanup" << std::endl;
client.del("setter_test_1");
if (getter.m_error) {
std::cerr << "Getter yielded an error" << std::endl;
}
if (setter.m_error) {
std::cerr << "Setter yielded an error" << std::endl;
}
boost::this_fiber::sleep_for(std::chrono::seconds(1));
}
int main(int argc, char **argv) {
#ifndef _WIN32
// core dumps may be disallowed by parent of this process; change that
struct rlimit core_limits;
core_limits.rlim_cur = core_limits.rlim_max = RLIM_INFINITY;
setrlimit(RLIMIT_CORE, &core_limits);
#endif
moose::tools::init_logging();
namespace po = boost::program_options;
po::options_description desc("redis test options");
desc.add_options()
("help,h", "Print this help message")
("port,p", po::value<short>()->default_value(6379), "use server port")
("server,s", po::value<std::string>()->default_value("127.0.0.1"), "give redis server ip");
try {
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
if (vm.count("help")) {
std::cout << "Behold your options!\n";
std::cout << desc << std::endl;
return EXIT_SUCCESS;
}
server_ip_string = vm["server"].as<std::string>();
port = vm["port"].as<short>();
boost::fibers::use_scheduling_algorithm< boost::fibers::algo::round_robin >();
testcase_1_get_and_set();
std::cout << "All test cases done" << std::endl;
return EXIT_SUCCESS;
} catch (const std::exception &sex) {
std::cerr << "Unexpected exception reached main: " << sex.what() << std::endl;
} catch (...) {
std::cerr << "Unhandled error reached main function. Aborting" << std::endl;
}
return EXIT_FAILURE;
}