]> git.proxmox.com Git - ceph.git/blob - ceph/src/seastar/tests/unit/mock_file.hh
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / seastar / tests / unit / mock_file.hh
1 /*
2 * This file is open source software, licensed to you under the terms
3 * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
4 * distributed with this work for additional information regarding copyright
5 * ownership. You may not use this file except in compliance with the License.
6 *
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing,
12 * software distributed under the License is distributed on an
13 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 * KIND, either express or implied. See the License for the
15 * specific language governing permissions and limitations
16 * under the License.
17 */
18 /*
19 * Copyright (C) 2017 ScyllaDB Ltd.
20 */
21
22 #pragma once
23
24 #include <boost/range/numeric.hpp>
25
26 #include <seastar/testing/seastar_test.hh>
27 #include <seastar/core/file.hh>
28
29 namespace seastar {
30
31 class mock_read_only_file final : public file_impl {
32 bool _closed = false;
33 uint64_t _total_file_size;
34 size_t _allowed_read_requests = 0;
35 std::function<void(size_t)> _verify_length;
36 private:
37 size_t verify_read(uint64_t position, size_t length) {
38 BOOST_CHECK(!_closed);
39 BOOST_CHECK_LE(position, _total_file_size);
40 BOOST_CHECK_LE(position + length, _total_file_size);
41 if (position + length != _total_file_size) {
42 _verify_length(length);
43 }
44 BOOST_CHECK(_allowed_read_requests);
45 assert(_allowed_read_requests);
46 _allowed_read_requests--;
47 return length;
48 }
49 public:
50 explicit mock_read_only_file(uint64_t file_size) noexcept
51 : _total_file_size(file_size)
52 , _verify_length([] (auto) { })
53 { }
54
55 void set_read_size_verifier(std::function<void(size_t)> fn) {
56 _verify_length = fn;
57 }
58 void set_expected_read_size(size_t expected) {
59 _verify_length = [expected] (auto length) {
60 BOOST_CHECK_EQUAL(length, expected);
61 };
62 }
63 void set_allowed_read_requests(size_t requests) {
64 _allowed_read_requests = requests;
65 }
66
67 virtual future<size_t> write_dma(uint64_t, const void*, size_t, const io_priority_class&) override {
68 throw std::bad_function_call();
69 }
70 virtual future<size_t> write_dma(uint64_t, std::vector<iovec>, const io_priority_class&) override {
71 throw std::bad_function_call();
72 }
73 virtual future<size_t> read_dma(uint64_t pos, void*, size_t len, const io_priority_class&) override {
74 return make_ready_future<size_t>(verify_read(pos, len));
75 }
76 virtual future<size_t> read_dma(uint64_t pos, std::vector<iovec> iov, const io_priority_class&) override {
77 auto length = boost::accumulate(iov | boost::adaptors::transformed([] (auto&& iov) { return iov.iov_len; }),
78 size_t(0), std::plus<size_t>());
79 return make_ready_future<size_t>(verify_read(pos, length));
80 }
81 virtual future<> flush() override {
82 return make_ready_future<>();
83 }
84 virtual future<struct stat> stat() override {
85 throw std::bad_function_call();
86 }
87 virtual future<> truncate(uint64_t) {
88 throw std::bad_function_call();
89 }
90 virtual future<> discard(uint64_t offset, uint64_t length) override {
91 throw std::bad_function_call();
92 }
93 virtual future<> allocate(uint64_t position, uint64_t length) override {
94 throw std::bad_function_call();
95 }
96 virtual future<uint64_t> size() override {
97 return make_ready_future<uint64_t>(_total_file_size);
98 }
99 virtual future<> close() override {
100 BOOST_CHECK(!_closed);
101 _closed = true;
102 return make_ready_future<>();
103 }
104 virtual subscription<directory_entry> list_directory(std::function<future<> (directory_entry de)>) override {
105 throw std::bad_function_call();
106 }
107 virtual future<temporary_buffer<uint8_t>> dma_read_bulk(uint64_t offset, size_t range_size, const io_priority_class&) override {
108 auto length = verify_read(offset, range_size);
109 return make_ready_future<temporary_buffer<uint8_t>>(temporary_buffer<uint8_t>(length));
110 }
111 };
112
113 }