RpcCommunicatorTest.cc 4.3 KB

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