mock_connection.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. #ifndef LIBHDFSPP_TEST_MOCK_CONNECTION_H_
  19. #define LIBHDFSPP_TEST_MOCK_CONNECTION_H_
  20. #include <asio/error_code.hpp>
  21. #include <asio/buffer.hpp>
  22. #include <asio/streambuf.hpp>
  23. #include <gmock/gmock.h>
  24. namespace hdfs {
  25. class MockConnectionBase {
  26. public:
  27. virtual ~MockConnectionBase();
  28. typedef std::pair<asio::error_code, std::string> ProducerResult;
  29. template <class MutableBufferSequence, class Handler>
  30. void async_read_some(const MutableBufferSequence &buf, Handler &&handler) {
  31. if (produced_.size() == 0) {
  32. ProducerResult r = Produce();
  33. if (r.first) {
  34. handler(r.first, 0);
  35. }
  36. asio::mutable_buffers_1 data = produced_.prepare(r.second.size());
  37. asio::buffer_copy(data, asio::buffer(r.second));
  38. produced_.commit(r.second.size());
  39. }
  40. size_t len = std::min(asio::buffer_size(buf), produced_.size());
  41. asio::buffer_copy(buf, produced_.data());
  42. produced_.consume(len);
  43. handler(asio::error_code(), len);
  44. }
  45. template <class ConstBufferSequence, class Handler>
  46. void async_write_some(const ConstBufferSequence &buf, Handler &&handler) {
  47. // CompletionResult res = OnWrite(buf);
  48. handler(asio::error_code(), asio::buffer_size(buf));
  49. }
  50. protected:
  51. virtual ProducerResult Produce() = 0;
  52. private:
  53. asio::streambuf produced_;
  54. };
  55. }
  56. #endif