RpcServerTest.cc 2.6 KB

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