RpcCommunicatorTest.cc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #include <WinSock2.h>
  2. #include <boost/bind.hpp>
  3. #include <boost/asio.hpp>
  4. #include <boost/threadpool.hpp>
  5. #include <boost/make_shared.hpp>
  6. #include <gtest/gtest.h>
  7. #include <glog/logging.h>
  8. #include <gflags/gflags.h>
  9. #include "Rpc/RpcCommunicator.h"
  10. #include "Thread/CountBarrier.h"
  11. namespace Egametang {
  12. static int globalPort = 10001;
  13. class RpcServerTest: public RpcCommunicator
  14. {
  15. public:
  16. CountBarrier& barrier;
  17. std::string recvMessage;
  18. RpcMeta recvMeta;
  19. boost::asio::ip::tcp::acceptor acceptor;
  20. public:
  21. RpcServerTest(boost::asio::io_service& ioService, int port, CountBarrier& barrier):
  22. RpcCommunicator(ioService), acceptor(ioService),
  23. barrier(barrier)
  24. {
  25. boost::asio::ip::address address;
  26. address.from_string("127.0.0.1");
  27. boost::asio::ip::tcp::endpoint endpoint(address, port);
  28. acceptor.open(endpoint.protocol());
  29. acceptor.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
  30. acceptor.bind(endpoint);
  31. acceptor.listen();
  32. acceptor.async_accept(socket,
  33. boost::bind(&RpcServerTest::OnAsyncAccept, this,
  34. boost::asio::placeholders::error));
  35. }
  36. void OnAsyncAccept(const boost::system::error_code& err)
  37. {
  38. if (err)
  39. {
  40. LOG(FATAL) << err.message();
  41. return;
  42. }
  43. auto meta = boost::make_shared<RpcMeta>();
  44. auto message = boost::make_shared<std::string>();
  45. RecvMeta(meta, message);
  46. }
  47. void Start()
  48. {
  49. ioService.run();
  50. }
  51. void Stop()
  52. {
  53. acceptor.close();
  54. socket.close();
  55. }
  56. virtual void OnRecvMessage(RpcMetaPtr meta, StringPtr message)
  57. {
  58. recvMessage = *message;
  59. recvMeta = *meta;
  60. auto responseMeta = boost::make_shared<RpcMeta>();
  61. auto response_message = boost::make_shared<std::string>(
  62. "response test rpc communicator string");
  63. responseMeta->size = response_message->size();
  64. responseMeta->method = 123456;
  65. SendMeta(responseMeta, 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 recvString;
  77. RpcMeta recvMeta;
  78. public:
  79. RpcClientTest(boost::asio::io_service& ioService, int port,
  80. CountBarrier& barrier):
  81. RpcCommunicator(ioService), 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. VLOG(2) << "port: " << port;
  87. socket.async_connect(endpoint,
  88. boost::bind(&RpcClientTest::OnAsyncConnect, this,
  89. boost::asio::placeholders::error));
  90. }
  91. void Start()
  92. {
  93. ioService.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(FATAL) << err.message() << err.value();
  104. return;
  105. }
  106. auto sendMeta = boost::make_shared<RpcMeta>();
  107. auto sendMessage = boost::make_shared<std::string>(
  108. "send test rpc communicator string");
  109. sendMeta->size = sendMessage->size();
  110. sendMeta->method = 654321;
  111. SendMeta(sendMeta, sendMessage);
  112. auto meta = boost::make_shared<RpcMeta>();
  113. auto message = boost::make_shared<std::string>();
  114. RecvMeta(meta, message);
  115. }
  116. virtual void OnRecvMessage(RpcMetaPtr meta, StringPtr message)
  117. {
  118. recvString = *message;
  119. recvMeta = *meta;
  120. barrier.Signal();
  121. }
  122. virtual void OnSendMessage()
  123. {
  124. }
  125. };
  126. class RpcCommunicatorTest: public testing::Test
  127. {
  128. protected:
  129. boost::asio::io_service ioServer;
  130. boost::asio::io_service ioClient;
  131. CountBarrier barrier;
  132. RpcServerTest rpcServer;
  133. RpcClientTest rpcClient;
  134. public:
  135. RpcCommunicatorTest():
  136. ioServer(), ioClient(),
  137. barrier(2), rpcServer(ioServer, globalPort, barrier),
  138. rpcClient(ioClient, globalPort, barrier)
  139. {
  140. }
  141. virtual ~RpcCommunicatorTest()
  142. {
  143. }
  144. };
  145. TEST_F(RpcCommunicatorTest, SendAndRecvString)
  146. {
  147. boost::threadpool::fifo_pool threadPool(2);
  148. threadPool.schedule(boost::bind(&RpcServerTest::Start, &rpcServer));
  149. boost::this_thread::sleep(boost::posix_time::milliseconds(2000));
  150. threadPool.schedule(boost::bind(&RpcClientTest::Start, &rpcClient));
  151. barrier.Wait();
  152. threadPool.wait();
  153. rpcServer.Stop();
  154. rpcClient.Stop();
  155. ASSERT_EQ(std::string("send test rpc communicator string"), rpcServer.recvMessage);
  156. ASSERT_EQ(rpcServer.recvMeta.size, rpcServer.recvMessage.size());
  157. ASSERT_EQ(654321U, rpcServer.recvMeta.method);
  158. ASSERT_EQ(std::string("response test rpc communicator string"), rpcClient.recvString);
  159. ASSERT_EQ(rpcClient.recvMeta.size, rpcClient.recvString.size());
  160. ASSERT_EQ(123456U, rpcClient.recvMeta.method);
  161. }
  162. } // namespace Egametang
  163. int main(int argc, char* argv[])
  164. {
  165. WSADATA wsaData;
  166. memset(&wsaData, 0, sizeof(wsaData));
  167. int ret = WSAStartup(MAKEWORD(2,0), &wsaData);
  168. assert(ret == 0);
  169. testing::InitGoogleTest(&argc, argv);
  170. google::InitGoogleLogging(argv[0]);
  171. google::ParseCommandLineFlags(&argc, &argv, true);
  172. return RUN_ALL_TESTS();
  173. }