RpcServerTest.cc 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include <functional>
  2. #include <memory>
  3. #include <boost/asio.hpp>
  4. #include <boost/threadpool.hpp>
  5. #include <gtest/gtest.h>
  6. #include <glog/logging.h>
  7. #include <gflags/gflags.h>
  8. #include <google/protobuf/service.h>
  9. #include "Thread/CountBarrier.h"
  10. #include "Rpc/RpcClient.h"
  11. #include "Rpc/RpcServer.h"
  12. #include "Rpc/RpcSession.h"
  13. #include "Rpc/RpcServerMock.h"
  14. #include "Rpc/Echo.pb.h"
  15. namespace Egametang {
  16. static int globalPort = 10003;
  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. MethodMap& GetMethodMap(RpcServerPtr server)
  42. {
  43. return server->methods;
  44. }
  45. };
  46. TEST_F(RpcServerTest, ClientAndServer)
  47. {
  48. boost::asio::io_service ioClient;
  49. boost::asio::io_service ioServer;
  50. boost::threadpool::fifo_pool threadPool(2);
  51. auto echoSevice = std::make_shared<MyEcho>();
  52. auto server = std::make_shared<RpcServer>(ioServer, globalPort);
  53. // 注册service
  54. server->Register(echoSevice);
  55. ASSERT_EQ(1U, GetMethodMap(server).size());
  56. auto client = std::make_shared<RpcClient>(ioClient, "127.0.0.1", globalPort);
  57. EchoService_Stub service(client.get());
  58. // 定义消息
  59. EchoRequest request;
  60. request.set_num(100);
  61. EchoResponse response;
  62. ASSERT_EQ(0U, response.num());
  63. // server和client分别在两个不同的线程
  64. threadPool.schedule(std::bind(&IOServiceRun, &ioServer));
  65. // 等待server OK
  66. boost::this_thread::sleep(boost::posix_time::milliseconds(100));
  67. threadPool.schedule(std::bind(&IOServiceRun, &ioClient));
  68. CountBarrier barrier;
  69. service.Echo(nullptr, &request, &response,
  70. google::protobuf::NewCallback(&barrier, &CountBarrier::Signal));
  71. barrier.Wait();
  72. // 加入任务队列,等client和server stop,io_service才stop
  73. ioClient.post(std::bind(&boost::asio::io_service::stop, &ioClient));
  74. ioServer.post(std::bind(&boost::asio::io_service::stop, &ioServer));
  75. // 必须主动让client和server stop才能wait线程
  76. threadPool.wait();
  77. ASSERT_EQ(100, response.num());
  78. }
  79. } // namespace Egametang
  80. int main(int argc, char* argv[])
  81. {
  82. testing::InitGoogleTest(&argc, argv);
  83. google::InitGoogleLogging(argv[0]);
  84. google::ParseCommandLineFlags(&argc, &argv, true);
  85. return RUN_ALL_TESTS();
  86. }