RpcCommunicatorTest.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #include <boost/asio.hpp>
  2. #include <boost/shared_ptr.hpp>
  3. #include <boost/make_shared.hpp>
  4. #include <gtest/gtest.h>
  5. #include "Rpc/RpcCommunicator.h"
  6. #include "Thread/CountBarrier.h"
  7. #include "Log/Log.h"
  8. namespace Egametang {
  9. static int globalPort = 11111;
  10. class RpcServerTest: public RpcCommunicator
  11. {
  12. public:
  13. CountBarrier& barrier;
  14. std::string recvMessage;
  15. RpcMeta recvMeta;
  16. boost::asio::ip::tcp::acceptor acceptor;
  17. public:
  18. RpcServerTest(boost::asio::io_service& ioService, int port, CountBarrier& barrier):
  19. RpcCommunicator(ioService), acceptor(ioService),
  20. barrier(barrier)
  21. {
  22. boost::asio::ip::address address;
  23. address.from_string("127.0.0.1");
  24. boost::asio::ip::tcp::endpoint endpoint(address, port);
  25. acceptor.open(endpoint.protocol());
  26. acceptor.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
  27. acceptor.bind(endpoint);
  28. acceptor.listen();
  29. acceptor.async_accept(socket,
  30. boost::bind(&RpcServerTest::OnAsyncAccept, this,
  31. boost::asio::placeholders::error));
  32. }
  33. void OnAsyncAccept(const boost::system::error_code& err)
  34. {
  35. if (err)
  36. {
  37. return;
  38. }
  39. auto meta = boost::make_shared<RpcMeta>();
  40. auto message = boost::make_shared<std::string>();
  41. RecvMeta(meta, message);
  42. }
  43. void Start()
  44. {
  45. ioService.run();
  46. }
  47. void Stop()
  48. {
  49. acceptor.close();
  50. socket.close();
  51. }
  52. virtual void OnRecvMessage(RpcMetaPtr meta, StringPtr message)
  53. {
  54. recvMessage = *message;
  55. recvMeta = *meta;
  56. auto responseMeta = boost::make_shared<RpcMeta>();
  57. auto response_message = boost::make_shared<std::string>(
  58. "response test rpc communicator string");
  59. responseMeta->size = response_message->size();
  60. responseMeta->method = 123456;
  61. SendMeta(responseMeta, response_message);
  62. barrier.Signal();
  63. }
  64. };
  65. class RpcClientTest: public RpcCommunicator
  66. {
  67. public:
  68. CountBarrier& barrier;
  69. std::string recvString;
  70. RpcMeta recvMeta;
  71. public:
  72. RpcClientTest(boost::asio::io_service& ioService, int port, 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. LOG(INFO) << "async connect error: " << err.message();
  95. return;
  96. }
  97. auto sendMeta = boost::make_shared<RpcMeta>();
  98. auto sendMessage = boost::make_shared<std::string>(
  99. "send test rpc communicator string");
  100. sendMeta->size = sendMessage->size();
  101. sendMeta->method = 654321;
  102. SendMeta(sendMeta, sendMessage);
  103. auto meta = boost::make_shared<RpcMeta>();
  104. auto message = boost::make_shared<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. };
  114. class RpcCommunicatorTest: public testing::Test
  115. {
  116. };
  117. TEST_F(RpcCommunicatorTest, SendAndRecvString)
  118. {
  119. boost::asio::io_service ioServer;
  120. boost::asio::io_service ioClient;
  121. CountBarrier barrier(2);
  122. RpcServerTest rpcServer(ioServer, globalPort, barrier);
  123. boost::thread serverThread(boost::bind(&RpcServerTest::Start, &rpcServer));
  124. boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
  125. RpcClientTest rpcClient(ioClient, globalPort, barrier);
  126. boost::thread clientThread(boost::bind(&RpcClientTest::Start, &rpcClient));
  127. barrier.Wait();
  128. ioClient.post(boost::bind(&boost::asio::io_service::stop, &ioClient));
  129. ioServer.post(boost::bind(&boost::asio::io_service::stop, &ioServer));
  130. serverThread.join();
  131. clientThread.join();
  132. ASSERT_EQ(std::string("send test rpc communicator string"), rpcServer.recvMessage);
  133. ASSERT_EQ(rpcServer.recvMeta.size, rpcServer.recvMessage.size());
  134. ASSERT_EQ(654321U, rpcServer.recvMeta.method);
  135. ASSERT_EQ(std::string("response test rpc communicator string"), rpcClient.recvString);
  136. ASSERT_EQ(rpcClient.recvMeta.size, rpcClient.recvString.size());
  137. ASSERT_EQ(123456U, rpcClient.recvMeta.method);
  138. }
  139. } // namespace Egametang
  140. int main(int argc, char* argv[])
  141. {
  142. testing::InitGoogleTest(&argc, argv);
  143. Egametang::Log::Init(argv[0]);
  144. return RUN_ALL_TESTS();
  145. }