mirror of
https://github.com/mhx/dwarfs.git
synced 2025-09-08 11:59:48 -04:00
test: more os_access_mock features
This commit is contained in:
parent
2c5ef8ff23
commit
407b75b28c
@ -143,11 +143,17 @@ void os_access_mock::mock_directory::add(std::string const& name,
|
||||
|
||||
class dir_reader_mock : public dir_reader {
|
||||
public:
|
||||
explicit dir_reader_mock(std::vector<fs::path>&& files)
|
||||
explicit dir_reader_mock(std::vector<fs::path>&& files,
|
||||
std::chrono::nanoseconds delay)
|
||||
: files_(files)
|
||||
, index_(0) {}
|
||||
, index_(0)
|
||||
, delay_{delay} {}
|
||||
|
||||
bool read(fs::path& name) override {
|
||||
if (delay_ > std::chrono::nanoseconds::zero()) {
|
||||
std::this_thread::sleep_for(delay_);
|
||||
}
|
||||
|
||||
if (index_ < files_.size()) {
|
||||
name = files_[index_++];
|
||||
return true;
|
||||
@ -159,6 +165,7 @@ class dir_reader_mock : public dir_reader {
|
||||
private:
|
||||
std::vector<fs::path> files_;
|
||||
size_t index_;
|
||||
std::chrono::nanoseconds const delay_;
|
||||
};
|
||||
|
||||
os_access_mock::os_access_mock()
|
||||
@ -339,6 +346,11 @@ void os_access_mock::set_map_file_error(std::filesystem::path const& path,
|
||||
e.remaining_successful_attempts = after_n_attempts;
|
||||
}
|
||||
|
||||
void os_access_mock::set_map_file_delay(std::filesystem::path const& path,
|
||||
std::chrono::nanoseconds delay) {
|
||||
map_file_delays_[path] = delay;
|
||||
}
|
||||
|
||||
size_t os_access_mock::size() const { return root_ ? root_->size() : 0; }
|
||||
|
||||
std::vector<std::string> os_access_mock::splitpath(fs::path const& path) {
|
||||
@ -408,7 +420,8 @@ os_access_mock::opendir(fs::path const& path) const {
|
||||
std::get<std::unique_ptr<mock_directory>>(de->v)->ent) {
|
||||
files.push_back(path / e.name);
|
||||
}
|
||||
return std::make_unique<dir_reader_mock>(std::move(files));
|
||||
return std::make_unique<dir_reader_mock>(std::move(files),
|
||||
dir_reader_delay_);
|
||||
}
|
||||
|
||||
throw std::runtime_error(fmt::format("oops in opendir: {}", path.string()));
|
||||
@ -447,6 +460,12 @@ os_access_mock::map_file(fs::path const& path, size_t size) const {
|
||||
}
|
||||
}
|
||||
|
||||
if (size >= map_file_delay_min_size_) {
|
||||
if (auto it = map_file_delays_.find(path); it != map_file_delays_.end()) {
|
||||
std::this_thread::sleep_for(it->second);
|
||||
}
|
||||
}
|
||||
|
||||
return std::make_unique<mmap_mock>(
|
||||
de->v | match{
|
||||
[this](std::string const& str) { return str; },
|
||||
|
@ -110,6 +110,12 @@ class os_access_mock : public os_access {
|
||||
void set_access_fail(std::filesystem::path const& path);
|
||||
void set_map_file_error(std::filesystem::path const& path,
|
||||
std::exception_ptr ep, int after_n_attempts = 0);
|
||||
void set_map_file_delay(std::filesystem::path const& path,
|
||||
std::chrono::nanoseconds delay);
|
||||
|
||||
void set_map_file_delay_min_size(size_t size) {
|
||||
map_file_delay_min_size_ = size;
|
||||
}
|
||||
|
||||
void setenv(std::string name, std::string value);
|
||||
|
||||
@ -147,6 +153,10 @@ class os_access_mock : public os_access {
|
||||
|
||||
std::set<std::filesystem::path> get_failed_paths() const;
|
||||
|
||||
void set_dir_reader_delay(std::chrono::nanoseconds delay) {
|
||||
dir_reader_delay_ = delay;
|
||||
}
|
||||
|
||||
std::vector<
|
||||
std::tuple<std::thread::id, std::vector<int>>> mutable set_affinity_calls;
|
||||
|
||||
@ -170,6 +180,9 @@ class os_access_mock : public os_access {
|
||||
std::map<std::string, std::string> env_;
|
||||
std::shared_ptr<os_access> real_os_;
|
||||
executable_resolver_type executable_resolver_;
|
||||
std::chrono::nanoseconds dir_reader_delay_{0};
|
||||
std::map<std::filesystem::path, std::chrono::nanoseconds> map_file_delays_;
|
||||
size_t map_file_delay_min_size_{0};
|
||||
};
|
||||
|
||||
class script_mock : public script {
|
||||
|
Loading…
x
Reference in New Issue
Block a user