brpc_mongo_protocol_unittest.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // Licensed to the Apache Software Foundation (ASF) under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. The ASF licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing,
  12. // software distributed under the License is distributed on an
  13. // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. // KIND, either express or implied. See the License for the
  15. // specific language governing permissions and limitations
  16. // under the License.
  17. // brpc - A framework to host and access services throughout Baidu.
  18. // Date: Thu Oct 15 21:08:31 CST 2015
  19. #include <sys/ioctl.h>
  20. #include <sys/types.h>
  21. #include <sys/socket.h>
  22. #include <gtest/gtest.h>
  23. #include <gflags/gflags.h>
  24. #include <google/protobuf/descriptor.h>
  25. #include "butil/time.h"
  26. #include "butil/macros.h"
  27. #include "brpc/socket.h"
  28. #include "brpc/acceptor.h"
  29. #include "brpc/server.h"
  30. #include "brpc/policy/mongo_protocol.h"
  31. #include "brpc/policy/most_common_message.h"
  32. #include "brpc/controller.h"
  33. #include "brpc/mongo_head.h"
  34. #include "brpc/mongo_service_adaptor.h"
  35. #include "brpc/policy/mongo.pb.h"
  36. int main(int argc, char* argv[]) {
  37. testing::InitGoogleTest(&argc, argv);
  38. GFLAGS_NS::ParseCommandLineFlags(&argc, &argv, true);
  39. return RUN_ALL_TESTS();
  40. }
  41. namespace {
  42. static const std::string EXP_REQUEST = "hello";
  43. static const std::string EXP_RESPONSE = "world";
  44. class MyEchoService : public ::brpc::policy::MongoService {
  45. void default_method(::google::protobuf::RpcController*,
  46. const ::brpc::policy::MongoRequest* req,
  47. ::brpc::policy::MongoResponse* res,
  48. ::google::protobuf::Closure* done) {
  49. brpc::ClosureGuard done_guard(done);
  50. EXPECT_EQ(EXP_REQUEST, req->message());
  51. res->mutable_header()->set_message_length(
  52. sizeof(brpc::mongo_head_t) + sizeof(int32_t) * 3 +
  53. sizeof(int64_t) + EXP_REQUEST.length());
  54. res->set_message(EXP_RESPONSE);
  55. }
  56. };
  57. class MyContext : public ::brpc::MongoContext {
  58. };
  59. class MyMongoAdaptor : public brpc::MongoServiceAdaptor {
  60. public:
  61. virtual void SerializeError(int /*response_to*/, butil::IOBuf* out_buf) const {
  62. brpc::mongo_head_t header = {
  63. (int32_t)(sizeof(brpc::mongo_head_t) + sizeof(int32_t) * 3 +
  64. sizeof(int64_t) + EXP_REQUEST.length()),
  65. 0, 0, 0};
  66. out_buf->append(static_cast<const void*>(&header), sizeof(brpc::mongo_head_t));
  67. int32_t response_flags = 0;
  68. int64_t cursor_id = 0;
  69. int32_t starting_from = 0;
  70. int32_t number_returned = 0;
  71. out_buf->append(&response_flags, sizeof(response_flags));
  72. out_buf->append(&cursor_id, sizeof(cursor_id));
  73. out_buf->append(&starting_from, sizeof(starting_from));
  74. out_buf->append(&number_returned, sizeof(number_returned));
  75. out_buf->append(EXP_RESPONSE);
  76. }
  77. virtual ::brpc::MongoContext* CreateSocketContext() const {
  78. return new MyContext;
  79. }
  80. };
  81. class MongoTest : public ::testing::Test{
  82. protected:
  83. MongoTest() {
  84. EXPECT_EQ(0, _server.AddService(
  85. &_svc, brpc::SERVER_DOESNT_OWN_SERVICE));
  86. // Hack: Regard `_server' as running
  87. _server._status = brpc::Server::RUNNING;
  88. _server._options.mongo_service_adaptor = &_adaptor;
  89. EXPECT_EQ(0, pipe(_pipe_fds));
  90. brpc::SocketId id;
  91. brpc::SocketOptions options;
  92. options.fd = _pipe_fds[1];
  93. EXPECT_EQ(0, brpc::Socket::Create(options, &id));
  94. EXPECT_EQ(0, brpc::Socket::Address(id, &_socket));
  95. };
  96. virtual ~MongoTest() {};
  97. virtual void SetUp() {};
  98. virtual void TearDown() {};
  99. void ProcessMessage(void (*process)(brpc::InputMessageBase*),
  100. brpc::InputMessageBase* msg, bool set_eof) {
  101. if (msg->_socket == NULL) {
  102. _socket->ReAddress(&msg->_socket);
  103. }
  104. msg->_arg = &_server;
  105. _socket->PostponeEOF();
  106. if (set_eof) {
  107. _socket->SetEOF();
  108. }
  109. (*process)(msg);
  110. }
  111. brpc::policy::MostCommonMessage* MakeRequestMessage(
  112. brpc::mongo_head_t* head) {
  113. head->message_length = sizeof(head) + EXP_REQUEST.length();
  114. head->op_code = brpc::MONGO_OPCODE_REPLY;
  115. brpc::policy::MostCommonMessage* msg =
  116. brpc::policy::MostCommonMessage::Get();
  117. msg->meta.append(&head, sizeof(head));
  118. msg->payload.append(EXP_REQUEST);
  119. return msg;
  120. }
  121. void CheckEmptyResponse() {
  122. int bytes_in_pipe = 0;
  123. ioctl(_pipe_fds[0], FIONREAD, &bytes_in_pipe);
  124. EXPECT_EQ(0, bytes_in_pipe);
  125. }
  126. int _pipe_fds[2];
  127. brpc::SocketUniquePtr _socket;
  128. brpc::Server _server;
  129. MyMongoAdaptor _adaptor;
  130. MyEchoService _svc;
  131. };
  132. TEST_F(MongoTest, process_request_logoff) {
  133. brpc::mongo_head_t header = { 0, 0, 0, 0 };
  134. header.op_code = brpc::MONGO_OPCODE_REPLY;
  135. header.message_length = sizeof(header) + EXP_REQUEST.length();
  136. butil::IOBuf total_buf;
  137. total_buf.append(static_cast<const void*>(&header), sizeof(header));
  138. total_buf.append(EXP_REQUEST);
  139. brpc::ParseResult req_pr = brpc::policy::ParseMongoMessage(
  140. &total_buf, _socket.get(), false, &_server);
  141. ASSERT_EQ(brpc::PARSE_OK, req_pr.error());
  142. _server._status = brpc::Server::READY;
  143. ProcessMessage(brpc::policy::ProcessMongoRequest, req_pr.message(), false);
  144. ASSERT_EQ(1ll, _server._nerror_bvar.get_value());
  145. }
  146. TEST_F(MongoTest, process_request_failed_socket) {
  147. brpc::mongo_head_t header = { 0, 0, 0, 0 };
  148. header.op_code = brpc::MONGO_OPCODE_REPLY;
  149. header.message_length = sizeof(header) + EXP_REQUEST.length();
  150. butil::IOBuf total_buf;
  151. total_buf.append(static_cast<const void*>(&header), sizeof(header));
  152. total_buf.append(EXP_REQUEST);
  153. brpc::ParseResult req_pr = brpc::policy::ParseMongoMessage(
  154. &total_buf, _socket.get(), false, &_server);
  155. ASSERT_EQ(brpc::PARSE_OK, req_pr.error());
  156. _socket->SetFailed();
  157. ProcessMessage(brpc::policy::ProcessMongoRequest, req_pr.message(), false);
  158. ASSERT_EQ(0ll, _server._nerror_bvar.get_value());
  159. }
  160. TEST_F(MongoTest, complete_flow) {
  161. butil::IOBuf request_buf;
  162. butil::IOBuf total_buf;
  163. brpc::Controller cntl;
  164. brpc::policy::MongoRequest req;
  165. brpc::policy::MongoResponse res;
  166. cntl._response = &res;
  167. // Assemble request
  168. brpc::mongo_head_t header = { 0, 0, 0, 0 };
  169. header.message_length = sizeof(header) + EXP_REQUEST.length();
  170. total_buf.append(static_cast<const void*>(&header), sizeof(header));
  171. total_buf.append(EXP_REQUEST);
  172. const size_t old_size = total_buf.size();
  173. // Handle request
  174. brpc::ParseResult req_pr =
  175. brpc::policy::ParseMongoMessage(&total_buf, _socket.get(), false, &_server);
  176. // head.op_code does not fit.
  177. ASSERT_EQ(brpc::PARSE_ERROR_TRY_OTHERS, req_pr.error());
  178. // no data should be consumed.
  179. ASSERT_EQ(old_size, total_buf.size());
  180. header.op_code = brpc::MONGO_OPCODE_REPLY;
  181. total_buf.clear();
  182. total_buf.append(static_cast<const void*>(&header), sizeof(header));
  183. total_buf.append(EXP_REQUEST);
  184. req_pr = brpc::policy::ParseMongoMessage(&total_buf, _socket.get(), false, &_server);
  185. ASSERT_EQ(brpc::PARSE_OK, req_pr.error());
  186. brpc::InputMessageBase* req_msg = req_pr.message();
  187. ProcessMessage(brpc::policy::ProcessMongoRequest, req_msg, false);
  188. // Read response from pipe
  189. butil::IOPortal response_buf;
  190. response_buf.append_from_file_descriptor(_pipe_fds[0], 1024);
  191. char buf[sizeof(brpc::mongo_head_t)];
  192. const brpc::mongo_head_t *phead = static_cast<const brpc::mongo_head_t*>(
  193. response_buf.fetch(buf, sizeof(buf)));
  194. response_buf.cutn(&header, sizeof(header));
  195. response_buf.cutn(buf, sizeof(int32_t));
  196. response_buf.cutn(buf, sizeof(int64_t));
  197. response_buf.cutn(buf, sizeof(int32_t));
  198. response_buf.cutn(buf, sizeof(int32_t));
  199. char msg_buf[200];
  200. int msg_length = phead->message_length - sizeof(brpc::mongo_head_t) - sizeof(int32_t) * 3 -
  201. sizeof(int64_t);
  202. response_buf.cutn(msg_buf, msg_length);
  203. msg_buf[msg_length] = '\0';
  204. ASSERT_FALSE(cntl.Failed());
  205. ASSERT_STREQ(EXP_RESPONSE.c_str(), msg_buf);
  206. }
  207. } //namespace