Skip to content

Commit d8816ab

Browse files
committed
add multithreads test for RedisCluster
1 parent 619d566 commit d8816ab

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

test/src/sw/redis++/test_main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ int main(int argc, char **argv) {
115115

116116
std::cout << "Pass pipeline and transaction tests" << std::endl;
117117

118-
sw::redis::test::ThreadsTest threads_test(opts);
118+
sw::redis::test::ThreadsTest threads_test(opts, cluster_node_opts);
119119
threads_test.run();
120120

121121
std::cout << "Pass threads tests" << std::endl;

test/src/sw/redis++/threads_test.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <thread>
1919
#include <chrono>
2020
#include <atomic>
21+
#include <memory>
2122
#include "utils.h"
2223

2324
namespace sw {
@@ -26,7 +27,9 @@ namespace redis {
2627

2728
namespace test {
2829

29-
ThreadsTest::ThreadsTest(const ConnectionOptions &opts) : _opts(opts) {}
30+
ThreadsTest::ThreadsTest(const ConnectionOptions &opts,
31+
const ConnectionOptions &cluster_opts) : _opts(opts),
32+
_cluster_opts(cluster_opts) {}
3033

3134
void ThreadsTest::run() {
3235
// 100 * 10000 = 1 million writes
@@ -36,23 +39,32 @@ void ThreadsTest::run() {
3639
// Default pool options: single connection and wait forever.
3740
_test_multithreads(Redis(_opts), thread_num, times);
3841

42+
_test_multithreads(RedisCluster(_cluster_opts), thread_num, times);
43+
3944
// Pool with 10 connections.
4045
ConnectionPoolOptions pool_opts;
4146
pool_opts.size = 10;
4247
_test_multithreads(Redis(_opts, pool_opts), thread_num, times);
4348

49+
_test_multithreads(RedisCluster(_cluster_opts, pool_opts), thread_num, times);
50+
4451
_test_timeout();
4552
}
4653

47-
void ThreadsTest::_test_multithreads(Redis redis, int thread_num, int times) {
54+
template <typename RedisType>
55+
void ThreadsTest::_test_multithreads(RedisType redis, int thread_num, int times) {
4856
std::vector<std::string> keys;
4957
keys.reserve(thread_num);
5058
for (auto idx = 0; idx != thread_num; ++idx) {
5159
auto key = test_key("multi-threads::" + std::to_string(idx));
5260
keys.push_back(key);
5361
}
5462

55-
KeyDeleter deleter(redis, keys.begin(), keys.end());
63+
using DeleterUPtr = std::unique_ptr<KeyDeleterTpl<RedisType>>;
64+
std::vector<DeleterUPtr> deleters;
65+
for (const auto &key : keys) {
66+
deleters.emplace_back(new KeyDeleterTpl<RedisType>(redis, key));
67+
}
5668

5769
std::vector<std::thread> workers;
5870
workers.reserve(thread_num);

test/src/sw/redis++/threads_test.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,19 @@ namespace test {
2727

2828
class ThreadsTest {
2929
public:
30-
explicit ThreadsTest(const ConnectionOptions &opts);
30+
ThreadsTest(const ConnectionOptions &opts, const ConnectionOptions &cluster_opts);
3131

3232
void run();
3333

3434
private:
35-
void _test_multithreads(Redis redis, int threads_num, int times);
35+
template <typename RedisType>
36+
void _test_multithreads(RedisType redis, int threads_num, int times);
3637

3738
void _test_timeout();
3839

3940
ConnectionOptions _opts;
41+
42+
ConnectionOptions _cluster_opts;
4043
};
4144

4245
}

0 commit comments

Comments
 (0)