From 1511603b6a151a5157fbf6108b59473680489f86 Mon Sep 17 00:00:00 2001 From: "Scott E. Graves" Date: Sat, 21 Dec 2024 08:35:38 -0600 Subject: [PATCH] unit tests and fixes --- .cspell/words.txt | 1 + .../include/file_manager/cache_size_mgr.hpp | 4 +- .../librepertory/include/types/repertory.hpp | 1 + .../src/file_manager/cache_size_mgr.cpp | 9 +- .../src/file_manager/open_file.cpp | 12 +- .../src/rpc/server/full_server.cpp | 3 +- .../librepertory/src/types/repertory.cpp | 9 +- .../repertory_test/src/file_manager_test.cpp | 8 +- .../repertory_test/src/open_file_test.cpp | 526 ++++++++++-------- 9 files changed, 315 insertions(+), 258 deletions(-) diff --git a/.cspell/words.txt b/.cspell/words.txt index c76e0db5..35f9955b 100644 --- a/.cspell/words.txt +++ b/.cspell/words.txt @@ -161,6 +161,7 @@ openssldir pkgconfig plarge_integer plex +println project_enable_fontconfig project_enable_gtkmm project_enable_libdsm diff --git a/repertory/librepertory/include/file_manager/cache_size_mgr.hpp b/repertory/librepertory/include/file_manager/cache_size_mgr.hpp index 022d80a6..c1395404 100644 --- a/repertory/librepertory/include/file_manager/cache_size_mgr.hpp +++ b/repertory/librepertory/include/file_manager/cache_size_mgr.hpp @@ -45,7 +45,7 @@ private: private: app_config *cfg_{nullptr}; std::uint64_t cache_size_{0U}; - std::mutex mtx_; + mutable std::mutex mtx_; std::condition_variable notify_; stop_type stop_requested_{false}; @@ -58,6 +58,8 @@ public: [[nodiscard]] auto shrink(std::uint64_t size) -> api_error; + [[nodiscard]] auto size() const -> std::uint64_t; + void stop(); }; } // namespace repertory diff --git a/repertory/librepertory/include/types/repertory.hpp b/repertory/librepertory/include/types/repertory.hpp index b78c0483..8ae7b64c 100644 --- a/repertory/librepertory/include/types/repertory.hpp +++ b/repertory/librepertory/include/types/repertory.hpp @@ -175,6 +175,7 @@ enum class api_error { bad_address, buffer_overflow, buffer_too_small, + cache_not_initialized, comm_error, decryption_error, directory_end_of_files, diff --git a/repertory/librepertory/src/file_manager/cache_size_mgr.cpp b/repertory/librepertory/src/file_manager/cache_size_mgr.cpp index eec02386..ec4cde4b 100644 --- a/repertory/librepertory/src/file_manager/cache_size_mgr.cpp +++ b/repertory/librepertory/src/file_manager/cache_size_mgr.cpp @@ -49,7 +49,7 @@ auto cache_size_mgr::expand(std::uint64_t size) -> api_error { unique_mutex_lock lock(mtx_); if (cfg_ == nullptr) { - return api_error::error; + return api_error::cache_not_initialized; } cache_size_ += size; @@ -98,7 +98,7 @@ auto cache_size_mgr::shrink(std::uint64_t size) -> api_error { if (cache_size_ >= size) { cache_size_ -= size; } else { - event_system::instance().raise(cache_size_, size); + event_system::instance().raise(cache_size_, size); cache_size_ = 0U; } @@ -107,6 +107,11 @@ auto cache_size_mgr::shrink(std::uint64_t size) -> api_error { return stop_requested_ ? api_error::error : api_error::success; } +auto cache_size_mgr::size() const -> std::uint64_t { + mutex_lock lock(mtx_); + return cache_size_; +} + void cache_size_mgr::stop() { if (stop_requested_) { return; diff --git a/repertory/librepertory/src/file_manager/open_file.cpp b/repertory/librepertory/src/file_manager/open_file.cpp index 833a47ae..4827b990 100644 --- a/repertory/librepertory/src/file_manager/open_file.cpp +++ b/repertory/librepertory/src/file_manager/open_file.cpp @@ -89,9 +89,17 @@ open_file::open_file(std::uint64_t chunk_size, std::uint8_t chunk_timeout, false); auto file_size = nf_->size(); - if (provider_.is_read_only() || file_size == fsi.size) { + if (provider_.is_read_only() || file_size.value() == fsi.size) { read_state_.set(0U, read_state_.size(), true); - } else if (not nf_->truncate(fsi.size)) { + } else if (nf_->truncate(fsi.size)) { + if (file_size.value() > fsi.size) { + set_api_error( + cache_size_mgr::instance().shrink(file_size.value() - fsi.size)); + } else { + set_api_error( + cache_size_mgr::instance().expand(fsi.size - file_size.value())); + } + } else { set_api_error(api_error::os_error); } } diff --git a/repertory/librepertory/src/rpc/server/full_server.cpp b/repertory/librepertory/src/rpc/server/full_server.cpp index db90669d..f9ea3f36 100644 --- a/repertory/librepertory/src/rpc/server/full_server.cpp +++ b/repertory/librepertory/src/rpc/server/full_server.cpp @@ -24,6 +24,7 @@ #include "app_config.hpp" #include "events/event_system.hpp" #include "events/events.hpp" +#include "file_manager/cache_size_mgr.hpp" #include "file_manager/i_file_manager.hpp" #include "providers/i_provider.hpp" #include "types/repertory.hpp" @@ -54,7 +55,7 @@ void full_server::handle_get_drive_information(const httplib::Request & /*req*/, utils::file::directory(get_config().get_cache_directory()).size(); res.set_content( json({ - {"cache_space_used", dir_size}, + {"cache_space_used", cache_size_mgr::instance().size()}, {"drive_space_total", provider_.get_total_drive_space()}, {"drive_space_used", provider_.get_used_drive_space()}, {"item_count", provider_.get_total_item_count()}, diff --git a/repertory/librepertory/src/types/repertory.cpp b/repertory/librepertory/src/types/repertory.cpp index bd89bdf0..c8ed6b70 100644 --- a/repertory/librepertory/src/types/repertory.cpp +++ b/repertory/librepertory/src/types/repertory.cpp @@ -25,8 +25,8 @@ #include "utils/string.hpp" namespace repertory { -auto database_type_from_string(std::string type, database_type default_type) - -> database_type { +auto database_type_from_string(std::string type, + database_type default_type) -> database_type { type = utils::string::to_lower(utils::string::trim(type)); if (type == "rocksdb") { return database_type::rocksdb; @@ -50,8 +50,8 @@ auto database_type_to_string(const database_type &type) -> std::string { } } -auto download_type_from_string(std::string type, download_type default_type) - -> download_type { +auto download_type_from_string(std::string type, + download_type default_type) -> download_type { type = utils::string::to_lower(utils::string::trim(type)); if (type == "direct") { return download_type::direct; @@ -87,6 +87,7 @@ static const std::unordered_map LOOKUP = { {api_error::bad_address, "bad_address"}, {api_error::buffer_overflow, "buffer_overflow"}, {api_error::buffer_too_small, "buffer_too_small"}, + {api_error::cache_not_initialized, "cache_not_initialized"}, {api_error::comm_error, "comm_error"}, {api_error::decryption_error, "decryption_error"}, {api_error::directory_end_of_files, "directory_end_of_files"}, diff --git a/repertory/repertory_test/src/file_manager_test.cpp b/repertory/repertory_test/src/file_manager_test.cpp index cdb552a5..cf8caafa 100644 --- a/repertory/repertory_test/src/file_manager_test.cpp +++ b/repertory/repertory_test/src/file_manager_test.cpp @@ -22,6 +22,7 @@ #include "test_common.hpp" #include "app_config.hpp" +#include "file_manager/cache_size_mgr.hpp" #include "file_manager/events.hpp" #include "file_manager/file_manager.hpp" #include "file_manager/i_open_file.hpp" @@ -51,7 +52,7 @@ auto file_manager::open(std::shared_ptr of, class file_manager_test : public ::testing::Test { public: - console_consumer c; + console_consumer con_consumer; std::unique_ptr cfg; mock_provider mp; static std::atomic inst; @@ -67,6 +68,8 @@ protected: cfg = std::make_unique(provider_type::sia, file_manager_dir); cfg->set_enable_download_timeout(false); + + cache_size_mgr::instance().initialize(cfg.get()); } void TearDown() override { event_system::instance().stop(); } @@ -696,7 +699,8 @@ TEST_F(file_manager_test, can_evict_file) { data_buffer data{{0, 1, 1}}; std::size_t bytes_written{}; - EXPECT_EQ(api_error::success, open_file->write(0U, data, bytes_written)); + auto res = open_file->write(0U, data, bytes_written); + EXPECT_EQ(api_error::success, res); auto opt_size = utils::file::file{source_path}.size(); EXPECT_TRUE(opt_size.has_value()); diff --git a/repertory/repertory_test/src/open_file_test.cpp b/repertory/repertory_test/src/open_file_test.cpp index 173eb95d..5e3eb19b 100644 --- a/repertory/repertory_test/src/open_file_test.cpp +++ b/repertory/repertory_test/src/open_file_test.cpp @@ -21,6 +21,8 @@ */ #include "test_common.hpp" +#include "app_config.hpp" +#include "file_manager/cache_size_mgr.hpp" #include "file_manager/open_file.hpp" #include "mocks/mock_provider.hpp" #include "mocks/mock_upload_manager.hpp" @@ -29,190 +31,219 @@ #include "utils/path.hpp" namespace repertory { -static constexpr const std::size_t test_chunk_size = 1024u; +static constexpr const std::size_t test_chunk_size{1024U}; -static void test_closeable_open_file(const open_file &o, bool directory, - const api_error &e, std::uint64_t size, +class open_file_test : public ::testing::Test { +public: + console_consumer con_consumer; + std::unique_ptr cfg; + static std::atomic inst; + mock_provider provider; + mock_upload_manager upload_mgr; + +protected: + void SetUp() override { + event_system::instance().start(); + + auto open_file_dir = repertory::utils::path::combine( + repertory::test::get_test_output_dir(), + {"open_file_test" + std::to_string(++inst)}); + + cfg = std::make_unique(provider_type::sia, open_file_dir); + + cache_size_mgr::instance().initialize(cfg.get()); + } + + void TearDown() override { event_system::instance().stop(); } +}; + +std::atomic open_file_test::inst{0U}; + +static void test_closeable_open_file(const open_file &file, bool directory, + const api_error &err, std::uint64_t size, const std::string &source_path) { - EXPECT_EQ(directory, o.is_directory()); - EXPECT_EQ(e, o.get_api_error()); - EXPECT_EQ(std::size_t(0u), o.get_open_file_count()); - EXPECT_EQ(std::uint64_t(size), o.get_file_size()); - EXPECT_STREQ(source_path.c_str(), o.get_source_path().c_str()); - EXPECT_TRUE(o.can_close()); + EXPECT_EQ(directory, file.is_directory()); + EXPECT_EQ(err, file.get_api_error()); + EXPECT_EQ(std::size_t(0U), file.get_open_file_count()); + EXPECT_EQ(std::uint64_t(size), file.get_file_size()); + EXPECT_STREQ(source_path.c_str(), file.get_source_path().c_str()); + EXPECT_TRUE(file.can_close()); } -static void validate_write(open_file &o, std::size_t offset, data_buffer data, - std::size_t bytes_written) { +static void validate_write(open_file &file, std::size_t offset, + data_buffer data, std::size_t bytes_written) { EXPECT_EQ(data.size(), bytes_written); data_buffer read_data{}; - EXPECT_EQ(api_error::success, o.read(data.size(), offset, read_data)); + EXPECT_EQ(api_error::success, file.read(data.size(), offset, read_data)); EXPECT_TRUE(std::equal(data.begin(), data.end(), read_data.begin())); } -TEST(open_file, properly_initializes_state_for_0_byte_file) { +TEST_F(open_file_test, properly_initializes_state_for_0_byte_file) { const auto source_path = test::generate_test_file_name("file_manager_open_file_test"); - mock_provider mp; - mock_upload_manager um; - - EXPECT_CALL(mp, is_read_only()).WillRepeatedly(Return(false)); + EXPECT_CALL(provider, is_read_only()).WillRepeatedly(Return(false)); filesystem_item fsi; fsi.directory = false; fsi.api_path = "/test.txt"; - fsi.size = 0u; + fsi.size = 0U; fsi.source_path = source_path; - open_file o(test_chunk_size, 0U, fsi, mp, um); - EXPECT_EQ(std::size_t(0u), o.get_read_state().size()); - EXPECT_FALSE(o.is_modified()); - EXPECT_EQ(test_chunk_size, o.get_chunk_size()); -} - -TEST(open_file, properly_initializes_state_based_on_chunk_size) { - const auto source_path = - test::generate_test_file_name("file_manager_open_file_test"); - - mock_provider mp; - mock_upload_manager um; - - EXPECT_CALL(mp, is_read_only()).WillRepeatedly(Return(false)); - - filesystem_item fsi; - fsi.directory = false; - fsi.api_path = "/test.txt"; - fsi.size = 8u; - fsi.source_path = source_path; - - EXPECT_CALL(um, remove_resume) + EXPECT_CALL(upload_mgr, remove_resume) .WillOnce( [&fsi](const std::string &api_path, const std::string &source_path2) { EXPECT_EQ(fsi.api_path, api_path); EXPECT_EQ(fsi.source_path, source_path2); }); - open_file o(1u, 0U, fsi, mp, um); - EXPECT_EQ(std::size_t(8u), o.get_read_state().size()); - EXPECT_TRUE(o.get_read_state().none()); - - EXPECT_FALSE(o.is_modified()); - - EXPECT_CALL(mp, set_item_meta(fsi.api_path, META_SOURCE, _)) - .WillOnce(Return(api_error::success)); - EXPECT_EQ(std::size_t(1u), o.get_chunk_size()); + open_file file(test_chunk_size, 0U, fsi, provider, upload_mgr); + EXPECT_EQ(std::size_t(0U), file.get_read_state().size()); + EXPECT_FALSE(file.is_modified()); + EXPECT_EQ(test_chunk_size, file.get_chunk_size()); } -TEST(open_file, will_not_change_source_path_for_0_byte_file) { +TEST_F(open_file_test, properly_initializes_state_based_on_chunk_size) { const auto source_path = test::generate_test_file_name("file_manager_open_file_test"); - mock_provider mp; - mock_upload_manager um; - - EXPECT_CALL(mp, is_read_only()).WillRepeatedly(Return(false)); + EXPECT_CALL(provider, is_read_only()).WillRepeatedly(Return(false)); filesystem_item fsi; fsi.directory = false; fsi.api_path = "/test.txt"; - fsi.size = 0u; + fsi.size = 8U; fsi.source_path = source_path; - open_file o(0u, 0U, fsi, mp, um); - test_closeable_open_file(o, false, api_error::success, 0u, source_path); + EXPECT_CALL(upload_mgr, remove_resume) + .WillOnce( + [&fsi](const std::string &api_path, const std::string &source_path2) { + EXPECT_EQ(fsi.api_path, api_path); + EXPECT_EQ(fsi.source_path, source_path2); + }); - o.close(); - EXPECT_EQ(api_error::success, o.get_api_error()); - EXPECT_STREQ(source_path.c_str(), o.get_source_path().c_str()); - EXPECT_TRUE(utils::file::file(fsi.source_path).exists()); + open_file file(1U, 0U, fsi, provider, upload_mgr); + EXPECT_EQ(std::size_t(8U), file.get_read_state().size()); + EXPECT_TRUE(file.get_read_state().none()); + + EXPECT_FALSE(file.is_modified()); + + EXPECT_CALL(provider, set_item_meta(fsi.api_path, META_SOURCE, _)) + .WillOnce(Return(api_error::success)); + EXPECT_EQ(std::size_t(1U), file.get_chunk_size()); } -TEST(open_file, will_change_source_path_if_file_size_is_greater_than_0) { +TEST_F(open_file_test, will_not_change_source_path_for_0_byte_file) { const auto source_path = test::generate_test_file_name("file_manager_open_file_test"); - mock_provider mp; - mock_upload_manager um; + EXPECT_CALL(provider, is_read_only()).WillRepeatedly(Return(false)); - EXPECT_CALL(mp, is_read_only()).WillRepeatedly(Return(false)); + filesystem_item fsi; + fsi.directory = false; + fsi.api_path = "/test.txt"; + fsi.size = 0U; + fsi.source_path = source_path; + + EXPECT_CALL(upload_mgr, remove_resume) + .WillOnce( + [&fsi](const std::string &api_path, const std::string &source_path2) { + EXPECT_EQ(fsi.api_path, api_path); + EXPECT_EQ(fsi.source_path, source_path2); + }); + + open_file file(0U, 0U, fsi, provider, upload_mgr); + test_closeable_open_file(file, false, api_error::success, 0U, source_path); + + file.close(); + EXPECT_EQ(api_error::success, file.get_api_error()); + EXPECT_STREQ(source_path.c_str(), file.get_source_path().c_str()); + EXPECT_TRUE(utils::file::file(fsi.source_path).exists()); +} + +TEST_F(open_file_test, will_change_source_path_if_file_size_is_greater_than_0) { + const auto source_path = + test::generate_test_file_name("file_manager_open_file_test"); + + EXPECT_CALL(provider, is_read_only()).WillRepeatedly(Return(false)); filesystem_item fsi; fsi.api_path = "/test.txt"; fsi.size = test_chunk_size; fsi.source_path = source_path; - EXPECT_CALL(um, remove_resume) + EXPECT_CALL(upload_mgr, remove_resume) .WillOnce( [&fsi](const std::string &api_path, const std::string &source_path2) { EXPECT_EQ(fsi.api_path, api_path); EXPECT_EQ(fsi.source_path, source_path2); }); - EXPECT_CALL(mp, set_item_meta(fsi.api_path, META_SOURCE, _)) + EXPECT_CALL(provider, set_item_meta(fsi.api_path, META_SOURCE, _)) .WillOnce([&fsi](const std::string &, const std::string &, const std::string &source_path2) -> api_error { EXPECT_STRNE(fsi.source_path.c_str(), source_path2.c_str()); return api_error::success; }); - open_file o(test_chunk_size, 0U, fsi, mp, um); - test_closeable_open_file(o, false, api_error::success, test_chunk_size, + open_file file(test_chunk_size, 0U, fsi, provider, upload_mgr); + test_closeable_open_file(file, false, api_error::success, test_chunk_size, source_path); - o.close(); - EXPECT_EQ(api_error::download_stopped, o.get_api_error()); - EXPECT_STRNE(source_path.c_str(), o.get_source_path().c_str()); + file.close(); + EXPECT_EQ(api_error::download_stopped, file.get_api_error()); + EXPECT_STRNE(source_path.c_str(), file.get_source_path().c_str()); EXPECT_FALSE(utils::file::file(source_path).exists()); } -TEST(open_file, - will_not_change_source_path_if_file_size_matches_existing_source) { +TEST_F(open_file_test, + will_not_change_source_path_if_file_size_matches_existing_source) { auto &rf = test::create_random_file(test_chunk_size); const auto source_path = rf.get_path(); rf.close(); - mock_provider mp; - mock_upload_manager um; - - EXPECT_CALL(mp, is_read_only()).WillRepeatedly(Return(false)); + EXPECT_CALL(provider, is_read_only()).WillRepeatedly(Return(false)); filesystem_item fsi; fsi.api_path = "/test.txt"; fsi.size = test_chunk_size; fsi.source_path = source_path; - open_file o(test_chunk_size, 0U, fsi, mp, um); - test_closeable_open_file(o, false, api_error::success, test_chunk_size, + EXPECT_CALL(upload_mgr, remove_resume) + .WillOnce( + [&fsi](const std::string &api_path, const std::string &source_path2) { + EXPECT_EQ(fsi.api_path, api_path); + EXPECT_EQ(fsi.source_path, source_path2); + }); + + open_file file(test_chunk_size, 0U, fsi, provider, upload_mgr); + test_closeable_open_file(file, false, api_error::success, test_chunk_size, source_path); - o.close(); - EXPECT_EQ(api_error::success, o.get_api_error()); - EXPECT_STREQ(source_path.c_str(), o.get_source_path().c_str()); + file.close(); + EXPECT_EQ(api_error::success, file.get_api_error()); + EXPECT_STREQ(source_path.c_str(), file.get_source_path().c_str()); EXPECT_TRUE(utils::file::file(source_path).exists()); } -TEST(open_file, write_with_incomplete_download) { +TEST_F(open_file_test, write_with_incomplete_download) { const auto source_path = test::generate_test_file_name("test"); auto &nf = test::create_random_file(test_chunk_size * 2u); - mock_provider mp; - mock_upload_manager um; - - EXPECT_CALL(mp, is_read_only()).WillRepeatedly(Return(false)); + EXPECT_CALL(provider, is_read_only()).WillRepeatedly(Return(false)); filesystem_item fsi; fsi.api_path = "/test.txt"; fsi.size = test_chunk_size * 2u; fsi.source_path = source_path; - open_file o(test_chunk_size, 0U, fsi, mp, um); - test_closeable_open_file(o, false, api_error::success, test_chunk_size * 2u, - source_path); + open_file file(test_chunk_size, 0U, fsi, provider, upload_mgr); + test_closeable_open_file(file, false, api_error::success, + test_chunk_size * 2u, source_path); - EXPECT_CALL(mp, set_item_meta(fsi.api_path, _)) + EXPECT_CALL(provider, set_item_meta(fsi.api_path, _)) .WillOnce([](const std::string &, const api_meta_map &meta) -> api_error { EXPECT_NO_THROW(EXPECT_FALSE(meta.at(META_CHANGED).empty())); EXPECT_NO_THROW(EXPECT_FALSE(meta.at(META_MODIFIED).empty())); @@ -220,7 +251,7 @@ TEST(open_file, write_with_incomplete_download) { return api_error::success; }); - EXPECT_CALL(mp, read_file_bytes) + EXPECT_CALL(provider, read_file_bytes) .WillRepeatedly([&nf](const std::string & /* api_path */, std::size_t size, std::uint64_t offset, data_buffer &data, @@ -229,7 +260,7 @@ TEST(open_file, write_with_incomplete_download) { return api_error::download_stopped; } - if (offset == 0u) { + if (offset == 0U) { std::size_t bytes_read{}; data.resize(size); auto ret = nf.read(data, offset, &bytes_read) ? api_error::success @@ -244,11 +275,12 @@ TEST(open_file, write_with_incomplete_download) { return api_error::download_stopped; }); - EXPECT_CALL(um, remove_upload).WillOnce([&fsi](const std::string &api_path) { - EXPECT_EQ(fsi.api_path, api_path); - }); + EXPECT_CALL(upload_mgr, remove_upload) + .WillOnce([&fsi](const std::string &api_path) { + EXPECT_EQ(fsi.api_path, api_path); + }); - EXPECT_CALL(um, store_resume) + EXPECT_CALL(upload_mgr, store_resume) .Times(2) .WillRepeatedly([&fsi](const i_open_file &cur_file) { EXPECT_EQ(fsi.api_path, cur_file.get_api_path()); @@ -257,55 +289,53 @@ TEST(open_file, write_with_incomplete_download) { data_buffer data = {10, 9, 8}; std::size_t bytes_written{}; - EXPECT_EQ(api_error::success, o.write(0u, data, bytes_written)); - validate_write(o, 0u, data, bytes_written); + EXPECT_EQ(api_error::success, file.write(0U, data, bytes_written)); + validate_write(file, 0U, data, bytes_written); const auto test_state = [&]() { - EXPECT_STREQ(source_path.c_str(), o.get_source_path().c_str()); + EXPECT_STREQ(source_path.c_str(), file.get_source_path().c_str()); - EXPECT_FALSE(o.can_close()); + EXPECT_FALSE(file.can_close()); - EXPECT_TRUE(o.is_modified()); + EXPECT_TRUE(file.is_modified()); - EXPECT_TRUE(o.get_read_state(0u)); - EXPECT_FALSE(o.get_read_state(1u)); + EXPECT_TRUE(file.get_read_state(0U)); + EXPECT_FALSE(file.get_read_state(1u)); }; test_state(); - o.close(); + file.close(); nf.close(); test_state(); - EXPECT_EQ(api_error::download_incomplete, o.get_api_error()); + EXPECT_EQ(api_error::download_incomplete, file.get_api_error()); EXPECT_TRUE(utils::file::file(fsi.source_path).exists()); } -TEST(open_file, write_new_file) { +TEST_F(open_file_test, write_new_file) { const auto source_path = test::generate_test_file_name("file_manager_open_file_test"); - mock_provider mp; - mock_upload_manager um; - - EXPECT_CALL(mp, is_read_only()).WillRepeatedly(Return(false)); + EXPECT_CALL(provider, is_read_only()).WillRepeatedly(Return(false)); filesystem_item fsi; fsi.api_path = "/test.txt"; - fsi.size = 0u; + fsi.size = 0U; fsi.source_path = source_path; - EXPECT_CALL(um, store_resume).WillOnce([&fsi](const i_open_file &o) { - EXPECT_EQ(fsi.api_path, o.get_api_path()); - EXPECT_EQ(fsi.source_path, o.get_source_path()); - }); + EXPECT_CALL(upload_mgr, store_resume) + .WillOnce([&fsi](const i_open_file &file) { + EXPECT_EQ(fsi.api_path, file.get_api_path()); + EXPECT_EQ(fsi.source_path, file.get_source_path()); + }); - open_file o(test_chunk_size, 0U, fsi, mp, um); - test_closeable_open_file(o, false, api_error::success, 0u, source_path); + open_file file(test_chunk_size, 0U, fsi, provider, upload_mgr); + test_closeable_open_file(file, false, api_error::success, 0U, source_path); data_buffer data = {10, 9, 8}; - EXPECT_CALL(mp, set_item_meta(fsi.api_path, _)) + EXPECT_CALL(provider, set_item_meta(fsi.api_path, _)) .WillOnce([&data](const std::string &, const api_meta_map &meta) -> api_error { EXPECT_NO_THROW(EXPECT_FALSE(meta.at(META_CHANGED).empty())); @@ -322,62 +352,62 @@ TEST(open_file, write_new_file) { return api_error::success; }); - EXPECT_CALL(um, remove_upload).WillOnce([&fsi](const std::string &api_path) { - EXPECT_EQ(fsi.api_path, api_path); - }); + EXPECT_CALL(upload_mgr, remove_upload) + .WillOnce([&fsi](const std::string &api_path) { + EXPECT_EQ(fsi.api_path, api_path); + }); - EXPECT_CALL(um, queue_upload).WillOnce([&fsi](const i_open_file &cur_file) { - EXPECT_EQ(fsi.api_path, cur_file.get_api_path()); - EXPECT_EQ(fsi.source_path, cur_file.get_source_path()); - }); + EXPECT_CALL(upload_mgr, queue_upload) + .WillOnce([&fsi](const i_open_file &cur_file) { + EXPECT_EQ(fsi.api_path, cur_file.get_api_path()); + EXPECT_EQ(fsi.source_path, cur_file.get_source_path()); + }); std::size_t bytes_written{}; - EXPECT_EQ(api_error::success, o.write(0u, data, bytes_written)); + EXPECT_EQ(api_error::success, file.write(0U, data, bytes_written)); const auto test_state = [&]() { - EXPECT_STREQ(source_path.c_str(), o.get_source_path().c_str()); + EXPECT_STREQ(source_path.c_str(), file.get_source_path().c_str()); - EXPECT_FALSE(o.can_close()); - EXPECT_TRUE(o.is_modified()); + EXPECT_FALSE(file.can_close()); + EXPECT_TRUE(file.is_modified()); - EXPECT_TRUE(o.get_read_state(0u)); - EXPECT_EQ(std::size_t(1u), o.get_read_state().size()); - EXPECT_EQ(data.size(), o.get_file_size()); + EXPECT_TRUE(file.get_read_state(0U)); + EXPECT_EQ(std::size_t(1u), file.get_read_state().size()); + EXPECT_EQ(data.size(), file.get_file_size()); }; test_state(); - o.close(); + file.close(); test_state(); - EXPECT_EQ(api_error::success, o.get_api_error()); + EXPECT_EQ(api_error::success, file.get_api_error()); EXPECT_TRUE(utils::file::file(fsi.source_path).exists()); } -TEST(open_file, write_new_file_multiple_chunks) { +TEST_F(open_file_test, write_new_file_multiple_chunks) { const auto source_path = test::generate_test_file_name("file_manager_open_file_test"); - mock_provider mp; - mock_upload_manager um; - - EXPECT_CALL(mp, is_read_only()).WillRepeatedly(Return(false)); + EXPECT_CALL(provider, is_read_only()).WillRepeatedly(Return(false)); filesystem_item fsi; fsi.api_path = "/test.txt"; - fsi.size = 0u; + fsi.size = 0U; fsi.source_path = source_path; - EXPECT_CALL(um, store_resume).WillOnce([&fsi](const i_open_file &o) { - EXPECT_EQ(fsi.api_path, o.get_api_path()); - EXPECT_EQ(fsi.source_path, o.get_source_path()); - }); - open_file o(test_chunk_size, 0U, fsi, mp, um); - test_closeable_open_file(o, false, api_error::success, 0u, source_path); + EXPECT_CALL(upload_mgr, store_resume) + .WillOnce([&fsi](const i_open_file &file) { + EXPECT_EQ(fsi.api_path, file.get_api_path()); + EXPECT_EQ(fsi.source_path, file.get_source_path()); + }); + open_file file(test_chunk_size, 0U, fsi, provider, upload_mgr); + test_closeable_open_file(file, false, api_error::success, 0U, source_path); data_buffer data = {10, 9, 8}; - EXPECT_CALL(mp, set_item_meta(fsi.api_path, _)) + EXPECT_CALL(provider, set_item_meta(fsi.api_path, _)) .WillOnce([&data](const std::string &, const api_meta_map &meta) -> api_error { EXPECT_NO_THROW(EXPECT_FALSE(meta.at(META_CHANGED).empty())); @@ -410,153 +440,159 @@ TEST(open_file, write_new_file_multiple_chunks) { return api_error::success; }); - EXPECT_CALL(um, remove_upload).WillOnce([&fsi](const std::string &api_path) { - EXPECT_EQ(fsi.api_path, api_path); - }); + EXPECT_CALL(upload_mgr, remove_upload) + .WillOnce([&fsi](const std::string &api_path) { + EXPECT_EQ(fsi.api_path, api_path); + }); - EXPECT_CALL(um, queue_upload).WillOnce([&fsi](const i_open_file &cur_file) { - EXPECT_EQ(fsi.api_path, cur_file.get_api_path()); - EXPECT_EQ(fsi.source_path, cur_file.get_source_path()); - }); + EXPECT_CALL(upload_mgr, queue_upload) + .WillOnce([&fsi](const i_open_file &cur_file) { + EXPECT_EQ(fsi.api_path, cur_file.get_api_path()); + EXPECT_EQ(fsi.source_path, cur_file.get_source_path()); + }); std::size_t bytes_written{}; - EXPECT_EQ(api_error::success, o.write(0u, data, bytes_written)); - EXPECT_EQ(api_error::success, o.write(test_chunk_size, data, bytes_written)); + EXPECT_EQ(api_error::success, file.write(0U, data, bytes_written)); + EXPECT_EQ(api_error::success, + file.write(test_chunk_size, data, bytes_written)); const auto test_state = [&]() { - EXPECT_STREQ(source_path.c_str(), o.get_source_path().c_str()); + EXPECT_STREQ(source_path.c_str(), file.get_source_path().c_str()); - EXPECT_FALSE(o.can_close()); - EXPECT_TRUE(o.is_modified()); + EXPECT_FALSE(file.can_close()); + EXPECT_TRUE(file.is_modified()); - EXPECT_EQ(std::size_t(2u), o.get_read_state().size()); - for (std::size_t i = 0u; i < 2u; i++) { - EXPECT_TRUE(o.get_read_state(i)); + EXPECT_EQ(std::size_t(2u), file.get_read_state().size()); + for (std::size_t i = 0U; i < 2u; i++) { + EXPECT_TRUE(file.get_read_state(i)); } - EXPECT_EQ(data.size() + test_chunk_size, o.get_file_size()); + EXPECT_EQ(data.size() + test_chunk_size, file.get_file_size()); }; test_state(); - o.close(); + file.close(); test_state(); - EXPECT_EQ(api_error::success, o.get_api_error()); + EXPECT_EQ(api_error::success, file.get_api_error()); EXPECT_TRUE(utils::file::file(fsi.source_path).exists()); } -TEST(open_file, resize_file_to_0_bytes) { - auto &rf = test::create_random_file(test_chunk_size * 4u); - const auto source_path = rf.get_path(); - rf.close(); +TEST_F(open_file_test, resize_file_to_0_bytes) { + auto &r_file = test::create_random_file(test_chunk_size * 4U); + const auto source_path = r_file.get_path(); + r_file.close(); - mock_provider mp; - mock_upload_manager um; - - EXPECT_CALL(mp, is_read_only()).WillRepeatedly(Return(false)); + EXPECT_CALL(provider, is_read_only()).WillRepeatedly(Return(false)); filesystem_item fsi; fsi.api_path = "/test.txt"; - fsi.size = test_chunk_size * 4u; + fsi.size = test_chunk_size * 4U; fsi.source_path = source_path; - open_file o(test_chunk_size, 0U, fsi, mp, um); - test_closeable_open_file(o, false, api_error::success, fsi.size, source_path); - EXPECT_CALL(mp, set_item_meta(fsi.api_path, _)) + EXPECT_EQ(api_error::success, cache_size_mgr::instance().expand(fsi.size)); + + open_file file(test_chunk_size, 0U, fsi, provider, upload_mgr); + test_closeable_open_file(file, false, api_error::success, fsi.size, + source_path); + EXPECT_CALL(provider, set_item_meta(fsi.api_path, _)) .WillOnce([](const std::string &, const api_meta_map &meta) -> api_error { EXPECT_NO_THROW(EXPECT_FALSE(meta.at(META_CHANGED).empty())); EXPECT_NO_THROW(EXPECT_FALSE(meta.at(META_MODIFIED).empty())); EXPECT_NO_THROW(EXPECT_FALSE(meta.at(META_SIZE).empty())); EXPECT_NO_THROW(EXPECT_FALSE(meta.at(META_WRITTEN).empty())); - EXPECT_EQ(std::size_t(0u), + EXPECT_EQ(std::size_t(0U), utils::string::to_size_t(meta.at(META_SIZE))); return api_error::success; }); - EXPECT_CALL(um, remove_upload).WillOnce([&fsi](const std ::string &api_path) { - EXPECT_EQ(fsi.api_path, api_path); - }); + EXPECT_CALL(upload_mgr, remove_upload) + .WillOnce([&fsi](const std ::string &api_path) { + EXPECT_EQ(fsi.api_path, api_path); + }); - EXPECT_CALL(um, queue_upload).WillOnce([&fsi](const i_open_file &cur_file) { - EXPECT_EQ(fsi.api_path, cur_file.get_api_path()); - EXPECT_EQ(fsi.source_path, cur_file.get_source_path()); - }); - EXPECT_CALL(um, store_resume).WillOnce([&fsi](const i_open_file &cur_file) { - EXPECT_EQ(fsi.api_path, cur_file.get_api_path()); - EXPECT_EQ(fsi.source_path, cur_file.get_source_path()); - }); + EXPECT_CALL(upload_mgr, queue_upload) + .WillOnce([&fsi](const i_open_file &cur_file) { + EXPECT_EQ(fsi.api_path, cur_file.get_api_path()); + EXPECT_EQ(fsi.source_path, cur_file.get_source_path()); + }); + EXPECT_CALL(upload_mgr, store_resume) + .WillOnce([&fsi](const i_open_file &cur_file) { + EXPECT_EQ(fsi.api_path, cur_file.get_api_path()); + EXPECT_EQ(fsi.source_path, cur_file.get_source_path()); + }); - EXPECT_EQ(api_error::success, o.resize(0u)); + EXPECT_EQ(api_error::success, file.resize(0U)); - EXPECT_EQ(std::size_t(0u), o.get_file_size()); - EXPECT_FALSE(o.can_close()); - EXPECT_TRUE(o.is_modified()); + EXPECT_EQ(std::size_t(0U), file.get_file_size()); + EXPECT_FALSE(file.can_close()); + EXPECT_TRUE(file.is_modified()); - EXPECT_EQ(std::size_t(0u), o.get_read_state().size()); + EXPECT_EQ(std::size_t(0U), file.get_read_state().size()); } -TEST(open_file, resize_file_by_full_chunk) { - auto &rf = test::create_random_file(test_chunk_size * 4u); - const auto source_path = rf.get_path(); - rf.close(); +TEST_F(open_file_test, resize_file_by_full_chunk) { + auto &r_file = test::create_random_file(test_chunk_size * 4U); + const auto source_path = r_file.get_path(); + r_file.close(); - mock_provider mp; - mock_upload_manager um; - - EXPECT_CALL(mp, is_read_only()).WillRepeatedly(Return(false)); + EXPECT_CALL(provider, is_read_only()).WillRepeatedly(Return(false)); filesystem_item fsi; fsi.api_path = "/test.txt"; - fsi.size = test_chunk_size * 4u; + fsi.size = test_chunk_size * 4U; fsi.source_path = source_path; - EXPECT_CALL(um, store_resume).WillOnce([&fsi](const i_open_file &o) { - EXPECT_EQ(fsi.api_path, o.get_api_path()); - EXPECT_EQ(fsi.source_path, o.get_source_path()); - }); + EXPECT_EQ(api_error::success, cache_size_mgr::instance().expand(fsi.size)); - open_file o(test_chunk_size, 0U, fsi, mp, um); - test_closeable_open_file(o, false, api_error::success, fsi.size, source_path); - EXPECT_CALL(mp, set_item_meta(fsi.api_path, _)) + EXPECT_CALL(upload_mgr, store_resume) + .WillOnce([&fsi](const i_open_file &file) { + EXPECT_EQ(fsi.api_path, file.get_api_path()); + EXPECT_EQ(fsi.source_path, file.get_source_path()); + }); + + open_file file(test_chunk_size, 0U, fsi, provider, upload_mgr); + test_closeable_open_file(file, false, api_error::success, fsi.size, + source_path); + EXPECT_CALL(provider, set_item_meta(fsi.api_path, _)) .WillOnce([](const std::string &, const api_meta_map &meta) -> api_error { EXPECT_NO_THROW(EXPECT_FALSE(meta.at(META_CHANGED).empty())); EXPECT_NO_THROW(EXPECT_FALSE(meta.at(META_MODIFIED).empty())); EXPECT_NO_THROW(EXPECT_FALSE(meta.at(META_SIZE).empty())); EXPECT_NO_THROW(EXPECT_FALSE(meta.at(META_WRITTEN).empty())); - EXPECT_EQ(std::size_t(test_chunk_size * 3u), + EXPECT_EQ(std::size_t(test_chunk_size * 3U), utils::string::to_size_t(meta.at(META_SIZE))); return api_error::success; }); - EXPECT_CALL(um, remove_upload).WillOnce([&fsi](const std::string &api_path) { - EXPECT_EQ(fsi.api_path, api_path); - }); + EXPECT_CALL(upload_mgr, remove_upload) + .WillOnce([&fsi](const std::string &api_path) { + EXPECT_EQ(fsi.api_path, api_path); + }); - EXPECT_CALL(um, queue_upload).WillOnce([&fsi](const i_open_file &cur_file) { - EXPECT_EQ(fsi.api_path, cur_file.get_api_path()); - EXPECT_EQ(fsi.source_path, cur_file.get_source_path()); - }); + EXPECT_CALL(upload_mgr, queue_upload) + .WillOnce([&fsi](const i_open_file &cur_file) { + EXPECT_EQ(fsi.api_path, cur_file.get_api_path()); + EXPECT_EQ(fsi.source_path, cur_file.get_source_path()); + }); - EXPECT_EQ(api_error::success, o.resize(test_chunk_size * 3u)); + EXPECT_EQ(api_error::success, file.resize(test_chunk_size * 3U)); - EXPECT_EQ(std::size_t(test_chunk_size * 3u), o.get_file_size()); - EXPECT_FALSE(o.can_close()); - EXPECT_TRUE(o.is_modified()); - EXPECT_EQ(std::size_t(3u), o.get_read_state().size()); + EXPECT_EQ(std::size_t(test_chunk_size * 3U), file.get_file_size()); + EXPECT_FALSE(file.can_close()); + EXPECT_TRUE(file.is_modified()); + EXPECT_EQ(std::size_t(3U), file.get_read_state().size()); } -TEST(open_file, can_add_handle) { +TEST_F(open_file_test, can_add_handle) { event_system::instance().start(); console_consumer c; const auto source_path = test::generate_test_file_name("file_manager_open_file_test"); - mock_provider mp; - mock_upload_manager um; - - EXPECT_CALL(mp, is_read_only()).WillRepeatedly(Return(false)); + EXPECT_CALL(provider, is_read_only()).WillRepeatedly(Return(false)); filesystem_item fsi; fsi.api_path = "/test.txt"; @@ -582,9 +618,9 @@ TEST(open_file, can_add_handle) { EXPECT_STREQ("1", ee.get_handle().get().c_str()); }); - EXPECT_CALL(mp, set_item_meta(fsi.api_path, META_SOURCE, _)) + EXPECT_CALL(provider, set_item_meta(fsi.api_path, META_SOURCE, _)) .WillOnce(Return(api_error::success)); - EXPECT_CALL(um, remove_resume) + EXPECT_CALL(upload_mgr, remove_resume) .WillOnce( [&fsi](const std::string &api_path, const std::string &source_path2) { EXPECT_EQ(fsi.api_path, api_path); @@ -594,7 +630,7 @@ TEST(open_file, can_add_handle) { event_capture capture( {"filesystem_item_opened", "filesystem_item_handle_opened"}); - open_file o(test_chunk_size, 0U, fsi, mp, um); + open_file o(test_chunk_size, 0U, fsi, provider, upload_mgr); #if defined(_WIN32) o.add(1u, {}); EXPECT_EQ(nullptr, o.get_open_data(1u).directory_buffer); @@ -608,17 +644,14 @@ TEST(open_file, can_add_handle) { event_system::instance().stop(); } -TEST(open_file, can_remove_handle) { +TEST_F(open_file_test, can_remove_handle) { event_system::instance().start(); console_consumer c; const auto source_path = test::generate_test_file_name("file_manager_open_file_test"); - mock_provider mp; - mock_upload_manager um; - - EXPECT_CALL(mp, is_read_only()).WillRepeatedly(Return(false)); + EXPECT_CALL(provider, is_read_only()).WillRepeatedly(Return(false)); filesystem_item fsi; fsi.api_path = "/test.txt"; @@ -644,13 +677,13 @@ TEST(open_file, can_remove_handle) { EXPECT_STREQ("1", ee.get_handle().get().c_str()); }); - EXPECT_CALL(um, remove_resume) + EXPECT_CALL(upload_mgr, remove_resume) .WillOnce( [&fsi](const std::string &api_path, const std::string &source_path2) { EXPECT_EQ(fsi.api_path, api_path); EXPECT_EQ(fsi.source_path, source_path2); }); - EXPECT_CALL(mp, set_item_meta(fsi.api_path, META_SOURCE, _)) + EXPECT_CALL(provider, set_item_meta(fsi.api_path, META_SOURCE, _)) .WillOnce(Return(api_error::success)); event_capture capture({ @@ -660,7 +693,7 @@ TEST(open_file, can_remove_handle) { "filesystem_item_closed", }); - open_file o(test_chunk_size, 0U, fsi, mp, um); + open_file o(test_chunk_size, 0U, fsi, provider, upload_mgr); #if defined(_WIN32) o.add(1u, {}); #else @@ -673,12 +706,13 @@ TEST(open_file, can_remove_handle) { event_system::instance().stop(); } -TEST(open_file, - can_read_locally_after_write_with_file_size_greater_than_existing_size) {} +TEST_F(open_file_test, + can_read_locally_after_write_with_file_size_greater_than_existing_size) { +} -TEST(open_file, test_valid_download_chunks) {} +TEST_F(open_file_test, test_valid_download_chunks) {} -TEST(open_file, test_full_download_with_partial_chunk) {} +TEST_F(open_file_test, test_full_download_with_partial_chunk) {} -TEST(open_file, source_is_read_after_full_download) {} +TEST_F(open_file_test, source_is_read_after_full_download) {} } // namespace repertory