RpcCommunicatorTest.cc 4.3 KB

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