diff options
author | Andreas Rogge <andreas.rogge@bareos.com> | 2022-10-14 17:18:23 +0300 |
---|---|---|
committer | Philipp Storz <philipp.storz@bareos.com> | 2022-11-03 19:26:50 +0300 |
commit | cf2d6b1ac3cf0e2185aa7a14ac3769f81c36009a (patch) | |
tree | 82c875d92cbba03c2c079bb89206362aa007d975 | |
parent | 8a477804fa43c6fd45df9abee52b30832e0af833 (diff) |
tests: add foreach_res and reload test
The new test does a foreach_res while another thread reloads the
configuration again and again.
-rw-r--r-- | core/src/tests/test_config_parser_dir.cc | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/core/src/tests/test_config_parser_dir.cc b/core/src/tests/test_config_parser_dir.cc index ea716e49c..501b4c8d8 100644 --- a/core/src/tests/test_config_parser_dir.cc +++ b/core/src/tests/test_config_parser_dir.cc @@ -31,6 +31,11 @@ #include "dird/dird_conf.h" #include "lib/output_formatter_resource.h" +#include <thread> +#include <condition_variable> +#include <chrono> +using namespace std::chrono_literals; + namespace directordaemon { static std::string sprintoutput{}; @@ -128,6 +133,63 @@ TEST(ConfigParser_Dir, bareos_configparser_tests) delete my_config; } +TEST(ConfigParser_Dir, foreach_res_and_reload) +{ + OSDependentInit(); + std::string path_to_config_file = std::string( + RELATIVE_PROJECT_SOURCE_DIR "/configs/bareos-configparser-tests"); + my_config = InitDirConfig(path_to_config_file.c_str(), M_ERROR_TERM); + my_config->ParseConfig(); + + [[maybe_unused]] auto do_reload = [](bool keep_config) { + auto backup = my_config->BackupResourcesContainer(); + my_config->ParseConfig(); + + me = (DirectorResource*)my_config->GetNextRes(R_DIRECTOR, nullptr); + my_config->own_resource_ = me; + ASSERT_NE(nullptr, me); + if (!keep_config) { + my_config->RestoreResourcesContainer(std::move(backup)); + ASSERT_NE(nullptr, me); + } + }; + + std::mutex cv_m; + std::condition_variable cv; + bool done = false; + + auto do_foreach_res = [&]() { + std::unique_lock lk(cv_m); + BareosResource* client; + client = (BareosResource*)0xfefe; + // auto handle = my_config->GetResourcesContainer(); + foreach_res (client, R_CLIENT) { + cv.wait(lk); + printf("%p %s\n", client->resource_name_, client->resource_name_); + } + done = true; + return; + }; + + auto reload_loop = [&]() { + std::unique_lock<std::mutex> lk(cv_m); + while (!done) { + lk.unlock(); + printf("Config reload\n"); + do_reload(true); + cv.notify_one(); + std::this_thread::sleep_for(10ms); + lk.lock(); + } + }; + + std::thread t1{do_foreach_res}, t2{reload_loop}; + t1.join(); + t2.join(); + + delete my_config; +} // namespace directordaemon + TEST(ConfigParser_Dir, runscript_test) { OSDependentInit(); |