RpcCommunicatorTest.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #include <boost/bind.hpp>
  2. #include <boost/asio.hpp>
  3. #include <gtest/gtest.h>
  4. #include "Rpc/RpcCommunicator.h"
  5. #include "Thread/ThreadPool.h"
  6. #include "Thread/CountBarrier.h"
  7. namespace Egametang {
  8. static int globalPort = 10001;
  9. class RpcServerTest: public RpcCommunicator
  10. {
  11. public:
  12. CountBarrier& barrier;
  13. std::string recvMessage;
  14. RpcMeta recvMeta;
  15. boost::asio::ip::tcp::acceptor acceptor;
  16. public:
  17. RpcServerTest(boost::asio::io_service& ioService, int port, CountBarrier& barrier):
  18. RpcCommunicator(ioService), acceptor(ioService),
  19. barrier(barrier)
  20. {
  21. boost::asio::ip::address address;
  22. address.from_string("127.0.0.1");
  23. boost::asio::ip::tcp::endpoint endpoint(address, port);
  24. acceptor.open(endpoint.protocol());
  25. acceptor.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
  26. acceptor.bind(endpoint);
  27. acceptor.listen();
  28. acceptor.async_accept(socket,
  29. boost::bind(&RpcServerTest::OnAsyncAccept, this,
  30. boost::asio::placeholders::error));
  31. }
  32. void OnAsyncAccept(const boost::system::error_code& err)
  33. {
  34. if (err)
  35. {
  36. return;
  37. }
  38. RpcMetaPtr meta(new RpcMeta());
  39. StringPtr message(new std::string);
  40. RecvMeta(meta, message);
  41. }
  42. void Start()
  43. {
  44. ioService.run();
  45. }
  46. void Stop()
  47. {
  48. acceptor.close();
  49. socket.close();
  50. }
  51. virtual void OnRecvMessage(RpcMetaPtr meta, StringPtr message)
  52. {
  53. recvMessage = *message;
  54. recvMeta = *meta;
  55. boost::hash<std::string> string_hash;
  56. RpcMetaPtr responseMeta(new RpcMeta());
  57. StringPtr response_message(new std::string("response test rpc communicator string"));
  58. responseMeta->size = response_message->size();
  59. responseMeta->method = 123456;
  60. SendMeta(responseMeta, response_message);
  61. barrier.Signal();
  62. }
  63. virtual void OnSendMessage()
  64. {
  65. }
  66. };
  67. class RpcClientTest: public RpcCommunicator
  68. {
  69. public:
  70. CountBarrier& barrier;
  71. std::string recvString;
  72. RpcMeta recvMeta;
  73. public:
  74. RpcClientTest(boost::asio::io_service& ioService, int port,
  75. CountBarrier& barrier):
  76. RpcCommunicator(ioService), barrier(barrier)
  77. {
  78. boost::asio::ip::address address;
  79. address.from_string("127.0.0.1");
  80. boost::asio::ip::tcp::endpoint endpoint(address, port);
  81. socket.async_connect(endpoint,
  82. boost::bind(&RpcClientTest::OnAsyncConnect, this,
  83. boost::asio::placeholders::error));
  84. }
  85. void Start()
  86. {
  87. ioService.run();
  88. }
  89. void Stop()
  90. {
  91. socket.close();
  92. }
  93. void OnAsyncConnect(const boost::system::error_code& err)
  94. {
  95. if (err)
  96. {
  97. return;
  98. }
  99. boost::hash<std::string> string_hash;
  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. ThreadPool threadPool(2);
  141. threadPool.Schedule(boost::bind(&RpcServerTest::Start, &rpcServer));
  142. threadPool.Schedule(boost::bind(&RpcClientTest::Start, &rpcClient));
  143. barrier.Wait();
  144. threadPool.Wait();
  145. rpcServer.Stop();
  146. rpcClient.Stop();
  147. ASSERT_EQ(std::string("send test rpc communicator string"), rpcServer.recvMessage);
  148. ASSERT_EQ(rpcServer.recvMeta.size, rpcServer.recvMessage.size());
  149. ASSERT_EQ(654321U, rpcServer.recvMeta.method);
  150. ASSERT_EQ(std::string("response test rpc communicator string"), rpcClient.recvString);
  151. ASSERT_EQ(rpcClient.recvMeta.size, rpcClient.recvString.size());
  152. ASSERT_EQ(123456U, rpcClient.recvMeta.method);
  153. }
  154. } // namespace Egametang
  155. int main(int argc, char* argv[])
  156. {
  157. testing::InitGoogleTest(&argc, argv);
  158. return RUN_ALL_TESTS();
  159. }