RpcServerTest.cc 2.5 KB

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