Commit c9cb309a authored by zhujiashun's avatar zhujiashun

redis_server_protocol: refine UT

parent 3c4d745b
......@@ -35,7 +35,6 @@
#include "brpc/redis_command.h"
#include "brpc/policy/redis_protocol.h"
#include "bthread/execution_queue.h"
#include "bthread/countdown_event.h"
namespace brpc {
......@@ -182,8 +181,6 @@ int ConsumeTask(RedisConnContext* meta, const RedisMessage& m) {
args, &output, done_guard.release());
if (result == RedisCommandHandler::OK) {
meta->handler_continue = NULL;
} else {
LOG(ERROR) << "Unknown handler result=" << (int)result;
}
} else {
std::string comm;
......@@ -201,8 +198,6 @@ int ConsumeTask(RedisConnContext* meta, const RedisMessage& m) {
it->second->Run(args, &output, done_guard.release());
if (result == RedisCommandHandler::CONTINUE) {
meta->handler_continue = it->second.get();
} else {
LOG(ERROR) << "Unknown handler result=" << (int)result;
}
}
}
......
......@@ -51,7 +51,7 @@ else()
message(FATAL_ERROR "Googletest is not available")
endif()
set(CMAKE_CPP_FLAGS "${DEFINE_CLOCK_GETTIME} -DBRPC_ENABLE_CPU_PROFILER -DBRPC_WITH_GLOG=${WITH_GLOG_VAL} -DGFLAGS_NS=${GFLAGS_NS}")
set(CMAKE_CPP_FLAGS "${DEFINE_CLOCK_GETTIME} -DBRPC_WITH_GLOG=${WITH_GLOG_VAL} -DGFLAGS_NS=${GFLAGS_NS}")
set(CMAKE_CPP_FLAGS "${CMAKE_CPP_FLAGS} -DBTHREAD_USE_FAST_PTHREAD_MUTEX -D__const__= -D_GNU_SOURCE -DUSE_SYMBOLIZE -DNO_TCMALLOC -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS -DUNIT_TEST -Dprivate=public -Dprotected=public -DBVAR_NOT_LINK_DEFAULT_VARIABLES -D__STRICT_ANSI__ -include ${PROJECT_SOURCE_DIR}/test/sstream_workaround.h")
set(CMAKE_CXX_FLAGS "${CMAKE_CPP_FLAGS} -O2 -pipe -Wall -W -fPIC -fstrict-aliasing -Wno-invalid-offsetof -Wno-unused-parameter -fno-omit-frame-pointer")
use_cxx11()
......
......@@ -665,19 +665,23 @@ butil::Mutex s_mutex;
std::unordered_map<std::string, std::string> m;
std::unordered_map<std::string, int64_t> int_map;
void* random_sleep(void *arg) {
google::protobuf::Closure* done = static_cast<google::protobuf::Closure*>(arg);
// [50, 100) ms
int sleep_ms = 50 + butil::fast_rand_less_than(50);
bthread_usleep(sleep_ms * 1000);
done->Run();
struct SleepArgs {
int sleep_ms;
google::protobuf::Closure* done;
};
void* sleep(void *arg) {
SleepArgs* args = static_cast<SleepArgs*>(arg);
bthread_usleep(args->sleep_ms * 1000);
args->done->Run();
delete args;
return NULL;
}
class SetCommandHandler : public brpc::RedisCommandHandler {
public:
SetCommandHandler(bool rand_sleep = false)
: _rand_sleep(rand_sleep) {}
SetCommandHandler(bool sleep = false)
: _sleep(sleep) {}
brpc::RedisCommandHandler::Result Run(const char* args[],
brpc::RedisMessage* output,
......@@ -687,24 +691,29 @@ public:
std::string value = args[2];
m[key] = value;
output->SetStatus("OK");
if (_rand_sleep) {
if (_sleep) {
SleepArgs *args = new SleepArgs;
args->sleep_ms = _sleep_ms;
args->done = done_guard.release();
bthread_t bth;
bthread_start_background(&bth, NULL, random_sleep, done_guard.release());
EXPECT_EQ(0, bthread_start_background(&bth, NULL, sleep, args));
if (_sleep_ms > 20) _sleep_ms -= 20;
}
return brpc::RedisCommandHandler::OK;
}
RedisCommandHandler* New() { _new_count++; return new SetCommandHandler(_rand_sleep); }
RedisCommandHandler* New() { _new_count++; return new SetCommandHandler(_sleep); }
int new_count() { return _new_count; }
private:
int _sleep_ms = 100;
int _new_count = 0;
bool _rand_sleep = false;
bool _sleep = false;
};
class GetCommandHandler : public brpc::RedisCommandHandler {
public:
GetCommandHandler(bool rand_sleep = false)
: _rand_sleep(rand_sleep) {}
GetCommandHandler(bool sleep = false)
: _sleep(sleep) {}
brpc::RedisCommandHandler::Result Run(const char* args[],
brpc::RedisMessage* output,
......@@ -717,24 +726,29 @@ public:
} else {
output->SetNilString();
}
if (_rand_sleep) {
if (_sleep) {
SleepArgs *args = new SleepArgs;
args->sleep_ms = _sleep_ms;
args->done = done_guard.release();
bthread_t bth;
bthread_start_background(&bth, NULL, random_sleep, done_guard.release());
EXPECT_EQ(0, bthread_start_background(&bth, NULL, sleep, args));
if (_sleep_ms > 20) _sleep_ms -= 20;
}
return brpc::RedisCommandHandler::OK;
}
RedisCommandHandler* New() { _new_count++; return new GetCommandHandler(_rand_sleep); }
RedisCommandHandler* New() { _new_count++; return new GetCommandHandler(_sleep); }
int new_count() { return _new_count; }
private:
int _sleep_ms = 100;
int _new_count = 0;
bool _rand_sleep = false;
bool _sleep = false;
};
class IncrCommandHandler : public brpc::RedisCommandHandler {
public:
IncrCommandHandler(bool rand_sleep = false)
: _rand_sleep(rand_sleep) {}
IncrCommandHandler(bool sleep = false)
: _sleep(sleep) {}
brpc::RedisCommandHandler::Result Run(const char* args[],
brpc::RedisMessage* output,
......@@ -745,18 +759,23 @@ public:
value = ++int_map[args[1]];
s_mutex.unlock();
output->SetInteger(value);
if (_rand_sleep) {
if (_sleep) {
SleepArgs *args = new SleepArgs;
args->sleep_ms = _sleep_ms;
args->done = done_guard.release();
bthread_t bth;
bthread_start_background(&bth, NULL, random_sleep, done_guard.release());
EXPECT_EQ(0, bthread_start_background(&bth, NULL, sleep, args));
if (_sleep_ms > 20) _sleep_ms -= 20;
}
return brpc::RedisCommandHandler::OK;
}
RedisCommandHandler* New() { _new_count++; return new IncrCommandHandler(_rand_sleep); }
RedisCommandHandler* New() { _new_count++; return new IncrCommandHandler(_sleep); }
int new_count() { return _new_count; }
private:
int _sleep_ms = 100;
int _new_count = 0;
bool _rand_sleep = false;
bool _sleep = false;
};
class RedisServiceImpl : public brpc::RedisService { };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment