RpcCommunicatorTest.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. RpcMetaPtr meta = boost::make_shared<RpcMeta>();
  42. StringPtr 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. RpcMetaPtr responseMeta = boost::make_shared<RpcMeta>();
  59. StringPtr 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. socket.async_connect(endpoint,
  85. boost::bind(&RpcClientTest::OnAsyncConnect, this,
  86. boost::asio::placeholders::error));
  87. }
  88. void Start()
  89. {
  90. ioService.run();
  91. }
  92. void Stop()
  93. {
  94. socket.close();
  95. }
  96. void OnAsyncConnect(const boost::system::error_code& err)
  97. {
  98. if (err)
  99. {
  100. return;
  101. }
  102. RpcMetaPtr sendMeta = boost::make_shared<RpcMeta>();
  103. StringPtr sendMessage = boost::make_shared<std::string>(
  104. "send test rpc communicator string");
  105. sendMeta->size = sendMessage->size();
  106. sendMeta->method = 654321;
  107. SendMeta(sendMeta, sendMessage);
  108. RpcMetaPtr meta = boost::make_shared<RpcMeta>();
  109. StringPtr message = boost::make_shared<std::string>();
  110. RecvMeta(meta, message);
  111. }
  112. virtual void OnRecvMessage(RpcMetaPtr meta, StringPtr message)
  113. {
  114. recvString = *message;
  115. recvMeta = *meta;
  116. barrier.Signal();
  117. }
  118. virtual void OnSendMessage()
  119. {
  120. }
  121. };
  122. class RpcCommunicatorTest: public testing::Test
  123. {
  124. protected:
  125. boost::asio::io_service ioServer;
  126. boost::asio::io_service ioClient;
  127. CountBarrier barrier;
  128. RpcServerTest rpcServer;
  129. RpcClientTest rpcClient;
  130. public:
  131. RpcCommunicatorTest():
  132. ioServer(), ioClient(),
  133. barrier(2), rpcServer(ioServer, globalPort, barrier),
  134. rpcClient(ioClient, globalPort, barrier)
  135. {
  136. }
  137. virtual ~RpcCommunicatorTest()
  138. {
  139. }
  140. };
  141. TEST_F(RpcCommunicatorTest, SendAndRecvString)
  142. {
  143. boost::threadpool::fifo_pool threadPool(2);
  144. threadPool.schedule(boost::bind(&RpcServerTest::Start, &rpcServer));
  145. boost::this_thread::sleep(boost::posix_time::milliseconds(100));
  146. threadPool.schedule(boost::bind(&RpcClientTest::Start, &rpcClient));
  147. barrier.Wait();
  148. threadPool.wait();
  149. rpcServer.Stop();
  150. rpcClient.Stop();
  151. ASSERT_EQ(std::string("send test rpc communicator string"), rpcServer.recvMessage);
  152. ASSERT_EQ(rpcServer.recvMeta.size, rpcServer.recvMessage.size());
  153. ASSERT_EQ(654321U, rpcServer.recvMeta.method);
  154. ASSERT_EQ(std::string("response test rpc communicator string"), rpcClient.recvString);
  155. ASSERT_EQ(rpcClient.recvMeta.size, rpcClient.recvString.size());
  156. ASSERT_EQ(123456U, rpcClient.recvMeta.method);
  157. }
  158. } // namespace Egametang
  159. int main(int argc, char* argv[])
  160. {
  161. testing::InitGoogleTest(&argc, argv);
  162. google::InitGoogleLogging(argv[0]);
  163. google::ParseCommandLineFlags(&argc, &argv, true);
  164. return RUN_ALL_TESTS();
  165. }