RpcServerTest.cc 2.5 KB

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