RpcCommunicatorTest.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #include <boost/asio.hpp>
  2. #include <boost/threadpool.hpp>
  3. #include <memory>
  4. #include <gtest/gtest.h>
  5. #include "Rpc/RpcCommunicator.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. auto meta = std::make_shared<RpcMeta>();
  39. auto message = std::make_shared<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. auto responseMeta = std::make_shared<RpcMeta>();
  56. auto response_message = std::make_shared<std::string>(
  57. "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. };
  64. class RpcClientTest: public RpcCommunicator
  65. {
  66. public:
  67. CountBarrier& barrier;
  68. std::string recvString;
  69. RpcMeta recvMeta;
  70. public:
  71. RpcClientTest(boost::asio::io_service& ioService, int port,
  72. CountBarrier& barrier):
  73. RpcCommunicator(ioService), barrier(barrier)
  74. {
  75. boost::asio::ip::address address;
  76. address.from_string("127.0.0.1");
  77. boost::asio::ip::tcp::endpoint endpoint(address, port);
  78. socket.async_connect(endpoint,
  79. boost::bind(&RpcClientTest::OnAsyncConnect, this,
  80. boost::asio::placeholders::error));
  81. }
  82. void Start()
  83. {
  84. ioService.run();
  85. }
  86. void Stop()
  87. {
  88. socket.close();
  89. }
  90. void OnAsyncConnect(const boost::system::error_code& err)
  91. {
  92. if (err)
  93. {
  94. return;
  95. }
  96. auto sendMeta = std::make_shared<RpcMeta>();
  97. auto sendMessage = std::make_shared<std::string>(
  98. "send test rpc communicator string");
  99. sendMeta->size = sendMessage->size();
  100. sendMeta->method = 654321;
  101. SendMeta(sendMeta, sendMessage);
  102. auto meta = std::make_shared<RpcMeta>();
  103. auto message = std::make_shared<std::string>();
  104. RecvMeta(meta, message);
  105. }
  106. virtual void OnRecvMessage(RpcMetaPtr meta, StringPtr message)
  107. {
  108. recvString = *message;
  109. recvMeta = *meta;
  110. barrier.Signal();
  111. }
  112. };
  113. class RpcCommunicatorTest: public testing::Test
  114. {
  115. };
  116. TEST_F(RpcCommunicatorTest, SendAndRecvString)
  117. {
  118. boost::asio::io_service ioServer;
  119. boost::asio::io_service ioClient;
  120. CountBarrier barrier(2);
  121. RpcServerTest rpcServer(ioServer, globalPort, barrier);
  122. RpcClientTest rpcClient(ioClient, globalPort, barrier);
  123. boost::threadpool::fifo_pool threadPool(2);
  124. threadPool.schedule(boost::bind(&RpcServerTest::Start, &rpcServer));
  125. boost::this_thread::sleep(boost::posix_time::milliseconds(500));
  126. threadPool.schedule(boost::bind(&RpcClientTest::Start, &rpcClient));
  127. barrier.Wait();
  128. threadPool.wait();
  129. ioClient.post(boost::bind(&boost::asio::io_service::stop, &ioClient));
  130. ioServer.post(boost::bind(&boost::asio::io_service::stop, &ioServer));
  131. ASSERT_EQ(std::string("send test rpc communicator string"), rpcServer.recvMessage);
  132. ASSERT_EQ(rpcServer.recvMeta.size, rpcServer.recvMessage.size());
  133. ASSERT_EQ(654321U, rpcServer.recvMeta.method);
  134. ASSERT_EQ(std::string("response test rpc communicator string"), rpcClient.recvString);
  135. ASSERT_EQ(rpcClient.recvMeta.size, rpcClient.recvString.size());
  136. ASSERT_EQ(123456U, rpcClient.recvMeta.method);
  137. }
  138. } // namespace Egametang
  139. int main(int argc, char* argv[])
  140. {
  141. testing::InitGoogleTest(&argc, argv);
  142. return RUN_ALL_TESTS();
  143. }