RpcCommunicatorTest.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #include <boost/asio.hpp>
  2. #include <boost/threadpool.hpp>
  3. #include <memory>
  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. std::bind(&RpcServerTest::OnAsyncAccept, this,
  32. std::placeholders::_1));
  33. }
  34. void OnAsyncAccept(const boost::system::error_code& err)
  35. {
  36. if (err)
  37. {
  38. return;
  39. }
  40. auto meta = std::make_shared<RpcMeta>();
  41. auto message = std::make_shared<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. auto responseMeta = std::make_shared<RpcMeta>();
  58. auto response_message = std::make_shared<std::string>(
  59. "response test rpc communicator string");
  60. responseMeta->size = response_message->size();
  61. responseMeta->method = 123456;
  62. SendMeta(responseMeta, response_message);
  63. barrier.Signal();
  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. VLOG(2) << "port: " << port;
  81. socket.async_connect(endpoint,
  82. std::bind(&RpcClientTest::OnAsyncConnect, this,
  83. std::placeholders::_1));
  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. auto sendMeta = std::make_shared<RpcMeta>();
  100. auto sendMessage = std::make_shared<std::string>(
  101. "send test rpc communicator string");
  102. sendMeta->size = sendMessage->size();
  103. sendMeta->method = 654321;
  104. SendMeta(sendMeta, sendMessage);
  105. auto meta = std::make_shared<RpcMeta>();
  106. auto message = std::make_shared<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. };
  116. class RpcCommunicatorTest: public testing::Test
  117. {
  118. };
  119. TEST_F(RpcCommunicatorTest, SendAndRecvString)
  120. {
  121. boost::asio::io_service ioServer;
  122. boost::asio::io_service ioClient;
  123. CountBarrier barrier(2);
  124. RpcServerTest rpcServer(ioServer, globalPort, barrier);
  125. RpcClientTest rpcClient(ioClient, globalPort, barrier);
  126. boost::threadpool::fifo_pool threadPool(2);
  127. threadPool.schedule(std::bind(&RpcServerTest::Start, &rpcServer));
  128. boost::this_thread::sleep(boost::posix_time::milliseconds(500));
  129. threadPool.schedule(std::bind(&RpcClientTest::Start, &rpcClient));
  130. barrier.Wait();
  131. threadPool.wait();
  132. ioClient.post(std::bind(&boost::asio::io_service::stop, &ioClient));
  133. ioServer.post(std::bind(&boost::asio::io_service::stop, &ioServer));
  134. ASSERT_EQ(std::string("send test rpc communicator string"), rpcServer.recvMessage);
  135. ASSERT_EQ(rpcServer.recvMeta.size, rpcServer.recvMessage.size());
  136. ASSERT_EQ(654321U, rpcServer.recvMeta.method);
  137. ASSERT_EQ(std::string("response test rpc communicator string"), rpcClient.recvString);
  138. ASSERT_EQ(rpcClient.recvMeta.size, rpcClient.recvString.size());
  139. ASSERT_EQ(123456U, rpcClient.recvMeta.method);
  140. }
  141. } // namespace Egametang
  142. int main(int argc, char* argv[])
  143. {
  144. testing::InitGoogleTest(&argc, argv);
  145. google::InitGoogleLogging(argv[0]);
  146. google::ParseCommandLineFlags(&argc, &argv, true);
  147. return RUN_ALL_TESTS();
  148. }