jamesge commented on a change in pull request #972: Redis server protocol URL: https://github.com/apache/incubator-brpc/pull/972#discussion_r349551720
########## File path: test/brpc_redis_unittest.cpp ########## @@ -547,4 +549,356 @@ TEST_F(RedisTest, quote_and_escape) { request.Clear(); } +TEST_F(RedisTest, codec) { + butil::Arena arena; + // status + { + brpc::RedisMessage r; + butil::IOBuf buf; + ASSERT_TRUE(r.set_status("OK", &arena)); + ASSERT_TRUE(r.SerializeToIOBuf(&buf)); + ASSERT_STREQ(buf.to_string().c_str(), "+OK\r\n"); + ASSERT_STREQ(r.c_str(), "OK"); + r.Clear(); + brpc::ParseError err = r.ConsumePartialIOBuf(buf, &arena); + ASSERT_EQ(err, brpc::PARSE_OK); + ASSERT_TRUE(r.is_string()); + ASSERT_STREQ("OK", r.c_str()); + } + // error + { + brpc::RedisMessage r; + butil::IOBuf buf; + ASSERT_TRUE(r.set_error("not exist \'key\'", &arena)); + ASSERT_TRUE(r.SerializeToIOBuf(&buf)); + ASSERT_STREQ(buf.to_string().c_str(), "-not exist \'key\'\r\n"); + r.Clear(); + brpc::ParseError err = r.ConsumePartialIOBuf(buf, &arena); + ASSERT_EQ(err, brpc::PARSE_OK); + ASSERT_TRUE(r.is_error()); + ASSERT_STREQ("not exist \'key\'", r.error_message()); + } + // string + { + brpc::RedisMessage r; + butil::IOBuf buf; + ASSERT_TRUE(r.set_nil_string()); + ASSERT_TRUE(r.SerializeToIOBuf(&buf)); + ASSERT_STREQ(buf.to_string().c_str(), "$-1\r\n"); + r.Clear(); + brpc::ParseError err = r.ConsumePartialIOBuf(buf, &arena); + ASSERT_EQ(err, brpc::PARSE_OK); + ASSERT_TRUE(r.is_nil()); + + r.Clear(); + ASSERT_TRUE(r.set_bulk_string("abcde'hello world", &arena)); + ASSERT_TRUE(r.SerializeToIOBuf(&buf)); + ASSERT_STREQ(buf.to_string().c_str(), "$17\r\nabcde'hello world\r\n"); + ASSERT_STREQ(r.c_str(), "abcde'hello world"); + r.Clear(); + err = r.ConsumePartialIOBuf(buf, &arena); + ASSERT_EQ(err, brpc::PARSE_OK); + ASSERT_TRUE(r.is_string()); + ASSERT_STREQ(r.c_str(), "abcde'hello world"); + } + // integer + { + brpc::RedisMessage r; + butil::IOBuf buf; + int t = 2; + int input[] = { -1, 1234567 }; + const char* output[] = { ":-1\r\n", ":1234567\r\n" }; + for (int i = 0; i < t; ++i) { + r.Clear(); + ASSERT_TRUE(r.set_integer(input[i])); + ASSERT_TRUE(r.SerializeToIOBuf(&buf)); + ASSERT_STREQ(buf.to_string().c_str(), output[i]); + r.Clear(); + brpc::ParseError err = r.ConsumePartialIOBuf(buf, &arena); + ASSERT_EQ(err, brpc::PARSE_OK); + ASSERT_TRUE(r.is_integer()); + ASSERT_EQ(r.integer(), input[i]); + } + } + // array + { + brpc::RedisMessage r; + butil::IOBuf buf; + ASSERT_TRUE(r.set_array(3, &arena)); + brpc::RedisMessage& sub_reply = r[0]; + sub_reply.set_array(2, &arena); + sub_reply[0].set_bulk_string("hello, it's me", &arena); + sub_reply[1].set_integer(422); + r[1].set_bulk_string("To go over everything", &arena); + r[2].set_integer(1); + ASSERT_TRUE(r[3].is_nil()); + ASSERT_TRUE(r.SerializeToIOBuf(&buf)); + ASSERT_STREQ(buf.to_string().c_str(), + "*3\r\n*2\r\n$14\r\nhello, it's me\r\n:422\r\n$21\r\n" + "To go over everything\r\n:1\r\n"); + r.Clear(); + ASSERT_EQ(r.ConsumePartialIOBuf(buf, &arena), brpc::PARSE_OK); + ASSERT_TRUE(r.is_array()); + ASSERT_EQ(3ul, r.size()); + ASSERT_TRUE(r[0].is_array()); + ASSERT_EQ(2ul, r[0].size()); + ASSERT_TRUE(r[0][0].is_string()); + ASSERT_STREQ(r[0][0].c_str(), "hello, it's me"); + ASSERT_TRUE(r[0][1].is_integer()); + ASSERT_EQ(r[0][1].integer(), 422); + ASSERT_TRUE(r[1].is_string()); + ASSERT_STREQ(r[1].c_str(), "To go over everything"); + ASSERT_TRUE(r[2].is_integer()); + ASSERT_EQ(1, r[2].integer()); + + r.Clear(); + // nil array + ASSERT_TRUE(r.set_array(-1, &arena)); + ASSERT_TRUE(r.SerializeToIOBuf(&buf)); + ASSERT_STREQ(buf.to_string().c_str(), "*-1\r\n"); + ASSERT_EQ(r.ConsumePartialIOBuf(buf, &arena), brpc::PARSE_OK); + ASSERT_TRUE(r.is_nil()); + } +} + +butil::Mutex s_mutex; +std::unordered_map<std::string, std::string> m; +std::unordered_map<std::string, int64_t> int_map; + +class SetCommandHandler : public brpc::RedisCommandHandler { +public: + brpc::RedisCommandResult Run(const std::vector<const char*>& args, + brpc::RedisMessage* output, butil::Arena* arena) { + std::string key = args[1]; + std::string value = args[2]; + m[key] = value; + output->set_status("OK", arena); + return brpc::REDIS_COMMAND_OK; + } + RedisCommandHandler* New() { new_count++; return new SetCommandHandler; } + int new_count = 0; +}; + +class GetCommandHandler : public brpc::RedisCommandHandler { +public: + brpc::RedisCommandResult Run(const std::vector<const char*>& args, + brpc::RedisMessage* output, butil::Arena* arena) { + std::string key = args[1]; + auto it = m.find(key); + if (it != m.end()) { + output->set_bulk_string(it->second, arena); + } else { + output->set_nil_string(); + } + return brpc::REDIS_COMMAND_OK; + } + RedisCommandHandler* New() { new_count++; return new GetCommandHandler; } + int new_count = 0; +}; + +class IncrCommandHandler : public brpc::RedisCommandHandler { +public: + brpc::RedisCommandResult Run(const std::vector<const char*>& args, + brpc::RedisMessage* output, butil::Arena* arena) { + int64_t value; + s_mutex.lock(); + value = ++int_map[args[1]]; + s_mutex.unlock(); + output->set_integer(value); + return brpc::REDIS_COMMAND_OK; + } + RedisCommandHandler* New() { new_count++; return new IncrCommandHandler; } + int new_count = 0; +}; + +class RedisServiceImpl : public brpc::RedisService { }; + +TEST_F(RedisTest, server_sanity) { + brpc::Server server; + brpc::ServerOptions server_options; + RedisServiceImpl* rsimpl = new RedisServiceImpl; + GetCommandHandler *gh = new GetCommandHandler; + SetCommandHandler *sh = new SetCommandHandler; + IncrCommandHandler *ih = new IncrCommandHandler; + rsimpl->AddHandler("get", gh); Review comment: AddCommandHandler吧,保持一致 ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@brpc.apache.org For additional commands, e-mail: dev-h...@brpc.apache.org