RpcCommunicatorTest.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #include <boost/bind.hpp>
  2. #include <boost/asio.hpp>
  3. #include <gtest/gtest.h>
  4. #include <gflags/gflags.h>
  5. #include <glog/logging.h>
  6. #include "Rpc/RpcCommunicator.h"
  7. #include "Thread/ThreadPool.h"
  8. #include "Thread/CountBarrier.h"
  9. namespace Egametang {
  10. static int global_port = 10001;
  11. class RpcServerTest: public RpcCommunicator
  12. {
  13. public:
  14. CountBarrier& barrier;
  15. std::string recv_message;
  16. RpcMeta recv_meta;
  17. boost::asio::ip::tcp::acceptor acceptor;
  18. public:
  19. RpcServerTest(boost::asio::io_service& io_service, int port, CountBarrier& barrier):
  20. RpcCommunicator(io_service), acceptor(io_service),
  21. barrier(barrier)
  22. {
  23. boost::asio::ip::address address;
  24. address.from_string("127.0.0.1");
  25. boost::asio::ip::tcp::endpoint endpoint(address, port);
  26. acceptor.open(endpoint.protocol());
  27. acceptor.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
  28. acceptor.bind(endpoint);
  29. acceptor.listen();
  30. acceptor.async_accept(socket,
  31. boost::bind(&RpcServerTest::OnAsyncAccept, this,
  32. boost::asio::placeholders::error));
  33. }
  34. void OnAsyncAccept(const boost::system::error_code& err)
  35. {
  36. if (err)
  37. {
  38. LOG(ERROR) << "async accept failed: " << err.message();
  39. return;
  40. }
  41. RpcMetaPtr meta(new RpcMeta());
  42. StringPtr message(new std::string);
  43. RecvMeta(meta, message);
  44. }
  45. void Start()
  46. {
  47. VLOG(2) << "Start Server";
  48. io_service.run();
  49. }
  50. void Stop()
  51. {
  52. acceptor.close();
  53. socket.close();
  54. }
  55. virtual void OnRecvMessage(RpcMetaPtr meta, StringPtr message)
  56. {
  57. VLOG(2) << "Server Recv string: " << *message;
  58. recv_message = *message;
  59. recv_meta = *meta;
  60. boost::hash<std::string> string_hash;
  61. RpcMetaPtr response_meta(new RpcMeta());
  62. StringPtr response_message(new std::string("response test rpc communicator string"));
  63. response_meta->size = response_message->size();
  64. response_meta->method = 123456;
  65. SendMeta(response_meta, response_message);
  66. barrier.Signal();
  67. }
  68. virtual void OnSendMessage()
  69. {
  70. }
  71. };
  72. class RpcClientTest: public RpcCommunicator
  73. {
  74. public:
  75. CountBarrier& barrier_;
  76. std::string recv_string_;
  77. RpcMeta meta_;
  78. public:
  79. RpcClientTest(boost::asio::io_service& io_service, int port,
  80. CountBarrier& barrier):
  81. RpcCommunicator(io_service), barrier_(barrier)
  82. {
  83. boost::asio::ip::address address;
  84. address.from_string("127.0.0.1");
  85. boost::asio::ip::tcp::endpoint endpoint(address, port);
  86. socket.async_connect(endpoint,
  87. boost::bind(&RpcClientTest::OnAsyncConnect, this,
  88. boost::asio::placeholders::error));
  89. }
  90. void Start()
  91. {
  92. VLOG(2) << "Start Client";
  93. io_service.run();
  94. }
  95. void Stop()
  96. {
  97. socket.close();
  98. }
  99. void OnAsyncConnect(const boost::system::error_code& err)
  100. {
  101. if (err)
  102. {
  103. LOG(ERROR) << "async connect failed: " << err.message();
  104. return;
  105. }
  106. boost::hash<std::string> string_hash;
  107. RpcMetaPtr send_meta(new RpcMeta());
  108. StringPtr send_message(new std::string("send test rpc communicator string"));
  109. send_meta->size = send_message->size();
  110. send_meta->method = 654321;
  111. SendMeta(send_meta, send_message);
  112. RpcMetaPtr meta(new RpcMeta());
  113. StringPtr message(new std::string);
  114. RecvMeta(meta, message);
  115. }
  116. virtual void OnRecvMessage(RpcMetaPtr meta, StringPtr message)
  117. {
  118. VLOG(2) << "Client Recv string: " << *message;
  119. recv_string_ = *message;
  120. meta_ = *meta;
  121. barrier_.Signal();
  122. }
  123. virtual void OnSendMessage()
  124. {
  125. }
  126. };
  127. class RpcCommunicatorTest: public testing::Test
  128. {
  129. protected:
  130. boost::asio::io_service io_server;
  131. boost::asio::io_service io_client;
  132. CountBarrier barrier_;
  133. RpcServerTest rpc_server;
  134. RpcClientTest rpc_client;
  135. public:
  136. RpcCommunicatorTest():
  137. io_server(), io_client(),
  138. barrier_(2), rpc_server(io_server, global_port, barrier_),
  139. rpc_client(io_client, global_port, barrier_)
  140. {
  141. }
  142. virtual ~RpcCommunicatorTest()
  143. {
  144. }
  145. };
  146. TEST_F(RpcCommunicatorTest, SendAndRecvString)
  147. {
  148. ThreadPool thread_pool(2);
  149. thread_pool.Schedule(boost::bind(&RpcServerTest::Start, &rpc_server));
  150. thread_pool.Schedule(boost::bind(&RpcClientTest::Start, &rpc_client));
  151. barrier_.Wait();
  152. thread_pool.Wait();
  153. rpc_server.Stop();
  154. rpc_client.Stop();
  155. ASSERT_EQ(std::string("send test rpc communicator string"), rpc_server.recv_message);
  156. ASSERT_EQ(rpc_server.recv_meta.size, rpc_server.recv_message.size());
  157. ASSERT_EQ(654321U, rpc_server.recv_meta.method);
  158. ASSERT_EQ(std::string("response test rpc communicator string"), rpc_client.recv_string_);
  159. ASSERT_EQ(rpc_client.meta_.size, rpc_client.recv_string_.size());
  160. ASSERT_EQ(123456U, rpc_client.meta_.method);
  161. }
  162. } // namespace Egametang
  163. int main(int argc, char* argv[])
  164. {
  165. testing::InitGoogleTest(&argc, argv);
  166. google::ParseCommandLineFlags(&argc, &argv, true);
  167. google::InitGoogleLogging(argv[0]);
  168. return RUN_ALL_TESTS();
  169. }