summaryrefslogtreecommitdiffstats
path: root/src/seastar/tests/unit/mock_file.hh
blob: 3ee4769519e26954423f54e27277863a9c8ad2e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
 * This file is open source software, licensed to you under the terms
 * of the Apache License, Version 2.0 (the "License").  See the NOTICE file
 * distributed with this work for additional information regarding copyright
 * ownership.  You may not use this file except in compliance with the License.
 *
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
/*
 * Copyright (C) 2017 ScyllaDB Ltd.
 */

#pragma once

#include <boost/range/numeric.hpp>

#include <seastar/testing/seastar_test.hh>
#include <seastar/core/file.hh>

namespace seastar {

class mock_read_only_file final : public file_impl {
    bool _closed = false;
    uint64_t _total_file_size;
    size_t _allowed_read_requests = 0;
    std::function<void(size_t)> _verify_length;
private:
    size_t verify_read(uint64_t position, size_t length) {
        BOOST_CHECK(!_closed);
        BOOST_CHECK_LE(position, _total_file_size);
        BOOST_CHECK_LE(position + length, _total_file_size);
        if (position + length != _total_file_size) {
            _verify_length(length);
        }
        BOOST_CHECK(_allowed_read_requests);
        assert(_allowed_read_requests);
        _allowed_read_requests--;
        return length;
    }
public:
    explicit mock_read_only_file(uint64_t file_size) noexcept
        : _total_file_size(file_size)
        , _verify_length([] (auto) { })
    { }

    void set_read_size_verifier(std::function<void(size_t)> fn) {
        _verify_length = fn;
    }
    void set_expected_read_size(size_t expected) {
        _verify_length = [expected] (auto length) {
            BOOST_CHECK_EQUAL(length, expected);
        };
    }
    void set_allowed_read_requests(size_t requests) {
        _allowed_read_requests = requests;
    }

    virtual future<size_t> write_dma(uint64_t, const void*, size_t, const io_priority_class&) override {
        throw std::bad_function_call();
    }
    virtual future<size_t> write_dma(uint64_t, std::vector<iovec>, const io_priority_class&) override {
        throw std::bad_function_call();
    }
    virtual future<size_t> read_dma(uint64_t pos, void*, size_t len, const io_priority_class&) override {
        return make_ready_future<size_t>(verify_read(pos, len));
    }
    virtual future<size_t> read_dma(uint64_t pos, std::vector<iovec> iov, const io_priority_class&) override {
        auto length = boost::accumulate(iov | boost::adaptors::transformed([] (auto&& iov) { return iov.iov_len; }),
                                        size_t(0), std::plus<size_t>());
        return make_ready_future<size_t>(verify_read(pos, length));
    }
    virtual future<> flush() override {
        return make_ready_future<>();
    }
    virtual future<struct stat> stat() override {
        throw std::bad_function_call();
    }
    virtual future<> truncate(uint64_t) {
        throw std::bad_function_call();
    }
    virtual future<> discard(uint64_t offset, uint64_t length) override {
        throw std::bad_function_call();
    }
    virtual future<> allocate(uint64_t position, uint64_t length) override {
        throw std::bad_function_call();
    }
    virtual future<uint64_t> size() override {
        return make_ready_future<uint64_t>(_total_file_size);
    }
    virtual future<> close() override {
        BOOST_CHECK(!_closed);
        _closed = true;
        return make_ready_future<>();
    }
    virtual subscription<directory_entry> list_directory(std::function<future<> (directory_entry de)>) override {
        throw std::bad_function_call();
    }
    virtual future<temporary_buffer<uint8_t>> dma_read_bulk(uint64_t offset, size_t range_size, const io_priority_class&) override {
        auto length = verify_read(offset, range_size);
        return make_ready_future<temporary_buffer<uint8_t>>(temporary_buffer<uint8_t>(length));
    }
};

}