RpcCommunicatorTest.cc 4.5 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 globalPort = 10001;
  11. class RpcServerTest: public RpcCommunicator
  12. {
  13. public:
  14. CountBarrier& barrier;
  15. std::string recvMessage;
  16. RpcMeta recvMeta;
  17. boost::asio::ip::tcp::acceptor acceptor;
  18. public:
  19. RpcServerTest(boost::asio::io_service& ioService, int port, CountBarrier& barrier):
  20. RpcCommunicator(ioService), acceptor(ioService),
  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. ioService.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. recvMessage = *message;
  59. recvMeta = *meta;
  60. boost::hash<std::string> string_hash;
  61. RpcMetaPtr responseMeta(new RpcMeta());
  62. StringPtr response_message(new std::string("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. 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. 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(ERROR) << "async connect failed: " << err.message();
  104. return;
  105. }
  106. boost::hash<std::string> string_hash;
  107. RpcMetaPtr sendMeta(new RpcMeta());
  108. StringPtr sendMessage(new std::string("send test rpc communicator string"));
  109. sendMeta->size = sendMessage->size();
  110. sendMeta->method = 654321;
  111. SendMeta(sendMeta, sendMessage);
  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. recvString = *message;
  120. recvMeta = *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 ioServer;
  131. boost::asio::io_service ioClient;
  132. CountBarrier barrier;
  133. RpcServerTest rpcServer;
  134. RpcClientTest rpcClient;
  135. public:
  136. RpcCommunicatorTest():
  137. ioServer(), ioClient(),
  138. barrier(2), rpcServer(ioServer, globalPort, barrier),
  139. rpcClient(ioClient, globalPort, barrier)
  140. {
  141. }
  142. virtual ~RpcCommunicatorTest()
  143. {
  144. }
  145. };
  146. TEST_F(RpcCommunicatorTest, SendAndRecvString)
  147. {
  148. ThreadPool threadPool(2);
  149. threadPool.Schedule(boost::bind(&RpcServerTest::Start, &rpcServer));
  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. testing::InitGoogleTest(&argc, argv);
  166. google::ParseCommandLineFlags(&argc, &argv, true);
  167. google::InitGoogleLogging(argv[0]);
  168. return RUN_ALL_TESTS();
  169. }