RpcServerTest.cc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include <boost/bind.hpp>
  2. #include <boost/asio.hpp>
  3. #include <boost/function.hpp>
  4. #include <boost/threadpool.hpp>
  5. #include <boost/make_shared.hpp>
  6. #include <gtest/gtest.h>
  7. #include <glog/logging.h>
  8. #include <gflags/gflags.h>
  9. #include <google/protobuf/service.h>
  10. #include "Thread/CountBarrier.h"
  11. #include "Rpc/RpcClient.h"
  12. #include "Rpc/RpcServer.h"
  13. #include "Rpc/RpcSession.h"
  14. #include "Rpc/RpcServerMock.h"
  15. #include "Rpc/Echo.pb.h"
  16. namespace Egametang {
  17. class MyEcho: public EchoService
  18. {
  19. public:
  20. virtual void Echo(
  21. google::protobuf::RpcController* controller,
  22. const EchoRequest* request,
  23. EchoResponse* response,
  24. google::protobuf::Closure* done)
  25. {
  26. int32 num = request->num();
  27. response->set_num(num);
  28. if (done)
  29. {
  30. done->Run();
  31. }
  32. }
  33. };
  34. static void IOServiceRun(boost::asio::io_service* ioService)
  35. {
  36. ioService->run();
  37. }
  38. class RpcServerTest: public testing::Test
  39. {
  40. protected:
  41. boost::asio::io_service ioClient;
  42. boost::asio::io_service ioServer;
  43. int port;
  44. public:
  45. RpcServerTest(): ioClient(), ioServer(), port(10003)
  46. {
  47. }
  48. virtual ~RpcServerTest()
  49. {
  50. }
  51. MethodMap& GetMethodMap(RpcServerPtr server)
  52. {
  53. return server->methods;
  54. }
  55. };
  56. TEST_F(RpcServerTest, ClientAndServer)
  57. {
  58. boost::threadpool::fifo_pool threadPool(2);
  59. ProtobufServicePtr echoSevice = boost::make_shared<MyEcho>();
  60. RpcServerPtr server = boost::make_shared<RpcServer>(ioServer, port);
  61. // 注册service
  62. server->Register(echoSevice);
  63. ASSERT_EQ(1U, GetMethodMap(server).size());
  64. RpcClientPtr client = boost::make_shared<RpcClient>(ioClient, "127.0.0.1", port);
  65. EchoService_Stub service(client.get());
  66. // 定义消息
  67. EchoRequest request;
  68. request.set_num(100);
  69. EchoResponse response;
  70. ASSERT_EQ(0U, response.num());
  71. // server和client分别在两个不同的线程
  72. threadPool.schedule(boost::bind(&IOServiceRun, &ioServer));
  73. // 等待server OK
  74. boost::this_thread::sleep(boost::posix_time::milliseconds(100));
  75. threadPool.schedule(boost::bind(&IOServiceRun, &ioClient));
  76. CountBarrier barrier;
  77. service.Echo(NULL, &request, &response,
  78. google::protobuf::NewCallback(&barrier, &CountBarrier::Signal));
  79. barrier.Wait();
  80. // 加入到io线程
  81. ioClient.post(boost::bind(&RpcClient::Stop, client));
  82. ioServer.post(boost::bind(&RpcServer::Stop, server));
  83. // 加入任务队列,等client和server stop,io_service才stop
  84. ioClient.post(boost::bind(&boost::asio::io_service::stop, &ioClient));
  85. ioServer.post(boost::bind(&boost::asio::io_service::stop, &ioServer));
  86. // 必须主动让client和server stop才能wait线程
  87. threadPool.wait();
  88. ASSERT_EQ(100, response.num());
  89. }
  90. } // namespace Egametang
  91. int main(int argc, char* argv[])
  92. {
  93. testing::InitGoogleTest(&argc, argv);
  94. google::InitGoogleLogging(argv[0]);
  95. google::ParseCommandLineFlags(&argc, &argv, true);
  96. return RUN_ALL_TESTS();
  97. }