RpcCommunicatorTest.cc 4.4 KB

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