RpcCommunicatorTest.cc 4.1 KB

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