Commit c9cb309a authored by zhujiashun's avatar zhujiashun

redis_server_protocol: refine UT

parent 3c4d745b
...@@ -35,7 +35,6 @@ ...@@ -35,7 +35,6 @@
#include "brpc/redis_command.h" #include "brpc/redis_command.h"
#include "brpc/policy/redis_protocol.h" #include "brpc/policy/redis_protocol.h"
#include "bthread/execution_queue.h" #include "bthread/execution_queue.h"
#include "bthread/countdown_event.h"
namespace brpc { namespace brpc {
...@@ -182,8 +181,6 @@ int ConsumeTask(RedisConnContext* meta, const RedisMessage& m) { ...@@ -182,8 +181,6 @@ int ConsumeTask(RedisConnContext* meta, const RedisMessage& m) {
args, &output, done_guard.release()); args, &output, done_guard.release());
if (result == RedisCommandHandler::OK) { if (result == RedisCommandHandler::OK) {
meta->handler_continue = NULL; meta->handler_continue = NULL;
} else {
LOG(ERROR) << "Unknown handler result=" << (int)result;
} }
} else { } else {
std::string comm; std::string comm;
...@@ -201,8 +198,6 @@ int ConsumeTask(RedisConnContext* meta, const RedisMessage& m) { ...@@ -201,8 +198,6 @@ int ConsumeTask(RedisConnContext* meta, const RedisMessage& m) {
it->second->Run(args, &output, done_guard.release()); it->second->Run(args, &output, done_guard.release());
if (result == RedisCommandHandler::CONTINUE) { if (result == RedisCommandHandler::CONTINUE) {
meta->handler_continue = it->second.get(); meta->handler_continue = it->second.get();
} else {
LOG(ERROR) << "Unknown handler result=" << (int)result;
} }
} }
} }
......
...@@ -51,7 +51,7 @@ else() ...@@ -51,7 +51,7 @@ else()
message(FATAL_ERROR "Googletest is not available") message(FATAL_ERROR "Googletest is not available")
endif() 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_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") 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() use_cxx11()
......
...@@ -665,19 +665,23 @@ butil::Mutex s_mutex; ...@@ -665,19 +665,23 @@ butil::Mutex s_mutex;
std::unordered_map<std::string, std::string> m; std::unordered_map<std::string, std::string> m;
std::unordered_map<std::string, int64_t> int_map; std::unordered_map<std::string, int64_t> int_map;
void* random_sleep(void *arg) { struct SleepArgs {
google::protobuf::Closure* done = static_cast<google::protobuf::Closure*>(arg); int sleep_ms;
// [50, 100) ms google::protobuf::Closure* done;
int sleep_ms = 50 + butil::fast_rand_less_than(50); };
bthread_usleep(sleep_ms * 1000);
done->Run(); void* sleep(void *arg) {
SleepArgs* args = static_cast<SleepArgs*>(arg);
bthread_usleep(args->sleep_ms * 1000);
args->done->Run();
delete args;
return NULL; return NULL;
} }
class SetCommandHandler : public brpc::RedisCommandHandler { class SetCommandHandler : public brpc::RedisCommandHandler {
public: public:
SetCommandHandler(bool rand_sleep = false) SetCommandHandler(bool sleep = false)
: _rand_sleep(rand_sleep) {} : _sleep(sleep) {}
brpc::RedisCommandHandler::Result Run(const char* args[], brpc::RedisCommandHandler::Result Run(const char* args[],
brpc::RedisMessage* output, brpc::RedisMessage* output,
...@@ -687,24 +691,29 @@ public: ...@@ -687,24 +691,29 @@ public:
std::string value = args[2]; std::string value = args[2];
m[key] = value; m[key] = value;
output->SetStatus("OK"); 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_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; 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; } int new_count() { return _new_count; }
private: private:
int _sleep_ms = 100;
int _new_count = 0; int _new_count = 0;
bool _rand_sleep = false; bool _sleep = false;
}; };
class GetCommandHandler : public brpc::RedisCommandHandler { class GetCommandHandler : public brpc::RedisCommandHandler {
public: public:
GetCommandHandler(bool rand_sleep = false) GetCommandHandler(bool sleep = false)
: _rand_sleep(rand_sleep) {} : _sleep(sleep) {}
brpc::RedisCommandHandler::Result Run(const char* args[], brpc::RedisCommandHandler::Result Run(const char* args[],
brpc::RedisMessage* output, brpc::RedisMessage* output,
...@@ -717,24 +726,29 @@ public: ...@@ -717,24 +726,29 @@ public:
} else { } else {
output->SetNilString(); 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_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; 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; } int new_count() { return _new_count; }
private: private:
int _sleep_ms = 100;
int _new_count = 0; int _new_count = 0;
bool _rand_sleep = false; bool _sleep = false;
}; };
class IncrCommandHandler : public brpc::RedisCommandHandler { class IncrCommandHandler : public brpc::RedisCommandHandler {
public: public:
IncrCommandHandler(bool rand_sleep = false) IncrCommandHandler(bool sleep = false)
: _rand_sleep(rand_sleep) {} : _sleep(sleep) {}
brpc::RedisCommandHandler::Result Run(const char* args[], brpc::RedisCommandHandler::Result Run(const char* args[],
brpc::RedisMessage* output, brpc::RedisMessage* output,
...@@ -745,18 +759,23 @@ public: ...@@ -745,18 +759,23 @@ public:
value = ++int_map[args[1]]; value = ++int_map[args[1]];
s_mutex.unlock(); s_mutex.unlock();
output->SetInteger(value); 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_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; 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; } int new_count() { return _new_count; }
private: private:
int _sleep_ms = 100;
int _new_count = 0; int _new_count = 0;
bool _rand_sleep = false; bool _sleep = false;
}; };
class RedisServiceImpl : public brpc::RedisService { }; 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