RpcServerTest.cc 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include <boost/bind.hpp>
  2. #include <boost/asio.hpp>
  3. #include <boost/function.hpp>
  4. #include <gtest/gtest.h>
  5. #include <gflags/gflags.h>
  6. #include <glog/logging.h>
  7. #include <google/protobuf/service.h>
  8. #include "Thread/CountBarrier.h"
  9. #include "Thread/ThreadPool.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. #include "Thread/ThreadPool.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. ThreadPool threadPool(2);
  59. RpcServicePtr echoSevice(new MyEcho);
  60. RpcServerPtr server(new RpcServer(ioServer, port));
  61. // 注册service
  62. server->Register(echoSevice);
  63. ASSERT_EQ(1U, GetMethodMap(server).size());
  64. RpcClientPtr client(new 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. threadPool.Schedule(boost::bind(&IOServiceRun, &ioClient));
  74. CountBarrier barrier;
  75. service.Echo(NULL, &request, &response,
  76. google::protobuf::NewCallback(&barrier, &CountBarrier::Signal));
  77. barrier.Wait();
  78. // 加入到io线程
  79. ioClient.post(boost::bind(&RpcClient::Stop, client));
  80. ioServer.post(boost::bind(&RpcServer::Stop, server));
  81. // 加入任务队列,等client和server stop,io_service才stop
  82. ioClient.post(boost::bind(&boost::asio::io_service::stop, &ioClient));
  83. ioServer.post(boost::bind(&boost::asio::io_service::stop, &ioServer));
  84. // 必须主动让client和server stop才能wait线程
  85. threadPool.Wait();
  86. ASSERT_EQ(100, response.num());
  87. }
  88. } // namespace Egametang
  89. int main(int argc, char* argv[])
  90. {
  91. testing::InitGoogleTest(&argc, argv);
  92. google::ParseCommandLineFlags(&argc, &argv, true);
  93. google::InitGoogleLogging(argv[0]);
  94. return RUN_ALL_TESTS();
  95. }