test.cpp
8.48 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
* Redox - A modern, asynchronous, and wicked fast C++11 client for Redis
*
* https://github.com/hmartiro/redox
*
* Copyright 2015 - Hayk Martirosyan <hayk.mart@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <iostream>
#include <gtest/gtest.h>
#include "redox.hpp"
namespace {
using namespace std;
using redox::Redox;
using redox::Command;
// ------------------------------------------
// The fixture for testing class Redox.
// ------------------------------------------
class RedoxTest : public ::testing::Test {
protected:
Redox rdx;
RedoxTest() {}
void connect() {
// Connect to the server
rdx.connect("localhost", 6379);
// Clear all keys used by the tests here
rdx.command({"DEL", "redox_test:a"});
}
virtual ~RedoxTest() {}
// CV and counter to wait for async commands to complete
atomic_int cmd_count = {0};
condition_variable cmd_waiter;
mutex cmd_waiter_lock;
// To make the callback code nicer
template <class ReplyT> using Callback = std::function<void(Command<ReplyT> &)>;
/**
* Helper function that returns a command callback to print out the
* command/reply and to test the reply against the provided value.
*/
template <class ReplyT> Callback<ReplyT> check(const ReplyT &value) {
cmd_count++;
return [this, value](Command<ReplyT> &c) {
EXPECT_TRUE(c.ok());
if (c.ok())
EXPECT_EQ(value, c.reply());
cmd_count--;
cmd_waiter.notify_all();
};
}
/**
* Wrapper for the callback that also prints out the command.
*/
template <class ReplyT> Callback<ReplyT> print(Callback<ReplyT> callback) {
return [callback](Command<ReplyT> &c) {
if (c.ok())
cout << "[ASYNC] " << c.cmd() << ": " << c.reply() << endl;
callback(c);
};
}
/**
* Combination of print and check for simplicity.
*/
template <class ReplyT> Callback<ReplyT> print_and_check(const ReplyT &value) {
return print<ReplyT>(check<ReplyT>(value));
}
/**
* Check the error
*/
template <class ReplyT> Callback<ReplyT> print_and_check_error(const ReplyT &value) {
cmd_count++;
return [this, value](Command<ReplyT> &c) {
EXPECT_FALSE(c.ok());
EXPECT_FALSE(c.lastError().empty());
// EXPECT_EQ(value, c.reply());
cout << c.cmd() << ": " << c.lastError() << endl;
cmd_count--;
cmd_waiter.notify_all();
};
}
/**
* Wait until all async commands that used check() as a callback
* complete.
*/
void wait_for_replies() {
unique_lock<mutex> ul(cmd_waiter_lock);
cmd_waiter.wait(ul, [this] { return (cmd_count == 0); });
rdx.disconnect();
}
template <class ReplyT> void check_sync(Command<ReplyT> &c, const ReplyT &value) {
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.reply(), value);
c.free();
}
template <class ReplyT> void print_and_check_sync(Command<ReplyT> &c, const ReplyT &value) {
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.reply(), value);
cout << "[SYNC] " << c.cmd() << ": " << c.reply() << endl;
c.free();
}
/**
* Check the error
*/
template <class ReplyT> void print_and_check_error_sync(Command<ReplyT> &c, const ReplyT &value) {
EXPECT_FALSE(c.ok());
EXPECT_FALSE(c.lastError().empty());
// EXPECT_EQ(value, c.reply());
cout << c.cmd() << ": " << c.lastError() << endl;
}
};
// -------------------------------------------
// Core unit tests - asynchronous
// -------------------------------------------
TEST_F(RedoxTest, TestConnection) { EXPECT_TRUE(rdx.connect("localhost", 6379)); }
TEST_F(RedoxTest, TestConnectionFailure) { EXPECT_FALSE(rdx.connect("localhost", 6380)); }
TEST_F(RedoxTest, GetSet) {
connect();
rdx.command<string>({"SET", "redox_test:a", "apple"}, print_and_check<string>("OK"));
rdx.command<string>({"GET", "redox_test:a"}, print_and_check<string>("apple"));
wait_for_replies();
}
TEST_F(RedoxTest, Delete) {
connect();
rdx.command<string>({"SET", "redox_test:a", "apple"}, print_and_check<string>("OK"));
rdx.command<int>({"DEL", "redox_test:a"}, print_and_check(1));
rdx.command<nullptr_t>({"GET", "redox_test:a"}, check(nullptr));
wait_for_replies();
}
TEST_F(RedoxTest, Incr) {
connect();
int count = 100;
for (int i = 0; i < count; i++) {
rdx.command<int>({"INCR", "redox_test:a"}, check(i + 1));
}
rdx.command<string>({"GET", "redox_test:a"}, print_and_check(to_string(count)));
wait_for_replies();
}
TEST_F(RedoxTest, Delayed) {
connect();
rdx.commandDelayed<int>({"INCR", "redox_test:a"}, check(1), 0.1);
this_thread::sleep_for(chrono::milliseconds(150));
rdx.command<string>({"GET", "redox_test:a"}, print_and_check(to_string(1)));
wait_for_replies();
}
TEST_F(RedoxTest, Loop) {
connect();
int count = 0;
int target_count = 20;
double dt = 0.005;
Command<int> &cmd = rdx.commandLoop<int>(
{"INCR", "redox_test:a"}, [this, &count](Command<int> &c) { check(++count)(c); }, dt);
double wait_time = dt * (target_count - 0.5);
this_thread::sleep_for(std::chrono::duration<double>(wait_time));
cmd.free();
rdx.command<string>({"GET", "redox_test:a"}, print_and_check(to_string(target_count)));
wait_for_replies();
}
TEST_F(RedoxTest, GetSetError) {
connect();
rdx.command<string>({"SET", "redox_test:a", "apple"}, print_and_check<string>("OK"));
rdx.command<int>({"GET", "redox_test:a"}, print_and_check_error<int>(3));
wait_for_replies();
}
// -------------------------------------------
// Core unit tests - synchronous
// -------------------------------------------
TEST_F(RedoxTest, GetSetSync) {
connect();
print_and_check_sync<string>(rdx.commandSync<string>({"SET", "redox_test:a", "apple"}), "OK");
print_and_check_sync<string>(rdx.commandSync<string>({"GET", "redox_test:a"}), "apple");
rdx.disconnect();
}
TEST_F(RedoxTest, DeleteSync) {
connect();
print_and_check_sync<string>(rdx.commandSync<string>({"SET", "redox_test:a", "apple"}), "OK");
print_and_check_sync(rdx.commandSync<int>({"DEL", "redox_test:a"}), 1);
check_sync(rdx.commandSync<nullptr_t>({"GET", "redox_test:a"}), nullptr);
rdx.disconnect();
}
TEST_F(RedoxTest, IncrSync) {
connect();
int count = 100;
for (int i = 0; i < count; i++) {
check_sync(rdx.commandSync<int>({"INCR", "redox_test:a"}), i + 1);
}
print_and_check_sync(rdx.commandSync<string>({"GET", "redox_test:a"}), to_string(count));
rdx.disconnect();
}
TEST_F(RedoxTest, GetSetSyncError) {
connect();
print_and_check_sync<string>(rdx.commandSync<string>({"SET", "redox_test:a", "apple"}), "OK");
print_and_check_error_sync<int>(rdx.commandSync<int>({"GET", "redox_test:a"}), 3);
rdx.disconnect();
}
TEST_F(RedoxTest, MultithreadedCRUD) {
connect();
int create_count(0);
int delete_count(0);
int createExcCount(0);
int deleteExcCount(0);
std::mutex startMutex;
bool start = false;
std::condition_variable start_cv;
const int count = 10000;
std::thread create_thread([&]() {
{
std::unique_lock<std::mutex> lock(startMutex);
start_cv.wait(lock, [&]() { return start; });
}
for (int i = 0; i < count; ++i) {
try {
rdx.commandSync<string>({"SET", "redox_test:mt", "create"});
}
catch (...) {
createExcCount++;
}
create_count++;
}
});
std::thread delete_thread([&]() {
{
std::unique_lock<std::mutex> lock(startMutex);
start_cv.wait(lock, [&]() { return start; });
}
for (int i = 0; i < count; ++i) {
try {
rdx.commandSync<int>({"DEL", "redox_test:mt"});
}
catch (...) {
deleteExcCount++;
}
delete_count++;
}
});
// Start threads
{
std::lock_guard<std::mutex> lock(startMutex);
start = true;
}
start_cv.notify_all();
// Wait for threads to finish
create_thread.join();
delete_thread.join();
EXPECT_EQ(count, create_count);
EXPECT_EQ(count, delete_count);
}
// -------------------------------------------
// End tests
// -------------------------------------------
} // namespace
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}