rpc_engine_test.cc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include "mock_connection.h"
  19. #include "test.pb.h"
  20. #include "RpcHeader.pb.h"
  21. #include "rpc/rpc_connection.h"
  22. #include <google/protobuf/io/coded_stream.h>
  23. #include <gmock/gmock.h>
  24. using ::hadoop::common::RpcResponseHeaderProto;
  25. using ::hadoop::common::EmptyRequestProto;
  26. using ::hadoop::common::EmptyResponseProto;
  27. using ::hadoop::common::EchoRequestProto;
  28. using ::hadoop::common::EchoResponseProto;
  29. using ::asio::error_code;
  30. using ::testing::Return;
  31. using ::std::make_pair;
  32. using ::std::string;
  33. namespace pb = ::google::protobuf;
  34. namespace pbio = ::google::protobuf::io;
  35. namespace hdfs {
  36. class MockRPCConnection : public MockConnectionBase {
  37. public:
  38. MockRPCConnection(::asio::io_service &io_service)
  39. : MockConnectionBase(&io_service) {}
  40. MOCK_METHOD0(Produce, ProducerResult());
  41. template <class Endpoint, class Callback>
  42. void async_connect(const Endpoint &, Callback &&handler) {
  43. handler(::asio::error_code());
  44. }
  45. void cancel() {}
  46. void close() {}
  47. };
  48. static inline std::pair<error_code, string>
  49. RpcResponse(const RpcResponseHeaderProto &h, const std::string &data,
  50. const ::asio::error_code &ec = error_code()) {
  51. uint32_t payload_length =
  52. pbio::CodedOutputStream::VarintSize32(h.ByteSize()) +
  53. pbio::CodedOutputStream::VarintSize32(data.size()) + h.ByteSize() +
  54. data.size();
  55. std::string res;
  56. res.resize(sizeof(uint32_t) + payload_length);
  57. uint8_t *buf = reinterpret_cast<uint8_t *>(const_cast<char *>(res.c_str()));
  58. buf = pbio::CodedOutputStream::WriteLittleEndian32ToArray(
  59. htonl(payload_length), buf);
  60. buf = pbio::CodedOutputStream::WriteVarint32ToArray(h.ByteSize(), buf);
  61. buf = h.SerializeWithCachedSizesToArray(buf);
  62. buf = pbio::CodedOutputStream::WriteVarint32ToArray(data.size(), buf);
  63. buf = pbio::CodedOutputStream::WriteStringToArray(data, buf);
  64. return std::make_pair(ec, std::move(res));
  65. }
  66. }
  67. using namespace hdfs;
  68. TEST(RpcEngineTest, TestRoundTrip) {
  69. ::asio::io_service io_service;
  70. Options options;
  71. RpcEngine engine(&io_service, options, "foo", "protocol", 1);
  72. RpcConnectionImpl<MockRPCConnection> *conn =
  73. new RpcConnectionImpl<MockRPCConnection>(&engine);
  74. EchoResponseProto server_resp;
  75. server_resp.set_message("foo");
  76. RpcResponseHeaderProto h;
  77. h.set_callid(1);
  78. h.set_status(RpcResponseHeaderProto::SUCCESS);
  79. EXPECT_CALL(conn->next_layer(), Produce())
  80. .WillOnce(Return(RpcResponse(h, server_resp.SerializeAsString())));
  81. std::unique_ptr<RpcConnection> conn_ptr(conn);
  82. engine.TEST_SetRpcConnection(&conn_ptr);
  83. EchoRequestProto req;
  84. req.set_message("foo");
  85. std::shared_ptr<EchoResponseProto> resp(new EchoResponseProto());
  86. engine.AsyncRpc("test", &req, resp, [resp, &io_service](const Status &stat) {
  87. ASSERT_TRUE(stat.ok());
  88. ASSERT_EQ("foo", resp->message());
  89. io_service.stop();
  90. });
  91. conn->Start();
  92. io_service.run();
  93. }
  94. TEST(RpcEngineTest, TestConnectionReset) {
  95. ::asio::io_service io_service;
  96. Options options;
  97. RpcEngine engine(&io_service, options, "foo", "protocol", 1);
  98. RpcConnectionImpl<MockRPCConnection> *conn =
  99. new RpcConnectionImpl<MockRPCConnection>(&engine);
  100. RpcResponseHeaderProto h;
  101. h.set_callid(1);
  102. h.set_status(RpcResponseHeaderProto::SUCCESS);
  103. EXPECT_CALL(conn->next_layer(), Produce())
  104. .WillOnce(Return(RpcResponse(
  105. h, "", make_error_code(::asio::error::connection_reset))));
  106. std::unique_ptr<RpcConnection> conn_ptr(conn);
  107. engine.TEST_SetRpcConnection(&conn_ptr);
  108. EchoRequestProto req;
  109. req.set_message("foo");
  110. std::shared_ptr<EchoResponseProto> resp(new EchoResponseProto());
  111. engine.AsyncRpc("test", &req, resp, [&io_service](const Status &stat) {
  112. ASSERT_FALSE(stat.ok());
  113. });
  114. engine.AsyncRpc("test", &req, resp, [&io_service](const Status &stat) {
  115. io_service.stop();
  116. ASSERT_FALSE(stat.ok());
  117. });
  118. conn->Start();
  119. io_service.run();
  120. }
  121. TEST(RpcEngineTest, TestTimeout) {
  122. ::asio::io_service io_service;
  123. Options options;
  124. options.rpc_timeout = 1;
  125. RpcEngine engine(&io_service, options, "foo", "protocol", 1);
  126. RpcConnectionImpl<MockRPCConnection> *conn =
  127. new RpcConnectionImpl<MockRPCConnection>(&engine);
  128. EXPECT_CALL(conn->next_layer(), Produce()).Times(0);
  129. std::unique_ptr<RpcConnection> conn_ptr(conn);
  130. engine.TEST_SetRpcConnection(&conn_ptr);
  131. EchoRequestProto req;
  132. req.set_message("foo");
  133. std::shared_ptr<EchoResponseProto> resp(new EchoResponseProto());
  134. engine.AsyncRpc("test", &req, resp, [resp, &io_service](const Status &stat) {
  135. io_service.stop();
  136. ASSERT_FALSE(stat.ok());
  137. });
  138. ::asio::deadline_timer timer(io_service);
  139. timer.expires_from_now(std::chrono::milliseconds(options.rpc_timeout * 2));
  140. timer.async_wait(std::bind(&RpcConnection::Start, conn));
  141. io_service.run();
  142. }
  143. int main(int argc, char *argv[]) {
  144. // The following line must be executed to initialize Google Mock
  145. // (and Google Test) before running the tests.
  146. ::testing::InitGoogleMock(&argc, argv);
  147. return RUN_ALL_TESTS();
  148. }