RpcChannelTest.cc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #include <gtest/gtest.h>
  2. #include <gflags/gflags.h>
  3. #include <glog/logging.h>
  4. #include "Rpc/RpcChannel.h"
  5. #include "Thread/CountBarrier.h"
  6. #include "Thread/ThreadPool.h"
  7. #include "Rpc/RpcController.h"
  8. #include "Rpc/Echo.pb.h"
  9. namespace Egametang {
  10. static int global_port = 10002;
  11. class RpcServerTest: public RpcCommunicator
  12. {
  13. public:
  14. CountBarrier& barrier_;
  15. int32 num_;
  16. boost::asio::ip::tcp::acceptor acceptor_;
  17. public:
  18. RpcServerTest(boost::asio::io_service& io_service, int port, CountBarrier& barrier):
  19. RpcCommunicator(io_service), acceptor_(io_service),
  20. barrier_(barrier), num_(0)
  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. LOG(ERROR) << "async accept failed: " << err.message();
  38. return;
  39. }
  40. RecvMeta();
  41. }
  42. void Stop()
  43. {
  44. acceptor_.close();
  45. socket_.close();
  46. }
  47. virtual void OnRecvMessage(RpcMetaPtr meta, StringPtr message)
  48. {
  49. // 接收消息
  50. EchoRequest request;
  51. request.ParseFromString(*message);
  52. num_ = request.num();
  53. VLOG(2) << "num: " << num_;
  54. // 回一个消息
  55. EchoResponse response;
  56. response.set_num(num_);
  57. std::string send_string = response.SerializeAsString();
  58. RpcMeta response_meta;
  59. response_meta.id = meta->id;
  60. response_meta.size = send_string.size();
  61. VLOG(3) << "send meta: " << response_meta.size << " "
  62. << response_meta.id << " " << response_meta.method;
  63. SendMeta(response_meta, send_string);
  64. barrier_.Signal();
  65. }
  66. virtual void OnSendMessage()
  67. {
  68. }
  69. };
  70. class RpcChannelTest: public testing::Test
  71. {
  72. };
  73. static void IOServiceRun(boost::asio::io_service& io_service)
  74. {
  75. io_service.run();
  76. }
  77. TEST_F(RpcChannelTest, Echo)
  78. {
  79. boost::asio::io_service io_server;
  80. boost::asio::io_service io_client;
  81. CountBarrier barrier(2);
  82. RpcServerTest rpc_server(io_server, global_port, barrier);
  83. RpcChannel rpc_channel(io_client, "127.0.0.1", global_port);
  84. EchoService_Stub service(&rpc_channel);
  85. ThreadPool thread_pool(2);
  86. thread_pool.PushTask(boost::bind(&IOServiceRun, boost::ref(io_server)));
  87. thread_pool.PushTask(boost::bind(&IOServiceRun, boost::ref(io_client)));
  88. EchoRequest request;
  89. request.set_num(100);
  90. EchoResponse response;
  91. ASSERT_EQ(0, response.num());
  92. service.Echo(NULL, &request, &response,
  93. google::protobuf::NewCallback(&barrier, &CountBarrier::Signal));
  94. barrier.Wait();
  95. rpc_channel.Stop();
  96. rpc_server.Stop();
  97. io_server.stop();
  98. io_client.stop();
  99. // rpc_channel是个无限循环的操作, 必须主动让channel和server stop才能wait线程
  100. thread_pool.Wait();
  101. ASSERT_EQ(100, response.num());
  102. }
  103. } // namespace Egametang
  104. int main(int argc, char* argv[])
  105. {
  106. testing::InitGoogleTest(&argc, argv);
  107. google::ParseCommandLineFlags(&argc, &argv, true);
  108. google::InitGoogleLogging(argv[0]);
  109. return RUN_ALL_TESTS();
  110. }